|
|
| version 1.2, 2003/11/23 09:59:10 | version 1.7, 2004/06/21 02:01:32 |
|---|---|
| Line 25 void dosio_term(void) { | Line 25 void dosio_term(void) { |
| /* ファイル操作 */ | /* ファイル操作 */ |
| FILEH file_open(const char *path) { | FILEH file_open(const char *path) { |
| #if defined(WIN32) && defined(OSLANG_EUC) | |
| char sjis[MAX_PATH]; | |
| codecnv_euc2sjis(sjis, sizeof(sjis), path, (UINT)-1); | |
| return(fopen(sjis, "rb+")); | |
| #else | |
| return(fopen(path, "rb+")); | return(fopen(path, "rb+")); |
| #endif | |
| } | } |
| FILEH file_open_rb(const char *path) { | FILEH file_open_rb(const char *path) { |
| #if defined(WIN32) && defined(OSLANG_EUC) | |
| char sjis[MAX_PATH]; | |
| codecnv_euc2sjis(sjis, sizeof(sjis), path, (UINT)-1); | |
| return(fopen(sjis, "rb+")); | |
| #else | |
| return(fopen(path, "rb+")); | return(fopen(path, "rb+")); |
| #endif | |
| } | } |
| FILEH file_create(const char *path) { | FILEH file_create(const char *path) { |
| #if defined(WIN32) && defined(OSLANG_EUC) | |
| char sjis[MAX_PATH]; | |
| codecnv_euc2sjis(sjis, sizeof(sjis), path, (UINT)-1); | |
| return(fopen(sjis, "wb+")); | |
| #else | |
| return(fopen(path, "wb+")); | return(fopen(path, "wb+")); |
| #endif | |
| } | } |
| long file_seek(FILEH handle, long pointer, int method) { | long file_seek(FILEH handle, long pointer, int method) { |
| Line 100 struct stat sb; | Line 118 struct stat sb; |
| return(-1); | return(-1); |
| } | } |
| static BOOL cnv_sttime(time_t *t, DOSDATE *dosdate, DOSTIME *dostime) { | |
| struct tm *ftime; | |
| ftime = localtime(t); | |
| if (ftime == NULL) { | |
| return(FAILURE); | |
| } | |
| if (dosdate) { | |
| dosdate->year = ftime->tm_year + 1900; | |
| dosdate->month = ftime->tm_mon + 1; | |
| dosdate->day = ftime->tm_mday; | |
| } | |
| if (dostime) { | |
| dostime->hour = ftime->tm_hour; | |
| dostime->minute = ftime->tm_min; | |
| dostime->second = ftime->tm_sec; | |
| } | |
| return(SUCCESS); | |
| } | |
| short file_getdatetime(FILEH handle, DOSDATE *dosdate, DOSTIME *dostime) { | short file_getdatetime(FILEH handle, DOSDATE *dosdate, DOSTIME *dostime) { |
| struct stat sb; | struct stat sb; |
| struct tm *ftime; | |
| if (fstat(fileno(handle), &sb) == 0) { | if (fstat(fileno(handle), &sb) == 0) { |
| ftime = localtime(&sb.st_mtime); | if (cnv_sttime(&sb.st_mtime, dosdate, dostime) == SUCCESS) { |
| if (ftime) { | |
| if (dosdate) { | |
| dosdate->year = ftime->tm_year + 1900; | |
| dosdate->month = ftime->tm_mon + 1; | |
| dosdate->day = ftime->tm_mday; | |
| } | |
| if (dostime) { | |
| dostime->hour = ftime->tm_hour; | |
| dostime->minute = ftime->tm_min; | |
| dostime->second = ftime->tm_sec; | |
| } | |
| return(0); | return(0); |
| } | } |
| } | } |
| Line 142 short file_dircreate(const char *path) { | Line 169 short file_dircreate(const char *path) { |
| /* カレントファイル操作 */ | /* カレントファイル操作 */ |
| void file_setcd(const char *exepath) { | void file_setcd(const char *exepath) { |
| milstr_ncpy(curpath, exepath, sizeof(curpath)); | file_cpyname(curpath, exepath, sizeof(curpath)); |
| curfilep = file_getname(curpath); | curfilep = file_getname(curpath); |
| *curfilep = '\0'; | *curfilep = '\0'; |
| } | } |
| char *file_getcd(const char *path) { | char *file_getcd(const char *path) { |
| milstr_ncpy(curfilep, path, sizeof(curpath) - (curfilep - curpath)); | file_cpyname(curfilep, path, sizeof(curpath) - (curfilep - curpath)); |
| return(curpath); | return(curpath); |
| } | } |
| FILEH file_open_c(const char *path) { | FILEH file_open_c(const char *path) { |
| milstr_ncpy(curfilep, path, sizeof(curpath) - (curfilep - curpath)); | file_cpyname(curfilep, path, sizeof(curpath) - (curfilep - curpath)); |
| return(file_open(curpath)); | return(file_open(curpath)); |
| } | } |
| FILEH file_open_rb_c(const char *path) { | FILEH file_open_rb_c(const char *path) { |
| milstr_ncpy(curfilep, path, sizeof(curpath) - (curfilep - curpath)); | file_cpyname(curfilep, path, sizeof(curpath) - (curfilep - curpath)); |
| return(file_open_rb(curpath)); | return(file_open_rb(curpath)); |
| } | } |
| FILEH file_create_c(const char *path) { | FILEH file_create_c(const char *path) { |
| milstr_ncpy(curfilep, path, sizeof(curpath) - (curfilep - curpath)); | file_cpyname(curfilep, path, sizeof(curpath) - (curfilep - curpath)); |
| return(file_create(curpath)); | return(file_create(curpath)); |
| } | } |
| short file_delete_c(const char *path) { | short file_delete_c(const char *path) { |
| milstr_ncpy(curfilep, path, sizeof(curpath) - (curfilep - curpath)); | file_cpyname(curfilep, path, sizeof(curpath) - (curfilep - curpath)); |
| return(file_delete(curpath)); | return(file_delete(curpath)); |
| } | } |
| short file_attr_c(const char *path) { | short file_attr_c(const char *path) { |
| milstr_ncpy(curfilep, path, sizeof(curpath) - (curfilep - curpath)); | file_cpyname(curfilep, path, sizeof(curpath) - (curfilep - curpath)); |
| return(file_attr_c(curpath)); | return(file_attr_c(curpath)); |
| } | } |
| #if defined(WIN32) | #if defined(WIN32) |
| FILEFINDH file_find1st(const char *dir, FILEFINDT *fft) { | static BOOL cnvdatetime(FILETIME *file, DOSDATE *dosdate, DOSTIME *dostime) { |
| FILETIME localtime; | |
| SYSTEMTIME systime; | |
| if ((FileTimeToLocalFileTime(file, &localtime) == 0) || | |
| (FileTimeToSystemTime(&localtime, &systime) == 0)) { | |
| return(FAILURE); | |
| } | |
| if (dosdate) { | |
| dosdate->year = (UINT16)systime.wYear; | |
| dosdate->month = (UINT8)systime.wMonth; | |
| dosdate->day = (UINT8)systime.wDay; | |
| } | |
| if (dostime) { | |
| dostime->hour = (UINT8)systime.wHour; | |
| dostime->minute = (UINT8)systime.wMinute; | |
| dostime->second = (UINT8)systime.wSecond; | |
| } | |
| return(SUCCESS); | |
| } | |
| static BOOL setflist(WIN32_FIND_DATA *w32fd, FLINFO *fli) { | |
| if ((w32fd->dwFileAttributes & FILEATTR_DIRECTORY) && | |
| ((!file_cmpname(w32fd->cFileName, ".")) || | |
| (!file_cmpname(w32fd->cFileName, "..")))) { | |
| return(FAILURE); | |
| } | |
| fli->caps = FLICAPS_SIZE | FLICAPS_ATTR; | |
| fli->size = w32fd->nFileSizeLow; | |
| fli->attr = w32fd->dwFileAttributes; | |
| if (cnvdatetime(&w32fd->ftLastWriteTime, &fli->date, &fli->time) | |
| == SUCCESS) { | |
| fli->caps |= FLICAPS_DATE | FLICAPS_TIME; | |
| } | |
| #if defined(OSLANG_EUC) | |
| codecnv_sjis2euc(fli->path, sizeof(fli->path), | |
| w32fd->cFileName, (UINT)-1); | |
| #else | |
| file_cpyname(fli->path, w32fd->cFileName, sizeof(fli->path)); | |
| #endif | |
| return(SUCCESS); | |
| } | |
| FLISTH file_list1st(const char *dir, FLINFO *fli) { | |
| char path[MAX_PATH]; | char path[MAX_PATH]; |
| HANDLE hdl; | HANDLE hdl; |
| WIN32_FIND_DATA w32fd; | WIN32_FIND_DATA w32fd; |
| milsjis_ncpy(path, dir, sizeof(path)); | file_cpyname(path, dir, sizeof(path)); |
| file_setseparator(path, sizeof(path)); | file_setseparator(path, sizeof(path)); |
| milsjis_ncat(path, "*.*", sizeof(path)); | file_catname(path, "*.*", sizeof(path)); |
| hdl = FindFirstFile(path, &w32fd); | hdl = FindFirstFile(path, &w32fd); |
| if (hdl == INVALID_HANDLE_VALUE) { | if (hdl != INVALID_HANDLE_VALUE) { |
| return(FILEFINDH_INVALID); | do { |
| } | if (setflist(&w32fd, fli) == SUCCESS) { |
| if (fft) { | return(hdl); |
| milsjis_ncpy(fft->path, w32fd.cFileName, sizeof(fft->path)); | } |
| fft->size = w32fd.nFileSizeLow; | } while(FindNextFile(hdl, &w32fd)); |
| fft->attr = w32fd.dwFileAttributes; | FindClose(hdl); |
| } | } |
| return((FILEFINDH)hdl); | return(FLISTH_INVALID); |
| } | } |
| BOOL file_findnext(FILEFINDH hdl, FILEFINDT *fft) { | BOOL file_listnext(FLISTH hdl, FLINFO *fli) { |
| WIN32_FIND_DATA w32fd; | WIN32_FIND_DATA w32fd; |
| if (!FindNextFile((HANDLE)hdl, &w32fd)) { | while(FindNextFile(hdl, &w32fd)) { |
| return(FAILURE); | if (setflist(&w32fd, fli) == SUCCESS) { |
| } | return(SUCCESS); |
| if (fft) { | } |
| milsjis_ncpy(fft->path, w32fd.cFileName, sizeof(fft->path)); | |
| fft->size = w32fd.nFileSizeLow; | |
| fft->attr = w32fd.dwFileAttributes; | |
| } | } |
| return(SUCCESS); | return(FAILURE); |
| } | } |
| void file_findclose(FILEFINDH hdl) { | void file_listclose(FLISTH hdl) { |
| FindClose((HANDLE)hdl); | FindClose(hdl); |
| } | } |
| #else | #else |
| FILEFINDH file_find1st(const char *dir, FILEFINDT *fft) { | FLISTH file_list1st(const char *dir, FLINFO *fli) { |
| DIR *ret; | DIR *ret; |
| Line 233 FILEFINDH file_find1st(const char *dir, | Line 302 FILEFINDH file_find1st(const char *dir, |
| if (ret == NULL) { | if (ret == NULL) { |
| goto ff1_err; | goto ff1_err; |
| } | } |
| if (file_findnext((FILEFINDH)ret, fft) == SUCCESS) { | if (file_listnext((FLISTH)ret, fli) == SUCCESS) { |
| return((FILEFINDH)ret); | return((FLISTH)ret); |
| } | } |
| closedir(ret); | closedir(ret); |
| ff1_err: | ff1_err: |
| return(FILEFINDH_INVALID); | return(FLISTH_INVALID); |
| } | } |
| BOOL file_findnext(FILEFINDH hdl, FILEFINDT *fft) { | BOOL file_listnext(FLISTH hdl, FLINFO *fli) { |
| struct dirent *de; | struct dirent *de; |
| struct stat sb; | struct stat sb; |
| UINT32 attr; | UINT32 attr; |
| UINT32 size; | |
| de = readdir((DIR *)hdl); | de = readdir((DIR *)hdl); |
| if (de == NULL) { | if (de == NULL) { |
| return(FAILURE); | return(FAILURE); |
| } | } |
| if (fft) { | if (fli) { |
| mileuc_ncpy(fft->path, de->d_name, sizeof(fft->path)); | |
| size = 0; | |
| attr = 0; | |
| if (stat(de->d_name, &sb) == 0) { | if (stat(de->d_name, &sb) == 0) { |
| size = sb.st_size; | fli->caps = FLICAPS_SIZE | FLICAPS_ATTR; |
| fli->size = sb.st_size; | |
| attr = 0; | |
| if (S_ISDIR(sb.st_mode)) { | if (S_ISDIR(sb.st_mode)) { |
| attr = FILEATTR_DIRECTORY; | attr = FILEATTR_DIRECTORY; |
| } | } |
| else if (!(sb.st_mode & S_IWUSR)) { | else if (!(sb.st_mode & S_IWUSR)) { |
| attr = FILEATTR_READONLY; | attr = FILEATTR_READONLY; |
| } | } |
| fli->attr = attr; | |
| if (cnv_sttime(&sb.st_mtime, &fli->date, &fli->time) == SUCCESS) { | |
| fli->caps |= FLICAPS_DATE | FLICAPS_TIME; | |
| } | |
| } | } |
| fft->size = size; | else { |
| fft->attr = attr; | fli->caps = 0; |
| fli->size = 0; | |
| fli->attr = 0; | |
| } | |
| mileuc_ncpy(fli->path, de->d_name, sizeof(fli->path)); | |
| } | } |
| return(SUCCESS); | return(SUCCESS); |
| } | } |
| void file_findclose(FILEFINDH hdl) { | void file_listclose(FLISTH hdl) { |
| closedir((DIR *)hdl); | closedir((DIR *)hdl); |
| } | } |
| Line 280 void file_findclose(FILEFINDH hdl) { | Line 355 void file_findclose(FILEFINDH hdl) { |
| void file_catname(char *path, const char *name, int maxlen) { | void file_catname(char *path, const char *name, int maxlen) { |
| char c; | int csize; |
| while(maxlen > 0) { | while(maxlen > 0) { |
| if (*path == '\0') { | if (*path == '\0') { |
| Line 289 void file_catname(char *path, const char | Line 364 void file_catname(char *path, const char |
| path++; | path++; |
| maxlen--; | maxlen--; |
| } | } |
| if (maxlen > 0) { | file_cpyname(path, name, maxlen); |
| maxlen--; | while((csize = milstr_charsize(path)) != 0) { |
| while(maxlen > 0) { | if ((csize == 1) && (*path == '\\')) { |
| maxlen--; | *path = '/'; |
| c = *name++; | |
| if (ISKANJI1ST(c)) { | |
| if ((maxlen == 0) || (*name == '\0')) { | |
| break; | |
| } | |
| *path++ = c; | |
| *path++ = *name++; | |
| } | |
| else if (c == '\\') { | |
| *path++ = '/'; | |
| } | |
| else if (c) { | |
| *path++ = c; | |
| } | |
| else { | |
| break; | |
| } | |
| } | } |
| *path = '\0'; | path += csize; |
| } | } |
| } | } |
| char *file_getname(char *path) { | char *file_getname(const char *path) { |
| char *ret; | const char *ret; |
| int csize; | |
| ret = path; | ret = path; |
| while(1) { | while((csize = milstr_charsize(path)) != 0) { |
| if (ISKANJI1ST(*path)) { | if ((csize == 1) && (*path == '/')) { |
| if (*(path+1) == '\0') { | |
| break; | |
| } | |
| path++; | |
| } | |
| else if (*path == '/') { | |
| ret = path + 1; | ret = path + 1; |
| } | } |
| else if (*path == '\0') { | path += csize; |
| break; | |
| } | |
| path++; | |
| } | } |
| return(ret); | return((char *)ret); |
| } | } |
| void file_cutname(char *path) { | void file_cutname(char *path) { |
| Line 346 void file_cutname(char *path) { | Line 396 void file_cutname(char *path) { |
| *p = '\0'; | *p = '\0'; |
| } | } |
| char *file_getext(char *path) { | char *file_getext(const char *path) { |
| char *p; | const char *p; |
| char *q; | const char *q; |
| p = file_getname(path); | p = file_getname(path); |
| q = NULL; | q = NULL; |
| Line 362 char *file_getext(char *path) { | Line 412 char *file_getext(char *path) { |
| if (q == NULL) { | if (q == NULL) { |
| q = p; | q = p; |
| } | } |
| return(q); | return((char *)q); |
| } | } |
| void file_cutext(char *path) { | void file_cutext(char *path) { |
| Line 388 void file_cutseparator(char *path) { | Line 438 void file_cutseparator(char *path) { |
| int pos; | int pos; |
| pos = strlen(path) - 1; | pos = strlen(path) - 1; |
| if ((pos > 0) && (path[pos] == '/')) { | if ((pos > 0) && // 2文字以上でー |
| (path[pos] == '/') && // ケツが \ でー | |
| ((pos != 1) || (path[0] != '.'))) { // './' ではなかったら | |
| path[pos] = '\0'; | path[pos] = '\0'; |
| } | } |
| } | } |