SF2 C++ FRC Class Library
Sensor Fusion Framework (SF2) for FRC
File.h
1 /* ============================================
2  SF2 source code is placed under the MIT license
3  Copyright (c) 2017 Kauai Labs
4 
5  Permission is hereby granted, free of charge, to any person obtaining a copy
6  of this software and associated documentation files (the "Software"), to deal
7  in the Software without restriction, including without limitation the rights
8  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  copies of the Software, and to permit persons to whom the Software is
10  furnished to do so, subject to the following conditions:
11 
12  The above copyright notice and this permission notice shall be included in
13  all copies or substantial portions of the Software.
14 
15  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  THE SOFTWARE.
22  ===============================================
23  */
24 
25 #ifndef SRC_PLATFORM_FILE_H_
26 #define SRC_PLATFORM_FILE_H_
27 
28 #include <forward_list>
29 #include <string>
30 
31 #include <dirent.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <iostream>
36 #include <fstream>
37 
38 using namespace std;
39 
40 class File;
41 
43 public:
44  virtual bool accept(File& dir, const string& name) = 0;
45  virtual ~FilenameFilter() {
46  }
47 };
48 
49 class File {
50  std::string path;
51 
52 public:
53  static const char separatorChar = '/';
54 
55  File(std::string path /* to either a directory or a file */) {
56  this->path = path;
57  }
58 
59  bool isDirectory() {
60  struct stat st;
61  if (stat(path.c_str(), &st) == 0) {
62  if (S_ISDIR(st.st_mode)) {
63  return true;
64  }
65  }
66  return false;
67  }
68 
69  bool canWrite() {
70  struct stat st;
71  if (stat(path.c_str(), &st) == 0) {
72  if ((st.st_mode & S_IFDIR) != 0) {
73  return true;
74  }
75  }
76  return false;
77  }
78 
79  string getName() {
80  std::string base_filename;
81  std::string::size_type const o(path.find_last_of("/\\"));
82  if (o != std::string::npos) {
83  base_filename = path.substr(o + 1);
84  }
85 
86  std::string::size_type const p(base_filename.find_last_of('.'));
87  if (p != std::string::npos) {
88  std::string file_without_extension = base_filename.substr(0, p);
89  return file_without_extension;
90  } else {
91  return base_filename;
92  }
93  }
94 
95  void listFiles(forward_list<File *>& list, FilenameFilter& filter) {
96  DIR *dpdf;
97  struct dirent *epdf;
98  dpdf = opendir(path.c_str());
99  if (dpdf != NULL) {
100  while ((epdf = readdir(dpdf))) {
101  printf("Filename: %s", epdf->d_name);
102  string fname(epdf->d_name);
103  if (filter.accept(*this, fname)) {
104  File *p_new_file = new File(epdf->d_name);
105  list.insert_after(list.end(), p_new_file);
106  }
107  }
108  closedir(dpdf);
109  }
110  }
111 
112 };
113 
114 class PrintWriter {
115  ofstream out;
116 public:
117 
118  PrintWriter(const string& file_path) :
119  out(file_path.c_str()) {
120  }
121 
122  void println(const string& line) {
123  if (out) {
124  out << line << "\n";
125  }
126  }
127  void close() {
128  if (out) {
129  out.close();
130  }
131  }
132 
133  ~PrintWriter() {
134  close();
135  }
136 };
137 
138 #endif /* SRC_PLATFORM_FILE_H_ */
Definition: File.h:49
Definition: File.h:42
Definition: File.h:114