unsigned long fileSize(FILE *stream, char *name) { #ifdef USE_FILE_API long n = 0; FILE *f = stream; if (!f) { if (!name) return 0; f = fileOpen(name, "r"); if (!f) return 0; } fileSeek(f, 0, SEEK_END); n = fileTell(f); if (!stream) { fileClose(f); } else { fileSeek(f, 0, SEEK_SET); } return n; #else FILE *f = stream; Err error = errNone; UInt32 size = 0; if (!f || !f->fileRef) return 0; error = VFSFileSize(f->fileRef, &size); return (error)?-1:size; #endif }
/* Add file to doc list */ static void AddVFSFile ( UInt16 volRefNum, FileInfoType* info, const Char* dir, UInt16 location, EnumerateCardType enumerateWhat ) { Err err; Char* path; FileRef ref; DocumentInfo docInfo; UInt32 type; UInt32 creatorID; /* Set path to document */ path = SafeMemPtrNew( StrLen( dir ) + StrLen( info->nameP ) + 2 ); StrCopy( path, dir ); StrCat( path, info->nameP ); err = VFSFileOpen( volRefNum, path, vfsModeRead, &ref ); if ( err == errNone ) { Char volumeLabel[ LABEL_LEN ]; err = VFSFileDBInfo( ref, docInfo.name, &docInfo.attributes, NULL, &docInfo.created, NULL, NULL, NULL, NULL, NULL, &type, &creatorID, NULL ); if ( err == errNone && IsDocScan( enumerateWhat ) && type == ViewerDocumentType && creatorID == ViewerAppID ) { DocumentData* handlePtr; MemHandle handle; UInt16 index; VFSFileSize( ref, &docInfo.size ); GetPluckerVolumeLabel( volRefNum, volumeLabel, LABEL_LEN ); docInfo.cardNo = 0; docInfo.filename = info->nameP; VFSFileClose( ref ); handle = FindDocData( docInfo.name, numOfElements, &index ); if ( handle != NULL ) { UInt16 categories; UInt32 oldCreated; UInt16 oldLocation; Boolean oldActive; Char oldVolumeLabel[ LABEL_LEN ]; UInt16 fileLen; UInt16 volumeLabelLen; UInt32 timestamp; SetFoundDocument( index ); handlePtr = MemHandleLock( handle ); oldCreated = handlePtr->created; oldLocation = handlePtr->location; oldActive = handlePtr->active; categories = handlePtr->categories; timestamp = handlePtr->timestamp; fileLen = StrLen( handlePtr->data ) + 1; volumeLabelLen = StrLen( handlePtr->data + fileLen ) + 1; StrNCopy( oldVolumeLabel, handlePtr->data + fileLen, volumeLabelLen ); MemHandleUnlock( handle ); /* If the document is newer than the stored one, or moved to external card then updated info */ if ( ! oldActive || oldCreated < docInfo.created || oldLocation != location || ! STREQ( volumeLabel, oldVolumeLabel ) ) { /* Assign remaining document data */ docInfo.location = location; docInfo.active = true; docInfo.categories = categories; /* A "new" document should be indicated as unread */ if ( ! oldActive || oldCreated < docInfo.created ) docInfo.timestamp = 0; else docInfo.timestamp = timestamp; /* If document has been moved to external card the meta database must be renamed */ if ( oldLocation != location ) VFSRenameMetaDocument( docInfo.name, oldLocation, &docInfo ); UpdateDocument( &docInfo, volumeLabel, index, &handle ); } } else { docInfo.location = location; docInfo.volumeRef = volRefNum; docInfo.active = true; docInfo.timestamp = 0; ErrTry { docInfo.categories = GetDefaultCategories( &docInfo ); } ErrCatch( UNUSED_PARAM( err ) ) { docInfo.categories = UNFILED_CATEGORY; } ErrEndCatch AddDocument( &docInfo, volumeLabel ); } }