--- np2/macosx/dosio.cpp 2003/10/16 17:59:37 1.1.1.1 +++ np2/macosx/dosio.cpp 2005/05/20 17:25:03 1.5 @@ -48,8 +48,8 @@ FILEH file_create(const char *path) { FILEH ret; FSSpec fss; Str255 fname; - OSType creator = '????'; - OSType fileType = '????'; + OSType creator = kUnknownType; + OSType fileType = kUnknownType; mkstr255(fname, path); FSMakeFSSpec(0, 0, fname, &fss); @@ -144,33 +144,39 @@ UINT file_getsize(FILEH handle) { } } +static void cnvdatetime(UTCDateTime *dt, DOSDATE *dosdate, DOSTIME *dostime) { + + LocalDateTime ldt; + DateTimeRec dtr; + + ZeroMemory(&dtr, sizeof(dtr)); + ConvertUTCToLocalDateTime(dt, &ldt); + SecondsToDate(ldt.lowSeconds, &dtr); + if (dosdate) { + dosdate->year = dtr.year; + dosdate->month = (UINT8)dtr.month; + dosdate->day = (UINT8)dtr.day; + } + if (dostime) { + dostime->hour = (UINT8)dtr.hour; + dostime->minute = (UINT8)dtr.minute; + dostime->second = (UINT8)dtr.second; + } +} + short file_getdatetime(FILEH handle, DOSDATE *dosdate, DOSTIME *dostime) { #ifdef TARGET_API_MAC_CARBON FSRef ref; FSCatalogInfo fsci; - LocalDateTime ldt; - DateTimeRec dtr; - ZeroMemory(&dtr, sizeof(dtr)); if ((FSGetForkCBInfo(handle, 0, NULL, NULL, NULL, &ref, NULL) != noErr) || (FSGetCatalogInfo(&ref, kFSCatInfoContentMod, &fsci, NULL, NULL, NULL) != noErr)) { return(-1); } - ConvertUTCToLocalDateTime(&fsci.contentModDate, &ldt); - SecondsToDate(ldt.lowSeconds, &dtr); - if (dosdate) { - dosdate->year = dtr.year; - dosdate->month = (BYTE)dtr.month; - dosdate->day = (BYTE)dtr.day; - } - if (dostime) { - dostime->hour = (BYTE)dtr.hour; - dostime->minute = (BYTE)dtr.minute; - dostime->second = (BYTE)dtr.second; - } + cnvdatetime(&fsci.contentModDate, dosdate, dostime); return(0); #else (void)handle; @@ -193,11 +199,11 @@ short file_delete(const char *path) { short file_attr(const char *path) { -#ifdef TARGET_API_MAC_CARBON Str255 fname; FSSpec fss; FSRef fsr; FSCatalogInfo fsci; + short ret; mkstr255(fname, path); if ((FSMakeFSSpec(0, 0, fname, &fss) != noErr) || @@ -206,38 +212,16 @@ short file_attr(const char *path) { NULL, NULL, NULL) != noErr)) { return(-1); } - else if (fsci.nodeFlags & kFSNodeIsDirectoryMask) { - return(FILEATTR_DIRECTORY); + if (fsci.nodeFlags & kFSNodeIsDirectoryMask) { + ret = FILEATTR_DIRECTORY; } else { - return(FILEATTR_ARCHIVE); + ret = FILEATTR_ARCHIVE; } -#else - Str255 fname; - FSSpec fss; - CInfoPBRec pb; - - mkstr255(fname, path); - FSMakeFSSpec(0, 0, fname, &fss); - pb.dirInfo.ioNamePtr = fss.name; - pb.dirInfo.ioVRefNum = fss.vRefNum; - pb.dirInfo.ioDrDirID = fss.parID; - if (fss.name[0] == 0) { - pb.dirInfo.ioFDirIndex = -1; + if (fsci.nodeFlags & kFSNodeLockedMask) { + ret |= FILEATTR_READONLY; } - else { - pb.dirInfo.ioFDirIndex = 0; - } - if (PBGetCatInfo(&pb, false) != noErr) { - return(-1); - } - if (pb.hFileInfo.ioFlAttrib & 0x10) { - return(FILEATTR_DIRECTORY); - } - else { - return(FILEATTR_ARCHIVE); - } -#endif + return(ret); } short file_dircreate(const char *path) { @@ -304,6 +288,133 @@ short file_attr_c(const char *path) { return(file_attr(curpath)); } +typedef struct { + BOOL eoff; + FSIterator fsi; + FSCatalogInfo fsci; + HFSUniStr255 name; +} _FLHDL, *FLHDL; + +static void char2str(char *dst, int size, const UniChar *uni, int unicnt) { + + CFStringRef cfsr; + + cfsr = CFStringCreateWithCharacters(NULL, uni, unicnt); + CFStringGetCString(cfsr, dst, size, CFStringGetSystemEncoding()); + CFRelease(cfsr); +} + +void *file_list1st(const char *dir, FLINFO *fli) { + + FLISTH ret; + Str255 fname; + FSSpec fss; + FSRef fsr; + FSIterator fsi; + + mkstr255(fname, dir); + if ((FSMakeFSSpec(0, 0, fname, &fss) != noErr) || + (FSpMakeFSRef(&fss, &fsr) != noErr) || + (FSOpenIterator(&fsr, kFSIterateFlat, &fsi) != noErr)) { + goto ff1_err1; + } + ret = _MALLOC(sizeof(_FLHDL), dir); + if (ret == NULL) { + goto ff1_err2; + } + ((FLHDL)ret)->eoff = FALSE; + ((FLHDL)ret)->fsi = fsi; + if (file_listnext(ret, fli) == SUCCESS) { + return(ret); + } + +ff1_err2: + FSCloseIterator(fsi); + +ff1_err1: + return(NULL); +} + +BOOL file_listnext(FLISTH hdl, FLINFO *fli) { + + FLHDL flhdl; + ItemCount count; + OSStatus r; + UTCDateTime *dt; + + flhdl = (FLHDL)hdl; + if ((flhdl == NULL) || (flhdl->eoff)) { + goto ffn_err; + } + r = FSGetCatalogInfoBulk(flhdl->fsi, 1, &count, NULL, + kFSCatInfoNodeFlags | kFSCatInfoDataSizes | + kFSCatInfoAllDates, + &flhdl->fsci, NULL, NULL, &flhdl->name); + if (r != noErr) { + flhdl->eoff = TRUE; + if (r != errFSNoMoreItems) { + goto ffn_err; + } + } + if (count != 1) { + flhdl->eoff = TRUE; + goto ffn_err; + } + if (fli) { + fli->caps = FLICAPS_SIZE | FLICAPS_ATTR | FLICAPS_DATE | FLICAPS_TIME; + if (flhdl->fsci.nodeFlags & kFSNodeIsDirectoryMask) { + fli->attr = FILEATTR_DIRECTORY; + fli->size = 0; + dt = &flhdl->fsci.createDate; + } + else { + fli->attr = FILEATTR_ARCHIVE; + fli->size = (UINT32)flhdl->fsci.dataLogicalSize; + dt = &flhdl->fsci.contentModDate; + } + cnvdatetime(dt, &fli->date, &fli->time); + char2str(fli->path, sizeof(fli->path), + flhdl->name.unicode, flhdl->name.length); + } + return(SUCCESS); + +ffn_err: + return(FAILURE); +} + +void file_listclose(FLISTH hdl) { + + if (hdl) { + FSCloseIterator(((FLHDL)hdl)->fsi); + _MFREE(hdl); + } +} + +BOOL getLongFileName(char *dst, const char *path) { + + FSSpec fss; + Str255 fname; + FSRef fref; + HFSUniStr255 name; + + if (*path == '\0') { + return(false); + } + mkstr255(fname, path); + FSMakeFSSpec(0, 0, fname, &fss); + FSpMakeFSRef(&fss, &fref); + if (FSGetCatalogInfo(&fref, kFSCatInfoNone, NULL, &name, NULL, NULL) + != noErr) { + return(false); + } + char2str(dst, 512, name.unicode, name.length); + if (!dst) { + return(false); + } + return(true); +} + + void file_catname(char *path, const char *sjis, int maxlen) { char *p; @@ -327,9 +438,9 @@ void file_catname(char *path, const char } } -char *file_getname(char *path) { +char *file_getname(const char *path) { - char *ret; + const char *ret; ret = path; while(*path != '\0') { @@ -337,7 +448,7 @@ char *file_getname(char *path) { ret = path; } } - return(ret); + return((char *)ret); } void file_cutname(char *path) { @@ -348,7 +459,7 @@ void file_cutname(char *path) { p[0] = '\0'; } -char *file_getext(char *path) { +char *file_getext(const char *path) { char *p; char *q;