Position Independent Source Code

Artifact Content
Login

Artifact 6be3bb68af7e378463fb2d1116a647a71582dbc7:


     1  /* Parsing CSV data in PISC */
     2  "Dicts.pisc" import
     3  
     4  : parse-csv-core ( reader -- vec [ vec ] ) 
     5  	:reader
     6  	<vector> :results
     7  	f :inString
     8  	[ $inString not ] :outOfString
     9  
    10  	/* Set up the temporary variables */
    11  	[ "" :tempStr ] :resetCell
    12  	[ resetCell <vector> :tempVec ] dup call :resetLine
    13  
    14  	[ $tempVec $tempStr vec-append :tempVec resetCell ] :addCell
    15  	[ $results $tempVec vec-append :results resetLine ] :addLine
    16  
    17  	/* Debugging 
    18  	"Temp Cell: " $tempStr concat print
    19  	"Temp Vector: " $tempVec >string concat print
    20  	*/
    21  
    22  	[ $reader -$EOF call not nip ] :notEOF
    23  	[ $reader -$read-rune call nip ] :readRune
    24  
    25  	[ notEOF ] [ 
    26  		readRune
    27  		/* By default, append a char */
    28  		t :default
    29  		[ f :default ] :break
    30  
    31  		dup "," str-eq outOfString and [ addCell break ] when
    32  		dup "\n" str-eq outOfString and [ addCell addLine break ] when
    33  		dup "\"" str-eq /* Toggle */ [ outOfString :inString break ] when
    34  		$default [ $tempStr swap concat :tempStr ] [ drop ] if
    35  	] while 
    36  	/* Add the straggling items */
    37  	addCell addLine 
    38  	$results ; 
    39  
    40  : csv-of-file ( filepath -- vec [ vec ] ) open-file-reader parse-csv-core ;
    41  : csv-of-string ( string -- vec [ vec ] ) str>rune-reader parse-csv-core ;