File: csv2rec.c

package info (click to toggle)
recutils 1.7-1.1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 12,276 kB
  • ctags: 7,034
  • sloc: ansic: 64,860; sh: 18,180; lisp: 1,840; yacc: 1,381; makefile: 331; lex: 241; sed: 16
file content (414 lines) | stat: -rw-r--r-- 9,419 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* -*- mode: C -*-
 *
 *       File:         csv2rec.c
 *       Date:         Fri Aug 20 16:35:25 2010
 *
 *       GNU recutils - csv to rec converter.
 *
 */

/* Copyright (C) 2010, 2011, 2012 Jose E. Marchesi */

/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include <xalloc.h>
#include <gettext.h>
#define _(str) gettext (str)

#include <csv.h>
#include <rec.h>
#include <recutl.h>

/* Forward declarations.  */
static void parse_args (int argc, char **argv);
static rec_db_t process_csv (void);
static int is_space (unsigned char c);
static int is_term (unsigned char c);
static void field_cb (void *s, size_t len, void *data);
static void record_cb (int c, void *data);

/*
 * Types
 */

struct csv2rec_ctx
{
  rec_db_t db;
  rec_rset_t rset;
  rec_record_t record;

  size_t num_fields;
  size_t lineno;

#define ALLOC_FIELDS 256
  bool header_p;
  size_t num_field_names;
  char **field_names;
};

/*
 * Global variables
 */

char *csv2rec_record_type = NULL;
char *csv2rec_csv_file = NULL;
bool csv2rec_strict = false;
bool csv2rec_omit_empty = false;

/*
 * Command line options management
 */

enum
  {
    COMMON_ARGS,
    RECORD_TYPE_ARG,
    STRICT_ARG,
    OMIT_EMPTY_ARG
  };

static const struct option GNU_longOptions[] =
  {
    COMMON_LONG_ARGS,
    {"type", required_argument, NULL, RECORD_TYPE_ARG},
    {"strict", no_argument, NULL, STRICT_ARG},
    {"omit-empty", no_argument, NULL, OMIT_EMPTY_ARG},
    {NULL, 0, NULL, 0}
  };

/*
 * Functions.
 */

void
recutl_print_help (void)
{
  /* TRANSLATORS: --help output, csv2rec synopsis.
     no-wrap */
  printf (_("\
Usage: csv2rec [OPTIONS]... [CSV_FILE]\n"));

  /* TRANSLATORS: --help output, csv2rec short description.
     no-wrap */
  fputs (_("\
Convert csv data into rec data.\n"), stdout);

  puts ("");
  /* TRANSLATORS: --help output, csv2rec options.
     no-wrap */
  fputs (_("\
  -t, --type=TYPE                     type name for the converted records; if this\n\
                                        parameter is ommited then no type is used.\n\
  -s, --strict                        be strict parsing the csv file.\n\
  -e, --omit-empty                    omit empty fields.\n"), stdout);

  recutl_print_help_common ();
  puts ("");
  recutl_print_help_footer ();
}

static void
parse_args (int argc,
            char **argv)
{
  int ret;
  char c;

  while ((ret = getopt_long (argc,
                             argv,
                             "t:se",
                             GNU_longOptions,
                             NULL)) != -1)
    {
      c = ret;
      switch (c)
        {
          COMMON_ARGS_CASES
        case RECORD_TYPE_ARG:
        case 't':
          {
            csv2rec_record_type = xstrdup (optarg);
            break;
          }
        case STRICT_ARG:
        case 's':
          {
            csv2rec_strict = true;
            break;
          }
        case OMIT_EMPTY_ARG:
        case 'e':
          {
            csv2rec_omit_empty = true;
            break;
          }
        default:
          {
            exit (EXIT_FAILURE);
          }
        }
    }

  /* Read the name of the csv file, if any.  */
  if (optind < argc)
    {
      if ((argc - optind) != 1)
        {
          recutl_print_help ();
          exit (EXIT_FAILURE);
        }

      csv2rec_csv_file = argv[optind++];
    }
}

static int
is_space (unsigned char c)
{
  return (c == CSV_SPACE) || (c == CSV_TAB);
}

static int
is_term (unsigned char c)
{
  return (c == CSV_CR) || (c == CSV_LF);
}

