Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Hi!
- I have written a little tool in c++ and would like to get some tips how to improve it.
- It reads a file and splits it in columns. Then it replaces the appearance of source column with target column in a target file.
- As comparison I also have written the same tool in D.
- Now i want to know how i can get the c++ version as fast an clean as the D version.
- c++:
- */
- #include <algorithm>
- #include <fstream>
- #include <iostream>
- #include <vector>
- #include <string>
- #include <sstream>
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <getopt.h>
- using namespace std;
- std::vector<string> split(const std::string& s,char delimiter){
- std::vector<string> res;
- std::istringstream stream(s);
- std::string tok;
- while(getline(stream,tok,delimiter)){
- if(!tok.empty()){
- res.push_back(tok);
- }
- }
- return res;
- }
- void replaceInLine(std::string& str,const string& from,const string& to){
- if(from.empty())
- return;
- size_t start_pos = 0;
- while((start_pos = str.find(from, start_pos)) != std::string::npos) {
- str.replace(start_pos, from.length(), to);
- start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
- }
- }
- int main(int argc,char** argv){
- int srcColumn = 0;
- int targetColumn = 1;
- int option_index;
- static struct option long_options[] = {
- {"sourceColumn", required_argument,0 , 's' },
- {"targetColumn",required_argument,0, 't' }
- };
- int c;
- string usage = R"eof(
- usage: source target [options] source target
- options:
- --sourceColumn -s Column which gets replaced
- --targetColumn -t Column which replaces source
- Parameter:
- source: file from which the apperance of column 1 in file tagret file
- gets replaced with column 2 of that file.
- target: file in wicht apperance of column 1 gets replaced with column 2
- )eof";
- while((c= getopt_long(argc, argv, "s:t:?h",long_options, &option_index)) != -1){
- switch(c){
- case 's':
- srcColumn = atoi(optarg);
- break;
- case 't':
- targetColumn = atoi(optarg);
- break;
- case '?':
- case 'h':
- default:
- cout << usage << endl;
- return -1;
- }
- }
- ifstream file(argv[argc-2]);
- ifstream orig(argv[argc-1]);
- if(!file || ! orig){
- cout << "couldn't open file!" << endl;
- return -1;
- }
- std::vector<std::vector<string>> columns;
- string line;
- while(getline(file,line)){
- columns.push_back(split(line,' '));
- }
- while(getline(orig,line)){
- for(auto rep: columns){
- if (rep.size()> max(srcColumn,targetColumn)){
- replaceInLine(line,rep[srcColumn],rep[targetColumn]);
- }
- }
- cout << line << endl;
- }
- }
- /*
- D:
- import std.stdio;
- import std.string;
- import std.conv;
- import std.algorithm;
- import std.getopt;
- int main(string[] args){
- int srcColumn = 0;
- int targetColumn = 1;
- auto res = getopt(args, "s|sourceColumn","column which replaces tagert column", &srcColumn,
- "t|targetColumn", "column which gets replaced",&targetColumn);
- if(res.helpWanted){
- options:
- defaultGetoptPrinter(
- r"usage: [options] source target
- options:
- ",res.options);
- return -1;
- }
- auto f = File(args[$-2]);
- auto orig = File(args[$-1]);
- string[][] columns;
- foreach(line;f.byLine()){
- columns ~= to!string(line).split();
- }
- foreach(line;orig.byLine()){
- foreach(rep;columns){
- if (rep.length > max(srcColumn,targetColumn)){
- line = line.replace(rep[srcColumn],rep[targetColumn]);
- }
- }
- writeln(line);
- }
- return 0;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement