// file-s.txt  7 Nov 2005

// -------- API ----------------
// isBlank c, isNum c, notBlank c
// atoi s, scand f s, scans f s
// a = readint f, reads f s
// prArray s

CH = 0		// first not blank
token = array 80	// token buffer

// nl space
: isBlank c =
  or (c == 10) (c == 32)

// not nl space EOF
: notBlank c =
  and (c != 10) and (c != 32) (c >=0)

: isNum c =
  and (c >= 48) (c <= 57)

// skip blank, the first non blank in CH
to skipblank f | c =
  c = fgetc f
  while isBlank c
    c = fgetc f
  CH = c

// cannot handle "-" yet
to atoi s | i v c =
  v = 0
  i = 0
  c = s[i]
  while c != 0
    v = v*10 + c - 48
    i = i + 1
    c = s[i]
  v

// scan decimal, keep in array s
to scand f s | i c =
  i = 0
  c = CH
  while isNum c
     s[i] = c
     c = fgetc f
     i = i + 1
  s[i] = 0

// scan string, keep in array s
to scans f s | i c =
  i = 0
  c = CH
  while notBlank c
    s[i] = c
    c = fgetc f
    i = i + 1
  s[i] = 0

// print char in array s
to prArray s | i c =
  c = s[0]
  i = 0
  while c != 0
     printc c
     i = i + 1
     c = s[i]

// read one int from f, return int, EOF 
to readint f  =
  skipblank f
  if CH < 0
    CH
    break
  scand f token
//  prArray token
  atoi token

// read one string, separator is blank
// return s, or empty s if EOF
to reads f s =
  skipblank f
  if CH < 0
    s[0] = 0
    break
  scans f s

// example of use, read int from f until EOF
//to freadi f | a =
//  a = readint f
//  while a >= 0
//    print a space
//    a = readint f

// example of use, read string from f until EOF 
//to freads f =
//  reads f token
//  if token[0] != 0 
//    prArray token space
//    freads f

//  example of reading char until EOF
//to prfile f | c =
//  c = fgetc f
//  while c >= 0
//    printc c
//    c = fgetc f

//to main | f =
//  f = fopen "out.txt" 1
//  fprint f 1
//  fprint f 2
//  fprint f 3
//  f = fopen "f.txt" 0
//  printc fgetc f
//  printc fgetc f
//  prfile f
//  f = fopen "num.txt" 0
//  readf f
//  prfile f
//  f = fopen "bubble.txt" 0
//  freads f
//  fclose f

//main

