| version 1.4, 2004/01/10 17:24:01 | version 1.11, 2005/03/20 08:58:19 | 
| Line 2 | Line 2 | 
 | #include        "dosio.h" | #include        "dosio.h" | 
 |  |  | 
 |  |  | 
| static  char    curpath[MAX_PATH]; | static  OEMCHAR curpath[MAX_PATH]; | 
| static  char    *curfilep = curpath; | static  OEMCHAR *curfilep = curpath; | 
|  |  | 
| #define ISKANJI(c)      (((((c) ^ 0x20) - 0xa1) & 0xff) < 0x3c) |  | 
|  |  | 
| #if defined(UNICODE) |  | 
|  |  | 
| static HANDLE CreateFile_A(LPCSTR lpFileName, |  | 
| DWORD dwDesiredAccess, |  | 
| DWORD dwShareMode, |  | 
| LPSECURITY_ATTRIBUTES lpSecurityAttributes, |  | 
| DWORD dwCreationDisposition, |  | 
| DWORD dwFlagsAndAttributes, |  | 
| HANDLE hTemplateFile) { |  | 
|  |  | 
| TCHAR   FileNameW[MAX_PATH*2]; |  | 
|  |  | 
| MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpFileName, -1, |  | 
| FileNameW, sizeof(FileNameW)/sizeof(TCHAR)); |  | 
| return(CreateFile(FileNameW, dwDesiredAccess, dwShareMode, |  | 
| lpSecurityAttributes, dwCreationDisposition, |  | 
| dwFlagsAndAttributes, hTemplateFile)); |  | 
| } |  | 
|  |  | 
| static inline BOOL DeleteFile_A(LPCSTR lpFileName) { |  | 
|  |  | 
| TCHAR   FileNameW[MAX_PATH*2]; |  | 
|  |  | 
| MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpFileName, -1, |  | 
| FileNameW, sizeof(FileNameW)/sizeof(TCHAR)); |  | 
| return(DeleteFile(FileNameW)); |  | 
| } |  | 
|  |  | 
| static inline DWORD GetFileAttributes_A(LPCSTR lpFileName) { |  | 
|  |  | 
| TCHAR   FileNameW[MAX_PATH*2]; |  | 
|  |  | 
| MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpFileName, -1, |  | 
| FileNameW, sizeof(FileNameW)/sizeof(TCHAR)); |  | 
| return(GetFileAttributes(FileNameW)); |  | 
| } |  | 
|  |  | 
| static inline BOOL CreateDirectory_A(LPCSTR lpFileName, |  | 
| LPSECURITY_ATTRIBUTES atr) { |  | 
|  |  | 
| TCHAR   FileNameW[MAX_PATH*2]; |  | 
|  |  | 
| MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpFileName, -1, |  | 
| FileNameW, sizeof(FileNameW)/sizeof(TCHAR)); |  | 
| return(CreateDirectory(FileNameW, atr)); |  | 
| } |  | 
|  |  | 
| static inline HANDLE FindFirstFile_A(LPCSTR lpFileName, |  | 
| WIN32_FIND_DATA *w32fd) { |  | 
|  |  | 
| TCHAR   FileNameW[MAX_PATH*2]; |  | 
|  |  | 
| MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpFileName, -1, |  | 
| FileNameW, sizeof(FileNameW)/sizeof(TCHAR)); |  | 
| return(FindFirstFile(FileNameW, w32fd)); |  | 
| } |  | 
| #else |  | 
|  |  | 
| #define CreateFile_A(a, b, c, d, e, f, g)       \ |  | 
| CreateFile(a, b, c, d, e, f, g) |  | 
| #define DeleteFile_A(a)                 DeleteFile(a) |  | 
| #define GetFileAttributes_A(a)  GetFileAttributes(a) |  | 
| #define CreateDirectory_A(a, b) CreateDirectory(a, b) |  | 
| #define FindFirstFile_A(a, b)   FindFirstFile(a, b) |  | 
|  |  | 
| #endif |  | 
 |  |  | 
 |  |  | 
 | // ---- | // ---- | 
