<?xl version="0.1" encoding="utf-8"?>

<Define> ^loading-csv ()
<Arguments>
	^encoding
	^file
</Arguments>
<Sequence> ()
	<Define> ^data
		([Load option="text" encoding="^encoding"
				terminate="\r\n|\r|\n"] file )
	</Define>
	<Define> ^ret () </Define>
	<For dec="data">
		<Define> ^d (car data) </Define>
		<Define> ^r () </Define>
		<Define> ^RegMode plain </Define>
		<Define> ^str ^"" </Define>

		<RegSwitch> ^d

		<!-- plain mode -->

		<Case rex="," mode="plain">
			<set> ^r (cons str r) </set>
			<set> ^str ^"" </set>
		</Case>
		<Case rex="\"" mode="plain">
			<set> ^RegMode quote </set>
		</Case>
		<Case rex="." mode="plain">
			<set> ^str (+ str pattern) </set>
		</Case>

		<!-- quote mode -->

		<Case rex="\"\"" mode="quote">
			<set> ^str (+ str "\"") </set>
		</Case>
		<Case rex="," mode="quote">
			<set> ^str (+ str ",") </set>
		</Case>
		<Case rex="\"" mode="quote">
			<set> ^RegMode plain </set>
		</Case>
		<Case rex="." mode="quote">
			<set> ^str (+ str pattern) </set>
		</Case>
		</RegSwitch>
		<set> ^r (cons str r) </set>
		<set> ^ret (cons (Reverse r) ret) </set>
	</For>
	(Reverse ret)
</Sequence>
</Define>

<Define> ^csv-tune ()
<Arguments>
	^file
</Arguments>
<Sequence> ()
	<Define> ^rret () </Define>
	<For dec="file">
		<Define> ^ee (car file) </Define>
		<Define> ^end-flag 0 </Define>
		<Define> ^ret () </Define>
		<For dec="ee">
			<Define> ^e
				<RegSwitch> (car ee)
				<Case rex="[ &cx20012121;]+">
					^" "
				</Case>
				</RegSwitch>
			</Define>
			<set> ^e
				<RegSwitch> ^e
				<Case rex=". .">
					^pattern
				</Case>
				<Case rex=" .">
					(CutString pattern 1 1)
				</Case>
				<Case rex=". ">
					(CutString pattern 0 1)
				</Case>
				<Case rex=" ">
					^""
				</Case>
				</RegSwitch>
			</set>
			<Switch>
			<Case> (= e "")
				1
			</Case>
			<Case> (= (CutString e 0 1) "#")
				<set> ^end-flag 1 </set>
				<Break/>
			</Case>
			<Case> 1
				<set> ^ret (cons e ret) </set>
			</Case>
			</Switch>
			<If> (= end-flag 1) <Then> <Break/> </Then> </If>
		</For>
		<set> ^rret (cons (Reverse ret) rret) </set>
	</For>
	(Reverse rret)
</Sequence>
</Define>

<Define> ^csv-qualifier () </Define>
<Define> ^csv-env (NewEnvironment (CurrentEnvironment)) </Define>

<Define> ^insert-qualifier ()
<Arguments> ^q-name ^data </Arguments>
<Sequence> ()
	<Define> ^new-qualifier ^'(()) </Define>
	<set> ^data (cdr data) </set>
	<If> (= csv-qualifier ()) <Else>
	<set> ^csv-qualifier (cdr csv-qualifier) </set>
	</Else></If>
	<For dec="csv-qualifier">
		<Define> ^d (car data) </Define>
		<If> (= d "") <Then>
			<set> ^new-qualifier (cons (car csv-qualifier)
					new-qualifier)
			</set>
		</Then>
		<Else>
			<set> ^new-qualifier (cons
				(cons (List q-name d) (car csv-qualifier))
					new-qualifier)
			</set>
		</Else></If>
		<set> ^data (cdr data) </set>
	</For>
	<For dec="data">
		<Define> ^d (car data) </Define>
		<If> (= d "") <Then>
			<set> ^new-qualifier (cons ()
					new-qualifier)
			</set>
		</Then>
		<Else>
			<set> ^new-qualifier (cons
				(cons (List q-name d) ())
					new-qualifier)
			</set>
		</Else></If>
	</For>
	<set> ^csv-qualifier (Reverse new-qualifier) </set>
