diff options
author | Antonin Kral <a.kral@bobek.cz> | 2010-03-25 19:21:32 +0100 |
---|---|---|
committer | Antonin Kral <a.kral@bobek.cz> | 2010-03-25 19:21:32 +0100 |
commit | 0ca01a91ae0a3562e54c226e7b9512feb2ea83d0 (patch) | |
tree | 2b3886e435b0217d6afd63a213b04d32bb4b4f6f /tools/import.cpp | |
parent | a696359b248adef0cc8576fce3f473535e995136 (diff) | |
download | mongodb-0ca01a91ae0a3562e54c226e7b9512feb2ea83d0.tar.gz |
Imported Upstream version 1.4.0
Diffstat (limited to 'tools/import.cpp')
-rw-r--r-- | tools/import.cpp | 59 |
1 files changed, 39 insertions, 20 deletions
diff --git a/tools/import.cpp b/tools/import.cpp index 47cbe32..e34e73d 100644 --- a/tools/import.cpp +++ b/tools/import.cpp @@ -76,28 +76,44 @@ class Import : public Tool { } pos++; - int skip = 1; + bool done = false; + string data; char * end; if ( _type == CSV && line[0] == '"' ){ - line++; - end = strstr( line , "\"" ); - skip = 2; - } - else { + line++; //skip first '"' + + while (true) { + end = strchr( line , '"' ); + if (!end){ + data += line; + done = true; + break; + } else if (end[1] == '"') { + // two '"'s get appended as one + data.append(line, end-line+1); //include '"' + line = end+2; //skip both '"'s + } else if (end[-1] == '\\') { + // "\\\"" gets appended as '"' + data.append(line, end-line-1); //exclude '\\' + data.append("\""); + line = end+1; //skip the '"' + } else { + data.append(line, end-line); + line = end+2; //skip '"' and ',' + break; + } + } + } else { end = strstr( line , _sep ); + if ( ! end ){ + done = true; + data = string( line ); + } else { + data = string( line , end - line ); + line = end+1; + } } - - bool done = false; - string data; - if ( ! end ){ - done = true; - data = string( line ); - } - else { - data = string( line , end - line ); - } - if ( _headerLine ){ while ( isspace( data[0] ) ) data = data.substr( 1 ); @@ -108,7 +124,6 @@ class Import : public Tool { if ( done ) break; - line = end + skip; } return b.obj(); } @@ -135,7 +150,7 @@ public: istream * in = &cin; - ifstream file( filename.c_str() , ios_base::in | ios_base::binary); + ifstream file( filename.c_str() , ios_base::in); if ( filename.size() > 0 && filename != "-" ){ if ( ! exists( filename ) ){ @@ -201,7 +216,7 @@ public: log(1) << "filesize: " << fileSize << endl; ProgressMeter pm( fileSize ); const int BUF_SIZE = 1024 * 1024 * 4; - boost::scoped_array<char> line(new char[BUF_SIZE]); + boost::scoped_array<char> line(new char[BUF_SIZE+2]); while ( *in ){ char * buf = line.get(); in->getline( buf , BUF_SIZE ); @@ -214,6 +229,8 @@ public: if ( ! len ) continue; + buf[len+1] = 0; + if ( in->rdstate() == ios_base::eofbit ) break; assert( in->rdstate() == 0 ); @@ -238,6 +255,8 @@ public: } cout << "imported " << num << " objects" << endl; + + conn().getLastError(); if ( errors == 0 ) return 0; |