| 1 | ;-------------------------------------------------------------------------- |
|---|
| 2 | ; A couple of utility functions for accessing ASCII data tables |
|---|
| 3 | ; |
|---|
| 4 | ; Mark Koennecke, December 2001 |
|---|
| 5 | ;-------------------------------------------------------------------------- |
|---|
| 6 | |
|---|
| 7 | ; The separator to use for splitting lines into columns |
|---|
| 8 | (define separator #\tab) |
|---|
| 9 | |
|---|
| 10 | ;------------------------------------------------------------------------- |
|---|
| 11 | ; the current row we are working at |
|---|
| 12 | (define currentRow '()) |
|---|
| 13 | |
|---|
| 14 | ;------------------------------------------------------------------------ |
|---|
| 15 | ; functifyFields creates functions for each field which allows to |
|---|
| 16 | ; retrieve the field value by a (fieldName) syntax. This is useful for |
|---|
| 17 | ; operator doing computations. The row to be accessed must be put into |
|---|
| 18 | ; currentRow however before this can work. |
|---|
| 19 | (define functifyFields (lambda (header) |
|---|
| 20 | (letrec ( (count 0) |
|---|
| 21 | (funcOne (lambda (fieldList count) |
|---|
| 22 | (if (eq? fieldList '()) |
|---|
| 23 | '() |
|---|
| 24 | (begin |
|---|
| 25 | (eval (read (open-input-string (string-append |
|---|
| 26 | "(define " (car fieldList) |
|---|
| 27 | " (lambda () (list-ref currentRow " |
|---|
| 28 | (number->string count) " )))" )))) |
|---|
| 29 | (funcOne (cdr fieldList) (+ count 1)) ) ) )) ) |
|---|
| 30 | (funcOne (vector-ref header 1) count) ) )) |
|---|
| 31 | ;------------------------------------------------------------------------- |
|---|
| 32 | ; readheader reads the header information of a database and stores it into |
|---|
| 33 | ; a vector. This vector will hold three things: A list of comment |
|---|
| 34 | ; lines (recognizable by a # int the first position of the line), a list |
|---|
| 35 | ; of field names and the dashline unchanged. |
|---|
| 36 | ;------------------------------------------------------------------------- |
|---|
| 37 | (define readheader (lambda (port) |
|---|
| 38 | (letrec ( (fline "") |
|---|
| 39 | (getcomment (lambda (port) |
|---|
| 40 | (let ((l (getline port))) |
|---|
| 41 | (if (char=? (list-ref l 0) #\#) |
|---|
| 42 | (cons (list->string l) (getcomment port)) |
|---|
| 43 | (begin |
|---|
| 44 | (set! fline (list->string l)) |
|---|
| 45 | '() ) ) ) ) ) |
|---|
| 46 | (comments (getcomment port)) |
|---|
| 47 | (names (string-split fline (list separator) )) |
|---|
| 48 | (dash (read-line port)) |
|---|
| 49 | ) |
|---|
| 50 | (vector comments names dash) ) ) ) |
|---|
| 51 | ;------------------------------------------------------------------------- |
|---|
| 52 | ; putdbline writes a database row to the given output port. The row is |
|---|
| 53 | ; specified as a list of field values. |
|---|
| 54 | ;------------------------------------------------------------------------- |
|---|
| 55 | (define putdbline (lambda (row port) |
|---|
| 56 | (letrec ( (putfield (lambda (row port) |
|---|
| 57 | (display (list-ref row 0) port) |
|---|
| 58 | (cond |
|---|
| 59 | ( (eq? (cdr row) '()) |
|---|
| 60 | (newline port) |
|---|
| 61 | ) |
|---|
| 62 | (else |
|---|
| 63 | (display separator port) |
|---|
| 64 | (putfield (cdr row) port) ) ) ) ) ) |
|---|
| 65 | (if (not (eq? (cadr row) '())) |
|---|
| 66 | (putfield row port)) ) ) ) |
|---|
| 67 | ;------------------------------------------------------------------------- |
|---|
| 68 | ; writeheader writes a database header to a output port |
|---|
| 69 | ;------------------------------------------------------------------------- |
|---|
| 70 | (define writeheader (lambda (h port) |
|---|
| 71 | (let ( (putcomment (lambda (com port) |
|---|
| 72 | (if (not (eq? com '())) |
|---|
| 73 | (begin |
|---|
| 74 | (write (car com) port) |
|---|
| 75 | (newline port) |
|---|
| 76 | (putcomment (cdr com) port) ) ) ) ) ) |
|---|
| 77 | (putcomment (vector-ref h 0) port) |
|---|
| 78 | (putdbline (vector-ref h 1) port) |
|---|
| 79 | (display (vector-ref h 2) port) |
|---|
| 80 | (newline port) ) )) |
|---|
| 81 | ;------------------------------------------------------------------------- |
|---|
| 82 | ;locatefield determines the index of a field in a list of row values |
|---|
| 83 | ;------------------------------------------------------------------------- |
|---|
| 84 | (define locatefield (lambda (header name) |
|---|
| 85 | (letrec ( (count -1) |
|---|
| 86 | (findindex (lambda (list name) |
|---|
| 87 | (cond |
|---|
| 88 | ( (eq? list '()) |
|---|
| 89 | -1 ) |
|---|
| 90 | ( (string=? (car list) name) |
|---|
| 91 | (+ count 1) ) |
|---|
| 92 | (else |
|---|
| 93 | (set! count (+ count 1)) |
|---|
| 94 | (findindex (cdr list) name) ) ) ) ) ) |
|---|
| 95 | (findindex (vector-ref header 1) name) ) )) |
|---|
| 96 | ;------------------------------------------------------------------------- |
|---|
| 97 | ; getname is a convenience function for returning the i'th column name |
|---|
| 98 | ;------------------------------------------------------------------------ |
|---|
| 99 | (define getname (lambda (header i) |
|---|
| 100 | (list-ref (vector-ref header 1) i))) |
|---|
| 101 | ;-------------------------------------------------------------------------- |
|---|
| 102 | ; getline reads a line of text and returns the line as a list of characters |
|---|
| 103 | ;-------------------------------------------------------------------------- |
|---|
| 104 | (define getline (lambda (port) |
|---|
| 105 | (let ((c (read-char port))) |
|---|
| 106 | (cond |
|---|
| 107 | ( (eof-object? c) c ) |
|---|
| 108 | ( (char=? c #\newline) '() ) |
|---|
| 109 | ; ( (char=? c #\cr) (cons #\space (getline port)) ) |
|---|
| 110 | (else (cons c (getline port)) ) ) ) ) ) |
|---|
| 111 | ;------------------------------------------------------------------------- |
|---|
| 112 | ; read-line reads a line as a string |
|---|
| 113 | ;------------------------------------------------------------------------- |
|---|
| 114 | (define read-line (lambda (port) (list->string (getline port)))) |
|---|
| 115 | ;============================================================================ |
|---|
| 116 | ; A string splitting procedure from the net. Originally written by oleg |
|---|
| 117 | |
|---|
| 118 | ; -- procedure: string-null? STRING |
|---|
| 119 | ; returns false if the string is the empt string, true else |
|---|
| 120 | (define string-null? (lambda (txt) |
|---|
| 121 | (if (> 0 (string-length txt)) |
|---|
| 122 | #t |
|---|
| 123 | #f ) ) ) |
|---|
| 124 | ; |
|---|
| 125 | (define ++ (lambda (x) (+ x 1))) |
|---|
| 126 | (define -- (lambda (x) (- x 1))) |
|---|
| 127 | |
|---|
| 128 | ; -- procedure+: string-split STRING CHARSET |
|---|
| 129 | ; -- procedure+: string-split STRING CHARSET MAXSPLIT |
|---|
| 130 | ; |
|---|
| 131 | ; Returns a list of words delimited by the characters in CHARSET in |
|---|
| 132 | ; STRING. CHARSET is a list of characters that are treated as delimiters. |
|---|
| 133 | ; Leading or trailing delimeters are NOT trimmed. That is, the resulting |
|---|
| 134 | ; list will have as many initial empty string elements as there are |
|---|
| 135 | ; leading delimiters in STRING. |
|---|
| 136 | ; |
|---|
| 137 | ; If MAXSPLIT is specified and positive, the resulting list will |
|---|
| 138 | ; contain at most MAXSPLIT elements, the last of which is the string |
|---|
| 139 | ; remaining after (MAXSPLIT - 1) splits. If MAXSPLIT is specified and |
|---|
| 140 | ; non-positive, the empty list is returned. "In time critical |
|---|
| 141 | ; applications it behooves you not to split into more fields than you |
|---|
| 142 | ; really need." |
|---|
| 143 | ; |
|---|
| 144 | ; This is based on the split function in Python/Perl |
|---|
| 145 | ; |
|---|
| 146 | ; (string-split " abc d e f ") ==> ("abc" "d" "e" "f") |
|---|
| 147 | ; (string-split " abc d e f " '() 1) ==> ("abc d e f ") |
|---|
| 148 | ; (string-split " abc d e f " '() 0) ==> () |
|---|
| 149 | ; (string-split ":abc:d:e::f:" '(#\:)) ==> ("" "abc" "d" "e" "" "f" "") |
|---|
| 150 | ; (string-split ":" '(#\:)) ==> ("" "") |
|---|
| 151 | ; (string-split "root:x:0:0:Lord" '(#\:) 2) ==> ("root" "x:0:0:Lord") |
|---|
| 152 | ; (string-split "/usr/local/bin:/usr/bin:/usr/ucb/bin" '(#\:)) |
|---|
| 153 | ; ==> ("/usr/local/bin" "/usr/bin" "/usr/ucb/bin") |
|---|
| 154 | ; (string-split "/usr/local/bin" '(#\/)) ==> ("" "usr" "local" "bin") |
|---|
| 155 | |
|---|
| 156 | (define (string-split str . rest) |
|---|
| 157 | ; maxsplit is a positive number |
|---|
| 158 | (define (split-by-whitespace str maxsplit) |
|---|
| 159 | (define (skip-ws i yet-to-split-count) |
|---|
| 160 | (cond |
|---|
| 161 | ((>= i (string-length str)) '()) |
|---|
| 162 | ((char-whitespace? (string-ref str i)) |
|---|
| 163 | (skip-ws (++ i) yet-to-split-count)) |
|---|
| 164 | (else (scan-beg-word (++ i) i yet-to-split-count)))) |
|---|
| 165 | (define (scan-beg-word i from yet-to-split-count) |
|---|
| 166 | (cond |
|---|
| 167 | ((zero? yet-to-split-count) |
|---|
| 168 | (cons (substring str from (string-length str)) '())) |
|---|
| 169 | (else (scan-word i from yet-to-split-count)))) |
|---|
| 170 | (define (scan-word i from yet-to-split-count) |
|---|
| 171 | (cond |
|---|
| 172 | ((>= i (string-length str)) |
|---|
| 173 | (cons (substring str from i) '())) |
|---|
| 174 | ((char-whitespace? (string-ref str i)) |
|---|
| 175 | (cons (substring str from i) |
|---|
| 176 | (skip-ws (++ i) (-- yet-to-split-count)))) |
|---|
| 177 | (else (scan-word (++ i) from yet-to-split-count)))) |
|---|
| 178 | (skip-ws 0 (-- maxsplit))) |
|---|
| 179 | |
|---|
| 180 | ; maxsplit is a positive number |
|---|
| 181 | ; str is not empty |
|---|
| 182 | (define (split-by-charset str delimeters maxsplit) |
|---|
| 183 | (define (scan-beg-word from yet-to-split-count) |
|---|
| 184 | (cond |
|---|
| 185 | ((>= from (string-length str)) '("")) |
|---|
| 186 | ((zero? yet-to-split-count) |
|---|
| 187 | (cons (substring str from (string-length str)) '())) |
|---|
| 188 | (else (scan-word from from yet-to-split-count)))) |
|---|
| 189 | (define (scan-word i from yet-to-split-count) |
|---|
| 190 | (cond |
|---|
| 191 | ((>= i (string-length str)) |
|---|
| 192 | (cons (substring str from i) '())) |
|---|
| 193 | ((memq (string-ref str i) delimeters) |
|---|
| 194 | (cons (substring str from i) |
|---|
| 195 | (scan-beg-word (++ i) (-- yet-to-split-count)))) |
|---|
| 196 | (else (scan-word (++ i) from yet-to-split-count)))) |
|---|
| 197 | (scan-beg-word 0 (-- maxsplit))) |
|---|
| 198 | |
|---|
| 199 | ; resolver of overloading... |
|---|
| 200 | ; if omitted, maxsplit defaults to |
|---|
| 201 | ; (++ (string-length str)) |
|---|
| 202 | (if (string-null? str) '() |
|---|
| 203 | (if (null? rest) |
|---|
| 204 | (split-by-whitespace str (++ (string-length str))) |
|---|
| 205 | (let ((charset (car rest)) |
|---|
| 206 | (maxsplit |
|---|
| 207 | (if (pair? (cdr rest)) (cadr rest) (++ (string-length str))))) |
|---|
| 208 | (cond |
|---|
| 209 | ((not (positive? maxsplit)) '()) |
|---|
| 210 | ((null? charset) (split-by-whitespace str maxsplit)) |
|---|
| 211 | (else (split-by-charset str charset maxsplit)))))) |
|---|
| 212 | ) |
|---|
| 213 | |
|---|