0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 17:10:48 +01:00

mongoimport: support for escaped " in csv with either "" or \" SERVER-773

This commit is contained in:
Mathias Stearn 2010-03-22 13:52:21 -04:00
parent f69f94cbb4
commit f816dc8419
2 changed files with 35 additions and 20 deletions

View File

@ -4,7 +4,7 @@ t = new ToolTest( "csv1" )
c = t.startDB( "foo" );
base = { a : 1 , b : "foo,bar" , c: 5, 'd d': 6 };
base = { a : 1 , b : "foo,bar\"baz,qux" , c: 5, 'd d': 6 };
assert.eq( 0 , c.count() , "setup1" );
c.insert( base );

View File

@ -76,28 +76,44 @@ class Import : public Tool {
}
pos++;
int skip = 1;
char * end;
if ( _type == CSV && line[0] == '"' ){
line++;
end = strstr( line , "\"" );
skip = 2;
}
else {
end = strstr( line , _sep );
}
bool done = false;
string data;
char * end;
if ( _type == CSV && line[0] == '"' ){
line++; //skip first '"'
if ( ! end ){
done = true;
data = string( line );
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;
}
}
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();
}