Skip to content

Commit

Permalink
Adding a bunch of stuff for INI data processing.
Browse files Browse the repository at this point in the history
  • Loading branch information
MainMemory committed Mar 5, 2015
1 parent e617b53 commit 61ad0e7
Show file tree
Hide file tree
Showing 4 changed files with 354 additions and 43 deletions.
9 changes: 9 additions & 0 deletions include/SADXModLoader/SADXEnums.h
Expand Up @@ -569,4 +569,13 @@ enum Buttons : int
makemasks(Button, L)
};

enum Languages
{
Languages_Japanese,
Languages_English,
Languages_French,
Languages_Spanish,
Languages_German
};

#endif /* SADXMODLOADER_SADXENUMS_H */
78 changes: 77 additions & 1 deletion src/SADXModLoader/TextConv.cpp
Expand Up @@ -9,6 +9,8 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define CP_SJIS 932

/**
* Convert multibyte text to UTF-16.
* @param mbs Multibyte text, null-terminated.
Expand Down Expand Up @@ -51,7 +53,7 @@ char *UTF16toMBS(const wchar_t *wcs, unsigned int cp)
char *SJIStoUTF8(const char *sjis)
{
// Convert from Shift-JIS to UTF-16.
wchar_t *wcs = MBStoUTF16(sjis, 932);
wchar_t *wcs = MBStoUTF16(sjis, CP_SJIS);
if (!wcs)
return nullptr;

Expand All @@ -64,6 +66,48 @@ char *SJIStoUTF8(const char *sjis)
return mbs;
}

/**
* Convert UTF-8 text to Shift-JIS.
* @param utf8 UTF-8 text, null-terminated.
* @return Shift-JIS text (allocated via new[]), or nullptr on error.
*/
char *UTF8toSJIS(const char *utf8)
{
// Convert from UTF-8 to UTF-16.
wchar_t *wcs = MBStoUTF16(utf8, CP_UTF8);
if (!wcs)
return nullptr;

// Convert from UTF-16 to Shift-JIS.
char *mbs = UTF16toMBS(wcs, CP_SJIS);
delete[] wcs;
if (!mbs)
return nullptr;

return mbs;
}

/**
* Convert UTF-8 text to Windows-1252.
* @param utf8 UTF-8 text, null-terminated.
* @return Windows-1252 text (allocated via new[]), or nullptr on error.
*/
char *UTF8to1252(const char *utf8)
{
// Convert from UTF-8 to UTF-16.
wchar_t *wcs = MBStoUTF16(utf8, CP_UTF8);
if (!wcs)
return nullptr;

// Convert from UTF-16 to Shift-JIS.
char *mbs = UTF16toMBS(wcs, 1252);
delete[] wcs;
if (!mbs)
return nullptr;

return mbs;
}

/** C++ wrappers. **/

#include <string>
Expand Down Expand Up @@ -121,3 +165,35 @@ string SJIStoUTF8(const string &sjis)
delete[] utf8;
return ustr;
}

/**
* Convert UTF-8 text to Shift-JIS.
* @param utf8 UTF-8 text.
* @return Shift-JIS text, or empty string on error.
*/
string UTF8toSJIS(const string &utf8)
{
char *sjis = SJIStoUTF8(utf8.c_str());
if (!sjis)
return string();

string jstr(sjis);
delete[] sjis;
return jstr;
}

/**
* Convert UTF-8 text to Windows-1252.
* @param utf8 UTF-8 text.
* @return Windows-1252 text, or empty string on error.
*/
string UTF8to1252(const string &utf8)
{
char *w1252 = SJIStoUTF8(utf8.c_str());
if (!w1252)
return string();

string estr(w1252);
delete[] w1252;
return estr;
}
28 changes: 28 additions & 0 deletions src/SADXModLoader/TextConv.hpp
Expand Up @@ -35,6 +35,20 @@ char *UTF16toMBS(const wchar_t *wcs, unsigned int cp);
*/
char *SJIStoUTF8(const char *sjis);

/**
* Convert UTF-8 text to Shift-JIS.
* @param utf8 UTF-8 text, null-terminated.
* @return Shift-JIS text (allocated via new[]), or nullptr on error.
*/
char *UTF8toSJIS(const char *utf8);

/**
* Convert UTF-8 text to Windows-1252.
* @param utf8 UTF-8 text, null-terminated.
* @return Windows-1252 text (allocated via new[]), or nullptr on error.
*/
char *UTF8to1252(const char *utf8);

#ifdef __cplusplus
}

Expand Down Expand Up @@ -66,6 +80,20 @@ std::string UTF16toMBS(const std::wstring &wcs, unsigned int cp);
*/
std::string SJIStoUTF8(const std::string &sjis);

/**
* Convert UTF-8 text to Shift-JIS.
* @param utf8 UTF-8 text.
* @return Shift-JIS text, or empty string on error.
*/
std::string UTF8toSJIS(const std::string &utf8);

/**
* Convert UTF-8 text to Windows-1252.
* @param utf8 UTF-8 text.
* @return Windows-1252 text, or empty string on error.
*/
std::string UTF8to1252(const std::string &utf8);

#endif /* __cplusplus */

#endif /* TEXTCONV_H */

0 comments on commit 61ad0e7

Please sign in to comment.