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 char *strdup(char *str)
147 * \brief Duplicates a string
149 char *strdup(char *str)
152 ret = malloc(strlen(str)+1);
158 * \fn int strpos8(char *str, Uint32 search)
159 * \brief Search a string for a UTF-8 character
161 int strpos8(char *str, Uint32 Search)
165 for(pos=0;str[pos];pos++)
169 if(str[pos] == Search) return pos;
172 if(*(Uint8*)(str+pos) < 128) continue;
174 pos += ReadUTF8( (Uint8*)&str[pos], &val );
175 if(val == Search) return pos;
181 * \fn int ReadUTF8(Uint8 *str, Uint32 *Val)
182 * \brief Read a UTF-8 character from a string
184 int ReadUTF8(Uint8 *str, Uint32 *Val)
186 *Val = 0xFFFD; // Assume invalid character
189 if( !(*str & 0x80) ) {
194 // Middle of a sequence
195 if( (*str & 0xC0) == 0x80 ) {
200 if( (*str & 0xE0) == 0xC0 ) {
201 *Val = (*str & 0x1F) << 6; // Upper 6 Bits
203 if( (*str & 0xC0) != 0x80) return -1; // Validity check
204 *Val |= (*str & 0x3F); // Lower 6 Bits
209 if( (*str & 0xF0) == 0xE0 ) {
210 *Val = (*str & 0x0F) << 12; // Upper 4 Bits
212 if( (*str & 0xC0) != 0x80) return -1; // Validity check
213 *Val |= (*str & 0x3F) << 6; // Middle 6 Bits
215 if( (*str & 0xC0) != 0x80) return -1; // Validity check
216 *Val |= (*str & 0x3F); // Lower 6 Bits
221 if( (*str & 0xF1) == 0xF0 ) {
222 *Val = (*str & 0x07) << 18; // Upper 3 Bits
224 if( (*str & 0xC0) != 0x80) return -1; // Validity check
225 *Val |= (*str & 0x3F) << 12; // Middle-upper 6 Bits
227 if( (*str & 0xC0) != 0x80) return -1; // Validity check
228 *Val |= (*str & 0x3F) << 6; // Middle-lower 6 Bits
230 if( (*str & 0xC0) != 0x80) return -1; // Validity check
231 *Val |= (*str & 0x3F); // Lower 6 Bits
235 // UTF-8 Doesn't support more than four bytes
240 * \fn int WriteUTF8(Uint8 *str, Uint32 Val)
241 * \brief Write a UTF-8 character sequence to a string
243 int WriteUTF8(Uint8 *str, Uint32 Val)
253 *str = 0xC0 | (Val >> 6);
255 *str = 0x80 | (Val & 0x3F);
260 if( Val < 0x10000 ) {
261 *str = 0xE0 | (Val >> 12);
263 *str = 0x80 | ((Val >> 6) & 0x3F);
265 *str = 0x80 | (Val & 0x3F);
270 if( Val < 0x110000 ) {
271 *str = 0xF0 | (Val >> 18);
273 *str = 0x80 | ((Val >> 12) & 0x3F);
275 *str = 0x80 | ((Val >> 6) & 0x3F);
277 *str = 0x80 | (Val & 0x3F);
281 // UTF-8 Doesn't support more than four bytes
286 * \fn Uint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
287 * \brief Converts a date into an Acess Timestamp
289 Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
296 stamp += day*3600*24;
297 stamp += month*DAYS_BEFORE[month]*3600*24;
299 ((year&3) == 0 || year%100 != 0)
300 || (year%100 == 0 && ((year/100)&3) == 0)
301 ) && month > 1) // Leap year and after feb
304 stamp += ((365*4+1) * ((year-2000)&~3)) * 3600*24; // Foour Year Segments
305 stamp += ((year-2000)&3) * 365*3600*24; // Inside four year segment
313 * \brief Pseudo random number generator
314 * \note Unknown effectiveness (made up on the spot)
319 static Uint randomState = RANDOM_SEED;
320 Uint ret = randomState;
321 int roll = randomState & 31;
322 randomState = (randomState << roll) | (randomState >> (32-roll));
323 randomState ^= 0x9A3C5E78;
326 static Uint randomState = RANDOM_SEED;
327 return randomState = (RANDOM_A*randomState + RANDOM_C) & 0xFFFFFFFF;