3 * Common Library Functions
8 #define RANDOM_SEED 0xACE55052
9 #define RANDOM_A 0x12231ADE
10 #define RANDOM_C 0x1BADBEEF
11 // Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec
12 const short DAYS_BEFORE[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
13 #define UNIX_TO_2K ((30*365*3600*24) + (7*3600*24)) //Normal years + leap years
16 int ReadUTF8(Uint8 *str, Uint32 *Val);
19 static const char cUCDIGITS[] = "0123456789ABCDEF";
21 * \fn static void itoa(char *buf, Uint num, int base, int minLength, char pad)
22 * \brief Convert an integer into a character string
24 void itoa(char *buf, Uint num, int base, int minLength, char pad)
33 if(base > 16 || base < 2) {
40 tmpBuf[pos] = cUCDIGITS[ num % base ];
41 num /= (Uint)base; // Shift `num` right 1 digit
44 tmpBuf[pos++] = cUCDIGITS[ num % base ]; // Last digit of `num`
49 while(minLength-- > 0) buf[i++] = pad;
50 while(pos-- > 0) buf[i++] = tmpBuf[pos]; // Reverse the order of characters
55 * \fn int tolower(int __c)
56 * \brief Converts a character to lower case
60 if('A' <= c && c <= 'Z')
66 * \fn int strucmp(char *Str1, char *Str2)
67 * \brief Compare \a Str1 and \a Str2 case-insensitively
69 int strucmp(char *Str1, char *Str2)
71 while(*Str1 && tolower(*Str1) == tolower(*Str2))
73 return tolower(*Str1) - tolower(*Str2);
77 * \fn int strpos(char *Str, char Ch)
78 * \brief Search a string for an ascii character
80 int strpos(char *Str, char Ch)
83 for(pos=0;Str[pos];pos++)
85 if(Str[pos] == Ch) return pos;
92 int ByteSum(void *Ptr, int Size)
95 while(Size--) sum += *(Uint8*)Ptr++;
100 * \fn Uint strlen(char *__str)
101 * \brief Get the length of string
103 Uint strlen(char *__str)
106 while(*__str++) ret++;
111 * \fn char *strcpy(char *__str1, char *__str2)
112 * \brief Copy a string to a new location
114 char *strcpy(char *__str1, char *__str2)
117 *__str1++ = *__str2++;
118 *__str1 = '\0'; // Terminate String
123 * \fn int strcmp(char *str1, char *str2)
124 * \brief Compare two strings return the difference between
125 * the first non-matching characters.
127 int strcmp(char *str1, char *str2)
129 while(*str1 && *str1 == *str2)
131 return *str1 - *str2;
135 * \fn int strncmp(char *Str1, char *Str2, size_t num)
136 * \brief Compare strings \a Str1 and \a Str2 to a maximum of \a num characters
138 int strncmp(char *Str1, char *Str2, size_t num)
140 while(num-- && *Str1 && *Str1 == *Str2)
146 * \fn int strpos8(char *str, Uint32 search)
147 * \brief Search a string for a UTF-8 character
149 int strpos8(char *str, Uint32 Search)
153 for(pos=0;str[pos];pos++)
157 if(str[pos] == Search) return pos;
160 if(*(Uint8*)(str+pos) < 128) continue;
162 pos += ReadUTF8( (Uint8*)&str[pos], &val );
163 if(val == Search) return pos;
169 * \fn int ReadUTF8(Uint8 *str, Uint32 *Val)
170 * \brief Read a UTF-8 character from a string
172 int ReadUTF8(Uint8 *str, Uint32 *Val)
174 *Val = 0xFFFD; // Assume invalid character
177 if( !(*str & 0x80) ) {
182 // Middle of a sequence
183 if( (*str & 0xC0) == 0x80 ) {
188 if( (*str & 0xE0) == 0xC0 ) {
189 *Val = (*str & 0x1F) << 6; // Upper 6 Bits
191 if( (*str & 0xC0) != 0x80) return -1; // Validity check
192 *Val |= (*str & 0x3F); // Lower 6 Bits
197 if( (*str & 0xF0) == 0xE0 ) {
198 *Val = (*str & 0x0F) << 12; // Upper 4 Bits
200 if( (*str & 0xC0) != 0x80) return -1; // Validity check
201 *Val |= (*str & 0x3F) << 6; // Middle 6 Bits
203 if( (*str & 0xC0) != 0x80) return -1; // Validity check
204 *Val |= (*str & 0x3F); // Lower 6 Bits
209 if( (*str & 0xF1) == 0xF0 ) {
210 *Val = (*str & 0x07) << 18; // Upper 3 Bits
212 if( (*str & 0xC0) != 0x80) return -1; // Validity check
213 *Val |= (*str & 0x3F) << 12; // Middle-upper 6 Bits
215 if( (*str & 0xC0) != 0x80) return -1; // Validity check
216 *Val |= (*str & 0x3F) << 6; // Middle-lower 6 Bits
218 if( (*str & 0xC0) != 0x80) return -1; // Validity check
219 *Val |= (*str & 0x3F); // Lower 6 Bits
223 // UTF-8 Doesn't support more than four bytes
228 * \fn int WriteUTF8(Uint8 *str, Uint32 Val)
229 * \brief Write a UTF-8 character sequence to a string
231 int WriteUTF8(Uint8 *str, Uint32 Val)
241 *str = 0xC0 | (Val >> 6);
243 *str = 0x80 | (Val & 0x3F);
248 if( Val < 0x10000 ) {
249 *str = 0xE0 | (Val >> 12);
251 *str = 0x80 | ((Val >> 6) & 0x3F);
253 *str = 0x80 | (Val & 0x3F);
258 if( Val < 0x110000 ) {
259 *str = 0xF0 | (Val >> 18);
261 *str = 0x80 | ((Val >> 12) & 0x3F);
263 *str = 0x80 | ((Val >> 6) & 0x3F);
265 *str = 0x80 | (Val & 0x3F);
269 // UTF-8 Doesn't support more than four bytes
274 * \fn Uint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
275 * \brief Converts a date into an Acess Timestamp
277 Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
284 stamp += day*3600*24;
285 stamp += month*DAYS_BEFORE[month]*3600*24;
287 ((year&3) == 0 || year%100 != 0)
288 || (year%100 == 0 && ((year/100)&3) == 0)
289 ) && month > 1) // Leap year and after feb
292 stamp += ((365*4+1) * ((year-2000)&~3)) * 3600*24; // Foour Year Segments
293 stamp += ((year-2000)&3) * 365*3600*24; // Inside four year segment
301 * \brief Pseudo random number generator
302 * \note Unknown effectiveness (made up on the spot)
307 static Uint randomState = RANDOM_SEED;
308 Uint ret = randomState;
309 int roll = randomState & 31;
310 randomState = (randomState << roll) | (randomState >> (32-roll));
311 randomState ^= 0x9A3C5E78;
314 static Uint randomState = RANDOM_SEED;
315 return randomState = (RANDOM_A*randomState + RANDOM_C) & 0xFFFFFFFF;