| version 1.1.1.1, 2003/10/16 17:59:41 | version 1.7, 2004/01/13 16:30:21 | 
| Line 1 | Line 1 | 
| #include        "compiler.h" | #include "compiler.h" | 
| #include        <sys/stat.h> |  | 
| #include        <time.h> |  | 
| #include        "codecnv.h" |  | 
| #include        "dosio.h" |  | 
| #if defined(WIN32) |  | 
| #include        <direct.h> |  | 
| #endif |  | 
| #if 0 |  | 
| #include <sys/param.h> |  | 
| #include <unistd.h> |  | 
| #endif |  | 
 |  |  | 
| static  char    curpath[MAX_PATH]; | #include <sys/stat.h> | 
| static  char    *curfilep = curpath; | #include <time.h> | 
|  | #include <dirent.h> | 
|  |  | 
|  | #include "codecnv.h" | 
|  | #include "dosio.h" | 
|  |  | 
|  |  | 
|  | static char curpath[MAX_PATH]; | 
|  | static char *curfilep = curpath; | 
 |  |  | 
 | #define ISKANJI(c)      ((((c) - 0xa1) & 0xff) < 0x5c) | #define ISKANJI(c)      ((((c) - 0xa1) & 0xff) < 0x5c) | 
 |  |  | 
 |  |  | 
| void dosio_init(void) { | void | 
|  | dosio_init(void) | 
|  | { | 
|  |  | 
|  | /* nothing to do */ | 
 | } | } | 
 |  |  | 
| void dosio_term(void) { | void | 
|  | dosio_term(void) | 
|  | { | 
|  |  | 
|  | /* nothing to do */ | 
 | } | } | 
 |  |  | 
| /* ¥Õ¥¡¥¤¥ëÁàºî */ | /* ¡¦¥æ¡¦¡£¡¦¡¢¡¦öÃà¼*/ | 
| FILEH file_open(const char *path) { | FILEH | 
|  | file_open(const char *path) | 
|  | { | 
|  | FILEH fh; | 
 |  |  | 
| return(fopen(path, "rb+")); | fh = fopen(path, "rb+"); | 
|  | if (fh) | 
|  | return fh; | 
|  | return fopen(path, "rb"); | 
 | } | } | 
 |  |  | 
| FILEH file_open_rb(const char *path) { | FILEH | 
|  | file_open_rb(const char *path) | 
|  | { | 
 |  |  | 
| return(fopen(path, "rb+")); | return fopen(path, "rb"); | 
 | } | } | 
 |  |  | 
| FILEH file_create(const char *path) { | FILEH | 
|  | file_create(const char *path) | 
|  | { | 
 |  |  | 
| return(fopen(path, "wb+")); | return fopen(path, "wb+"); | 
 | } | } | 
 |  |  | 
| long file_seek(FILEH handle, long pointer, int method) { | long | 
|  | file_seek(FILEH handle, long pointer, int method) | 
|  | { | 
 |  |  | 
 | fseek(handle, pointer, method); | fseek(handle, pointer, method); | 
| return(ftell(handle)); | return ftell(handle); | 
 | } | } | 
 |  |  | 
| UINT file_read(FILEH handle, void *data, UINT length) { | UINT | 
|  | file_read(FILEH handle, void *data, UINT length) | 
|  | { | 
 |  |  | 
| return((UINT)fread(data, 1, length, handle)); | return (UINT)fread(data, 1, length, handle); | 
 | } | } | 
 |  |  | 
| UINT file_write(FILEH handle, const void *data, UINT length) { | UINT | 
|  | file_write(FILEH handle, const void *data, UINT length) | 
|  | { | 
 |  |  | 
| return((UINT)fwrite(data, 1, length, handle)); | return (UINT)fwrite(data, 1, length, handle); | 
 | } | } | 
 |  |  | 
