Open std::fstream in a unicode-safe manner.

This commit is contained in:
Jordan Woyak 2013-02-28 19:33:39 -06:00
parent 95558cdc69
commit dea1e2827d
19 changed files with 57 additions and 25 deletions

View File

@ -25,6 +25,7 @@
#include <string.h>
#include "Common.h"
#include "StringUtil.h"
// User directory indices for GetUserPath
enum {
@ -226,4 +227,15 @@ private:
} // namespace
// To deal with Windows being dumb at unicode:
template <typename T>
void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode)
{
#ifdef _WIN32
fstream.open(UTF8ToTStr(filename).c_str(), openmode);
#else
fstream.open(filename, openmode);
#endif
}
#endif

View File

@ -25,6 +25,7 @@
#include <fstream>
#include <algorithm>
#include "FileUtil.h"
#include "StringUtil.h"
#include "IniFile.h"
@ -400,7 +401,7 @@ bool IniFile::Load(const char* filename)
// Open file
std::ifstream in;
in.open(filename, std::ios::in);
OpenFStream(in, filename, std::ios::in);
if (in.fail()) return false;
@ -452,7 +453,7 @@ bool IniFile::Load(const char* filename)
bool IniFile::Save(const char* filename)
{
std::ofstream out;
out.open(filename, std::ios::out);
OpenFStream(out, filename, std::ios::out);
if (out.fail())
{

View File

@ -74,7 +74,7 @@ public:
m_num_entries = 0;
// try opening for reading/writing
m_file.open(filename, ios_base::in | ios_base::out | ios_base::binary);
OpenFStream(m_file, filename, ios_base::in | ios_base::out | ios_base::binary);
m_file.seekg(0, std::ios::end);
std::fstream::pos_type end_pos = m_file.tellg();

View File

@ -186,7 +186,7 @@ void LogContainer::Trigger(LogTypes::LOG_LEVELS level, const char *msg)
FileLogListener::FileLogListener(const char *filename)
{
m_logfile.open(filename, std::ios::app);
OpenFStream(m_logfile, filename, std::ios::app);
SetEnable(true);
}

View File

@ -86,7 +86,8 @@ bool CheckTitleTIK(u64 _titleID)
static void CreateReplacementFile(std::string &filename)
{
std::ofstream replace(filename.c_str());
std::ofstream replace;
OpenFStream(replace, filename, std::ios_base::out);
replace <<"\" __22__\n";
replace << "* __2a__\n";
//replace << "/ __2f__\n";
@ -108,7 +109,8 @@ void ReadReplacements(replace_v& replacements)
if (!File::Exists(filename))
CreateReplacementFile(filename);
std::ifstream f(filename.c_str());
std::ifstream f;
OpenFStream(f, filename, std::ios_base::in);
char letter;
std::string replacement;

View File

@ -754,7 +754,8 @@ bool DSPAssembler::AssembleFile(const char *fname, int pass)
{
int disable_text = 0; // modified by Hermes
std::ifstream fsrc(fname);
std::ifstream fsrc;
OpenFStream(fsrc, fname, std::ios_base::in);
if (fsrc.fail())
{

View File

@ -297,7 +297,7 @@ void Wiimote::WriteData(const wm_write_data* const wd)
{
// writing the whole mii block each write :/
std::ofstream file;
file.open((File::GetUserPath(D_WIIUSER_IDX) + "mii.bin").c_str(), std::ios::binary | std::ios::out);
OpenFStream(file, File::GetUserPath(D_WIIUSER_IDX) + "mii.bin", std::ios::binary | std::ios::out);
file.write((char*)m_eeprom + 0x0FCA, 0x02f0);
file.close();
}

View File

@ -106,7 +106,7 @@ void Wiimote::SpeakerData(wm_speaker_data* sd)
File::Delete("rmtdump.wav");
File::Delete("rmtdump.bin");
atexit(stopdamnwav);
ofile.open("rmtdump.bin", ofile.binary | ofile.out);
OpenFStream(ofile, "rmtdump.bin", ofile.binary | ofile.out);
wav.Start("rmtdump.wav", 6000/*Common::swap16(m_reg_speaker.sample_rate)*/);
}
wav.AddMonoSamples(samples, sd->length*2);

View File

@ -285,7 +285,8 @@ void CCodeWindow::OnSymbolsMenu(wxCommandEvent& event)
if (!path.IsEmpty())
{
std::ifstream f(WxStrToStr(path).c_str());
std::ifstream f;
OpenFStream(f, WxStrToStr(path), std::ios_base::in);
std::string line;
while (std::getline(f, line))

View File

@ -137,7 +137,8 @@ CISOProperties::CISOProperties(const std::string fileName, wxWindow* parent, wxW
else
{
// Will fail out if GameConfig folder doesn't exist
std::ofstream f(GameIniFile.c_str());
std::ofstream f;
OpenFStream(f, GameIniFile, std::ios_base::out);
if (f)
{
f << "# " << OpenISO->GetUniqueID() << " - " << OpenISO->GetName() << '\n'

View File

@ -69,7 +69,8 @@ bool SaveTGA(const char* filename, int width, int height, void* pdata)
bool SaveData(const char* filename, const char* data)
{
std::ofstream f(filename, std::ios::binary);
std::ofstream f;
OpenFStream(f, filename, std::ios::binary);
f << data;
return true;

View File

@ -252,7 +252,8 @@ void ValidatePixelShaderIDs(API_TYPE api, PIXELSHADERUIDSAFE old_id, const std::
static int num_failures = 0;
char szTemp[MAX_PATH];
sprintf(szTemp, "%spsuid_mismatch_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++);
std::ofstream file(szTemp);
std::ofstream file;
OpenFStream(file, szTemp, std::ios_base::out);
file << msg;
file << "\n\nOld shader code:\n" << old_code;
file << "\n\nNew shader code:\n" << new_code;

View File

@ -277,10 +277,12 @@ void VertexManager::Flush()
// save the shaders
char strfile[255];
sprintf(strfile, "%sps%.3d.txt", File::GetUserPath(D_DUMPFRAMES_IDX).c_str(), g_ActiveConfig.iSaveTargetId);
std::ofstream fps(strfile);
std::ofstream fps;
OpenFStream(fps, strfile, std::ios_base::out);
fps << ps->strprog.c_str();
sprintf(strfile, "%svs%.3d.txt", File::GetUserPath(D_DUMPFRAMES_IDX).c_str(), g_ActiveConfig.iSaveTargetId);
std::ofstream fvs(strfile);
std::ofstream fvs;
OpenFStream(fvs, strfile, std::ios_base::out);
fvs << vs->strprog.c_str();
}

View File

@ -114,7 +114,8 @@ void ValidateVertexShaderIDs(API_TYPE api, VERTEXSHADERUIDSAFE old_id, const std
static int num_failures = 0;
char szTemp[MAX_PATH];
sprintf(szTemp, "%svsuid_mismatch_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++);
std::ofstream file(szTemp);
std::ofstream file;
OpenFStream(file, szTemp, std::ios_base::out);
file << msg;
file << "\n\nOld shader code:\n" << old_code;
file << "\n\nNew shader code:\n" << new_code;

View File

@ -64,7 +64,8 @@ bool CompileVertexShader(const char* code, unsigned int len, D3DBlob** blob)
static int num_failures = 0;
char szTemp[MAX_PATH];
sprintf(szTemp, "%sbad_vs_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++);
std::ofstream file(szTemp);
std::ofstream file;
OpenFStream(file, szTemp, std::ios_base::out);
file << code;
file.close();
@ -121,7 +122,8 @@ bool CompileGeometryShader(const char* code, unsigned int len, D3DBlob** blob,
static int num_failures = 0;
char szTemp[MAX_PATH];
sprintf(szTemp, "%sbad_gs_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++);
std::ofstream file(szTemp);
std::ofstream file;
OpenFStream(file, szTemp, std::ios_base::out);
file << code;
file.close();
@ -180,7 +182,8 @@ bool CompilePixelShader(const char* code, unsigned int len, D3DBlob** blob,
static int num_failures = 0;
char szTemp[MAX_PATH];
sprintf(szTemp, "%sbad_ps_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++);
std::ofstream file(szTemp);
std::ofstream file;
OpenFStream(file, szTemp, std::ios_base::out);
file << code;
file.close();

View File

@ -50,7 +50,8 @@ bool CompileVertexShader(const char *code, int len, u8 **bytecode, int *bytecode
static int num_failures = 0;
char szTemp[MAX_PATH];
sprintf(szTemp, "%sbad_vs_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++);
std::ofstream file(szTemp);
std::ofstream file;
OpenFStream(file, szTemp, std::ios_base::out);
file << code;
file.close();
@ -105,7 +106,8 @@ bool CompilePixelShader(const char *code, int len, u8 **bytecode, int *bytecodel
static int num_failures = 0;
char szTemp[MAX_PATH];
sprintf(szTemp, "%sbad_ps_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++);
std::ofstream file(szTemp);
std::ofstream file;
OpenFStream(file, szTemp, std::ios_base::out);
file << code;
file.close();

View File

@ -264,7 +264,8 @@ bool PixelShaderCache::CompilePixelShader(FRAGMENTSHADER& ps, const char* pstrpr
static int num_failures = 0;
char szTemp[MAX_PATH];
sprintf(szTemp, "%sbad_ps_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++);
std::ofstream file(szTemp);
std::ofstream file;
OpenFStream(file, szTemp, std::ios_base::out);
file << pstrprogram;
file.close();

View File

@ -249,10 +249,12 @@ void VertexManager::vFlush()
// save the shaders
char strfile[255];
sprintf(strfile, "%sps%.3d.txt", File::GetUserPath(D_DUMPFRAMES_IDX).c_str(), g_ActiveConfig.iSaveTargetId);
std::ofstream fps(strfile);
std::ofstream fps;
OpenFStream(fps, strfile, std::ios_base::out);
fps << ps->strprog.c_str();
sprintf(strfile, "%svs%.3d.txt", File::GetUserPath(D_DUMPFRAMES_IDX).c_str(), g_ActiveConfig.iSaveTargetId);
std::ofstream fvs(strfile);
std::ofstream fvs;
OpenFStream(fvs, strfile, std::ios_base::out);
fvs << vs->strprog.c_str();
}

View File

@ -144,7 +144,8 @@ bool VertexShaderCache::CompileVertexShader(VERTEXSHADER& vs, const char* pstrpr
static int num_failures = 0;
char szTemp[MAX_PATH];
sprintf(szTemp, "%sbad_vs_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++);
std::ofstream file(szTemp);
std::ofstream file;
OpenFStream(file, szTemp, std::ios_base::out);
file << pstrprogram;
file.close();