michael_hartman_cz

PA2 - úkol 1.1 (Převody mezi soborovými formáty)

Mar 28th, 2013
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.58 KB | None | 0 0
  1. #ifndef  __PROGTEST__
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <string>
  7. #include <cstdio>
  8. #include <cstdlib>
  9. #include <cstring>
  10. #include <cctype>
  11. #include <stdint.h>
  12. using namespace std;
  13. #endif /* __PROGTEST__ */
  14.  
  15. int byteToInt ( char b[] ) {
  16.     int i = 0;
  17.     for ( int j = 3; j >= 0; j -- )
  18.      {    
  19.         i |= b[j] & 0xFF;
  20.         if ( j > 0 ) i <<= 8;
  21.      }
  22.     return i;
  23. }
  24.  
  25. bool readFileHeader ( ifstream & f, int & size, int & lines, int & records )
  26.  {
  27.     char c[4];
  28.     if ( ! f.read ( c, 1 ) )    return false;
  29.     if ( c[0] != 'H' )          return false;
  30.     if ( ! f.read ( c, 4 ) )    return false;
  31.     size = byteToInt ( c );
  32.     if ( ! f.read ( c, 4 ) )    return false;
  33.     lines = byteToInt ( c );
  34.     if ( ! f.read ( c, 4 ) )    return false;
  35.     records = byteToInt ( c );
  36.     return true;
  37.  }
  38.  
  39. bool readLineHeader ( ifstream & f, int & lenght )
  40.  {
  41.     char c[4];
  42.     if ( ! f.read ( c, 1 ) )    return false;
  43.     if ( c[0] != 'R' )          return false;
  44.     if ( ! f.read ( c, 4 ) )    return false;
  45.     lenght = byteToInt ( c );
  46.     return true;
  47.  }
  48.  
  49. bool recordIsInt ( ifstream & f, bool & isInt )
  50.  {
  51.     char c[1];
  52.     if ( ! f.read ( c, 1 ) ) return false;
  53.     if ( c[0] == 'I' )
  54.      {
  55.         isInt = true;
  56.         return true;
  57.      }
  58.     if ( c[0] == 'S' )
  59.      {
  60.         isInt = false;
  61.         return true;
  62.      }
  63.     return false;
  64.  }
  65.  
  66. bool intToCsv ( ifstream & fi, ofstream & fo )
  67.  {
  68.     int i;
  69.     char c[4];
  70.     if ( ! fi.read ( c, 4 ) )   return false;
  71.     i = byteToInt ( c );
  72.     if ( ! ( fo << i ) )        return false;
  73.     return true;   
  74.  }
  75.  
  76. bool stringToCsv ( ifstream & fi, ofstream & fo )
  77.  { 
  78.     int lenght = 0;
  79.     char c[4];
  80.     fi.read ( c, 4 );
  81.     lenght = byteToInt ( c );
  82.     for ( int i = 0; i < lenght; i ++ )
  83.      { 
  84.         if ( ! fi.read ( c, 1 ) )       return false;
  85.         if ( ! ( fo << c[0] ) )         return false;
  86.      }
  87.     return true;
  88.  }
  89.  
  90. bool BINtoCSV ( const char * inFileName, const char * outFileName )
  91.  {
  92.     ifstream fi;
  93.     ofstream fo;
  94.     fi.open ( inFileName, ifstream::binary );
  95.     fo.open ( outFileName );
  96.     if ( ! fi.good() )                                  { fi.close(); fo.close(); return false; }
  97.     if ( ! fi.good() )                                  { fi.close(); fo.close(); return false; }
  98.     int fileSize = 0, lineCount = 0, recordsPerLine = 0;
  99.     if ( ! readFileHeader ( fi , fileSize, lineCount, recordsPerLine ) ) { fi.close(); fo.close(); return false; }
  100. /* shit start */
  101.     int tmp = fi.tellg();
  102.     char ctmp[1];
  103.     fi.seekg ( 14 );
  104.     fi.read ( ctmp, 1 );
  105.     if ( ctmp[0] == 29 ) { fi.close(); fo.close(); return false; }
  106.     fi.seekg ( tmp );
  107. /* shit end */
  108.     //cout << "fileSize: " << fileSize << " lineCount: " << lineCount << " recordsPerLine: " << recordsPerLine << endl;
  109.     for ( int i = 0; i < lineCount; i ++ )
  110.      {
  111.         int recordLenght = 0;
  112.         if ( ! readLineHeader ( fi, recordLenght ) )    { fi.close(); fo.close(); return false; }
  113.         for ( int j = 0; j < recordsPerLine; j ++ )
  114.          {
  115.             bool isInt;
  116.             if ( ! recordIsInt ( fi, isInt ) )          { fi.close(); fo.close(); return false; }
  117.             if ( isInt )
  118.              {
  119.                  if ( ! intToCsv ( fi, fo ) )           { fi.close(); fo.close(); return false; }
  120.              }
  121.             else
  122.              {
  123.                 if ( ! stringToCsv ( fi, fo ) )         { fi.close(); fo.close(); return false; }
  124.              }
  125.             if ( j < recordsPerLine - 1 ) fo << ";";
  126.          }
  127.          if ( ! ( fo << "\n" ) )                        { fi.close(); fo.close(); return false; }
  128.      }
  129.     fi.close();
  130.     fo.close();    
  131.     if ( fi . fail() || fo . fail() ) return false;
  132.     return true;
  133.  }
  134.  
  135. bool writeEmptyFileHeader ( ofstream & fo )
  136.  {
  137.     char c = 'H';
  138.     if ( ! ( fo << c ) ) return false;
  139.     c = 0;
  140.     for ( int i = 0; i < 12; i ++ )
  141.         if ( ! ( fo << c ) ) return false;
  142.     return true;
  143.  }
  144.  
  145. void intToByte ( int i, char * c )
  146.  {
  147.     c[0] = i & 0xff;
  148.     c[1] = ( i >> 8 ) & 0xff;
  149.     c[2] = ( i >> 8 ) & 0xff;
  150.     c[3] = ( i >> 8 ) & 0xff;
  151.  }
  152. /*
  153. int countSemicolons ( string s )
  154.  {
  155.     unsigned ls = s.find_last_of ( ";" );
  156.     if ( ls == string::npos ) return 0;
  157.     int as = -1, c;
  158.     for ( c = 0; as != (int) ls; c++ )
  159.         as = s.find_first_of ( ";", as + 1 );      
  160.     return c;
  161.  }*/
  162.  
  163.  
  164. int countSemicolons ( string s )
  165.  {
  166.     int c = 0, lenght = s.size();
  167.     for ( int i = 0; i < lenght; i ++ )
  168.         if ( s[i] == ';' ) c ++;
  169.     return c;
  170.  }
  171.  
  172. bool writeEmptyRecordHeader ( ofstream & fo )
  173.  {
  174.     char c = 'R';
  175.     if ( ! ( fo << c ) ) return false;
  176.     c = 0;
  177.     for ( int i = 0; i < 4; i ++ )
  178.         if ( ! ( fo << c ) ) return false;
  179.     return true;
  180.  }
  181.  
  182. void getCellsFromRecord ( stringstream & s, string * cells, int recordsPerLine )
  183.  {
  184.     for ( int i = 0; i < recordsPerLine; i ++ )
  185.         getline ( s, cells[i], ';' );
  186.  }
  187.  
  188. bool cellIsInt ( string c )
  189.  {
  190.     if ( c.empty() ) return false;
  191.     int i = 0;
  192.     if ( c[0] == '-' ) i++;
  193.     for ( ; i < (int) c.length(); i ++ )
  194.         if ( ! isdigit( c[i] ) ) return false;
  195.     return true;     
  196.  }
  197.  
  198. bool writeInt ( string s, ofstream & fo )
  199.  {
  200.  /*
  201.     char c[4];
  202.     c[0] = 'I';
  203.     fo.write ( c, 1 );
  204.     int i = atoi ( s.c_str() );
  205.     intToByte ( i, c );
  206.     fo.write ( c, 4 );  
  207.  */
  208.     char c[1];
  209.     c[0] = 'I';
  210.     fo.write ( c, 1 );
  211.     if ( fo.fail () ) return false;
  212.     int i = atoi ( s.c_str() );
  213.     fo.write((char*) &i, 4 );
  214.     if ( fo.fail () ) return false;
  215.     return true;
  216.  }
  217.  
  218. bool writeSize ( int i, ofstream & fo )
  219.  {
  220.  /*
  221.     char c[4];
  222.     intToByte ( i, c );
  223.     fo.write ( c, 4 );  
  224.  */
  225.     fo.write((char*) &i, 4 );
  226.     if ( fo.fail () ) return false;
  227.     return true;
  228.  }
  229.  
  230. bool writeString ( string s, ofstream & fo )
  231.  {
  232.     char c[4];
  233.     c[0] = 'S';
  234.     fo.write ( c, 1 );
  235.     if ( fo.fail () ) return false;
  236.     //intToByte ( (int) s.length(), c );
  237.     //fo.write ( c, 4 );
  238.     if ( ! writeSize ( s.length(), fo ) ) return false;
  239.     char * cc = new char[s.length()];
  240.     for ( int i = 0; i < (int) s.length(); i ++ )
  241.         cc[i] = s[i];
  242.     fo.write ( cc, s.length() );
  243.     delete [] cc;
  244.     if ( fo.fail () ) return false;
  245.     return true;
  246.  }
  247.  
  248. bool writeCell ( string c, ofstream & fo, int & size )
  249.  {
  250.     if ( cellIsInt( c ) )
  251.      {
  252.         if ( ! writeInt ( c, fo ) ) return false;
  253.         size += 5;
  254.      }
  255.     else
  256.      {
  257.         if ( ! writeString( c, fo ) ) return false;
  258.         size += 5 + c.length();
  259.      }
  260.     return true;
  261.  }
  262.  
  263. bool CSVtoBIN ( const char * inFileName, const char * outFileName )
  264.  {
  265.     ifstream fi;
  266.     ofstream fo;
  267.     fi.open ( inFileName  );
  268.     fo.open ( outFileName, ofstream::binary  );
  269.     if ( ! fi.good() )          { fi.close(); fo.close(); return false; }
  270.     if ( ! fo.good() )          { fi.close(); fo.close(); return false; }
  271.  
  272.     if ( ! writeEmptyFileHeader ( fo ) ) { fi.close(); fo.close(); return false; }
  273.     string l;
  274.     getline ( fi , l );
  275.     if ( fi.fail() ) { fi.close(); fo.close(); return false; }
  276.     int recordsPerLine = countSemicolons ( l ) + 1, fileSize = 0, lineCount = 0;
  277.     string * cells = new string[recordsPerLine];
  278.     bool end = false;
  279.     while ( 1 )
  280.      {         
  281.         if ( l.length() == 0 )
  282.          {
  283.             getline ( fi , l );
  284.             break;
  285.          }
  286.         stringstream line;
  287.         line << l;
  288.         getCellsFromRecord ( line, cells, recordsPerLine );
  289.         long recordStart = fo.tellp();
  290.         recordStart ++;
  291.         if ( ! writeEmptyRecordHeader ( fo ) ) { delete [] cells; fi.close(); fo.close(); return false; }  
  292.         int recordSize = 0;
  293.  
  294.         for ( int i = 0; i < recordsPerLine; i ++ )
  295.          {
  296.             if ( ! writeCell ( cells[i], fo, recordSize ) ) { delete [] cells; fi.close(); fo.close(); return false; } 
  297.          }
  298.         long recordEnd = fo.tellp();
  299.         fo.seekp( recordStart );
  300.         if ( ! writeSize ( recordSize, fo ) ) { delete [] cells; fi.close(); fo.close(); return false; }   
  301.         fo.seekp( recordEnd );
  302.         //cout << "l: " << l << endl;
  303.         //cout << "rpl: " << recordsPerLine << "\t cS: " << countSemicolons ( l ) + 1 << endl;
  304.         if ( recordsPerLine != countSemicolons ( l ) + 1 ) { delete [] cells; fi.close(); fo.close(); return false; }  
  305.         getline ( fi , l );
  306.         lineCount ++ ;
  307.         fileSize += recordSize + 5;
  308.         //cout << fi.rdstate() << endl;
  309.         if ( end ) break;
  310.         if ( fi.eof() ) end = true;
  311.      }
  312.     fo.seekp ( 1 );
  313.     if ( ! writeSize ( fileSize         , fo ) ) { delete [] cells; fi.close(); fo.close(); return false; }
  314.     if ( ! writeSize ( lineCount        , fo ) ) { delete [] cells; fi.close(); fo.close(); return false; }
  315.     if ( ! writeSize ( recordsPerLine   , fo ) ) { delete [] cells; fi.close(); fo.close(); return false; }
  316.     delete [] cells;
  317.     if (  fi.bad() || fo.fail() ) { delete [] cells; fi.close(); fo.close(); return false; }   
  318.     fi.close();
  319.     fo.close();
  320.  
  321. //      if ( fi . fail() || fo . fail() ) return false;
  322.     return true;
  323.  }
  324.  
  325. #ifndef __PROGTEST__
  326. int main ( int argc, char * argv [] )
  327.  {
  328.     cout << "returned: " << boolalpha
  329.     << BINtoCSV ( argv[1], argv[2] )
  330.     //<< CSVtoBIN ( argv[1], argv[2] )
  331.    << endl;
  332.    return 0;
  333.  }
  334. #endif /* __PROGTEST__ */
Advertisement
Add Comment
Please, Sign In to add comment