- more updates to handle unicode (most of the focus was getting tools working in windows using non ascii file paths)

This commit is contained in:
Mark Vejvoda
2011-05-23 19:23:00 +00:00
parent 737c577099
commit 1085f24c6f
17 changed files with 321 additions and 105 deletions

View File

@@ -16,6 +16,7 @@
#include <cstdlib>
#include <stdexcept>
#include "platform_util.h"
#include "conversion.h"
using namespace Shared::Util;
using namespace std;
@@ -683,11 +684,15 @@ void MapPreview::switchSurfaces(MapSurfaceType surf1, MapSurfaceType surf2) {
void MapPreview::loadFromFile(const string &path) {
// "Could not open file, result: 3 - 2 No such file or directory [C:\Documents and Settings\人間五\Application Data\megaglest\maps\clearings_in_the_woods.gbm]
#ifdef WIN32
FILE* f1= _wfopen(utf8_decode(path).c_str(), L"rb");
wstring wstr = utf8_decode(path);
FILE* f1= _wfopen(wstr.c_str(), L"rb");
#else
FILE *f1 = fopen(path.c_str(), "rb");
#endif
int fileErrno = errno;
if (f1 != NULL) {
//read header
@@ -750,7 +755,13 @@ void MapPreview::loadFromFile(const string &path) {
mapFileLoaded = path;
}
else {
#ifdef WIN32
DWORD error = GetLastError();
string strError = "Could not open file, result: " + intToStr(error) + " - " + intToStr(fileErrno) + " " + strerror(fileErrno) + " [" + path + "]";
throw strError;
#else
throw runtime_error("error opening map file: " + path);
#endif
}
}

View File

@@ -1832,6 +1832,13 @@ bool searchAndReplaceTextInFile(string fileName, string findText, string replace
fp2 = fopen(tempfileName.c_str(),"w");
#endif
if(fp1 == NULL) {
throw runtime_error("cannot open input file [" + fileName + "]");
}
if(fp2 == NULL) {
throw runtime_error("cannot open output file [" + tempfileName + "]");
}
while(fgets(buffer,MAX_LEN_SINGLE_LINE + 2,fp1)) {
buff_ptr = buffer;
if(findText != "") {
@@ -1881,14 +1888,24 @@ void copyFileTo(string fromFileName, string toFileName) {
out.put(in.get());
}
}
else if(in.is_open() == false) {
throw runtime_error("cannot open input file [" + fromFileName + "]");
}
else if(out.is_open() == false) {
throw runtime_error("cannot open input file [" + toFileName + "]");
}
//Close both files
in.close();
out.close();
#ifdef WIN32
fclose(fp1);
fclose(fp2);
if(fp1) {
fclose(fp1);
}
if(fp2) {
fclose(fp2);
}
#endif
}

View File

@@ -29,7 +29,7 @@ namespace Shared { namespace PlatformCommon {
const int IRC_SERVER_PORT = 6667;
void addlog (const char * fmt, ...) {
FILE * fp;
//FILE * fp;
char buf[1024];
va_list va_alist;

View File

@@ -53,7 +53,7 @@ LPWSTR Ansi2WideString(LPCSTR lpaszString) {
}
// Convert a wide Unicode string to an UTF8 string
std::string utf8_encode(const std::wstring &wstr) {
std::string utf8_encode(const std::wstring wstr) {
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo( size_needed, 0 );
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
@@ -64,7 +64,7 @@ std::string utf8_encode(const std::wstring &wstr) {
}
// Convert an UTF8 string to a wide Unicode String
std::wstring utf8_decode(const std::string &str) {
std::wstring utf8_decode(const std::string str) {
string friendly_path = str;
replaceAll(friendly_path, "/", "\\");
replaceAll(friendly_path, "\\\\", "\\");

View File

@@ -650,7 +650,7 @@ string ext(const string &s) {
i=s.find_last_of('.')+1;
if (i != string::npos) {
if (i == string::npos) {
throw runtime_error(string(__FILE__) + " line: " + intToStr(__LINE__) + " i==string::npos for [" + s + "]");
//}
return (s.substr(i, s.size()-i));
@@ -723,16 +723,24 @@ int round(float f){
// ==================== misc ====================
bool fileExists(const string &path){
bool fileExists(const string &path) {
#ifdef WIN32
FILE* file= _wfopen(utf8_decode(path).c_str(), L"rb");
wstring wstr = utf8_decode(path);
FILE* file= _wfopen(wstr.c_str(), L"rb");
#else
FILE* file= fopen(path.c_str(), "rb");
#endif
if(file!=NULL){
if(file != NULL) {
fclose(file);
return true;
}
else {
int fileErrno = errno;
#ifdef WIN32
DWORD error = GetLastError();
string strError = "Could not open file, result: " + intToStr(error) + " - " + intToStr(fileErrno) + " " + strerror(fileErrno) + " [" + path + "]";
#endif
}
return false;
}

View File

@@ -22,6 +22,7 @@
#include "types.h"
#include "properties.h"
#include "platform_common.h"
#include "platform_util.h"
#include "leak_dumper.h"
@@ -106,8 +107,12 @@ XmlNode *XmlIo::load(const string &path, std::map<string,string> mapTagReplaceme
config->setParameter(XMLUni::fgDOMValidate, true);
#endif
XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *document= parser->parseURI(path.c_str());
if(document==NULL){
#ifdef WIN32
if(document == NULL) {
document= parser->parseURI(utf8_decode(path).c_str());
}
#endif
if(document == NULL) {
throw runtime_error("Can not parse URL: " + path);
}