summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2020-06-18 10:53:43 +0200
committerThomas Schmucker <ts@its1.de>2020-06-18 10:53:43 +0200
commit575ae35abccb0a22d17e3121c19dd59a71aebf3f (patch)
tree6fbddb4b4f4ef1b0b14da8fb7107305eab28b090
parent36c50e4e77d472e9b7838e1f6385f36ddb52d258 (diff)
downloadlibcsv-575ae35abccb0a22d17e3121c19dd59a71aebf3f.tar.gz
libcsv-575ae35abccb0a22d17e3121c19dd59a71aebf3f.tar.bz2
libcsv-575ae35abccb0a22d17e3121c19dd59a71aebf3f.zip
header datei
-rw-r--r--csv-test.c46
-rw-r--r--csv.h51
2 files changed, 57 insertions, 40 deletions
diff --git a/csv-test.c b/csv-test.c
index fe8afff..de92e46 100644
--- a/csv-test.c
+++ b/csv-test.c
@@ -7,6 +7,8 @@
7#include <assert.h> 7#include <assert.h>
8#include <signal.h> 8#include <signal.h>
9 9
10#include "csv.h"
11
10#define INLINE static inline 12#define INLINE static inline
11 13
12/* === CSV-MEMORY Interface === */ 14/* === CSV-MEMORY Interface === */
@@ -37,18 +39,6 @@ csv_mem_free(void *ptr, void *cb_arg)
37 39
38/* === CSV-OPTIONS Interface === */ 40/* === CSV-OPTIONS Interface === */
39 41
40typedef struct {
41 char quote_symbol;
42 char sep_symbol;
43 void *cb_arg;
44 void (*cb_error)(const char *, void *);
45 void *(*cb_allocate)(size_t, size_t, void *);
46 void *(*cb_reallocate)(void *, size_t, size_t, void *);
47 void (*cb_free)(void *, void *);
48} csv_options_t;
49
50extern const csv_options_t csv_default_options;
51
52const csv_options_t csv_default_options = { 42const csv_options_t csv_default_options = {
53 .quote_symbol = '"', 43 .quote_symbol = '"',
54 .sep_symbol = ',', 44 .sep_symbol = ',',
@@ -73,11 +63,6 @@ csv_fatal_error(const char *msg, const csv_options_t * const csv_options)
73 63
74/* === CSV-STRING Interface === */ 64/* === CSV-STRING Interface === */
75 65
76typedef struct {
77 char *str;
78 int cap, pos;
79} csv_string_t;
80
81INLINE void 66INLINE void
82csv_string_init(csv_string_t *csv_string) 67csv_string_init(csv_string_t *csv_string)
83{ 68{
@@ -150,11 +135,6 @@ csv_string_free(csv_string_t *csv_string, const csv_options_t * const csv_option
150 135
151/* === CSV-FIELD Interface === */ 136/* === CSV-FIELD Interface === */
152 137
153typedef struct {
154 int *fields;
155 int cap, pos;
156} csv_field_t;
157
158INLINE void 138INLINE void
159csv_field_init(csv_field_t *csv_field) 139csv_field_init(csv_field_t *csv_field)
160{ 140{
@@ -218,20 +198,6 @@ csv_field_free(csv_field_t *csv_field, const csv_options_t * const csv_options)
218 198
219/* === CSV Interface === */ 199/* === CSV Interface === */
220 200
221typedef struct {
222 const csv_options_t *csv_options;
223 csv_string_t csv_string;
224 csv_field_t csv_field;
225} csv_t;
226
227void csv_init(csv_t *csv);
228void csv_init_opt(csv_t *csv, const csv_options_t * const csv_options);
229void csv_free(csv_t *csv);
230int csv_nfields(csv_t *csv);
231const char * csv_field(csv_t *csv, int idx);
232int csv_read(csv_t *csv, FILE *in);
233void csv_write(const csv_options_t * const csv_options, int n, const char *fields[], FILE *out);
234
235void 201void
236csv_init(csv_t *csv) 202csv_init(csv_t *csv)
237{ 203{
@@ -436,7 +402,7 @@ csv_read(csv_t *csv, FILE *in)
436} 402}
437 403
438static void 404static void
439csv_write_field(const csv_options_t * const csv_options, const char *field, FILE *out) 405csv_write_field(FILE *out, const char *field, const csv_options_t * const csv_options)
440{ 406{
441 assert(csv_options != NULL); 407 assert(csv_options != NULL);
442 assert(field != NULL); 408 assert(field != NULL);
@@ -462,7 +428,7 @@ csv_write_field(const csv_options_t * const csv_options, const char *field, FILE
462} 428}
463 429
464void 430void
465csv_write(const csv_options_t * const csv_options, int n, const char *fields[], FILE *out) 431csv_write(FILE *out, int n, const char *fields[], const csv_options_t * const csv_options)
466{ 432{
467 assert(csv_options != NULL); 433 assert(csv_options != NULL);
468 assert(fields != NULL); 434 assert(fields != NULL);
@@ -472,12 +438,12 @@ csv_write(const csv_options_t * const csv_options, int n, const char *fields[],
472 438
473 /* process first field */ 439 /* process first field */
474 if ( 0 != n && fields[0] != NULL ) { 440 if ( 0 != n && fields[0] != NULL ) {
475 csv_write_field(csv_options, fields[0], out); 441 csv_write_field(out, fields[0], csv_options);
476 442
477 /* process next fields */ 443 /* process next fields */
478 for ( int i = 1; i != n && fields[i] != NULL; ++i ) { 444 for ( int i = 1; i != n && fields[i] != NULL; ++i ) {
479 putc(SEP, out); 445 putc(SEP, out);
480 csv_write_field(csv_options, fields[i], out); 446 csv_write_field(out, fields[i], csv_options);
481 } 447 }
482 } 448 }
483 fprintf(out, "\r\n"); 449 fprintf(out, "\r\n");
diff --git a/csv.h b/csv.h
new file mode 100644
index 0000000..83755f3
--- /dev/null
+++ b/csv.h
@@ -0,0 +1,51 @@
1#ifndef ITS1_CSV_H_INCLUDED
2#define ITS1_CSV_H_INCLUDED
3
4#include <stdio.h> /* FILE */
5#include <stddef.h> /* size_t */
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11typedef struct {
12 char quote_symbol;
13 char sep_symbol;
14 void *cb_arg;
15 void (*cb_error)(const char *, void *);
16 void *(*cb_allocate)(size_t, size_t, void *);
17 void *(*cb_reallocate)(void *, size_t, size_t, void *);
18 void (*cb_free)(void *, void *);
19} csv_options_t;
20
21extern const csv_options_t csv_default_options;
22
23typedef struct {
24 char *str;
25 int cap, pos;
26} csv_string_t;
27
28typedef struct {
29 int *fields;
30 int cap, pos;
31} csv_field_t;
32
33typedef struct {
34 const csv_options_t *csv_options;
35 csv_string_t csv_string;
36 csv_field_t csv_field;
37} csv_t;
38
39void csv_init(csv_t *csv);
40void csv_init_opt(csv_t *csv, const csv_options_t * const csv_options);
41void csv_free(csv_t *csv);
42int csv_nfields(csv_t *csv);
43const char * csv_field(csv_t *csv, int idx);
44int csv_read(csv_t *csv, FILE *in);
45void csv_write(FILE *out, int n, const char *fields[], const csv_options_t * const csv_options);
46
47#ifdef __cplusplus
48}
49#endif
50
51#endif