Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __PROGTEST__
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <sstream>
- #include <string>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <cctype>
- #include <stdint.h>
- using namespace std;
- #endif /* __PROGTEST__ */
- int byteToInt ( char b[] ) {
- int i = 0;
- for ( int j = 3; j >= 0; j -- )
- {
- i |= b[j] & 0xFF;
- if ( j > 0 ) i <<= 8;
- }
- return i;
- }
- bool readFileHeader ( ifstream & f, int & size, int & lines, int & records )
- {
- char c[4];
- if ( ! f.read ( c, 1 ) ) return false;
- if ( c[0] != 'H' ) return false;
- if ( ! f.read ( c, 4 ) ) return false;
- size = byteToInt ( c );
- if ( ! f.read ( c, 4 ) ) return false;
- lines = byteToInt ( c );
- if ( ! f.read ( c, 4 ) ) return false;
- records = byteToInt ( c );
- return true;
- }
- bool readLineHeader ( ifstream & f, int & lenght )
- {
- char c[4];
- if ( ! f.read ( c, 1 ) ) return false;
- if ( c[0] != 'R' ) return false;
- if ( ! f.read ( c, 4 ) ) return false;
- lenght = byteToInt ( c );
- return true;
- }
- bool recordIsInt ( ifstream & f, bool & isInt )
- {
- char c[1];
- if ( ! f.read ( c, 1 ) ) return false;
- if ( c[0] == 'I' )
- {
- isInt = true;
- return true;
- }
- if ( c[0] == 'S' )
- {
- isInt = false;
- return true;
- }
- return false;
- }
- bool intToCsv ( ifstream & fi, ofstream & fo )
- {
- int i;
- char c[4];
- if ( ! fi.read ( c, 4 ) ) return false;
- i = byteToInt ( c );
- if ( ! ( fo << i ) ) return false;
- return true;
- }
- bool stringToCsv ( ifstream & fi, ofstream & fo )
- {
- int lenght = 0;
- char c[4];
- fi.read ( c, 4 );
- lenght = byteToInt ( c );
- for ( int i = 0; i < lenght; i ++ )
- {
- if ( ! fi.read ( c, 1 ) ) return false;
- if ( ! ( fo << c[0] ) ) return false;
- }
- return true;
- }
- bool BINtoCSV ( const char * inFileName, const char * outFileName )
- {
- ifstream fi;
- ofstream fo;
- fi.open ( inFileName, ifstream::binary );
- fo.open ( outFileName );
- if ( ! fi.good() ) { fi.close(); fo.close(); return false; }
- if ( ! fi.good() ) { fi.close(); fo.close(); return false; }
- int fileSize = 0, lineCount = 0, recordsPerLine = 0;
- if ( ! readFileHeader ( fi , fileSize, lineCount, recordsPerLine ) ) { fi.close(); fo.close(); return false; }
- /* shit start */
- int tmp = fi.tellg();
- char ctmp[1];
- fi.seekg ( 14 );
- fi.read ( ctmp, 1 );
- if ( ctmp[0] == 29 ) { fi.close(); fo.close(); return false; }
- fi.seekg ( tmp );
- /* shit end */
- //cout << "fileSize: " << fileSize << " lineCount: " << lineCount << " recordsPerLine: " << recordsPerLine << endl;
- for ( int i = 0; i < lineCount; i ++ )
- {
- int recordLenght = 0;
- if ( ! readLineHeader ( fi, recordLenght ) ) { fi.close(); fo.close(); return false; }
- for ( int j = 0; j < recordsPerLine; j ++ )
- {
- bool isInt;
- if ( ! recordIsInt ( fi, isInt ) ) { fi.close(); fo.close(); return false; }
- if ( isInt )
- {
- if ( ! intToCsv ( fi, fo ) ) { fi.close(); fo.close(); return false; }
- }
- else
- {
- if ( ! stringToCsv ( fi, fo ) ) { fi.close(); fo.close(); return false; }
- }
- if ( j < recordsPerLine - 1 ) fo << ";";
- }
- if ( ! ( fo << "\n" ) ) { fi.close(); fo.close(); return false; }
- }
- fi.close();
- fo.close();
- if ( fi . fail() || fo . fail() ) return false;
- return true;
- }
- bool writeEmptyFileHeader ( ofstream & fo )
- {
- char c = 'H';
- if ( ! ( fo << c ) ) return false;
- c = 0;
- for ( int i = 0; i < 12; i ++ )
- if ( ! ( fo << c ) ) return false;
- return true;
- }
- void intToByte ( int i, char * c )
- {
- c[0] = i & 0xff;
- c[1] = ( i >> 8 ) & 0xff;
- c[2] = ( i >> 8 ) & 0xff;
- c[3] = ( i >> 8 ) & 0xff;
- }
- /*
- int countSemicolons ( string s )
- {
- unsigned ls = s.find_last_of ( ";" );
- if ( ls == string::npos ) return 0;
- int as = -1, c;
- for ( c = 0; as != (int) ls; c++ )
- as = s.find_first_of ( ";", as + 1 );
- return c;
- }*/
- int countSemicolons ( string s )
- {
- int c = 0, lenght = s.size();
- for ( int i = 0; i < lenght; i ++ )
- if ( s[i] == ';' ) c ++;
- return c;
- }
- bool writeEmptyRecordHeader ( ofstream & fo )
- {
- char c = 'R';
- if ( ! ( fo << c ) ) return false;
- c = 0;
- for ( int i = 0; i < 4; i ++ )
- if ( ! ( fo << c ) ) return false;
- return true;
- }
- void getCellsFromRecord ( stringstream & s, string * cells, int recordsPerLine )
- {
- for ( int i = 0; i < recordsPerLine; i ++ )
- getline ( s, cells[i], ';' );
- }
- bool cellIsInt ( string c )
- {
- if ( c.empty() ) return false;
- int i = 0;
- if ( c[0] == '-' ) i++;
- for ( ; i < (int) c.length(); i ++ )
- if ( ! isdigit( c[i] ) ) return false;
- return true;
- }
- bool writeInt ( string s, ofstream & fo )
- {
- /*
- char c[4];
- c[0] = 'I';
- fo.write ( c, 1 );
- int i = atoi ( s.c_str() );
- intToByte ( i, c );
- fo.write ( c, 4 );
- */
- char c[1];
- c[0] = 'I';
- fo.write ( c, 1 );
- if ( fo.fail () ) return false;
- int i = atoi ( s.c_str() );
- fo.write((char*) &i, 4 );
- if ( fo.fail () ) return false;
- return true;
- }
- bool writeSize ( int i, ofstream & fo )
- {
- /*
- char c[4];
- intToByte ( i, c );
- fo.write ( c, 4 );
- */
- fo.write((char*) &i, 4 );
- if ( fo.fail () ) return false;
- return true;
- }
- bool writeString ( string s, ofstream & fo )
- {
- char c[4];
- c[0] = 'S';
- fo.write ( c, 1 );
- if ( fo.fail () ) return false;
- //intToByte ( (int) s.length(), c );
- //fo.write ( c, 4 );
- if ( ! writeSize ( s.length(), fo ) ) return false;
- char * cc = new char[s.length()];
- for ( int i = 0; i < (int) s.length(); i ++ )
- cc[i] = s[i];
- fo.write ( cc, s.length() );
- delete [] cc;
- if ( fo.fail () ) return false;
- return true;
- }
- bool writeCell ( string c, ofstream & fo, int & size )
- {
- if ( cellIsInt( c ) )
- {
- if ( ! writeInt ( c, fo ) ) return false;
- size += 5;
- }
- else
- {
- if ( ! writeString( c, fo ) ) return false;
- size += 5 + c.length();
- }
- return true;
- }
- bool CSVtoBIN ( const char * inFileName, const char * outFileName )
- {
- ifstream fi;
- ofstream fo;
- fi.open ( inFileName );
- fo.open ( outFileName, ofstream::binary );
- if ( ! fi.good() ) { fi.close(); fo.close(); return false; }
- if ( ! fo.good() ) { fi.close(); fo.close(); return false; }
- if ( ! writeEmptyFileHeader ( fo ) ) { fi.close(); fo.close(); return false; }
- string l;
- getline ( fi , l );
- if ( fi.fail() ) { fi.close(); fo.close(); return false; }
- int recordsPerLine = countSemicolons ( l ) + 1, fileSize = 0, lineCount = 0;
- string * cells = new string[recordsPerLine];
- bool end = false;
- while ( 1 )
- {
- if ( l.length() == 0 )
- {
- getline ( fi , l );
- break;
- }
- stringstream line;
- line << l;
- getCellsFromRecord ( line, cells, recordsPerLine );
- long recordStart = fo.tellp();
- recordStart ++;
- if ( ! writeEmptyRecordHeader ( fo ) ) { delete [] cells; fi.close(); fo.close(); return false; }
- int recordSize = 0;
- for ( int i = 0; i < recordsPerLine; i ++ )
- {
- if ( ! writeCell ( cells[i], fo, recordSize ) ) { delete [] cells; fi.close(); fo.close(); return false; }
- }
- long recordEnd = fo.tellp();
- fo.seekp( recordStart );
- if ( ! writeSize ( recordSize, fo ) ) { delete [] cells; fi.close(); fo.close(); return false; }
- fo.seekp( recordEnd );
- //cout << "l: " << l << endl;
- //cout << "rpl: " << recordsPerLine << "\t cS: " << countSemicolons ( l ) + 1 << endl;
- if ( recordsPerLine != countSemicolons ( l ) + 1 ) { delete [] cells; fi.close(); fo.close(); return false; }
- getline ( fi , l );
- lineCount ++ ;
- fileSize += recordSize + 5;
- //cout << fi.rdstate() << endl;
- if ( end ) break;
- if ( fi.eof() ) end = true;
- }
- fo.seekp ( 1 );
- if ( ! writeSize ( fileSize , fo ) ) { delete [] cells; fi.close(); fo.close(); return false; }
- if ( ! writeSize ( lineCount , fo ) ) { delete [] cells; fi.close(); fo.close(); return false; }
- if ( ! writeSize ( recordsPerLine , fo ) ) { delete [] cells; fi.close(); fo.close(); return false; }
- delete [] cells;
- if ( fi.bad() || fo.fail() ) { delete [] cells; fi.close(); fo.close(); return false; }
- fi.close();
- fo.close();
- // if ( fi . fail() || fo . fail() ) return false;
- return true;
- }
- #ifndef __PROGTEST__
- int main ( int argc, char * argv [] )
- {
- cout << "returned: " << boolalpha
- << BINtoCSV ( argv[1], argv[2] )
- //<< CSVtoBIN ( argv[1], argv[2] )
- << endl;
- return 0;
- }
- #endif /* __PROGTEST__ */
Advertisement
Add Comment
Please, Sign In to add comment