GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
fs_util.cpp
1 /**
2  * Copyright (c) 2009 Carnegie Mellon University.
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an "AS
13  * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14  * express or implied. See the License for the specific language
15  * governing permissions and limitations under the License.
16  *
17  * For more about this software visit:
18  *
19  * http://www.graphlab.ml.cmu.edu
20  *
21  */
22 
23 
24 #include <boost/version.hpp>
25 #include <boost/filesystem.hpp>
26 #include <boost/algorithm/string/predicate.hpp>
27 
28 
29 #include <vector>
30 #include <iostream>
31 #include <iomanip>
32 #include <sstream>
33 #include <string>
34 
35 
36 
37 
38 #include <graphlab/util/fs_util.hpp>
39 
40 
41 
42 void graphlab::fs_util::
43 list_files_with_suffix(const std::string& pathname,
44  const std::string& suffix,
45  std::vector<std::string>& files) {
46  namespace fs = boost::filesystem;
47  fs::path dir_path(pathname);
48  fs::directory_iterator end_iter;
49  files.clear();
50  if ( fs::exists(dir_path) && fs::is_directory(dir_path)) {
51  for( fs::directory_iterator dir_iter(dir_path) ;
52  dir_iter != end_iter ; ++dir_iter) {
53  if (fs::is_regular_file(dir_iter->status()) ) {
54 #if BOOST_FILESYSTEM_VERSION >= 3
55  const std::string filename = dir_iter->path().filename().string();
56 #else
57  const std::string filename = dir_iter->leaf();
58 #endif
59  if (suffix.size() > 0 && !boost::ends_with(filename, suffix))
60  continue;
61  files.push_back(filename);
62  }
63  }
64  }
65  std::sort(files.begin(), files.end());
66 // namespace fs = boost::filesystem;
67 // fs::path path(pathname);
68 // assert(fs::exists(path));
69 // for(fs::directory_iterator iter( path ), end_iter;
70 // iter != end_iter; ++iter) {
71 // if( ! fs::is_directory(iter->status()) ) {
72 
73 // #if BOOST_FILESYSTEM_VERSION >= 3
74 // std::string filename(iter->path().filename().string());
75 // #else
76 // std::string filename(iter->path().filename());
77 // #endif
78 // size_t pos =
79 // filename.size() >= suffix.size()?
80 // filename.size() - suffix.size() : 0;
81 // std::string ending(filename.substr(pos));
82 // if(ending == suffix) {
83 // #if BOOST_FILESYSTEM_VERSION >= 3
84 // files.push_back(iter->path().filename().string());
85 // #else
86 // files.push_back(iter->path().filename());
87 // #endif
88 // }
89 // }
90 // }
91 // std::sort(files.begin(), files.end());
92 } // end of list files with suffix
93 
94 
95 
96 void graphlab::fs_util::
97 list_files_with_prefix(const std::string& pathname,
98  const std::string& prefix,
99  std::vector<std::string>& files) {
100  namespace fs = boost::filesystem;
101  fs::path dir_path(pathname);
102  fs::directory_iterator end_iter;
103  files.clear();
104  if ( fs::exists(dir_path) && fs::is_directory(dir_path)) {
105  for( fs::directory_iterator dir_iter(dir_path) ;
106  dir_iter != end_iter ; ++dir_iter) {
107  if (fs::is_regular_file(dir_iter->status()) ) {
108  const std::string filename = dir_iter->path().filename().string();
109  if (prefix.size() > 0 && !boost::starts_with(filename, prefix)) {
110  continue;
111  }
112  files.push_back(dir_iter->path().string());
113  }
114  }
115  }
116  std::sort(files.begin(), files.end());
117 } // end of list files with prefix
118 
119 
120 
121 
122 
123 std::string graphlab::fs_util::
124 change_suffix(const std::string& fname,
125  const std::string& new_suffix) {
126  size_t pos = fname.rfind('.');
127  assert(pos != std::string::npos);
128  const std::string new_base(fname.substr(0, pos));
129  return new_base + new_suffix;
130 } // end of change_suffix
131 
132