-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfileManager.cpp
202 lines (173 loc) · 5.94 KB
/
fileManager.cpp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* fileManager.cpp
* Copyright (C) 2013 uqcskenn <uqcskenn@hawke>
*
* Distributed under terms of the MIT license.
*/
#include "fileManager.h"
#include "util.h"
#include <cassert>
#include <iostream>
FileWrapper::FileWrapper() {
file = NULL;
filename = "";
fileOpened = false;
closeAtEnd = false;
recordsWritten = 0;
}
FileWrapper::FileWrapper(std::string filename) {
file = NULL;
filename = filename;
fileOpened = false;
closeAtEnd = false;
recordsWritten = 0;
}
int FileWrapper::open() {
if(!fileOpened) {
if(recordsWritten) {
file = fopen(filename.c_str(), "a");
} else {
file = fopen(filename.c_str(), "w");
}
}
if (file == NULL) {
perror("problem opening file");
return 1;
}
fileOpened = true;
return 0;
}
void FileWrapper::close() {
if(fileOpened & closeAtEnd) {
fclose(file);
fileOpened = false;
}
}
FileWrapper::~FileWrapper() {
close();
}
FileManager::FileManager() {
openCount = 0;
}
FileManager::~FileManager() {
std::vector<FileWrapper *>::iterator iter2;
for(iter2 = files.begin(); iter2 != files.end(); ++iter2) {
if(*iter2 != NULL) {
delete *iter2;
*iter2 = NULL;
}
}
}
void FileManager::add(std::string pattern, std::string filename) {
int ret;
// check if the pattern has been seen before
std::map<std::string, int>::iterator pm_iter = patternMapping.find(pattern);
std::map<std::string, int>::iterator fp_iter;
if(pm_iter == patternMapping.end()) {
// add in the pattern key to the hash
patternMapping[pattern] = -1;
// now check to see if the filename has been seen before
fp_iter = filenameMapping.find(filename);
if (fp_iter == filenameMapping.end()) {
// it hasn't so make a new filewrapper and associate the
// pattern and the filename with it's index in the vector
FileWrapper * fw = new FileWrapper(filename);
// since the size value will always be one more than the index
// after we push on the filewrapper below the index will match
int n = files.size();
patternMapping[pattern] = n;
filenameMapping[filename] = n;
fw->filename = filename;
fw->closeAtEnd = true;
files.push_back(fw);
} else {
// the file has been seen before but the pattern hasn't
// so we need to associate the new pattern with the file
patternMapping[pattern] = fp_iter->second;
}
assert(patternMapping[pattern] != -1);
} else {
// the pattern is known
// check to see if the filename is also known
fp_iter = filenameMapping.find(filename);
if (fp_iter != filenameMapping.end() && filename != fp_iter->first) {
// same pattern different filename
// error in mapping file
std::cerr << "[WARNING]: The pattern "<<pattern<<" is associated with both "<<filename<<" and "<<fp_iter->first<<std::endl;
}
}
}
void FileManager::add(std::string pattern) {
std::map<std::string, int>::iterator fp_iter;
// check if the pattern has been seen before
std::map<std::string, int>::iterator pm_iter = patternMapping.find(pattern);
if(pm_iter == patternMapping.end()) {
// add in the pattern key to the hash
patternMapping[pattern] = -1;
fp_iter = filenameMapping.find("");
if (fp_iter == filenameMapping.end()) {
// it hasn't so make a new filewrapper and associate the
// pattern and the filename with it's index in the vector
FileWrapper * fw = new FileWrapper("");
// since the size value will always be one more than the index
// after we push on the filewrapper below the index will match
int n = files.size();
patternMapping[pattern] = n;
filenameMapping[""] = n;
fw->filename = "";
fw->closeAtEnd = false;
fw->file = stdout;
fw->fileOpened = true;
files.push_back(fw);
} else {
// the file has been seen before but the pattern hasn't
// so we need to associate the new pattern with the file
patternMapping[pattern] = filenameMapping[""];
}
assert(patternMapping[pattern] != -1);
} else {
// the pattern is known
// check to see if the filename is also known
fp_iter = filenameMapping.find("");
if (fp_iter == filenameMapping.end() || "" != fp_iter->first) {
// same pattern different filename
// error in mapping file
fprintf(stderr, "[WARNING]: The pattern \"%s\" is associated with both \"%s\" and \"%s\"\n",
pattern.c_str(),
"(stdout)",
fp_iter->first.c_str());
}
}
}
FILE * FileManager::find (std::string key) {
FileWrapper * fw;
std::map<std::string, int>::iterator iter = patternMapping.find(key);
if(iter == patternMapping.end()) {
//fprintf(stderr, "[ERROR]: The pattern \"%s\" is not found in the mapping\n", key.c_str());
return NULL;
}
fw = files[iter->second];
if(fw->fileOpened) {
++fw->recordsWritten;
return fw->file;
} else if (openCount >= OPEN_MAX) {
// we're above the max number of allowed open files
// pop the first one off the queue and close it
FileWrapper * fo = closingQueue.top();
fo->close();
--openCount;
closingQueue.pop();
}
int ret = fw->open();
if(ret) {
return NULL;
}
if(fw->file != stdout) {
// sdtout doesn't really count as it is implicitly opened for the process
// so we don't want to add it into our queue
closingQueue.push(fw);
++openCount;
}
++fw->recordsWritten;
return fw->file;
}