#include "stdafx.h" #include "SHLConfig.h" #include <direct.h> #include <stdio.h> #include <stdlib.h> const int MAX_PATH_LEN = 256; const char* MAX_SEG_UNIT = "maxsegunit"; const char* USER_DICT = "userdict"; const char* FOR_FTI = "forfti"; extern struct SHlConfig g_cfgHlTb; bool LoadConfig(void) { bool bResult = false; char* pchPath = GetCurrentPath(); if (NULL != pchPath) { char* pchFileContent = ReadConfigFile(pchPath); if (NULL != pchFileContent) { g_cfgHlTb.nMaxSegM = GetCfgValue(pchFileContent, MAX_SEG_UNIT); g_cfgHlTb.bUserdict = GetCfgValue(pchFileContent, USER_DICT); g_cfgHlTb.bForFti = GetCfgValue(pchFileContent, FOR_FTI); if (-1 != g_cfgHlTb.nMaxSegM && -1 != g_cfgHlTb.bUserdict && -1 != g_cfgHlTb.bForFti) { if (NULL != pchPath) { delete pchPath; pchPath = NULL; } if (NULL != pchFileContent) { delete pchFileContent; pchFileContent = NULL; } bResult = true; } } else { if (NULL != pchPath) { delete pchPath; pchPath = NULL; } } } return bResult; } char* GetCurrentPath() { char* pchPathBuf = new char[(sizeof(char) * MAX_PATH_LEN)]; if (NULL != pchPathBuf) { pchPathBuf[MAX_PATH_LEN - 1] = '/0'; pchPathBuf = _getcwd(NULL, MAX_PATH_LEN); if (NULL != pchPathBuf) { sprintf(pchPathBuf, "%s//config.ini", pchPathBuf); } } return pchPathBuf; } char* ReadConfigFile(char* pchConfigPath) { char* pchFileContent = NULL; FILE* fp = NULL; fp = fopen(pchConfigPath, "a+"); if (NULL != fp) { fseek(fp, 0, SEEK_END); a_sql_uint32 len = ftell(fp); fseek(fp, 0, SEEK_SET); if (len > 0) { pchFileContent = new char[len + 1]; if (NULL != pchFileContent) { pchFileContent[len] = '/0'; size_t nReadLen = fread(pchFileContent, sizeof(char), len, fp); if (nReadLen > 0) { pchFileContent[nReadLen] = '/0'; } } } } fclose(fp); return pchFileContent; } a_sql_uint32 GetCfgValue(const char* pchCfgContent, const char* pchName, a_sql_uint32 nDefaultValue) { if (NULL == pchCfgContent || NULL == pchName) { return -1; } a_sql_uint32 nNameLen = strlen(pchName); a_sql_uint32 nContentLen = strlen(pchCfgContent); if (nContentLen <= nNameLen) { return nDefaultValue; } const char* pchTemp = pchCfgContent; const char* pChEnd = pchCfgContent + nContentLen - nNameLen - 1 ; while (pchTemp < pChEnd) { if(memcmp(pchTemp, pchName, nNameLen) == 0 && pchTemp[nNameLen] == '=') { //matched pchTemp += nNameLen + 1; return atoi(pchTemp); } //skip this line char ch = *pchTemp; while(ch && (ch != '/n')) { pchTemp++; ch =*pchTemp; } if(ch == '/n') pchTemp++; } return nDefaultValue; }