| short file_close(FILEH handle) { | short | 
|  | file_close(FILEH handle) | 
|  | { | 
 |  |  | 
 | fclose(handle); | fclose(handle); | 
| return(0); | return 0; | 
 | } | } | 
 |  |  | 
| UINT file_getsize(FILEH handle) { | UINT | 
|  | file_getsize(FILEH handle) | 
|  | { | 
 | struct stat sb; | struct stat sb; | 
 |  |  | 
| if (fstat(fileno(handle), &sb) == 0) { | if (fstat(fileno(handle), &sb) == 0) | 
| return(sb.st_size); | return sb.st_size; | 
| } | return 0; | 
| return(0); |  | 
 | } | } | 
 |  |  | 
| short file_attr(const char *path) { | short | 
|  | file_attr(const char *path) | 
| struct stat     sb; | { | 
| short   attr; | struct stat sb; | 
|  | short attr; | 
 |  |  | 
 | if (stat(path, &sb) == 0) { | if (stat(path, &sb) == 0) { | 
 | #ifndef WIN32 |  | 
 | if (S_ISDIR(sb.st_mode)) { | if (S_ISDIR(sb.st_mode)) { | 
| return(FILEATTR_DIRECTORY); | return FILEATTR_DIRECTORY; | 
 | } | } | 
 | attr = 0; | attr = 0; | 
 | if (!(sb.st_mode & S_IWUSR)) { | if (!(sb.st_mode & S_IWUSR)) { | 
 | attr |= FILEATTR_READONLY; | attr |= FILEATTR_READONLY; | 
 | } | } | 
| #else | return attr; | 
| if (sb.st_mode & _S_IFDIR) { |  | 
| attr = FILEATTR_DIRECTORY; |  | 
| } |  | 
| else { |  | 
| attr = 0; |  | 
| } |  | 
| if (!(sb.st_mode & S_IWRITE)) { |  | 
| attr |= FILEATTR_READONLY; |  | 
| } |  | 
| #endif |  | 
| return(attr); |  | 
 | } | } | 
| return(-1); | return -1; | 
 | } | } | 
 |  |  | 
| short file_getdatetime(FILEH handle, DOSDATE *dosdate, DOSTIME *dostime) { | static BOOL | 
|  | 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) { |  | 
| dostime->hour = ftime->tm_hour; |  | 
| dostime->minute = ftime->tm_min; |  | 
| dostime->second = ftime->tm_sec; |  | 
| } |  | 
| return(0); |  | 
 | } | } | 
 |  | if (dostime) { | 
 |  | dostime->hour = ftime->tm_hour; | 
 |  | dostime->minute = ftime->tm_min; | 
 |  | dostime->second = ftime->tm_sec; | 
 |  | } | 
 |  | return SUCCESS; | 
 | } | } | 
| return(-1); | 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; | 
 | } | } | 
 |  |  | 
| short file_delete(const char *path) { | short | 
|  | file_delete(const char *path) | 
|  | { | 
 |  |  | 
| return(unlink(path)); | return (short)unlink(path); | 
 | } | } | 
 |  |  | 
| short file_dircreate(const char *path) { | short | 
|  | file_dircreate(const char *path) | 
|  | { | 
 |  |  | 
| #ifndef WIN32 | return (short)mkdir(path, 0777); | 
| return((short)mkdir(path, 0777)); |  | 
| #else |  | 
| return((short)mkdir(path)); |  | 
| #endif |  | 
 | } | } | 
 |  |  | 
 |  |  | 
| /* ¥«¥ì¥ó¥È¥Õ¥¡¥¤¥ëÁàºî */ | /* ¡¦¥©¡¦ø§ó¥È¥Õ¥¡¥¤¥ëÁàº*/ | 
| void file_setcd(const char *exepath) { | void | 
|  | file_setcd(const char *exepath) | 
|  | { | 
 |  |  | 
 | milstr_ncpy(curpath, exepath, sizeof(curpath)); | milstr_ncpy(curpath, exepath, sizeof(curpath)); | 
 | curfilep = file_getname(curpath); | curfilep = file_getname(curpath); | 
 | *curfilep = '\0'; | *curfilep = '\0'; | 
 | } | } | 
 |  |  | 
