Fix {Read,Write}FileToString.

We should be using binary always.
This commit is contained in:
Matthew Parlane 2013-11-05 00:33:41 +13:00
parent 3f1ea21e4f
commit e15f628935
12 changed files with 35 additions and 58 deletions

View file

@ -935,14 +935,14 @@ std::string GetThemeDir(const std::string& theme_name)
return dir;
}
bool WriteStringToFile(bool text_file, const std::string &str, const char *filename)
bool WriteStringToFile(const std::string &str, const char *filename)
{
return File::IOFile(filename, text_file ? "w" : "wb").WriteBytes(str.data(), str.size());
return File::IOFile(filename, "wb").WriteBytes(str.data(), str.size());
}
bool ReadFileToString(bool text_file, const char *filename, std::string &str)
bool ReadFileToString(const char *filename, std::string &str)
{
File::IOFile file(filename, text_file ? "r" : "rb");
File::IOFile file(filename, "rb");
auto const f = file.GetHandle();
if (!f)
@ -952,29 +952,6 @@ bool ReadFileToString(bool text_file, const char *filename, std::string &str)
str.resize(GetSize(f));
bool retval = file.ReadArray(&str[0], str.size(), &read_size);
// On Windows, reading a text file automatically translates \r\n to \n.
// This means we will read less characters than the expected size of the
// file. In that case, ignore the return value and count ourselves.
#ifdef _WIN32
if (text_file)
{
size_t count = 0;
for (size_t i = 0; i < read_size; ++i)
{
if (str[i] == '\n')
count += 2;
else
count += 1;
}
if (count != str.size())
return false;
str.resize(read_size);
return true;
}
#endif
return retval;
}

View file

@ -143,8 +143,8 @@ std::string GetBundleDirectory();
std::string &GetExeDirectory();
#endif
bool WriteStringToFile(bool text_file, const std::string &str, const char *filename);
bool ReadFileToString(bool text_file, const char *filename, std::string &str);
bool WriteStringToFile(const std::string &str, const char *filename);
bool ReadFileToString(const char *filename, std::string &str);
// simple wrapper for cstdlib file functions to
// hopefully will make error checking easier

View file

@ -151,7 +151,7 @@ bool CBoot::Load_BS2(const std::string& _rBootROMFilename)
// Load the whole ROM dump
std::string data;
if (!File::ReadFileToString(false, _rBootROMFilename.c_str(), data))
if (!File::ReadFileToString(_rBootROMFilename.c_str(), data))
return false;
u32 ipl_hash = HashAdler32((const u8*)data.data(), data.size());

View file

