Advertisement
Guest User

C++/D

a guest
Jul 20th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.80 KB | None | 0 0
  1. /*
  2. Hi!
  3. I have written a little tool in c++ and would like to get some tips how to improve it.
  4. It reads a file and splits it in columns. Then it replaces the appearance of source column with target column in a target file.
  5. As comparison I also have written the same tool in D.
  6. Now i want to know how i can get the c++ version as fast an clean as the D version.
  7.  
  8. c++:
  9. */
  10. #include <algorithm>
  11. #include <fstream>
  12. #include <iostream>
  13. #include <vector>
  14. #include <string>
  15. #include <sstream>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <getopt.h>
  20.  
  21. using namespace std;
  22.  
  23. std::vector<string> split(const std::string& s,char delimiter){
  24.    std::vector<string> res;
  25.    std::istringstream stream(s);
  26.    std::string tok;
  27.    while(getline(stream,tok,delimiter)){
  28.       if(!tok.empty()){
  29.          res.push_back(tok);
  30.       }
  31.    }
  32.    return res;
  33. }
  34.  
  35. void replaceInLine(std::string& str,const string& from,const string& to){
  36.  if(from.empty())
  37.         return;
  38.     size_t start_pos = 0;
  39.     while((start_pos = str.find(from, start_pos)) != std::string::npos) {
  40.         str.replace(start_pos, from.length(), to);
  41.         start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
  42.     }
  43. }
  44.  
  45. int main(int argc,char** argv){
  46.    
  47.    int srcColumn = 0;
  48.    int targetColumn = 1;
  49.    int option_index;
  50.    static struct option long_options[] = {
  51.             {"sourceColumn",     required_argument,0 ,  's' },
  52.             {"targetColumn",required_argument,0, 't'  }
  53.         };
  54.    int c;
  55.    string usage = R"eof(
  56. usage: source target [options] source target
  57. options:
  58.   --sourceColumn -s Column which gets replaced
  59.   --targetColumn -t Column which replaces source
  60.  
  61. Parameter:
  62.   source: file from which the apperance of column 1 in file tagret file
  63.        gets replaced with column 2 of that file.
  64.   target: file in wicht apperance of column 1 gets replaced with column 2
  65.   )eof";
  66.    while((c=  getopt_long(argc, argv, "s:t:?h",long_options, &option_index)) != -1){
  67.       switch(c){
  68.          case 's':
  69.               srcColumn = atoi(optarg);
  70.               break;
  71.          case 't':
  72.               targetColumn = atoi(optarg);
  73.               break;
  74.          case '?':
  75.          case 'h':
  76.          default:
  77.               cout << usage << endl;
  78.               return -1;
  79.       }
  80.    }
  81.    
  82.    ifstream file(argv[argc-2]);
  83.    ifstream orig(argv[argc-1]);
  84.    if(!file || ! orig){
  85.       cout << "couldn't open file!" << endl;
  86.       return -1;
  87.    }
  88.    std::vector<std::vector<string>> columns;
  89.    string line;
  90.    while(getline(file,line)){
  91.       columns.push_back(split(line,' '));
  92.    }
  93.    while(getline(orig,line)){
  94.       for(auto rep: columns){
  95.          if (rep.size()> max(srcColumn,targetColumn)){
  96.             replaceInLine(line,rep[srcColumn],rep[targetColumn]);
  97.          }
  98.       }
  99.       cout << line << endl;
  100.    }
  101. }
  102.  
  103. /*
  104. D:
  105. import std.stdio;
  106. import std.string;
  107. import std.conv;
  108. import std.algorithm;
  109. import std.getopt;
  110.  
  111. int main(string[] args){
  112.     int srcColumn = 0;
  113.     int targetColumn = 1;
  114.     auto res = getopt(args, "s|sourceColumn","column which replaces tagert column", &srcColumn,
  115.                            "t|targetColumn", "column which gets replaced",&targetColumn);
  116.     if(res.helpWanted){
  117. options:
  118.    defaultGetoptPrinter(
  119. r"usage: [options] source target
  120. options:
  121. ",res.options);
  122. return -1;
  123.     }
  124.     auto f = File(args[$-2]);
  125.     auto orig = File(args[$-1]);
  126.     string[][] columns;
  127.     foreach(line;f.byLine()){
  128.        columns ~= to!string(line).split();
  129.     }
  130.     foreach(line;orig.byLine()){
  131.        foreach(rep;columns){
  132.           if (rep.length > max(srcColumn,targetColumn)){
  133.              line = line.replace(rep[srcColumn],rep[targetColumn]);
  134.           }
  135.        }
  136.        writeln(line);
  137.     }
  138.     return 0;
  139. }
  140. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement