Guest User

Untitled

a guest
Oct 27th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <algorithm>
  2. #include <experimental/filesystem>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <optional>
  6. #include <string>
  7. #include <string_view>
  8. #include <sstream>
  9. #include <utility>
  10. #include <vector>
  11.  
  12. using namespace std;
  13. namespace fs = std::experimental::filesystem;
  14.  
  15. [[nodiscard]] auto split_string(const string_view& input, const char delimiter)
  16. {
  17.     stringstream ss {input.data()};
  18.     vector<string> result;
  19.     // result.reserve(count(begin(input), end(input), delimiter));
  20.    
  21.     for (string buffer;
  22.          getline(ss, buffer, delimiter);)
  23.             {result.push_back(move(buffer));}
  24.    
  25.     return result;
  26. }
  27.  
  28. [[nodiscard]] optional<int> get_target_column(ifstream& input,
  29.                                               const string_view& label,
  30.                                               const char delimiter)
  31. {
  32.     string first_line;
  33.    
  34.     if (getline(input, first_line); // Init-statement for if/switch
  35.         first_line.size() == 0) { throw runtime_error("Input file missing"); }
  36.     input.seekg(0);
  37.    
  38.     auto tokens = split_string(first_line, delimiter);
  39.        
  40.     if (auto it = find(begin(tokens), end(tokens), label); // Init-statement for if/switch
  41.         it == tokens.end()) {
  42.             return {}; //return empty optional
  43.     }
  44.     else {
  45.         return distance(begin(tokens), it);
  46.     }
  47. }
  48.  
  49. [[nodiscard]] auto get_file_handlers(const string_view& input, const string_view& output)
  50. {
  51.     ifstream in_file {input.data(), ios::in};
  52.     if (!in_file.is_open()) { throw runtime_error("Unable to open input file"); }
  53.    
  54.     ofstream out_file {output.data(), ios::out | ios::trunc};
  55.     if (!out_file.is_open()) { throw runtime_error("Unable to open output file");}
  56.    
  57.     return pair(move(in_file), move(out_file)); //Template argument deduction for class templates (no make_pair)
  58. }
  59.  
  60. void do_work(ifstream& input,
  61.              ofstream& output,
  62.              int target_index,
  63.              const string_view& new_value,
  64.              const char delimiter)
  65. {
  66.     string buffer;
  67.    
  68.     getline(input, buffer); // for the header line
  69.     output << buffer << endl;
  70.    
  71.     while (getline(input, buffer)) {
  72.         auto tokens = split_string(buffer, delimiter);
  73.         tokens[target_index] = new_value.data();
  74.        
  75.         for (auto& i: tokens) {
  76.             output << i;
  77.             output << (i == tokens.back() ? '\n':delimiter);
  78.         }
  79.     }
  80. }
  81.  
  82. [[noreturn]] void usage_terminate(const string_view& progname) noexcept
  83. {
  84.     cout << "Usage: " << progname << " [IN_FILE] [COLUMN] [NEW_VALUE] [OUT_FILE]" << endl;
  85.     abort();
  86. }
  87.  
  88. int main(int argc, char* argv[])
  89. {
  90.     try
  91.     {
  92.         if (argc != 5) { throw runtime_error("Bad arguments"); }
  93.  
  94.         auto [in_file, out_file] = get_file_handlers(argv[1], argv[4]);
  95.  
  96.         string_view new_value = argv[3];
  97.         auto target_index = get_target_column(in_file, argv[2], ',');
  98.         if (target_index) {
  99.             do_work(in_file, out_file, *target_index, new_value, ',');
  100.         }
  101.         else {
  102.             throw runtime_error("Column name doesn’t exist in the input file");
  103.         }
  104.     }
  105.     catch (runtime_error& e)
  106.     {
  107.         cout << e.what() << endl;
  108.         usage_terminate(argv[0]);
  109.     }
  110.    
  111.     return 0;
  112. }
Add Comment
Please, Sign In to add comment