UINT64 mame_fsize(mame_file *file) { /* switch off the file type */ switch (file->type) { case PLAIN_FILE: { int size, offs; offs = osd_ftell(file->file); osd_fseek(file->file, 0, SEEK_END); size = osd_ftell(file->file); osd_fseek(file->file, offs, SEEK_SET); return size; } case RAM_FILE: case ZIPPED_FILE: return file->length; } return 0; }
UINT64 mame_ftell(mame_file *file) { /* switch off the file type */ switch (file->type) { case PLAIN_FILE: return osd_ftell(file->file); case RAM_FILE: case ZIPPED_FILE: return file->offset; } return -1L; }
/* Opens a zip stream for reading return: !=0 success, zip stream ==0 error */ ZIP* openzip(int pathtype, int pathindex, const char* zipfile) { /* allocate */ ZIP* zip = (ZIP*)malloc( sizeof(ZIP) ); if (!zip) { return 0; } /* open */ zip->fp = osd_fopen(pathtype, pathindex, zipfile, "rb"); if (!zip->fp) { errormsg ("Opening for reading", ERROR_FILESYSTEM, zipfile); free(zip); return 0; } /* go to end */ if (osd_fseek(zip->fp, 0L, SEEK_END) != 0) { errormsg ("Seeking to end", ERROR_FILESYSTEM, zipfile); osd_fclose(zip->fp); free(zip); return 0; } /* get length */ zip->length = osd_ftell(zip->fp); if (zip->length < 0) { errormsg ("Get file size", ERROR_FILESYSTEM, zipfile); osd_fclose(zip->fp); free(zip); return 0; } if (zip->length == 0) { errormsg ("Empty file", ERROR_CORRUPT, zipfile); osd_fclose(zip->fp); free(zip); return 0; } /* read ecd data */ if (ecd_read(zip)!=0) { errormsg ("Reading ECD (end of central directory)", ERROR_CORRUPT, zipfile); osd_fclose(zip->fp); free(zip); return 0; } /* compile ecd info */ zip->end_of_cent_dir_sig = read_dword (zip->ecd+ZIPESIG); zip->number_of_this_disk = read_word (zip->ecd+ZIPEDSK); zip->number_of_disk_start_cent_dir = read_word (zip->ecd+ZIPECEN); zip->total_entries_cent_dir_this_disk = read_word (zip->ecd+ZIPENUM); zip->total_entries_cent_dir = read_word (zip->ecd+ZIPECENN); zip->size_of_cent_dir = read_dword (zip->ecd+ZIPECSZ); zip->offset_to_start_of_cent_dir = read_dword (zip->ecd+ZIPEOFST); zip->zipfile_comment_length = read_word (zip->ecd+ZIPECOML); zip->zipfile_comment = zip->ecd+ZIPECOM; /* verify that we can work with this zipfile (no disk spanning allowed) */ if ((zip->number_of_this_disk != zip->number_of_disk_start_cent_dir) || (zip->total_entries_cent_dir_this_disk != zip->total_entries_cent_dir) || (zip->total_entries_cent_dir < 1)) { errormsg("Cannot span disks", ERROR_UNSUPPORTED, zipfile); free(zip->ecd); osd_fclose(zip->fp); free(zip); return 0; } if (osd_fseek(zip->fp, zip->offset_to_start_of_cent_dir, SEEK_SET)!=0) { errormsg ("Seeking to central directory", ERROR_CORRUPT, zipfile); free(zip->ecd); osd_fclose(zip->fp); free(zip); return 0; } /* read from start of central directory */ zip->cd = (char*)malloc( zip->size_of_cent_dir ); if (!zip->cd) { free(zip->ecd); osd_fclose(zip->fp); free(zip); return 0; } if (osd_fread(zip->fp, zip->cd, zip->size_of_cent_dir)!=zip->size_of_cent_dir) { errormsg ("Reading central directory", ERROR_CORRUPT, zipfile); free(zip->cd); free(zip->ecd); osd_fclose(zip->fp); free(zip); return 0; } /* reset ent */ zip->ent.name = 0; /* rewind */ zip->cd_pos = 0; /* file name */ zip->zip = (char*)malloc(strlen(zipfile)+1); if (!zip->zip) { free(zip->cd); free(zip->ecd); osd_fclose(zip->fp); free(zip); return 0; } strcpy(zip->zip, zipfile); zip->pathtype = pathtype; zip->pathindex = pathindex; return zip; }
//----------------------------------- // Constructor //----------------------------------- CSystem_IniFile::CSystem_IniFile( const CStdString &strFullPath ) : m_dirtyFlag( FALSE ) { if( strFullPath == "" ) return; m_fileName = strFullPath; std::vector< std::string > inputVector; // Read the entire file into RAM, as reads/writes are unbuffered osd_file *file = osd_fopen( FILETYPE_MAMEOX_FULLPATH, 0, m_fileName.c_str(), "r" ); if( !file ) return; osd_fseek( file, 0, SEEK_END ); UINT32 fileSize = osd_ftell( file ); osd_fseek( file, 0, SEEK_SET ); char *buffer = new char[fileSize]; if( !buffer ) return; osd_fread( file, buffer, fileSize ); osd_fclose( file ); // Break the file up into a vector of lines UINT32 i = 0; while( i < fileSize ) { char buf[8192]; UINT32 j = 0; for( ; buffer[i] != '\n' && buffer[i] != '\r' && j < 8192; ++i, ++j ) buf[j] = buffer[i]; // Skip paired \r\n's while( buffer[i+1] == '\n' || buffer[i+1] == '\r' ) ++i; if( j == 8192 ) { PRINTMSG(( T_ERROR, "Corrupt INI file, read 8k chars before finding a \\n!" )); return; } else buf[j] = 0; // Catch no newline at end of file if( buffer[i] == '\n' || buffer[i] == '\r' ) { ++i; std::string inputStr = buf; inputVector.push_back( CSystem_StringModifier::KillLeadingWhitespaceStr( inputStr ) ); } else break; } // The file data is no longer needed delete[] buffer; // Break each line up std::vector<std::string>::iterator it = inputVector.begin(); for( ; it != inputVector.end(); ++it ) { // Check to see if the current string is a new section if( (*it)[0] == '[' ) { std::string sectionHeader = (*it); std::vector<std::string> temp; for( ;(it+1) != inputVector.end() && (*(it+1))[0] != '['; ++it ) { if( (*(it+1)).size() ) // Don't bother storing blank lines temp.push_back( (*(it+1)) ); } m_data[sectionHeader] = temp; } } }