File Services and I/O Devices
Input Output Devices
ClanLib includes a simple I/O device abstraction defined by the CL_IODevice class. The abstraction allows ClanLib and your application to use the same objects and functions for various different types of devices such as files, sockets, pipes and memory buffers.
You can implement your own I/O devices by implementing the CL_IODeviceProvider interface and passing the object to the CL_IODevice constructor. However, in most situations you simply use the standard I/O devices provided by ClanLib:
- CL_File
- CL_IODevice_Memory
- CL_PipeConnection
- CL_TCPConnection (clanNetwork)
Files
Reading or writing to files is pretty straightforward. Simply construct a CL_File object with the desired open mode, access flags and share flags and start calling functions on it:
CL_File file("filename.txt", CL_File::open_existing); CL_DataBuffer buffer(file.get_size()); file.read(buffer.get_data(), buffer.get_size()); file.close();
Additional support for other file related operations are supported by a number of different helper classes:
- CL_FileHelp
- CL_PathHelp
- CL_Directory
- CL_DirectoryScanner
CL_FileHelp contains static functions that support common functions on files, such as copying or deleting a file. CL_PathHelp is a helper class that assists in working with file paths. The CL_Directory class contains static functions operating on a directory and the CL_DirectoryScanner class assists in enumerating files located in a directory.
The functionality of these classes are all pretty straightforward so we will not spend much more time on those, except for a simple example for each:
CL_FileHelp::delete_file("foobar.txt"); CL_Console::write_line("The file extension is %1", CL_PathHelp::get_extension("foobar.txt")); CL_Console::write_line("Current directory is %1", CL_Directory::get_current()); CL_DirectoryScanner scanner; if (scanner.scan("/", "*")) { while (scanner.next()) { CL_Console::write_line(scanner.get_name()); } }
Pipes
ClanLib provides two classes for supporting pipes:
- CL_PipeListen
- CL_PipeConnection
CL_PipeListen is used to setup a named pipe that other processes can connect to. CL_PipeConnection is used for the actual communication and represents the pipe connection between two parties.
This design is similar to CL_TCPListen and CL_TCPConnection in the clanNetwork library.
Zip Archives
Zip file format support classes:
- CL_ZipArchive + CL_ZipEntry
- CL_ZipReader + CL_ZipWriter
Virtual File Systems (VFS)
File system abstraction classes:
- CL_VirtualFileSystem
- CL_VirtualFileSource
- CL_VirtualDirectory
- CL_VirtualDirectoryListing
- CL_VirtualDirectoryListingEntry