Share Pastebin
Guest
Public paste!

register72

By: a guest | Jul 5th, 2009 | Syntax: C++ | Size: 1.95 KB | Hits: 6 | Expires: Never
Copy text to clipboard
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. string& trim(string &str)
  8. {
  9.     int i,j,start,end;
  10.  
  11.     //ltrim
  12.     for (i=0; (str[i]!=0 && str[i]<=32); )
  13.         i++;
  14.     start=i;
  15.  
  16.     //rtrim
  17.     for(i=0,j=0; str[i]!=0; i++)
  18.         j = ((str[i]<=32)? j+1 : 0);
  19.     end=i-j;
  20.     str = str.substr(start,end-start);
  21.     return str;
  22. }
  23.  
  24. int main(int argc, char* argv[])
  25. {
  26.        
  27.         if(argc!= 3)
  28.         {
  29.                 cout<<"Usage ldifclean inputfile outputfile"<<endl;
  30.                 return 0;
  31.         }
  32.        
  33.         ifstream ifile;
  34.         ofstream ofile;;
  35.  
  36.         ifile.open(argv[1]);
  37.         if(ifile.is_open()!=true)
  38.         {
  39.                 cout<<"Cannot open inputfile "<<argv[1]<<endl;
  40.                 return 0;
  41.         }
  42.  
  43.         ofile.open(argv[2]);
  44.         if(ofile.is_open()!=true)
  45.         {
  46.                 cout<<"Cannot open outputfile "<<argv[2]<<endl;
  47.                 return 0;
  48.         }
  49.        
  50.         std::string line;
  51.         std::string prevvalue="";
  52.         std::string prevattribute="";
  53.        
  54.         std::string value="";
  55.         std::string attribute="";
  56.  
  57.         int coloncount=0;
  58.         bool firstline=true;
  59.  
  60.         while(!ifile.eof())
  61.         {
  62.                 //We have a new (attribute:value) pair, write down the previous one
  63.                 if(coloncount==2)
  64.                 {                                
  65.                                 ofile<<prevattribute<<":"<<prevvalue<<endl;
  66.                                 prevattribute=attribute;
  67.                                 prevvalue=value;
  68.                                 coloncount=1;
  69.                 }
  70.  
  71.                 getline(ifile,line);
  72.  
  73.                 //Empty line: end of the old entry and beginning of a new one
  74.                 if(line.length()==0)
  75.                 {
  76.                         ofile<<prevattribute<<":"<<prevvalue<<endl;            
  77.                         ofile<<endl;
  78.                         prevvalue="";
  79.                         prevattribute="";
  80.                         firstline=true;
  81.                         coloncount=0;
  82.                         continue;
  83.                 }
  84.  
  85.                
  86.                 string::size_type colonfound =line.find_first_of(":");
  87.                 if(colonfound==string::npos)
  88.                 {
  89.                         //We are on a broken line. Accumulate the value
  90.                          prevvalue=prevvalue.append(trim(line));
  91.                          continue;
  92.                 }
  93.  
  94.                 attribute=line.substr(0,colonfound);
  95.                 value=line.substr(colonfound+1,line.length()-colonfound-1);
  96.                 coloncount++;
  97.                 if(firstline==true)
  98.                 {
  99.                         prevattribute=attribute;
  100.                         prevvalue=value;
  101.                         firstline=false;
  102.                 }
  103.  
  104.         }
  105.         return 0;
  106. }