Thread: [q-lang-cvs] q-csv csv.c,1.9,1.10 csv.q,1.9,1.10
Brought to you by:
agraef
From: RER <ed...@us...> - 2008-01-17 20:54:25
|
Update of /cvsroot/q-lang/q-csv In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv12881 Modified Files: csv.c csv.q Log Message: added sreadcsv/swritecsv routines, updated routines to have multichar delimiters Index: csv.c =================================================================== RCS file: /cvsroot/q-lang/q-csv/csv.c,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** csv.c 14 Jan 2008 06:15:17 -0000 1.9 --- csv.c 17 Jan 2008 20:54:18 -0000 1.10 *************** *** 1,3 **** ! /* CSV according to RFC 4180 (http://tools.ietf.rg/html/rfc4180) This is free software; you can redistribute it and/or --- 1,3 ---- ! /* CSV according to RFC 4180 (http://tools.ietf.rg/html/rfc4180.txt) This is free software; you can redistribute it and/or *************** *** 17,21 **** $Id$ ! Written by Eddie Rucker 3-13 Jan, 2008 */ #include <stdio.h> --- 17,21 ---- $Id$ ! Written by Eddie Rucker 3-17 Jan, 2008 */ #include <stdio.h> *************** *** 29,86 **** #define AUTOQUOTE 1 ! /* fread_block reads embbeded '\n's ! Does not account for badly formatted records */ FUNCTION (csv, fread_csvstr, argc, argv) { FILE *fp; ! char *buff, *bp, *tp, *strdl, *s; ! int len = 0, q_cnt = 0, size = BSIZE; ! register int c; ! if (argc != 2 ! || !isfile(argv[0], &fp) ! || !isstr(argv[1], &strdl)) return __FAIL; ! if (!(buff = (char *)malloc(size))) ! return NULL; ! bp = buff; ! while ((c = fgetc(fp)) != EOF) { ! if (len > size) { ! if (!(tp = (char *)realloc(buff, size <<= 1))) { ! free(buff); return __ERROR; } ! buff = tp; ! bp = buff + len; } - if (c == '\n' && !(q_cnt & 1)) - break; - if (c == *strdl) - ++q_cnt; - ++len; - *bp++ = c; } - *bp++ = c; - *bp = 0; - s = to_utf8(buff, NULL); - free(buff); - if (s) - return mkstr(s); - else - return __ERROR; } - /* convience function to convert numbers */ ! expr *convert(char *s, int cvt_flag) { long i; double d; char *p, *t; ! if (cvt_flag) { i = strtol(s, &p, 0); if (*p == 0) --- 29,93 ---- #define AUTOQUOTE 1 ! /* fread_block reads embbeded '\n's. ! Does not account for badly formatted records. ! */ FUNCTION (csv, fread_csvstr, argc, argv) { FILE *fp; ! char *bf, *tb, *s_dl, *s; ! int n_q = 0, n_s_dl; ! long sz = BSIZE, n = 0; ! if (argc != 2 || !isfile(argv[0], &fp) || !isstr(argv[1], &s_dl)) return __FAIL; ! if (!(s = bf = (char *)malloc(sz))) ! return __ERROR; ! n_s_dl = strlen(s_dl); ! while (1) { ! s = bf + n; ! if (n + BSIZE > sz) { ! if (!(tb = realloc(bf, sz <<= 1))) { ! free(bf); return __ERROR; } ! s = (bf = tb) + n; ! } ! fgets(s, BSIZE, fp); ! if (ferror(fp)) { ! free(bf); ! return __ERROR; ! } ! if (feof(fp)) ! goto fini; ! n += strlen(s); ! if (!*(bf+n-1) == '\n') ! continue; ! while (*s) { ! if (!strncmp(s, s_dl, n_s_dl)) { ! ++n_q; ! s += n_s_dl; ! } else ! ++s; ! } ! if (!(n_q & 1)) { ! fini: ! s = to_utf8(bf, NULL); ! free(bf); ! return mkstr(s); } } } /* convience function to convert numbers */ ! expr *convert(char *s, int cvt_f) { long i; double d; char *p, *t; ! if (cvt_f) { i = strtol(s, &p, 0); if (*p == 0) *************** *** 95,120 **** } ! #define putfld \ ! if (fld_len >= fld_size - 1) { \ ! if (!(tfield = (char *)realloc(field, fld_size <<= 1))) \ ! goto st30; \ ! field = tfield; \ ! } \ ! fieldp = field + fld_len++; \ ! *fieldp++ = *sp ! ! ! /* force == 0 forces the field to be quoted */ ! ! #define putrec(force) \ ! *fieldp = 0; \ ! if (rec_len >= rec_size - 1) { \ ! if (!(trec = (expr *)realloc(rec, (rec_size += 16)*sizeof(expr)))) \ ! goto st30; \ rec = trec; \ } \ ! if ((rec[rec_len++] = convert(field, cvt_f&force)) == NULL) \ ! goto st30 --- 102,126 ---- } + #define putfld(len) \ + if (n_fld + len >= fld_sz) { \ + if (!(tfld = (char *)realloc(fld, fld_sz <<= 1))) \ + goto done; \ + fld = tfld; \ + } \ + fldp = fld + n_fld; \ + strncpy(fldp, s, len); \ + n_fld += len \ + + /* qt == 0 forces the field to be quoted */ ! #define putrec(qt) \ ! *(fldp + 1) = 0; \ ! if (n_rec >= rec_sz - 1) { \ ! if (!(trec = (expr *)realloc(rec, (rec_sz += 64)*sizeof(expr)))) \ ! goto done; \ rec = trec; \ } \ ! if ((rec[n_rec++] = convert(fld, qt)) == NULL) \ ! goto done *************** *** 135,143 **** FUNCTION(csv, csvstr_to_tuple, argc, argv) { ! int n, cvt_f, fld_size = 256, fld_len, rec_size = 64, ws_cnt = 0, ! rec_len = 0, exit_type = 0; ! char *field, *tfield, *fieldp, *s, *flddl, *strdl, errmsg[80]; expr *xs, *rec, *trec; ! register char *sp; if (argc != 2 --- 141,149 ---- FUNCTION(csv, csvstr_to_tuple, argc, argv) { ! int n, cvt_f, fld_sz = 256, n_fld, rec_sz = 64, n_ws = 0, ! n_rec = 0, n_s_dl, n_fld_dl, st = 0; ! char *fld, *tfld, *fldp, *s, *fld_dl, *s_dl, errmsg[80]; expr *xs, *rec, *trec; ! char *sp; if (argc != 2 *************** *** 145,290 **** || n != 3 || !isbool(xs[0], &cvt_f) ! || !isstr(xs[1], &flddl) ! || !isstr(xs[2], &strdl) || !isstr(argv[1], &s)) return __FAIL; ! ! if (!(field = (char *)malloc(fld_size))) return __ERROR; ! if (!(rec = (expr *)malloc(rec_size*sizeof(expr)))) { ! free(field); return __ERROR; } ! ! sp = s - 1; ! st0: ++sp; ! fieldp = field; ! fld_len = 0; ! if (!strncmp(sp, flddl, 1)) { ! putrec(FORCEQUOTE); ! goto st0; ! } else if (!strncmp(sp, strdl, 1)) { ! goto st1; ! } else if (*sp == '\n' || *sp == 0) { ! putrec(FORCEQUOTE); ! goto st10; ! } else if (isspace(*sp)) { ! /* fld_dlm might be ws make sure it is after (c == *fld_dlm) */ ! goto st0; ! } else { ! putfld; ! goto st4; ! } ! st1: ++sp; ! if (!strncmp(sp, strdl, 1)) { ! goto st2; ! } else if (*sp == 0) { ! goto st20; ! } else { ! putfld; ! goto st1; ! } ! st2: ++sp; ! if (!strncmp(sp, strdl, 1)) { ! putfld; ! goto st1; ! } else if (!strncmp(sp, flddl, 1)) { ! putrec(FORCEQUOTE); ! goto st0; ! } else if (*sp == '\n' || *sp == 0) { ! putrec(FORCEQUOTE); ! goto st10; ! } else if (isspace(*sp)) { ! /* fld_dlm might be ws make sure it is after (c == *fld_dlm) */ ! goto st3; ! } else { ! goto st20; ! } ! st3: ++sp; ! if (!strncmp(sp, flddl, 1)) { ! putrec(FORCEQUOTE); ! goto st0; ! } else if (*sp == '\n' || *sp == 0) { ! putrec(FORCEQUOTE); ! goto st10; ! } else if (isspace(*sp)) { ! /* fld_dlm might be ws make sure it is after (c == *fld_dlm) */ ! goto st3; ! } else { ! goto st20; ! } ! st4: ++sp; ! if (!strncmp(sp, flddl, 1)) { ! putrec(AUTOQUOTE); ! goto st0; ! } else if (*sp == '\n' || *sp == 0) { ! putrec(AUTOQUOTE); ! goto st10; ! } else if (!strncmp(sp, strdl, 1)) { ! goto st20; ! } else if (isspace(*sp)) { ! /* fld_dlm might be ws make sure it is after (c == *fld_dlm) */ ! ws_cnt = 1; ! putfld; ! goto st5; ! } else { ! putfld; ! goto st4; ! } ! st5: ++sp; ! if (!strncmp(sp, flddl, 1)) { ! fieldp -= ws_cnt; /* get rid of ws at the end of the field */ ! putrec(AUTOQUOTE); ! goto st0; ! } else if (*sp == '\n' || *sp == 0) { ! fieldp -= ws_cnt; /* get rid of ws at the end of the field */ ! putrec(AUTOQUOTE); ! goto st10; ! } else if (isspace(*sp)) { ! /* fld_dlm might be ws make sure it is after (c == *fld_dlm) */ ! ++ws_cnt; ! putfld; ! goto st5; ! } else { ! putfld; ! goto st4; } ! st10: ! free(field); ! rec = realloc(rec, rec_len*sizeof(expr)); ! return mktuplev(rec_len, rec); ! st20: ! exit_type = 1; ! st30: ! free(field); ! for (n = 0; n < rec_len; ++n) ! dispose(rec[n]); ! free(rec); ! if (exit_type == 1) { ! sprintf(errmsg, "Column %d: Unexpected end of file.", (fld_len+1)); ! return mkapp(mksym(sym(csv_error)), mkstr(strdup(errmsg))); } - return __ERROR; } - #define resize_str \ ! if (len > size) { \ ! if (!(tstr = realloc(str, sizeof(char)*(size <<= 1)))) { \ ! free(str); \ return __ERROR; \ } \ ! str = tstr; \ } \ ! t = str + mark #define insert_num \ ! mark = len; \ ! len += strlen(tbuff); \ resize_str; \ ! strncpy(t, tbuff, len - mark) ! /* convert tuple to csv string --- 151,297 ---- || n != 3 || !isbool(xs[0], &cvt_f) ! || !isstr(xs[1], &fld_dl) ! || !isstr(xs[2], &s_dl) || !isstr(argv[1], &s)) return __FAIL; ! ! if (!(fld = (char *)malloc(fld_sz))) return __ERROR; ! if (!(rec = (expr *)malloc(rec_sz*sizeof(expr)))) { ! free(fld); return __ERROR; } ! ! n_s_dl = strlen(s_dl); ! n_fld_dl = strlen(fld_dl); ! fldp = fld; ! while (st < 10) { ! switch (st) { ! case 0: ! fldp = fld; ! n_fld = 0; ! if (!strncmp(s, fld_dl, n_fld_dl)) { ! *fldp = 0; ! putrec(FORCEQUOTE); ! s += n_fld_dl; ! } else if (!strncmp(s, s_dl, n_s_dl)) { ! s += n_s_dl; ! st = 1; ! } else if (!*s || *s == '\n' || *s == EOF) { ! putrec(FORCEQUOTE); ! st = 10; ! } else if (isspace(*s)) { ! ++s; ! } else { ! putfld(1); ! ++s; ! st = 4; ! } ! break; ! case 1: ! if (!strncmp(s, s_dl, n_s_dl)) { ! s += n_s_dl; ! st = 2; ! } else if (!*s || *s == EOF) { ! sprintf(errmsg, "Column %d: Expected {%s}.", n_fld+1, s_dl); ! st = 20; ! } else { ! putfld(1); ! ++s; ! } ! break; ! case 2: ! if (!strncmp(s, s_dl, n_s_dl)) { ! putfld(n_s_dl); ! s += n_s_dl; ! st = 1; ! } else if (!strncmp(s, fld_dl, n_fld_dl)) { ! putrec(FORCEQUOTE); ! s += n_fld_dl; ! st = 0; ! } else if (!*s || *s == '\n' || *s == EOF) { ! putrec(FORCEQUOTE); ! st = 10; ! } else if (isspace(*s)) { ! ++s; ! st = 3; ! } else { ! sprintf(errmsg, "Column %d: Expected {%s}.", n_fld+1, fld_dl); ! st = 20; ! } ! break; ! case 3: ! if (!strncmp(s, fld_dl, n_fld_dl)) { ! putrec(FORCEQUOTE); ! s += n_fld_dl; ! st = 0; ! } else if (!*s || *s == '\n' || *s == EOF) { ! putrec(FORCEQUOTE); ! st = 10; ! } else if (isspace(*s)) { ! ++s; ! } else { ! sprintf(errmsg, "Column %d: Expected {%s}.", n_fld+1, fld_dl); ! st = 20; ! } ! break; ! case 4: ! if (!strncmp(s, s_dl, n_s_dl)) { ! sprintf(errmsg, "Column %d: Expected {%s}.", n_fld+1, fld_dl); ! st = 20; ! } else if (!strncmp(s, fld_dl, n_fld_dl)) { ! fldp -= n_ws; ! n_fld -= n_ws; ! putrec(cvt_f); ! s += n_fld_dl; ! st = 0; ! } else if (!*s || *s == '\n' || *s == EOF) { ! putrec(cvt_f); ! st = 10; ! } else if (isspace(*s)) { ! n_ws = n_ws ? n_ws+1 : 1; ! putfld(1); ! ++s; ! } else { ! n_ws = 0; ! putfld(1); ! ++s; ! } ! break; ! } } ! done: ! free(fld); ! switch (st) { ! case 10: ! rec = realloc(rec, sizeof(expr)*n_rec); ! return mktuplev(n_rec, rec); ! default: ! for (n = 0; n < n_rec; ++n) ! dispose(rec[n]); ! free(rec); ! if (st == 20) ! return mkapp(mksym(sym(csv_error)), mkstr(strdup(errmsg))); ! return __ERROR; } } #define resize_str \ ! if (len > sz) { \ ! if (!(ts = (char *)realloc(s, sz <<= 1))) { \ ! free(s); \ return __ERROR; \ } \ ! s = ts; \ } \ ! t = s + mrk #define insert_num \ ! mrk = len; \ ! len += strlen(tb); \ resize_str; \ ! strncpy(t, tb, len - mrk) /* convert tuple to csv string *************** *** 304,309 **** FUNCTION (csv, tuple_to_csvstr, argc, argv) { ! int i, n, size = 256, mark, qcnt, len = 0, cvt_f; ! char *str, *tstr, *sval, *strdl, *flddl, *p, tbuff[48], errmsg[80]; long ival; double dval; --- 311,316 ---- FUNCTION (csv, tuple_to_csvstr, argc, argv) { ! int i, n, sz = 256, mrk, n_q, len = 0, cvt_f, n_s_dl, n_fld_dl; ! char *s, *ts, *p, *sval, *s_dl, *fld_dl, tb[48], errmsg[80]; long ival; double dval; *************** *** 315,375 **** || i != 3 || !isbool(ys[0], &cvt_f) /* unquote flag */ ! || !isstr(ys[1], &flddl) ! || !isstr(ys[2], &strdl) || !istuple(argv[1], &n, &xs)) return __FAIL; ! if (!(str = (char *)malloc(size))) return __ERROR; ! for (i = 0; i < n; ++i) { if (isint(xs[i], &ival)) { if (cvt_f) ! sprintf(tbuff, "%d%s", ival, flddl); else ! sprintf(tbuff, "%s%d%s%s", strdl, ival, strdl, flddl); insert_num; } else if (isfloat(xs[i], &dval)) { if (cvt_f) ! sprintf(tbuff, "%.16g%s", dval, flddl); else ! sprintf(tbuff, "%s%.16g%s%s", strdl, dval, strdl, flddl); insert_num; } else if (isstr(xs[i], &sval)) { ! qcnt = 0; p = sval; ! while (*p) ! if (!strncmp(p++, strdl, 1)) ! ++qcnt; ! mark = len; ! len += p - sval + qcnt + 3; /* strlen + double strdls + dquotes */ resize_str; p = sval; ! *t++ = *strdl; ! while (*t++ = *p) { ! if (!strncmp(p, strdl, 1)) ! *t++ = *p; ! ++p; } ! --t; ! *t++ = *strdl; ! *t++ = *flddl; } else { ! sprintf(errmsg, "Field %d: Invalid conversion type.", (i+1)); return mkapp(mksym(sym(csv_error)), mkstr(strdup(errmsg))); } } ! mark = len; #if defined (_WIN32) ! len += 2; resize_str; *(t-2) = '\r'; #else - ++len; resize_str; #endif *(t-1) = '\n'; *t = 0; ! str = (char *)realloc(str, len); ! return mkstr(str); } --- 322,393 ---- || i != 3 || !isbool(ys[0], &cvt_f) /* unquote flag */ ! || !isstr(ys[1], &fld_dl) ! || !isstr(ys[2], &s_dl) || !istuple(argv[1], &n, &xs)) return __FAIL; ! if (!(s = (char *)malloc(sz))) return __ERROR; ! ! n_s_dl = strlen(s_dl); ! n_fld_dl = strlen(fld_dl); for (i = 0; i < n; ++i) { if (isint(xs[i], &ival)) { if (cvt_f) ! sprintf(tb, "%d%s", ival, fld_dl); else ! sprintf(tb, "%s%d%s%s", s_dl, ival, s_dl, fld_dl); insert_num; } else if (isfloat(xs[i], &dval)) { if (cvt_f) ! sprintf(tb, "%.16g%s", dval, fld_dl); else ! sprintf(tb, "%s%.16g%s%s", s_dl, dval, s_dl, fld_dl); insert_num; } else if (isstr(xs[i], &sval)) { ! n_q = 0; p = sval; ! while (*p) { ! if (!strncmp(p, s_dl, n_s_dl)) ! ++n_q; ! ++p; ! } ! mrk = len; ! /* strlen + double s_dls + dquotes + fld_dl */ ! len += (p - sval + 1) + n_s_dl*(1 + n_q) + n_fld_dl; resize_str; p = sval; ! strncpy(t, s_dl, n_s_dl); ! t += n_s_dl; ! while (*p) { ! if (!strncmp(p, s_dl, n_s_dl)) { ! strncpy(t, s_dl, n_s_dl); ! t += n_s_dl; ! strncpy(t, s_dl, n_s_dl); ! t += n_s_dl; ! p += n_s_dl; ! } else ! *t++ = *p++; } ! strncpy(t, s_dl, n_s_dl); ! t += n_s_dl; ! strncpy(t, fld_dl, n_fld_dl); ! t += n_fld_dl; } else { ! sprintf(errmsg, "Field %d: Invalid conversion type.", i+1); return mkapp(mksym(sym(csv_error)), mkstr(strdup(errmsg))); } } ! mrk = (len -= n_fld_dl - 1); /* write over last fld_dl */ #if defined (_WIN32) ! ++len; /* guarantee enough space for for \r\n */ resize_str; *(t-2) = '\r'; #else resize_str; #endif *(t-1) = '\n'; *t = 0; ! s = (char *)realloc(s, len); ! return mkstr(s); } Index: csv.q =================================================================== RCS file: /cvsroot/q-lang/q-csv/csv.q,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** csv.q 14 Jan 2008 04:28:03 -0000 1.9 --- csv.q 17 Jan 2008 20:54:18 -0000 1.10 *************** *** 49,55 **** NOTES ! (1) A properly formated CSV string MUST end with a '\n'. ! (2) When the auto conversion flag is false, all fields are strings. ! (3) The 'csv_error MSG' rule is invoked on improperly formatted strings. */ --- 49,54 ---- NOTES ! (1) When the auto conversion flag is false, all fields are strings. ! (2) The 'csv_error MSG' rule is invoked on improperly formatted strings. */ *************** *** 57,60 **** --- 56,125 ---- /* User may define csv_error for custom error handling. */ + /* String functions */ + public sreadcsv_data ARGS; + sreadcsv_data S:String + = csvstr_to_tuple (true, ",", "\"") S; + sreadcsv_data (S:String, FieldDelim:String) + = csvstr_to_tuple (true, FieldDelim, "\"") S; + sreadcsv_data (S:String, FieldDelim:String, StrDelim:String) + = csvstr_to_tuple (true, FieldDelim, StrDelim) S; + + public sreadcsv_string ARGS; + sreadcsv_string S:String + = csvstr_to_tuple (false, ",", "\"") S; + sreadcsv_string (S:String, FieldDelim:String) + = csvstr_to_tuple (false, FieldDelim, "\"") S; + sreadcsv_string (S:String, FieldDelim:String, StrDelim:String) + = csvstr_to_tuple (false, FieldDelim, StrDelim) S; + + public sreadcsvlist_data ARGS; + sreadcsvlist_data L:List + = map (csvstr_to_tuple (true, ",", "\"")) L; + sreadcsvlist_data (S:String, FieldDelim:String) + = map (csvstr_to_tuple (true, FieldDelim, "\"")) L; + sreadcsvlist_data (S:String, FieldDelim:String, StrDelim:String) + = map (csvstr_to_tuple (true, FieldDelim, StrDelim)) L; + + public sreadcsvlist_string ARGS; + sreadcsvlist_string L:List + = map (csvstr_to_tuple (false, ",", "\"")) L; + sreadcsvlist_string (S:String, FieldDelim:String) + = map (csvstr_to_tuple (false, FieldDelim, "\"")) L; + sreadcsvlist_string (S:String, FieldDelim:String, StrDelim:String) + = map (csvstr_to_tuple (false, FieldDelim, StrDelim)) L; + + public swritecsv_data ARGS; + swritecsv_data Rec:Tuple + = tuple_to_csvstr (true, ",", "\"") Rec; + swritecsv_data (Rec:Tuple, FieldDelim:String) + = tuple_to_csvstr (true, FieldDelim, "\"") Rec; + swritecsv_data (Rec:Tuple, FieldDelim:String, StrDelim:String) + = tuple_to_csvstr (true, FieldDelim, StrDelim) Tec; + + public swritecsv_string ARGS; + swritecsv_string Rec:Tuple + = tuple_to_csvstr (false, ",", "\"") Rec; + swritecsv_string (Rec:Tuple, FieldDelim:String) + = tuple_to_csvstr (false, FieldDelim, "\"") Rec; + swritecsv_string (Rec:Tuple, FieldDelim:String, StrDelim:String) + = tuple_to_csvstr (false, FieldDelim, StrDelim) Rec; + + public swritecsvlist_data ARGS; + swritecsvlist_data L:List + = map (tuple_to_csvstr (true, ",", "\"")) L; + swritecsvlist_data (L:List, FieldDelim:String) + = map (tuple_to_csvstr (true, FieldDelim, "\"")) L; + swritecsvlist_data (L:List, FieldDelim:String, StrDelim:String) + = map (tuple_to_csvstr (true, FieldDelim, StrDelim)) L; + + public swritecsvlist_string ARGS; + swritecsvlist_string L:List + = map (tuple_to_csvstr (false, ",", "\"")) L; + swritecsvlist_string (L:List, FieldDelim:String) + = map (tuple_to_csvstr (false, FieldDelim, "\"")) L; + swritecsvlist_string (L:List, FieldDelim:String, StrDelim:String) + = map (tuple_to_csvstr (false, FieldDelim, StrDelim)) L; + + public freadcsv_data ARGS; /* Read one CSV record from the file with number conversion. */ *************** *** 62,65 **** --- 127,132 ---- freadcsv_data F:File = csvstr_to_tuple (true, ",", "\"") (fread_csvstr F "\""); + freadcsv_data (F:File, FieldDelim:String) + = csvstr_to_tuple (true, FieldDelim, "\"") (fread_csvstr F "\""); freadcsv_data (F:File, FieldDelim:String, StrDelim:String) = csvstr_to_tuple (true, FieldDelim, StrDelim) (fread_csvstr F StrDelim); *************** *** 69,75 **** freadcsv_string F:File ! = csvstr_to_tuple (false, "," "\"") (fread_csvstr F "\""); freadcsv_string (F:File, FieldDelim:String, StrDelim:String) ! = csvstr_to_tuple (false, "," "\"") (fread_csvstr F StrDelim); public fwritecsv_data FILE REC; --- 136,171 ---- freadcsv_string F:File ! = csvstr_to_tuple (false, ",", "\"") (fread_csvstr F "\""); ! freadcsv_string (F:File, FieldDelim:String) ! = csvstr_to_tuple (false, FieldDelim, "\"") (fread_csvstr F "\""); freadcsv_string (F:File, FieldDelim:String, StrDelim:String) ! = csvstr_to_tuple (false, ",", "\"") (fread_csvstr F StrDelim); ! ! public freadcsvlist_data ARGS, freadcsvlist_string ARGS; ! /* Read an entire file of records into a list of tuples. */ ! ! freadcsvlist_data F:File ! = [] if feof F; ! = [freadcsv_data F | freadcsvlist_data F]; ! freadcsvlist_data (F:File, FieldDelim:String) ! = [] if feof F; ! = [freadcsv_data (F, FieldDelim) ! | freadcsvlist_data (F, FieldDelim)]; ! freadcsvlist_data (F:File, FieldDelim:String, StrDelim:String) ! = [] if feof F; ! = [freadcsv_data (F, FieldDelim, StrDelim) ! | freadcsvlist_data (F, FieldDelim, StrDelim)]; ! ! freadcsvlist_string F:File ! = [] if feof F; ! = [freadcsv_string F | freadcsvlist_data F]; ! freadcsvlist_string (F:File, FieldDelim:String) ! = [] if feof F; ! = [freadcsv_string (F, FieldDelim) ! | freadcsvlist_string (F, FieldDelim)]; ! freadcsvlist_string (F:File, FieldDelim:String, StrDelim:String) ! = [] if feof F; ! = [freadcsv_string (F, FieldDelim, StrDelim) ! | freadcsvlist_string (F, FieldDelim, StrDelim)]; public fwritecsv_data FILE REC; *************** *** 78,81 **** --- 174,179 ---- fwritecsv_data F:File Rec:Tuple = fwrites F (tuple_to_csvstr (true, ",", "\"") Rec); + fwritecsv_data (F:File, FieldDelim:String) Rec:Tuple + = fwrites F (tuple_to_csvstr (true, FieldDelim, "\"") Rec); fwritecsv_data (F:File, FieldDelim:String, StrDelim:String) Rec:Tuple = fwrites F (tuple_to_csvstr (true, FieldDelim, StrDelim) Rec); *************** *** 86,119 **** fwritecsv_string F:File Rec:Tuple = fwrites F (tuple_to_csvstr (false, ",", "\"") Rec); fwritecsv_string (F:File, FieldDelim:String, StrDelim:String) Rec:Tuple = fwrites F (tuple_to_csvstr (false, FieldDelim, StrDelim) Rec); ! public freadcsvfile_data ARGS, freadcsvfile_string ARGS; ! /* Read an entire file of records into a list of tuples. */ ! ! freadcsvfile_data F:File ! = [] if feof F; ! = [freadcsv_data F | freadcsvfile_data F]; ! freadcsvfile_data (F:File, FieldDelim:String, StrDelim:String) ! = [] if feof F; ! = [freadcsv_data (F, FieldDelim, StrDelim) | freadcsvfile_data F]; ! ! freadcsvfile_string F:File ! = [] if feof F; ! = [freadcsv_string F | freadcsvfile_data F]; ! freadcsvfile_string (F:File, FieldDelim:String, StrDelim:String) ! = [] if feof F; ! = [freadcsv_string (F, FieldDelim, StrDelim) | freadcsvfile_string F]; ! ! public fwritecsvfile_data FILE LIST, fwritecsvfile_string ARGS; /* Write a list of tuples to a CSV formatted file. */ ! fwritecsvfile_data F:File L:List = do (fwritecsv_data F) L; ! fwritecsvfile_data (F:File, FieldDelim:String, StrDelim:String) L:List = do (fwritecsv_data (F, FieldDelim, StrDelim)) L; ! fwritecsvfile_string F:File L:List = do (fwritecsv_string F) L; ! fwritecsvfile_string (F:File, FieldDelim:String, StrDelim:String) L:List = do (fwritecsv_string (F, FieldDelim, StrDelim)) L; \ No newline at end of file --- 184,206 ---- fwritecsv_string F:File Rec:Tuple = fwrites F (tuple_to_csvstr (false, ",", "\"") Rec); + fwritecsv_string (F:File, FieldDelim:String) Rec:Tuple + = fwrites F (tuple_to_csvstr (false, FieldDelim, "\"") Rec); fwritecsv_string (F:File, FieldDelim:String, StrDelim:String) Rec:Tuple = fwrites F (tuple_to_csvstr (false, FieldDelim, StrDelim) Rec); ! public fwritecsvlist_data FILE LIST, fwritecsvlist_string ARGS; /* Write a list of tuples to a CSV formatted file. */ ! fwritecsvlist_data F:File L:List = do (fwritecsv_data F) L; ! fwritecsvlist_data (F:File, FieldDelim:String) L:List ! = do (fwritecsv_data (F, FieldDelim)) L; ! fwritecsvlist_data (F:File, FieldDelim:String, StrDelim:String) L:List = do (fwritecsv_data (F, FieldDelim, StrDelim)) L; ! fwritecsvlist_string F:File L:List = do (fwritecsv_string F) L; ! fwritecsvlist_string (F:File, FieldDelim:String) L:List ! = do (fwritecsv_string (F, FieldDelim)) L; ! fwritecsvlist_string (F:File, FieldDelim:String, StrDelim:String) L:List = do (fwritecsv_string (F, FieldDelim, StrDelim)) L; \ No newline at end of file |