GProfile::GProfile(const char *szBuffer, __int64 dwSize, bool bIsXML/* = 0*/) : m_objectContainer("configuration") { m_bIsXML = bIsXML; m_pTreeNotify = new GBTree(); if (m_bIsXML) { m_objectContainer.AddReference(GProfileSection::GetStaticTag(), &m_lstSections, &gGListHandler,0,1); try { m_objectContainer.FromXML(szBuffer); // note: now [m_lstSections] contains the GProfileSection objects m_bCached = true; } catch(GException &) { // Failed to open a configuration file for this application } catch(...) { // Failed to open a configuration file for this application } } else { ProfileParse(szBuffer, dwSize); } }
void GProfile::Load() { if (m_bCached) return; // destroy the cached objects Destroy(); char *szBuffer = 0; long dwSize = 0; #ifdef _WIN32 // open the file HANDLE hFile = CreateFile((const char *)m_strFile, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); dwSize = GetFileSize(hFile, NULL); szBuffer = new char[dwSize + 1]; // read the file long dwRead; if (!ReadFile(hFile, szBuffer, dwSize, (DWORD *)&dwRead, 0)) { delete [] szBuffer; CloseHandle(hFile); } // close the file CloseHandle(hFile); #else // open the file GString strTemp; int nResult = strTemp.FromFile((const char *)m_strFile, 0); szBuffer = new char[strTemp.Length() + 1]; memcpy(szBuffer,(const char *)strTemp, strTemp.Length()); dwSize = strTemp.Length(); #endif // terminate the buffer //szBuffer[dwSize] = 0; ProfileParse(szBuffer, dwSize); delete [] szBuffer; }
// // load the profile configuration file yourself, // and create this object "with no disk config file" GProfile::GProfile(const char *szBuffer, long dwSize) { ProfileParse(szBuffer, dwSize); }
void GProfile::Load() { // if there is no external config file, there is nothing to load. if (m_strFile.IsEmpty() || m_strFile == "NONE") return; if (m_bCached) return; // destroy the cached objects Destroy(); if (m_bIsXML) { //FromXMLFile(m_strFile); m_objectContainer.FromXMLFile(m_strFile); m_bCached = true; return; } char *szBuffer = 0; long dwSize = 0; #if defined(_WIN32) && !defined(__WINPHONE) try { // open the file HANDLE hFile = CreateFile(m_strFile, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (INVALID_HANDLE_VALUE == hFile) ThrowLastError(m_strFile); dwSize = GetFileSize(hFile, NULL); if (dwSize == -1) ThrowLastError(m_strFile); szBuffer = new char[dwSize + 1]; if (!szBuffer) throw -1; // read the file long dwRead; if (!ReadFile(hFile, szBuffer, dwSize, (DWORD *)&dwRead, 0)) { delete [] szBuffer; CloseHandle(hFile); ThrowLastError(m_strFile); } // close the file CloseHandle(hFile); } catch(GException &) { GString strMessage("\nFailed to open a configuration file for this application.\n"); throw GException(2, (const char *)strMessage); } #else // open the file GString strTemp; int nResult = strTemp.FromFile((const char *)m_strFile, 0); if (nResult == 0) { GString strMessage("\nFailed to open a configuration file for this application.\n"); throw GException(2, (const char *)strMessage); } szBuffer = new char[strTemp.Length() + 1]; memcpy(szBuffer,(const char *)strTemp, strTemp.Length()); dwSize = strTemp.Length(); #endif // terminate the buffer szBuffer[dwSize] = 0; ProfileParse(szBuffer, dwSize); delete [] szBuffer; }