#include <string>
#include <vector>
#include <iterator>
#include <iostream>
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <boost/regex.hpp>
#include <boost/filesystem.hpp>
using namespace std;
typedef std::vector < std::string > stringVector;
void getFileList(const string& pathName, stringVector &recusiveFileVec)
{
boost::filesystem::recursive_directory_iterator rdi(pathName);
boost::filesystem::recursive_directory_iterator end_rdi;
recusiveFileVec.empty();
for (; rdi != end_rdi; rdi++) {
if (is_directory(*rdi)) {
std::cout << *rdi << "is pathName" << std::endl;
} else {
recusiveFileVec.push_back(rdi->path().string());
std::cout << *rdi << " is a file" << std::endl;
}
}
}
int processFile( const string &filePathName, const char* directory)
{
boost::regex pathRegex(directory);
std::string fmt("");
std::string path = regex_replace(filePathName, pathRegex, fmt);
stringVector filePathVec;
boost::split(filePathVec, path, boost::is_any_of("/"));
int size = filePathVec.size();
string category;
if (size > 2) {
category = "__" + filePathVec[size - 3] + "__" + filePathVec[size - 2];
} else if (size == 2) {
category = string("__") + filePathVec[size - 2];
} else {
category = "";
}
string tempFileName = filePathName + string(".bak");
std::ifstream inputFile(filePathName.c_str());
std::ofstream outfile(tempFileName.c_str());
if (inputFile.is_open()) {
while (inputFile.good()) {
string line;
getline(inputFile, line);
stringVector lineVec;
boost::split(lineVec, line, boost::is_any_of("\t"));
if (lineVec.size() < 2) {
//cout << "This line only has one segment by '\t'";
continue;
}
else {
stringVector titleVec;
boost::algorithm::split_regex( titleVec, lineVec[0], boost::regex( " [-|&] " )) ;
string outString;
size = titleVec.size();
cout << "size = " << size << endl;
if (size == 1) {
outString = titleVec[0] + lineVec[1] + category;
cout << "outString = " << outString << endl;
outfile << outString << endl;
}
else if (size == 2) {
outString = titleVec[0] + string("##") + lineVec[1] + line + category;
cout << "outString = " << outString << endl;
outfile << outString << endl;
outString = titleVec[1] + string("##") + lineVec[1] + line + category;
cout << "outString = " << outString << endl;
outfile << outString << endl;
}
}
}
} else {
cout << "Unable to open file";
return -1;
}
inputFile.close();
outfile.close();
// boost::filesystem::remove(filePath.c_str());
// boost::filesystem::rename(tempfile.c_str(), filePath.c_str());
return 1;
}
int main(int argc, char *argv[])
{
if (argc < 2) {
cout << "Invalid arg --- You should input a Directory path as an argument.(ex: /home/hello" << endl;
return -1;
}
stringVector fileList;
getFileList(argv[1], fileList);
for (int i = 0; i < fileList.size(); i++) {
processFile(fileList[i], argv[1]);
cout<<fileList[i]<<endl;
}
}