Advertisement
Tuta816

Untitled

Dec 13th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <cstdio>
  5. #include <regex>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9. using namespace std::tr1;
  10.  
  11. //user-declared functions
  12. void strip_normal_comment(string &text);
  13. void strip_comments_blanks(const string file_name);
  14. string punctuation(string &text);
  15.  
  16. int main ()
  17. {
  18. strip_comments_blanks("Finali.txt");
  19. return 0;
  20. }
  21.  
  22. //------------------------------------------------------
  23. //Name: void strip_normal_comment(string &text)
  24. //Purpose: Remove a "normal comment" (//block) from text
  25. //------------------------------------------------------
  26. void strip_normal_comment(string &text)
  27. {
  28. //Does not continue if line is less than/equal to a character.
  29. //The comment itself (//) has two characters
  30. if (text.length() <= 1)
  31. return;
  32.  
  33. for (size_t i = 0; i < text.length(); i++)
  34. {
  35. //If matches
  36. if (text[i] == '/' && text[i + 1] == '/')
  37. {
  38. //Then erase everything starting with the position 'i'.
  39. //This position represents the place of the first '/'
  40. text.erase(i);
  41. return;
  42. }
  43. }
  44. }
  45.  
  46. //------------------------------------------------------
  47. //Name: string punctuation(string &text)
  48. //Purpose: Use Regex to format punction.
  49. //Note: (,),*,+,and . are all special characters that need []
  50. // brackets around them when in use.
  51. //------------------------------------------------------
  52. string punctuation(string &text)
  53. {
  54. string rsmi, rcma, reql, rmin, rcln, rpar, rcpn, rmlt, rpls, rper;
  55. string smi, cma, eql, min, cln, par, cpn, mlt, pls, per;
  56.  
  57. regex osmi(";");
  58. rsmi = " ; ";
  59. smi = regex_replace(text, osmi, rsmi);
  60.  
  61. regex ocma(",");
  62. rcma = " , ";
  63. cma = regex_replace(smi, ocma, rcma);
  64.  
  65. regex oeql("=");
  66. reql = " = ";
  67. eql = regex_replace(cma, oeql, reql);
  68.  
  69. regex omin("-");
  70. rmin = " - ";
  71. min = regex_replace(eql, omin, rmin);
  72.  
  73. regex ocln(":");
  74. rcln = " : ";
  75. cln = regex_replace(min, ocln, rcln);
  76.  
  77. regex opar("[(]");
  78. rpar = " ( ";
  79. par = regex_replace(cln, opar, rpar);
  80.  
  81. regex ocpn("[)]");
  82. rcpn = " ) ";
  83. cpn = regex_replace(par, ocpn, rcpn);
  84.  
  85. regex omlt("[*]");
  86. rmlt = " * ";
  87. mlt = regex_replace(cpn, omlt, rmlt);
  88.  
  89. regex opls("[+]");
  90. rpls = " + ";
  91. pls = regex_replace(mlt, opls, rpls);
  92.  
  93. regex oper("[.]");
  94. rper = ". ";
  95. per = regex_replace(pls, opls, rpls);
  96.  
  97. return per;
  98. }
  99.  
  100. //------------------------------------------------------
  101. //Name: string trim(const string str, const string whitespace = " \t")
  102. //Purpose: finds where the string starts and ends
  103. //------------------------------------------------------
  104. string trim(const string str, const string whitespace = " \t")
  105. {
  106. const auto strBegin = str.find_first_not_of(whitespace);
  107.  
  108. if (strBegin == std::string::npos)
  109. return ""; // no content
  110.  
  111. const auto strEnd = str.find_last_not_of(whitespace);
  112. const auto strRange = strEnd - strBegin + 1;
  113.  
  114. return str.substr(strBegin, strRange);
  115. }
  116.  
  117. //------------------------------------------------------
  118. //string reduce(const string str, const string fill = " ", const string whitespace = " \t")
  119. //Purpose: Remove a "normal comment" (//block) from text
  120. //------------------------------------------------------
  121. string reduce(const string str, const string fill = " ", const string whitespace = " \t")
  122. {
  123. // trim first
  124. auto result = trim(str, whitespace);
  125.  
  126. // replace sub ranges
  127. auto beginSpace = result.find_first_of(whitespace);
  128. while (beginSpace != std::string::npos)
  129. {
  130. const auto endSpace = result.find_first_not_of(whitespace, beginSpace);
  131. const auto range = endSpace - beginSpace;
  132.  
  133. //shifts characters over to the left
  134. result.replace(beginSpace, range, fill);
  135.  
  136. const auto newStart = beginSpace + fill.length();
  137. beginSpace = result.find_first_of(whitespace, newStart);
  138. }
  139.  
  140. return result;
  141. }
  142.  
  143. //------------------------------------------------------
  144. //Name: void strip_comments_blanks(const string file_name)
  145. //Purpose: removes comments
  146. //Functions called: void strip_normal_comment(string &text)
  147. //------------------------------------------------------
  148. void strip_comments_blanks(const string file_name)
  149. {
  150. //open the file, and check for existance
  151. ifstream ifs;
  152. ifs.open(file_name.c_str());
  153.  
  154. if(ifs.fail())
  155. cout<< "Cannot find file " << file_name << "!\n";
  156. else
  157. {
  158. ofstream created;
  159. //result file (the one with no comments nor extra spaces) will
  160. //be called uncommented_formatted-YourFileName.extension
  161.  
  162. string created_name = "uncommented_formatted-" + file_name;
  163. created.open(created_name.c_str());
  164.  
  165. string line;
  166. string temp;
  167. size_t pos = 0;
  168. size_t pos2 = 0;
  169.  
  170. bool matched_pos = false;
  171.  
  172. //infinite loop. a "break" inside this while will tell it when
  173. //to stop.
  174.  
  175. while(true)
  176. {
  177. if (!matched_pos)
  178. {
  179. //Extract line, if not at end of file
  180. if(getline(ifs, line))
  181. {
  182. temp = line;
  183. strip_normal_comment(temp);
  184. temp.erase(temp.begin(), find_if(temp.begin(), temp.end(), not1(ptr_fun<int, int>(isspace))));
  185. }else
  186. break;
  187. }
  188.  
  189. if(temp.length() > 0)
  190. {
  191. created << reduce(punctuation(temp)) << '\n';
  192. }
  193. }
  194. created.close();
  195. }
  196. ifs.close();//close file.
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement