|
|
| version 1.6, 2003/12/19 16:08:01 | version 1.17, 2011/01/15 14:56:55 |
|---|---|
| Line 1 | Line 1 |
| /* $Id$ */ | |
| #include "compiler.h" | #include "compiler.h" |
| #include <sys/stat.h> | #include <sys/stat.h> |
| #include <time.h> | #include <time.h> |
| #include <dirent.h> | |
| #include "codecnv.h" | #include "codecnv.h" |
| #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) - 0xa1) & 0xff) < 0x5c) | #define ISKANJI(c) ((((c) - 0xa1) & 0xff) < 0x5c) |
| Line 30 dosio_term(void) | Line 31 dosio_term(void) |
| /* ファイル操作 */ | /* ファイル操作 */ |
| FILEH | FILEH |
| file_open(const char *path) | file_open(const OEMCHAR *path) |
| { | { |
| FILEH fh; | FILEH fh; |
| Line 41 file_open(const char *path) | Line 42 file_open(const char *path) |
| } | } |
| FILEH | FILEH |
| file_open_rb(const char *path) | file_open_rb(const OEMCHAR *path) |
| { | { |
| return fopen(path, "rb"); | return fopen(path, "rb"); |
| } | } |
| FILEH | FILEH |
| file_create(const char *path) | file_create(const OEMCHAR *path) |
| { | { |
| return fopen(path, "wb+"); | return fopen(path, "wb+"); |
| Line 95 file_getsize(FILEH handle) | Line 96 file_getsize(FILEH handle) |
| } | } |
| short | short |
| file_attr(const char *path) | file_attr(const OEMCHAR *path) |
| { | { |
| struct stat sb; | struct stat sb; |
| short attr; | short attr; |
| Line 113 file_attr(const char *path) | Line 114 file_attr(const char *path) |
| return -1; | return -1; |
| } | } |
| short | static BOOL |
| file_getdatetime(FILEH handle, DOSDATE *dosdate, DOSTIME *dostime) | cnvdatetime(struct stat *sb, DOSDATE *dosdate, DOSTIME *dostime) |
| { | { |
| struct stat sb; | |
| struct tm *ftime; | struct tm *ftime; |
| if (fstat(fileno(handle), &sb) == 0) { | ftime = localtime(&sb->st_mtime); |
| ftime = localtime(&sb.st_mtime); | if (ftime) { |
| if (ftime) { | if (dosdate) { |
| if (dosdate) { | dosdate->year = ftime->tm_year + 1900; |
| dosdate->year = ftime->tm_year + 1900; | dosdate->month = ftime->tm_mon + 1; |
| dosdate->month = ftime->tm_mon + 1; | dosdate->day = ftime->tm_mday; |
| dosdate->day = ftime->tm_mday; | } |
| } | if (dostime) { |
| if (dostime) { | dostime->hour = ftime->tm_hour; |
| dostime->hour = ftime->tm_hour; | dostime->minute = ftime->tm_min; |
| dostime->minute = ftime->tm_min; | dostime->second = ftime->tm_sec; |
| dostime->second = ftime->tm_sec; | |
| } | |
| return 0; | |
| } | } |
| return SUCCESS; | |
| } | } |
| return FAILURE; | |
| } | |
| short | |
| file_getdatetime(FILEH handle, DOSDATE *dosdate, DOSTIME *dostime) | |
| { | |
| struct stat sb; | |
| if ((fstat(fileno(handle), &sb) == 0) | |
| && (cnvdatetime(&sb, dosdate, dostime))) | |
| return 0; | |
| return -1; | return -1; |
| } | } |
| short | short |
| file_delete(const char *path) | file_delete(const OEMCHAR *path) |
| { | { |
| return (short)unlink(path); | return (short)unlink(path); |
| } | } |
| short | short |
| file_dircreate(const char *path) | file_dircreate(const OEMCHAR *path) |
| { | { |
| return (short)mkdir(path, 0777); | return (short)mkdir(path, 0777); |
| Line 155 file_dircreate(const char *path) | Line 164 file_dircreate(const char *path) |
| /* カレントファイル操作 */ | /* カレントファイル操作 */ |
| void | void |
| file_setcd(const char *exepath) | file_setcd(const OEMCHAR *exepath) |
| { | { |
| milstr_ncpy(curpath, exepath, sizeof(curpath)); | milstr_ncpy(curpath, exepath, sizeof(curpath)); |
| Line 164 file_setcd(const char *exepath) | Line 173 file_setcd(const char *exepath) |
| } | } |
| char * | char * |
| file_getcd(const char *sjis) | file_getcd(const OEMCHAR *filename) |
| { | { |
| *curfilep = '\0'; | *curfilep = '\0'; |
| file_catname(curpath, sjis, sizeof(curpath)); | file_catname(curpath, filename, sizeof(curpath)); |
| return curpath; | return curpath; |
| } | } |
| FILEH | FILEH |
| file_open_c(const char *sjis) | file_open_c(const OEMCHAR *filename) |
| { | { |
| *curfilep = '\0'; | *curfilep = '\0'; |
| file_catname(curpath, sjis, sizeof(curpath)); | file_catname(curpath, filename, sizeof(curpath)); |
| return file_open(curpath); | return file_open(curpath); |
| } | } |
| FILEH | FILEH |
| file_open_rb_c(const char *sjis) | file_open_rb_c(const OEMCHAR *filename) |
| { | { |
| *curfilep = '\0'; | *curfilep = '\0'; |
| file_catname(curpath, sjis, sizeof(curpath)); | file_catname(curpath, filename, sizeof(curpath)); |
| return file_open_rb(curpath); | return file_open_rb(curpath); |
| } | } |
| FILEH | FILEH |
| file_create_c(const char *sjis) | file_create_c(const OEMCHAR *filename) |
| { | { |
| *curfilep = '\0'; | *curfilep = '\0'; |
| file_catname(curpath, sjis, sizeof(curpath)); | file_catname(curpath, filename, sizeof(curpath)); |
| return file_create(curpath); | return file_create(curpath); |
| } | } |
| short | short |
| file_delete_c(const char *sjis) | file_delete_c(const OEMCHAR *filename) |
| { | { |
| *curfilep = '\0'; | *curfilep = '\0'; |
| file_catname(curpath, sjis, sizeof(curpath)); | file_catname(curpath, filename, sizeof(curpath)); |
| return file_delete(curpath); | return file_delete(curpath); |
| } | } |
| short | short |
| file_attr_c(const char *sjis) | file_attr_c(const OEMCHAR *filename) |
| { | { |
| *curfilep = '\0'; | *curfilep = '\0'; |
| file_catname(curpath, sjis, sizeof(curpath)); | file_catname(curpath, filename, sizeof(curpath)); |
| return file_attr_c(curpath); | return file_attr(curpath); |
| } | } |
| FILEFINDH | FLISTH |
| file_find1st(const char *dir, FILEFINDT *fft) | file_list1st(const OEMCHAR *dir, FLINFO *fli) |
| { | { |
| DIR *ret; | FLISTH ret; |
| ret = opendir(dir); | ret = (FLISTH)_MALLOC(sizeof(_FLISTH), "FLISTH"); |
| if (ret == NULL) { | if (ret == NULL) { |
| return FILEFINDH_INVALID; | VERBOSE(("file_list1st: couldn't alloc memory (size = %d)", sizeof(_FLISTH))); |
| return FLISTH_INVALID; | |
| } | } |
| if (file_findnext((FILEFINDH)ret, fft) == SUCCESS) { | |
| return (FILEFINDH)ret; | milstr_ncpy(ret->path, dir, sizeof(ret->path)); |
| } | file_setseparator(ret->path, sizeof(ret->path)); |
| closedir(ret); | ret->hdl = opendir(ret->path); |
| return FILEFINDH_INVALID; | VERBOSE(("file_list1st: opendir(%s)", ret->path)); |
| if (ret->hdl == NULL) { | |
| VERBOSE(("file_list1st: opendir failure")); | |
| _MFREE(ret); | |
| return FLISTH_INVALID; | |
| } | |
| if (file_listnext((FLISTH)ret, fli) == SUCCESS) { | |
| return (FLISTH)ret; | |
| } | |
| VERBOSE(("file_list1st: file_listnext failure")); | |
| closedir(ret->hdl); | |
| _MFREE(ret); | |
| return FLISTH_INVALID; | |
| } | } |
| BOOL | BOOL |
| file_findnext(FILEFINDH hdl, FILEFINDT *fft) | file_listnext(FLISTH hdl, FLINFO *fli) |
| { | { |
| OEMCHAR buf[MAX_PATH]; | |
| struct dirent *de; | struct dirent *de; |
| struct stat sb; | struct stat sb; |
| UINT32 attr; | |
| UINT32 size; | |
| de = readdir((DIR *)hdl); | de = readdir(hdl->hdl); |
| if (de == NULL) { | if (de == NULL) { |
| VERBOSE(("file_listnext: readdir failure")); | |
| return FAILURE; | return FAILURE; |
| } | } |
| if (fft) { | |
| mileuc_ncpy(fft->path, de->d_name, sizeof(fft->path)); | milstr_ncpy(buf, hdl->path, sizeof(buf)); |
| size = 0; | milstr_ncat(buf, de->d_name, sizeof(buf)); |
| attr = 0; | if (stat(buf, &sb) != 0) { |
| if (stat(de->d_name, &sb) == 0) { | VERBOSE(("file_listnext: stat failure. (path = %s)", buf)); |
| size = sb.st_size; | return FAILURE; |
| if (S_ISDIR(sb.st_mode)) { | |
| attr = FILEATTR_DIRECTORY; | |
| } | |
| else if (!(sb.st_mode & S_IWUSR)) { | |
| attr = FILEATTR_READONLY; | |
| } | |
| } | |
| fft->size = size; | |
| fft->attr = attr; | |
| } | } |
| fli->caps = FLICAPS_SIZE | FLICAPS_ATTR | FLICAPS_DATE | FLICAPS_TIME; | |
| fli->size = sb.st_size; | |
| fli->attr = 0; | |
| if (S_ISDIR(sb.st_mode)) { | |
| fli->attr |= FILEATTR_DIRECTORY; | |
| } | |
| if (!(sb.st_mode & S_IWUSR)) { | |
| fli->attr |= FILEATTR_READONLY; | |
| } | |
| cnvdatetime(&sb, &fli->date, &fli->time); | |
| milstr_ncpy(fli->path, de->d_name, sizeof(fli->path)); | |
| VERBOSE(("file_listnext: success")); | |
| return SUCCESS; | return SUCCESS; |
| } | } |
| void | void |
| file_findclose(FILEFINDH hdl) | file_listclose(FLISTH hdl) |
| { | { |
| closedir((DIR *)hdl); | if (hdl) { |
| closedir(hdl->hdl); | |
| _MFREE(hdl); | |
| } | |
| } | } |
| static int | static int |
| euckanji1st(const char *str, int pos) | euckanji1st(const OEMCHAR *str, int pos) |
| { | { |
| int ret; | int ret; |
| int c; | |
| ret = 0; | for (ret = 0; pos >= 0; ret ^= 1) { |
| while ((pos >= 0) && (((str[pos--] - 0xa1) & 0xff) < 0x5d)) { | c = (UINT8)str[pos--]; |
| ret ^= 1; | if (!ISKANJI(c)) |
| break; | |
| } | } |
| return ret; | return ret; |
| } | } |
| void | void |
| file_cpyname(char *dst, const char *src, int maxlen) | file_cpyname(OEMCHAR *dst, const OEMCHAR *src, int maxlen) |
| { | { |
| int i; | int i; |
| if (maxlen--) { | if (maxlen-- > 0) { |
| for (i = 0; i < maxlen && src[i] != '\0'; i++) { | for (i = 0; i < maxlen && src[i] != '\0'; i++) { |
| dst[i] = src[i]; | dst[i] = src[i]; |
| } | } |
| Line 302 file_cpyname(char *dst, const char *src, | Line 333 file_cpyname(char *dst, const char *src, |
| } | } |
| void | void |
| file_catname(char *path, const char *sjis, int maxlen) | file_catname(OEMCHAR *path, const OEMCHAR *filename, int maxlen) |
| { | { |
| while (maxlen) { | for (; maxlen > 0; path++, maxlen--) { |
| if (*path == '\0') { | if (*path == '\0') { |
| break; | break; |
| } | } |
| path++; | |
| maxlen--; | |
| } | } |
| if (maxlen) { | if (maxlen > 0) { |
| codecnv_sjis2euc(path, maxlen, sjis, (UINT)-1); | milstr_ncpy(path, filename, maxlen); |
| for (; path[0] != '\0'; path++) { | for (; *path != '\0'; path++) { |
| if (!ISKANJI(path[0])) { | if (!ISKANJI(*path)) { |
| if (path[1] == '\0') { | path++; |
| if (*path == '\0') { | |
| break; | break; |
| } | } |
| path++; | } else if (((*path - 0x41) & 0xff) < 26) { |
| } else if ((((path[0]) - 0x41) & 0xff) < 26) { | *path |= 0x20; |
| path[0] |= 0x20; | } else if (*path == '\\') { |
| } else if (path[0] == '\\') { | *path = G_DIR_SEPARATOR; |
| path[0] = '/'; | |
| } | } |
| } | } |
| } | } |
| } | } |
| BOOL | BOOL |
| file_cmpname(const char *path, const char *sjis) | file_cmpname(const OEMCHAR *path, const OEMCHAR *path2) |
| { | { |
| char euc[MAX_PATH]; | |
| codecnv_sjis2euc(euc, sizeof(euc), sjis, (UINT)-1); | return strcasecmp(path, path2); |
| return strcmp(path, euc); | |
| } | } |
| char * | OEMCHAR * |
| file_getname(char *path) | file_getname(const OEMCHAR *path) |
| { | { |
| char *ret; | const OEMCHAR *ret; |
| for (ret = path; path[0] != '\0'; path++) { | for (ret = path; *path != '\0'; path++) { |
| if (ISKANJI(path[0])) { | if (ISKANJI(*path)) { |
| if (path[1] == '\0') { | path++; |
| if (*path == '\0') { | |
| break; | break; |
| } | } |
| path++; | } else if (*path == G_DIR_SEPARATOR) { |
| } else if (path[0] == '/') { | |
| ret = path + 1; | ret = path + 1; |
| } | } |
| } | } |
| return ret; | return (OEMCHAR *)ret; |
| } | } |
| void | void |
| file_cutname(char *path) | file_cutname(OEMCHAR *path) |
| { | { |
| char *p; | OEMCHAR *p; |
| p = file_getname(path); | p = file_getname(path); |
| *p = '\0'; | *p = '\0'; |
| } | } |
| char * | OEMCHAR * |
| file_getext(char *path) | file_getext(const OEMCHAR *path) |
| { | { |
| char *p; | const OEMCHAR *p, *q; |
| char *q; | |
| p = file_getname(path); | for (p = file_getname(path), q = NULL; *p != '\0'; p++) { |
| q = NULL; | |
| while (*p != '\0') { | |
| if (*p == '.') { | if (*p == '.') { |
| q = p + 1; | q = p + 1; |
| } | } |
| p++; | |
| } | } |
| if (q == NULL) { | if (q == NULL) { |
| q = p; | q = p; |
| } | } |
| return q; | return (char *)q; |
| } | } |
| void | void |
| file_cutext(char *path) | file_cutext(OEMCHAR *path) |
| { | { |
| char *p; | OEMCHAR *p, *q; |
| char *q; | |
| p = file_getname(path); | for (p = file_getname(path), q = NULL; *p != '\0'; p++) { |
| q = NULL; | |
| while (*p != '\0') { | |
| if (*p == '.') { | if (*p == '.') { |
| q = p; | q = p; |
| } | } |
| p++; | |
| } | } |
| if (q != NULL) { | if (q != NULL) { |
| *q = '\0'; | *q = '\0'; |
| Line 405 file_cutext(char *path) | Line 424 file_cutext(char *path) |
| } | } |
| void | void |
| file_cutseparator(char *path) | file_cutseparator(OEMCHAR *path) |
| { | { |
| int pos; | int pos; |
| pos = strlen(path) - 1; | pos = strlen(path) - 1; |
| if ((pos > 0) && (path[pos] == '/')) { | if ((pos > 0) && (path[pos] == G_DIR_SEPARATOR)) { |
| path[pos] = '\0'; | path[pos] = '\0'; |
| } | } |
| } | } |
| void | void |
| file_setseparator(char *path, int maxlen) | file_setseparator(OEMCHAR *path, int maxlen) |
| { | { |
| int pos; | int pos; |
| pos = strlen(path); | pos = strlen(path); |
| if ((pos) && (path[pos-1] != '/') && ((pos + 2) < maxlen)) { | if ((pos) && (path[pos-1] != G_DIR_SEPARATOR) && ((pos + 2) < maxlen)) { |
| path[pos++] = '/'; | path[pos++] = G_DIR_SEPARATOR; |
| path[pos] = '\0'; | path[pos] = '\0'; |
| } | } |
| } | } |