| char *file_getcd(const char *sjis) { | char * | 
|  | file_getcd(const char *sjis) | 
|  | { | 
 |  |  | 
 | *curfilep = '\0'; | *curfilep = '\0'; | 
 | file_catname(curpath, sjis, sizeof(curpath)); | file_catname(curpath, sjis, sizeof(curpath)); | 
| return(curpath); | return curpath; | 
 | } | } | 
 |  |  | 
| FILEH file_open_c(const char *sjis) { | FILEH | 
|  | file_open_c(const char *sjis) | 
|  | { | 
 |  |  | 
 | *curfilep = '\0'; | *curfilep = '\0'; | 
 | file_catname(curpath, sjis, sizeof(curpath)); | file_catname(curpath, sjis, sizeof(curpath)); | 
| return(file_open(curpath)); | return file_open(curpath); | 
 | } | } | 
 |  |  | 
| FILEH file_open_rb_c(const char *sjis) { | FILEH | 
|  | file_open_rb_c(const char *sjis) | 
|  | { | 
 |  |  | 
 | *curfilep = '\0'; | *curfilep = '\0'; | 
 | file_catname(curpath, sjis, sizeof(curpath)); | file_catname(curpath, sjis, sizeof(curpath)); | 
| return(file_open_rb(curpath)); | return file_open_rb(curpath); | 
 | } | } | 
 |  |  | 
| FILEH file_create_c(const char *sjis) { | FILEH | 
|  | file_create_c(const char *sjis) | 
|  | { | 
 |  |  | 
 | *curfilep = '\0'; | *curfilep = '\0'; | 
 | file_catname(curpath, sjis, sizeof(curpath)); | file_catname(curpath, sjis, sizeof(curpath)); | 
| return(file_create(curpath)); | return file_create(curpath); | 
 | } | } | 
 |  |  | 
| short file_delete_c(const char *sjis) { | short | 
|  | file_delete_c(const char *sjis) | 
|  | { | 
 |  |  | 
 | *curfilep = '\0'; | *curfilep = '\0'; | 
 | file_catname(curpath, sjis, sizeof(curpath)); | file_catname(curpath, sjis, sizeof(curpath)); | 
| return(file_delete(curpath)); | return file_delete(curpath); | 
 | } | } | 
 |  |  | 
| short file_attr_c(const char *sjis) { | short | 
|  | file_attr_c(const char *sjis) | 
|  | { | 
 |  |  | 
 | *curfilep = '\0'; | *curfilep = '\0'; | 
 | file_catname(curpath, sjis, sizeof(curpath)); | file_catname(curpath, sjis, sizeof(curpath)); | 
| return(file_attr_c(curpath)); | return file_attr_c(curpath); | 
 | } | } | 
 |  |  | 
 |  | FLISTH | 
 |  | file_list1st(const char *dir, FLINFO *fli) | 
 |  | { | 
 |  | char eucpath[MAX_PATH]; | 
 |  | DIR *ret; | 
 |  |  | 
| static int euckanji1st(const char *str, int pos) { | mileuc_ncpy(eucpath, dir, sizeof(eucpath)); | 
|  | file_setseparator(eucpath, sizeof(eucpath)); | 
|  | ret = opendir(eucpath); | 
|  | if (ret == NULL) { | 
|  | return FLISTH_INVALID; | 
|  | } | 
|  | if (file_listnext((FLISTH)ret, fli) == SUCCESS) { | 
|  | return (FLISTH)ret; | 
|  | } | 
|  | closedir(ret); | 
|  | return FLISTH_INVALID; | 
|  | } | 
 |  |  | 
| int             ret; | BOOL | 
|  | file_listnext(FLISTH hdl, FLINFO *fli) | 
|  | { | 
|  | struct dirent *de; | 
|  | struct stat sb; | 
|  |  | 
|  | de = readdir((DIR *)hdl); | 
|  | if (de == NULL) { | 
|  | return FAILURE; | 
|  | } | 
|  | if (stat(de->d_name, &sb) != 0) { | 
|  | return FAILURE; | 
|  | } | 
|  |  | 
|  | fli->caps = FLICAPS_SIZE | FLICAPS_ATTR | FLICAPS_DATE | FLICAPS_TIME; | 
|  | fli->size = sb.st_size; | 
|  | 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); | 
|  | mileuc_ncpy(fli->path, de->d_name, sizeof(fli->path)); | 
|  |  | 
|  | return SUCCESS; | 
|  | } | 
|  |  | 
|  | void | 
|  | file_listclose(FLISTH hdl) | 
|  | { | 
|  |  | 
|  | closedir((DIR *)hdl); | 
|  | } | 
|  |  | 
|  | static int | 
|  | euckanji1st(const char *str, int pos) | 
|  | { | 
|  | int ret; | 
 |  |  | 
 | ret = 0; | ret = 0; | 
| while((pos >= 0) && | while ((pos >= 0) && (((str[pos--] - 0xa1) & 0xff) < 0x5d)) { | 
| (((str[pos--] - 0xa1) & 0xff) < 0x5d)) { |  | 
 | ret ^= 1; | ret ^= 1; | 
 | } | } | 
| return(ret); | return ret; | 
 | } | } | 
 |  |  | 
