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
|
#include <iostream>
#include <string>
#include <fstream>
#include <cstdio>
#include <regex>
#include <algorithm>
using namespace std;
using namespace std::tr1;
//user-declared functions
void strip_normal_comment(string &text);
void strip_comments_blanks(const string file_name);
string punctuation(string &text);
int main ()
{
strip_comments_blanks("Finali.txt");
return 0;
}
//------------------------------------------------------
//Name: void strip_normal_comment(string &text)
//Purpose: Remove a "normal comment" (//block) from text
//------------------------------------------------------
void strip_normal_comment(string &text)
{
//Does not continue if line is less than/equal to a character.
//The comment itself (//) has two characters
if (text.length() <= 1)
return;
for (size_t i = 0; i < text.length(); i++)
{
//If matches
if (text[i] == '/' && text[i + 1] == '/')
{
//Then erase everything starting with the position 'i'.
//This position represents the place of the first '/'
text.erase(i);
return;
}
}
}
string punctuation(string &text)
{
string rsmi, rcma, reql, rmin, rcln, rpar, rcpn, rmlt, rpls;
string smi, cma, eql, min, cln, par, cpn, mlt, pls;
regex osmi(";");
rsmi = " ; ";
smi = regex_replace(text, osmi, rsmi);
regex ocma(",");
rcma = " , ";
cma = regex_replace(smi, ocma, rcma);
regex oeql("=");
reql = " = ";
eql = regex_replace(cma, oeql, reql);
regex omin("-");
rmin = " - ";
min = regex_replace(eql, omin, rmin);
regex ocln(":");
rcln = " : ";
cln = regex_replace(min, ocln, rcln);
/*regex opar("(");
rpar = " ( ";
par = regex_replace(cln, opar, rpar);
regex ocpn(")");
rcpn = " ) ";
cpn = regex_replace(cln, ocpn, rcpn);
regex omlt("*");
rmlt = " * ";
mlt = regex_replace(cln, omlt, rmlt);
regex opls("+");
rpls = " + ";
pls = regex_replace(min, opls, rpls);*/
return cln;
//return pls;
}
//------------------------------------------------------
//Name: string trim(const string str, const string whitespace = " \t")
//Purpose: finds where the string starts and ends
//------------------------------------------------------
string trim(const string str, const string whitespace = " \t")
{
const auto strBegin = str.find_first_not_of(whitespace);
if (strBegin == std::string::npos)
return ""; // no content
const auto strEnd = str.find_last_not_of(whitespace);
const auto strRange = strEnd - strBegin + 1;
return str.substr(strBegin, strRange);
}
//------------------------------------------------------
//string reduce(const string str, const string fill = " ", const string whitespace = " \t")
//Purpose: Remove a "normal comment" (//block) from text
//------------------------------------------------------
string reduce(const string str, const string fill = " ", const string whitespace = " \t")
{
// trim first
auto result = trim(str, whitespace);
// replace sub ranges
auto beginSpace = result.find_first_of(whitespace);
while (beginSpace != std::string::npos)
{
const auto endSpace = result.find_first_not_of(whitespace, beginSpace);
const auto range = endSpace - beginSpace;
//shifts characters over to the left
result.replace(beginSpace, range, fill);
const auto newStart = beginSpace + fill.length();
beginSpace = result.find_first_of(whitespace, newStart);
}
return result;
}
//------------------------------------------------------
//Name: void strip_comments_blanks(const string file_name)
//Purpose: removes comments
//Functions called: void strip_normal_comment(string &text)
//------------------------------------------------------
void strip_comments_blanks(const string file_name)
{
//open the file, and check for existance
ifstream ifs;
ifs.open(file_name.c_str());
if(ifs.fail())
cout<< "Cannot find file " << file_name << "!\n";
else
{
ofstream created;
//result file (the one with no comments nor extra spaces) will
//be called uncommented_formatted-YourFileName.extension
string created_name = "uncommented_formatted-" + file_name;
created.open(created_name.c_str());
string line;
string temp;
size_t pos = 0;
size_t pos2 = 0;
bool matched_pos = false;
//infinite loop. a "break" inside this while will tell it when
//to stop.
while(true)
{
if (!matched_pos)
{
//Extract line, if not at end of file
if(getline(ifs, line))
{
temp = line;
strip_normal_comment(temp);
temp.erase(temp.begin(), find_if(temp.begin(), temp.end(), not1(ptr_fun<int, int>(isspace))));
}else
break;
}
if(temp.length() > 0)
{
created << reduce(punctuation(temp)) << '\n';
}
}
created.close();
}
ifs.close();//close file.
}
|