2012-09-01 17:33:33 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* tools/csvparser.c
|
|
|
|
*
|
2021-02-19 11:57:46 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-09-01 17:33:33 +02:00
|
|
|
*
|
2021-02-19 11:57:46 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-09-01 17:33:33 +02:00
|
|
|
*
|
2021-02-19 11:57:46 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-09-01 17:33:33 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "csvparser.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-04-08 17:15:17 +02:00
|
|
|
* Pre-processor Definitions
|
2012-09-01 17:33:33 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
bool g_debug;
|
2021-02-19 11:29:43 +01:00
|
|
|
char g_line[LINESIZE + 1];
|
2012-09-01 17:33:33 +02:00
|
|
|
char g_parm[MAX_FIELDS][MAX_PARMSIZE];
|
|
|
|
int g_lineno;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-03 17:16:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: skip_space
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-01 17:33:33 +02:00
|
|
|
static char *skip_space(char *ptr)
|
|
|
|
{
|
|
|
|
while (*ptr && isspace((int)*ptr)) ptr++;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2012-09-03 17:16:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: copy_parm
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-01 17:33:33 +02:00
|
|
|
static char *copy_parm(char *src, char *dest)
|
|
|
|
{
|
|
|
|
char *start = src;
|
|
|
|
int i;
|
|
|
|
|
2012-09-03 17:16:32 +02:00
|
|
|
/* De-quote the parameter and copy it into the parameter array */
|
|
|
|
|
2012-09-01 17:33:33 +02:00
|
|
|
for (i = 0; i < MAX_PARMSIZE; i++)
|
|
|
|
{
|
|
|
|
if (*src == '"')
|
|
|
|
{
|
|
|
|
*dest = '\0';
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
else if (*src == '\n' || *src == '\0')
|
|
|
|
{
|
2021-02-19 11:29:43 +01:00
|
|
|
fprintf(stderr, "%d: Unexpected end of line: \"%s\"\n",
|
|
|
|
g_lineno, start);
|
2012-09-01 17:33:33 +02:00
|
|
|
exit(4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*dest++ = *src++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "%d: Parameter too long: \"%s\"\n", g_lineno, start);
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
|
2012-09-03 17:16:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: find_parm
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-01 17:33:33 +02:00
|
|
|
static char *find_parm(char *ptr)
|
|
|
|
{
|
|
|
|
char *start = ptr;
|
|
|
|
|
|
|
|
if (*ptr != '"')
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%d: I'm confused: \"%s\"\n", g_lineno, start);
|
|
|
|
exit(5);
|
|
|
|
}
|
2021-02-19 11:29:43 +01:00
|
|
|
|
2012-09-01 17:33:33 +02:00
|
|
|
ptr++;
|
|
|
|
|
|
|
|
ptr = skip_space(ptr);
|
|
|
|
if (*ptr == '\n' || *ptr == '\0')
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (*ptr != ',')
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%d: Expected ',': \"%s\"\n", g_lineno, start);
|
|
|
|
exit(6);
|
|
|
|
}
|
2021-02-19 11:29:43 +01:00
|
|
|
|
2012-09-01 17:33:33 +02:00
|
|
|
ptr++;
|
|
|
|
|
|
|
|
ptr = skip_space(ptr);
|
|
|
|
if (*ptr != '"')
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%d: Expected \": \"%s\"\n", g_lineno, start);
|
|
|
|
exit(7);
|
|
|
|
}
|
2021-02-19 11:29:43 +01:00
|
|
|
|
2012-09-01 17:33:33 +02:00
|
|
|
ptr++;
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-03 17:16:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: read_line
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-01 17:33:33 +02:00
|
|
|
char *read_line(FILE *stream)
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
|
2021-02-19 11:29:43 +01:00
|
|
|
for (; ; )
|
2012-09-01 17:33:33 +02:00
|
|
|
{
|
|
|
|
g_line[LINESIZE] = '\0';
|
|
|
|
if (!fgets(g_line, LINESIZE, stream))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_lineno++;
|
|
|
|
if (g_debug)
|
|
|
|
{
|
|
|
|
printf("Line: %s\n", g_line);
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = skip_space(g_line);
|
|
|
|
if (*ptr && *ptr != '#' && *ptr != '\n')
|
|
|
|
{
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-03 17:16:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: parse_csvline
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-01 17:33:33 +02:00
|
|
|
int parse_csvline(char *ptr)
|
|
|
|
{
|
|
|
|
int nparms;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Format "arg1","arg2","arg3",... Spaces will be tolerated outside of the
|
|
|
|
* quotes. Any initial spaces have already been skipped so the first thing
|
|
|
|
* should be '"'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (*ptr != '"')
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%d: Bad line: \"%s\"\n", g_lineno, g_line);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
nparms = 0;
|
|
|
|
|
2012-09-03 17:16:32 +02:00
|
|
|
/* Copy each comma-separated value in an array (stripping quotes from each
|
|
|
|
* of the values).
|
|
|
|
*/
|
|
|
|
|
2012-09-01 17:33:33 +02:00
|
|
|
do
|
|
|
|
{
|
2018-01-04 20:36:14 +01:00
|
|
|
if (nparms >= MAX_FIELDS)
|
2017-12-21 19:37:10 +01:00
|
|
|
{
|
2021-02-19 11:29:43 +01:00
|
|
|
fprintf(stderr, "%d: Too many Parameters: \"%s\"\n",
|
|
|
|
g_lineno, g_line);
|
2017-12-21 19:37:10 +01:00
|
|
|
exit(8);
|
|
|
|
}
|
|
|
|
|
2012-09-01 17:33:33 +02:00
|
|
|
ptr = copy_parm(ptr, &g_parm[nparms][0]);
|
|
|
|
nparms++;
|
|
|
|
ptr = find_parm(ptr);
|
|
|
|
}
|
|
|
|
while (ptr);
|
|
|
|
|
2012-09-03 17:16:32 +02:00
|
|
|
/* If debug is enabled, show what we got */
|
|
|
|
|
2012-09-01 17:33:33 +02:00
|
|
|
if (g_debug)
|
|
|
|
{
|
|
|
|
printf("Parameters: %d\n", nparms);
|
|
|
|
for (i = 0; i < nparms; i++)
|
|
|
|
{
|
2021-02-19 11:29:43 +01:00
|
|
|
printf(" Parm%d: \"%s\"\n", i + 1, g_parm[i]);
|
2012-09-01 17:33:33 +02:00
|
|
|
}
|
|
|
|
}
|
2012-09-03 17:16:32 +02:00
|
|
|
|
2012-09-01 17:33:33 +02:00
|
|
|
return nparms;
|
|
|
|
}
|