</Sequence>
</Define>


<Define> ^do-csv ()
<Arguments>
	^encoding
	^file-name
</Arguments>
<Sequence> ()
<csv-env type="raw">

	<Define> ^.# ()
	<Arguments> ^cmd ^lst </Arguments>
	0
	</Define>

	<Define> ^.character ()
	<Arguments> ^cmd ^lst </Arguments>
	<Sequence> ()
		<Define> ^ret ^'("") </Define>
		<Define> ^cmd (cdr cmd) </Define>
		<Define> ^lst (cdr lst) </Define>
		<For dec="lst">
			<Define> ^c (car cmd) </Define>
			<Switch>
			<Case> (= c "small")
				<set> ^ret (cons
				([Cast type="string" option="small"]
					(car lst))
					ret)
				</set>
			</Case>
			<Case> (= c "capital")
				<set> ^ret (cons
				([Cast type="string" option="capital"]
					(car lst))
					ret)
				</set>
			</Case>
			<Case> 1
				<set> ^ret (cons (car lst) ret) </set>
			</Case>
			</Switch>
			<set> ^cmd (cdr cmd) </set>
		</For>
		(Reverse ret)
	</Sequence>
	</Define>

	<Define> ^.pre.string ()
	<Arguments> ^cmd ^lst </Arguments>
	<Sequence> ()
		<Define> ^ret ^'("") </Define>
		<Define> ^cmd (cdr cmd) </Define>
		<Define> ^lst (cdr lst) </Define>
		<For dec="lst">
			<set> ^ret (cons (+ (car cmd) (car lst))
					ret) </set>
			<set> ^cmd (cdr cmd) </set>
		</For>
		(Reverse ret)
	</Sequence>
	</Define>

	<Define> ^.qualifier ()
	<Arguments> ^cmd ^lst </Arguments>
	<Sequence> ()
		(insert-qualifier 'qualifier cmd)
		2
	</Sequence>
	</Define>

	<Define> ^.qtype ()
	<Arguments> ^cmd ^lst </Arguments>
	<Sequence> ()
		(insert-qualifier 'qtype cmd)
		2
	</Sequence>
	</Define>

	<Define> ^.qdata ()
	<Arguments> ^cmd ^lst </Arguments>
	<Sequence> ()
		(insert-qualifier 'qdata cmd)
		2
	</Sequence>
	</Define>

</csv-env>

<Define> ^data (loading-csv encoding file-name) </Define>
<Define> ^ret-data () </Define>
<Define> ^cmd-data () </Define>

<For dec="data">
	<Define> ^e (car data) </Define>
	<Define> ^top (car e) </Define>
	<Switch>
	<Case> (= e '(""))
		()
	</Case>
	<Case> (= top "")
		<set> ^ret-data (cons e ret-data) </set>
	</Case>
	<Case> (= (CutString top 0 1) "#")
		()
	</Case>
	<Case> (= (CutString top 0 1) ".")
		<set> ^cmd-data (cons e cmd-data) </set>
	</Case>
	</Switch>
</For>

<set> ^data (Reverse ret-data) </set>
<set> ^cmd-data (Reverse cmd-data) </set>

<For dec="cmd-data">
	<Define> ^cmd (car cmd-data) </Define>
	<Define> ^sym ([Cast type="symbol"] (car cmd)) </Define>
(PrintStd sym)
(PrintStd cmd)
	<set> ^ret-data () </set>
	<Define> ^n-data ^data </Define>
	<If> (=
	<For dec="n-data">
		<Define> ^d
			(Eval csv-env (List sym 
				(List 'quote cmd)
				(List 'quote (car n-data))))
		</Define>
		<If> (= d 2) <Then>
			<Break> 2 </Break> </Then></If>
		<Switch>
		<Case> (= d 0)
			()
		</Case>
		<Case> (= d 1)
			<set> ^ret-data (cons (car n-data) ret-data) </set>
		</Case>
		<Case> 1
			<set> ^ret-data (cons d ret-data) </set>
		</Case>
		</Switch>
		0
	</For>
		0)
	<Then>
		<set> ^data (Reverse ret-data) </set>
	</Then>
	</If>
</For>


^data

</Sequence>
</Define>
