Rev 1 | Blame | Compare with Previous | Last modification | View Log
// INIFile.cpp: implementation of the INIFile class.////////////////////////////////////////////////////////////////////////#pragma warning( disable : 4996 )#include "stdafx.h"#include "INIFile.h"//////////////////////////////////////////////////////////////////////// Globals//////////////////////////////////////////////////////////////////////int CIniFile::m_iOutLen = CINIFILE_BUFLEN;//char CIniFile::Text[ BUFLEN];//int CIniFile::m_iCopy = 0;//////////////////////////////////////////////////////////////////////// Construction/Destruction////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// CIniFile::CIniFile( char *FileName, char *Dir)//// Constructor//////////////////////////////////////////////////////////////////////CIniFile::CIniFile( char *FileName, char *Dir){char temp[ _MAX_PATH];SECURITY_ATTRIBUTES sa = { sizeof( SECURITY_ATTRIBUTES),NULL,TRUE };//invalid semaphore handlem_hSemaphore = NULL;//build the buffers for keys and sections etc.Text = new char [299];m_cDefault = new char [m_iOutLen];m_cKey = new char [m_iOutLen];m_cSection = new char [m_iOutLen];//save the filenameif (Dir == NULL)strcat( strcat( strcpy( m_cFileName, _getcwd( temp, _MAX_PATH )), "\\"), FileName);elsestrcat( strcat( strcpy( m_cFileName, Dir), "\\"), FileName);//clear the sectionm_cSection[ 0] = '\0';m_cDefault[ 0] = '\0';m_iCopy = 1;//get a unnamed semaphoreif ((m_hSemaphore = CreateSemaphore( &sa, 1, 1, NULL)) == NULL)return;}//////////////////////////////////////////////////////////////////////// CIniFile::~CIniFile()//// Destructor//////////////////////////////////////////////////////////////////////CIniFile::~CIniFile(){//reduce the count of copies, the last one will free our resourcesif (m_iCopy)if(--m_iCopy) return;//delete char buffersdelete [] Text;delete [] m_cDefault;delete [] m_cKey;delete [] m_cSection;if (m_hSemaphore)CloseHandle( m_hSemaphore);}/////////////////////////////////////////////////////////////////////////////// CIniFile::CIniFile( const INIFile &)//// Copy constructor/////////////////////////////////////////////////////////////////////////////CIniFile::CIniFile( const CIniFile &obj){//copy the valuesText = obj.Text;m_cDefault = obj.m_cDefault;strcpy( m_cFileName, obj.m_cFileName);m_cKey = obj.m_cKey;m_cSection = obj.m_cSection;m_hSemaphore = obj.m_hSemaphore;m_iCopy++;}//////////////////////////////////////////////////////////////////////// const char *CIniFile::GetString( const char *Key, const char *Section, const char *Default)//// Get a string from the ini-file//////////////////////////////////////////////////////////////////////const char *CIniFile::GetString( const char *Key, const char *Section, const char *Default){//clear the output textText[ 0] = '\0';if ((Key == NULL) || !strlen(Key))return Text;//get the default stringif (Default == NULL)m_cDefault[ 0] ='\0';elsestrcpy( m_cDefault, Default);//get the section nameif (Section != NULL)strcpy( m_cSection, Section);//get the keystrcpy( m_cKey, Key);//lock the fileLock();//get the stringGetPrivateProfileString( m_cSection, m_cKey, Text, Text, m_iOutLen, m_cFileName);//write the default stringif (!strlen( Text))WritePrivateProfileString(m_cSection, m_cKey, strcpy( Text, m_cDefault), m_cFileName);//release the file for other usersUnLock();//return a pointer to the stringreturn Text;}//////////////////////////////////////////////////////////////////////// void CIniFile::PutString( char *Input, char *Key, char *Section)//// Write a string in the ini-file//////////////////////////////////////////////////////////////////////void CIniFile::PutString( char *Input, char *Key, char *Section){//get the section nameif (Section != NULL)strcpy( m_cSection, Section);//get the keystrcpy( m_cKey, Key);//lock the fileLock();//write the string in the ini fileWritePrivateProfileString(m_cSection, m_cKey, Input, m_cFileName);//release the file for other usersUnLock();}//////////////////////////////////////////////////////////////////////// int CIniFile::GetInteger(char *Key, char *Section, int Default)//// Get an integer from the ini-file//////////////////////////////////////////////////////////////////////int CIniFile::GetInteger(char *Key, char *Section, int Default){char temp[ CINIFILE_BUFLEN];//make sure the default is integerstrcpy( m_cDefault, itoa( Default, temp, CINIFILE_DECIMAL));//read first teh stringGetString( Key, Section, m_cDefault);//convert string to intreturn atoi( Text);}//////////////////////////////////////////////////////////////////////// void CIniFile::PutInteger(int Number, char *Key, char *Section)//// Write a string in the ini-file//////////////////////////////////////////////////////////////////////void CIniFile::PutInteger(int Number, char *Key, char *Section){char temp[ CINIFILE_BUFLEN];PutString( itoa( Number, temp, CINIFILE_DECIMAL), Key, Section);}//////////////////////////////////////////////////////////////////////// int CIniFile::Lock()//// Lock the ini-file for exclusive use//////////////////////////////////////////////////////////////////////int CIniFile::Lock(){//check for valid semaphoreif ((m_hSemaphore == NULL) || (m_hSemaphore == INVALID_HANDLE_VALUE))return FALSE;//wait untill port is releasedwhile (WaitForSingleObject( m_hSemaphore, INFINITE) != WAIT_OBJECT_0) ;return TRUE;}//////////////////////////////////////////////////////////////////////// int CIniFile::UnLock()//// Unlock the ini-file from exclusive use//////////////////////////////////////////////////////////////////////int CIniFile::UnLock(){//check for valid semaphoreif ((m_hSemaphore == NULL) || (m_hSemaphore == INVALID_HANDLE_VALUE))return FALSE;//release the com port for internal useif (!ReleaseSemaphore( m_hSemaphore, 1, NULL))return FALSE;return TRUE;}