aboutsummaryrefslogtreecommitdiff
path: root/site/udo/csv.udo
diff options
context:
space:
mode:
Diffstat (limited to 'site/udo/csv.udo')
-rwxr-xr-xsite/udo/csv.udo75
1 files changed, 75 insertions, 0 deletions
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