vocabtree  0.0.1
filesystem Namespace Reference

Provides useful wrappers around many filesystem related functionality, including reading writing certain common data structures as well as common operations (ex. More...

Data Structures

struct  cvmat_header
 

Functions

bool file_exists (const std::string &name)
 Returns true if file exists at location, else returns false. More...
 
void create_file_directory (const std::string &absfilepath)
 Recursively creates all directories if needed up to the specified file. More...
 
bool write_cvmat (const std::string &fname, const cv::Mat &data)
 Writes a cv::Mat structure to the specified location. More...
 
bool load_cvmat (const std::string &fname, cv::Mat &data)
 Loads a cv::Mat structure from the specified location. More...
 
bool write_sparse_vector (const std::string &fname, const std::vector< std::pair< uint32_t, float > > &data)
 Writes the BoW feature to the specified location. More...
 
bool load_sparse_vector (const std::string &fname, std::vector< std::pair< uint32_t, float > > &data)
 Loads the BoW feature from the specified location. More...
 
std::vector< std::string > list_files (const std::string &path, const std::string &ext="", bool recursive=true)
 Lists all files in the given directory with an optional extension. More...
 
std::string basename (const std::string &path, bool include_extension=false)
 Returns the basename of the input filepath, including or not including the extension. More...
 
bool write_text (const std::string &fname, const std::string &text)
 Writes a text file to the input file location given the input string. More...
 

Detailed Description

Provides useful wrappers around many filesystem related functionality, including reading writing certain common data structures as well as common operations (ex.

file_exists).

Function Documentation

std::string filesystem::basename ( const std::string &  path,
bool  include_extension 
)

Returns the basename of the input filepath, including or not including the extension.

Definition at line 100 of file filesystem.cxx.

100  {
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  }
void filesystem::create_file_directory ( const std::string &  absfilepath)

Recursively creates all directories if needed up to the specified file.

Definition at line 17 of file filesystem.cxx.

Referenced by benchmark_dataset(), compute_bow(), compute_bow_features(), compute_features(), main(), BagOfWords::save(), MatchesPage::write(), and SimpleDataset::write().

17  {
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  }
bool filesystem::file_exists ( const std::string &  name)

Returns true if file exists at location, else returns false.

Definition at line 12 of file filesystem.cxx.

Referenced by BagOfWords::BagOfWords(), compute_bow(), compute_bow_features(), compute_features(), InvertedIndex::InvertedIndex(), load_cvmat(), load_sparse_vector(), main(), SimpleDataset::read(), VocabTree::search(), InvertedIndex::search(), SimpleDataset::SimpleDataset(), VocabTree::train(), InvertedIndex::train(), BagOfWords::train(), and MatchesPage::write().

12  {
13  struct stat buffer;
14  return (stat (name.c_str(), &buffer) == 0);
15  }
std::vector< std::string > filesystem::list_files ( const std::string &  path,
const std::string &  ext = "",
bool  recursive = true 
)

Lists all files in the given directory with an optional extension.

The extension must include the dot (ie. ext=".txt"). If recursive is true (default), will recursively enter all directories

Definition at line 71 of file filesystem.cxx.

Referenced by SimpleDataset::construct_dataset().

71  {
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  }
bool filesystem::load_cvmat ( const std::string &  fname,
cv::Mat &  data 
)

Loads a cv::Mat structure from the specified location.

Returns true if file exists, false otherwise.

Definition at line 42 of file filesystem.cxx.

References file_exists().

Referenced by bench_oxford(), benchmark_dataset(), compute_bow(), compute_bow_features(), BagOfWords::load(), main(), VocabTree::search(), VocabTree::train(), and BagOfWords::train().

42  {
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  }
bool filesystem::load_sparse_vector ( const std::string &  fname,
std::vector< std::pair< uint32_t, float > > &  data 
)

Loads the BoW feature from the specified location.

First dimension of data is cluster index, second dimension is TF score.

Definition at line 61 of file filesystem.cxx.

References file_exists().

Referenced by InvertedIndex::search(), and InvertedIndex::train().

61  {
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  }
bool filesystem::write_cvmat ( const std::string &  fname,
const cv::Mat &  data 
)

Writes a cv::Mat structure to the specified location.

Definition at line 30 of file filesystem.cxx.

References filesystem::cvmat_header::elem_size.

Referenced by compute_features(), main(), and BagOfWords::save().

30  {
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  }
bool filesystem::write_sparse_vector ( const std::string &  fname,
const std::vector< std::pair< uint32_t, float > > &  data 
)

Writes the BoW feature to the specified location.

First dimension of data is cluster index, second dimension is TF score.

Definition at line 53 of file filesystem.cxx.

Referenced by compute_bow(), compute_bow_features(), and main().

53  {
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  }
bool filesystem::write_text ( const std::string &  fname,
const std::string &  text 
)

Writes a text file to the input file location given the input string.

Returns true if success, false otherwise.

Definition at line 106 of file filesystem.cxx.

Referenced by MatchesPage::write().

106  {
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  }