libcsv.c 3.54 KB
Newer Older
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
/*
 *		Loader for comma delimited database files
 *
 *		(c) Copright 1998 Building Number Three Ltd
 *
 *
 *		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
 *		2 of the License, or (at your option) any later version.
 */




/*
 *	Take a table of good old comma seperate junk, and load it into
 * 	a table
 */
 

#include <stdio.h>
#include <string.h> 
#include "libcsv.h"

static char linebuf[16384];		/* Hack for now */
char *csv_error;
int csv_line;

static char *csv_strdup(const char *p)
{
	char *n=strdup(p);
	if(n==NULL)
		csv_error="out of memory";
	return n;
}

static int count_fields(char *ptr)
{	
	int count=0;
	int quoted=0;
	
	if(*ptr==0)
		return -1;		/* Blank line flag */
		
	while(*ptr)
	{
		/* Embedded quote */
		if(*ptr=='"' && ptr[1]=='"')
		{
			ptr+=2;
			continue;
		}
		if(*ptr=='"')
			quoted=1-quoted;
		else if(*ptr==',')
			count+=1-quoted;
		ptr++;
	}
	return count+1;
}

static char *cut_quoted(char **p)
{
	char *x;
	char *e;
	char *n;
	
	x=*p;
	x++;	/* Open quote */
	
	e=x;
	
	while(*x)
	{
		if(*x=='"' && x[1]=='"')
		{
			memmove(x,x+1,strlen(x));
			x++;
			continue;
		}
		if(*x=='"')
		{
			*x=0;
			x++;
/*			printf("%p %p (%s)\n", e, x, e);*/
			n=csv_strdup(e);
			*p=x;
			return n;
		}
		x++;
	}
	/* Error */
	csv_error="missing quote";
	return NULL;
}		

static char *cut_comma(char **ptr)
{
	char *e=strchr(*ptr,',');
	char *n=*ptr;
	
	if(e==NULL)
	{
		e=n+strlen(n);
		*ptr=e;
		return csv_strdup(n);
	}
	*e=0;
	*ptr=e+1;
	return csv_strdup(n);
}

static int smash_fields(char *ptr, char **array, int len)
{
	if(*ptr==0)
	{
		int i;
		for(i=0;i<len;i++)
			array[i]="";
		return 0;
	}
	

	while(*ptr)
	{
		if(*ptr=='"')
			*array=cut_quoted(&ptr);
		else
			*array=cut_comma(&ptr);
/*		printf("[%s]\n", *array);*/
		if(*array++ == NULL)
			return -1;
	}
	return 0;
}


static char * read_line(FILE *f)
{
	int n;
	if(fgets(linebuf, sizeof(linebuf),f)==NULL)
		return NULL;
	csv_line++;
	n=strlen(linebuf);
	if(n && linebuf[n-1]=='\n')
		linebuf[--n]=0;
	if(n && linebuf[n-1]=='\r')
		linebuf[n-1]=0;
	return linebuf;
}
			
int csv_load_table(FILE *f, struct csv_table *ptr)
{
	int width;
	int length;
	int size;
	
	csv_line = -1;
	
	if(read_line(f)==NULL)
		return -1;
	
	size=256;
	
	ptr->row=(struct csv_row *)malloc(sizeof(struct csv_row)*size);
	if(ptr->row==NULL)
	{
		csv_error="out of memory";
		return -1;
	}
	length=0;
	
	do
	{
		width = count_fields(linebuf);
		if(width < 1)
			width = 1;
		ptr->row[length].data=(char **)malloc(width*sizeof(char *));
		ptr->row[length].width = width;
		if(ptr->row[length].data==NULL)
		{
			csv_error="out of memory";
			return -1;
		}
		memset(ptr->row[length].data, 0, width*sizeof(char *));
		
		if(smash_fields(linebuf, ptr->row[length].data, width)==-1)
			return -1;
		length++;
		
/*		printf("-------------\n");*/
		fflush(stdout);
		if(length==size)
		{
			size+=256;
			ptr->row=(struct csv_row *)realloc(ptr->row, sizeof(struct csv_row)*size);
			if(ptr->row==NULL)
			{
				csv_error="out of memory";
				return -1;
			}
		}
		if(read_line(f)==NULL)
			break;
	}
	while(1);
	ptr->height = length;
	ptr->size = size;
	return 0;
}

void csv_destroy_table(struct csv_table *table)
{
	int i, j;
		
	for(i=0;i<table->height;i++)
	{
		struct csv_row *r=&table->row[i];
		for(j=0;j<r->width;j++)
			if(r->data[j])
				free(r->data[j]);
	}
224
	free (table->row);
225 226 227 228 229 230 231 232 233
}

#ifdef TEST
int main(int argc, char *argv)
{
	struct csv_table t;
	printf("Got %d\n", parse_table(stdin, &t));
}
#endif