#include "StdAfx.h" #include "initools.h" #include "aecdbobjxdatamap.h" #include "earthwormdef.h" #define SP_PT TCHAR(0x07) // ÏìÁå bel ×öΪ·Ö¸î·û #pragma region IniTools bool IniTools::getSections(LPCTSTR szPath, std::vector §ions) { if (_taccess(szPath, 0x0) != 0x0) return false; DWORD dw(0), len(MAX_PATH); TCHAR *buf = new TCHAR[len]; ::memset(buf, 0, sizeof(TCHAR) * len); dw = ::GetPrivateProfileSectionNames(buf, len, szPath); while (dw == len - 2) { DELETE_ARR_(buf); len += MAX_PATH; buf = new TCHAR[len]; ::memset(buf, 0, sizeof(TCHAR) * len); dw = ::GetPrivateProfileSectionNames(buf, len, szPath); } TCHAR *psz(buf); while ((len = (DWORD)_tcslen(psz))) { sections.push_back(psz); psz += len + 1; } DELETE_ARR_(buf); return !sections.empty(); } bool IniTools::getSectionContent(LPCTSTR szPath, LPCTSTR szSection, std::map &mapV) { SH_BIM_BLOCK bims; getSectionContent(szPath, szSection, bims); for (size_t i = 0; i < bims.size(); ++i) { mapV.insert(bims[i]); } return !mapV.empty(); } bool IniTools::getSectionContent(LPCTSTR szPath, LPCTSTR szSection, SH_BIM_BLOCK &mapV) { if (_taccess(szPath, 0x0) != 0x0) return false; DWORD dw(0), len(MAX_PATH); TCHAR *buf = new TCHAR[len]; ::memset(buf, 0, sizeof(TCHAR) * len); dw = ::GetPrivateProfileSection(szSection, buf, len, szPath); while (dw == len - 2) { DELETE_ARR_(buf); len += MAX_PATH; buf = new TCHAR[len]; ::memset(buf, 0, sizeof(TCHAR) * len); dw = ::GetPrivateProfileSection(szSection, buf, len, szPath); } TCHAR *psz(buf); mapV.clear(); std::map::const_iterator it; int idx(-1); CString str(_T("")), pre(_T("")), back(_T("")); while ((len = (DWORD)_tcslen(psz))) { str = psz; idx = str.Find(_T('=')); if (idx != -1) { pre = str.Left(idx); back = str.Right(str.GetLength() - idx - 1); mapV.push_back(std::make_pair(pre, back)); } psz += len + 1; } DELETE_ARR_(buf); return !mapV.empty(); } bool IniTools::setSectionContent(LPCTSTR szPath, LPCTSTR szSection, const std::map &mapV) { if (!szSection) return false; //if (_taccess(szPath, 0x0) != 0x0) return false; // Æ´×Ö·û´® std::map::const_iterator it = mapV.begin(); std::wstring ss; for (; it != mapV.end(); ++it) { ss += it->first; ss += _T("="); ss += it->second; ss.push_back(_T('\0')); } ss.push_back(_T('\0')); return ::WritePrivateProfileSection(szSection, ss.c_str(), szPath) == FALSE ? false : true; } #pragma endregion