aboutsummaryrefslogtreecommitdiff
path: root/BUG3/text_tools.udo
blob: 63cc2a8f5f541294f6ac813e4f724772ae251368 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#ifndef UDO_TEXTTOOLS
#define UDO_TEXTTOOLS ##

opcode notify, 0, S
	Stext xin
	Snew = "\n"
	iwidth = 60
	index = 0
	while (index < iwidth) do
		Snew = strcat(Snew, "*");
		index += 1
	od
	prints strcat(strcat(Snew, "\n"), strcat(Stext, "\n\n"))
endop


#ifndef BUG_VERSION
#define BUG_VERSION #-1#
	notify("No BUG_VERSION specified: must match directive file")
	exitnow
#end

opcode gettextindex, i, S
	Sname xin
	ifinalindex = -1
	index = 0
	while (index < lenarray(gSequenced)) do
		if (strcmp(gSequenced[index], Sname) == 0) then
			ifinalindex = index
			igoto complete
		endif
		index += 1
	od

complete:
	xout ifinalindex
endop


opcode stripnewline, S, S
	Sline xin
	index = strindex(Sline, "\n")
	if (index != -1) then
		Sline = strsub(Sline, 0, index)
	endif
	xout Sline
endop

opcode parserowitems, i, Siii
	Sline, ix1, ix2, imode xin ; mode 0 = count, 1 = write sections, 2 = write chords
	inum = 0
	while (strlen(Sline) > 0) do
		icomma = strindex(Sline, ",")
		Stemp = strsub(Sline, 0, icomma)
		icomma = (icomma == -1) ? 0 : icomma
		Sline = strsub(Sline, icomma + 1)
		if (imode == 1) then
			tabw_i strtod(Stemp), inum, gisectiondata[ix1][ix2]
		elseif (imode == 2) then
			tabw_i strtod(Stemp), inum, gichordgroups[ix1][ix2]
		endif
		inum += 1
	od
	xout inum
endop




opcode getparameter, i, iii
	ipos, idataindex, iparameter xin
	xout tab_i(ipos, gisectiondata[idataindex][iparameter])
endop

/*
opcode getparameter, i, iiii
	isection, instrindex, iparameter, ipos xin
	isectiondataindex = gisections[isection][instrindex]
	ifn = gisectiondata[isectiondataindex][iparameter]
	xout tab_i(ipos, ifn)
endop
*/
opcode getparameterfn, i, iii
	isection, instrindex, iparameter xin
	isectiondataindex = gisections[isection][instrindex]
	xout gisectiondata[isectiondataindex][iparameter]
endop

opcode getparameterfns, i[], ii
	isection, instrindex xin
	isectiondataindex = gisections[isection][instrindex]
	xout getrow(gisectiondata, isectiondataindex)
endop

opcode parserow, 0, Siii
	Sline, ix1, ix2, imode xin ; mode 1 = section patterns , 2 = chords
	isize = parserowitems(Sline, ix1, ix2, 0)
	if (imode == 1) then
		gisectiondata[ix1][ix2] ftgen 0, 0, -isize, -7, 0
	elseif (imode == 2) then
		gichordgroups[ix1][ix2] ftgen 0, 0, -isize, -7, 0
	endif
	isize = parserowitems(Sline, ix1, ix2, imode)
endop



opcode parsetext, 0, S
	Sfile xin
	iparameter = -1
	isection = -1
	ichordnum = -1
	isectiondataindex = -1
	imode = -1
read:
	Sline, ilinenum readfi Sfile
	Sline = stripnewline(Sline)
	if (strlen(Sline) > 0) then
		Sfirstchar = strsub(Sline, 0, 1)
		if (strcmp(Sfirstchar, ";") == 0) then ; comment
			; no action
		elseif (strcmp(Sfirstchar, "v") == 0) then ; version
			imode = 0
			iversion = strtod(strsub(Sline, 2))
			if (iversion != $BUG_VERSION) then
				notify(sprintf("Incompatible directive file version: got %.2f , expected %.2f", iversion, $BUG_VERSION))
				exitnow
			endif
		elseif (strcmp(Sfirstchar, "s") == 0) then ; section
			imode = 1
			icomma = strindex(Sline, ",")
			isectionnum = strtod(strsub(Sline, 2, icomma))
			Sub = strsub(Sline, icomma+1)
			icomma = strindex(Sub, ",")
			isectionlength = strtod(strsub(Sub, 0, icomma))
			ichordgroup = strtod(strsub(Sub, icomma+1))
			gisectionchords[isectionnum] = ichordgroup
			tabw_i isectionlength, isectionnum, gisectionlengths

		elseif (strcmp(Sfirstchar, "b") == 0) then ; bpm
			imode = -1
			gitempo = strtod(strsub(Sline, 2))

		elseif (strcmp(Sfirstchar, "c") == 0) then ; chord group
			imode = 3
			ichordgroup = strtod(strsub(Sline, 2))
			ichordnum = 0

		elseif (strcmp(Sfirstchar, "i") == 0) then ; pattern
			imode = 4
			iparameter = 0
			
			icomma = strindex(Sline, ",")
			instrindex = gettextindex(strsub(Sline, 2, icomma))
			Sub = strsub(Sline, icomma+1)
			icomma2 = strindex(Sub, ",")
			isection = strtod(strsub(Sub, 0, icomma2))
			if (icomma2 != -1) then ; repeat or null section
				irepeatsection = strtod(strsub(Sub, icomma2+1))
				if (irepeatsection == -1) then
					inewsection = -1
				else
					inewsection = gisections[irepeatsection][instrindex]
				endif
				gisections[isection][instrindex] = inewsection
			else
				isectiondataindex += 1
				gisections[isection][instrindex] = isectiondataindex
			endif
		elseif (imode == 3) then ; chords
			parserow(Sline, ichordgroup, ichordnum, 2)
			ichordnum += 1

		elseif (imode == 4) then
			parserow(Sline, isectiondataindex, iparameter, 1)
			iparameter += 1
		endif
	endif
	if (ilinenum != -1) igoto read
endop



#end