Fix {Read,Write}FileToString.
We should be using binary always.
This commit is contained in:
parent
3f1ea21e4f
commit
e15f628935
|
@ -935,14 +935,14 @@ std::string GetThemeDir(const std::string& theme_name)
|
||||||
return dir;
|
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();
|
auto const f = file.GetHandle();
|
||||||
|
|
||||||
if (!f)
|
if (!f)
|
||||||
|
@ -952,29 +952,6 @@ bool ReadFileToString(bool text_file, const char *filename, std::string &str)
|
||||||
str.resize(GetSize(f));
|
str.resize(GetSize(f));
|
||||||
bool retval = file.ReadArray(&str[0], str.size(), &read_size);
|
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;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -143,8 +143,8 @@ std::string GetBundleDirectory();
|
||||||
std::string &GetExeDirectory();
|
std::string &GetExeDirectory();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool WriteStringToFile(bool text_file, const std::string &str, const char *filename);
|
bool WriteStringToFile(const std::string &str, const char *filename);
|
||||||
bool ReadFileToString(bool text_file, const char *filename, std::string &str);
|
bool ReadFileToString(const char *filename, std::string &str);
|
||||||
|
|
||||||
// simple wrapper for cstdlib file functions to
|
// simple wrapper for cstdlib file functions to
|
||||||
// hopefully will make error checking easier
|
// hopefully will make error checking easier
|
||||||
|
|
|
@ -151,7 +151,7 @@ bool CBoot::Load_BS2(const std::string& _rBootROMFilename)
|
||||||
|
|
||||||
// Load the whole ROM dump
|
// Load the whole ROM dump
|
||||||
std::string data;
|
std::string data;
|
||||||
if (!File::ReadFileToString(false, _rBootROMFilename.c_str(), data))
|
if (!File::ReadFileToString(_rBootROMFilename.c_str(), data))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
u32 ipl_hash = HashAdler32((const u8*)data.data(), data.size());
|
u32 ipl_hash = HashAdler32((const u8*)data.data(), data.size());
|
||||||
|
|
|
@ -206,7 +206,7 @@ void BinaryStringBEToCode(const std::string &str, std::vector<u16> &code)
|
||||||
bool LoadBinary(const char *filename, std::vector<u16> &code)
|
bool LoadBinary(const char *filename, std::vector<u16> &code)
|
||||||
{
|
{
|
||||||
std::string buffer;
|
std::string buffer;
|
||||||
if (!File::ReadFileToString(false, filename, buffer))
|
if (!File::ReadFileToString(filename, buffer))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
BinaryStringBEToCode(buffer, code);
|
BinaryStringBEToCode(buffer, code);
|
||||||
|
@ -217,7 +217,7 @@ bool SaveBinary(const std::vector<u16> &code, const char *filename)
|
||||||
{
|
{
|
||||||
std::string buffer;
|
std::string buffer;
|
||||||
CodeToBinaryStringBE(code, buffer);
|
CodeToBinaryStringBE(code, buffer);
|
||||||
if (!File::WriteStringToFile(false, buffer, filename))
|
if (!File::WriteStringToFile(buffer, filename))
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -99,7 +99,7 @@ bool DSPAssembler::Assemble(const char *text, std::vector<u16> &code, std::vecto
|
||||||
if (line_numbers)
|
if (line_numbers)
|
||||||
line_numbers->clear();
|
line_numbers->clear();
|
||||||
const char *fname = "tmp.asm";
|
const char *fname = "tmp.asm";
|
||||||
if (!File::WriteStringToFile(true, text, fname))
|
if (!File::WriteStringToFile(text, fname))
|
||||||
return false;
|
return false;
|
||||||
InitPass(1);
|
InitPass(1);
|
||||||
if (!AssembleFile(fname, 1))
|
if (!AssembleFile(fname, 1))
|
||||||
|
|
|
@ -145,7 +145,7 @@ bool InstallCodeHandler()
|
||||||
u32 codelist_location = 0x800028B8; // Debugger on location (0x800022A8 = Debugger off, using codehandleronly.bin)
|
u32 codelist_location = 0x800028B8; // Debugger on location (0x800022A8 = Debugger off, using codehandleronly.bin)
|
||||||
std::string data;
|
std::string data;
|
||||||
std::string _rCodeHandlerFilename = File::GetSysDirectory() + GECKO_CODE_HANDLER;
|
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;
|
return false;
|
||||||
|
|
||||||
// Install code handler
|
// Install code handler
|
||||||
|
|
|
@ -55,7 +55,7 @@ bool DumpDSPCode(const u8 *code_be, int size_in_bytes, u32 crc)
|
||||||
if (!disasm.Disassemble(0, code, 0x0000, text))
|
if (!disasm.Disassemble(0, code, 0x0000, text))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return File::WriteStringToFile(true, text, txtFile);
|
return File::WriteStringToFile(text, txtFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO make this useful :p
|
// TODO make this useful :p
|
||||||
|
|
|
@ -287,7 +287,7 @@ bool CVolumeDirectory::SetApploader(const std::string& _rApploader)
|
||||||
if (!_rApploader.empty())
|
if (!_rApploader.empty())
|
||||||
{
|
{
|
||||||
std::string data;
|
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");
|
PanicAlertT("Apploader unable to load from file");
|
||||||
return false;
|
return false;
|
||||||
|
@ -320,7 +320,7 @@ void CVolumeDirectory::SetDOL(const std::string& _rDOL)
|
||||||
if (!_rDOL.empty())
|
if (!_rDOL.empty())
|
||||||
{
|
{
|
||||||
std::string data;
|
std::string data;
|
||||||
File::ReadFileToString(false, _rDOL.c_str(), data);
|
File::ReadFileToString(_rDOL.c_str(), data);
|
||||||
m_DOLSize = data.size();
|
m_DOLSize = data.size();
|
||||||
m_DOL = new u8[m_DOLSize];
|
m_DOL = new u8[m_DOLSize];
|
||||||
copy(data.begin(), data.end(), m_DOL);
|
copy(data.begin(), data.end(), m_DOL);
|
||||||
|
|
|
@ -157,7 +157,7 @@ void ApplyShader()
|
||||||
// Fallback to shared user dir
|
// Fallback to shared user dir
|
||||||
path = File::GetSysDirectory() + SHADERS_DIR DIR_SEP + g_ActiveConfig.sPostProcessingShader + ".glsl";
|
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());
|
ERROR_LOG(VIDEO, "Post-processing shader not found: %s", path.c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,7 +108,7 @@ void GFXDebuggerBase::DumpPixelShader(const char* path)
|
||||||
}
|
}
|
||||||
|
|
||||||
File::CreateEmptyFile(filename);
|
File::CreateEmptyFile(filename);
|
||||||
File::WriteStringToFile(true, output, filename);
|
File::WriteStringToFile(output, filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GFXDebuggerBase::DumpVertexShader(const char* path)
|
void GFXDebuggerBase::DumpVertexShader(const char* path)
|
||||||
|
@ -117,7 +117,7 @@ void GFXDebuggerBase::DumpVertexShader(const char* path)
|
||||||
sprintf(filename, "%sdump_vs.txt", path);
|
sprintf(filename, "%sdump_vs.txt", path);
|
||||||
|
|
||||||
File::CreateEmptyFile(filename);
|
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)
|
void GFXDebuggerBase::DumpPixelShaderConstants(const char* path)
|
||||||
|
|
|
@ -140,7 +140,7 @@ void TexDecoder_OpenCL_Initialize()
|
||||||
{
|
{
|
||||||
std::string code;
|
std::string code;
|
||||||
filename = File::GetSysDirectory() + OPENCL_DIR DIR_SEP "TextureDecoder.cl";
|
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());
|
ERROR_LOG(VIDEO, "Failed to load OpenCL code %s - file is missing?", filename.c_str());
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -72,8 +72,8 @@ bool SuperTrip(const char *asm_code)
|
||||||
std::string text2;
|
std::string text2;
|
||||||
Disassemble(code1, true, &text1);
|
Disassemble(code1, true, &text1);
|
||||||
Disassemble(code2, true, &text2);
|
Disassemble(code2, true, &text2);
|
||||||
File::WriteStringToFile(true, text1, "code1.txt");
|
File::WriteStringToFile(text1, "code1.txt");
|
||||||
File::WriteStringToFile(true, text2, "code2.txt");
|
File::WriteStringToFile(text2, "code2.txt");
|
||||||
*/
|
*/
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -126,7 +126,7 @@ void RunAsmTests()
|
||||||
/*
|
/*
|
||||||
std::vector<u16> code;
|
std::vector<u16> code;
|
||||||
std::string text_orig;
|
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))
|
if (!Assemble(text_orig.c_str(), &code))
|
||||||
{
|
{
|
||||||
printf("SuperTrip: First assembly failed\n");
|
printf("SuperTrip: First assembly failed\n");
|
||||||
|
@ -168,15 +168,15 @@ void RunAsmTests()
|
||||||
RoundTrip(rand_code);
|
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());
|
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.
|
// This is CLOSE to working. Sorry about the local path btw. This is preliminary code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
std::string dsp_test;
|
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());
|
fail = fail || !SuperTrip(dsp_test.c_str());
|
||||||
if (!fail)
|
if (!fail)
|
||||||
printf("All passed!\n");
|
printf("All passed!\n");
|
||||||
|
@ -288,9 +288,9 @@ int main(int argc, const char *argv[])
|
||||||
// Two binary inputs, let's diff.
|
// Two binary inputs, let's diff.
|
||||||
std::string binary_code;
|
std::string binary_code;
|
||||||
std::vector<u16> code1, code2;
|
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);
|
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);
|
BinaryStringBEToCode(binary_code, code2);
|
||||||
Compare(code1, code2);
|
Compare(code1, code2);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -301,7 +301,7 @@ int main(int argc, const char *argv[])
|
||||||
std::string dumpfile, results;
|
std::string dumpfile, results;
|
||||||
std::vector<u16> reg_vector;
|
std::vector<u16> reg_vector;
|
||||||
|
|
||||||
File::ReadFileToString(false, input_name.c_str(), dumpfile);
|
File::ReadFileToString(input_name.c_str(), dumpfile);
|
||||||
BinaryStringBEToCode(dumpfile, reg_vector);
|
BinaryStringBEToCode(dumpfile, reg_vector);
|
||||||
|
|
||||||
results.append("Start:\n");
|
results.append("Start:\n");
|
||||||
|
@ -362,7 +362,7 @@ int main(int argc, const char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!output_name.empty())
|
if (!output_name.empty())
|
||||||
File::WriteStringToFile(true, results, output_name.c_str());
|
File::WriteStringToFile(results, output_name.c_str());
|
||||||
else
|
else
|
||||||
printf("%s", results.c_str());
|
printf("%s", results.c_str());
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -377,12 +377,12 @@ int main(int argc, const char *argv[])
|
||||||
}
|
}
|
||||||
std::string binary_code;
|
std::string binary_code;
|
||||||
std::vector<u16> 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);
|
BinaryStringBEToCode(binary_code, code);
|
||||||
std::string text;
|
std::string text;
|
||||||
Disassemble(code, true, text);
|
Disassemble(code, true, text);
|
||||||
if (!output_name.empty())
|
if (!output_name.empty())
|
||||||
File::WriteStringToFile(true, text, output_name.c_str());
|
File::WriteStringToFile(text, output_name.c_str());
|
||||||
else
|
else
|
||||||
printf("%s", text.c_str());
|
printf("%s", text.c_str());
|
||||||
}
|
}
|
||||||
|
@ -394,7 +394,7 @@ int main(int argc, const char *argv[])
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
std::string source;
|
std::string source;
|
||||||
if (File::ReadFileToString(true, input_name.c_str(), source))
|
if (File::ReadFileToString(input_name.c_str(), source))
|
||||||
{
|
{
|
||||||
if(multiple)
|
if(multiple)
|
||||||
{
|
{
|
||||||
|
@ -429,7 +429,7 @@ int main(int argc, const char *argv[])
|
||||||
|
|
||||||
for (int i = 0; i < lines; i++)
|
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());
|
printf("ERROR reading %s, skipping...\n", files[i].c_str());
|
||||||
lines--;
|
lines--;
|
||||||
|
@ -450,7 +450,7 @@ int main(int argc, const char *argv[])
|
||||||
|
|
||||||
|
|
||||||
CodesToHeader(codes, &files, lines, output_header_name.c_str(), header);
|
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;
|
delete[] codes;
|
||||||
}
|
}
|
||||||
|
@ -471,13 +471,13 @@ int main(int argc, const char *argv[])
|
||||||
{
|
{
|
||||||
std::string binary_code;
|
std::string binary_code;
|
||||||
CodeToBinaryStringBE(code, 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())
|
if (!output_header_name.empty())
|
||||||
{
|
{
|
||||||
std::string header;
|
std::string header;
|
||||||
CodeToHeader(code, input_name, output_header_name.c_str(), 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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue