Increase MAX_STATIC_DATA
[bazic.git] / utils.h
1 array hextable -> '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' 'A' 'B' 'C' 'D' 'E' 'F';
2
3 ! Fills a chunk of memory.
4
5 [ memset ptr i size;
6         while (size--)
7                 (ptr++)->0 = i;
8 ];
9
10 ! Copies a chunk of memory.
11
12 [ memcpy dest src len;
13         if (dest > src)
14         {
15                 ! Copy down.
16                 dest = dest + len;
17                 src = src + len;
18                 while (len--)
19                         (--dest)->0 = (--src)->0;
20         }
21         else
22         {
23                 ! Copy up.
24                 while (len--)
25                         (dest++)->0 = (src++)->0;
26         }
27 ];
28
29 ! Emits a string.
30
31 [ astring s;
32         while (s->0)
33                 print (char) (s++)->0;
34 ];
35
36 ! Duplicates a string.
37
38 [ strdup s  s1 i;
39         i = strlen(s);
40         s1 = mem_alloc(i+1);
41         strcpy(s1, s);
42         return s1;
43 ];
44
45 ! Compares two strings.
46
47 [ strcmp s1 s2;
48         while (s1->0 == s2->0)
49         {
50                 if (s1->0 == 0)
51                         return 0;
52                 s1++;
53                 s2++;
54         }
55         return 1;
56 ];
57
58 ! Counts the length of a string.
59
60 [ strlen s  i;
61         i = 0;
62         while ((s++)->0)
63                 i++;
64         return i;
65 ];
66
67 ! Copies a string from one place to another.
68
69 [ strcpy dest src;
70         do {
71                 (dest++)->0 = (src++)->0;
72         } until (src->0 == 0);
73         dest->0 = 0;
74 ];
75
76 ! Outputs a hex number.
77
78 [ phex i digits;
79         if (digits == 4)
80         {
81                 print (char) hextable->((i / 4096) & 15);
82                 print (char) hextable->((i / 256) & 15);
83         }
84         print (char) hextable->((i / 16) & 15);
85         print (char) hextable->(i & 15);
86 ];
87         
88 ! Dumps some memory in hex.
89
90 [ hexdump p size;
91         while (size)
92         {
93                 phex(p->0, 2);
94                 print " ";
95                 size--;
96                 p++;
97         }
98         print "^";
99 ];
100