blob: 312add88357d3be23f17675aba32b3127c3d0f35 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
implement Stob;
include "sys.m";
sys: Sys;
include "convcs.m";
bigendian := 1;
header := 1;
init(arg : string) : string
{
sys = load Sys Sys->PATH;
case arg {
"le" =>
bigendian = 0;
header = 0;
"be" =>
header = 0;
}
return nil;
}
stob(state : Convcs->State, s : string) : (Convcs->State, array of byte)
{
if(state == nil){
if(header)
s = sys->sprint("%c", 16rfeff) + s;
state = "doneheader";
}
b := array[len s * 2] of byte;
j := 0;
if(bigendian){
for(i := 0; i < len s; i++){
c := s[i];
b[j++] = byte (c >> 8);
b[j++] = byte c;
}
}else{
for(i := 0; i < len s; i++){
c := s[i];
b[j++] = byte c;
b[j++] = byte (c >> 8);
}
}
return (state, b);
}
|