tahoma2d/toonz/sources/common/tiio/compatibility/tfile_io.c
Ivan Mahonin 2fc36cb841 Allow to cross compile for Windows with Linux/Mingw (part 2 of 3, bugfixes) (#1149)
* #mingw #bug: place explicit instantiations on templates before first use

* #mingw #bug: remove API attributes from inline functions

* #mingw #bug: add 'static' attribute for local functions

* #mingw #bug: fix API attributes for splitSpeedInOutSegment

* #mingw #bug: replace strstream to stringstream

* #mingw #bug: remove cross references to plasticskeleton from tnzcore

* #mingw #bug: fix bug with order of initializaition of static variables

* #mingw #bug: fix glutInit
2017-05-19 19:20:33 +09:00

93 lines
1.8 KiB
C

#include "tfile_io.h"
#ifdef _WIN32
#include <windows.h>
#include <assert.h>
static LPSTR AtlW2AHelper(LPSTR lpa, LPCWSTR lpw, int nChars, UINT acp) {
assert(lpw != NULL);
assert(lpa != NULL);
/*
verify that no illegal character present
since lpa was allocated based on the size of lpw
don t worry about the number of chars
*/
lpa[0] = '\0';
WideCharToMultiByte(acp, 0, lpw, -1, lpa, nChars, NULL, NULL);
return lpa;
}
char *convertWCHAR2CHAR(const wchar_t *fname) {
int size = 0;
LPCWSTR lpw = fname;
char *name = NULL;
char *outName = 0;
if (lpw) {
LPSTR pStr = 0;
size = (lstrlenW(lpw) + 1) * 2;
pStr = (LPSTR)malloc(size * sizeof(char));
name = AtlW2AHelper(pStr, lpw, size, 0);
}
return name;
}
#else
#include <stdlib.h>
char *convertWCHAR2CHAR(const wchar_t *wc) {
int count = 0;
const wchar_t *ptr = wc;
char *c = 0;
char *buff;
while ((*ptr) != '\0') {
++count;
++ptr;
}
c = (char *)malloc((count + 1) * sizeof(char));
buff = c;
ptr = wc;
while ((*ptr) != '\0') {
*c = *ptr;
++c;
++ptr;
}
*c = 0;
return buff;
}
#endif
/*-----------------------------------*/
#if defined(MACOSX) || defined(LINUX)
FILE *_wfopen(const wchar_t *fname, const wchar_t *mode) {
char *cfname = convertWCHAR2CHAR(fname);
char *cmode = convertWCHAR2CHAR(mode);
FILE *f = fopen(cfname, cmode);
free(cfname);
free(cmode);
return f;
}
/*-----------------------------------*/
int _wstat(const wchar_t *fname, struct stat *buf) {
char *cfname = convertWCHAR2CHAR(fname);
int rc = stat(cfname, buf);
free(cfname);
return rc;
}
/*-----------------------------------*/
int _wremove(const wchar_t *fname) {
char *cfname = convertWCHAR2CHAR(fname);
int rc = remove(cfname);
free(cfname);
return rc;
}
#endif