~
[scx.git] / include / FileHelp.hpp
blob25ad739bd6d4f7227bc042a250feaf4b5b89798b
1 #ifndef SCX_FILEHELP_HPP
2 #define SCX_FILEHELP_HPP
4 #include <inttypes.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fstream>
9 using namespace std;
11 namespace scx
13 static inline size_t FileSize(const char* path)
15 struct stat statbuf;
16 stat(path, &statbuf);
17 return statbuf.st_size;
20 static inline bool CreateFile(const char* path, uint64_t size)
22 ofstream stream(path, ios::out);
23 stream.seekp(size-1, ios::beg);
24 stream.put('\0');
25 stream.close();
26 return (FileSize(path) == size) ? true : false;
29 static inline string FileSuffix(const string& name, char ch = '.')
31 size_t pos = name.find_last_of(ch);
32 return name.substr(pos+1, name.size());
35 static inline string FileDir(const string& path)
37 size_t pos = path.find_last_of('/');
38 if (pos == string::npos) {
39 return "./";
40 } else if (pos == path.size()-1){
41 return path.substr(0, path.size());
42 } else {
43 return path.substr(0, pos+1);
49 #endif