vocabtree  0.0.1
filesystem.cxx
Go to the documentation of this file.
1 #include "filesystem.hpp"
2 
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <fstream>
6 #include <iomanip>
7 
8 #include <boost/filesystem.hpp>
9 
10 namespace filesystem {
11 
12  bool file_exists(const std::string& name) {
13  struct stat buffer;
14  return (stat (name.c_str(), &buffer) == 0);
15  }
16 
17  void create_file_directory(const std::string &absfilepath) {
18  boost::filesystem::path p(absfilepath.c_str());
19  boost::filesystem::path d = p.parent_path();
20  if(!boost::filesystem::exists(d)) {
21  boost::filesystem::create_directories(d);
22  }
23  }
24  struct cvmat_header {
25  uint64_t elem_size;
26  int32_t elem_type;
27  uint32_t rows, cols;
28  };
29 
30  bool write_cvmat(const std::string &fname, const cv::Mat &data) {
31  std::ofstream ofs(fname.c_str(), std::ios::binary | std::ios::trunc);
32  cvmat_header h;
33  h.elem_size = data.elemSize();
34  h.elem_type = data.type();
35  h.rows = data.rows;
36  h.cols = data.cols;
37  ofs.write((char *)&h, sizeof(cvmat_header));
38  ofs.write((char *)data.ptr(), h.rows * h.cols * h.elem_size);
39  return (ofs.rdstate() & std::ofstream::failbit) == 0;
40  }
41 
42  bool load_cvmat(const std::string &fname, cv::Mat &data) {
43  if(!file_exists(fname)) return false;
44  std::ifstream ifs(fname.c_str(), std::ios::binary);
45  cvmat_header h;
46  ifs.read((char *)&h, sizeof(cvmat_header));
47  if (h.rows == 0 || h.cols == 0) return false;
48  data.create(h.rows, h.cols, h.elem_type);
49  ifs.read((char *)data.ptr(), h.rows * h.cols * h.elem_size);
50  return (ifs.rdstate() & std::ifstream::failbit) == 0;
51  }
52 
53  bool write_sparse_vector(const std::string &fname, const std::vector<std::pair<uint32_t, float > > &data) {
54  std::ofstream ofs(fname.c_str(), std::ios::binary | std::ios::trunc);
55  uint32_t dim0 = data.size();
56  ofs.write((char *)&dim0, sizeof(uint32_t));
57  ofs.write((char *)&data[0], sizeof(std::pair<uint32_t, float >) * dim0);
58  return (ofs.rdstate() & std::ofstream::failbit) == 0;
59  }
60 
61  bool load_sparse_vector(const std::string &fname, std::vector<std::pair<uint32_t, float > > &data) {
62  if(!file_exists(fname)) return false;
63  std::ifstream ifs(fname.c_str(), std::ios::binary);
64  uint32_t dim0;
65  ifs.read((char *)&dim0, sizeof(uint32_t));
66  data.resize(dim0);
67  ifs.read((char *)&data[0], sizeof(std::pair<uint32_t, float >) * dim0);
68  return (ifs.rdstate() & std::ifstream::failbit) == 0;
69  }
70 
71  std::vector<std::string> list_files(const std::string &path, const std::string &ext, bool recursive) {
72  boost::filesystem::path input_path(path);
73  std::vector<std::string> file_list;
74  if (boost::filesystem::exists(input_path) && boost::filesystem::is_directory(input_path)) {
75  if(recursive) {
76  boost::filesystem::recursive_directory_iterator end;
77  for (boost::filesystem::recursive_directory_iterator it(input_path); it != end; ++it) {
78  if (!boost::filesystem::is_directory(*it)) {
79  if ((ext.length() > 0 && boost::filesystem::extension(*it) == boost::filesystem::path(ext)) ||
80  ext.length() == 0) {
81  file_list.push_back(it->path().string());
82  }
83  }
84  }
85  } else {
86  boost::filesystem::directory_iterator end;
87  for (boost::filesystem::directory_iterator it(input_path); it != end; ++it) {
88  if (!boost::filesystem::is_directory(*it)) {
89  if ((ext.length() > 0 && boost::filesystem::extension(*it) == boost::filesystem::path(ext)) ||
90  ext.length() == 0) {
91  file_list.push_back(it->path().string());
92  }
93  }
94  }
95  }
96  }
97  return file_list;
98  }
99 
100  std::string basename(const std::string &path, bool include_extension) {
101  if (!include_extension) return boost::filesystem::basename(boost::filesystem::path(path));
102 
103  return boost::filesystem::basename(boost::filesystem::path(path)) + boost::filesystem::extension(path);
104  }
105 
106  bool write_text(const std::string &fname, const std::string &text) {
107  std::ofstream ofs(fname, std::ios::trunc);
108  ofs.write(text.c_str(), text.size());
109  return (ofs.rdstate() & std::ofstream::failbit) == 0;
110  }
111 }