ProteoWizard
Filesystem.hpp
Go to the documentation of this file.
1//
2// $Id$
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#ifdef __cplusplus_cli
29// "boost/filesystem/path.hpp" uses "generic" as an identifier which is a reserved word in C++/CLI
30#define generic __identifier(generic)
31#endif
32
33#include "Export.hpp"
34#include "String.hpp"
35#include "Container.hpp"
36#include <boost/filesystem/operations.hpp>
37#include <boost/filesystem/convenience.hpp>
38#include <boost/filesystem/fstream.hpp>
39#include <boost/version.hpp>
41
42namespace bfs = boost::filesystem;
43
44#ifndef BOOST_FILESYSTEM_VERSION
45# if (BOOST_VERSION/100) >= 1046
46# define BOOST_FILESYSTEM_VERSION 3
47# else
48# define BOOST_FILESYSTEM_VERSION 2
49# endif
50#endif // BOOST_FILESYSTEM_VERSION
51
52
53// boost filesystem v2 support is going away
54// and v3 breaks the API in surprising ways
55// see http://www.boost.org/doc/libs/1_47_0/libs/filesystem/v3/doc/deprecated.html
56#if BOOST_FILESYSTEM_VERSION == 2
57// in BFS2 p.filename() or p.leaf() or p.extension() returns a string
58#define BFS_STRING(p) p
59#define BFS_GENERIC_STRING(p) p
60// in BFS2 complete() is in namespace
61#define BFS_COMPLETE bfs::complete
62#else
63// in BFS3 p.filename() or p.leaf() or p.extension() returns a bfs::path
64#define BFS_STRING(p) (p).string()
65#define BFS_GENERIC_STRING(p) (p).generic_string()
66// in BFS3 complete() is not in namespace
67#define BFS_COMPLETE bfs::system_complete
68#endif
69
70namespace pwiz {
71namespace util {
72
73
74/// on Windows, closes all file handles and memory mapped sections relating to the given filepath
75PWIZ_API_DECL void force_close_handles_to_filepath(const std::string& filepath, bool closeMemoryMappedSections = false) noexcept(true);
76
77/// expands (aka globs) a pathmask to zero or more matching paths and returns the number of matching paths
78/// - matching paths can be either files or directories
79/// - matching paths will be absolute if input pathmask was absolute
80/// - matching paths will be relative if input pathmask was relative
81PWIZ_API_DECL int expand_pathmask(const bfs::path& pathmask,
82 vector<bfs::path>& matchingPaths);
83
84/// if recursive is true, then copies "from" and all its contents to "to";
85/// if "to" already exists or the copy fails for another reason, the error is reported in one of two ways:
86/// - if "ec" is not NULL, it will set it to the error code
87/// - if "ec" is NULL, a boost::filesystem_error is thrown
88PWIZ_API_DECL void copy_directory(const bfs::path& from, const bfs::path& to, bool recursive = true, boost::system::error_code* ec = 0);
89
91{
92 /// sizes are treated as multiples of 2;
93 /// abbreviations are: GiB (Gibibyte), MiB (Mebibyte), KiB (Kibibyte), B (byte)
95
96 /// sizes are treated as multiples of 2;
97 /// abbreviations are: GB (Gigabyte), MB (Megabyte), KB (Kilobyte), B (byte)
99
100 /// sizes are treated as multiples of 10;
101 /// abbreviations are: GB (Gigabyte), MB (Megabyte), KB (Kilobyte), B (byte)
104
105
106/// abbreviates a byte size (file or RAM) as a readable string, using the specified notation
108std::string abbreviate_byte_size(boost::uintmax_t byteSize,
110
111
112PWIZ_API_DECL bool isHTTP(const std::string& filepath);
113
114PWIZ_API_DECL std::string read_file_header(const std::string& filepath, size_t length = 512);
115
116
117/// 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
118PWIZ_API_DECL std::pair<int, int> get_console_bounds(const std::pair<int, int>& defaultBounds = std::pair<int, int>(80, 24));
119
120} // util
121} // pwiz
122
123#endif // _FILESYSTEM_HPP_
#define PWIZ_API_DECL
Definition Export.hpp:32
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...
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),...
PWIZ_API_DECL std::string read_file_header(const std::string &filepath, size_t length=512)
PWIZ_API_DECL bool isHTTP(const std::string &filepath)
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 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
@ ByteSizeAbbreviation_JEDEC
sizes are treated as multiples of 2; abbreviations are: GB (Gigabyte), MB (Megabyte),...
@ ByteSizeAbbreviation_IEC
sizes are treated as multiples of 2; abbreviations are: GiB (Gibibyte), MiB (Mebibyte),...
@ ByteSizeAbbreviation_SI
sizes are treated as multiples of 10; abbreviations are: GB (Gigabyte), MB (Megabyte),...
PWIZ_API_DECL void force_close_handles_to_filepath(const std::string &filepath, bool closeMemoryMappedSections=false) noexcept(true)
on Windows, closes all file handles and memory mapped sections relating to the given filepath