Bool OpenReadOnlyTest() { Bool result = true; File* file; String text; UInt fileSize; //File does not exists, open it and it will fail File::Delete(_fileName); file = File::OpenReadOnly(_fileName); CHECK file == 0; //File exists, open it and it will work, write to it and it will fail CHECK (file = File::Create(_fileName)); file->Write((VoidPtr)_fileName, String::CStrByteSize(_fileName)); DeletePtr(file); CHECK (file = File::OpenReadOnly(_fileName)); fileSize = ToUInt(file->GetFileSize()); text.Reserve(fileSize / sizeof(TChar)); file->Read(text.DrivePointer(fileSize / sizeof(TChar)), fileSize); CHECK text == _fileName; DeletePtr(file); File::Delete(_fileName); return result; }
Bool TextFile::ReadAll(CStr fileName, String& text, Exception* out_ex) { ASSERT_PARAMETER(fileName); Bool result = false; File* file = File::Open(fileName, File::Disposition::OpenExisting, File::Access::AccessRead, File::Flags::ShareRead | File::Flags::OptimizeForSequentialAccess, out_ex); if(file) { UInt fileSize = ToUInt(file->GetFileSize()); UInt length = fileSize / sizeof(TChar); //If fileSize is odd then +1 will compensate the division rounding if(fileSize % 2) ++fileSize; text.Reserve(length); TChar* buffer = text.DrivePointer(length); result = file->Read(buffer, fileSize); GALATEA_DELETE_PTR(file); if(!result) { text.Clear(); text.Shrink(); } } return result; }
String TextFile::ReadAll(CStr fileName) { Assert(fileName); File* file; String text; UInt fileSize; UInt length; TChar* buffer; file = File::OpenReadOnly(fileName); if(file) { fileSize = ToUInt(file->GetFileSize()); length = fileSize / sizeof(TChar); text.Reserve(length); buffer = text.DrivePointer(length); file->Read(buffer, fileSize); DeletePtr(file); } return text; }