Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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;
- }
- }
- }
- //------------------------------------------------------
- //Name: string punctuation(string &text)
- //Purpose: Use Regex to format punction.
- //Note: (,),*,+,and . are all special characters that need []
- // brackets around them when in use.
- //------------------------------------------------------
- string punctuation(string &text)
- {
- string rsmi, rcma, reql, rmin, rcln, rpar, rcpn, rmlt, rpls, rper;
- string smi, cma, eql, min, cln, par, cpn, mlt, pls, per;
- 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(par, ocpn, rcpn);
- regex omlt("[*]");
- rmlt = " * ";
- mlt = regex_replace(cpn, omlt, rmlt);
- regex opls("[+]");
- rpls = " + ";
- pls = regex_replace(mlt, opls, rpls);
- regex oper("[.]");
- rper = ". ";
- per = regex_replace(pls, opls, rpls);
- return per;
- }
- //------------------------------------------------------
- //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.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement