#include "DebugServices.h"
#include <windows.h>
#include <stdio.h>
#include "isocode.h"
#include "loclibrary.h"
#include "Shlwapi.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <wchar.h>
#ifdef __cplusplus
extern "c" {
#endif
#ifdef _MSC_VER
#define swprintf _snwprintf
#define snprintf _snprintf
#endif
#define DEFAULT_LANG_CODE "en"
static LANGID _getUserLanguage( void ) {
return GetUserDefaultUILanguage();
}
static int _getISOCode(LANGID wLangID, char *isoLangCode, int codeLen) {
int i;
unsigned short langCode;
for (i = 0; i < NUM_ISOCODES; i++) {
int startIndex = i * MODULO_ISOCODES;
langCode = (ISOCODES[startIndex] << 8);
langCode += ( (unsigned short) (ISOCODES[startIndex + 1]) );
if (langCode == wLangID) {
char *langStr = (char *)&(ISOCODES[startIndex+2]);
strncpy(isoLangCode, langStr, codeLen);
return 0;
}
}
return 1;
}
static char isoLangCode[LANG_CODE_LEN + 1] = "";
static LANGID wLangID = (LANGID) -1;
static void _setLanguageIfNeeded(void) {
if (!strncmp(isoLangCode,"",LANG_CODE_LEN + 1)) {
if (wLangID == (LANGID) -1) {
wLangID = _getUserLanguage();
}
if (_getISOCode(wLangID, isoLangCode, LANG_CODE_LEN + 1)) {
strncpy(isoLangCode, DEFAULT_LANG_CODE, LANG_CODE_LEN+1);
}
}
}
static char appPathNameA[MAX_PATH] = "";
int PathForResourceA ( HMODULE module, const char *name, char *locFile, int locFileLen)
{
int ret = 0;
if ( !strcmp( appPathNameA, "" ) )
{
char folder[MAX_PATH];
char * app;
GetModuleFileNameA( module, folder, MAX_PATH );
app = strrchr( folder, '\\' );
require_action( app, exit, ret = 0 );
*app++ = '\0';
snprintf( appPathNameA, MAX_PATH, "%s\\Resources\\%s", folder, app );
}
ret = PathForResourceWithPathA (appPathNameA, name, locFile, locFileLen);
exit:
return ret;
}
static wchar_t appPathNameW[MAX_PATH] = L"";
int PathForResourceW ( HMODULE module, const wchar_t *name, wchar_t *locFile, int locFileLen)
{
int ret = 0;
if ( !wcscmp( appPathNameW, L"" ) )
{
wchar_t folder[MAX_PATH];
wchar_t * app;
GetModuleFileNameW( module, folder, MAX_PATH);
app = wcsrchr( folder, '\\' );
require_action( app, exit, ret = 0 );
*app++ = '\0';
swprintf( appPathNameW, MAX_PATH, L"%ls\\Resources\\%ls", folder, app );
}
ret = PathForResourceWithPathW (appPathNameW, name, locFile, locFileLen);
exit:
return ret;
}
#define TMP_BUF_SIZE MAX_PATH
int PathForResourceWithPathA (const char *path, const char *nm,
char *locFile, int locFileLen) {
char tmpBuffer[TMP_BUF_SIZE];
snprintf(tmpBuffer, MAX_PATH, "%s.Resources\\%s", path, nm);
if (!PathFileExistsA(tmpBuffer)) {
_setLanguageIfNeeded();
snprintf(tmpBuffer, TMP_BUF_SIZE,
"%s.Resources\\%s.lproj", path, isoLangCode);
if (PathFileExistsA(tmpBuffer)) {
snprintf(tmpBuffer, TMP_BUF_SIZE, "%s\\%s", tmpBuffer, nm);
if (!PathFileExistsA(tmpBuffer)) return 0;
strncpy(locFile, tmpBuffer, locFileLen);
return (int) strlen(locFile);
}
snprintf(tmpBuffer, TMP_BUF_SIZE, "%s.Resources\\%s.lproj\\%s",
path, DEFAULT_LANG_CODE, nm);
if (!PathFileExistsA(tmpBuffer)) return 0;
}
strncpy(locFile, tmpBuffer, locFileLen);
return (int) strlen(locFile);
}
int PathForResourceWithPathW (const wchar_t *path, const wchar_t *nm,
wchar_t *locFile, int locFileLen) {
wchar_t tmpBuffer[TMP_BUF_SIZE];
swprintf(tmpBuffer, TMP_BUF_SIZE, L"%ls.Resources\\%ls", path, nm);
if (!PathFileExistsW(tmpBuffer)) {
_setLanguageIfNeeded();
swprintf(tmpBuffer, TMP_BUF_SIZE,
L"%ls.Resources\\%S.lproj", path, isoLangCode);
if (PathFileExistsW(tmpBuffer)) {
swprintf(tmpBuffer, TMP_BUF_SIZE, L"%ls\\%ls", tmpBuffer, nm);
if (!PathFileExistsW(tmpBuffer)) return 0;
wcsncpy(locFile, tmpBuffer, locFileLen);
return (int) wcslen(locFile);
}
swprintf(tmpBuffer, TMP_BUF_SIZE, L"%ls.Resources\\%S.lproj\\%ls",
path, DEFAULT_LANG_CODE, nm);
if (!PathFileExistsW(tmpBuffer)) return 0;
}
wcsncpy(locFile, tmpBuffer, locFileLen);
return (int) wcslen(locFile);
}
#ifdef __cplusplus
}
#endif