From 9fbf91db06a6d4f4b5cd8bb45389a731bb86bf22 Mon Sep 17 00:00:00 2001 From: Richard Date: Sun, 13 Apr 2025 18:48:02 +0100 Subject: initial --- site/udo/csv.udo | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100755 site/udo/csv.udo (limited to 'site/udo/csv.udo') diff --git a/site/udo/csv.udo b/site/udo/csv.udo new file mode 100755 index 0000000..1829c0b --- /dev/null +++ b/site/udo/csv.udo @@ -0,0 +1,75 @@ +#ifndef UDO_CSV +#define UDO_CSV ## +/* + CSV parsing and writing + + This file is part of the SONICS UDO collection by Richard Knight 2021 + License: GPL-2.0-or-later + http://1bpm.net +*/ + +opcode csv_parseline, i[], Si + Sline, ilen xin + iout[] init ilen + index = 0 + irun = 1 + while (irun == 1) do + ipos strindex Sline, "," + if (ipos == -1) then + irun = 0 + iout[index] = strtod(Sline) + else + Stemp strsub Sline, 0, ipos + Sline strsub Sline, ipos+1 + iout[index] = strtod(Stemp) + index += 1 + endif + od + xout iout +endop + +opcode csv_formline, S, i[]j + iarray[], iformat xin + if (iformat == -1) then + Sformat = "%f" + else + Sformat = "%d" + endif + Sout = "" + ilen = lenarray(iarray) + index = 0 + while (index < ilen) do + Sout strcat Sout, sprintf(Sformat, iarray[index]) + if (index < ilen - 1) then + Sout strcat Sout, "," + endif + index += 1 + od + Sout strcat Sout, "\n" + xout Sout +endop + + +; form from table +opcode csv_formline, S, ij + ifn, iformat xin + if (iformat == -1) then + Sformat = "%f" + else + Sformat = "%d" + endif + Sout = "" + ilen = ftlen(ifn) + index = 0 + while (index < ilen) do + Sout strcat Sout, sprintf(Sformat, table:i(index, ifn)) + if (index < ilen - 1) then + Sout strcat Sout, "," + endif + index += 1 + od + Sout strcat Sout, "\n" + xout Sout +endop + +#end -- cgit v1.2.3