Leosac  0.8.0
Open Source Access Control
unixfs.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
26 #ifndef UNIXFS_HPP
27 #define UNIXFS_HPP
28 
29 #include <fstream>
30 #include <list>
31 #include <string>
32 
34 
35 namespace Leosac
36 {
37 namespace Tools
38 {
39 class UnixFs
40 {
41  UnixFs() = delete;
42 
43  public:
44  using FileList = std::list<std::string>;
45 
46  public:
51  static std::string getCWD();
52 
59  static FileList listFiles(const std::string &folder,
60  const std::string &extension = std::string());
61 
67  static std::string stripPath(const std::string &filename);
68 
74  static std::string readAll(const std::string &path);
75 
80  static bool fileExists(const std::string &path);
81 
87  template <typename T>
88  static T readSysFsValue(const std::string &path)
89  {
90  std::ifstream file(path);
91  T val;
92 
93  if (!file.good())
94  throw(FsException("could not open \'" + path + '\''));
95  file >> val;
96  file.clear();
97  file.seekg(0);
98  return (val);
99  }
100 
106  template <typename T>
107  static void writeSysFsValue(const std::string &path, const T &val)
108  {
109  std::ofstream file(path);
110 
111  if (!file.good())
112  throw(FsException("could not open " + path + '\''));
113  file << val;
114  file.clear();
115  file.seekp(0);
116  }
117 };
118 }
119 }
120 
121 #endif // UNIXFS_HPP
Leosac::Tools::UnixFs::readSysFsValue
static T readSysFsValue(const std::string &path)
read value from a sysfs file
Definition: unixfs.hpp:88
Leosac::Tools::UnixFs::listFiles
static FileList listFiles(const std::string &folder, const std::string &extension=std::string())
list all files with the extension ".extension" in folder
Definition: unixfs.cpp:55
Leosac::Tools::UnixFs::UnixFs
UnixFs()=delete
Leosac::Tools::UnixFs::FileList
std::list< std::string > FileList
Definition: unixfs.hpp:44
Leosac::Tools::UnixFs::getCWD
static std::string getCWD()
get current working directory without trailing slash
Definition: unixfs.cpp:40
Leosac::Tools::UnixFs::readAll
static std::string readAll(const std::string &path)
read all file contents and put it in a string
Definition: unixfs.cpp:98
Leosac::Tools::UnixFs::stripPath
static std::string stripPath(const std::string &filename)
remove the full path from a filename
Definition: unixfs.cpp:88
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
FsException
Definition: fsexception.hpp:33
Leosac::Tools::UnixFs::writeSysFsValue
static void writeSysFsValue(const std::string &path, const T &val)
write value to a sysfs file
Definition: unixfs.hpp:107
Leosac::Tools::UnixFs
Definition: unixfs.hpp:39
fsexception.hpp
Exception class for filesystem related errors.
Leosac::Tools::UnixFs::fileExists
static bool fileExists(const std::string &path)
Definition: unixfs.cpp:109