@ -206,7 +206,7 @@ void BinaryStringBEToCode(const std::string &str, std::vector<u16> &code)
bool LoadBinary(const char *filename, std::vector<u16> &code)
{
std::string buffer;
if (!File::ReadFileToString(false, filename, buffer))
if (!File::ReadFileToString(filename, buffer))
return false;
BinaryStringBEToCode(buffer, code);
@ -217,7 +217,7 @@ bool SaveBinary(const std::vector<u16> &code, const char *filename)
{
std::string buffer;
CodeToBinaryStringBE(code, buffer);
if (!File::WriteStringToFile(false, buffer, filename))
if (!File::WriteStringToFile(buffer, filename))
return false;
return true;
}

View file

@ -99,7 +99,7 @@ bool DSPAssembler::Assemble(const char *text, std::vector<u16> &code, std::vecto
if (line_numbers)
line_numbers->clear();
const char *fname = "tmp.asm";
if (!File::WriteStringToFile(true, text, fname))
if (!File::WriteStringToFile(text, fname))
return false;
InitPass(1);
if (!AssembleFile(fname, 1))

View file

@ -145,7 +145,7 @@ bool InstallCodeHandler()
u32 codelist_location = 0x800028B8; // Debugger on location (0x800022A8 = Debugger off, using codehandleronly.bin)
std::string data;
std::string _rCodeHandlerFilename = File::GetSysDirectory() + GECKO_CODE_HANDLER;
if (!File::ReadFileToString(false, _rCodeHandlerFilename.c_str(), data))
if (!File::ReadFileToString(_rCodeHandlerFilename.c_str(), data))
return false;
// Install code handler

View file

@ -55,7 +55,7 @@ bool DumpDSPCode(const u8 *code_be, int size_in_bytes, u32 crc)
if (!disasm.Disassemble(0, code, 0x0000, text))
return false;
return File::WriteStringToFile(true, text, txtFile);
return File::WriteStringToFile(text, txtFile);
}
// TODO make this useful :p

View file

@ -287,7 +287,7 @@ bool CVolumeDirectory::SetApploader(const std::string& _rApploader)
if (!_rApploader.empty())
{
std::string data;
if (!File::ReadFileToString(false, _rApploader.c_str(), data))
if (!File::ReadFileToString(_rApploader.c_str(), data))
{
PanicAlertT("Apploader unable to load from file");
return false;
@ -320,7 +320,7 @@ void CVolumeDirectory::SetDOL(const std::string& _rDOL)
if (!_rDOL.empty())
{
std::string data;
File::ReadFileToString(false, _rDOL.c_str(), data);
File::ReadFileToString(_rDOL.c_str(), data);
m_DOLSize = data.size();
m_DOL = new u8[m_DOLSize];
copy(data.begin(), data.end(), m_DOL);

View file

@ -157,7 +157,7 @@ void ApplyShader()
// Fallback to shared user dir
path = File::GetSysDirectory() + SHADERS_DIR DIR_SEP + g_ActiveConfig.sPostProcessingShader + ".glsl";
}
if(!File::ReadFileToString(true, path.c_str(), code)) {
if(!File::ReadFileToString(path.c_str(), code)) {
ERROR_LOG(VIDEO, "Post-processing shader not found: %s", path.c_str());
return;
}

View file

@ -108,7 +108,7 @@ void GFXDebuggerBase::DumpPixelShader(const char* path)
}
File::CreateEmptyFile(filename);
File::WriteStringToFile(true, output, filename);
File::WriteStringToFile(output, filename);
}
void GFXDebuggerBase::DumpVertexShader(const char* path)
@ -117,7 +117,7 @@ void GFXDebuggerBase::DumpVertexShader(const char* path)
sprintf(filename, "%sdump_vs.txt", path);
File::CreateEmptyFile(filename);
/// File::WriteStringToFile(true, GenerateVertexShaderCode(g_nativeVertexFmt->m_components, g_ActiveConfig.backend_info.APIType), filename);
/// File::WriteStringToFile(GenerateVertexShaderCode(g_nativeVertexFmt->m_components, g_ActiveConfig.backend_info.APIType), filename);
}
void GFXDebuggerBase::DumpPixelShaderConstants(const char* path)

View file

@ -140,7 +140,7 @@ void TexDecoder_OpenCL_Initialize()
{
std::string code;
filename = File::GetSysDirectory() + OPENCL_DIR DIR_SEP "TextureDecoder.cl";
if (!File::ReadFileToString(true, filename.c_str(), code))
if (!File::ReadFileToString(filename.c_str(), code))
{
ERROR_LOG(VIDEO, "Failed to load OpenCL code %s - file is missing?", filename.c_str());
return;

View file

@ -72,8 +72,8 @@ bool SuperTrip(const char *asm_code)
std::string text2;
Disassemble(code1, true, &text1);
Disassemble(code2, true, &text2);
File::WriteStringToFile(true, text1, "code1.txt");
File::WriteStringToFile(true, text2, "code2.txt");
File::WriteStringToFile(text1, "code1.txt");
File::WriteStringToFile(text2, "code2.txt");
*/
return true;
}
@ -126,7 +126,7 @@ void RunAsmTests()
/*
std::vector<u16> code;
std::string text_orig;
File::ReadFileToString(false, "testdata/dsp_test.S", &text_orig);
File::ReadFileToString("testdata/dsp_test.S", &text_orig);
if (!Assemble(text_orig.c_str(), &code))
{
printf("SuperTrip: First assembly failed\n");
@ -168,15 +168,15 @@ void RunAsmTests()
RoundTrip(rand_code);
if (File::ReadFileToString(true, "C:/devkitPro/examples/wii/asndlib/dsptest/dsp_test.ds", &dsp_test))
if (File::ReadFileToString("C:/devkitPro/examples/wii/asndlib/dsptest/dsp_test.ds", &dsp_test))
SuperTrip(dsp_test.c_str());
//.File::ReadFileToString(true, "C:/devkitPro/trunk/libogc/libasnd/dsp_mixer/dsp_mixer.s", &dsp_test);
//.File::ReadFileToString("C:/devkitPro/trunk/libogc/libasnd/dsp_mixer/dsp_mixer.s", &dsp_test);
// This is CLOSE to working. Sorry about the local path btw. This is preliminary code.
*/
std::string dsp_test;
if (File::ReadFileToString(true, "Testdata/dsp_test.s", dsp_test))
if (File::ReadFileToString("Testdata/dsp_test.s", dsp_test))
fail = fail || !SuperTrip(dsp_test.c_str());
if (!fail)
printf("All passed!\n");
@ -288,9 +288,9 @@ int main(int argc, const char *argv[])
// Two binary inputs, let's diff.
std::string binary_code;
std::vector<u16> code1, code2;
File::ReadFileToString(false, input_name.c_str(), binary_code);
File::ReadFileToString(input_name.c_str(), binary_code);
BinaryStringBEToCode(binary_code, code1);
File::ReadFileToString(false, output_name.c_str(), binary_code);
File::ReadFileToString(output_name.c_str(), binary_code);
BinaryStringBEToCode(binary_code, code2);
Compare(code1, code2);
return 0;
@ -301,7 +301,7 @@ int main(int argc, const char *argv[])
std::string dumpfile, results;
std::vector<u16> reg_vector;
File::ReadFileToString(false, input_name.c_str(), dumpfile);
File::ReadFileToString(input_name.c_str(), dumpfile);
BinaryStringBEToCode(dumpfile, reg_vector);
results.append("Start:\n");
@ -362,7 +362,7 @@ int main(int argc, const char *argv[])
}
if (!output_name.empty())
File::WriteStringToFile(true, results, output_name.c_str());
File::WriteStringToFile(results, output_name.c_str());
else
printf("%s", results.c_str());
return 0;
@ -377,12 +377,12 @@ int main(int argc, const char *argv[])
}
std::string binary_code;
std::vector<u16> code;
File::ReadFileToString(false, input_name.c_str(), binary_code);
File::ReadFileToString(input_name.c_str(), binary_code);
BinaryStringBEToCode(binary_code, code);
std::string text;
Disassemble(code, true, text);
if (!output_name.empty())
File::WriteStringToFile(true, text, output_name.c_str());
File::WriteStringToFile(text, output_name.c_str());
else
printf("%s", text.c_str());
}
@ -394,7 +394,7 @@ int main(int argc, const char *argv[])
return 1;
}
std::string source;
if (File::ReadFileToString(true, input_name.c_str(), source))
if (File::ReadFileToString(input_name.c_str(), source))
{
if(multiple)
{
@ -429,7 +429,7 @@ int main(int argc, const char *argv[])
for (int i = 0; i < lines; i++)
{
if (!File::ReadFileToString(true, files[i].c_str(), currentSource))
if (!File::ReadFileToString(files[i].c_str(), currentSource))
{
printf("ERROR reading %s, skipping...\n", files[i].c_str());
lines--;
@ -450,7 +450,7 @@ int main(int argc, const char *argv[])
CodesToHeader(codes, &files, lines, output_header_name.c_str(), header);
File::WriteStringToFile(true, header, (output_header_name + ".h").c_str());
File::WriteStringToFile(header, (output_header_name + ".h").c_str());
delete[] codes;
}
@ -471,13 +471,13 @@ int main(int argc, const char *argv[])
{
std::string binary_code;
CodeToBinaryStringBE(code, binary_code);
File::WriteStringToFile(false, binary_code, output_name.c_str());
File::WriteStringToFile(binary_code, output_name.c_str());
}
if (!output_header_name.empty())
{
std::string header;
CodeToHeader(code, input_name, output_header_name.c_str(), header);
File::WriteStringToFile(true, header, (output_header_name + ".h").c_str());
File::WriteStringToFile(header, (output_header_name + ".h").c_str());
}
}
}