mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
* 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
This commit is contained in:
parent
41bbf1ba6b
commit
0af977933a
@ -9,6 +9,10 @@
|
|||||||
* src/doc/Makefile.am: add new documentation: generic-druid-framework.txt
|
* src/doc/Makefile.am: add new documentation: generic-druid-framework.txt
|
||||||
* src/doc/generic-druid-framework.txt: new documentation
|
* 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 <derek@ihtfp.com>
|
2004-01-07 Derek Atkins <derek@ihtfp.com>
|
||||||
|
|
||||||
* src/engine/Transaction.h: fix the xaccTransOrder() documentation
|
* src/engine/Transaction.h: fix the xaccTransOrder() documentation
|
||||||
|
186
src/import-export/csv/gnc-csv2glist.c
Normal file
186
src/import-export/csv/gnc-csv2glist.c
Normal file
@ -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 <glib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
//#include "file-utils.h"
|
||||||
|
|
||||||
|
#define TEST_CSV
|
||||||
|
|
||||||
|
/*
|
||||||
|
<warlord> 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
|
39
src/import-export/csv/gnc-csv2glist.h
Normal file
39
src/import-export/csv/gnc-csv2glist.h
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* gnc-csv2glist.h -- Parse Comma Separated Variable files
|
||||||
|
*
|
||||||
|
* Created by: Derek Atkins <derek@ihtfp.com>
|
||||||
|
* Copyright (c) 2004 Derek Atkins <derek@ihtfp.com>
|
||||||
|
*
|
||||||
|
* 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
|
11
src/import-export/csv/test/test.csv
Normal file
11
src/import-export/csv/test/test.csv
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
"test1","test2","test3","test4"
|
||||||
|
"a","b","c","d"
|
||||||
|
1.1,2.2,3.3,4.4
|
||||||
|
"weirdstuff","wierder''",,
|
||||||
|
,"",
|
||||||
|
,
|
||||||
|
|
||||||
|
"\"",","
|
||||||
|
,",,,",",",",","
|
||||||
|
|
||||||
|
,,",'",',""
|
Can't render this file because it has a wrong number of fields in line 5.
|
Loading…
Reference in New Issue
Block a user