| Line 81  void dosio_init(void) { } | Line 12  void dosio_init(void) { } | 
 | void dosio_term(void) { } | void dosio_term(void) { } | 
 |  |  | 
 | // ファイル操作 | // ファイル操作 | 
| FILEH file_open(const char *path) { | FILEH DOSIOCALL file_open(const OEMCHAR *path) { | 
 |  |  | 
 | FILEH   ret; | FILEH   ret; | 
 |  |  | 
| if ((ret = CreateFile_A(path, GENERIC_READ | GENERIC_WRITE, | if ((ret = CreateFile(path, GENERIC_READ | GENERIC_WRITE, | 
 | 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) | 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) | 
 | == INVALID_HANDLE_VALUE) { | == INVALID_HANDLE_VALUE) { | 
| if ((ret = CreateFile_A(path, GENERIC_READ, | if ((ret = CreateFile(path, GENERIC_READ, | 
 | 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) | 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) | 
 | == INVALID_HANDLE_VALUE) { | == INVALID_HANDLE_VALUE) { | 
 | return(FILEH_INVALID); | return(FILEH_INVALID); | 
| Line 97  FILEH file_open(const char *path) { | Line 28  FILEH file_open(const char *path) { | 
 | return(ret); | return(ret); | 
 | } | } | 
 |  |  | 
| FILEH file_open_rb(const char *path) { | FILEH DOSIOCALL file_open_rb(const OEMCHAR *path) { | 
 |  |  | 
 | FILEH   ret; | FILEH   ret; | 
 |  |  | 
| if ((ret = CreateFile_A(path, GENERIC_READ, FILE_SHARE_READ, 0, | if ((ret = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, 0, | 
 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) | 
 | == INVALID_HANDLE_VALUE) { | == INVALID_HANDLE_VALUE) { | 
 | return(FILEH_INVALID); | return(FILEH_INVALID); | 
| Line 109  FILEH file_open_rb(const char *path) { | Line 40  FILEH file_open_rb(const char *path) { | 
 | return(ret); | return(ret); | 
 | } | } | 
 |  |  | 
| FILEH file_create(const char *path) { | FILEH DOSIOCALL file_create(const OEMCHAR *path) { | 
 |  |  | 
 | FILEH   ret; | FILEH   ret; | 
 |  |  | 
| if ((ret = CreateFile_A(path, GENERIC_READ | GENERIC_WRITE, | if ((ret = CreateFile(path, GENERIC_READ | GENERIC_WRITE, | 
 | 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) | 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) | 
 | == INVALID_HANDLE_VALUE) { | == INVALID_HANDLE_VALUE) { | 
 | return(FILEH_INVALID); | return(FILEH_INVALID); | 
| Line 121  FILEH file_create(const char *path) { | Line 52  FILEH file_create(const char *path) { | 
 | return(ret); | return(ret); | 
 | } | } | 
 |  |  | 
| long file_seek(FILEH handle, long pointer, int method) { | long DOSIOCALL file_seek(FILEH handle, long pointer, int method) { | 
 |  |  | 
 | return(SetFilePointer(handle, pointer, 0, method)); | return(SetFilePointer(handle, pointer, 0, method)); | 
 | } | } | 
 |  |  | 
| UINT file_read(FILEH handle, void *data, UINT length) { | UINT DOSIOCALL file_read(FILEH handle, void *data, UINT length) { | 
 |  |  | 
 | DWORD   readsize; | DWORD   readsize; | 
 |  |  | 
| Line 136  UINT file_read(FILEH handle, void *data, | Line 67  UINT file_read(FILEH handle, void *data, | 
 | return(readsize); | return(readsize); | 
 | } | } | 
 |  |  | 
| UINT file_write(FILEH handle, const void *data, UINT length) { | UINT DOSIOCALL file_write(FILEH handle, const void *data, UINT length) { | 
 |  |  | 
 | DWORD   writesize; | DWORD   writesize; | 
 |  |  | 
| Line 151  UINT file_write(FILEH handle, const void | Line 82  UINT file_write(FILEH handle, const void | 
 | return(0); | return(0); | 
 | } | } | 
 |  |  | 
| short file_close(FILEH handle) { | short DOSIOCALL file_close(FILEH handle) { | 
 |  |  | 
 | CloseHandle(handle); | CloseHandle(handle); | 
 | return(0); | return(0); | 
 | } | } | 
 |  |  | 
| UINT file_getsize(FILEH handle) { | UINT DOSIOCALL file_getsize(FILEH handle) { | 
 |  |  | 
 | return(GetFileSize(handle, NULL)); | return(GetFileSize(handle, NULL)); | 
 | } | } | 
 |  |  | 
| static BOOL cnvdatetime(FILETIME *file, DOSDATE *dosdate, DOSTIME *dostime) { | static BRESULT DOSIOCALL cnvdatetime(FILETIME *file, DOSDATE *dosdate, DOSTIME *dostime) { | 
 |  |  | 
 | FILETIME        localtime; | FILETIME        localtime; | 
 | SYSTEMTIME      systime; | SYSTEMTIME      systime; | 
| Line 184  static BOOL cnvdatetime(FILETIME *file, | Line 115  static BOOL cnvdatetime(FILETIME *file, | 
 | return(SUCCESS); | return(SUCCESS); | 
 | } | } | 
 |  |  | 
| short file_getdatetime(FILEH handle, DOSDATE *dosdate, DOSTIME *dostime) { | short DOSIOCALL file_getdatetime(FILEH handle, DOSDATE *dosdate, DOSTIME *dostime) { | 
 |  |  | 
 | FILETIME        lastwrite; | FILETIME        lastwrite; | 
 |  |  | 
| Line 195  short file_getdatetime(FILEH handle, DOS | Line 126  short file_getdatetime(FILEH handle, DOS | 
 | return(0); | return(0); | 
 | } | } | 
 |  |  | 
| short file_delete(const char *path) { | short DOSIOCALL file_delete(const OEMCHAR *path) { | 
 |  |  | 
| return(DeleteFile_A(path)?0:-1); | return(DeleteFile(path)?0:-1); | 
 | } | } | 
 |  |  | 
| short file_attr(const char *path) { | short DOSIOCALL file_attr(const OEMCHAR *path) { | 
 |  |  | 
| return((short)GetFileAttributes_A(path)); | return((short)GetFileAttributes(path)); | 
 | } | } | 
 |  |  | 
| short file_dircreate(const char *path) { | short DOSIOCALL file_dircreate(const OEMCHAR *path) { | 
 |  |  | 
| return(CreateDirectory_A(path, NULL)?0:-1); | return(CreateDirectory(path, NULL)?0:-1); | 
 | } | } | 
 |  |  | 
 |  |  | 
 | // カレントファイル操作 | // カレントファイル操作 | 
| void file_setcd(const char *exepath) { | void DOSIOCALL file_setcd(const OEMCHAR *exepath) { | 
 |  |  | 
| file_cpyname(curpath, exepath, sizeof(curpath)); | file_cpyname(curpath, exepath, NELEMENTS(curpath)); | 
 | curfilep = file_getname(curpath); | curfilep = file_getname(curpath); | 
 | *curfilep = '\0'; | *curfilep = '\0'; | 
 | } | } | 
 |  |  | 
| char *file_getcd(const char *path) { | OEMCHAR * DOSIOCALL file_getcd(const OEMCHAR *path) { | 
 |  |  | 
| *curfilep = '\0'; | file_cpyname(curfilep, path, NELEMENTS(curpath) - (curfilep - curpath)); | 
| file_catname(curpath, path, sizeof(curpath)); |  | 
 | return(curpath); | return(curpath); | 
 | } | } | 
 |  |  | 
| FILEH file_open_c(const char *path) { | FILEH DOSIOCALL file_open_c(const OEMCHAR *path) { | 
 |  |  | 
| *curfilep = '\0'; | file_cpyname(curfilep, path, NELEMENTS(curpath) - (curfilep - curpath)); | 
| file_catname(curpath, path, sizeof(curpath)); |  | 
 | return(file_open(curpath)); | return(file_open(curpath)); | 
 | } | } | 
 |  |  | 
| FILEH file_open_rb_c(const char *path) { | FILEH DOSIOCALL file_open_rb_c(const OEMCHAR *path) { | 
 |  |  | 
| *curfilep = '\0'; | file_cpyname(curfilep, path, NELEMENTS(curpath) - (curfilep - curpath)); | 
| file_catname(curpath, path, sizeof(curpath)); |  | 
 | return(file_open_rb(curpath)); | return(file_open_rb(curpath)); | 
 | } | } | 
 |  |  | 
| FILEH file_create_c(const char *path) { | FILEH DOSIOCALL file_create_c(const OEMCHAR *path) { | 
 |  |  | 
| *curfilep = '\0'; | file_cpyname(curfilep, path, NELEMENTS(curpath) - (curfilep - curpath)); | 
| file_catname(curpath, path, sizeof(curpath)); |  | 
 | return(file_create(curpath)); | return(file_create(curpath)); | 
 | } | } | 
 |  |  | 
| short file_delete_c(const char *path) { | short DOSIOCALL file_delete_c(const OEMCHAR *path) { | 
 |  |  | 
| *curfilep = '\0'; | file_cpyname(curfilep, path, NELEMENTS(curpath) - (curfilep - curpath)); | 
| file_catname(curpath, path, sizeof(curpath)); |  | 
 | return(file_delete(curpath)); | return(file_delete(curpath)); | 
 | } | } | 
 |  |  | 
| short file_attr_c(const char *path) { | short DOSIOCALL file_attr_c(const OEMCHAR *path) { | 
 |  |  | 
| *curfilep = '\0'; | file_cpyname(curfilep, path, NELEMENTS(curpath) - (curfilep - curpath)); | 
| file_catname(curpath, path, sizeof(curpath)); |  | 
 | return(file_attr(curpath)); | return(file_attr(curpath)); | 
 | } | } | 
 |  |  | 
 |  |  | 
| FLISTH file_list1st(const char *dir, FLINFO *fli) { | static BRESULT DOSIOCALL setflist(WIN32_FIND_DATA *w32fd, FLINFO *fli) { | 
|  |  | 
|  | #if !defined(_WIN32_WCE) | 
|  | if ((w32fd->dwFileAttributes & FILEATTR_DIRECTORY) && | 
|  | ((!file_cmpname(w32fd->cFileName, OEMTEXT("."))) || | 
|  | (!file_cmpname(w32fd->cFileName, OEMTEXT(".."))))) { | 
|  | return(FAILURE); | 
|  | } | 
|  | #endif | 
|  | fli->caps = FLICAPS_SIZE | FLICAPS_ATTR | FLICAPS_DATE | FLICAPS_TIME; | 
|  | fli->size = w32fd->nFileSizeLow; | 
|  | fli->attr = w32fd->dwFileAttributes; | 
|  | cnvdatetime(&w32fd->ftLastWriteTime, &fli->date, &fli->time); | 
|  | milstr_ncpy(fli->path, w32fd->cFileName, NELEMENTS(fli->path)); | 
|  | return(SUCCESS); | 
|  | } | 
|  |  | 
|  | FLISTH DOSIOCALL file_list1st(const OEMCHAR *dir, FLINFO *fli) { | 
 |  |  | 
| char                    path[MAX_PATH]; | OEMCHAR                 path[MAX_PATH]; | 
 | HANDLE                  hdl; | HANDLE                  hdl; | 
 | WIN32_FIND_DATA w32fd; | WIN32_FIND_DATA w32fd; | 
 |  |  | 
| milsjis_ncpy(path, dir, sizeof(path)); | milstr_ncpy(path, dir, NELEMENTS(path)); | 
| file_setseparator(path, sizeof(path)); | file_setseparator(path, NELEMENTS(path)); | 
| milsjis_ncat(path, "*.*", sizeof(path)); | milstr_ncat(path, OEMTEXT("*.*"), NELEMENTS(path)); | 
 | TRACEOUT(("file_list1st %s", path)); | TRACEOUT(("file_list1st %s", path)); | 
| hdl = FindFirstFile_A(path, &w32fd); | hdl = FindFirstFile(path, &w32fd); | 
| if ((hdl != INVALID_HANDLE_VALUE) && (fli)) { | if (hdl != INVALID_HANDLE_VALUE) { | 
| fli->caps = FLICAPS_SIZE | FLICAPS_ATTR | FLICAPS_DATE | FLICAPS_TIME; | do { | 
| fli->size = w32fd.nFileSizeLow; | if (setflist(&w32fd, fli) == SUCCESS) { | 
| fli->attr = w32fd.dwFileAttributes; | return(hdl); | 
| cnvdatetime(&w32fd.ftLastWriteTime, &fli->date, &fli->time); | } | 
| #if defined(UNICODE) | } while(FindNextFile(hdl, &w32fd)); | 
| WideCharToMultiByte(CP_ACP, 0, w32fd.cFileName, -1, | FindClose(hdl); | 
| fli->path, sizeof(fli->path), NULL, NULL); |  | 
| #else |  | 
| milstr_ncpy(fli->path, w32fd.cFileName, sizeof(fli->path)); |  | 
| TRACEOUT(("-> %s", w32fd.cFileName)); |  | 
| #endif |  | 
 | } | } | 
| return(hdl); | return(FLISTH_INVALID); | 
 | } | } | 
 |  |  | 
| BOOL file_listnext(FLISTH hdl, FLINFO *fli) { | BRESULT DOSIOCALL file_listnext(FLISTH hdl, FLINFO *fli) { | 
 |  |  | 
 | WIN32_FIND_DATA w32fd; | WIN32_FIND_DATA w32fd; | 
 |  |  | 
| if (!FindNextFile(hdl, &w32fd)) { | while(FindNextFile(hdl, &w32fd)) { | 
| return(FAILURE); | if (setflist(&w32fd, fli) == SUCCESS) { | 
| } | return(SUCCESS); | 
| if (fli) { | } | 
| fli->caps = FLICAPS_SIZE | FLICAPS_ATTR | FLICAPS_DATE | FLICAPS_TIME; |  | 
| fli->size = w32fd.nFileSizeLow; |  | 
| fli->attr = w32fd.dwFileAttributes; |  | 
| cnvdatetime(&w32fd.ftLastWriteTime, &fli->date, &fli->time); |  | 
| #if defined(UNICODE) |  | 
| WideCharToMultiByte(CP_ACP, 0, w32fd.cFileName, -1, |  | 
| fli->path, sizeof(fli->path), NULL, NULL); |  | 
| #else |  | 
| milstr_ncpy(fli->path, w32fd.cFileName, sizeof(fli->path)); |  | 
| #endif |  | 
 | } | } | 
| return(SUCCESS); | return(FAILURE); | 
 | } | } | 
 |  |  | 
| void file_listclose(FLISTH hdl) { | void DOSIOCALL file_listclose(FLISTH hdl) { | 
 |  |  | 
 | FindClose(hdl); | FindClose(hdl); | 
 | } | } | 
 |  |  | 
 |  |  | 
| char *file_getname(char *path) { | OEMCHAR * DOSIOCALL file_getname(const OEMCHAR *path) { | 
 |  |  | 
| char    *ret; | const OEMCHAR   *ret; | 
|  | int                     csize; | 
 |  |  | 
 | ret = path; | ret = path; | 
| while(*path != '\0') { | while((csize = milstr_charsize(path)) != 0) { | 
| if (!ISKANJI(*path)) { | if (csize == 1) { | 
 | if ((*path == '\\') || (*path == '/') || (*path == ':')) { | if ((*path == '\\') || (*path == '/') || (*path == ':')) { | 
 | ret = path + 1; | ret = path + 1; | 
 | } | } | 
 | } | } | 
| else { | path += csize; | 
| if (path[1]) { |  | 
| path++; |  | 
| } |  | 
| } |  | 
| path++; |  | 
 | } | } | 
| return(ret); | return((OEMCHAR *)ret); | 
 | } | } | 
 |  |  | 
| void file_cutname(char *path) { | void DOSIOCALL file_cutname(OEMCHAR *path) { | 
 |  |  | 
| char    *p; | OEMCHAR *p; | 
 |  |  | 
 | p = file_getname(path); | p = file_getname(path); | 
 | p[0] = '\0'; | p[0] = '\0'; | 
 | } | } | 
 |  |  | 
| char *file_getext(char *path) { | OEMCHAR * DOSIOCALL file_getext(const OEMCHAR *path) { | 
 |  |  | 
| char    *p; | const OEMCHAR   *p; | 
| char    *q; | const OEMCHAR   *q; | 
|  | int                     csize; | 
 |  |  | 
 | p = file_getname(path); | p = file_getname(path); | 
 | q = NULL; | q = NULL; | 
|  | while((csize = milstr_charsize(p)) != 0) { | 
| while(*p != '\0') { | if ((csize == 1) && (*p == '.')) { | 
| if (!ISKANJI(*p)) { | q = p + 1; | 
| if (*p == '.') { |  | 
| q = p + 1; |  | 
| } |  | 
| } |  | 
| else { |  | 
| if (p[1]) { |  | 
| p++; |  | 
| } |  | 
 | } | } | 
| p++; | p += csize; | 
 | } | } | 
| if (!q) { | if (q == NULL) { | 
 | q = p; | q = p; | 
 | } | } | 
| return(q); | return((OEMCHAR *)q); | 
 | } | } | 
 |  |  | 
| void file_cutext(char *path) { | void DOSIOCALL file_cutext(OEMCHAR *path) { | 
 |  |  | 
| char    *p; | OEMCHAR *p; | 
| char    *q; | OEMCHAR *q; | 
|  | int             csize; | 
 |  |  | 
 | p = file_getname(path); | p = file_getname(path); | 
 | q = NULL; | q = NULL; | 
|  | while((csize = milstr_charsize(p)) != 0) { | 
| while(*p != '\0') { | if ((csize == 1) && (*p == '.')) { | 
| if (!ISKANJI(*p)) { | q = p; | 
| if (*p == '.') { |  | 
| q = p; |  | 
| } |  | 
| } |  | 
| else { |  | 
| if (p[1]) { |  | 
| p++; |  | 
| } |  | 
 | } | } | 
| p++; | p += csize; | 
 | } | } | 
 | if (q) { | if (q) { | 
 | *q = '\0'; | *q = '\0'; | 
 | } | } | 
 | } | } | 
 |  |  | 
| void file_cutseparator(char *path) { | void DOSIOCALL file_cutseparator(OEMCHAR *path) { | 
 |  |  | 
 | int             pos; | int             pos; | 
 |  |  | 
| pos = strlen(path) - 1; | pos = OEMSTRLEN(path) - 1; | 
 | if ((pos > 0) &&                                                        // 2文字以上でー | if ((pos > 0) &&                                                        // 2文字以上でー | 
 | (path[pos] == '\\') &&                                  // ケツが \ でー | (path[pos] == '\\') &&                                  // ケツが \ でー | 
 | (!milstr_kanji2nd(path, pos)) &&                // 漢字の2バイト目ぢゃなくてー | (!milstr_kanji2nd(path, pos)) &&                // 漢字の2バイト目ぢゃなくてー | 
| Line 413  void file_cutseparator(char *path) { | Line 322  void file_cutseparator(char *path) { | 
 | } | } | 
 | } | } | 
 |  |  | 
| void file_setseparator(char *path, int maxlen) { | void DOSIOCALL file_setseparator(OEMCHAR *path, int maxlen) { | 
 |  |  | 
 | int             pos; | int             pos; | 
 |  |  | 
| pos = strlen(path) - 1; | pos = OEMSTRLEN(path) - 1; | 
 | if ((pos < 0) || | if ((pos < 0) || | 
 | ((pos == 1) && (path[1] == ':')) || | ((pos == 1) && (path[1] == ':')) || | 
 | ((path[pos] == '\\') && (!milstr_kanji2nd(path, pos))) || | ((path[pos] == '\\') && (!milstr_kanji2nd(path, pos))) || |