Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / Objective C

Date-Time Conversion Utility

Rate me:
Please Sign up or sign in to vote.
4.57/5 (14 votes)
21 Sep 20012 min read 232.6K   3.1K   40  
A simple app that converts to and between time_t, DATE, and regular date string expressions
// DTConverterDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DTConverter.h"
#include "DTConverterDlg.h"
#include "DlgOptions.h"

// Some helper functions
long GetIniEntry(LPCTSTR strBufferPara, LPCTSTR strEntry, CString& strValue, int iNum=0);
void SetIniEntry(CFile *pFile, LPCTSTR strEntry, LPCTSTR strValue);
CString STRF(LPCTSTR lpszFormat, ...);

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDTConverterDlg dialog

CDTConverterDlg::CDTConverterDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDTConverterDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDTConverterDlg)
	m_strEditTimeT = _T("");
	m_strEditOleDate = _T("");
	m_strEditString = _T("");
	m_strDays = _T("");
	m_strHours = _T("");
	m_strMinutes = _T("");
	m_strSeconds = _T("");
	m_strSpan = _T("");
	m_strTotalSec = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  m_bAutoPaste = FALSE;
  m_bAutoConvert = FALSE;
  m_iAutoPaste = AUTOPASTE_FULL;
  m_iAutoConvert = AUTOCONV_NONE;
  m_iLastPressedButton = 1;
}

void CDTConverterDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDTConverterDlg)
	DDX_Control(pDX, IDC_BUTTON_CONVERT_STRING, m_btnString);
	DDX_Control(pDX, IDC_BUTTON_CONVERT_PARTSSPAN, m_btnPartsSpan);
	DDX_Control(pDX, IDC_BUTTON_CONVERT_TIME_T, m_btnTimeT);
	DDX_Control(pDX, IDC_BUTTON_CONVERT_SPAN, m_btnSpan);
	DDX_Control(pDX, IDC_BUTTON_CONVERT_OLE_DATE, m_btnOleDate);
	DDX_Control(pDX, IDC_BUTTON_CONVERT_TOTALSEC, m_btnConvTotalSec);
	DDX_Control(pDX, IDC_EDIT_SPAN, m_edbSpan);
	DDX_Control(pDX, IDC_EDIT_STRING, m_edbString);
	DDX_Control(pDX, IDC_EDIT_OLE_DATE, m_edbOleDate);
	DDX_Control(pDX, IDC_EDIT_TIME_T, m_edbTimeT);
	DDX_Text(pDX, IDC_EDIT_TIME_T, m_strEditTimeT);
	DDX_Text(pDX, IDC_EDIT_OLE_DATE, m_strEditOleDate);
	DDX_Text(pDX, IDC_EDIT_STRING, m_strEditString);
	DDX_Text(pDX, IDC_EDIT_D, m_strDays);
	DDX_Text(pDX, IDC_EDIT_H, m_strHours);
	DDX_Text(pDX, IDC_EDIT_M, m_strMinutes);
	DDX_Text(pDX, IDC_EDIT_S, m_strSeconds);
	DDX_Text(pDX, IDC_EDIT_SPAN, m_strSpan);
	DDX_Text(pDX, IDC_EDIT_TOTALSEC, m_strTotalSec);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDTConverterDlg, CDialog)
	//{{AFX_MSG_MAP(CDTConverterDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_CONVERT_TIME_T, OnButtonConvertTimeT)
	ON_BN_CLICKED(IDC_BUTTON_CONVERT_OLE_DATE, OnButtonConvertOleDate)
	ON_BN_CLICKED(IDC_BUTTON_CONVERT_STRING, OnButtonConvertString)
	ON_BN_CLICKED(IDC_BUTTON_CONVERT_STRINGSPAN, OnButtonConvertSpanParts)
	ON_BN_CLICKED(IDC_BUTTON_CONVERT_SPAN, OnButtonConvertSpan)
	ON_BN_CLICKED(IDC_BUTTON_CONVERT_TOTALSEC, OnButtonConvertTotalsec)
	ON_EN_SETFOCUS(IDC_EDIT_SPAN, OnSetfocusEditSpan)
	ON_EN_SETFOCUS(IDC_EDIT_STRING, OnSetfocusEditString)
	ON_EN_SETFOCUS(IDC_EDIT_TIME_T, OnSetfocusEditTimeT)
	ON_EN_SETFOCUS(IDC_EDIT_OLE_DATE, OnSetfocusEditOleDate)
	ON_EN_SETFOCUS(IDC_EDIT_TOTALSEC, OnSetfocusEditTotalsec)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BTN_OPTIONS, OnBtnOptions)
	//}}AFX_MSG_MAP
  ON_MESSAGE(WM_ACTIVATEAPP, OnActivateApp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDTConverterDlg message handlers

BOOL CDTConverterDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
      pSysMenu->AppendMenu(MF_STRING, 0x0020, "Options");
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	if (GetFileAttributes(OPTIONSFILE)!=0xFFFFFFFF) ReadOptions();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDTConverterDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
  else if ((nID & 0xFFF0) == 0x0020)
    OnBtnOptions();
  else
		CDialog::OnSysCommand(nID, lParam);
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDTConverterDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDTConverterDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDTConverterDlg::OnClose() 
{
  SaveOptions();
	
	CDialog::OnClose();
}

///////////////////////////////////////////////////////////////////////////////////
// User-defined autopaste functions
///////////////////////////////////////////////////////////////////////////////////
//
void CDTConverterDlg::OnSetfocusEditSpan() 
{
  if (!m_bAutoPaste) return;
  if (m_iAutoPaste!=AUTOPASTE_HALF) return;

  CString strTmp = CutOffAt2ndSpace(GetClipboardText());
  COleDateTime dt;
  dt.ParseDateTime(strTmp);
  if (dt.GetStatus()==COleDateTime::valid)
  {
    SetDefaultButton();
    AfxMessageBox(IDS_ERR_INVALID_ENTRY);
    return;
  }
  else
  {
    strTmp = CutOffAt1stSpace(strTmp);
    if (atof(strTmp)==0.0)
    {
      SetDefaultButton();
      AfxMessageBox(IDS_ERR_INVALID_ENTRY);
      return;
    }
  }
  ClearAllDTSFields();
  m_strSpan = strTmp;
  m_bAutoPaste=FALSE;
  UpdateData(FALSE);

  if (m_bAutoConvert) OnButtonConvertSpan();
}

// -----------------------------------------------------------------------------------
void CDTConverterDlg::OnSetfocusEditOleDate() 
{
  if (!m_bAutoPaste) return;
  if (m_iAutoPaste!=AUTOPASTE_HALF) return;

  CString strTmp = CutOffAt2ndSpace(GetClipboardText());
  COleDateTime dt;
  dt.ParseDateTime(strTmp);
  if (dt.GetStatus()==COleDateTime::valid)
  {
    SetDefaultButton();
    AfxMessageBox(IDS_ERR_INVALID_ENTRY);
    return;
  }
  else
  {
    strTmp = CutOffAt1stSpace(strTmp);
    if (atof(strTmp)==0.0)
    {
      SetDefaultButton();
      AfxMessageBox(IDS_ERR_INVALID_ENTRY);
      return;
    }
  }

  ClearAllDTFields();
  m_strEditOleDate = strTmp;
  m_bAutoPaste=FALSE;
  UpdateData(FALSE);

  if (m_bAutoConvert) OnButtonConvertOleDate();
}

// -----------------------------------------------------------------------------------
void CDTConverterDlg::OnSetfocusEditString() 
{
  if (!m_bAutoPaste) return;
  if (m_iAutoPaste==AUTOPASTE_NONE || m_iAutoPaste==AUTOPASTE_FULL) return;

  CString strTmp = CutOffAt2ndSpace(GetClipboardText());
  COleDateTime dt;
  dt.ParseDateTime(strTmp);
  if (dt.GetStatus()==COleDateTime::invalid)
  {
    SetDefaultButton();
    AfxMessageBox(IDS_ERR_INVALID_ENTRY);
    return;
  }

  ClearAllDTFields();
  m_strEditString = strTmp;
  m_bAutoPaste=FALSE;
  UpdateData(FALSE);

  if (m_bAutoConvert) OnButtonConvertString();
}

// -----------------------------------------------------------------------------------
void CDTConverterDlg::OnSetfocusEditTimeT() 
{
  if (!m_bAutoPaste) return;
  if (m_iAutoPaste!=AUTOPASTE_HALF) return;

  CString strTmp = CutOffAt2ndSpace(GetClipboardText());
  COleDateTime dt;
  dt.ParseDateTime(strTmp);
  if (dt.GetStatus()==COleDateTime::valid)
  {
    SetDefaultButton();
    AfxMessageBox(IDS_ERR_INVALID_ENTRY);
    return;
  }
  else
  {
    strTmp = CutOffAt1stSpace(strTmp);
    if (atof(strTmp)==0.0)
    {
      SetDefaultButton();
      AfxMessageBox(IDS_ERR_INVALID_ENTRY);
      return;
    }
  }

  ClearAllDTFields();
	m_strEditTimeT = strTmp;
  m_bAutoPaste=FALSE;
  UpdateData(FALSE);

  if (m_bAutoConvert) OnButtonConvertTimeT();
}

// -----------------------------------------------------------------------------------
void CDTConverterDlg::OnSetfocusEditTotalsec() 
{
  UpdateData();
  if (!m_bAutoPaste) return;
  if (m_iAutoPaste!=AUTOPASTE_HALF) return;

  CString strTmp = CutOffAt2ndSpace(GetClipboardText());
  COleDateTime dt;
  dt.ParseDateTime(strTmp);
  if (dt.GetStatus()==COleDateTime::valid)
  {
    SetDefaultButton();
    AfxMessageBox(IDS_ERR_INVALID_ENTRY);
    return;
  }
  else
  {
    strTmp = CutOffAt1stSpace(strTmp);
    if (atof(strTmp)==0.0)
    {
      SetDefaultButton();
      AfxMessageBox(IDS_ERR_INVALID_ENTRY);
      return;
    }
  }

  ClearAllDTSFields();
	m_strTotalSec = strTmp;
  m_bAutoPaste=FALSE;
  UpdateData(FALSE);

  if (m_bAutoConvert) OnButtonConvertTotalsec();
}

///////////////////////////////////////////////////////////////////////////////////
// Convert functions
///////////////////////////////////////////////////////////////////////////////////
//
void CDTConverterDlg::OnButtonConvertTimeT() 
{
	UpdateData();

  m_iLastPressedButton = 1;
	COleDateTime dt(atol(m_strEditTimeT));
  if (dt.GetStatus()==COleDateTime::invalid)
  {
    AfxMessageBox(IDS_ERR_INVALID_ENTRY);
    m_edbTimeT.SetSel(0, -1);
    m_edbTimeT.SetFocus();
    return;
  }
	m_strEditOleDate.Format("%lf", dt.m_dt);

	m_strEditString = dt.Format();
	
	UpdateData(FALSE);
}

// -----------------------------------------------------------------------------------
void CDTConverterDlg::OnButtonConvertOleDate() 
{
	UpdateData();
	m_iLastPressedButton = 2;
	COleDateTime dt(atof(m_strEditOleDate));
  if (dt.GetStatus()==COleDateTime::invalid)
  {
    AfxMessageBox(IDS_ERR_INVALID_ENTRY);
    m_edbOleDate.SetSel(0, -1);
    m_edbOleDate.SetFocus();
    return;
  }
	m_strEditString = dt.Format();

	CTime t(dt.GetYear(), dt.GetMonth(), dt.GetDay(), dt.GetHour(), dt.GetMinute(), dt.GetSecond());
	m_strEditTimeT.Format("%d", t.GetTime());
  if (!m_strEditTimeT.Compare("-1"))
  {
    AfxMessageBox(IDS_ERR_INVALID_ENTRY);
    m_edbOleDate.SetSel(0, -1);
    m_edbOleDate.SetFocus();
    if (m_iAutoPaste==AUTOPASTE_HALF) m_bAutoPaste=TRUE;
    return;
  }
	
	UpdateData(FALSE);
}

// -----------------------------------------------------------------------------------
void CDTConverterDlg::OnButtonConvertString() 
{
	UpdateData();
	m_iLastPressedButton = 3;
	COleDateTime dt;
	dt.ParseDateTime(m_strEditString);
  if (dt.GetStatus()==COleDateTime::invalid)
  {
    AfxMessageBox(IDS_ERR_INVALID_ENTRY);
    m_edbString.SetSel(0, -1);
    m_edbString.SetFocus();
    return;
  }
	m_strEditOleDate.Format("%lf", dt.m_dt);

	CTime t(dt.GetYear(), dt.GetMonth(), dt.GetDay(), dt.GetHour(), dt.GetMinute(), dt.GetSecond());
	m_strEditTimeT.Format("%d", t.GetTime());

	UpdateData(FALSE);
}

// -----------------------------------------------------------------------------------
void CDTConverterDlg::OnButtonConvertSpan() 
{
	UpdateData();
  m_iLastPressedButton = 4;
  COleDateTimeSpan odts(atof(m_strSpan));

  m_strDays.Format("%ld", odts.GetDays());
  m_strHours.Format("%ld", odts.GetHours());
  m_strMinutes.Format("%ld", odts.GetMinutes());
  m_strSeconds.Format("%ld", odts.GetSeconds());
  m_strTotalSec.Format("%.0f", odts.GetTotalSeconds());

  UpdateData(FALSE);
}

// -----------------------------------------------------------------------------------
void CDTConverterDlg::OnButtonConvertTotalsec() 
{
	UpdateData();
  m_iLastPressedButton = 5;
  COleDateTimeSpan odts(0,0,0,atol(m_strTotalSec));

  m_strDays.Format("%ld", odts.GetDays());
  m_strHours.Format("%ld", odts.GetHours());
  m_strMinutes.Format("%ld", odts.GetMinutes());
  m_strSeconds.Format("%ld", odts.GetSeconds());
  m_strSpan.Format("%f", odts.m_span);

  UpdateData(FALSE);
}

// -----------------------------------------------------------------------------------
void CDTConverterDlg::OnButtonConvertSpanParts() 
{
	UpdateData();
  m_iLastPressedButton = 6;
  COleDateTimeSpan odts(atol(m_strDays), atol(m_strHours), atol(m_strMinutes), atol(m_strSeconds));
  m_strSpan.Format("%f", odts.m_span);
  m_strTotalSec.Format("%.0f", odts.GetTotalSeconds());

  UpdateData(FALSE);
}

///////////////////////////////////////////////////////////////////////////////////
// Helper functions
///////////////////////////////////////////////////////////////////////////////////
//
CString CDTConverterDlg::GetClipboardText()
{
  CString strTxt;
  if (!IsClipboardFormatAvailable(CF_TEXT)) return strTxt;

  if (!OpenClipboard()) return strTxt;

  HGLOBAL   hglb; 
  LPTSTR    lptstr;
  hglb = GetClipboardData(CF_TEXT); 
  if (hglb != NULL) 
  { 
    lptstr = (char *)GlobalLock(hglb); 
    if (lptstr != NULL) 
    { 
      strTxt = CString(lptstr);
      GlobalUnlock(hglb); 
    } 
  } 
  CloseClipboard(); 
  return strTxt;
}

// -----------------------------------------------------------------------------------
void CDTConverterDlg::OnActivateApp( BOOL bActive, HTASK hTask )
{
  if (bActive)
  {
    if (m_iAutoPaste!=AUTOPASTE_FULL) return;
    if (!m_bAutoPaste) return;

    m_bAutoPaste = FALSE;
    CString strTmp = CutOffAt2ndSpace(GetClipboardText());
    COleDateTime dt;
    dt.ParseDateTime(strTmp);
    if (dt.GetStatus()==COleDateTime::valid)
    {
      TRACE("It's a time format. Paste it.\n");
      ClearAllDTSFields();
      ClearAllDTFields();
      m_strEditString = strTmp;
      UpdateData(FALSE);
      if (m_iAutoConvert==AUTOCONV_STRING) OnButtonConvertString();
    }
    else
    {
      strTmp = CutOffAt1stSpace(strTmp);
      if (atof(strTmp)==0.0) return;
      TRACE("Valid format so paste it\n");
      ClearAllDTSFields();
      ClearAllDTFields();
      switch(m_iAutoConvert)
      {
        case AUTOCONV_NONE:
          m_strEditTimeT = strTmp;
          m_strEditOleDate = strTmp;
          m_strSpan = strTmp;
          m_strTotalSec = strTmp;
          UpdateData(FALSE);
          break;
        case AUTOCONV_TIMET:
          m_strEditTimeT = strTmp;
          UpdateData(FALSE);
          OnButtonConvertTimeT();
          break;
        case AUTOCONV_DATE:
          m_strEditOleDate = strTmp;
          UpdateData(FALSE);
          OnButtonConvertOleDate();
          break;
        case AUTOCONV_SPAN:
          m_strSpan = strTmp;
          UpdateData(FALSE);
          OnButtonConvertSpan();
          break;
        case AUTOCONV_TOTALSEC:
          m_strTotalSec = strTmp;
          UpdateData(FALSE);
          OnButtonConvertTotalsec();
          break;
      }
    }
  }
  else
  {
    SetDefaultButton();
    m_bAutoPaste=TRUE;
  }
}

// -----------------------------------------------------------------------------------
void CDTConverterDlg::ClearAllDTFields(void)
{
	m_strEditTimeT.Empty();
	m_strEditOleDate.Empty();
	m_strEditString.Empty();
}

// -----------------------------------------------------------------------------------
void CDTConverterDlg::ClearAllDTSFields(void)
{
	m_strDays.Empty();
	m_strHours.Empty();
	m_strMinutes.Empty();
	m_strSeconds.Empty();
	m_strSpan.Empty();
	m_strTotalSec.Empty();
}

// -----------------------------------------------------------------------------------
CString CDTConverterDlg::CutOffAt2ndSpace(LPCTSTR strInputPara)
{
  CString strInput(strInputPara);

  // More than one space?
  int iFirstSpace = strInput.Find(' ');
  CString strSecondPart = strInput.Mid(iFirstSpace+1);
  int iSecondSpace = strSecondPart.Find(' ');
  if ( iSecondSpace!=-1 ) return strInput.Left(strInput.Find(' ')+1+iSecondSpace);
  else return strInput;
}

// -----------------------------------------------------------------------------------
CString CDTConverterDlg::CutOffAt1stSpace(LPCTSTR strInputPara)
{
  CString strInput(strInputPara);

  // Space?
  int iSpace = strInput.Find(' ');
  if ( iSpace!=-1 ) return strInput.Left(iSpace);
  else return strInput;
}

// -----------------------------------------------------------------------------------
void CDTConverterDlg::SetDefaultButton(void)
{
  switch (m_iLastPressedButton)
  {
    case 1: m_btnTimeT.SetFocus();        break;
    case 2: m_btnOleDate.SetFocus();      break;
    case 3: m_btnString.SetFocus();       break;
    case 4: m_btnSpan.SetFocus();         break;
    case 5: m_btnConvTotalSec.SetFocus(); break;
    case 6: m_btnPartsSpan.SetFocus();    break;
  }
}

void CDTConverterDlg::ReadOptions(void)
{
  CFile f;
  CFileException e;

  if (!f.Open(OPTIONSFILE, CFile::modeRead, &e))
  {
    AfxMessageBox(STRF("Error opening file %s. Options could not be read in.", OPTIONSFILE));
	  return;
  }

  UINT uiNrOfBytes = 1000;
  char pBuff[1000];

  try
  {
    uiNrOfBytes = f.Read(pBuff, uiNrOfBytes);
    pBuff[uiNrOfBytes] = '\0';
  }
  catch (CFileException *e)
  {
    AfxMessageBox(STRF("Error reading file %s. Options could not be read in.", OPTIONSFILE));
	  e->Delete();
  }
  f.Close();

  // and read the file's content
  //
  CString strBuffer(pBuff);
  CString strTmp;

	if ( !GetIniEntry(strBuffer, OPTION_AUTOPASTE, strTmp) )
    m_iAutoPaste = atoi(strTmp);
  if ( !GetIniEntry(strBuffer, OPTION_AUTOCONVERT, strTmp) )
    m_bAutoConvert = strTmp.CompareNoCase("N");
	if ( !GetIniEntry(strBuffer, OPTION_FULLAUTOCONVERT, strTmp) )
    m_iAutoConvert = atoi(strTmp);
}

void CDTConverterDlg::SaveOptions(void)
{
  CFile f;
  CFileException e;

  if (!f.Open(OPTIONSFILE, CFile::modeCreate | CFile::modeWrite, &e))
  {
    AfxMessageBox(STRF("Error opening file %s. Options not saved.", OPTIONSFILE));
	  return;
  }

  try
  {
    CString strTmp;
    strTmp.Format("%d", m_iAutoPaste);
    SetIniEntry(&f, OPTION_AUTOPASTE, strTmp);
    strTmp.Format("%s", m_bAutoConvert ? "Y":"N");
    SetIniEntry(&f, OPTION_AUTOCONVERT, strTmp);
    strTmp.Format("%d", m_iAutoConvert);
    SetIniEntry(&f, OPTION_FULLAUTOCONVERT, strTmp);
  }
  catch (CFileException *e)
  {
    AfxMessageBox(STRF("Error writing file %s. Options not saved.", OPTIONSFILE));
	  e->Delete();
  }

  f.Close();
}

// Helper funcs for writing the settings in an INI file
//
long GetIniEntry(LPCTSTR strBufferPara, 
                 LPCTSTR strEntryPara, 
                 CString& strValue,
                 int iNum)
{
  CString strBuffer = strBufferPara;
  CString strEntry  = strEntryPara;

	// find iNum-th occurence (0, 1, 2,...)
	int iCount = -1, pos;
	CString strLine, strName;
	
	while ((pos = strBuffer.Find('\n')) != -1)
	{
		strLine = strBuffer.Left(pos-1);
		strBuffer = strBuffer.Right(strBuffer.GetLength()-pos-1); // rest
		if (strLine.Find('=') == -1) continue; // next line
		strName = strLine.Left(strLine.Find('='));
		if (strName == strEntry)
		{
			iCount++;
			strValue = strLine.Right(strLine.GetLength()-strLine.Find('=')-1);
			if (iCount == iNum) return 0;
		}
	}
	// last line
	strLine = strBuffer;
	if (strLine.Find('=') == -1) return 1;
	
	strName = strLine.Left(strLine.Find('='));
	if (strName == strEntry)
	{
		iCount++;
		strValue = strLine.Right(strLine.GetLength()-strLine.Find('=')-1);
		if (iCount == iNum) return 0;
	}	

	return 1;
}

// ------------------------------------------------------------------------------------
void SetIniEntry(CFile *pFile, LPCTSTR strEntry, LPCTSTR strValue)
{
	CString strLine = CString(strEntry) + "=" + CString(strValue) + "\r\n";

	pFile->Write(strLine, strLine.GetLength()); 
}

// ------------------------------------------------------------------------------------
CString STRF(LPCTSTR lpszFormat, ...)
{
	CString s;

	va_list argList;
	va_start(argList, lpszFormat);
	s.FormatV(lpszFormat, argList);
	return s;
}

// ------------------------------------------------------------------------------------
void CDTConverterDlg::OnBtnOptions() 
{
	CDlgOptions dlgOptions;
  dlgOptions.SetAutoPaste(m_iAutoPaste);
  dlgOptions.SetAutoConvert(m_bAutoConvert);
  dlgOptions.SetFullAutoConvert(m_iAutoConvert);
	if (dlgOptions.DoModal()==IDOK)
  {
    m_iAutoPaste = dlgOptions.GetAutoPaste();
    m_bAutoConvert = dlgOptions.GetAutoConvert();
    m_iAutoConvert = dlgOptions.GetFullAutoConvert();
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Web Developer
United States United States
I've done extensive work with C++, MFC, COM, and ATL on the Windows side. On the Web side, I've worked with VB, ASP, JavaScript, and COM+. I've also been involved with server-side Java, which includes JSP, Servlets, and EJB, and more recently with ASP.NET/C#.

Comments and Discussions