Caffe2 - C++ API
A deep learning, cross platform ML framework
utils.h
1 #pragma once
2 #include <fstream>
3 
4 #include "caffe2/core/logging.h"
5 
6 namespace caffe2 {
7 namespace emulator {
8 
9 /*
10  * Replace a @substring in a given @line with @target
11  */
12 inline std::string replace(
13  std::string line,
14  const std::string& substring,
15  const std::string& target) {
16  size_t index = 0;
17  while (true) {
18  index = line.find(substring, index);
19  if (index == std::string::npos) {
20  break;
21  }
22  line.replace(index, substring.length(), target);
23  index += substring.length();
24  }
25  return line;
26 }
27 
28 /*
29  * Split given @str into a vector of strings delimited by @delim
30  */
31 inline std::vector<std::string> split(const string& str, const string& delim) {
32  std::vector<std::string> tokens;
33  size_t prev = 0, pos = 0;
34  do {
35  pos = str.find(delim, prev);
36  if (pos == std::string::npos) {
37  pos = str.length();
38  }
39  std::string token = str.substr(prev, pos - prev);
40  if (!token.empty()) {
41  tokens.push_back(token);
42  }
43  prev = pos + delim.length();
44  } while (pos < str.length() && prev < str.length());
45  return tokens;
46 }
47 
48 /*
49  * Check if the given @path is valid.
50  * Remove the file/folder if @remove is specified
51  */
52 inline bool check_path_valid(std::string path, bool remove = true) {
53  CAFFE_ENFORCE(!path.empty());
54  std::ifstream file(path.c_str());
55  // The file should exist or the path is valid
56  if (!file.good() && !static_cast<bool>(std::ofstream(path).put('t'))) {
57  return false;
58  }
59  file.close();
60  if (remove) {
61  std::remove(path.c_str());
62  }
63  return true;
64 }
65 
66 } // namespace emulator
67 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13