아나 감동의 쓰나미.. .ini 에서 등록한 키워드 읽어오기 굳이 ini아니라도 되지만..-_-..
1. MFC에서 CMapStringToString 클래스를 사용하여 읽어오기
CMapStringToString m_hashTable;
void ReadPrivateProfile()
{
char tmpStr[KEY_SIZE], cbExeDir[256];
GetModuleFileName(NULL, cbExeDir, 256);
cbExeDir[strlen(cbExeDir) - 3] = '\0';
strcat(cbExeDir, "ini");
if( GetPrivateProfileString("C/C++", "KEYWORD", NULL, tmpStr, KEY_SIZE, cbExeDir ))
ReadKeywod(tmpStr);
}
void ReadKeywod(char* key)
{
char* seps = ",";
char* token = strtok(key, seps);
while(token != NULL)
{
AddKeyword_HashItem(token);
token = strtok(NULL, seps);
}
}
void AddKeyword_HashItem(char* keyword)
{
UINT b = m_hashTable.HashKey((const char*)keyword);
CString str;
str.Format("%d", b);
m_hashTable.SetAt(keyword, str);
}
2. CIniFile 클래스를 사용해서 읽어오기 ( 래핑 클래스 )
1. IniFile.cpp
#include "stdafx.h"
#include "IniFile.h"
CIniFile::CIniFile(VOID)
{
ZeroMemory(mFileName, sizeof(mFileName));
}
CIniFile::~CIniFile(VOID)
{
}
BOOL CIniFile::Open(LPCTSTR fileName)
{
if (!fileName)
return FALSE;
_tcsncpy(mFileName, fileName, MAX_PATH);
return TRUE;
}
BOOL CIniFile::Close(VOID)
{
return TRUE;
}
BOOL CIniFile::GetValue(LPCTSTR keyName, LPCTSTR valueName, LPDWORD value)
{
if (!keyName || !valueName || !value)
return FALSE;
*value = GetPrivateProfileInt(keyName, valueName, 0, mFileName);
return TRUE;
}
BOOL CIniFile::GetValue(LPCTSTR keyName, LPCTSTR valueName, FLOAT *value)
{
if (!keyName || !valueName || !value)
return FALSE;
TCHAR Value[16] = {0,};
GetPrivateProfileString(keyName, valueName, _T(""), Value, 16, mFileName);
*value = (FLOAT) _tstof(Value);
return TRUE;
}
BOOL CIniFile::GetValue(LPCTSTR keyName, LPCTSTR valueName, LPTSTR value, LPDWORD bufferLength)
{
if (!keyName || !valueName || !value || !bufferLength)
return FALSE;
*bufferLength = GetPrivateProfileString(keyName, valueName, _T(""), value, *bufferLength, mFileName);
return TRUE;
}
BOOL CIniFile::SetValue(LPCTSTR keyName, LPCTSTR valueName, DWORD value)
{
if (!keyName || !valueName)
return FALSE;
TCHAR Value[16] = {0,};
_itot(value, Value, 10);
WritePrivateProfileString(keyName, valueName, Value, mFileName);
return TRUE;
}
BOOL CIniFile::SetValue(LPCTSTR keyName, LPCTSTR valueName, LPCTSTR value)
{
if (!keyName || !valueName || !value)
return FALSE;
WritePrivateProfileString(keyName, valueName, value, mFileName);
return TRUE;
}
BOOL CIniFile::SetValue(LPCTSTR keyName, LPCTSTR valueName, FLOAT value)
{
if (!keyName || !valueName)
return FALSE;
TCHAR Value[16] = {0,};
_stprintf(Value, _T("%f"), value);
WritePrivateProfileString(keyName, valueName, Value, mFileName);
return TRUE;
}
2. InitFile.h
#pragma once
class CIniFile
{
public:
CIniFile(VOID);
virtual ~CIniFile(VOID);
private:
TCHAR mFileName[MAX_PATH];
public:
BOOL Open(LPCTSTR fileName);
BOOL Close(VOID);
BOOL SetValue(LPCTSTR keyName, LPCTSTR valueName, LPCTSTR value);
BOOL SetValue(LPCTSTR keyName, LPCTSTR valueName, DWORD value);
BOOL SetValue(LPCTSTR keyName, LPCTSTR valueName, FLOAT value);
BOOL GetValue(LPCTSTR keyName, LPCTSTR valueName, LPTSTR value, LPDWORD bufferLength);
BOOL GetValue(LPCTSTR keyName, LPCTSTR valueName, LPDWORD value);
BOOL GetValue(LPCTSTR keyName, LPCTSTR valueName, FLOAT *value);
};