From ed5b0c5a17a781db78a3c902ac867ee9a5df2761 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Wed, 24 Jun 2020 15:56:33 +0200 Subject: reformat comments --- csv.c | 63 ++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/csv.c b/csv.c index 863d31d..49e1c02 100644 --- a/csv.c +++ b/csv.c @@ -12,7 +12,7 @@ #define CSV_DEFAULT_SEPARATOR ',' #endif -/* === CSV-MEMORY Interface === */ +// === CSV-MEMORY Interface === static void * csv_mem_allocate(size_t n, size_t sz, void *cb_arg) @@ -39,7 +39,7 @@ csv_mem_free(void *ptr, size_t sz, void *cb_arg) free(ptr); } -/* === CSV-OPTIONS Interface === */ +// === CSV-OPTIONS Interface === const csv_options_t csv_default_options = { .field_delimiter = CSV_DEFAULT_DELIMITER, @@ -50,7 +50,7 @@ const csv_options_t csv_default_options = { .cb_free = csv_mem_free }; -/* === CSV-ERROR Interface === */ +// === CSV-ERROR Interface === static void csv_fatal_error(csv_err_t csv_err, const csv_options_t * const csv_options) @@ -63,7 +63,7 @@ csv_fatal_error(csv_err_t csv_err, const csv_options_t * const csv_options) abort(); } -/* === CSV-STRING Interface === */ +// === CSV-STRING Interface === static inline void csv_string_init(csv_string_t *csv_string) @@ -99,8 +99,8 @@ csv_string_append(csv_string_t *csv_string, char ch, const csv_options_t * const static const int INITIAL_CAP = 16; - if ( csv_string->pos == csv_string->cap ) { /* grow if needed */ - if ( csv_string->str == NULL ) { /* first call? */ + if ( csv_string->pos == csv_string->cap ) { // grow if needed + if ( csv_string->str == NULL ) { // first call? if ( (csv_string->str = csv_options->cb_allocate(INITIAL_CAP, 1, csv_options->cb_memory_arg)) == NULL ) { csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options); return; @@ -108,8 +108,8 @@ csv_string_append(csv_string_t *csv_string, char ch, const csv_options_t * const csv_string->cap = INITIAL_CAP; } - else { /* subsequent call */ - int cap = (csv_string->cap * 3) / 2; /* *= 1.5 */ + else { // subsequent call + int cap = (csv_string->cap * 3) / 2; // *= 1.5 char *str = csv_options->cb_reallocate(csv_string->str, cap, 1, csv_options->cb_memory_arg); if ( str == NULL ) { csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options); @@ -120,7 +120,7 @@ csv_string_append(csv_string_t *csv_string, char ch, const csv_options_t * const } } - csv_string->str[csv_string->pos++] = ch; /* append char */ + csv_string->str[csv_string->pos++] = ch; // append char } static inline void @@ -131,11 +131,11 @@ csv_string_free(csv_string_t *csv_string, const csv_options_t * const csv_option csv_options->cb_free(csv_string->str, csv_string->cap, csv_options->cb_memory_arg); - /* call *_init() for sane default values; prevent possible double-free */ + // call *_init() for sane default values; prevent possible double-free csv_string_init(csv_string); } -/* === CSV-FIELD Interface === */ +// === CSV-FIELD Interface === static inline void csv_field_init(csv_field_t *csv_field) @@ -163,8 +163,8 @@ csv_field_append(csv_field_t *csv_field, int idx, const csv_options_t * const cs static const size_t INITIAL_CAP = 16; - if ( csv_field->pos == csv_field->cap ) { /* grow if needed */ - if ( csv_field->fields == NULL ) { + if ( csv_field->pos == csv_field->cap ) { // grow if needed + if ( csv_field->fields == NULL ) { // first call if ( (csv_field->fields = csv_options->cb_allocate(INITIAL_CAP, sizeof(csv_field->fields[0]), csv_options->cb_memory_arg)) == NULL ) { csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options); return; @@ -172,8 +172,8 @@ csv_field_append(csv_field_t *csv_field, int idx, const csv_options_t * const cs csv_field->cap = INITIAL_CAP; } - else { - int cap = (csv_field->cap * 3) / 2; /* *= 1.5 */ + else { // subsequent call + int cap = (csv_field->cap * 3) / 2; // *= 1.5 int *fields = csv_options->cb_reallocate(csv_field->fields, cap, sizeof(csv_field->fields[0]), csv_options->cb_memory_arg); if ( fields == NULL ) { csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options); @@ -184,7 +184,7 @@ csv_field_append(csv_field_t *csv_field, int idx, const csv_options_t * const cs } } - csv_field->fields[csv_field->pos++] = idx; /* append idx */ + csv_field->fields[csv_field->pos++] = idx; // append idx } static inline void @@ -198,7 +198,7 @@ csv_field_free(csv_field_t *csv_field, const csv_options_t * const csv_options) csv_field_init(csv_field); } -/* === CSV Interface === */ +// === CSV Interface === void csv_init(csv_t *csv) @@ -264,12 +264,12 @@ csv_read(csv_t *csv, FILE *in) assert(csv != NULL); assert(in != NULL); - /* initialze if needed... */ + // initialze if needed... if ( csv->csv_options == NULL ) { csv_init(csv); } - /* do not try to read if EOF has already been seen */ + // do not try to read if EOF has already been seen if ( feof(in) ) { csv_cleanup(csv); return -1; @@ -305,9 +305,9 @@ csv_read(csv_t *csv, FILE *in) if ( ch == EOF ) { state = STATE_END_FILE; } - else if ( ch == '\r' ) { /* Teste auf CR.. */ + else if ( ch == '\r' ) { // test for CR.. ch = getc(in); - if ( ch != '\n' ) { /* ... LF */ + if ( ch != '\n' ) { // ..LF ungetc(ch, in); } state = STATE_END_LINE; @@ -356,7 +356,7 @@ csv_read(csv_t *csv, FILE *in) } else { csv_string_append(&csv->csv_string, DELIM, csv->csv_options); - ungetc(ch, in); /* zuviel gelesenes Zeichen zurückstellen */ + ungetc(ch, in); // we have read too far... Put the character back! } } else { @@ -406,14 +406,16 @@ csv_read(csv_t *csv, FILE *in) if ( csv_string_empty(&csv->csv_string) ) { csv_cleanup(csv); - return -1; /* EOF reached */ + return -1; // EOF reached } - /* EOF wurde gelesen, allerdings sind Daten zur Vearbeitung vorhanden! */ - /* => aktuelles Feld abschließen... */ + /* + * The last data record was not terminated with a NEWLINE-Symbol. + * So we can't signal End-Of-File for now. Terminate the current + * field and return the numbor of fields processed so far. + */ csv_string_append(&csv->csv_string, '\0', csv->csv_options); - /* ... und die Anzahl der Felder zurückliefern! */ return csv->csv_field.pos; default: @@ -421,7 +423,7 @@ csv_read(csv_t *csv, FILE *in) break; } } - /* NOT REACHED */ + // NOT REACHED } const char * @@ -435,7 +437,7 @@ csv_err_str(csv_err_t csv_err) case CSV_ERR_IO_WRITE: return "write error"; default: return "unknown error"; } - /* NOT REACHED */ + // NOT REACHED } static void @@ -479,7 +481,6 @@ csv_write(FILE *out, const csv_t * const csv) const char SEP = csv->csv_options->field_separator; - if ( 0 < csv->csv_field.pos ) { const char *str = csv->csv_string.str; const int *field = csv->csv_field.fields; @@ -511,12 +512,12 @@ csv_write_ex(FILE *out, int n, const char *fields[], const csv_options_t * csv_o const char SEP = csv_options->field_separator; - /* process first field */ + // process first field if ( 0 != n && *fields != NULL ) { --n; csv_write_field(out, *fields++, csv_options); - /* process next fields */ + // process next fields for ( ; n && *fields != NULL; --n, ++fields ) { putc(SEP, out); csv_write_field(out, *fields, csv_options); -- cgit v1.3