#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Given a string which might contain unescaped newlines, split it up into
* lines which do not contain unescaped newlines, returned as a
* NULL-terminated array of malloc'd strings.
*/
char **split_on_unescaped_newlines(const char *txt) {
const char *ptr, *lineStart;
char **buf, **bptr;
int fQuote, nLines;
/* First pass: count how many lines we will need */
for ( nLines = 1, ptr = txt, fQuote = 0; *ptr; ptr++ ) {
if ( fQuote ) {
if ( *ptr == '\"' ) {
if ( ptr[1] == '\"' ) {
ptr++;
continue;
}
fQuote = 0;
}
} else if ( *ptr == '\"' ) {
fQuote = 1;
} else if ( *ptr == '\n' ) {
nLines++;
}
}
buf
= malloc( sizeof(char*) * (nLines
+1) );
if ( !buf ) {
return NULL;
}
/* Second pass: populate results */
lineStart = txt;
for ( bptr = buf, ptr = txt, fQuote = 0; ; ptr++ ) {
if ( fQuote ) {
if ( *ptr == '\"' ) {
if ( ptr[1] == '\"' ) {
ptr++;
continue;
}
fQuote = 0;
continue;
} else if ( *ptr ) {
continue;
}
}
if ( *ptr == '\"' ) {
fQuote = 1;
} else if ( *ptr == '\n' || !*ptr ) {
size_t len = ptr - lineStart;
if ( len == 0 ) {
*bptr = NULL;
return buf;
}
if ( !*bptr ) {
for ( bptr--; bptr >= buf; bptr-- ) {
}
return NULL;
}
memcpy( *bptr
, lineStart
, len
); (*bptr)[len] = '\0';
if ( *ptr ) {
lineStart = ptr + 1;
bptr++;
} else {
bptr[1] = NULL;
return buf;
}
}
}
}
int main(void) {
// your code goes here
char line[80] = "52,243,,542";
split_on_unescaped_newlines(line);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKLyoKICogR2l2ZW4gYSBzdHJpbmcgd2hpY2ggbWlnaHQgY29udGFpbiB1bmVzY2FwZWQgbmV3bGluZXMsIHNwbGl0IGl0IHVwIGludG8KICogbGluZXMgd2hpY2ggZG8gbm90IGNvbnRhaW4gdW5lc2NhcGVkIG5ld2xpbmVzLCByZXR1cm5lZCBhcyBhCiAqIE5VTEwtdGVybWluYXRlZCBhcnJheSBvZiBtYWxsb2MnZCBzdHJpbmdzLgogKi8KY2hhciAqKnNwbGl0X29uX3VuZXNjYXBlZF9uZXdsaW5lcyhjb25zdCBjaGFyICp0eHQpIHsKICAgIGNvbnN0IGNoYXIgKnB0ciwgKmxpbmVTdGFydDsKICAgIGNoYXIgKipidWYsICoqYnB0cjsKICAgIGludCBmUXVvdGUsIG5MaW5lczsKCiAgICAvKiBGaXJzdCBwYXNzOiBjb3VudCBob3cgbWFueSBsaW5lcyB3ZSB3aWxsIG5lZWQgKi8KICAgIGZvciAoIG5MaW5lcyA9IDEsIHB0ciA9IHR4dCwgZlF1b3RlID0gMDsgKnB0cjsgcHRyKysgKSB7CiAgICAgICAgaWYgKCBmUXVvdGUgKSB7CiAgICAgICAgICAgIGlmICggKnB0ciA9PSAnXCInICkgewogICAgICAgICAgICAgICAgaWYgKCBwdHJbMV0gPT0gJ1wiJyApIHsKICAgICAgICAgICAgICAgICAgICBwdHIrKzsKICAgICAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGZRdW90ZSA9IDA7CiAgICAgICAgICAgIH0KICAgICAgICB9IGVsc2UgaWYgKCAqcHRyID09ICdcIicgKSB7CiAgICAgICAgICAgIGZRdW90ZSA9IDE7CiAgICAgICAgfSBlbHNlIGlmICggKnB0ciA9PSAnXG4nICkgewogICAgICAgICAgICBuTGluZXMrKzsKICAgICAgICB9CiAgICB9CgogICAgYnVmID0gbWFsbG9jKCBzaXplb2YoY2hhciopICogKG5MaW5lcysxKSApOwoKICAgIGlmICggIWJ1ZiApIHsKICAgICAgICByZXR1cm4gTlVMTDsKICAgIH0KCiAgICAvKiBTZWNvbmQgcGFzczogcG9wdWxhdGUgcmVzdWx0cyAqLwogICAgbGluZVN0YXJ0ID0gdHh0OwogICAgZm9yICggYnB0ciA9IGJ1ZiwgcHRyID0gdHh0LCBmUXVvdGUgPSAwOyA7IHB0cisrICkgewogICAgICAgIGlmICggZlF1b3RlICkgewogICAgICAgICAgICBpZiAoICpwdHIgPT0gJ1wiJyApIHsKICAgICAgICAgICAgICAgIGlmICggcHRyWzFdID09ICdcIicgKSB7CiAgICAgICAgICAgICAgICAgICAgcHRyKys7CiAgICAgICAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBmUXVvdGUgPSAwOwogICAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgIH0gZWxzZSBpZiAoICpwdHIgKSB7CiAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgaWYgKCAqcHRyID09ICdcIicgKSB7CiAgICAgICAgICAgIGZRdW90ZSA9IDE7CiAgICAgICAgfSBlbHNlIGlmICggKnB0ciA9PSAnXG4nIHx8ICEqcHRyICkgewogICAgICAgICAgICBzaXplX3QgbGVuID0gcHRyIC0gbGluZVN0YXJ0OwoKICAgICAgICAgICAgaWYgKCBsZW4gPT0gMCApIHsKICAgICAgICAgICAgICAgICpicHRyID0gTlVMTDsKICAgICAgICAgICAgICAgIHJldHVybiBidWY7CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgICpicHRyID0gbWFsbG9jKCBsZW4gKyAxICk7CgogICAgICAgICAgICBpZiAoICEqYnB0ciApIHsKICAgICAgICAgICAgICAgIGZvciAoIGJwdHItLTsgYnB0ciA+PSBidWY7IGJwdHItLSApIHsKICAgICAgICAgICAgICAgICAgICBmcmVlKCAqYnB0ciApOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZnJlZSggYnVmICk7CiAgICAgICAgICAgICAgICByZXR1cm4gTlVMTDsKICAgICAgICAgICAgfQoKICAgICAgICAgICAgbWVtY3B5KCAqYnB0ciwgbGluZVN0YXJ0LCBsZW4gKTsKICAgICAgICAgICAgKCpicHRyKVtsZW5dID0gJ1wwJzsKCiAgICAgICAgICAgIGlmICggKnB0ciApIHsKICAgICAgICAgICAgICAgIGxpbmVTdGFydCA9IHB0ciArIDE7CiAgICAgICAgICAgICAgICBicHRyKys7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBicHRyWzFdID0gTlVMTDsKICAgICAgICAgICAgICAgIHJldHVybiBidWY7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn0KCmludCBtYWluKHZvaWQpIHsKCS8vIHlvdXIgY29kZSBnb2VzIGhlcmUKICAgIGNoYXIgbGluZVs4MF0gPSAiNTIsMjQzLCw1NDIiOwogICAgCiAgICBzcGxpdF9vbl91bmVzY2FwZWRfbmV3bGluZXMobGluZSk7CiAgICBwcmludGYoImxpbmUgICVzXG4iLCBsaW5lWzBdKTsKICAgIHByaW50ZigibGluZSAgJXNcbiIsIGxpbmVbMV0pOwoJCglyZXR1cm4gMDsKfQo=