Dedicated server no longer depends on qt. Added code for utf-8 conversion on windows and linux (without qt). Some small issues remain (like the data-dir and charset for the server).

This commit is contained in:
lotodore
2007-10-01 00:33:05 +00:00
parent c17be5af25
commit b4a1ce8bd5
18 changed files with 465 additions and 60 deletions
+34
View File
@@ -0,0 +1,34 @@
/***************************************************************************
* Copyright (C) 2007 by Lothar May *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/* Helper class for character conversion functions. */
#ifndef _CRYPTHELPER_H_
#define _CRYPTHELPER_H_
#include <string>
class ConvHelper
{
public:
static std::string NativeToUtf8(const std::string &inStr);
static std::string Utf8ToNative(const std::string &inStr);
};
#endif
+95
View File
@@ -0,0 +1,95 @@
/***************************************************************************
* Copyright (C) 2007 by Lothar May *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "convhelper.h"
#ifdef _WIN32
#error This file is not for Windows.
#endif
#include <string>
#include <iostream>
#include <iconv.h>
#include <errno.h>
using namespace std;
string
ConvHelper::NativeToUtf8(const std::string &inStr)
{
string retStr(inStr);
size_t insize = inStr.length();
char *inbuf = const_cast<char *>(inStr.data());
const size_t c_outsize = insize * 6; // max size of utf-8 char is 6 per input char
size_t outsize = c_outsize;
char *c_outbuf = new char[outsize];
char *outbuf = c_outbuf;
//nl_langinfo(CODESET)
iconv_t conversion = iconv_open("UTF-8", "ISO-8859-1");
if (conversion == (iconv_t)(-1))
cout << "iconv_open() failed: " << strerror(errno) << endl;
else
{
size_t retval = iconv(conversion, &inbuf, &insize, &outbuf, &outsize);
if (retval == -1)
cout << "iconv() failed: " << strerror(errno) << endl;
retStr = string(c_outbuf, c_outsize - outsize);
}
delete[] c_outbuf;
iconv_close(conversion);
return retStr;
}
string
ConvHelper::Utf8ToNative(const std::string &inStr)
{
string retStr(inStr);
size_t insize = inStr.length();
char *inbuf = const_cast<char *>(inStr.data());
const size_t c_outsize = insize;
size_t outsize = c_outsize;
char *c_outbuf = new char[outsize];
char *outbuf = c_outbuf;
iconv_t conversion = iconv_open("ISO-8859-1", "UTF-8");
if (conversion == (iconv_t)(-1))
cout << "iconv_open() failed: " << strerror(errno) << endl;
else
{
size_t retval = iconv(conversion, &inbuf, &insize, &outbuf, &outsize);
if (retval == -1)
cout << "iconv() failed: " << strerror(errno) << endl;
retStr = string(c_outbuf, c_outsize - outsize);
}
delete[] c_outbuf;
iconv_close(conversion);
return retStr;
}
+76
View File
@@ -0,0 +1,76 @@
/***************************************************************************
* Copyright (C) 2007 by Lothar May *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "convhelper.h"
#ifndef _WIN32
#error This file is Windows only.
#endif
#include <windows.h>
using namespace std;
static string
Convert(const std::string &inStr, int fromCP, int toCP)
{
// convert str from current Windows source to target charset
string retStr(inStr);
if (!inStr.empty())
{
int len = (int)inStr.length() + 1;
int reqLen = ::MultiByteToWideChar(fromCP, 0, inStr.c_str(), len, NULL, 0);
if (reqLen)
{
wchar_t *wstr = new wchar_t[reqLen];
wstr[0] = L'\0';
if (::MultiByteToWideChar(fromCP, 0, inStr.c_str(), len, wstr, reqLen) == (int)reqLen)
{
len = reqLen;
reqLen = ::WideCharToMultiByte(toCP, 0, wstr, len, NULL, 0, NULL, NULL);
if (reqLen)
{
char *str = new char[reqLen];
if (::WideCharToMultiByte(toCP, 0, wstr, len, str, reqLen, NULL, NULL) == (int)reqLen)
retStr = str;
delete[] str;
}
}
delete[] wstr;
}
}
return retStr;
}
string
ConvHelper::NativeToUtf8(const std::string &inStr)
{
return Convert(inStr, CP_ACP, CP_UTF8);
}
string
ConvHelper::Utf8ToNative(const std::string &inStr)
{
return Convert(inStr, CP_UTF8, CP_ACP);
}