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