From 0af977933a6889ee266c69f4b03e7a5fd632d664 Mon Sep 17 00:00:00 2001 From: Derek Atkins Date: Tue, 13 Jan 2004 05:13:06 +0000 Subject: [PATCH] * src/import-export/csv/gnc-csv2glist.[ch]: add file from Kevin Hammack to parse CSV files. * src/import-export/csv/test/test.csv: a test CSV file git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@9766 57a11ea4-9604-0410-9ed3-97b8803252fd --- ChangeLog | 4 + src/import-export/csv/gnc-csv2glist.c | 186 ++++++++++++++++++++++++++ src/import-export/csv/gnc-csv2glist.h | 39 ++++++ src/import-export/csv/test/test.csv | 11 ++ 4 files changed, 240 insertions(+) create mode 100644 src/import-export/csv/gnc-csv2glist.c create mode 100644 src/import-export/csv/gnc-csv2glist.h create mode 100644 src/import-export/csv/test/test.csv diff --git a/ChangeLog b/ChangeLog index 25b8dff6b0..0924d31e47 100644 --- a/ChangeLog +++ b/ChangeLog @@ -9,6 +9,10 @@ * src/doc/Makefile.am: add new documentation: generic-druid-framework.txt * src/doc/generic-druid-framework.txt: new documentation + * src/import-export/csv/gnc-csv2glist.[ch]: add file from Kevin Hammack + to parse CSV files. + * src/import-export/csv/test/test.csv: a test CSV file + 2004-01-07 Derek Atkins * src/engine/Transaction.h: fix the xaccTransOrder() documentation diff --git a/src/import-export/csv/gnc-csv2glist.c b/src/import-export/csv/gnc-csv2glist.c new file mode 100644 index 0000000000..5f4685e757 --- /dev/null +++ b/src/import-export/csv/gnc-csv2glist.c @@ -0,0 +1,186 @@ + +/* + Copyright 2004 Kevin dot Hammack at comcast dot net + + 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. + + 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, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + */ + +//#include "config.h" + +#include +#include +#include +#include +#include + +//#include "file-utils.h" + +#define TEST_CSV + +/* + Yea, just returning a GList would be fine.. Or a GList of + GLists, one per row.. +*/ + + +/* + split_csv_list - Take a string, split on commas into a GList. + Tricky part: honor quotes. + Quotes aren't stripped. + This would have been both easier and cleaner in scheme, and / or + using regular expressions. + I was thinking small and fast when I wrote it. :-P +*/ + +static GList * +split_csv_line(char *line) { + GList *csvlist = NULL; + gchar *begin; + gchar *current; + gchar *cell; + gchar quote=0; + gboolean eol = FALSE; + + current = line; + begin = current; + + while (!eol) { + + if (quote && + (*current == quote) && (*(current-1) != '\\') && + (current != begin)) { + + quote = 0; + } + else if (!quote && (*current == '"')) { quote = '"'; } + else if (!quote && (*current == '\'')) { quote = '\''; } + + if (!quote && (*current == ',')) { *current = 0; } + + if (*current == '\n') { + *current = 0; + eol = TRUE; + } + + if (*current == 0) { + cell = g_strdup( begin ); + csvlist = g_list_prepend(csvlist, cell); + current++; + begin = current; + quote = 0; + } + else { + current++; + } + + } + + return g_list_reverse(csvlist); +} + + +#if 1 +gint64 +gnc_getline (gchar **line, FILE *file) +{ + char str[BUFSIZ]; + gint64 len; + GString *gs = g_string_new(""); + + if (!line || !file) return 0; + + while (fgets(str, sizeof(str), file) != NULL) { + g_string_append(gs, str); + + len = strlen(str); + if (str[len-1] == '\n') + break; + } + + len = gs->len; + *line = g_string_free(gs, FALSE); + return len; +} +#endif + + +GList * +gnc_csv_parse (FILE *handle) +{ + GList *csvlists = NULL; + ssize_t bytes_read; + char *line; + + while (bytes_read = gnc_getline (&line, handle) > 0) { + csvlists = g_list_prepend(csvlists, split_csv_line(line)); + g_free(line); + } + + return g_list_reverse(csvlists); +} + + + +#ifdef TEST_CSV + +static void print_glist_rec (GList *list) { + +} +// print_glist_rec(list); + +static void print_glist(GList *list, gpointer dummy) { + + printf("%d: (", g_list_length(list)); + + while (list != NULL) { + if ((list->data != NULL) && ( *((char *) list->data) != 0)) { + printf( "%s", list->data) ; + } + else { + printf( "\"\"" ); + } + list = list->next; + if (list) { + printf(" "); + } + } + + printf(")\n"); +} + +int main (int argc, char **argv) { + + FILE *fp; + int result; + GList *parsed_csv; + GList *current; + int dummy = 1; + + if (argc < 2) { + printf("usage:\n\tcsv2glist fname.csv\n"); + } + + fp = fopen (argv[1], "r"); + if (fp == NULL) return 1; + + parsed_csv = gnc_csv_parse(fp); + + g_list_foreach(parsed_csv, (GFunc)print_glist, &dummy); + +} + +#endif diff --git a/src/import-export/csv/gnc-csv2glist.h b/src/import-export/csv/gnc-csv2glist.h new file mode 100644 index 0000000000..2fa32495ec --- /dev/null +++ b/src/import-export/csv/gnc-csv2glist.h @@ -0,0 +1,39 @@ +/* + * gnc-csv2glist.h -- Parse Comma Separated Variable files + * + * Created by: Derek Atkins + * Copyright (c) 2004 Derek Atkins + * + * 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. + * + * 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, contact: + * + * Free Software Foundation Voice: +1-617-542-5942 + * 59 Temple Place - Suite 330 Fax: +1-617-542-2652 + * Boston, MA 02111-1307, USA gnu@gnu.org + */ + +#ifndef GNC_IMPORT_CSV_2GLIST_H +#define GNC_IMPORT_CSV_2GLIST_H + +/** + * gnc_csv_parse -- parse a Comma Separated Variable FILE into + * a list of lists of values. + * + * Args: file : the input file to read + * Returns : a list of lists of strings. Both lists and all strings + * must be g_free()'d by the caller. + */ + +GList * gnc_csv_parse (FILE *file); + +#endif diff --git a/src/import-export/csv/test/test.csv b/src/import-export/csv/test/test.csv new file mode 100644 index 0000000000..4f7cd84c7b --- /dev/null +++ b/src/import-export/csv/test/test.csv @@ -0,0 +1,11 @@ +"test1","test2","test3","test4" +"a","b","c","d" +1.1,2.2,3.3,4.4 +"weirdstuff","wierder''",, +,"", +, + +"\"","," +,",,,",",",","," + +,,",'",',""