Tuesday, September 26, 2006

Twisted, Python, C++, ADO, ODBC, etc

Just an intro. I'm Benry. I'm an R&D Engineer at a company that makes tracking devices in Hayward, CA. I will discuss small elements of my designs, and found code that should be more easily distributed and for my future reference. I am most comfortable with C++ and Python, although I can do anything in Assembly (know Motorola, PIC, Rabbit, and TI), SQL, VHDL, and the list continues. I'm a programmer. I'm not a nerd or a geek, but I love to see things work and to fix problems. I write a lot of bugs, but so does everyone. I'm open to intepretation and suggestions as I'm using this as a learning tool. Currently, I'm writing a COM server to encapsulate a Twisted server, so that I can import it into a C++ with MFC or CLI GUI.

So, welcome to my interpreted world of programming!

My first code (I'll edit the code tags later) is using INI files (obviously in Windows). I don't know of an easier way to store settings for software unless it's directly in the registry. When I was learning how to use Windows and write code in it, I was wondering why they didn't use .conf files. I didn't know about the purpose of the registry, but now know that it's because Windows used to force all programs to keep their settings in windows.ini. This was a bad idea, for obvious reasons, so they structured it a bit different, gave it an interface (regedit) and a couple COM interfaces, so now all programs should be writing to the registry. I refuse. I learned on a Solaris box, then a Redhat box, and was forced into learning Windows when I couldn't find a job in the Linux/UNIX world out of college. Anyway. I use INI files. The example I'm going to show today is something that I came up with in VC++ with MFC to check various paths for the INI file. I made sure to check the path of the executible, the default path used during development (i.e. C:\). If the INI file didn't exist in either of these places, it will prompt the user for where to find the damn thing. Once it's found, the global variable representing the path to the INI file (keys to the kingdom) is set, and program execution continues as normal. I did this in Python this morning, but it was a lot easier and a bit different.

So here's the OnInitDialog()called when the application loads the main dialog.

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

m_pDialog = NULL;

char strTemp[MAX_ARRAY];
::GetCurrentDirectory(MAX_ARRAY, strTemp);
m_sFilePath.Format("%s\\", strTemp);

m_sErrorPath = m_sFilePath;

CheckFilePath();
...
}

Everything with an "m_" is a class member.

The function CheckFilePath() is below:

BOOL CApplicationDlg::CheckFilePath()
{
CFile testFile;
CString sFileTemp;

if(!testFile.Open(m_sFilePath + "app.ini",CFile::modeReadWrite)){
if(!testFile.Open("C:\\app.ini", CFile::modeReadWrite)){
if(!testFile.Open("C:\\Program Files\\AppPath\\app.ini", CFile::modeReadWrite)){

testFile.Abort();
testFile.Flush();

AfxMessageBox("Please choose the path of app.ini.");
m_cmnDialog1.SetFilter("INI Files (*.ini)|*.ini|");
m_cmnDialog1.ShowOpen();

//Check if it was cancelled first
if(m_cmnDialog1.GetCancelError() || m_cmnDialog1.GetFileName() == ""){
return FALSE;
}

// Check for a valid path name, and repeat loop.

if(m_cmnDialog1.GetFileName() != ""){
sFileTemp = m_cmnDialog1.GetFileName();
testFile.Open(sFileTemp,CFile::modeReadWrite); // Used to get the path
sFileTemp = testFile.GetFileName(); // Get's the file name to strip
m_sFilePath = testFile.GetFilePath(); // from the full path
m_sFilePath.TrimRight(sFileTemp); // and store it in sFilePath
}
}
else{
m_sFilePath = "C:\\Program Files\\AppPath\\";
}
}
else{
m_sFilePath = "C:\\";
}
}

testFile.Abort();
return TRUE;
}