void Profile::LoadEditableDataFromDir( CString sDir ) { CString fn = sDir + EDITABLE_INI; // // Don't load unreasonably large editable.xml files. // int iBytes = FILEMAN->GetFileSizeInBytes( fn ); if( iBytes > MAX_EDITABLE_INI_SIZE_BYTES ) { LOG->Warn( "The file '%s' is unreasonably large. It won't be loaded.", fn.c_str() ); return; } IniFile ini; ini.ReadFile( fn ); ini.GetValue("Editable","DisplayName", m_sDisplayName); ini.GetValue("Editable","LastUsedHighScoreName", m_sLastUsedHighScoreName); ini.GetValue("Editable","WeightPounds", m_iWeightPounds); // This is data that the user can change, so we have to validate it. wstring wstr = CStringToWstring(m_sDisplayName); if( wstr.size() > 12 ) wstr = wstr.substr(0, 12); m_sDisplayName = WStringToCString(wstr); // TODO: strip invalid chars? if( m_iWeightPounds != 0 ) CLAMP( m_iWeightPounds, 50, 500 ); }
/* Convert from the given codepage to UTF-8. Return true if successful. */ static bool CodePageConvert(CString &txt, int cp) { if( txt.size() == 0 ) return true; int size = MultiByteToWideChar(cp, MB_ERR_INVALID_CHARS, txt.data(), txt.size(), NULL, 0); if( size == 0 ) { LOG->Trace("%s\n", werr_ssprintf(GetLastError(), "err: ").c_str()); return false; /* error */ } wstring out; out.append(size, ' '); /* Nonportable: */ size = MultiByteToWideChar(cp, MB_ERR_INVALID_CHARS, txt.data(), txt.size(), (wchar_t *) out.data(), size); ASSERT( size != 0 ); txt = WStringToCString(out); return true; }