6 /* Buffer for building field content */
7 static char field_buffer[8192];
8 static int field_pos = 0;
10 void reset_field_buffer() {
12 field_buffer[0] = '\0';
15 void append_to_field(char c) {
16 if (field_pos < sizeof(field_buffer) - 1) { /* Reserve space for null terminator */
17 field_buffer[field_pos++] = c;
18 field_buffer[field_pos] = '\0';
22 void append_string_to_field(const char* s) {
23 while (*s && field_pos < sizeof(field_buffer) - 1) { /* Reserve space for null terminator */
24 field_buffer[field_pos++] = *s++;
26 field_buffer[field_pos] = '\0';
29 char* get_field_content() {
30 char* result = strdup(field_buffer); /* NOTE: Caller must free() this memory */
32 fprintf(stderr, "Memory allocation failed in get_field_content()\n");
33 exit(1); /* Conservative approach: exit on memory failure */
44 TEXTDATA [\x20-\x21\x23-\x2B\x2D-\x7E]
53 {COMMA} { return COMMA_TOK; }
55 {CRLF} { return CRLF_TOK; }
57 {LF} { return CRLF_TOK; }
64 <ESCAPED_FIELD>{DQUOTE}{DQUOTE} { append_to_field('"'); }
66 <ESCAPED_FIELD>{DQUOTE} {
67 yylval.str = get_field_content();
72 <ESCAPED_FIELD>{TEXTDATA} { append_to_field(yytext[0]); }
74 <ESCAPED_FIELD>{COMMA} { append_to_field(','); }
76 <ESCAPED_FIELD>{CR} { append_to_field('\r'); }
78 <ESCAPED_FIELD>{LF} { append_to_field('\n'); }
80 <ESCAPED_FIELD>. { append_to_field(yytext[0]); }
82 <ESCAPED_FIELD><<EOF>> {
83 /* Handle unterminated quoted field */
84 yylval.str = get_field_content();
90 yylval.str = strdup(yytext); /* NOTE: Caller must free() this memory */
92 fprintf(stderr, "Memory allocation failed in TEXTDATA rule\n");
93 exit(1); /* Conservative approach: exit on memory failure */
98 [ \t] { /* ignore whitespace outside of fields */ }
100 . { return yytext[0]; }