[Project64] Change file handling to be ansi

This commit is contained in:
zilmar 2015-11-30 21:14:14 +11:00
parent 832248d818
commit f50f634daf
3 changed files with 216 additions and 209 deletions

View File

@ -6,17 +6,17 @@ stdstr::stdstr()
{ {
} }
stdstr::stdstr( const std::string & str ) : stdstr::stdstr(const std::string & str) :
std::string(str) std::string(str)
{ {
} }
stdstr::stdstr( const stdstr & str ) : stdstr::stdstr(const stdstr & str) :
std::string((const std::string &)str) std::string((const std::string &)str)
{ {
} }
stdstr::stdstr( const char * str ) : stdstr::stdstr(const char * str) :
std::string(str ? str : "") std::string(str ? str : "")
{ {
} }
@ -56,12 +56,12 @@ void stdstr::ArgFormat(const char * strFormat, va_list & args)
#pragma warning(push) #pragma warning(push)
#pragma warning(disable:4996) #pragma warning(disable:4996)
size_t nlen = _vscprintf( strFormat, args ) + 1; size_t nlen = _vscprintf(strFormat, args) + 1;
char * buffer = (char *)alloca(nlen * sizeof(char)); char * buffer = (char *)alloca(nlen * sizeof(char));
buffer[nlen - 1] = 0; buffer[nlen - 1] = 0;
if(buffer != NULL) if (buffer != NULL)
{ {
vsprintf( buffer, strFormat , args ); vsprintf(buffer, strFormat, args);
*this = buffer; *this = buffer;
} }
#pragma warning(pop) #pragma warning(pop)
@ -71,7 +71,7 @@ void stdstr::Format(const char * strFormat, ...)
{ {
va_list args; va_list args;
va_start(args, strFormat); va_start(args, strFormat);
ArgFormat(strFormat,args); ArgFormat(strFormat, args);
va_end(args); va_end(args);
} }
@ -87,79 +87,81 @@ stdstr& stdstr::ToUpper(void)
return *this; return *this;
} }
void stdstr::Replace(const char search, const char replace ) void stdstr::Replace(const char search, const char replace)
{ {
std::string& str = *this; std::string& str = *this;
std::string::size_type pos = str.find( search ); std::string::size_type pos = str.find(search);
while ( pos != std::string::npos ) while (pos != std::string::npos)
{ {
str.replace( pos, 1, &replace ); str.replace(pos, 1, &replace);
pos = str.find( search, pos + 1 ); pos = str.find(search, pos + 1);
} }
} }
void stdstr::Replace(const char * search, const char replace ) void stdstr::Replace(const char * search, const char replace)
{ {
std::string& str = *this; std::string& str = *this;
std::string::size_type pos = str.find( search ); std::string::size_type pos = str.find(search);
size_t SearchSize = strlen(search); size_t SearchSize = strlen(search);
while ( pos != std::string::npos ) while (pos != std::string::npos)
{ {
str.replace( pos, SearchSize, &replace ); str.replace(pos, SearchSize, &replace);
pos = str.find( search, pos + 1 ); pos = str.find(search, pos + 1);
} }
} }
void stdstr::Replace(const std::string& search, const std::string& replace ) void stdstr::Replace(const std::string& search, const std::string& replace)
{ {
std::string& str = *this; std::string& str = *this;
std::string::size_type pos = str.find( search ); std::string::size_type pos = str.find(search);
size_t SearchSize = search.size(); size_t SearchSize = search.size();
while ( pos != std::string::npos ) while (pos != std::string::npos)
{ {
str.replace( pos, SearchSize, replace ); str.replace(pos, SearchSize, replace);
pos = str.find( search, pos + replace.length() ); pos = str.find(search, pos + replace.length());
} }
} }
stdstr & stdstr::TrimLeft (const char * chars2remove) stdstr & stdstr::TrimLeft(const char * chars2remove)
{ {
if (!empty()) if (!empty())
{ {
std::string::size_type pos = find_first_not_of(chars2remove); std::string::size_type pos = find_first_not_of(chars2remove);
if (pos != std::string::npos) if (pos != std::string::npos)
{ {
erase(0,pos); erase(0, pos);
} else { }
else {
erase(begin(), end()); // make empty erase(begin(), end()); // make empty
} }
} }
return *this; return *this;
} }
stdstr & stdstr::TrimRight (const char * chars2remove) stdstr & stdstr::TrimRight(const char * chars2remove)
{ {
if (!empty()) if (!empty())
{ {
std::string::size_type pos = find_last_not_of(chars2remove); std::string::size_type pos = find_last_not_of(chars2remove);
if (pos != std::string::npos) if (pos != std::string::npos)
{ {
erase(pos+1); erase(pos + 1);
} else { }
else {
erase(begin(), end()); // make empty erase(begin(), end()); // make empty
} }
} }
return *this; return *this;
} }
stdstr & stdstr::Trim (const char * chars2remove) stdstr & stdstr::Trim(const char * chars2remove)
{ {
if (!empty()) if (!empty())
{ {
std::string::size_type pos = find_first_not_of(chars2remove); std::string::size_type pos = find_first_not_of(chars2remove);
if (pos != std::string::npos) if (pos != std::string::npos)
{ {
erase(0,pos); erase(0, pos);
} }
else else
{ {
@ -169,7 +171,7 @@ stdstr & stdstr::Trim (const char * chars2remove)
pos = find_last_not_of(chars2remove); pos = find_last_not_of(chars2remove);
if (pos != std::string::npos) if (pos != std::string::npos)
{ {
erase(pos+1); erase(pos + 1);
} }
else else
{ {
@ -179,7 +181,7 @@ stdstr & stdstr::Trim (const char * chars2remove)
return *this; return *this;
} }
stdstr & stdstr::FromUTF16 ( const wchar_t * UTF16Source, bool * bSuccess ) stdstr & stdstr::FromUTF16(const wchar_t * UTF16Source, bool * bSuccess)
{ {
bool bConverted = false; bool bConverted = false;
@ -188,13 +190,13 @@ stdstr & stdstr::FromUTF16 ( const wchar_t * UTF16Source, bool * bSuccess )
*this = ""; *this = "";
bConverted = true; bConverted = true;
} }
else if(wcslen(UTF16Source) > 0) else if (wcslen(UTF16Source) > 0)
{ {
DWORD nNeeded = WideCharToMultiByte(CP_UTF8, 0, UTF16Source, -1, NULL, 0, NULL, NULL); DWORD nNeeded = WideCharToMultiByte(CP_UTF8, 0, UTF16Source, -1, NULL, 0, NULL, NULL);
if(nNeeded > 0) if (nNeeded > 0)
{ {
char * buf = (char *)alloca(nNeeded + 1); char * buf = (char *)alloca(nNeeded + 1);
if( buf != NULL ) if (buf != NULL)
{ {
memset(buf, 0, nNeeded + 1); memset(buf, 0, nNeeded + 1);
@ -214,20 +216,20 @@ stdstr & stdstr::FromUTF16 ( const wchar_t * UTF16Source, bool * bSuccess )
return *this; return *this;
} }
std::wstring stdstr::ToUTF16 ( bool * bSuccess) std::wstring stdstr::ToUTF16(UINT CodePage, bool * bSuccess)
{ {
bool bConverted = false; bool bConverted = false;
std::wstring res; std::wstring res;
DWORD nNeeded = MultiByteToWideChar(CP_UTF8, 0, this->c_str(), (int)this->length(), NULL, 0); DWORD nNeeded = MultiByteToWideChar(CodePage, 0, this->c_str(), (int)this->length(), NULL, 0);
if(nNeeded > 0) if (nNeeded > 0)
{ {
wchar_t * buf = (wchar_t *)alloca((nNeeded + 1) * sizeof(wchar_t)); wchar_t * buf = (wchar_t *)alloca((nNeeded + 1) * sizeof(wchar_t));
if( buf != NULL ) if (buf != NULL)
{ {
memset(buf, 0, (nNeeded + 1) * sizeof(wchar_t)); memset(buf, 0, (nNeeded + 1) * sizeof(wchar_t));
nNeeded = MultiByteToWideChar(CP_UTF8, 0, this->c_str(), (int)this->length(), buf, nNeeded); nNeeded = MultiByteToWideChar(CodePage, 0, this->c_str(), (int)this->length(), buf, nNeeded);
if (nNeeded) if (nNeeded)
{ {
res = buf; res = buf;

View File

@ -13,40 +13,45 @@ class stdstr :
public std::string public std::string
{ {
public: public:
enum
{
CODEPAGE_UTF8 = 65001,
CODEPAGE_932 = 932,
};
stdstr(); stdstr();
stdstr( const std::string & str ); stdstr(const std::string & str);
stdstr( const stdstr & str ); stdstr(const stdstr & str);
stdstr( const char * str ); stdstr(const char * str);
strvector Tokenize ( char delimiter ) const; strvector Tokenize(char delimiter) const;
strvector Tokenize ( const char * delimiter ) const; strvector Tokenize(const char * delimiter) const;
void Format ( const char * strFormat, ... ); void Format(const char * strFormat, ...);
stdstr& ToLower ( void ); stdstr& ToLower(void);
stdstr& ToUpper ( void ); stdstr& ToUpper(void);
void Replace ( const char search, const char replace ); void Replace(const char search, const char replace);
void Replace ( const char * search, const char replace ); void Replace(const char * search, const char replace);
void Replace ( const std::string & search, const std::string & replace ); void Replace(const std::string & search, const std::string & replace);
stdstr & Trim ( const char * chars2remove = "\t " ); stdstr & Trim(const char * chars2remove = "\t ");
stdstr & TrimLeft ( const char * chars2remove = "\t " ); stdstr & TrimLeft(const char * chars2remove = "\t ");
stdstr & TrimRight ( const char * chars2remove = "\t " ); stdstr & TrimRight(const char * chars2remove = "\t ");
stdstr & FromUTF16 ( const wchar_t * UTF16Source, bool * bSuccess = NULL); stdstr & FromUTF16(const wchar_t * UTF16Source, bool * bSuccess = NULL);
std::wstring ToUTF16 ( bool * bSuccess = NULL); std::wstring ToUTF16(unsigned int CodePage = CODEPAGE_UTF8, bool * bSuccess = NULL);
void ArgFormat( const char * strFormat, va_list & args);
void ArgFormat(const char * strFormat, va_list & args);
}; };
class stdstr_f: public stdstr class stdstr_f : public stdstr
{ {
public: public:
stdstr_f(const char * strFormat, ...) stdstr_f(const char * strFormat, ...)
{ {
va_list args; va_list args;
va_start(args, strFormat); va_start(args, strFormat);
ArgFormat(strFormat,args); ArgFormat(strFormat, args);
va_end(args); va_end(args);
} }
}; };

View File

@ -474,7 +474,7 @@ bool CRomBrowser::FillRomInfo(ROM_INFO * pRomInfo)
InternalName[count + 1] ^= InternalName[count + 2]; InternalName[count + 1] ^= InternalName[count + 2];
} }
InternalName[20] = '\0'; InternalName[20] = '\0';
wcscpy(pRomInfo->InternalName, stdstr(InternalName).ToUTF16().c_str()); wcscpy(pRomInfo->InternalName, stdstr(InternalName).ToUTF16(stdstr::CODEPAGE_932).c_str());
} }
pRomInfo->CartID[0] = *(RomData + 0x3F); pRomInfo->CartID[0] = *(RomData + 0x3F);
pRomInfo->CartID[1] = *(RomData + 0x3E); pRomInfo->CartID[1] = *(RomData + 0x3E);
@ -728,7 +728,7 @@ void CRomBrowser::FillRomList(strlist & FileList, const CPath & BaseDirectory, c
InternalName[count + 1] ^= InternalName[count + 2]; InternalName[count + 1] ^= InternalName[count + 2];
} }
InternalName[20] = '\0'; InternalName[20] = '\0';
wcscpy(RomInfo.InternalName, stdstr(InternalName).ToUTF16().c_str()); wcscpy(RomInfo.InternalName, stdstr(InternalName).ToUTF16(stdstr::CODEPAGE_932).c_str());
} }
RomInfo.RomSize = (int32_t)f->Size; RomInfo.RomSize = (int32_t)f->Size;
@ -1431,7 +1431,7 @@ void CRomBrowser::RomList_GetDispInfo(uint32_t pnmh)
break; break;
default: wcsncpy(lpdi->item.pszText, L" ", lpdi->item.cchTextMax); default: wcsncpy(lpdi->item.pszText, L" ", lpdi->item.cchTextMax);
} }
if (lpdi->item.pszText == NULL || wcslen(lpdi->item.pszText) == 0) { wcscpy(lpdi->item.pszText, L" "); } if (lpdi->item.pszText == NULL || wcslen(lpdi->item.pszText) == 0) { lpdi->item.pszText = L" "; }
} }
void CRomBrowser::RomList_OpenRom(uint32_t /*pnmh*/) void CRomBrowser::RomList_OpenRom(uint32_t /*pnmh*/)