long FileExist(char * name) { long i; if ((i = FileOpenRead(name)) == 0) return 0; FileCloseRead(i); return 1; }
void * FileLoadMalloc(char * name, long * SizeLoadMalloc) { long handle; long size1, size2; unsigned char * adr; retry: handle = FileOpenRead(name); if (!handle) { char str[256]; strcpy(str, name); int mb; mb = ShowError("FileLoadMalloc", str, 4); switch (mb) { case IDABORT: ExitApp(1); break; case IDRETRY: goto retry; break; case IDIGNORE: if (SizeLoadMalloc != NULL) *SizeLoadMalloc = 0; return(NULL); break; } } FileSeek(handle, 0, FILE_SEEK_END); size1 = FileSizeHandle(handle); adr = (unsigned char *)malloc(size1); if (!adr) { int mb; mb = ShowError("FileLoadMalloc", name, 4); switch (mb) { case IDABORT: ExitApp(1); break; case IDRETRY: FileCloseRead(handle); goto retry; break; case IDIGNORE: FileCloseRead(handle); if (SizeLoadMalloc != NULL) *SizeLoadMalloc = 0; return(0); break; } } FileSeek(handle, 0, FILE_SEEK_START); size2 = FileRead(handle, adr, size1); FileCloseRead(handle); if (size1 != size2) { free(adr); int mb; mb = ShowError("FileLoadMalloc", name, 4); switch (mb) { case IDABORT: ExitApp(1); break; case IDRETRY: goto retry; break; case IDIGNORE: if (SizeLoadMalloc != NULL) *SizeLoadMalloc = 0; return(0); break; } } if (SizeLoadMalloc != NULL) *SizeLoadMalloc = size1; return(adr); }
//! Use Expat parser to parse an XML file bool mxflib::XMLParserParseFile(XML_Parser *pParser, mxflib::XMLParserHandlerPtr Hand, void *UserData, const char *filename, bool ParseNamespaces /*=false*/) { if(!Hand) { error("No handler defined in call to XMLParserParseFile()\n"); return false; } // Open the input file FileHandle InFile = FileOpenRead(filename); if(!FileValid(InFile)) { Hand->fatalError(UserData, "Couldn't open file %s\n", filename); return false; } // Build a new parser XML_Parser Parser = ParseNamespaces ? XML_ParserCreateNS(NULL, '|') : XML_ParserCreate(NULL); if(!Parser) { Hand->fatalError(UserData, "Could't create an expat XML parser\n"); FileClose(InFile); return false; } // Set the caller's parser pointer if requested if(pParser) *pParser = Parser; // Set the element handlers XML_SetElementHandler(Parser, Hand->startElement, Hand->endElement); // Set the user data XML_SetUserData(Parser, UserData); int Done = 0; do { const int BufferSize = 1024 * 64; UInt8 *Buffer = (UInt8*)XML_GetBuffer(Parser, BufferSize); int Bytes = (int)FileRead(InFile, Buffer, BufferSize); if(FileEof(InFile)) Done = -1; if (XML_ParseBuffer(Parser, Bytes, Done) == XML_STATUS_ERROR) { Hand->fatalError(UserData, "Parse error at line %d:\n%s\n", XML_GetCurrentLineNumber(Parser), XML_ErrorString(XML_GetErrorCode(Parser))); XML_ParserFree(Parser); FileClose(InFile); return false; } } while(!Done); // Free the parser XML_ParserFree(Parser); FileClose(InFile); return true; }
//Write the file back out, with the changes bool BlueZip::Write(bool Store) { //TODO: Do not use a TempFile, send straight to the output char TempFileName[MAX_PATH]; File f; zList* z; zList** next = &Files; //where to insert the next zList int i, j; //global enumeration variables if ((Files == NULL) && (Pending == NULL)) { ErrMsg("Blank ZIP files not allowed"); return false; } //Always use a temporary file name (they may have the ZIP file on a floppy) f = FileOpenTemp(TempFileName); if (!FileValid(f)) { ErrMsg(Failed to open the temporary file); return false; } if (Files != NULL) { File Orig = FileOpenRead(FileName); if (!FileValid(Orig)) { ErrMsg("Failed to open the reading file"); return false; } const int BlockSize = 4096; char* Buffer = new char[BlockSize]; datCentral hLocal; for (z = Files; z != NULL; z = z->next) { if (!z->Delete) { //Remove any that have dropped out of the list *next = z; next = &z->next; //Perform a ZIP copy SeekBeg(Orig, z->data.Offset + z->FileDeltaPos); z->data.Offset = FilePos(f); u32 sig; FileRead(Orig, &sig, 4); Assert(sig == sigLocal); FileWrite(f, &sig, 4); hLocal.ReadLocal(Orig); hLocal.WriteLocal(f); i = hLocal.CompSize + hLocal.lFileName + hLocal.lExtra; while(i != 0) { j = min(i, BlockSize); FileRead(Orig, Buffer, j); FileWrite(f, Buffer, j); i -= j; } } } FileClose(Orig); delete[] Buffer; } while (Pending != NULL) { fList* fAdd = Pending; Pending = Pending->next; z = fAdd->ZipUp(f, Store); if (z == NULL) { ErrMsg("Failed to add the file"); } else { *next = z; next = &z->next; } delete fAdd; } //Write out the central header data.Count = 0; data.Offset = FilePos(f); for (z = Files; z != NULL; z = z->next, data.Count++) z->WriteCentral(f); data.Size = FilePos(f) - data.Offset; WriteEnd(f); FileClose(f); //Using a temp file if (!FileReplace(FileName, TempFileName)) { ErrMsg("Failed to copy the temporary file"); return false; } return true; }
/************************************** Main ********************************* PURPOSE: The purpose of the program is to allow users to insert 128k numbers into the BinarySearchTree and SplayTree and calculate the average time for several test cases of inserting 500, 1000, 2000, 4000, 8000, 16000, 32000, 64000, and 128000 numbers. The main function first initializes the array to hold numbers from a file called NUM128K.DAT. Then, it calls getMenu() which allows users to choose which algorithm to test or change the test case size. BST and splayTree functions have a timer each to calculate the execution time in nanosecond. The program will also output the time result. DATA TABLE: x...............................LONGLONG type that holds time result m...............................integer type that holds ifstream data size............................integer type that holds the size of the array (test cases) choice..........................integer type that is returned by getMenu() inFile..........................ifstream type that allows to get data from NUM128K.DAT file checkRead.......................boolean type that verifies file is opened properly Frequency.......................timer variable for converting to nanosecond PerformanceCount1...............timer starts PerformanceCount2...............timer stops *****************************************************************************/ int main( ) { LONGLONG x; int m; int size; int choice; ifstream inFile; //char buffer[80]; // initialize array to zero for( int s = 0; s < MAX_SIZE; s++ ) { myArray[s] = 0; } // check if file is opened for read int checkRead = FileOpenRead( inFile ); if( checkRead ) cout << "File Opened Successfully" << endl; else { cout << "File Opened Failed" << endl; exit(0); } // grab data from file into the array while( inFile.good() ) { int s = 0; while( inFile >> m && s < MAX_SIZE ) { myArray[s] = m; s++; } inFile.close(); } cout << "Input test cases of N ( 500 - 128000 ) -> " << flush; cin >> size; while( true ) { choice = getMenu(); switch( choice ) { case 1: // Timer starts QueryPerformanceFrequency( &Frequency ); QueryPerformanceCounter( &PerformanceCount1 ); // Call Binary Search Tree Function BST( size ); // Timer stops QueryPerformanceCounter( &PerformanceCount2 ); // subtract timer from start to end x = PerformanceCount2.QuadPart - PerformanceCount1.QuadPart; // convert to nanosecond unit x = 1000000000*x/Frequency.QuadPart; // output the time result //sprintf( buffer, "Execute time: %I64d\n", x / size ); printf("Execute time: %I64d\n", x / size ); // Empty the tree for the next test t.makeEmpty(); break; case 2: // Timer starts QueryPerformanceFrequency( &Frequency ); QueryPerformanceCounter( &PerformanceCount1 ); // Call SplayTree function splayTree( size ); // Timer stops QueryPerformanceCounter( &PerformanceCount2 ); // subtract timer from start to end x = PerformanceCount2.QuadPart - PerformanceCount1.QuadPart; // convert to nanosecond unit x = 1000000000*x/Frequency.QuadPart; // output the time result //sprintf( buffer, "Execute time: %I64d\n", x / size ); printf("Execute time: %I64d\n", x / size ); // Empty the tree for the next test splay.makeEmpty(); break; case 3: cout << "Input test cases of N ( 500 - 128000 ) -> " << flush; cin >> size; break; case 4: return 0; default: cout << "Invalid input\n"; break; } } return 0; }