tahoma2d/toonz/sources/image/compatibility/tfile_io.c

94 lines
1.9 KiB
C
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include "tfile_io.h"
2016-04-15 17:11:23 +12:00
#ifdef _WIN32
2016-03-19 06:57:51 +13:00
#include <windows.h>
#include <assert.h>
static LPSTR AtlW2AHelper(LPSTR lpa, LPCWSTR lpw, int nChars, UINT acp) {
2016-06-15 18:43:10 +12:00
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
2016-03-19 06:57:51 +13:00
*/
2016-06-15 18:43:10 +12:00
lpa[0] = '\0';
WideCharToMultiByte(acp, 0, lpw, -1, lpa, nChars, NULL, NULL);
return lpa;
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
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;
2016-03-19 06:57:51 +13:00
}
#else
#include <stdlib.h>
2016-06-15 18:43:10 +12:00
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;
2016-03-19 06:57:51 +13:00
}
#endif
/*-----------------------------------*/
#if defined(MACOSX) || defined(LINUX) || defined(FREEBSD)
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
FILE *_wfopen(const wchar_t *fname, const wchar_t *mode) {
char *cfname = convertWCHAR2CHAR(fname);
char *cmode = convertWCHAR2CHAR(mode);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
FILE *f = fopen(cfname, cmode);
free(cfname);
free(cmode);
return f;
2016-03-19 06:57:51 +13:00
}
/*-----------------------------------*/
2016-06-15 18:43:10 +12:00
int _wstat(const wchar_t *fname, struct stat *buf) {
char *cfname = convertWCHAR2CHAR(fname);
int rc = stat(cfname, buf);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
free(cfname);
return rc;
2016-03-19 06:57:51 +13:00
}
/*-----------------------------------*/
2016-06-15 18:43:10 +12:00
int _wremove(const wchar_t *fname) {
char *cfname = convertWCHAR2CHAR(fname);
int rc = remove(cfname);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
free(cfname);
return rc;
2016-03-19 06:57:51 +13:00
}
#endif