-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfilefunc.h
89 lines (75 loc) · 3.06 KB
/
filefunc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#pragma once
#include <cstring>
#include <string>
#include <vector>
namespace filefunc
{
//read and write file
bool fileExist(const std::string& name);
bool pathExist(const std::string& name);
std::vector<char> readFile(const std::string& filename, int length = -1);
int writeFile(const char* data, int length, const std::string& filename);
int writeFile(const std::vector<char>& data, const std::string& filename);
std::string readFileToString(const std::string& filename);
int writeStringToFile(const std::string& str, const std::string& filename);
template <class T>
void readDataToVector(const char* data, int length, std::vector<T>& v, int length_one)
{
int count = length / length_one;
v.resize(count);
for (int i = 0; i < count; i++)
{
memcpy(&v[i], data + length_one * i, length_one);
}
}
template <class T>
void readDataToVector(const char* data, int length, std::vector<T>& v)
{
readDataToVector(data, length, v, sizeof(T));
}
template <class T>
void readFileToVector(const std::string& filename, std::vector<T>& v)
{
auto buffer = readFile(filename);
readDataToVector(buffer.data(), buffer.size(), v);
}
template <class T>
void writeVectorToData(char* data, int length, std::vector<T>& v, int length_one)
{
int count = length / length_one;
v.resize(count);
for (int i = 0; i < count; i++)
{
memcpy(data + length_one * i, &v[i], length_one);
}
}
template <class T>
int writeVectorToFile(const std::vector<T>& v, const std::string& filename)
{
return writeFile((const char*)v.data(), v.size() * sizeof(T), filename);
}
//other file operations
bool is_path_char(char c);
char get_path_char();
size_t getLastPathCharPos(const std::string& filename, int utf8 = 0);
size_t getLastEftPathCharPos(const std::string& filename, int utf8 = 0);
std::vector<std::string> getFilesInPath(const std::string& pathname, int recursive = 0, int include_path = 0, int absolute_path = 0);
std::string getFileTime(const std::string& filename, const std::string& format = "%Y-%m-%d %H:%M:%S");
void changePath(const std::string& path);
std::string getCurrentPath();
void makePath(const std::string& path);
void copyFile(const std::string& src, const std::string& dst);
void removeFile(const std::string& filename);
std::string getRelativePath(const std::string& filename, const std::string& basepath);
//functions about file name
std::string getFileExt(const std::string& filename);
std::string getFileMainName(const std::string& filename);
std::string getFilenameWithoutPath(const std::string& filename);
std::string getFileMainNameWithoutPath(const std::string& filename);
std::string changeFileExt(const std::string& filename, const std::string& ext);
std::string getParentPath(const std::string& filename, int utf8 = 0); //utf8 has no effect on non-win32
std::string getFilePath(const std::string& filename, int utf8 = 0);
std::string toLegalFilename(const std::string& filename, int allow_path = 1);
std::string getAbsolutePath(const std::string& filename);
bool compareNature(const std::string& a, const std::string& b);
} //namespace filefunc