pp::INI::File::File (wchar_t* filename) { sections.clear (); // We skip a few bytes (Unicode BOM) in crertain cirumstances, so this is the // actual pointer we need to free... wchar_t* alloc; wszName = _wcsdup (filename); errno_t ret; TRY_FILE_IO (_wfopen_s (&fINI, filename, L"rb"), filename, ret); if (ret == 0 && fINI != 0) { fseek (fINI, 0, SEEK_END); long size = ftell (fINI); rewind (fINI); wszData = new wchar_t [size]; alloc = wszData; fread (wszData, size, 1, fINI); // This is essentially our Unicode BOM, if the first character is '[', then it's ANSI. if (((char *)wszData) [0] == '[') { char* string = new char [size + 1]; memcpy (string, wszData, size); string [size] = '\0'; delete [] wszData; wszData = new wchar_t [size + 1]; MultiByteToWideChar (CP_OEMCP, 0, string, -1, wszData, size + 1); alloc = wszData; } // Otherwise it's Unicode, let's fix up a couple of things else { if (*wszData != L'[') ++wszData; wszData [size / 2 - 1] = '\0'; } parse (); delete [] alloc; wszData = nullptr; fflush (fINI); fclose (fINI); } else { //AD_MessageBox (L"Unable to Locate INI File", filename, MB_OK); delete [] wszName; wszName = nullptr; wszData = nullptr; } }
void pp::INI::File::write (std::wstring fname) { FILE* fOut; errno_t ret; // Strip Read-Only /////////AD_SetNormalFileAttribs (fname); TRY_FILE_IO (_wfopen_s (&fOut, fname.c_str (), L"w,ccs=UTF-16LE"), fname.c_str (), ret); if (ret != 0 || fOut == 0) { //AD_MessageBox (L"ERROR: Cannot open INI file for writing. Is it read-only?", fname.c_str (), MB_OK | MB_ICONSTOP); return; } std::vector <std::wstring>::iterator it = ordered_sections.begin (); std::vector <std::wstring>::iterator end = ordered_sections.end (); while (it != end) { Section& section = get_section (*it); fwprintf (fOut, L"[%s]\n", section.name.c_str ()); std::vector <std::wstring>::iterator key_it = section.ordered_keys.begin (); std::vector <std::wstring>::iterator key_end = section.ordered_keys.end (); while (key_it != key_end) { std::wstring val = section.get_value (*key_it); fwprintf (fOut, L"%s=%s\n", key_it->c_str (), val.c_str ()); ++key_it; } // Append a newline for everything except the last line... if (++it != end) fwprintf (fOut, L"\n"); } fflush (fOut); fclose (fOut); // Make Read-Only ////SetFileAttributes (fname.c_str (), FILE_ATTRIBUTE_READONLY); }
void bmt::INI::File::write (std::wstring fname) { FILE* fOut; errno_t ret; // Strip Read-Only SetFileAttributes (fname.c_str (), FILE_ATTRIBUTE_NORMAL); TRY_FILE_IO (_wfopen_s (&fOut, fname.c_str (), L"w,ccs=UTF-16LE"), fname.c_str (), ret); if (ret != 0 || fOut == 0) { //BMT_MessageBox (L"ERROR: Cannot open INI file for writing. Is it read-only?", fname.c_str (), MB_OK | MB_ICONSTOP); return; } std::map <std::wstring, Section>::iterator it = sections.begin (); std::map <std::wstring, Section>::iterator end = sections.end (); while (it != end) { Section& section = (*it).second; fwprintf (fOut, L"[%s]\n", section.name.c_str ()); std::map <std::wstring, std::wstring>::iterator key_it = section.pairs.begin (); std::map <std::wstring, std::wstring>::iterator key_end = section.pairs.end (); while (key_it != key_end) { fwprintf (fOut, L"%s=%s\n", key_it->first.c_str (), key_it->second.c_str ()); ++key_it; } fwprintf (fOut, L"\n"); ++it; } fflush (fOut); fclose (fOut); // Make Read-Only SetFileAttributes (fname.c_str (), FILE_ATTRIBUTE_READONLY); }