📄 file_02.cc
字号:
// file: $isip/class/system/File/file_02.cc// version: $Id: file_02.cc,v 1.4 2000/10/11 18:54:01 duncan Exp $//// isip include files//#include "File.h"#include <SysString.h>#include <Console.h>// method: diagnose//// arguments:// Integral::DEBUG level: (input) debug level for diagnostics//// return: a boolean value indicating status//boolean File::diagnose(Integral::DEBUG level_a) { //--------------------------------------------------------------------------- // // 0. preliminaries // //--------------------------------------------------------------------------- // output the class name // if (level_a > Integral::NONE) { SysString output(L"diagnosing class "); output.concat(CLASS_NAME); output.concat(L": "); Console::put(output); Console::increaseIndention(); } //--------------------------------------------------------------------------- // // 1. required public methods // //--------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing required public methods...\n"); Console::increaseIndention(); } // local variables, test constructor // File file_1; File file_2; // open an existing file // file_1.open(L"index.html"); // test the debug methods // file_1.setDebug(debug_level_d); if (level_a > Integral::BRIEF) { file_1.debug(L"debug"); } file_1.close(); // testing memory allocation // if (level_a > Integral::BRIEF) { Console::put(L"testing memory management...\n"); } File::setGrowSize((long)500); for (long j = 1; j <= 10; j++) { File** dyn_file = new File*[j * 100]; // create the objects // for (long i = 0; i < j * 100; i++) { dyn_file[i] = new File(); } // delete objects // for (long i = (j * 100) - 1; i >= 0; i--) { delete dyn_file[i]; } delete [] dyn_file; } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------------- // // 2. class-specific public methods: // extensions to required methods // //--------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: extensions to required methods...\n"); Console::increaseIndention(); } // local variables // File file_3; File file_4; // open file_3 // file_3.open(L"index.html"); // swap the two files // file_3.swap(file_4); // file_4 should be open now, while file_3 is not open // if (file_4.fp_d == (FILE*)NULL || file_3.fp_d != (FILE*)NULL) { return Error::handle(name(), L"swap", Error::TEST, __FILE__, __LINE__); } // get the memsize // long mem_size = file_3.memSize(); if (level_a > Integral::BRIEF) { SysString numeric; numeric.assign(mem_size); SysString output(L"memSize of file_3: "); output.concat(numeric); Console::put(output); } // close file // file_4.close(); // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------------- // // 3. class-specific public methods: // file operation methods // //--------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: file operation methods...\n"); Console::increaseIndention(); } // local variables // File file_read; File file_write; SysString existing_file; existing_file.assign(L"index.html"); SysString temp_file; Integral::makeTemp(temp_file); // set open retry // file_read.setOpenRetry(DEF_OPEN_RETRY, DEF_OPEN_DELAY); // open a file in read text mode // if (!file_read.open(existing_file)) { return Error::handle(name(), L"open", Error::TEST, __FILE__, __LINE__); } // logical test // if (!file_read.isReadable() || !file_read.isOpen()) { return Error::handle(name(), L"open read", Error::TEST, __FILE__, __LINE__); } // open a file in write text mode // if (!file_write.open(temp_file, File::WRITE_ONLY, File::TEXT)) { return Error::handle(name(), L"open", Error::TEST, __FILE__, __LINE__); } // lock the file // file_write.lock(); // logical test // if (!file_write.isWritable() || !file_write.isOpen() || !file_write.isLocked()) { return Error::handle(name(), L"open lock", Error::TEST, __FILE__, __LINE__); } // unlock the file // file_write.unlock(); // close the files // file_read.close(); file_write.close(); // logical test // if (file_read.isOpen() || file_write.isOpen() || file_write.isLocked()) { return Error::handle(name(), L"close / lock", Error::TEST, __FILE__, __LINE__); } // open a file in write binary mode // Integral::makeTemp(temp_file); file_write.open(temp_file, File::WRITE_ONLY, File::TEXT); // write something into it // long data[50]; for (long i = 0; i < 50; i++) { data[i] = i; } if (file_write.write(data, sizeof(long), 50) != 50) { return Error::handle(name(), L"write", Error::TEST, __FILE__, __LINE__); } if (file_write.write(data, sizeof(long), 10) != 10) { return Error::handle(name(), L"write", Error::TEST, __FILE__, __LINE__); } // test the file size // long pos = file_write.size(); // the size of file should be the size of 60 long numbers // if (pos != 60 * sizeof(long)) { return Error::handle(name(), L"seek/tell", Error::TEST, __FILE__, __LINE__); } // truncate to having only 50 numbers // file_write.truncate(50 * sizeof(long)); // get the file size again // pos = file_write.size(); // the size of file should be the size of 55 long numbers // if (pos != 50 * sizeof(long)) { printf("pos %ld\n", pos); return Error::handle(name(), L"size", Error::TEST, __FILE__, __LINE__); } // close the binary file // file_write.close(); // test the size of the file // if (size(temp_file) != 50 * sizeof(long)) { return Error::handle(name(), L"size", Error::TEST, __FILE__, __LINE__); } // test an error condition for file size // SysString err_temp_file; Integral::makeTemp(err_temp_file); Console::open(err_temp_file); long size_err = size(L"THIS_FILE_SHOULD_NOT_EXIST"); Console::close(); if (size_err != -1) { return Error::handle(name(), L"size", Error::TEST, __FILE__, __LINE__); } // open the file in read binary mode // file_read.open(temp_file, File::READ_ONLY, File::BINARY); // read the numbers // long data_read[50]; // we are trying to read more than the 50 available numbers, // the actual number read should still be 50, and an EOF is detected // if (file_read.read(data_read, sizeof(long), 55) != 50) { return Error::handle(name(), L"write/read", Error::TEST, __FILE__, __LINE__); } for (long i = 0; i < 50; i++) { if (data_read[i] != data[i]) { return Error::handle(name(), L"write/read", Error::TEST, __FILE__, __LINE__); } } // we are at the end of the file now, an EOF has been detected // if (!file_read.eof()) { return Error::handle(name(), L"eof", Error::TEST, __FILE__, __LINE__); } // rewind the file // file_read.rewind(); pos = file_read.tell(); if (pos != 0) { return Error::handle(name(), L"rewind", Error::TEST, __FILE__, __LINE__); } // close the file // file_read.close(); // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------------- // // 4. class-specific public methods: // i/o methods // //--------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: i/o methods...\n"); Console::increaseIndention(); } // local variables // File out(File::ERROR); File foo_wo; File foo_rp; SysString str1, str2; SysChar char1, char2; // open a file for write only // foo_wo.open(temp_file, File::WRITE_ONLY, File::TEXT); // write a string, a new line char and a char to it // str1.assign(L"testing output"); char1.assign(L'X'); foo_wo.put(str1); foo_wo.put(L"\n"); foo_wo.put(char1); // test the output to stderr // if (level_a > Integral::BRIEF) { Console::put(L"this is testing the stderr");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -