aboutsummaryrefslogtreecommitdiff
path: root/site/udo/csv.udo
blob: 1829c0b88fa3cb022b44464791dc90ef3d064715 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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