| void file_cpyname(char *dst, const char *src, int maxlen) { | void | 
|  | file_cpyname(char *dst, const char *src, int maxlen) | 
| int             i; | { | 
|  | int i; | 
 |  |  | 
 | if (maxlen--) { | if (maxlen--) { | 
| for (i=0; i<maxlen && src[i]; i++) { | for (i = 0; i < maxlen && src[i] != '\0'; i++) { | 
 | dst[i] = src[i]; | dst[i] = src[i]; | 
 | } | } | 
| if (i) { | if (i > 0) { | 
 | if (euckanji1st(src, i-1)) { | if (euckanji1st(src, i-1)) { | 
 | i--; | i--; | 
 | } | } | 
| Line 220  void file_cpyname(char *dst, const char | Line 309  void file_cpyname(char *dst, const char | 
 | } | } | 
 | } | } | 
 |  |  | 
| void file_catname(char *path, const char *sjis, int maxlen) { | void | 
|  | file_catname(char *path, const char *sjis, int maxlen) | 
|  | { | 
 |  |  | 
| while(maxlen) { | while (maxlen) { | 
 | if (*path == '\0') { | if (*path == '\0') { | 
 | break; | break; | 
 | } | } | 
| Line 231  void file_catname(char *path, const char | Line 322  void file_catname(char *path, const char | 
 | } | } | 
 | if (maxlen) { | if (maxlen) { | 
 | codecnv_sjis2euc(path, maxlen, sjis, (UINT)-1); | codecnv_sjis2euc(path, maxlen, sjis, (UINT)-1); | 
| while(1) { | for (; path[0] != '\0'; path++) { | 
| if (!ISKANJI(*path)) { | if (!ISKANJI(path[0])) { | 
| if (*(path+1) == '\0') { | if (path[1] == '\0') { | 
 | break; | break; | 
 | } | } | 
 | path++; | path++; | 
 |  | } else if ((((path[0]) - 0x41) & 0xff) < 26) { | 
 |  | path[0] |= 0x20; | 
 |  | } else if (path[0] == '\\') { | 
 |  | path[0] = '/'; | 
 | } | } | 
 | else if ((((*path) - 0x41) & 0xff) < 26) { |  | 
 | *path |= 0x20; |  | 
 | } |  | 
 | else if (*path == '\\') { |  | 
 | *path = '/'; |  | 
 | } |  | 
 | else if (*path == '\0') { |  | 
 | break; |  | 
 | } |  | 
 | path++; |  | 
 | } | } | 
 | } | } | 
 | } | } | 
 |  |  | 
| BOOL file_cmpname(char *path, const char *sjis) { | BOOL | 
|  | file_cmpname(const char *path, const char *sjis) | 
| char    euc[MAX_PATH]; | { | 
|  | char euc[MAX_PATH]; | 
 |  |  | 
 | codecnv_sjis2euc(euc, sizeof(euc), sjis, (UINT)-1); | codecnv_sjis2euc(euc, sizeof(euc), sjis, (UINT)-1); | 
| return(strcmp(path, euc)); | return strcmp(path, euc); | 
 | } | } | 
 |  |  | 
| char *file_getname(char *path) { | char * | 
|  | file_getname(char *path) | 
|  | { | 
|  | char *ret; | 
 |  |  | 
| char    *ret; | for (ret = path; path[0] != '\0'; path++) { | 
|  | if (ISKANJI(path[0])) { | 
| ret = path; | if (path[1] == '\0') { | 
| while(1) { |  | 
| if (ISKANJI(*path)) { |  | 
| if (*(path+1) == '\0') { |  | 
 | break; | break; | 
 | } | } | 
 | path++; | path++; | 
| } | } else if (path[0] == '/') { | 
| else if (*path == '/') { |  | 
 | ret = path + 1; | ret = path + 1; | 
 | } | } | 
 | else if (*path == '\0') { |  | 
 | break; |  | 
 | } |  | 
 | path++; |  | 
 | } | } | 
| return(ret); | return ret; | 
 | } | } | 
 |  |  | 
| void file_cutname(char *path) { | void | 
|  | file_cutname(char *path) | 
| char    *p; | { | 
|  | char *p; | 
 |  |  | 
 | p = file_getname(path); | p = file_getname(path); | 
 | *p = '\0'; | *p = '\0'; | 
 | } | } | 
 |  |  | 
| char *file_getext(char *path) { | char * | 
|  | file_getext(char *path) | 
| char    *p; | { | 
| char    *q; | char *p; | 
|  | char *q; | 
 |  |  | 
 | p = file_getname(path); | p = file_getname(path); | 
 | q = NULL; | q = NULL; | 
| while(*p != '\0') { | while (*p != '\0') { | 
 | if (*p == '.') { | if (*p == '.') { | 
 | q = p + 1; | q = p + 1; | 
 | } | } | 
| Line 307  char *file_getext(char *path) { | Line 390  char *file_getext(char *path) { | 
 | if (q == NULL) { | if (q == NULL) { | 
 | q = p; | q = p; | 
 | } | } | 
| return(q); | return q; | 
 | } | } | 
 |  |  | 
| void file_cutext(char *path) { | void | 
|  | file_cutext(char *path) | 
| char    *p; | { | 
| char    *q; | char *p; | 
|  | char *q; | 
 |  |  | 
 | p = file_getname(path); | p = file_getname(path); | 
 | q = NULL; | q = NULL; | 
| while(*p != '\0') { | while (*p != '\0') { | 
 | if (*p == '.') { | if (*p == '.') { | 
 | q = p; | q = p; | 
 | } | } | 
| Line 328  void file_cutext(char *path) { | Line 412  void file_cutext(char *path) { | 
 | } | } | 
 | } | } | 
 |  |  | 
| void file_cutseparator(char *path) { | 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) && (path[pos] == '/')) { | 
| Line 338  void file_cutseparator(char *path) { | Line 423  void file_cutseparator(char *path) { | 
 | } | } | 
 | } | } | 
 |  |  | 
| void file_setseparator(char *path, int maxlen) { | void | 
|  | file_setseparator(char *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] != '/') && ((pos + 2) < maxlen)) { | 
| Line 348  void file_setseparator(char *path, int m | Line 434  void file_setseparator(char *path, int m | 
 | path[pos] = '\0'; | path[pos] = '\0'; | 
 | } | } | 
 | } | } | 
 |  |  |