diff options
| author | Charles.Forsyth <devnull@localhost> | 2006-12-22 17:07:39 +0000 |
|---|---|---|
| committer | Charles.Forsyth <devnull@localhost> | 2006-12-22 17:07:39 +0000 |
| commit | 37da2899f40661e3e9631e497da8dc59b971cbd0 (patch) | |
| tree | cbc6d4680e347d906f5fa7fca73214418741df72 /appl/lib/convcs/cp_stob.b | |
| parent | 54bc8ff236ac10b3eaa928fd6bcfc0cdb2ba46ae (diff) | |
20060303a
Diffstat (limited to 'appl/lib/convcs/cp_stob.b')
| -rw-r--r-- | appl/lib/convcs/cp_stob.b | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/appl/lib/convcs/cp_stob.b b/appl/lib/convcs/cp_stob.b new file mode 100644 index 00000000..40c70792 --- /dev/null +++ b/appl/lib/convcs/cp_stob.b @@ -0,0 +1,49 @@ +implement Stob; + +include "sys.m"; +include "convcs.m"; + +sys : Sys; +map : array of byte; + +ERRCHAR : con 16rFFFD; + +init(arg : string) : string +{ + sys = load Sys Sys->PATH; + if (arg == nil) + return "codepage path required"; + fd := sys->open(arg, Sys->OREAD); + if (fd == nil) + return sys->sprint("%s: %r", arg); + + buf := array[Sys->UTFmax * 256] of byte; + nread := 0; + for (;nread < len buf;) { + toread := len buf - nread; + n := sys->read(fd, buf[nread:], toread); + if (n <= 0) + break; + nread += n; + } + codepage := string buf[0:nread]; + if (len codepage != 256) { + codepage = nil; + return sys->sprint("%s: bad codepage", arg); + } + buf = nil; + map = array[16r10000] of { * => byte '?' }; + for (i := 0; i < 256; i++) + map[codepage[i]] = byte i; + return nil; +} + +stob(nil : Convcs->State, str : string) : (Convcs->State, array of byte) +{ + b := array [len str] of byte; + n := len str; + + for (i := 0; i < n; i++) + b[i] = map[str[i]]; + return (nil, b); +}
\ No newline at end of file |
