From e9d72fa16cba59bcb3dfd2bc75c51fd1c40d3380 Mon Sep 17 00:00:00 2001 From: Beman Date: Sat, 25 Jul 2015 14:47:52 -0400 Subject: [PATCH] Upgrade build infrastructure. Modernize tutorial examples to use C++11 to simplify code and make it easier to understand. Initial tutorial edits. --- doc/tutorial.html | 47 ++++++++++++++++------------------ example/msvc/common.props | 3 +++ example/msvc/tut3/tut3.vcxproj | 12 +++++++++ example/msvc/tut4/tut4.vcxproj | 12 +++++++++ example/test/bld.bat | 4 +-- example/test/bld.sh | 4 +-- example/tut0.cpp | 2 -- example/tut2.cpp | 10 ++++---- example/tut3.cpp | 18 ++++++------- example/tut4.cpp | 27 +++++++++---------- 10 files changed, 78 insertions(+), 61 deletions(-) diff --git a/doc/tutorial.html b/doc/tutorial.html index 067808d..f28279d 100644 --- a/doc/tutorial.html +++ b/doc/tutorial.html @@ -121,29 +121,26 @@ is well. A set of tutorial programs has been copied (by setup) to and then built. You are encouraged to modify and experiment with them as the tutorial progresses. Just invoke the bld script again to rebuild.

-

If something didn't work right, here are troubleshooting suggestions:

+

If something didn't work right, here are some troubleshooting suggestions:

Reporting the size of a file - (tut1.cpp)

-

Let's get started. One of the simplest things we can do is report the size of +

Let's get started. Our first example program, tut1.cpp, reports the size of a file.

-
tut1.cpp
-
#include <iostream>
 #include <boost/filesystem.hpp>
 using namespace boost::filesystem;
@@ -158,7 +155,6 @@ int main(int argc, char* argv[])
   std::cout << argv[1] << " " << file_size(argv[1]) << '\n';
   return 0;
 }
-
@@ -244,7 +240,8 @@ Aborted
>tut1 foo
-

An exception is thrown; the exact form of the response depends on +

An exception is thrown;
+ the exact form of the response depends on Windows system options.
@@ -279,8 +276,8 @@ Aborted

>tut1 .
-

An exception is thrown; the exact form of the response depends on - Windows system options. +

An exception is thrown;
+ the exact form of the response depends on Windows system options.
@@ -300,14 +297,12 @@ described by their name is met. Otherwise they return false, including when any element of the path argument can't be found.

-

tut2.cpp uses several of the status query functions to cope with non-existent +

tut2.cpp uses several of the status query functions to cope with non-existent files and with different kinds of files:

-
tut2.cpp
-
int main(int argc, char* argv[])
 {
   path p (argv[1]);   // p reads clearer than argv[1] in the following code
@@ -328,7 +323,6 @@ files and with different kinds of files:

return 0; }
-
@@ -407,7 +401,8 @@ Aborted
>dir e:\
 The device is not ready.
 >tut2 e:\
-

An exception is thrown; the exact form of the response depends on +

An exception is thrown;
+ the exact form of the response depends on Windows system options.
@@ -429,7 +424,7 @@ acts as the end iterator.

The value type of directory_iterator is directory_entry. A -directory_entry object contains a path and file_status +directory_entry object contains path and file_status information.  A directory_entry object can be used directly, but can also be passed to path arguments in function calls.

@@ -437,13 +432,12 @@ can be used directly, but can also be passed to path arguments in f

The other need is increased robustness in the face of the many kinds of errors that can affect file system operations. We could do that at the level of each call to a Boost.Filesystem function (see Error -reporting), but it is easier to supply an overall try/catch block.

+reporting), but for simplicity tut3.cpp +uses an overall try/catch block.

-
tut3.cpp
-
int main(int argc, char* argv[])
 {
   path p (argv[1]);   // p reads clearer than argv[1] in the following code
@@ -479,7 +473,6 @@ reporting), but it is easier to supply an overall try/catch block.

return 0; }
-
@@ -574,10 +567,14 @@ boost::filesystem::status: The device is not ready: "e:\" results ourselves. -

Move on to tut4.cpp to see how those changes play out!

+

The next sections show how how those changes play out, so read on!

Using path decomposition, plus sorting results - (tut4.cpp)

+

For directories, tut4.cpp builds a +std::vector of all the entries and then sorts it before writing to +cout.

+
@@ -598,7 +595,7 @@ boost::filesystem::status: The device is not ready: "e:\" { cout << p << " is a directory containing:\n"; - typedef vector<path> vec; // store paths, + typedef std::vector<path> vec; // store paths, vec v; // so we can sort them later copy(directory_iterator(p), directory_iterator(), back_inserter(v)); @@ -1235,7 +1232,7 @@ It has a

Distributed under the Boost Software License, Version 1.0. See www.boost.org/LICENSE_1_0.txt

Revised -23 July 2015

+24 July 2015

