-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextPurifier.cpp
196 lines (160 loc) · 4.93 KB
/
TextPurifier.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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
using namespace std;
void listDir(vector<string> &filesInDirectory);
string get_file_contents(const char *filename);
bool invalidChar (char c);
void stripUnicode(string &str);
string data = "";
int main()
{
vector<string> filesInCutGameDirectory;
vector<string> filesInAsciiDirectory;
vector<string> filesInExtractedGameDirectory;
//make directory return values
int mkdirRet;
int chdirRet;
//get cwd
char* cwd;
char buffer[PATH_MAX];
cwd = getcwd(buffer, PATH_MAX);
string currentDirectory(cwd);
//create directory strings
string masterDirectory = currentDirectory + "/ESPN_HTML_Cache";
string cutDownGameDirectory = masterDirectory + "/Cut_Down_Game_Files";
string asciiDirectory = masterDirectory + "/Ascii_Cut_Game_Files";
string extractedGameDirectory = masterDirectory + "/Data_Extracted_Files";
//convert directory strings to const char for use in mkdir
const char* cMasterDirectory = masterDirectory.c_str();
const char* cCutDownGameDirectory = cutDownGameDirectory.c_str();
const char* cAsciiDirectory = asciiDirectory.c_str();
const char* cExtractedGameDirectory = extractedGameDirectory.c_str();
//create directoies incase they dont exist.
mkdirRet = mkdir(cMasterDirectory, S_IRWXU | S_IRWXG | S_IRWXO);
if (mkdirRet == -1)
cout << "\"" << cMasterDirectory << "\" already exists!" << endl;
mkdirRet = mkdir(cCutDownGameDirectory, S_IRWXU | S_IRWXG | S_IRWXO);
if (mkdirRet == -1)
cout << "\"" << cCutDownGameDirectory << "\" already exists!" << endl;
mkdirRet = mkdir(cAsciiDirectory, S_IRWXU | S_IRWXG | S_IRWXO);
if (mkdirRet == -1)
cout << "\"" << cAsciiDirectory << "\" already exists!" << endl;
mkdirRet = mkdir(cExtractedGameDirectory, S_IRWXU | S_IRWXG | S_IRWXO);
if (mkdirRet == -1)
cout << "\"" << cExtractedGameDirectory << "\" already exists!" << endl;
//change to cut_game directory
chdirRet = chdir(cCutDownGameDirectory);
if (chdirRet == -1)
{
cout << "Could not change directory! Quitting." << endl;
return 1;
}
//list all files in cut_game directory
listDir(filesInCutGameDirectory);
//change to cut_game directory
chdirRet = chdir(cExtractedGameDirectory);
if (chdirRet == -1)
{
cout << "Could not change directory! Quitting." << endl;
return 1;
}
//list all files in cut_game directory
listDir(filesInExtractedGameDirectory);
//change to ascii_game directory
chdirRet = chdir(cAsciiDirectory);
if (chdirRet == -1)
{
cout << "Could not change directory! Quitting." << endl;
return 1;
}
//list all files in ascii_game directory
listDir(filesInAsciiDirectory);
//change to cut game directory
chdirRet = chdir(cCutDownGameDirectory);
if (chdirRet == -1)
{
cout << "Could not change directory! Quitting." << endl;
return 1;
}
for(size_t i = 2; i < filesInCutGameDirectory.size(); i++)
{
string gameID = filesInCutGameDirectory[i];
gameID = gameID.substr(4);
string AsciiGameID = "Ascii_" + gameID;
bool skip_bool = false;
if (find(filesInAsciiDirectory.begin(), filesInAsciiDirectory.end(), AsciiGameID) != filesInAsciiDirectory.end())
{
skip_bool = true;
}
if (find(filesInExtractedGameDirectory.begin(), filesInExtractedGameDirectory.end(), gameID) != filesInExtractedGameDirectory.end())
{
skip_bool = true;
}
if (skip_bool)
{
//Skip This File
}
else
{
data = "";
const char* fileName = filesInCutGameDirectory[i].c_str();
data = get_file_contents(fileName);
//strip all non unicode characters
stripUnicode(data);
string tempString = asciiDirectory + "/" + AsciiGameID;
const char* cTempString = tempString.c_str();
fstream fileInput;
fileInput.open (cTempString, std::fstream::in | std::fstream::out | std::fstream::app);
fileInput << data;
fileInput.close();
}
}
}
void listDir(vector<string> &filesInDirectory)
{
DIR *d;
struct dirent *dir;
d = opendir(".");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
filesInDirectory.push_back(dir->d_name);
}
closedir(d);
}
}
string get_file_contents(const char *filename)
{
ifstream in(filename, ios::in);
if (in)
{
string contents;
in.seekg(0, ios::end);
contents.resize(in.tellg());
in.seekg(0, ios::beg);
in.read(&contents[0], contents.size());
in.close();
return contents;
}
throw(errno);
}
bool invalidChar (char c)
{
return !(c >= 0 && c < 128);
}
void stripUnicode(string &str)
{
str.erase(remove_if(str.begin(),str.end(), invalidChar), str.end());
}
//non ascii striping of strings found at "http://stackoverflow.com/questions/10178700/c-strip-non-ascii-characters-from-string"
//listDir algorithm "http://stackoverflow.com/questions/4204666/how-to-list-files-in-a-directory-in-a-c-program"
//get_file_contents algorithm "http://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html"