ProteoWizard
Filesystem.hpp
Go to the documentation of this file.
1 //
2 // $Id: Filesystem.hpp 7303 2015-03-13 20:19:40Z chambm $
3 //
4 //
5 // Original author: Matt Chambers <matt.chambers .@. vanderbilt.edu>
6 //
7 // Copyright 2008 Spielberg Family Center for Applied Proteomics
8 // Cedars Sinai Medical Center, Los Angeles, California 90048
9 // Copyright 2008 Vanderbilt University - Nashville, TN 37232
10 //
11 // Licensed under the Apache License, Version 2.0 (the "License");
12 // you may not use this file except in compliance with the License.
13 // You may obtain a copy of the License at
14 //
15 // http://www.apache.org/licenses/LICENSE-2.0
16 //
17 // Unless required by applicable law or agreed to in writing, software
18 // distributed under the License is distributed on an "AS IS" BASIS,
19 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 // See the License for the specific language governing permissions and
21 // limitations under the License.
22 //
23 
24 
25 #ifndef _FILESYSTEM_HPP_
26 #define _FILESYSTEM_HPP_
27 
28 #include "Export.hpp"
29 #include "String.hpp"
30 #include "Container.hpp"
31 #include <boost/filesystem/operations.hpp>
32 #include <boost/filesystem/convenience.hpp>
33 #include <boost/filesystem/fstream.hpp>
34 #include <boost/version.hpp>
36 
37 namespace bfs = boost::filesystem;
38 
39 #ifndef BOOST_FILESYSTEM_VERSION
40 # if (BOOST_VERSION/100) >= 1046
41 # define BOOST_FILESYSTEM_VERSION 3
42 # else
43 # define BOOST_FILESYSTEM_VERSION 2
44 # endif
45 #endif // BOOST_FILESYSTEM_VERSION
46 
47 
48 // boost filesystem v2 support is going away
49 // and v3 breaks the API in surprising ways
50 // see http://www.boost.org/doc/libs/1_47_0/libs/filesystem/v3/doc/deprecated.html
51 #if BOOST_FILESYSTEM_VERSION == 2
52 // in BFS2 p.filename() or p.leaf() or p.extension() returns a string
53 #define BFS_STRING(p) p
54 #define BFS_GENERIC_STRING(p) p
55 // in BFS2 complete() is in namespace
56 #define BFS_COMPLETE bfs::complete
57 #else
58 // in BFS3 p.filename() or p.leaf() or p.extension() returns a bfs::path
59 #define BFS_STRING(p) (p).string()
60 #define BFS_GENERIC_STRING(p) (p).generic_string()
61 // in BFS3 complete() is not in namespace
62 #define BFS_COMPLETE bfs::system_complete
63 #endif
64 
65 namespace pwiz {
66 namespace util {
67 
68 
69 /// expands (aka globs) a pathmask to zero or more matching paths and returns the number of matching paths
70 /// - matching paths can be either files or directories
71 /// - matching paths will be absolute if input pathmask was absolute
72 /// - matching paths will be relative if input pathmask was relative
73 PWIZ_API_DECL int expand_pathmask(const bfs::path& pathmask,
74  vector<bfs::path>& matchingPaths);
75 
76 /// if recursive is true, then copies "from" and all its contents to "to";
77 /// if "to" already exists or the copy fails for another reason, the error is reported in one of two ways:
78 /// - if "ec" is not NULL, it will set it to the error code
79 /// - if "ec" is NULL, a boost::filesystem_error is thrown
80 PWIZ_API_DECL void copy_directory(const bfs::path& from, const bfs::path& to, bool recursive = true, boost::system::error_code* ec = 0);
81 
83 {
84  /// sizes are treated as multiples of 2;
85  /// abbreviations are: GiB (Gibibyte), MiB (Mebibyte), KiB (Kibibyte), B (byte)
87 
88  /// sizes are treated as multiples of 2;
89  /// abbreviations are: GB (Gigabyte), MB (Megabyte), KB (Kilobyte), B (byte)
91 
92  /// sizes are treated as multiples of 10;
93  /// abbreviations are: GB (Gigabyte), MB (Megabyte), KB (Kilobyte), B (byte)
95 };
96 
97 
98 /// abbreviates a byte size (file or RAM) as a readable string, using the specified notation
100 std::string abbreviate_byte_size(boost::uintmax_t byteSize,
101  ByteSizeAbbreviation abbreviationType = ByteSizeAbbreviation_SI);
102 
103 
104 PWIZ_API_DECL std::string read_file_header(const std::string& filepath, size_t length = 512);
105 
106 
107 /// attempts to get the platform-specific console bounds (number of columns and lines), returns defaultBounds if an error occurs or the platform is not supported
108 PWIZ_API_DECL std::pair<int, int> get_console_bounds(const std::pair<int, int>& defaultBounds = std::pair<int, int>(80, 24));
109 
110 } // util
111 } // pwiz
112 
113 #endif // _FILESYSTEM_HPP_
PWIZ_API_DECL int expand_pathmask(const bfs::path &pathmask, vector< bfs::path > &matchingPaths)
expands (aka globs) a pathmask to zero or more matching paths and returns the number of matching path...
sizes are treated as multiples of 2; abbreviations are: GiB (Gibibyte), MiB (Mebibyte), KiB (Kibibyte), B (byte)
Definition: Filesystem.hpp:86
PWIZ_API_DECL void copy_directory(const bfs::path &from, const bfs::path &to, bool recursive=true, boost::system::error_code *ec=0)
if recursive is true, then copies "from" and all its contents to "to"; if "to" already exists or the ...
PWIZ_API_DECL std::string read_file_header(const std::string &filepath, size_t length=512)
sizes are treated as multiples of 10; abbreviations are: GB (Gigabyte), MB (Megabyte), KB (Kilobyte), B (byte)
Definition: Filesystem.hpp:94
PWIZ_API_DECL std::pair< int, int > get_console_bounds(const std::pair< int, int > &defaultBounds=std::pair< int, int >(80, 24))
attempts to get the platform-specific console bounds (number of columns and lines), returns defaultBounds if an error occurs or the platform is not supported
PWIZ_API_DECL std::string abbreviate_byte_size(boost::uintmax_t byteSize, ByteSizeAbbreviation abbreviationType=ByteSizeAbbreviation_SI)
abbreviates a byte size (file or RAM) as a readable string, using the specified notation ...
#define PWIZ_API_DECL
Definition: Export.hpp:32
sizes are treated as multiples of 2; abbreviations are: GB (Gigabyte), MB (Megabyte), KB (Kilobyte), B (byte)
Definition: Filesystem.hpp:90