-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFileHandler.cpp
More file actions
124 lines (108 loc) · 2.37 KB
/
FileHandler.cpp
File metadata and controls
124 lines (108 loc) · 2.37 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "FileHandler.h"
#include <fstream>
#include <algorithm>
#include <cctype>
#include "filefunctions.h"
#include <boost/filesystem/exception.hpp>
using namespace std;
CFileHandler::CFileHandler(const char* filename)
: filesize(-1),
ifs(0)
{
Init(filename);
}
CFileHandler::CFileHandler(std::string filename)
: filesize(-1),
ifs(0)
{
Init(filename.c_str());
}
void CFileHandler::Init(const char* filename)
{
string fnstr;
ifs = 0;
try {
fs::path fn(filename,fs::native);
fnstr = fn.native_file_string();
} catch (boost::filesystem::filesystem_error err) {
fnstr.clear();
}
if (!fnstr.empty())
{
ifs=new std::ifstream(fnstr.c_str(), ios::in|ios::binary);
if (ifs->is_open()) {
ifs->seekg(0, ios_base::end);
filesize = ifs->tellg();
ifs->seekg(0, ios_base::beg);
return;
}
delete ifs;
ifs = 0;
}
}
CFileHandler::~CFileHandler(void)
{
if(ifs)
delete ifs;
}
bool CFileHandler::FileExists()
{
return ifs;
}
void CFileHandler::Read(void* buf,int length)
{
if(ifs){
ifs->read((char*)buf,length);
}
}
void CFileHandler::Seek(int length)
{
if(ifs){
ifs->seekg(length);
}
}
int CFileHandler::Peek()
{
if(ifs){
return ifs->peek();
}
return EOF;
}
bool CFileHandler::Eof()
{
if(ifs){
return ifs->eof();
}
return true;
}
std::vector<std::string> CFileHandler::FindFiles(std::string pattern)
{
std::vector<fs::path> found;
std::string patternPath=pattern;
if(patternPath.find_last_of('\\')!=string::npos){
patternPath.erase(patternPath.find_last_of('\\')+1);
}
if(patternPath.find_last_of('/')!=string::npos){
patternPath.erase(patternPath.find_last_of('/')+1);
}
if(pattern.find('\\')==string::npos && pattern.find('/')==string::npos)
patternPath.clear();
std::string filter=pattern;
filter.erase(0,patternPath.length());
fs::path fn(patternPath,fs::native);
found = find_files(fn,filter);
std::vector<std::string> foundstrings;
for (std::vector<fs::path>::iterator it = found.begin(); it != found.end(); it++)
foundstrings.push_back(it->string());
//todo: get a real regex handler
while(filter.find_last_of('*')!=string::npos)
filter.erase(filter.find_last_of('*'),1);
// while(filter.find_last_of('.')!=string::npos)
// filter.erase(filter.find_last_of('.'),1);
std::transform(filter.begin(), filter.end(), filter.begin(), (int (*)(int))std::tolower);
return foundstrings;
}
int CFileHandler::FileSize()
{
return filesize;
}