learning boost c++ libraries - sample chapter

48
 Community Experience Distilled Solve practical programming problems using powerful, portable, and expressive libraries from Boost Learning Boost C++ Libraries Arindam Mukherjee

Upload: packt-publishing

Post on 01-Nov-2015

21 views

Category:

Documents


0 download

DESCRIPTION

Chapter No. 9 Files, Directories, and IOStreamsSolve practical programming problems using powerful, portable, and expressive libraries from BoostFor more information : http://bit.ly/1SXtxs8

TRANSCRIPT

  • C o m m u n i t y E x p e r i e n c e D i s t i l l e d

    Solve practical programming problems using powerful, portable, and expressive libraries from Boost

    Learning Boost C++ Libraries

    Arindam

    Mukherjee

    Learning Boost C++ Libraries

    Filled with dozens of working code examples that illustrate the use of over 40 popular Boost libraries, this book takes you on a tour of Boost, helping you to independently build the libraries from source and use them in your own code.

    The fi rst half of the book focuses on basic programming interfaces including generic containers and algorithms, strings, resource management, exception safety, and a miscellany of programming utilities that make everyday programming chores easy. Following a short interlude that introduces template metaprogramming and functional programming, the later chapters are devoted to systems programming interfaces, focusing on directory handling, I/O, concurrency, and network programming

    Who this book is written for

    If you are a C++ programmer who has never used Boost libraries before, this book will get you up-to-speed with using them. Whether you are developing new C++ software or maintaining existing code written using Boost libraries, this hands-on introduction will help you decide on the right library and techniques to solve your practical programming problems.

    $ 49.99 US 31.99 UK

    Prices do not include local sales tax or VAT where applicable

    Arindam MukherjeeVisit www.PacktPub.com for books, eBooks,

    code, downloads, and PacktLib.

    What you will learn from this book

    Write effi cient and maintainable code using expressive interfaces from Boost libraries

    Leverage a variety of fl exible, practical, and highly effi cient containers and algorithms beyond STL

    Solve common programming problems by applying a wide array of utility libraries

    Design and write portable multithreaded code that is easy to read and maintain

    Craft highly scalable and effi cient TCP and UDP servers

    Build and deploy Boost libraries across a variety of popular platforms

    Use C++11 functionality and emulate C++11 language features in C++03 code

    Learning Boost C

    ++ Libraries"CommunityExperienceDistilled"

  • In this package, you will find: The author biography

    A preview chapter from the book, Chapter 9 'Files, Directories, and IOStreams'

    A synopsis of the books content

    More information on Learning Boost C++ Libraries

  • About the Author

    Arindam Mukherjee is a senior principal software engineer at Symantec, Pune, India, where he is involved in the research and development of business continuity solutions for enterprises. He has used C++ extensively for developing large-scale distributed systems. He was a speaker at Dr. Dobb's Journal India Conference 2014 and is the organizer of regular meets for the Pune C++ and Boost Meetup. He believes that writing books and articles, speaking for interest groups, and engaging with the programming community are the best ways to develop a critical understanding of technology. He is also an amateur musician, dabbles in food photography, and loves profound discussions with his 4-year-old daughter, especially about dinosaurs and their diets.

  • PrefaceBoost is not just a collection of useful, portable, generic C++ libraries. It is an important incubator for ideas and concepts that make their way to the ISO C++ Standard itself. If you are involved in the development of software written in C++, then learning to use the Boost libraries would save you from reinventing the wheel, improve the quality of your software, and very likely push up your productivity.

    I fi rst came across the Boost libraries a decade ago, while looking for a portable C++ regular expressions library. Over the next couple of days, porting Perl and Korn Shell text-processing code to C++ became a breeze, and I took an instant liking to Boost. In using many more Boost libraries to write software since then, I often found myself digging deep into the documentation, or asking questions on the mailing list and online forums to understand library semantics and nuances. As effective as that was, I always sorely missed a book that would get me started on the most useful Boost libraries and help me become productive faster. This is that book.

    Boost has a wide array of libraries for solving various kinds of programming tasks. This book is a tutorial introduction to a selection of over of the most useful libraries from Boost to solve programming problems effectively. The chosen libraries represent the breadth of cross-cutting concerns from software development, including data structures and algorithms, text processing, memory management, exception safety, date and time calculations, fi le and directory management, concurrency, and fi le and network I/O, among others. You will learn about each library by understanding the kind of problems it helps solve, learning the fundamental concepts associated with it, and looking at a series of code examples to understand how the library is used. Libraries introduced earlier in this book are freely used in later examples, exposing you to the frequent synergies that occur in practice between the Boost libraries.

  • Preface

    As a collection of peer-reviewed, open source libraries, Boost draws heavily from community expertise. I fi rmly believe that this book will give you a strong practical foundation in using the Boost libraries. This foundation will refl ect in the quality of the software you write, and also give you the leverage to engage with the Boost community and make valuable contributions to it.

    What this book coversChapter 1, Introducing Boost, discusses how to set up a development environment to use the Boost libraries. We cover different ways of obtaining Boost library binary packages, building them from source for different confi gurations, and using them in a development environment.

    Chapter 2, The First Brush with Boost's Utilities, explores a handful of Boost libraries for common programming tasks that include dealing with variant data types, handling command-line arguments, and detecting the confi guration parameters of the development environment.

    Chapter 3, Memory Management and Exception Safety, explains what is meant by exception safety, and shows how to write exception-safe code using the different smart pointer types provided by Boost and C++11.

    Chapter 4, Working with Strings, explores the Boost String Algorithms library for performing various computations with character strings, the Boost Range library for elegantly defi ning subsequences, the Boost Tokenizer library to split strings into tokens using different strategies, and the Boost Regex library to search for complex patterns in text.

    Chapter 5, Effective Data Structures beyond STL, deals with the Boost Container library focusing on containers not available in the C++ Standard Library. We see the Pointer Container library for storing dynamically-allocated objects in action, and use the Boost Iterator library to generate various value sequences from underlying containers.

    Chapter 6, Bimap and Multi-index Containers, looks at bidirectional maps and multi-index containerstwo nifty container templates from Boost.

    Chapter 7, Higher Order and Compile-time Programming, delves into compile-time programming using Boost Type Traits and Template Metaprogramming libraries. We take a fi rst look at Domain Specifi c Embedded Languages and use Boost Phoenix to build basic expression templates. We use Boost Spirit to build simple parsers using the Spirit Qi DSEL.

  • Preface

    Chapter 8, Date and Time Libraries, introduces the Boost Date Time and Boost Chrono libraries to represent dates, time points, intervals, and periods.

    Chapter 9, Files, Directories, and IOStreams, features the Boost Filesystem library for manipulating fi lesystem entries, and the Boost IOStreams library for performing type-safe I/O with rich semantics.

    Chapter 10, Concurrency with Boost, uses the Boost Thread library and Boost Coroutine library to write concurrent logic, and shows various synchronization techniques in action.

    Chapter 11, Network Programming Using Boost Asio, shows techniques for writing scalable TCP and UDP servers and clients using the Asio library.

    Appendix, C++11 Language Features Emulation, summarizes C++11 move semantics and Boost's emulation of several C++11 features in C++03.

  • Chapter 9

    [ 335 ]

    Files, Directories, and IOStreams

    Programming for real-world systems requires interacting with various subsystems of the operating system to utilize their services. Starting with this chapter, we look at the various Boost libraries that provide programmatic access to OS subsystems.

    In this chapter, we look at the Boost libraries for performing input and output, and interacting with fi lesystems. We cover these libraries in the following sections of the chapter:

    Managing fi les and directories with Boost Filesystem Extensible I/O with Boost IOStreams

    Using the libraries and techniques covered in this chapter, you will be able to write portable C++ programs that interact with fi lesystems and perform all kinds of I/O using a standard interface. We do not cover network I/O in this chapter, but devote Chapter 10, Concurrency with Boost, to this topic.

  • Files, Directories, and IOStreams

    [ 336 ]

    Managing fi les and directories with Boost FilesystemSoftware written using the Boost libraries runs on multiple operating systems, including Linux, Microsoft Windows, Mac OS, and various other BSD variants. How these operating systems access paths to fi les and directories may differ in several ways; for example, MS Windows uses backward slashes as the directory separator while all Unix variants, including Linux, BSD, and Mac, use forward slashes. Non-English operating systems may use other characters as directory separators, and sometimes, multiple directory separators may be supported. The Boost Filesystem library hides these platform-specifi c peculiarities and lets you write code that is much more portable. Using the functions and types in the Boost Filesystem library, you can write OS-agnostic code to perform common operations on the fi lesystem that an application needs to run, like copying, renaming, and deleting fi les, traversing directories, creating directories and links, and so on.

    Manipulating pathsFilesystem paths are represented using objects of type boost::filesystem::path. Given an object of type boost::filesystem::path, we can glean useful information from it and derive other path objects from it. A path object allows us to model a real fi lesystem path and derive information from it, but it need not represent a path that really exists in the system.

    Printing pathsLet us look at our fi rst example of using Boost Filesystem to print the current working directory of a process:

    Listing 9.1: The fi rst example of using Boost Filesyst em

    1 #include 2 #include 3 4 namespace fs = boost::filesystem; 5 6 int main() { 7 // Get the current working directory 8 fs::path cwd = fs::current_path(); 910 // Print the path to stdout

  • Chapter 9

    [ 337 ]

    11 std::cout

  • Files, Directories, and IOStreams

    [ 338 ]

    We print each directory component in the path, iterating through them using a range-based for-loop in C++11 (line 15). If range-based for-loop is not available, we should use the begin and end member functions in path to iterate through path elements. On my Windows box, this program prints the following:

    generic: E:/DATA/Packt/Boost/Draft/Book/Chapter07/examplesnative:E:\DATA\Packt\Boost\Draft\Book\Chapter07\examplesquoted: "E:\DATA\Packt\Boost\Draft\Book\Chapter07\examples"Components:[E:][/][DATA][Packt] [Boost][Draft][Book][Chapter07][examples]

    On my Ubuntu box, this is the output I get:

    generic: /home/amukher1/devel/c++/book/ch07native: /home/amukher1/devel/c++/book/ch07quoted: "/home/amukher1/devel/c++/book/ch07"Components:[/][home][amukher1] [devel][c++][book][ch07]

    The program prints its current working directory in the generic and native formats. You can see that there is no difference between the two on Ubuntu (and generally on any Unix).

    On Windows, the fi rst component of the path is the drive letter, generally referred to as the root name. This is followed by / (the root folder) and each subdirectory in the path. On Unix, there is no root name (as is usually the case), so the listing starts with / (the root directory) followed by each subdirectory in the path.

    The cwd object of type path is streamable (line 19) and printing it to standard output prints it in the native format, enclosed in quotes.

  • Chapter 9

    [ 339 ]

    Compiling and linking examples with Boost FilesystemBoost Filesystem is not a header-only library. The Boost Filesystem shared libraries are installed as part of the Boost operating system packages, or built from source as described in Chapter 1, Introducing Boost.On LinuxIf you installed Boost libraries using your native package manager, then you can use the following commands to build your programs. Note that the library names are in system layout.$ g++ .c -o -lboost_filesystem -lboost_system

    If you built Boost from source as shown in Chapter 1, Introducing Boost, and installed it under /opt/boost, you can use the following commands to compile and link your sources:

    $ g++ .cpp -c -I/opt/boost/include

    $ g++ .o -o -L/opt/boost/lib -lboost_filesystem-mt -lboost_system-mt -Wl,-rpath,/opt/boost/lib

    Since we built the libraries with names in tagged layout, we link against appropriately named versions of Boost Filesystem and Boost System. The -Wl,-rpath,/opt/boost/lib part embeds the path to the Boost shared libraries in the generated executable so that the runtime linker knows from where to pick the shared libraries for the executable to run.On WindowsOn Windows, under Visual Studio 2012 or later, you can enable auto-linking and need not explicitly specify the libraries to link. For this, you need to edit the Confi guration Properties settings in the Project Properties dialog box (brought up using Alt + F7 in the IDE):1. Under VC++ Directories, append \include to the Include Directories property.2. Under VC++ Directories, append \lib to the Library Directories property.3. Under Debugging, set the Environment property to PATH=%PATH%;\lib.4. Under C/C++ > Preprocessor, defi ne the following preprocessor symbols:BOOST_ALL_DYN_LINK

    BOOST_AUTO_LINK_TAGGED (only if you built using tagged layout)5. Build by hitting F7 from the Visual Studio IDE and run your program by hitting Ctrl + F5 from the IDE.

  • Files, Directories, and IOStreams

    [ 340 ]

    Constructing pathsYou can construct instances of boost::filesystem::path using one of the path constructors or by combining existing paths in some way. Strings and string literals are implicitly convertible to path objects. You can construct relative as well as absolute paths, convert relative paths to absolute paths, append or strip elements from the path and "normalize" paths, as shown in listing 9.2:

    Listing 9.2a: Constructing empty path ob jects

    1 #define BOOST_FILESYSTEM_NO_DEPRECATED 2 #include 3 #include 4 #include < cassert> 5 namespace fs = boost::filesystem; 6 7 int main() { 8 fs::path p1; // empty path 9 assert(p1.empty()); // does not fire10 p1 = "/opt/boost"; // assign an absolute path11 assert(!p1.empty());12 p1.clear();13 assert(p1.empty());14 }

    A default constructed path object represents an empty path, as illustrated by the preceding example. You can assign a path string to an empty path object (line 10) and it ceases to be empty (line 11). On calling the clear member function on the path (line 12), it once again turns empty (line 13). Over the years, some parts of the Boost Filesystem library have been deprecated and replaced by better alternatives. We defi ne the macro BOOST_FILESYSTEM_NO_DEPRECATED (line 1) to ensure that such deprecated member functions and types are not accessible.

    Listing 9.2b: Constructing relative paths

    15 void make_relative_paths() {16 fs::path p2(".."); // relative path17 p2 /= "..";18 std::cout

  • Chapter 9

    [ 341 ]

    25 std::cout

  • Files, Directories, and IOStreams

    [ 342 ]

    39 std::cout

  • Chapter 9

    [ 343 ]

    In the preceding example, .hpp is the extension (including the period or dot) and path is the stem of the fi lename. In case of a fi lename with multiple embedded dots (for example, libboost_filesystem-mt.so.1.56.0), the extension is considered to start from the last (right-most) dot.

    Now consider the following Windows path:

    E:\DATA\boost\include\boost\filesystem\path.hpp

    The component E: is called the root name. The leading backslash following E: is called the root directory. The concatenation of the root name with the root directory (E:\) is called the root path. The following is a short function that prints these different components of a path using member functions of boost::filesystem::path:

    Listing 9.3: Splitting a path into components

    1 #include 2 #include 3 #include 4 namespace fs = boost::filesystem; 5 6 void printPathParts(const fs::path& p1) 7 { 8 std::cout

  • Files, Directories, and IOStreams

    [ 344 ]

    2829 if (p1.has_parent_path())30 std::cout

  • Chapter 9

    [ 345 ]

    We call printPathParts with different relative and absolute paths. The results may vary across operating systems. For example, on Windows, a call to has_root_name (line 17) returns false for all the paths except the Windows path E:\DATA\books.txt (line 54), which is considered an absolute path. Calling root_name on this path returns E:. On UNIX however, the backslashes are not recognized as separators and considered part of the path components, so E:\DATA\books.txt will be interpreted as a relative path with the fi lename E:\DATA\books.txt, the stem E:\DATA\books, and the extension .txt. This, coupled with the fact that forward slashes are recognized on Windows as path separators, is a good reason to never use backslashes in path literals like we have done here.

    For maximum portability, always use forward slashes in path literals or generate paths using the overloaded operator/ and operator/=.

    We can also compare two paths to see whether they are equal and equivalent. Two paths can be compared for equality using the overloaded operator==, which returns true only if the two paths are decomposable to the same components. Note that this means the paths /opt and /opt/ are not equal; in the former, the fi lename component is opt, while in the latter, it is . (dot). Two paths that are not equal can still be equivalent if they represent the same underlying fi lesystem entry. For example, /opt/boost and /opt/cmake/../boost/ are equivalent although they are not equal paths. To compute equivalence, we can use the boost::filesystem::equivalent function, which returns true if the two paths refer to the same entry in the fi lesystem:

    boost::filesystem::path p1("/opt/boost"), p2("/opt/cmake");if (boost::filesystem::equivalent(p1, p2 / ".." / "boost") { std::cout

  • Files, Directories, and IOStreams

    [ 346 ]

    This will print the components separated by a pair of spaces:

    / optboost include boost thread.hpp

    The begin and end member functions of boost::filesystem::path return a random-access iterator of type boost::filesystem::path::iterator, which you can use with Standard Library algorithms in interesting ways. For example, to fi nd the number of components in a path, you can use:

    size_t count = std::distance(p1.begin(), p1.end());

    Now, consider two paths: /opt/boost/include/boost/filesystem/path.hpp and /opt/boost/include/boost/thread/detail/thread.hpp. We will now write a function that computes the common subdirectory under which both paths are located:

    Listing 9.4: Finding the common prefi x path

    1 #include 2 #include 3 namespace fs = boost::filesystem; 4 5 fs::path commonPrefix(const fs::path& first, 6 const fs::path& second) { 7 auto prefix = 8 [](const fs::path& p1, const fs::path& p2) { 9 auto result =10 std::mismatch(p1.begin(), p1.end(), p2.begin());11 fs::path ret;12 std::for_each(p2.begin(), result.second,13 [&ret](const fs::path& p) {14 ret /= p;15 });16 return ret;17 };1819 size_t n1 = std::distance(first.begin(), first.end());20 size_t n2 = std::distance(second.begin(), second.end());21 22 return (n1 < n2) ? prefix(first, second)23 : prefix(second, first);24 }

  • Chapter 9

    [ 347 ]

    Calling the commonPrefix function on the two paths correctly returns /opt/boost/include/boost. For this function to work correctly, we should pass paths that do not have . or .. components, something that a more complete implementation can take care of. To compute the prefi x, we fi rst defi ne a nested function called prefix using a lambda expression (lines 7-17), which performs the actual computation. We compute the element count of the two paths (lines 19, 20) and pass the shorter path as the fi rst argument and the longer one as the second argument to the prefix function (lines 22-23). In the prefix function, we use the std::mismatch algorithm on the two paths to compute the fi rst component where they do not match (line 10). We then construct the common prefi x as the path up to this fi rst mismatch and return it (lines 12-15).

    Traversing directoriesBoost Filesystem provides two iterator classes, directory_iterator and recursive_directory_iterator, that make iterating through directories fairly simple. Both conform to the input iterator concept and provide an operator++ for forward traversal. In the fi rst example here, we see directory_iterator in action:

    Listing 9.5: Iterating directories

    1 #include 2 #include 3 #include 4 namespace fs = boost::filesystem; 5 6 void traverse(const fs::path& dirpath) { 7 if (!exists(dirpath) || !is_directory(dirpath)) { 8 return; 9 }1011 fs::directory_iterator dirit(dirpath), end;1213 std::for_each(dirit, end, [](const fs::directory_entry& entry) {14 std::cout

  • Files, Directories, and IOStreams

    [ 348 ]

    The traverse function takes a parameter dirpath of type boost::filesystem::path representing the directory to traverse. Using the namespace level functions, exists and is_directory (line 7), the function checks to see that dirpath actually exists and is a directory before proceeding.

    To perform the iteration, we create an instance dirit of boost::filesystem::directory_iterator for the path and a second default-constructed directory_iterator instance called end (line 11). The default-constructed directory_iterator acts as the end-of-sequence marker. Dereferencing a valid iterator of type directory_iterator returns an object of type boost::filesystem::directory_entry. The sequence represented by the iterator range [dirit, end) is the list of entries in the directory. To iterate through them, we use the familiar std::for_each standard algorithm. We use a lambda to defi ne the action to perform on each entry, which is to simply print it to the standard output (lines 13-14).

    While we can write recursive logic around boost::directory_iterator to iterate through a directory tree recursively, boost::recursive_directory_iterator provides an easier alternative. We can replace boost::directory_iterator with boost::recursive_directory_iterator in listing 9.5 and it will still work, performing a depth-fi rst traversal of the directory tree. But the recursive_directory_iterator interface provides additional capabilities like skipping descent into specifi c directories and keeping track of the depth of descent. A hand-written loop serves better to fully leverage these capabilities, as shown in the following example:

    Listing 9.6: Recursively iterating directories

    1 void traverseRecursive(const fs::path& path) 2 { 3 if (!exists(path) || !is_directory(path)) { 4 return; 5 } 6 7 try { 8 fs::recursive_directory_iterator it(path), end; 910 while (it != end) {11 printFileProperties(*it, it.level());1213 if (!is_symlink(it->path())14 && is_directory(it->path())15 && it->path().filename() == "foo") {16 it.no_push();

  • Chapter 9

    [ 349 ]

    17 }18 boost::system::error_code ec;19 it.increment(ec);21 if (ec) {22 std::cerr

  • Files, Directories, and IOStreams

    [ 350 ]

    We process each entry by a call to a fi ctitious function printFileProperties (line 11), which takes two argumentsthe result of dereferencing the recursive_directory_iterator instance, and the depth of traversal obtained by a call to the level member function of the iterator. The level function returns zero for fi rst-level directories and its return value is incremented by 1 for each additional level of descent. The printFileProperties function can use this to indent entries in subdirectories, for example. We will implement the printFileProperties function in the next section.

    To add dimension to the example, we decide not to descend into directories named foo. For this, we check for directories named foo (lines 13-15) and call the no_push member function on the recursive_directory_iterator to prevent descending into the directory (line 16). Likewise, we can call the pop member function on the iterator at any time to go up a level in the directory tree without necessarily completing iteration at the current level.

    On systems that support symbolic links, if the recursive_directory_iterator encounters a symbolic link pointing to a directory, it does not follow the link to descend into the directory. If we want to override this behavior, we should pass a second argument of the enum type boost::filesystem::symlink_option to the recursive_directory_iterator constructor. The symlink_option enum provides the values none (or no_recurse), which is the default, and recurse, which indicates that symbolic links should be followed to descend into directories.

    Querying fi lesystem entriesBoost Filesystem provides a set of functions to perform useful operations on fi les and directories. Most of these are functions in the boost::filesystem namespace. Using these functions, we can check whether a fi le exists, its size in bytes, its last modifi cation time, the fi le type, whether it is empty, and so on. We use this slew of functions to write the printFileProperties function we used in the preceding section:

    Listing 9.7: Querying fi le system entries

    1 #include 2 #include 3 #include 4 namespace fs = boost::filesystem; 5 namespace pxtm = boost::posix_time; 6 7 void printFileProperties(const fs::directory_entry& entry, 8 int indent = 0) { 9 const fs::path& path= entry.path();10 fs::file_status stat = entry.symlink_status();

  • Chapter 9

    [ 351 ]

    11 std::cout

  • Files, Directories, and IOStreams

    [ 352 ]

    The printFileProperties is used to print a short summary for a given fi le, including attributes like type, size, last modifi cation time, name, and for symbolic links, the target fi le. The fi rst argument to this function is of type directory_entry, the result of dereferencing a directory_iterator or recursive_directory_iterator. The second argument is the depth of traversal. We obtain the path to the fi le referenced by the directory_entry object by calling the path member function of directory_entry (line 9). We obtain a reference to a file_status object by calling the symlink_status member function of directory_entry (line 10). The file_status object contains additional details about a fi lesystem entry, which we use in our example to print the status of special fi les. The symlink_status function acts on all kinds of fi les not just symbolic links, but it returns the status of the symbolic link itself without following it to the target. If you need the status of the target each time you query the symbolic link, use the status member function instead of symlink_status. The status and symlink_status member functions are faster than the global functions of the same name because they keep the fi le stats cached instead of querying the fi lesystem on every call.

    We determine the type of each entry before printing information appropriate for the type. To do this, we use the convenience functions is_symlink, is_regular_file and is_directory (lines 14, 19, 24). On POSIX systems like Linux, there are other kinds of fi les like block and character devices, fi fos, and Unix domain sockets. To identify such fi les, we use the file_status object we obtained earlier (line 10). We call the type member function on the file_status object to determine the exact type of special fi le (line 29). Note that we fi rst check if the fi le is a symbolic link and then perform other tests. That is because is_regular_file or is_directory may also return true for a symbolic link, based on the type of the target fi le.

    This function prints each entry in the following format:

    file_type sizetime name -> target

    The fi le type is indicated by a single letter (D: directory, F: regular fi le, L: symbolic link, C: character device, B: block device, P: fi fo, S: Unix domain socket). The size is printed in bytes, the last modifi cation time is printed as a long integer, and the fi le name is printed without the full path. Only for symbolic links, a trailing arrow followed by the target path is appended after the name. Hyphens (-) appear for missing fi elds when fi le size or last write time are not available. For each level of descent, the entry is indented with an extra pair of spaces (line 11).

  • Chapter 9

    [ 353 ]

    Here is a sample output from running this function on my Linux system:

    You can also run this on the /dev directory on Linux to look at how device fi les are listed.

    To get the target fi le pointed to by a symbolic link, we call the read_symlink function (line 15). To get the size of a fi le in bytes, we call the file_size function (line 21), and to get the last modifi cation time of a fi le, we call the last_write_time function (lines 22, 26, and 46). The last_write_time function returns the Unix time at which the fi le was last modifi ed. We print a meaningful representation of this time stamp by calling the boost::posix_time::from_time_t function to convert this numeric timestamp into a printable date time string (see Chapter 7, Higher Order and Compile-time Programming).

    In order to build this program, you must additionally link against the Boost DateTime library, as shown here:

    $ g++ listing8_7.cpp -o listing8_7 -std=c++11 -lboost_filesystem -lboost_date_time

    There are several such functions for querying objects in the fi lesystem for different kinds of informationfor example, fi nding the number of hard links to a fi le. We can query the file_status object (line 10) for fi le permissions. Notice that we do not qualify these namespace level functions with the namespace; they are correctly resolved using Argument Dependent Lookup based on the type of their arguments (boost::filesystem::path).

  • Files, Directories, and IOStreams

    [ 354 ]

    Performing operations on fi lesIn addition to querying fi lesystem entries for information, we can also use the Boost Filesystem library to perform operations on fi les like creating directories and links, copying fi les and moving them, and so on.

    Creating directoriesIt is easy to create directories using the function boost::filesystem::create_directory. You pass it a path and it creates a directory at that path if one does not exist; it does nothing if the directory already exists. If the path exists but is not a directory, create_directory throws an exception. There is also a non-throwing version that takes a boost::system::error_code reference, which it sets on error. These functions returns true if they create the directory and false if they do not:

    Listing 9.8: Creatin g directories

    1 #include 2 #include 3 #include 4 namespace fs = boost::filesystem; 5 6 int main() { 7 fs::path p1 = "notpresent/dirtest"; 8 boost::system::error_code ec; 9 if (!is_directory(p1.parent_path()) || exists(p1)) {10 assert( !create_directory(p1, ec) );1112 if (is_directory(p1)) assert(!ec.value());13 else assert(ec.value());14 }1516 try {17 if (create_directories(p1)) {18 assert( !create_directory(p1) );19 }20 } catch (std::exception& e) {21 std::cout

  • Chapter 9

    [ 355 ]

    In this example, calling create_directory on the path notpresent/dirtest relative to the current directory fails (line 10) either if there is no directory called notpresent already in your current directory, or if notpresent/dirtest exists. This is because create_directory expects the parent directory of the path passed to exist, and it does not create a path that already exists. If we did not pass the error code parameter, this call to create_directory would have thrown an exception that would need to be handled. If notpresent/dirtest already exists and is a directory, then create_directory fails, but does not set the error code (line 12).

    The function boost::filesystem::create_directories creates all path components needed, akin to mkdir p on Unix systems. The call to it (line 17) succeeds unless there are permission issues or the path already exists. It creates the directory, including any missing directories along the path. Calls to create_directory and create_directories are idempotent; if the target directory exists, no error is returned or exception thrown, but the functions return false because no new directory was created.

    Creating symbolic linksSymbolic links, sometimes called soft links, are entries in the fi lesystem that act like aliases to other fi les. They can refer to fi les as well as directories and are often used to provide alternate, simplifi ed names and paths for fi les and directories. Symbolic links have been around on UNIX systems for quite a while now and have been available in some form on Windows since Windows 2000. We can use the function boost::filesystem::create_symlink to create symbolic links. For creating symbolic links to directories, the function boost::filesystem::create_directory_symlink is recommended for better portability.

    Listing 9.9: Creating symbolic links

    1 #include 2 namespace fs = boost::filesystem; 3 4 void makeSymLink(const fs::path& target, const fs::path& link) { 5 boost::system::error_code ec; 6 7 if (is_directory(target)) { 8 create_directory_symlink(target, link); 9 } else {10 create_symlink(target, link);11 }12 }

  • Files, Directories, and IOStreams

    [ 356 ]

    This shows a function makeSymLink that creates a symbolic link to a given path. The fi rst parameter to the function is the target path that the link must alias, and the second parameter is the path to the link itself. This order of arguments is reminiscent of the UNIX ln command. If the target is a directory, this function calls create_directory_symlink (line 8), while for all other cases it calls create_symlink (line 10). Note that the target path need not exist at the time of creation of the symbolic link and a dangling symbolic link will be created in such a case. Calling these functions has the same effect as the command ln s target link on POSIX systems. On Windows, you get the same effect by running the command mklink /D link target when target is a directory, or by running the command mklink link target when target is not a directory. The function makeSymLink will throw if create_directory_symlink or create_symlink threw an exception.

    Copying fi lesCopying fi les is another common chore that Boost Filesystem helps in. The boost::filesystem::copy_file function copies regular fi les from source to destination and fails if the fi le already exists at the destination. Using an appropriate override, it can be made to overwrite the fi le at the destination instead. The boost::filesystem::copy_symlink takes a source symbolic link and creates a second symbolic link at the destination that aliases the same fi le as the source. You cannot pass a directory as the destination to either function. There is also a boost::copy_directory function, which does not seem to do what its name suggests. It creates directories and copies attributes of the source directory to the target directory. So, we will roll out our own recursive directory-copying utility function instead:

    Listing 9.10: Recursively copying directories

    1 void copyDirectory(const fs::path& src, const fs::path& target) { 2 if (!is_directory(src) 3 || (exists(target) && !is_directory(target)) 4 || !is_directory(absolute(target).parent_path()) 5 || commonPrefix(src, target) == src) { 6 throw std::runtime_error("Preconditions not satisfied"); 7 } 8 9 boost::system::error_code ec;10 fs::path effectiveTarget = target;11 if (exists(target)) {12 effectiveTarget /= src.filename();13 }14 create_directory(effectiveTarget);

  • Chapter 9

    [ 357 ]

    1516 fs::directory_iterator iter(src), end;17 while (iter != end) {18 auto status = iter->symlink_status();19 auto currentTarget = effectiveTarget/20 iter->path().filename();2122 if (status.type() == fs::regular_file) {23 copy_file(*iter, currentTarget,24 fs::copy_option::overwrite_if_exists);25 } else if (status.type() == fs::symlink_file) {26 copy_symlink(*iter, currentTarget);27 } else if (status.type() == fs::directory_file) {28 copyDirectory(*iter, effectiveTarget);29 } // else do nothing30 ++iter;31 }32 }

    Listing 9.10 defi nes the copyDirectory function, which recursively copies a source directory to a target directory. It performs basic validations and throws an exception if the requisite initial conditions are not met (line 6). If any of the following conditions hold true, then a necessary precondition is violated:

    1. The source path is not a directory (line 2)2. The target path exists, but is not a directory (line 3)3. The parent of the target path is not a directory (line 4)4. The target path is a subdirectory of the source path (line 5)

    To detect violation 4, we reuse the commonPrefix function we defi ned in listing 9.4. If the target path already exists, a subdirectory with the same name as the source directory is created under it to hold the copied contents (lines 11-12, 14). Otherwise, the target directory is created and the content is copied into it.

    Beyond this, we iterate recursively through the source directory using directory_iterator instead of recursive_directory_iterator (line 17). We use copy_file to copy regular fi les, passing the copy_option::overwrite_if_exists option to make sure a destination fi le that already exists is overwritten (lines 23-24). We use copy_symlink to copy a symbolic link (line 26). Each time we encounter a subdirectory, we recursively call copyDirectory (line 28). If an exception is thrown from the Boost Filesystem functions called by copyDirectory, it terminates the copy.

  • Files, Directories, and IOStreams

    [ 358 ]

    Moving and deleting fi lesYou can move or rename fi les and directories using the boost::filesystem::rename function, which takes the old and new paths as arguments. The two-argument overload throws an exception if it fails, while the three-argument overload sets an error code:

    void rename(const path& old_path, const path& new_path);void rename(const path& old_path, const path& new_path, error_code& ec);

    If new_path does not exist, it is created provided its parent directory exists; otherwise, the call to rename fails. If old_path is not a directory, then new_path, if it exists, cannot be a directory either. If old_path is a directory, then new_path, if it exists, must be an empty directory or the function fails. When a directory is moved to another empty directory, the contents of the source directory are copied inside the target empty directory, and then the source directory is removed. Renaming symbolic links acts on the links, not on the fi les they refer to.

    You can delete fi les and empty directories by calling boost::filesystem::remove passing it the path to the fi lesystem entry. To recursively remove a directory that is not empty, you must call boost::filesystem::remove_all.

    bool remove(const path& p);bool remove(const path& p, error_code& ec);uintmax_t remove_all(const path& p);uintmax_t remove_all(const path& p, error_code& ec);

    The remove function returns false if the fi le named by the path does not exist. This removes symbolic links without impacting the fi les they alias. The remove_all function returns the total number of entries it removes. On error, the single-argument overloads of remove and remove_all throw an exception, while the two-argument overloads set the error code reference passed to it without throwing an exception.

    Path-aware fstreamsIn addition, the header fi le boost/filesystem/fstream.hpp provides versions of Standard fi le stream classes that work with boost::filesystem::path objects. These are very handy when you are writing code that uses boost::filesystem and also needs to read and write fi les.

    A C++ Technical Specifi cation based on the Boost Filesystem library has been recently approved by ISO. This makes way for its inclusion in a future revision of the C++ Standard Library.

  • Chapter 9

    [ 359 ]

    Extensible I/O with Boost IOStreamsThe Standard Library IOStreams facility is meant to provide a framework for operations of all kinds on all manner of devices, but it has not proven to be the easiest of frameworks to extend. The Boost IOStreams library supplements this framework with a simpler interface for extending I/O facilities to newer devices, and provides some pretty useful classes that address common needs while reading and writing data.

    Architecture of Boost IOStreamsThe Standard Library IOStreams framework provides two basic abstractions, streams and stream buffers. Streams provide a uniform interface to the application for reading or writing a sequence of characters on an underlying device. Stream buffers provide a lower-level abstraction for the actual device, which is leveraged and further abstracted by streams.

    The Boost IOStreams framework provides the boost::iostreams::stream and boost::iostreams::stream_buffer templates, which are generic implementations of the stream and stream buffer abstractions. These two templates implement their functionality in terms of a further set of concepts, which are described as follows:

    A source is an abstraction for an object from which a sequence of characters can be read.

    A sink is an abstraction for an object to which a sequence of characters can be written.

    A device is a source, a sink, or both. An input fi lter modifi es a sequence of characters read from a source,

    while an output fi lter modifi es a sequence of characters before it is written to a sink.

    A fi lter is an input fi lter or an output fi lter. It is possible to write a fi lter that can be used either as an input fi lter or as an output fi lter; this is known as a dual use fi lter.

    To perform I/O on a device, we associate a sequence of zero or more fi lters plus the device with an instance of boost::iostreams::stream or an instance of boost::iostreams::stream_buffer. A sequence of fi lters is called a chain and a sequence of fi lters with a device at the end is said to be a complete chain.

  • Files, Directories, and IOStreams

    [ 360 ]

    The following diagram is a unifi ed view of input and output operation, illustrating the I/O path between a stream object and the underlying device:

    stream

    device

    chainfilter1

    filter2

    filter3

    filter4

    filter5

    stream_buffer

    Use

    Use

    The Boost IOStreams architecture

    Input is read from the device and passed through an optional stack of fi lters to reach the stream buffer from where it is accessible via the stream. Output is written from the stream via the stream buffer and passed through a stack of fi lters before reaching the device. The fi lters, if any, act on the data read from the device to present a transformed sequence to the reader of the stream. They also act on the data to be written to the device and transform it before it is written. The preceding diagram is meant for visualizing these interactions but is slightly inaccurate; in code, a fi lter cannot act both as an input fi lter and an output fi lter at the same time.

    The Boost IOStreams library comes with several built-in device and fi lter classes, and it is easy to create our own too. In the following sections, we illustrate the use of different components of the Boost IOStreams library with code examples.

  • Chapter 9

    [ 361 ]

    Using devicesA device provides an interface to read and write characters to an underlying medium. It abstracts a real medium like a disk, memory, or network connection. In this book, we will focus on using the number of readily available devices shipped as part of the Boost IOStreams library. Methods of writing our own device classes are beyond the scope of this book, but you should have little diffi culty in picking them up from the online documentation once you are familiar with the content we cover in this chapter.

    Devices for fi le I/OBoost defi nes a number of devices for performing I/O on fi les and the one we look at fi rst is a device that abstracts platform-specifi c fi le descriptors. Each platform uses some native handle for open fi les, different from how standard C++ represents open fi les using fstreams. These could be integer fi le descriptors on POSIX systems and HANDLEs on Windows, for example. The Boost IOStreams library provides the boost::iostreams::file_descriptor_source, boost::iostreams::file_descriptor_sink, and boost::iostreams::file_descriptor devices that adapt POSIX fi le descriptors and Windows fi le handles into devices for input and output. In the following example, we use a file_descriptor_source object to read successive lines from a fi le on a POSIX system using the stream interface. This is useful if you want to use a stream interface for I/O on a fi le that is opened using system calls that deal in fi le descriptors.

    Listing 9.11: Using t he fi le_descriptor device

    1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 namespace io = boost::iostreams; 910 int main(int argc, char *argv[]) {11 if (argc < 2) {12 return 0;13 }1415 int fdr = open(argv[1], O_RDONLY);

  • Files, Directories, and IOStreams

    [ 362 ]

    16 if (fdr >= 0) {17 io::file_descriptor_source fdDevice(fdr,18 io::file_descriptor_flags::close_handle);19 io::stream in(fdDevice);20 assert(fdDevice.is_open());2122 std::string line;23 while (std::getline(in, line))24 std::cout

  • Chapter 9

    [ 363 ]

    We may also want to build our program to use the Boost libraries we built from source in Chapter 1, Introducing Boost. For this, I use the following command line to build this program on my Ubuntu box, specifying the include path and the library path, as well as the libboost_iostreams-mt library to link against:

    $ g++listing8_11.cpp -o listing8_11-I /opt/boost/include -std=c++11 -L /opt/boost/lib -lboost_iostreams-mt -Wl,-rpath,/opt/boost/lib

    To write to a fi le via a fi le descriptor, we need to use a file_descriptor_sink object. We can also use a file_descriptor object to both read and write to the same device. There are other devices that allow writing to fi lesthe file_source, file_sink, and file devices allow you to read and write named fi les. The mapped_file_source, mapped_file_sink, and mapped_file devices allow you to read and write to fi les via memory mappings.

    Devices for reading and writing to memoryThe Standard Library std::stringstream family of classes is commonly used for reading and writing formatted data to memory. If you want to read and write from any given contiguous memory area, like an array or byte buffer, the array family of devices (array_source, array_sink, and array) from Boost IOStreams library comes in handy:

    Listing 9.12: Using array devices

    1 #include 2 #include 3 #include 4 #include 5 #include 6 namespace io = boost::iostreams; 7 8 int main() { 9 char out_array[256];10 io::array_sink sink(out_array, out_array + sizeof(out_array));11 io::stream out(sink);12 out

  • Files, Directories, and IOStreams

    [ 364 ]

    1920 io::copy(in, std::cout);21 }

    This example follows the same pattern as Listing 9.11, but we use two devices, a sink and a source, instead of one. In each case, we do the following:

    We create an appropriately initialized device We create a stream object and associate the device with it We perform input or output on the stream

    We fi rst defi ne an array_sink device, which is used to write to a contiguous region of memory. The region of memory is passed to the device constructor as a pair of pointers to the fi rst element of an array of chars and the one past the last element (line 10). We associate this device with a stream object out (line 11) and then write some content to the stream using insertion operators (

  • Chapter 9

    [ 365 ]

    Listing 9.13: Using back_insert_device

    1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 namespace io = boost::iostreams; 8 9 int main() {10 typedef std::vector charvec;11 charvec output;12 io::back_insert_device sink(output);13 io::stream out(sink);14 out

  • Files, Directories, and IOStreams

    [ 366 ]

    Regular streams and stream buffers do not support fi lters and we need to use fi ltering streams and fi ltering stream buffers instead in order to use fi lters. Filtering streams and stream buffers maintain a stack of fi lters with the source or sink at the top and the outermost fi lter at the bottom in a data structure called a chain.

    We will now look at several utility fi lters that are shipped as part of the Boost IOStreams library. Writing our own fi lters is outside the scope of this book, but the excellent online documentation covers this topic in adequate detail.

    Basic fi ltersIn the fi rst example of using fi lters, we use boost::iostreams::counter fi lter to keep a count of characters and lines in text read from a fi le:

    Listing 9.14: Using the counter fi lter

    1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 namespace io = boost::iostreams; 8 9 int main(int argc, char *argv[]) {10 if (argc

  • Chapter 9

    [ 367 ]

    We create a boost::iostream::file_source device for reading the contents of a fi le named on the command line (line 14). We create a counter fi lter for counting the number of lines and characters read (line 15). We create an object of filtering_istream (line 16) and push the fi lter (line 17) followed by the device (line 19). Till the device is pushed, we can assert that the fi ltering stream is incomplete (line 18) and it is complete once the device is pushed (line 20). We copy the contents read from the fi ltering input stream to the standard output (line 22) and then access the character and line counts.

    To access the counts, we need to refer to the counter fi lter object sitting in the chain inside the fi ltering stream. To get to this, we call the component member template function of filtering_istream passing in the index of the fi lter we want and the type of the fi lter. This returns a pointer to the counter fi lter object (line 24) and we retrieve the number of characters and lines read by calling the appropriate member functions (lines 25-26).

    In the next example, we use boost::iostreams::grep_filter to fi lter out blank lines. Unlike the counter fi lter which did not modify the input stream, this transforms the output stream by removing blank lines.

    Listing 9.15: Using the grep_fi lter

    1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 namespace io = boost::iostreams; 8 9 int main(int argc, char *argv[]) {10 if (argc

  • Files, Directories, and IOStreams

    [ 368 ]

    This example is on the same lines as the listing 9.14 except that we use a different fi lter, boost::iostreams::grep_filter, to fi lter out blank lines. We create an instance of the grep_filter object, passing three arguments to its constructor. The fi rst argument is the regular expression ^\s*$ that matches blank lineslines that contain zero or more whitespace characters (line 16). Note that the backslash is escaped in code. The second argument is the constant match_default to indicate that we use Perl regular expression syntax (line 17). The third argument boost::iostreams::grep::invert tells the fi lter to let only those lines that match the regular expression to be fi ltered out (line 17). The default behavior is to fi lter out only those lines that do not match the regular expression.

    To build this program on Unix, you must additionally link against the Boost Regex library:

    $ g++ listing8_15.cpp -o listing8_15 -std=c++11 -lboost_iostreams-lboost_regex

    On a system without the Boost native packages and with Boost installed at a custom location, use the following more elaborate command line:

    $ g++ listing8_15.cpp -o listing8_15-I /opt/boost/include -std=c++11 -L /opt/boost/lib -lboost_iostreams-mt-lboost_regex-mt -Wl,-rpath,/opt/boost/lib

    On Windows, using Visual Studio and enabling auto linking against DLLs, you do not need to explicitly specify the Regex or IOStream DLLs.

    Filters for compression and decompressionBoost IOStreams library comes with three different fi lters for compressing and decompressing data, one each for gzip, zlib, and bzip2 formats. The gzip and zlib formats implement different variants of the DEFLATE algorithm for compression, while the bzip2 format uses the more space-effi cient Burrows-Wheeler algorithm. Since these are external libraries, they must be built and linked to our executables if we use these compression formats. If you have followed the detailed steps outlined in Chapter 1, Introducing Boost, to build Boost libraries with support for zlib and bzip2, then the zlib and bzip2 shared libraries should have been built along with the Boost Iostreams shared library.

    In the following example, we compress a fi le named on the command line and write it to the disk. We then read it back, decompress it, and write it to the standard output.

    Listing 9.16: Using gzip compressor and decompressor

    1 #include 2 #include

  • Chapter 9

    [ 369 ]

    3 #include 4 #include 5 #include 6 #include 7 namespace io = boost::iostreams; 8 9 int main(int argc, char *argv[]) {10 if (argc

  • Files, Directories, and IOStreams

    [ 370 ]

    It is possible to override several defaults by supplying additional arguments to the constructor of the compressor or decompressor fi lter, but the essential structure does not change. By changing the header from gzip.hpp to bzip2.hpp (line 4), and replacing the gzip_compressor and gzip_decompressor with bzip2_compressor and bzip2_decompressor in the preceding code, we can test the code for the bzip2 format; likewise for the zlib format. Ideally, the extensions should be changed aptly (.bz2 for bzip2 and .zlib for zlib). On most Unix systems, it will be worthwhile to test the generated compressed fi les by uncompressing them independently using gzip and bzip2 tools. Command-line tools for zlib archives seem scanty and less standardized. On my Ubuntu system, the qpdf program comes with a raw zlib compression/decompression utility called zlib-flate, which can compress to and decompress from zlib format.

    The steps to build this program are the same as the steps outlined to build listing 9.15. Even if you use the zlib_compressor or bzip2_compressor fi lters instead, the necessary shared libraries will be automatically picked up by the linker (and later, the runtime linker during execution) as long as the option -Wl,-rpath,/opt/boost/lib is used during linking and the path /opt/boost/lib contains the shared libraries for zlib and bzip2.

    Composing fi ltersFiltering streams can apply multiple fi lters to a character sequence in a pipeline. Using the push method on the fi ltering stream, we form the pipeline starting with the outermost fi lter, inserting the fi lters in the desired order, and ending with the device.

    This means that for fi ltering an output stream, you fi rst push the fi lter that gets applied fi rst and work forward pushing each successive fi lter, followed at the end by the sink. For example, in order to fi lter out some lines and compress before writing to a sink, the sequence of pushes would be like the following:

    filtering_ostream fos;fos.push(grep);fos.push(gzip);fos.push(sink);

    For fi ltering input streams, you push the fi lters, starting with the fi lter that gets applied last and work backward pushing each preceding fi lter, followed at the end by the source. For example, in order to read a fi le, decompress it and then perform a line count, the sequence of pushes will look like this:

    filtering_istream fis;fis.push(counter);fis.push(gunzip);fis.push(source);

  • Chapter 9

    [ 371 ]

    PipeliningIt turns out that a little operator overloading can make this much more expressive. We can write the preceding chains using the pipe operator (operator|) in the following alternative notation:

    filtering_ostream fos;fos.push(grep | gzip | sink);

    filtering_istream fis;fis.push(counter | gunzip | source);

    The preceding snippet is clearly more expressive with fewer lines of code. From left to right, the fi lters are strung together in the order you push them into the stream, with the device at the end. Not all fi lters can be combined in this way, but many readily available ones from the Boost IOStreams library can; more defi nitively, fi lters must conform to the Pipable concept to be combined this way. Here is a complete example of a program that reads the text in a fi le, removes blank lines, and then compresses it using bzip2:

    Listing 9.17: Piping fi lters

    1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 namespace io = boost::iostreams;1011 int main(int argc, char *argv[]) {12 if (argc

  • Files, Directories, and IOStreams

    [ 372 ]

    The preceding example strings together a grep fi lter for fi ltering out blank lines (lines 16-18) and a bzip2 compressor (line 15) with a fi le source device using pipes (line 20). The rest of the code should be familiar from listings 9.15 and 9.16.

    Branching data streams with teeWhile using fi lter chains with multiple fi lters, it is sometimes useful, especially for debugging, to capture the data fl owing between two fi lters. The boost::iostreams:: tee_filter is an output fi lter akin to the Unix tee command that sits interposed between two fi lters and extracts a copy of the data stream fl owing between the two fi lters. Essentially, when you want to capture data at different intermediate stages of processing, you can use a tee_filter:

    filter1

    outputfilter1 Tee filter2filter3

    input filter3

    filter1

    output

    You can also multiplex two sink devices to create a tee device, such that writing some content to the tee device writes it to both the underlying devices. The boost::iostream::tee_device class template combines two sinks to create such a tee device. By nesting tee devices or pipelining tee fi lters, we can generate several parallel streams that can be processed differently. The boost::iostreams::tee function template can generate tee fi lters and tee streams. It has two overloadsa single-argument overload that takes a sink and generates a tee_filter, and a two-argument overload that takes two sinks and returns a tee_device. The following example shows how to compress a fi le to three different compression formats (gzip, zlib, and bzip2) using very little code:

    Listing 9.18: Branching output streams with tees

    1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 namespace io = boost::iostreams;10

  • Chapter 9

    [ 373 ]

    11 int main(int argc, char *argv[]) {12 if (argc

  • Files, Directories, and IOStreams

    [ 374 ]

    Notice that the calls to tee are not namespace-qualifi ed but get correctly resolved due to Argument Dependent Lookup (see Chapter 2, The First Brush with Boost's Utilities).

    The Boost IOStreams library provides a very rich framework for writing and using devices and fi lters. This chapter introduces only the basic uses of this library and there is a whole host of fi lters, devices, and adaptors that can be combined into useful patterns for I/O.

    Self-test questionsFor multiple-choice questions, choose all options that apply:

    1. What is unique to the canonical and equivalent functions for manipulating paths?a. The arguments cannot name real paths.b. Both are namespace-level functions.c. The arguments must name real paths.

    2. What is the problem with the following code snippet assuming the path is of type boost::filesystem::path?if (is_regular_file(path)) { /* */ }else if (is_directory(path)) { /* */ }else if (is_symlink(path)) { /* */ }

    a. It must have static value fi eld.b. It must have an embedded type called type.c. It must have static type fi eld.d. It must have an embedded type called result.

    3. Given this code snippet:boost::filesystem::path p1("/opt/boost/include/boost/thread.hpp");size_t n = std::distance(p1.begin(), p1.end());

    What is the value of n?a. 5, the total number of components in the path.b. 6, the total number of components in the path.c. 10, the sum of the number of slashes and components.d. 4, the total number of directory components.

  • Chapter 9

    [ 375 ]

    4. You want to read a text fi le, remove all blank lines using a grep_filter, replace specifi c keywords using the regex_filter, and count the characters and lines in the result. Which of the following pipelines will you use?a. file_source | grep_filter| regex_filter | counterb. grep_filter | regex_filter | counter | file_sourcec. counter | regex_filter | grep_filter |file_sourced. file_source | counter | grep_filter | regex_filter

    5. True or false: A tee fi lter cannot be used with an input stream.

    a. True.b. False.

    SummaryIn this chapter, we covered the Boost Filesystem library for reading fi le metadata and state of fi les and directories, and performing operations on them. We also covered the high-level Boost IOStreams framework for performing type-safe I/O with rich semantics.

    Working with fi les and performing I/O are basic system programming tasks that almost any useful piece of software needs to perform and the Boost libraries we covered in this chapter ease those tasks through a set of portable interfaces. In the next chapter, we will turn our attention to another systems programming topic concurrency and multithreading.

  • Where to buy this book You can buy Learning Boost C++ Libraries from the Packt Publishing website.

    Alternatively, you can buy the book from Amazon, BN.com, Computer Manuals and most internet

    book retailers.

    Click here for ordering and shipping details.

    www.PacktPub.com

    Stay Connected:

    Get more information Learning Boost C++ Libraries