diff --git a/example/msvc/common.props b/example/msvc/common.props index 3784ff6..527c3c8 100644 --- a/example/msvc/common.props +++ b/example/msvc/common.props @@ -7,6 +7,9 @@ ..\..\..\..\.. + + ..\..\..\..\..\stage\lib;%(AdditionalLibraryDirectories) + \ No newline at end of file diff --git a/example/msvc/tut3/tut3.vcxproj b/example/msvc/tut3/tut3.vcxproj index 7e1054e..f452e0d 100644 --- a/example/msvc/tut3/tut3.vcxproj +++ b/example/msvc/tut3/tut3.vcxproj @@ -98,6 +98,9 @@ Console true + + "$(TargetDir)\$(TargetName).exe" . + @@ -112,6 +115,9 @@ Console true + + "$(TargetDir)\$(TargetName).exe" . + @@ -130,6 +136,9 @@ true true + + "$(TargetDir)\$(TargetName).exe" . + @@ -148,6 +157,9 @@ true true + + "$(TargetDir)\$(TargetName).exe" . + diff --git a/example/msvc/tut4/tut4.vcxproj b/example/msvc/tut4/tut4.vcxproj index 9914fb8..f080b01 100644 --- a/example/msvc/tut4/tut4.vcxproj +++ b/example/msvc/tut4/tut4.vcxproj @@ -98,6 +98,9 @@ Console true + + "$(TargetDir)\$(TargetName).exe" . + @@ -112,6 +115,9 @@ Console true + + "$(TargetDir)\$(TargetName).exe" . + @@ -130,6 +136,9 @@ true true + + "$(TargetDir)\$(TargetName).exe" . + @@ -148,6 +157,9 @@ true true + + "$(TargetDir)\$(TargetName).exe" . + diff --git a/example/test/bld.bat b/example/test/bld.bat index 9768600..98a98cf 100644 --- a/example/test/bld.bat +++ b/example/test/bld.bat @@ -3,5 +3,5 @@ rem Copyright Beman Dawes, 2010 rem Distributed under the Boost Software License, Version 1.0. rem See www.boost.org/LICENSE_1_0.txt -bjam %* >bjam.log -find "error" b2.log +find "error" bjam.log -grep "error" b2.log +grep "error" #include #include -using namespace std; +using std::cout; using namespace boost::filesystem; int main(int argc, char* argv[]) @@ -22,26 +22,24 @@ int main(int argc, char* argv[]) return 1; } - path p (argv[1]); // p reads clearer than argv[1] in the following code + path p (argv[1]); try { - if (exists(p)) // does p actually exist? + if (exists(p)) { - if (is_regular_file(p)) // is p a regular file? + if (is_regular_file(p)) cout << p << " size is " << file_size(p) << '\n'; - else if (is_directory(p)) // is p a directory? + else if (is_directory(p)) { cout << p << " is a directory containing:\n"; - copy(directory_iterator(p), directory_iterator(), // directory_iterator::value_type - ostream_iterator(cout, "\n")); // is directory_entry, which is - // converted to a path by the - // path stream inserter + for (directory_entry& x : directory_iterator(p)) + cout << x.path() << '\n'; } else - cout << p << " exists, but is neither a regular file nor a directory\n"; + cout << p << " exists, but is not a regular file or directory\n"; } else cout << p << " does not exist\n"; diff --git a/example/tut4.cpp b/example/tut4.cpp index 5888c3a..d20f170 100644 --- a/example/tut4.cpp +++ b/example/tut4.cpp @@ -12,7 +12,7 @@ #include #include #include -using namespace std; +using std::cout; using namespace boost::filesystem; int main(int argc, char* argv[]) @@ -23,34 +23,31 @@ int main(int argc, char* argv[]) return 1; } - path p (argv[1]); // p reads clearer than argv[1] in the following code + path p (argv[1]); try { - if (exists(p)) // does p actually exist? + if (exists(p)) { - if (is_regular_file(p)) // is p a regular file? + if (is_regular_file(p)) cout << p << " size is " << file_size(p) << '\n'; - else if (is_directory(p)) // is p a directory? + else if (is_directory(p)) { cout << p << " is a directory containing:\n"; - typedef vector vec; // store paths, - vec v; // so we can sort them later + std::vector v; - copy(directory_iterator(p), directory_iterator(), back_inserter(v)); + for (auto&& x : directory_iterator(p)) + v.push_back(x.path().filename().string()); - sort(v.begin(), v.end()); // sort, since directory iteration - // is not ordered on some file systems + std::sort(v.begin(), v.end()); - for (vec::const_iterator it(v.begin()), it_end(v.end()); it != it_end; ++it) - { - cout << " " << *it << '\n'; - } + for (auto&& x : v) + cout << x << '\n'; } else - cout << p << " exists, but is neither a regular file nor a directory\n"; + cout << p << " exists, but is not a regular file or directory\n"; } else cout << p << " does not exist\n";