// $Revision: 1.6 $ // Copyright (C) 1994-1995 Taligent, Inc. All rights reserved. #ifndef TaligentSamples_FILESYSTEMSNIPPETS #include "FileSystemSnippets.h" #endif #ifndef Taligent_FILESYSTEMINTERFACE #include #endif #ifndef Taligent_MULTIPLEROOTLOCATOR #include #endif #ifndef TaligentSamples_PRIORITYPROPERTY #include "PriorityProperty.h" #endif TaligentTypeExtensionMacro(TFileSystemSnippets) TFileSystemSnippets::TFileSystemSnippets() : TSnippets(), fDirectory() { SNIPPETINFO(GetEntityFromPath); SNIPPETINFO(PathToText); SNIPPETINFO(GetPathOfEntity); SNIPPETINFO(GetNameOfEntity); SNIPPETINFO(PrintDirectory); SNIPPETINFO(CreateDirectory); SNIPPETINFO(CreateFile); SNIPPETINFO(DeleteEntity); SNIPPETINFO(DeleteDirectoryContents); SNIPPETINFO(WriteToFile); SNIPPETINFO(AppendToFile); SNIPPETINFO(ReadFromFile); SNIPPETINFO(PrintTestFile); SNIPPETINFO(ReadFromMappedFile); // SNIPPETINFO(UsingAFileRange); // SNIPPETINFO(WritingAProperty); // SNIPPETINFO(ReadingAProperty); SNIPPETINFO(CopyAnEntity); SNIPPETINFO(MoveAnEntity); } TFileSystemSnippets::~TFileSystemSnippets() { } // This shows how to get a file system entity from a given path. void TFileSystemSnippets::GetEntityFromPath() { TMultipleRootLocator locator("RuntimeEnv/Data/Samples/FileSystem"); try { //- TFile file = locator.FindFirstByName("README.FileSystemSnippets"); //- GetDisplay() << file.GetName() << endl; } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "README.FileSystemSnippets not found." << endl; } } } // Shows how to get a string representation of a TPathName. void TFileSystemSnippets::PathToText() { try { TMultipleRootLocator locator("RuntimeEnv/Data/Samples/FileSystem"); TFile file = locator.FindFirstByName("README.FileSystemSnippets"); TPathName path(file); //- TStandardText pathString; TPathNameParser::GetDefaultParser().PathNameToHostSpecific(path, pathString); //- GetDisplay() << endl << pathString << endl; } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "README.FileSystemSnippets not found." << endl; } } } // This shows how to get the path of a file system entity. void TFileSystemSnippets::GetPathOfEntity() { try { TMultipleRootLocator locator("RuntimeEnv/Data/Samples/FileSystem"); TFile file = locator.FindFirstByName("README.FileSystemSnippets"); //- TPathName pathOfEntity(file); //- GetDisplay() << endl << PathToText(pathOfEntity) << endl; } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "README.FileSystemSnippets not found." << endl; } } } // Gets the name of an entity. void TFileSystemSnippets::GetNameOfEntity() { try { TMultipleRootLocator locator("RuntimeEnv/Data/Samples/FileSystem"); TFile file = locator.FindFirstByName("README.FileSystemSnippets"); //- TFileSystemEntityName name = file.GetName(); GetDisplay() << name << endl; //- } catch (const TFileSystemException& exception) { GetDisplay() << endl << "Caught file system exception while getting name." << endl; } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "README.FileSystemSnippets not found." << endl; } } } // This example iterates over every entity in a directory and prints its name. // It does not recursively traverse sub-directories. void TFileSystemSnippets::PrintDirectory() { try { TMultipleRootLocator locator("RuntimeEnv/Data/Samples"); TDirectory directory = locator.FindFirstByName("FileSystem"); TDirectoryIterator iterator(directory); for (TFileSystemEntity entity = iterator.First(); entity != TDirectoryIterator::kEndOfIteration; entity = iterator.Next()) { GetDisplay() << entity.GetName() << endl; } } catch (const TFileSystemException& exception) { GetDisplay() << endl << "Caught file system exception" << endl; } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "FileSystem directory not found." << endl; } } } // Shows how to create a directory. void TFileSystemSnippets::CreateDirectory() { try { TMultipleRootLocator locator("RuntimeEnv/Data/Samples"); TDirectory directory = locator.FindFirstByName("FileSystem"); //- TDirectory myDirectory = directory.CreateDirectory("MyDirectory"); //- PrintDirectory(directory); } catch (const TFileSystemEntityAlreadyExists& exception) { GetDisplay() << "MyDirectory already exists." << endl; } catch (const TFileSystemException& exception) { GetDisplay() << "Caught file system exception while creating directory" << endl; } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "FileSystem directory not found." << endl; } } } // Shows how to create a file. void TFileSystemSnippets::CreateFile() { try { TMultipleRootLocator locator("RuntimeEnv/Data/Samples"); TDirectory directory = locator.FindFirstByName("FileSystem"); //- directory.CreateFile("MyFile"); //- PrintDirectory(directory); } catch (const TFileSystemEntityAlreadyExists& exception) { GetDisplay() << "Couldn't create file. MyFile already exists." << endl; } catch (const TFileSystemException& exception) { GetDisplay() << "Caught exception while creating file." << endl; } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "FileSystem directory not found." << endl; } } } // Shows how to delete an entity. // Note: If the entity is a directory, the directory must have all of its contents // removed befory you can delete it. See DeleteDirectoryContents() to delete its contents. void TFileSystemSnippets::DeleteEntity() { try { TMultipleRootLocator locator("RuntimeEnv/Data/Samples/FileSystem"); TFile file = locator.FindFirstByName("MyFile"); //- file.DeleteSelf(); //- PrintDirectory("RuntimeEnv/Data/Samples", "FileSystem"); } catch (const TFileSystemEntityInUse& exception) { GetDisplay() << "Couldn't delete. MyFile is in use." << endl; } catch (const TFileSystemException& exception) { GetDisplay() << "Caught exception while deleting file." << endl; } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "MyFile not found. Run CreateFile() first." << endl; } } } // Shows how to delete the contents of a directory. If any of the contents are // not readable the myDirectory.DeleteAllContents() call will throw an exception. void TFileSystemSnippets::DeleteDirectoryContents() { try { TMultipleRootLocator locator("RuntimeEnv/Data/Samples/FileSystem/MyDirectory"); TDirectory myDirectory = locator.GetWritableDirectory(); myDirectory.CreateFile("InterestingFile"); myDirectory.CreateFile("NotSoInterestingFile"); myDirectory.CreateFile("FishClassifieds"); GetDisplay() << endl << "Directory with files" << endl; PrintDirectory(myDirectory); //- myDirectory.DeleteAllContents(); //- GetDisplay() << endl << endl << "Directory without files" << endl; PrintDirectory(myDirectory); myDirectory.DeleteSelf(); } catch (const TFileSystemException& exception) { GetDisplay() << "Caught file system exception." << endl; } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "MyDirectory not found. Run CreateDirectory() first." << endl; } } } // Shows how to write to a file using TFileStream. void TFileSystemSnippets::WriteToFile() { try { TMultipleRootLocator locator("RuntimeEnv/Data/Samples/FileSystem"); TFile file = locator.FindFirstByName("FileStreamTest"); TStandardText text("This string is, in fact, a string."); //- TFileStream fileStream(file); text >>= fileStream; //- } catch (const TFileSystemAccessDenied& exception) { GetDisplay() << "TFileStream constructor failed, couldn't get write access." << endl; } catch (const TFileSystemVolumeAccessError& exception) { GetDisplay() << "Couldn't stream-in, volume doesn't have enough space." << endl; } catch (const TFileSystemException& exception) { GetDisplay() << "Caught file system exception." << endl; } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "FileStreamTest not found." << endl; } } PrintTestFile(); } // Shows how to write to the end of a file using TFileStream. void TFileSystemSnippets::AppendToFile() { TStandardText text("This is the text appended to the file."); try { TMultipleRootLocator locator("RuntimeEnv/Data/Samples/FileSystem"); TFile file = locator.FindFirstByName("FileStreamTest"); //- TFileStream fileStream(file); fileStream.Seek(fileStream.GetLogicalEndOfStream()); text >>= fileStream; //- } catch (const TFileSystemAccessDenied& exception) { GetDisplay() << "Couldn't get write access on file." << endl; } catch (const TFileSystemVolumeAccessError& exception) { GetDisplay() << "Volume doesn't have enough space." << endl; } catch (const TFileSystemException& exception) { GetDisplay() << "Caught file system exception." << endl; } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "FileStreamTest not found." << endl; } } PrintTestFile(); } // Shows how to read from a TFileStream void TFileSystemSnippets::ReadFromFile() { TStandardText text; try { TMultipleRootLocator locator("RuntimeEnv/Data/Samples/FileSystem"); TFile file = locator.FindFirstByName("FileStreamTest"); //- TFileStream fileStream(file, MOpenFile::kRead); text <<= fileStream; //- GetDisplay() << endl << text << endl; } catch (const TFileSystemAccessDenied& exception) { GetDisplay() << "Couldn't get read access on file." << endl; } catch (const TFileSystemException& exception) { GetDisplay() << "Caught file system exception." << endl; } catch (const TStreamException& exception) { GetDisplay() << "Caught streaming exception. Probably end of file." << endl; GetDisplay() << "The odds are that you need to run WriteToFile() first." << endl; } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "FileStreamTest not found." << endl; } } } // Prints the contents of FileStreamTest to the display. void TFileSystemSnippets::PrintTestFile() { TStandardText text; try { TMultipleRootLocator locator("RuntimeEnv/Data/Samples/FileSystem"); TFile file = locator.FindFirstByName("FileStreamTest"); TFileStream fileStream(file, MOpenFile::kRead); try { while (fileStream.GetPosition() != fileStream.GetLogicalEndOfStream()) { text <<= fileStream; GetDisplay() << text << endl; } } catch (...) {} } catch (const TFileSystemAccessDenied& exception) { GetDisplay() << "Couldn't get read access on file." << endl; } catch (const TFileSystemException& exception) { GetDisplay() << endl << "Couldn't open file: FileStreamTest" << endl; } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "FileStreamTest not found." << endl; } } } // Opens a file and prints out the characters. void TFileSystemSnippets::ReadFromMappedFile() { try { TMultipleRootLocator locator("RuntimeEnv/Data/Samples/FileSystem"); TFile file = locator.FindFirstByName("README.FileSystemSnippets"); //- TMappedFile mappedFile(file, MOpenFile::kRead); unsigned long length = mappedFile.GetEndOfFile(); TMemorySurrogate memory; mappedFile.GetMemorySurrogate(memory); char* charPtr = (char*) memory.GetStartAddress(); //- GetDisplay() << TRepChars(charPtr) << endl; } catch (const TFileSystemAccessDenied& exception) { GetDisplay() << "Couldn't get read access on file." << endl; } catch (const TFileSystemMappedFileError& exception) { GetDisplay() << "Caught mapped file exception." << endl; GetDisplay() << "Probably ran out of swap space." << endl; } catch (const TFileSystemException& exception) { GetDisplay() << endl << "Couldn't read from mapped file." << endl; } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "README.FileSystemSnippets not found." << endl; } } } #if 0 // Maps a certain range of a file into memory. The range can be changed after the // TMappedFile has been created by calling TMappedFile::SetRange(). void TFileSystemSnippets::UsingAFileRange() { try { TMultipleRootLocator locator("RuntimeEnv/Data/Samples/FileSystem"); TFile file = locator.FindFirstByName("README.FileSystemSnippets"); const FileSystemEntitySize kBeginning = 10; const FileSystemEntitySize kEnd = 50; //- TFileRange range(kBeginning, kEnd); TMappedFile mappedFile(file, MOpenFile::kRead, MOpenFile::kAllowNone, range); TMemorySurrogate memory; mappedFile.GetMemorySurrogate(memory); char* charPtr = (char*) memory.GetStartAddress(); //- GetDisplay() << TRepChars(charPtr) << endl; } catch (const TFileSystemAccessDenied& exception) { GetDisplay() << "Couldn't get read access on file." << endl; } catch (const TFileSystemMappedFileError& exception) { GetDisplay() << "Caught mapped file exception." << endl; GetDisplay() << "Probably ran out of swap space." << endl; } catch (const TFileSystemException& exception) { GetDisplay() << endl << "Couldn't read from mapped file." << endl; } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "README.FileSystemSnippets not found." << endl; } } } // Writes a property to an entity. void TFileSystemSnippets::WritingAProperty() { try { TMultipleRootLocator locator("RuntimeEnv/Data/Samples/FileSystem"); TFile file = locator.FindFirstByName("FileStreamTest"); //- TPriorityProperty property(TPriorityProperty::kLowPriority); file.WriteProperty(property); //- GetDisplay() << "Run ReadingAProperty() to test for a successful write." << endl; } catch (const TFileSystemException& exception) { GetDisplay() << endl << "Couldn't write the property to the file." << endl; } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "FileStreamTest not found." << endl; } } } // Reads a property from an entity. void TFileSystemSnippets::ReadingAProperty() { try { TMultipleRootLocator locator("RuntimeEnv/Data/Samples/FileSystem"); TFile file = locator.FindFirstByName("FileStreamTest"); //- TPriorityProperty property; bool safeRead = file.ReadProperty(property); if (!safeRead) { GetDisplay() << "Couldn't read property. Run WritingAProperty() first." << endl; } else { if (property.GetValue() == TPriorityProperty::kLowPriority) { GetDisplay() << "kLowPriority" << endl; } else { GetDisplay() << "The wrong priority value was read." << endl; } } //- } catch (const TFileSystemException& exception) { GetDisplay() << endl << "Couldn't get the property of the file." << endl; } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "FileStreamTest not found." << endl; } } } #endif // Copies a file to another directory. void TFileSystemSnippets::CopyAnEntity() { try { TMultipleRootLocator locator("RuntimeEnv/Data/Samples/FileSystem"); TFile sourceFile = locator.FindFirstByName("FileStreamTest"); TMultipleRootLocator samplesLocator("RuntimeEnv/Data"); TDirectory destinationDirectory = samplesLocator.FindFirstByName("Samples"); TFile destinationFile; PrintDirectory(destinationDirectory); GetDisplay() << endl; //- try { TFileSystemCopier copier; destinationFile = copier.Copy(sourceFile, destinationDirectory); } catch (const TFileSystemException& exception) { GetDisplay() << "Couldn't copy file." << endl; } //- PrintDirectory(destinationDirectory); // Clean up try { destinationFile.DeleteSelf(); } catch (const TFileSystemException& exception) { GetDisplay() << "Couldn't delete copied file." << endl; } } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "Entity not found." << endl; } } } // Moves a directory and all of its contents to another directory. // If you want to move an entity to another volume, you have to copy it // to the other volume and then delete the original. void TFileSystemSnippets::MoveAnEntity() { try { TDirectory sourceDirectory; TMultipleRootLocator samplesLocator("RuntimeEnv/Data"); TDirectory destinationDirectory = samplesLocator.FindFirstByName("Samples"); TMultipleRootLocator locator("RuntimeEnv/Data/Samples"); TDirectory directory = locator.FindFirstByName("FileSystem"); sourceDirectory = directory.CreateDirectory("TestDirectory"); PrintDirectory(destinationDirectory); GetDisplay() << endl; //- try { TFileSystemMover mover; mover.Move(sourceDirectory, destinationDirectory); } catch (const TFileSystemAccessDenied& exception) { GetDisplay() << "Couldn't move TestDirectory. Access denied." << endl; } catch (const TFileSystemEntityAlreadyExists& exception) { GetDisplay() << "Couldn't move TestDirectory, it already exists." << endl; } //- PrintDirectory(destinationDirectory); } catch (const TFileSystemAccessDenied& exception) { GetDisplay() << "Couldn't create TestDirectory. Access denied." << endl; } catch (const TFileSystemEntityAlreadyExists& exception) { GetDisplay() << "Couldn't create TestDirectory, it already exists." << endl; } catch (const TMultipleRootLocatorException& exception) { const long entityNotFound = TMultipleRootLocatorException::kNamedEntityNotFound; if (exception.GetDescriptionIndex() == entityNotFound) { GetDisplay() << "Directory not found." << endl; } } // Clean up try { TMultipleRootLocator locator("RuntimeEnv/Data/Samples"); locator.FindFirstByName("TestDirectory").DeleteSelf(); } catch (...) { GetDisplay() << "Couldn't delete directory." << endl; } } //---------------------------- Utility functions ---------------------------------- TStandardText TFileSystemSnippets::PathToText(const TPathName& path) { TStandardText pathString; TPathNameParser::GetDefaultParser().PathNameToHostSpecific(path, pathString); return pathString; } void TFileSystemSnippets::PrintDirectory(const TDirectory& directory) { try { TDirectoryIterator iterator(directory); for (TFileSystemEntity entity = iterator.First(); entity != TDirectoryIterator::kEndOfIteration; entity = iterator.Next()) { GetDisplay() << entity.GetName() << endl; } } catch (const TFileSystemException& exception) { GetDisplay() << "Caught exception while printing directory" << endl; } } void TFileSystemSnippets::PrintDirectory(const THostSpecificPathName& parentDirectory, const TFileSystemEntityName& directoryName) { try { TMultipleRootLocator locator(parentDirectory); TDirectory directory = locator.FindFirstByName(directoryName); PrintDirectory(directory); } catch (const TFileSystemException& exception) { GetDisplay() << "Caught exception while getting entity from path." << endl; } }