DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
src/hardware/snd_pc98/x11/dosio.c
00001 #include "np2glue.h"
00002 //#include "compiler.h"
00003 
00004 #include <sys/stat.h>
00005 #include <time.h>
00006 
00007 //#include "codecnv.h"
00008 #include "dosio.h"
00009 
00010 
00011 //static OEMCHAR curpath[MAX_PATH];
00012 //static OEMCHAR *curfilep = curpath;
00013 
00014 #define ISKANJI(c)      ((((c) - 0xa1) & 0xff) < 0x5c)
00015 
00016 
00017 void
00018 dosio_init(void)
00019 {
00020 
00021         /* nothing to do */
00022 }
00023 
00024 void
00025 dosio_term(void)
00026 {
00027 
00028         /* nothing to do */
00029 }
00030 
00031 /* ファイル操作 */
00032 FILEH
00033 file_open(const OEMCHAR *path)
00034 {
00035         FILEH fh;
00036 
00037         fh = fopen(path, "rb+");
00038         if (fh)
00039                 return fh;
00040         return fopen(path, "rb");
00041 }
00042 
00043 FILEH
00044 file_open_rb(const OEMCHAR *path)
00045 {
00046 
00047         return fopen(path, "rb");
00048 }
00049 
00050 FILEH
00051 file_create(const OEMCHAR *path)
00052 {
00053 
00054         return fopen(path, "wb+");
00055 }
00056 
00057 long
00058 file_seek(FILEH handle, long pointer, int method)
00059 {
00060 
00061         fseek(handle, pointer, method);
00062         return ftell(handle);
00063 }
00064 
00065 UINT
00066 file_read(FILEH handle, void *data, UINT length)
00067 {
00068 
00069         return (UINT)fread(data, 1, length, handle);
00070 }
00071 
00072 UINT
00073 file_write(FILEH handle, const void *data, UINT length)
00074 {
00075 
00076         return (UINT)fwrite(data, 1, length, handle);
00077 }
00078 
00079 short
00080 file_close(FILEH handle)
00081 {
00082 
00083         fclose(handle);
00084         return 0;
00085 }
00086 
00087 UINT
00088 file_getsize(FILEH handle)
00089 {
00090         struct stat sb;
00091 
00092         if (fstat(fileno(handle), &sb) == 0)
00093                 return sb.st_size;
00094         return 0;
00095 }
00096 
00097 #if 0
00098 short
00099 file_attr(const OEMCHAR *path)
00100 {
00101         struct stat sb;
00102         short attr;
00103 
00104         if (stat(path, &sb) == 0) {
00105                 if (S_ISDIR(sb.st_mode)) {
00106                         return FILEATTR_DIRECTORY;
00107                 }
00108                 attr = 0;
00109                 if (!(sb.st_mode & S_IWUSR)) {
00110                         attr |= FILEATTR_READONLY;
00111                 }
00112                 return attr;
00113         }
00114         return -1;
00115 }
00116 #endif
00117 
00118 static BOOL
00119 cnvdatetime(struct stat *sb, DOSDATE *dosdate, DOSTIME *dostime)
00120 {
00121         struct tm *ftime;
00122 
00123         ftime = localtime(&sb->st_mtime);
00124         if (ftime) {
00125                 if (dosdate) {
00126                         dosdate->year = ftime->tm_year + 1900;
00127                         dosdate->month = ftime->tm_mon + 1;
00128                         dosdate->day = ftime->tm_mday;
00129                 }
00130                 if (dostime) {
00131                         dostime->hour = ftime->tm_hour;
00132                         dostime->minute = ftime->tm_min;
00133                         dostime->second = ftime->tm_sec;
00134                 }
00135                 return SUCCESS;
00136         }
00137         return FAILURE;
00138 }
00139 
00140 short
00141 file_getdatetime(FILEH handle, DOSDATE *dosdate, DOSTIME *dostime)
00142 {
00143         struct stat sb;
00144 
00145         if ((fstat(fileno(handle), &sb) == 0)
00146          && (cnvdatetime(&sb, dosdate, dostime)))
00147                 return 0;
00148         return -1;
00149 }
00150 
00151 #if 0
00152 short
00153 file_delete(const OEMCHAR *path)
00154 {
00155 
00156         return (short)unlink(path);
00157 }
00158 
00159 short
00160 file_dircreate(const OEMCHAR *path)
00161 {
00162 
00163         return (short)mkdir(path, 0777);
00164 }
00165 #endif
00166 
00167 
00168 #if 0
00169 /* カレントファイル操作 */
00170 void
00171 file_setcd(const OEMCHAR *exepath)
00172 {
00173 
00174         milstr_ncpy(curpath, exepath, sizeof(curpath));
00175         curfilep = file_getname(curpath);
00176         *curfilep = '\0';
00177 }
00178 
00179 char *
00180 file_getcd(const OEMCHAR *filename)
00181 {
00182 
00183         *curfilep = '\0';
00184         file_catname(curpath, filename, sizeof(curpath));
00185         return curpath;
00186 }
00187 
00188 FILEH
00189 file_open_c(const OEMCHAR *filename)
00190 {
00191 
00192         *curfilep = '\0';
00193         file_catname(curpath, filename, sizeof(curpath));
00194         return file_open(curpath);
00195 }
00196 
00197 FILEH
00198 file_open_rb_c(const OEMCHAR *filename)
00199 {
00200 
00201         *curfilep = '\0';
00202         file_catname(curpath, filename, sizeof(curpath));
00203         return file_open_rb(curpath);
00204 }
00205 
00206 FILEH
00207 file_create_c(const OEMCHAR *filename)
00208 {
00209 
00210         *curfilep = '\0';
00211         file_catname(curpath, filename, sizeof(curpath));
00212         return file_create(curpath);
00213 }
00214 
00215 short
00216 file_delete_c(const OEMCHAR *filename)
00217 {
00218 
00219         *curfilep = '\0';
00220         file_catname(curpath, filename, sizeof(curpath));
00221         return file_delete(curpath);
00222 }
00223 
00224 short
00225 file_attr_c(const OEMCHAR *filename)
00226 {
00227 
00228         *curfilep = '\0';
00229         file_catname(curpath, filename, sizeof(curpath));
00230         return file_attr(curpath);
00231 }
00232 #endif
00233 
00234 #if 0
00235 FLISTH
00236 file_list1st(const OEMCHAR *dir, FLINFO *fli)
00237 {
00238         FLISTH ret;
00239 
00240         ret = (FLISTH)_MALLOC(sizeof(_FLISTH), "FLISTH");
00241         if (ret == NULL) {
00242                 VERBOSE(("file_list1st: couldn't alloc memory (size = %d)", sizeof(_FLISTH)));
00243                 return FLISTH_INVALID;
00244         }
00245 
00246         milstr_ncpy(ret->path, dir, sizeof(ret->path));
00247         file_setseparator(ret->path, sizeof(ret->path));
00248         ret->hdl = opendir(ret->path);
00249         VERBOSE(("file_list1st: opendir(%s)", ret->path));
00250         if (ret->hdl == NULL) {
00251                 VERBOSE(("file_list1st: opendir failure"));
00252                 _MFREE(ret);
00253                 return FLISTH_INVALID;
00254         }
00255         if (file_listnext((FLISTH)ret, fli) == SUCCESS) {
00256                 return (FLISTH)ret;
00257         }
00258         VERBOSE(("file_list1st: file_listnext failure"));
00259         closedir(ret->hdl);
00260         _MFREE(ret);
00261         return FLISTH_INVALID;
00262 }
00263 
00264 BOOL
00265 file_listnext(FLISTH hdl, FLINFO *fli)
00266 {
00267         OEMCHAR buf[MAX_PATH];
00268         struct dirent *de;
00269         struct stat sb;
00270 
00271         de = readdir(hdl->hdl);
00272         if (de == NULL) {
00273                 VERBOSE(("file_listnext: readdir failure"));
00274                 return FAILURE;
00275         }
00276 
00277         milstr_ncpy(buf, hdl->path, sizeof(buf));
00278         milstr_ncat(buf, de->d_name, sizeof(buf));
00279         if (stat(buf, &sb) != 0) {
00280                 VERBOSE(("file_listnext: stat failure. (path = %s)", buf));
00281                 return FAILURE;
00282         }
00283 
00284         fli->caps = FLICAPS_SIZE | FLICAPS_ATTR | FLICAPS_DATE | FLICAPS_TIME;
00285         fli->size = sb.st_size;
00286         fli->attr = 0;
00287         if (S_ISDIR(sb.st_mode)) {
00288                 fli->attr |= FILEATTR_DIRECTORY;
00289         }
00290         if (!(sb.st_mode & S_IWUSR)) {
00291                 fli->attr |= FILEATTR_READONLY;
00292         }
00293         cnvdatetime(&sb, &fli->date, &fli->time);
00294         milstr_ncpy(fli->path, de->d_name, sizeof(fli->path));
00295         VERBOSE(("file_listnext: success"));
00296         return SUCCESS;
00297 }
00298 
00299 void
00300 file_listclose(FLISTH hdl)
00301 {
00302 
00303         if (hdl) {
00304                 closedir(hdl->hdl);
00305                 _MFREE(hdl);
00306         }
00307 }
00308 #endif
00309 
00310 static int
00311 euckanji1st(const OEMCHAR *str, int pos)
00312 {
00313         int ret;
00314         int c;
00315 
00316         for (ret = 0; pos >= 0; ret ^= 1) {
00317                 c = (UINT8)str[pos--];
00318                 if (!ISKANJI(c))
00319                         break;
00320         }
00321         return ret;
00322 }
00323 
00324 void
00325 file_cpyname(OEMCHAR *dst, const OEMCHAR *src, int maxlen)
00326 {
00327         int i;
00328 
00329         if (maxlen-- > 0) {
00330                 for (i = 0; i < maxlen && src[i] != '\0'; i++) {
00331                         dst[i] = src[i];
00332                 }
00333                 if (i > 0) {
00334                         if (euckanji1st(src, i-1)) {
00335                                 i--;
00336                         }
00337                 }
00338                 dst[i] = '\0';
00339         }
00340 }
00341 
00342 void
00343 file_catname(OEMCHAR *path, const OEMCHAR *filename, int maxlen)
00344 {
00345 
00346         for (; maxlen > 0; path++, maxlen--) {
00347                 if (*path == '\0') {
00348                         break;
00349                 }
00350         }
00351         if (maxlen > 0) {
00352 //              milstr_ncpy(path, filename, maxlen);
00353         strncpy(path, filename, maxlen);
00354                 for (; *path != '\0'; path++) {
00355                         if (!ISKANJI(*path)) {
00356                                 path++;
00357                                 if (*path == '\0') {
00358                                         break;
00359                                 }
00360                         } else if (((*path - 0x41) & 0xff) < 26) {
00361                                 *path |= 0x20;
00362                         } else if (*path == '\\') {
00363                                 *path = _G_DIR_SEPARATOR;
00364                         }
00365                 }
00366         }
00367 }
00368 
00369 BOOL
00370 file_cmpname(const OEMCHAR *path, const OEMCHAR *path2)
00371 {
00372 
00373         return strcasecmp(path, path2);
00374 }
00375 
00376 OEMCHAR *
00377 file_getname(const OEMCHAR *path)
00378 {
00379         const OEMCHAR *ret;
00380 
00381         for (ret = path; *path != '\0'; path++) {
00382                 if (ISKANJI(*path)) {
00383                         path++;
00384                         if (*path == '\0') {
00385                                 break;
00386                         }
00387                 } else if (*path == _G_DIR_SEPARATOR) {
00388                         ret = path + 1;
00389                 }
00390         }
00391         return (OEMCHAR *)ret;
00392 }
00393 
00394 void
00395 file_cutname(OEMCHAR *path)
00396 {
00397         OEMCHAR *p;
00398 
00399         p = file_getname(path);
00400         *p = '\0';
00401 }
00402 
00403 OEMCHAR *
00404 file_getext(const OEMCHAR *path)
00405 {
00406         const OEMCHAR *p, *q;
00407 
00408         for (p = file_getname(path), q = NULL; *p != '\0'; p++) {
00409                 if (*p == '.') {
00410                         q = p + 1;
00411                 }
00412         }
00413         if (q == NULL) {
00414                 q = p;
00415         }
00416         return (OEMCHAR *)q;
00417 }
00418 
00419 void
00420 file_cutext(OEMCHAR *path)
00421 {
00422         OEMCHAR *p, *q;
00423 
00424         for (p = file_getname(path), q = NULL; *p != '\0'; p++) {
00425                 if (*p == '.') {
00426                         q = p;
00427                 }
00428         }
00429         if (q != NULL) {
00430                 *q = '\0';
00431         }
00432 }
00433 
00434 void
00435 file_cutseparator(OEMCHAR *path)
00436 {
00437         int pos;
00438 
00439         pos = (int)strlen(path) - 1;
00440         if ((pos > 0) && (path[pos] == _G_DIR_SEPARATOR)) {
00441                 path[pos] = '\0';
00442         }
00443 }
00444 
00445 void
00446 file_setseparator(OEMCHAR *path, int maxlen)
00447 {
00448         int pos;
00449 
00450         pos = (int)strlen(path);
00451         if ((pos) && (path[pos-1] != _G_DIR_SEPARATOR) && ((pos + 2) < maxlen)) {
00452                 path[pos++] = _G_DIR_SEPARATOR;
00453                 path[pos] = '\0';
00454         }
00455 }