DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
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 
00315         for (ret = 0; pos >= 0; ret ^= 1) {
00316                 int c = (UINT8)str[pos--];
00317                 if (!ISKANJI(c))
00318                         break;
00319         }
00320         return ret;
00321 }
00322 
00323 void
00324 file_cpyname(OEMCHAR *dst, const OEMCHAR *src, int maxlen)
00325 {
00326 
00327         if (maxlen-- > 0) {
00328                 int i;
00329                 for (i = 0; i < maxlen && src[i] != '\0'; i++) {
00330                         dst[i] = src[i];
00331                 }
00332                 if (i > 0) {
00333                         if (euckanji1st(src, i-1)) {
00334                                 i--;
00335                         }
00336                 }
00337                 dst[i] = '\0';
00338         }
00339 }
00340 
00341 void
00342 file_catname(OEMCHAR *path, const OEMCHAR *filename, int maxlen)
00343 {
00344 
00345         for (; maxlen > 0; path++, maxlen--) {
00346                 if (*path == '\0') {
00347                         break;
00348                 }
00349         }
00350         if (maxlen > 0) {
00351 //              milstr_ncpy(path, filename, maxlen);
00352         strncpy(path, filename, maxlen);
00353                 for (; *path != '\0'; path++) {
00354                         if (!ISKANJI(*path)) {
00355                                 path++;
00356                                 if (*path == '\0') {
00357                                         break;
00358                                 }
00359                         } else if (((*path - 0x41) & 0xff) < 26) {
00360                                 *path |= 0x20;
00361                         } else if (*path == '\\') {
00362                                 *path = _G_DIR_SEPARATOR;
00363                         }
00364                 }
00365         }
00366 }
00367 
00368 BOOL
00369 file_cmpname(const OEMCHAR *path, const OEMCHAR *path2)
00370 {
00371 
00372         return strcasecmp(path, path2);
00373 }
00374 
00375 OEMCHAR *
00376 file_getname(const OEMCHAR *path)
00377 {
00378         const OEMCHAR *ret;
00379 
00380         for (ret = path; *path != '\0'; path++) {
00381                 if (ISKANJI(*path)) {
00382                         path++;
00383                         if (*path == '\0') {
00384                                 break;
00385                         }
00386                 } else if (*path == _G_DIR_SEPARATOR) {
00387                         ret = path + 1;
00388                 }
00389         }
00390         return (OEMCHAR *)ret;
00391 }
00392 
00393 void
00394 file_cutname(OEMCHAR *path)
00395 {
00396         OEMCHAR *p;
00397 
00398         p = file_getname(path);
00399         *p = '\0';
00400 }
00401 
00402 OEMCHAR *
00403 file_getext(const OEMCHAR *path)
00404 {
00405         const OEMCHAR *p, *q;
00406 
00407         for (p = file_getname(path), q = NULL; *p != '\0'; p++) {
00408                 if (*p == '.') {
00409                         q = p + 1;
00410                 }
00411         }
00412         if (q == NULL) {
00413                 q = p;
00414         }
00415         return (OEMCHAR *)q;
00416 }
00417 
00418 void
00419 file_cutext(OEMCHAR *path)
00420 {
00421         OEMCHAR *p, *q;
00422 
00423         for (p = file_getname(path), q = NULL; *p != '\0'; p++) {
00424                 if (*p == '.') {
00425                         q = p;
00426                 }
00427         }
00428         if (q != NULL) {
00429                 *q = '\0';
00430         }
00431 }
00432 
00433 void
00434 file_cutseparator(OEMCHAR *path)
00435 {
00436         int pos;
00437 
00438         pos = (int)strlen(path) - 1;
00439         if ((pos > 0) && (path[pos] == _G_DIR_SEPARATOR)) {
00440                 path[pos] = '\0';
00441         }
00442 }
00443 
00444 void
00445 file_setseparator(OEMCHAR *path, int maxlen)
00446 {
00447         int pos;
00448 
00449         pos = (int)strlen(path);
00450         if ((pos) && (path[pos-1] != _G_DIR_SEPARATOR) && ((pos + 2) < maxlen)) {
00451                 path[pos++] = _G_DIR_SEPARATOR;
00452                 path[pos] = '\0';
00453         }
00454 }