// stringstream.b import "io" import "keyboard" import "string" import "heap" export { sst_start, sst_readch, sst_unreadch, sst_readint, sst_readstring, sst_readline } /* imported from string: new_string() string_length(s) string_addchar(s, c) string_getchar(s, i) string_setchar(s, i, c) string_print(s) string_frombcplstring(s) string_delete(s) */ let putback = 0; let thestring = nil; // globals not good here, just so we can concentrate on one idea let length = 0; let position = 0; let sst_start(s) be { thestring := s; length := string_length(s); position := 0; putback := 0 } let sst_readch() be { let answer; if putback <> 0 then { answer := putback; putback := 0; resultis answer } if position >= length then resultis '\n'; answer := string_getchar(thestring, position); position +:= 1; resultis answer } let sst_unreadch(c) be { putback := c } let sst_readint() be { let n = 0, neg = false, c = sst_readch(); while c <= ' ' do c := sst_readch(); test c = '-' then { neg := true; c := sst_readch() } else if c = '+' then c := sst_readch(); while c >= '0' /\ c <= '9' do { n := n * 10 + c - '0'; c := sst_readch() } sst_unreadch(); if neg then n := - n; resultis n } let sst_readstring() be { let s = new_string(), c = sst_readch(); while c <= ' ' /\ c <> '\n' do c := sst_readch(); while c > ' ' do { string_addchar(s, c); c := sst_readch() } resultis s } let sst_readline() be { let s = new_string(), c = sst_readch(); while c <> '\n' do { string_addchar(s, c); c := sst_readch() } resultis s } let start() be { let line, a, b, c, d, e; init(); out("type a single character, two ints, then two strings\n> "); line := kbd_readline(); sst_start(line); a := sst_readch(); b := sst_readint(); c := sst_readint(); d := sst_readstring(); e := sst_readstring(); out("a: '%c'\n", a); out("b: %d\n", b); out("c: %d\n", c); out("d: \""); string_print(d); out("\"\n"); out("e: \""); string_print(e); out("\"\n") }