Guest User

Untitled

a guest
Feb 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <fstream>
  4. #include <cstring>
  5. #include <sstream>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <iterator>
  9. #include <cctype>
  10.  
  11. void checkOpt(std::set<char>& s){
  12. //checking for conflicting flags
  13. if((s.find('q') != s.end()) && (s.find('s') != s.end() || s.find('c') != s.end())){
  14. std::cerr << "CONFLICTING FLAGS" << std::endl;
  15. std::exit(1);
  16. }
  17. //checking if all flags are valid
  18. static char const v[] = {'q','s','c','p','l'};
  19. std::set<char> valid(std::begin(v),std::end(v));
  20. for(std::set<char>::iterator it=s.begin(); it!= s.end(); ++it) {
  21. if(valid.find(*it) == valid.end()){
  22. std::cerr << '-' << *it << " INVALID FLAG" << std::endl;
  23. std::exit(1);
  24. }
  25. }
  26. }
  27.  
  28. bool is_realword(std::string s){
  29. for(std::string::iterator it = s.begin(); it != s.end(); ++it){
  30. int ascii = *it;
  31. if (!((ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122)))
  32. return false;
  33. }
  34. return true;
  35. }
  36.  
  37. std::string myReplaceAll(std::string str, const std::string& from, const std::string& to) {
  38. size_t start_pos = 0;
  39. while((start_pos = str.find(from, start_pos)) != std::string::npos) {
  40. int end_pos = start_pos + from.length();
  41. while(std::isspace(str[end_pos])) end_pos++;
  42. str.replace(start_pos, end_pos - start_pos, to);
  43. start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
  44. }
  45. return str;
  46. }
  47.  
  48. bool adjspc(char l, char r){return isspace(l)&&isspace(r);}
  49.  
  50. std::string manipulate_file(const char* fileName, std::set<char>& flags){
  51. std::ifstream ifile(fileName);
  52. std::string contents( (std::istreambuf_iterator<char>(ifile)),
  53. (std::istreambuf_iterator<char>()));
  54. ifile.close();
  55. if (flags.find('c') != flags.end()){
  56. if (flags.find('s') != flags.end()) contents.erase(std::unique(contents.begin(), contents.end(), adjspc),contents.end());
  57. std::vector<std::string> words;
  58. std::string word = "";
  59. for(std::string::iterator it = contents.begin(); it != contents.end(); ++it){
  60. if (!isspace(*it)){
  61. word += *it;
  62. } else {
  63. if (word.length()!=0) words.push_back(word);
  64. word.clear();
  65. }
  66. }
  67. for(std::vector<std::string>::iterator it = words.begin(); it != words.end(); ++it)
  68. if (!is_realword(*it))
  69. contents = myReplaceAll(contents, *it, "");
  70. } else if (flags.find('s') != flags.end()) {
  71. std::vector<std::string> lines;
  72. std::string line = "";
  73. for(std::string::iterator it = contents.begin(); it != contents.end(); ++it){
  74. if(*it != '\n') line += *it;
  75. else {
  76. if (line.length()!=0) {
  77. line.erase(std::unique(line.begin(), line.end(), adjspc),line.end());
  78. line.erase(line.find_last_not_of(" \t\n\r\f\v") + 1);
  79. lines.push_back(line);
  80. }
  81. line.clear();
  82. }
  83. }
  84. std::string out = "";
  85. for (std::vector<std::string>::iterator it = lines.begin(); it != lines.end(); ++it)
  86. out += *it + '\n';
  87. return out;
  88. } else if (flags.find('q') != flags.end()){
  89. contents.clear();
  90. }
  91. return contents;
  92. }
  93.  
  94. int main(int argc, char* argv[]){
  95. std::set<char> o;
  96. std::set<char> *optSet = &o; //holds the opts passed in by user
  97. const char* fileName; //holds filename
  98. int file_count = 0;
  99. for(int i = 1; i < argc; ++i){
  100. if (argv[i][0] == '-'){ //is it an opt?
  101. if(std::strlen(argv[i])==2){ //is the opt one character long?
  102. optSet->insert(argv[i][1]); //put it in the set of opts
  103. } else {
  104. std::cerr << argv[i] << " INVALID FLAG" << std::endl; //print bad flag
  105. std::exit(1); //exit program
  106. }
  107. } else { //if it's not an opt, it's a filename
  108. file_count += 1;
  109. fileName = argv[i];
  110. }
  111. }
  112. if (file_count > 1){
  113. std::cerr << "TOO MANY FILES" << std::endl;
  114. std::exit(1);
  115. }
  116. if(!(optSet->empty())) //check if there were any opts
  117. checkOpt(*optSet);//check if opts are valid and no conflicts
  118. if(fileName){
  119. std::ifstream infile(fileName); //make stream of file
  120. if(!infile.good()){ //check if file exists
  121. std::cerr << fileName << " CANNOT OPEN" << std::endl;
  122. std::exit(1);
  123. }
  124. infile.close();
  125. std::cout << manipulate_file(fileName, *optSet);
  126. }
  127. std::exit(0);
  128. }
Add Comment
Please, Sign In to add comment