]> Creatis software - bbtk.git/blob - kernel/appli/bbSed/bbSed.cpp
51ca114fd82642ab297d8bf2015eb1f725a99c74
[bbtk.git] / kernel / appli / bbSed / bbSed.cpp
1
2 #include <stdio.h>
3 #include <stdio.h>
4 #include <iostream>
5 #include <fstream>
6 #include <sstream>
7 #include <vector>
8
9
10
11     void replace( std::string& str,
12                         const std::string& from, 
13                         const std::string& to )
14     {
15       using std::string;
16       string::size_type pos = str.find( from );
17       while ( pos != string::npos )
18       {
19         str.replace( pos, from.size(), to );
20         pos = str.find( from, pos+from.size()-1 );
21       } 
22     }
23
24
25
26
27 //==========================================================================
28 int main(int argc, char **argv)
29 {
30   
31   if (argc!=4) 
32     {
33       std::cerr << "usage : "<< argv[0] <<" fileIn  \"FindString\" \"ReplaceString\"" << std::endl;
34       return 1;
35     }
36
37   FILE *ffIn;
38   std::string fileIn;
39   std::string fileOut;
40   std::string findstring;
41   std::string replacestring;
42   std::string lineStr;
43   char strTmp[255];
44
45   fileIn        = argv[1];
46   findstring    = argv[2];
47   replacestring = argv[3];
48
49   ffIn =  fopen(fileIn.c_str(),"r");
50   if (ffIn){
51         while(!feof(ffIn)){
52             fgets( strTmp , 255, ffIn );
53             lineStr=strTmp;
54             if( feof(ffIn) && (lineStr.length()==1) ) { 
55             } else {
56               replace( lineStr,findstring, replacestring );
57               std::cout << lineStr;
58             }
59         }
60         fclose(ffIn);   
61   } else {
62       std::cerr << "ERROR. File : "<< argv[1] <<" Not exists." << std::endl;
63       return 1;
64   }
65
66
67   return 0;
68 }
69 //==========================================================================
70
71