cleaned all trailing white space from source files.
[sdk] / samples / eC / ecas / misc.ec
1 import "ecere"
2 public:
3
4 //TODO:  Make this use a tree instead of a linear lookup
5 class Dictionary {
6    uint lookup(const char *str, bool create) {
7       uint ret = 0;
8       for (i:array) {
9          if (!strcmp(i, str))
10             return ret;
11          ret++;
12       }
13       if (!create)
14          return (uint)-1;
15       array.Add(StrDup(str));
16       return ret;
17    }
18    const char *name(uint index) {
19       if (index >= array.size)
20          return null;
21       return array[index];
22    }
23    ~Dictionary() {
24       for (i:array)
25          delete i;
26    }
27 private:
28    Array<char*> array {};
29 };
30
31 //CharFlags is a collection of characters that are to be returned (or not returned) by the find* functions
32 class CharFlags {
33    property char *charsSet {
34       set {
35          const byte *n = (const byte*)value;
36          byte val = 0;
37          while (*n)
38             flags[(uint)*n++] = ++val;
39       }
40    }
41    /*property char * {
42       set { charsSet = value; }
43    }*/
44
45    byte flags[256]; //flags[c] indicates whether character c is selected
46    void Clear(void) {
47       memset(flags, 0, sizeof(flags));
48    }
49    void Set(const char *needles, int val) {
50       const byte *n = (const byte*)needles;
51       byte v = (byte)val;
52       while (*n)
53          flags[(uint)*n++] = v;
54    }
55 };
56
57 char *findfirst(const char *haystack, uint haystacklen, CharFlags cf) {
58    const byte *h = (const byte*)haystack;
59    while (haystacklen--) {
60       if (cf.flags[(uint)*h++])
61          return h-1;
62    }
63    return null;
64 }
65 char *findfirstnon(const char *haystack, uint haystacklen, CharFlags cf) {
66    const byte *h = (const byte*)haystack;
67    while (haystacklen--) {
68       if (!cf.flags[(uint)*h++])
69          return h-1;
70    }
71    return null;
72 }
73 char *findlast(const char *haystack, uint haystacklen, CharFlags cf) {
74    const byte *h = (const byte*)haystack + haystacklen;
75    while (haystacklen--) {
76       if (cf.flags[(uint)*--h])
77          return h;
78    }
79    return null;
80 }
81 char *findlastnon(const char *haystack, uint haystacklen, CharFlags cf) {
82    const byte *h = (const byte*)haystack + haystacklen;
83    while (haystacklen--) {
84       if (!cf.flags[(uint)*--h])
85          return h;
86    }
87    return null;
88 }
89
90 char *StrDup(const char *src) {
91    char *ret;
92    uint len;
93    if (!src)
94       src = "";
95    len = strlen(src)+1;
96    ret = new char[len];
97    memcpy(ret, src, len);
98    return ret;
99 }
100
101 define NB_ALLOWHIGHERDIGITS = 256;
102 define NB_ALLOWCAPLETTERS = 512;
103 define NB_ALLOWLCASELETTERS = 1024;
104 define NB_ALLOWLETTERS = (NB_ALLOWCAPLETTERS | NB_ALLOWLCASELETTERS);
105
106 define NB_Dec      = ((uint16)(10));
107 define NB_Hex      = ((uint16)(16 | NB_ALLOWLETTERS));
108 define NB_Bin      = ((uint16)(2));
109 define NB_Oct      = ((uint16)(8));
110 define NB_OctLoose = ((uint16)(8 | NB_ALLOWHIGHERDIGITS));
111
112 enum ReadULLError {noerror=0,empty=1,overflow=2};
113
114 //input must only contain valid digits for the given base
115 unsigned long long ReadULL_Valid(const char *ptr,const char *eptr, uint16 base, ReadULLError *errorout)
116 {
117         unsigned long long ret=0;
118         unsigned long long multiplier=1;
119         unsigned long long temp;
120         ReadULLError rerror = noerror;
121         if (ptr>=eptr)
122         {
123                 rerror = empty;
124                 goto rn_end;
125         }
126         //the range ptr-->eptr is all valid digits, and eptr is iterating backwards through the range.
127         while (eptr-->ptr)
128         {
129                 char c=*eptr;
130                 //this series of if statements takes advantage of the fact that 'a'>'A'>'0'
131                 if (c>='a')
132                         c-='a'-10;
133                 else if (c>='A')
134                         c-='A'-10;
135                 else
136                         c-='0';
137                 temp=c;
138                 temp*=multiplier;
139                 if ((c&&!temp) || ret > ~temp) //if overflow occurred
140                         rerror = overflow;
141                 ret+=temp;
142                 multiplier *= (unsigned long long)base & 0xFF;
143         }
144 rn_end:
145         if (errorout)
146       *errorout = rerror;
147         return ret;
148 }