void
field_cb (void *s, size_t len, void *data)
{
  char *str;
  char *field_name;
  rec_field_t field;
  struct csv2rec_ctx *ctx;
  size_t i;

  ctx = (struct csv2rec_ctx *) data;
  str = xmalloc (len + 1);
  memcpy (str, s, len);
  str[len] = '\0';

  if (ctx->header_p)
    {
      /* Add a new field name to ctx.field_names.  */

      if ((ctx->num_field_names % ALLOC_FIELDS) == 0)
        ctx->field_names =
          realloc (ctx->field_names, ((ctx->num_field_names / ALLOC_FIELDS) + 1) * (sizeof(char *) * ALLOC_FIELDS));

      /* Normalize the name: spaces and tabs are turned into dashes
         '_'.  */
      for (i = 0; i < strlen (str); i++)
        {
          if ((str[i] == ' ') || (str[i] == '\t'))
            {
              str[i] = '_';
            }
        }

      /* Verify that it is a valid field name.  */
      field_name = str;
      if (!rec_field_name_p (field_name))
        {
          recutl_fatal (_("invalid field name '%s' in header\n"),
                        str);
        }
      ctx->field_names[ctx->num_field_names++] = str;
    }
  else
    {
      /* Create a new field and insert it in the current record.  */

      if (!ctx->record)
        {
          /* Create a new record.  */
          ctx->record = rec_record_new ();
          if (!ctx->record)
            recutl_out_of_memory ();
        }
      
      if (!csv2rec_omit_empty || (strlen(str) > 0))
        {
          if (ctx->num_fields > ctx->num_field_names)
            {
              char *source = csv2rec_csv_file;

              if (!source)
                {
                  source = "stdin";
                }

              fprintf (stderr,
                       _("%s: %lu: this line contains %lu fields, but %lu header fields were read\n"),
                       source,
                       ctx->lineno, ctx->num_field_names, ctx->num_fields);
              exit (EXIT_FAILURE);
            }
          field = rec_field_new (ctx->field_names[ctx->num_fields], str);
          rec_mset_append (rec_record_mset (ctx->record), MSET_FIELD, (void *) field, MSET_ANY);
        }

      ctx->num_fields++;
    }
}

void
record_cb (int c, void *data)
{
  struct csv2rec_ctx *ctx;
  ctx = (struct csv2rec_ctx *) data;

  ctx->lineno++;

  if (ctx->header_p)
    {
      ctx->header_p = false;
    }
  else
    {
      if (!ctx->rset)
        {
          /* Create a new record set.  */
          ctx->rset = rec_rset_new ();
          if (!ctx->rset)
            recutl_out_of_memory ();

          /* Add a type, if needed.  */
          if (csv2rec_record_type)
            {
              rec_rset_set_type (ctx->rset, csv2rec_record_type);
            }
          
          /* Add it to the database.  */
          if (!ctx->db)
            {
              ctx->db = rec_db_new ();
              if (!ctx->db)
                recutl_out_of_memory ();
            }
          rec_db_insert_rset (ctx->db, ctx->rset, rec_db_size (ctx->db));
        }
      
      /* Add the current record to the record set.  */
      rec_mset_append (rec_rset_mset (ctx->rset), MSET_RECORD, (void *) ctx->record, MSET_ANY);
      ctx->record = NULL;
      
      /* Reset the field counter.  */
      ctx->num_fields = 0;
    }
}

static rec_db_t
process_csv (void)
{
  struct csv2rec_ctx ctx;
  FILE *in;
  struct csv_parser p;
  unsigned char options = 0;
  char buf[1024];
  size_t bytes_read = 0;

  /* Initialize the data in the context.  */
  ctx.db = NULL;
  ctx.rset = NULL;
  ctx.record = NULL;
  ctx.header_p = true;
  ctx.field_names = NULL;
  ctx.num_field_names = 0;
  ctx.num_fields = 0;
  ctx.lineno = 0;

  /* Set the files to read/write from/to.

     If a filename was specified, read the csv file from there.
     Otherwise use the standard input.  The output is written to the
     standard output in any case.  */
  if (csv2rec_csv_file)
    {
      if (!(in = fopen (csv2rec_csv_file, "r")))
        {
          recutl_fatal (_("cannot read file %s\n"), csv2rec_csv_file);
        }
    }
  else
    {
      in = stdin;
    }

  /* Initialize the csv library.  */
  if (csv_init (&p, options) != 0)
    {
      recutl_fatal (_("failed to initialize csv parser\n"));
    }

  /* Set some properties of the parser.  */
  if (csv2rec_strict)
    {
      options |= CSV_STRICT;
      csv_set_opts (&p, options);
    }

  csv_set_space_func (&p, is_space);
  csv_set_term_func  (&p, is_term);

  /* Parse the input file in chunks of data.  */
  while ((bytes_read = fread (buf, 1, 1024, in)) > 0)
    {
      if (csv_parse (&p, buf, bytes_read, field_cb, record_cb, &ctx) != bytes_read)
        {
          recutl_fatal (_("error while parsing CSV file: %s\n"),
                        csv_strerror (csv_error (&p)));
        }

    }
  
  return ctx.db;
}

int
main (int argc, char *argv[])
{
  int ret;
  rec_db_t db;
  rec_writer_t writer;

  recutl_init ("csv2rec");

  parse_args (argc, argv);
  db = process_csv ();
  ret = EXIT_SUCCESS;

  if (db)
    {
      writer = rec_writer_new (stdout);
      rec_write_db (writer, db);

      rec_writer_destroy (writer);
      rec_db_destroy (db);
    }
  else
    {
      ret = EXIT_FAILURE;
    }

  return ret;
}

/* End of csv2rec.c */