3 * Common Library Functions
8 #define RANDOM_SEED 0xACE55052
9 #define RANDOM_A 0x00731ADE
10 #define RANDOM_C 12345
11 #define RANDOM_SPRUCE 0xf12b02b
12 // Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec
13 const short DAYS_BEFORE[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
14 #define UNIX_TO_2K ((30*365*3600*24) + (7*3600*24)) //Normal years + leap years
17 int ReadUTF8(Uint8 *str, Uint32 *Val);
20 static Uint giRandomState = RANDOM_SEED;
23 static const char cUCDIGITS[] = "0123456789ABCDEF";
25 * \fn static void itoa(char *buf, Uint num, int base, int minLength, char pad)
26 * \brief Convert an integer into a character string
28 void itoa(char *buf, Uint num, int base, int minLength, char pad)
37 if(base > 16 || base < 2) {
44 tmpBuf[pos] = cUCDIGITS[ num % base ];
45 num /= (Uint)base; // Shift `num` right 1 digit
48 tmpBuf[pos++] = cUCDIGITS[ num % base ]; // Last digit of `num`
53 while(minLength-- > 0) buf[i++] = pad;
54 while(pos-- > 0) buf[i++] = tmpBuf[pos]; // Reverse the order of characters
59 * \fn int tolower(int __c)
60 * \brief Converts a character to lower case
64 if('A' <= c && c <= 'Z')
70 * \fn int strucmp(char *Str1, char *Str2)
71 * \brief Compare \a Str1 and \a Str2 case-insensitively
73 int strucmp(char *Str1, char *Str2)
75 while(*Str1 && tolower(*Str1) == tolower(*Str2))
77 return tolower(*Str1) - tolower(*Str2);
81 * \fn int strpos(char *Str, char Ch)
82 * \brief Search a string for an ascii character
84 int strpos(char *Str, char Ch)
87 for(pos=0;Str[pos];pos++)
89 if(Str[pos] == Ch) return pos;
96 int ByteSum(void *Ptr, int Size)
99 while(Size--) sum += *(Uint8*)Ptr++;
104 * \fn Uint strlen(char *__str)
105 * \brief Get the length of string
107 Uint strlen(char *__str)
110 while(*__str++) ret++;
115 * \fn char *strcpy(char *__str1, char *__str2)
116 * \brief Copy a string to a new location
118 char *strcpy(char *__str1, char *__str2)
121 *__str1++ = *__str2++;
122 *__str1 = '\0'; // Terminate String
127 * \fn int strcmp(char *str1, char *str2)
128 * \brief Compare two strings return the difference between
129 * the first non-matching characters.
131 int strcmp(char *str1, char *str2)
133 while(*str1 && *str1 == *str2)
135 return *str1 - *str2;
139 * \fn int strncmp(char *Str1, char *Str2, size_t num)
140 * \brief Compare strings \a Str1 and \a Str2 to a maximum of \a num characters
142 int strncmp(char *Str1, char *Str2, size_t num)
144 if(num == 0) return 0; // TODO: Check what should officially happen here
145 while(--num && *Str1 && *Str1 == *Str2)
151 * \fn char *strdup(char *str)
152 * \brief Duplicates a string
154 char *strdup(char *str)
157 ret = malloc(strlen(str)+1);
163 * \fn int strpos8(char *str, Uint32 search)
164 * \brief Search a string for a UTF-8 character
166 int strpos8(char *str, Uint32 Search)
170 for(pos=0;str[pos];pos++)
174 if(str[pos] == Search) return pos;
177 if(*(Uint8*)(str+pos) < 128) continue;
179 pos += ReadUTF8( (Uint8*)&str[pos], &val );
180 if(val == Search) return pos;
186 * \fn int ReadUTF8(Uint8 *str, Uint32 *Val)
187 * \brief Read a UTF-8 character from a string
189 int ReadUTF8(Uint8 *str, Uint32 *Val)
191 *Val = 0xFFFD; // Assume invalid character
194 if( !(*str & 0x80) ) {
199 // Middle of a sequence
200 if( (*str & 0xC0) == 0x80 ) {
205 if( (*str & 0xE0) == 0xC0 ) {
206 *Val = (*str & 0x1F) << 6; // Upper 6 Bits
208 if( (*str & 0xC0) != 0x80) return -1; // Validity check
209 *Val |= (*str & 0x3F); // Lower 6 Bits
214 if( (*str & 0xF0) == 0xE0 ) {
215 *Val = (*str & 0x0F) << 12; // Upper 4 Bits
217 if( (*str & 0xC0) != 0x80) return -1; // Validity check
218 *Val |= (*str & 0x3F) << 6; // Middle 6 Bits
220 if( (*str & 0xC0) != 0x80) return -1; // Validity check
221 *Val |= (*str & 0x3F); // Lower 6 Bits
226 if( (*str & 0xF1) == 0xF0 ) {
227 *Val = (*str & 0x07) << 18; // Upper 3 Bits
229 if( (*str & 0xC0) != 0x80) return -1; // Validity check
230 *Val |= (*str & 0x3F) << 12; // Middle-upper 6 Bits
232 if( (*str & 0xC0) != 0x80) return -1; // Validity check
233 *Val |= (*str & 0x3F) << 6; // Middle-lower 6 Bits
235 if( (*str & 0xC0) != 0x80) return -1; // Validity check
236 *Val |= (*str & 0x3F); // Lower 6 Bits
240 // UTF-8 Doesn't support more than four bytes
245 * \fn int WriteUTF8(Uint8 *str, Uint32 Val)
246 * \brief Write a UTF-8 character sequence to a string
248 int WriteUTF8(Uint8 *str, Uint32 Val)
258 *str = 0xC0 | (Val >> 6);
260 *str = 0x80 | (Val & 0x3F);
265 if( Val < 0x10000 ) {
266 *str = 0xE0 | (Val >> 12);
268 *str = 0x80 | ((Val >> 6) & 0x3F);
270 *str = 0x80 | (Val & 0x3F);
275 if( Val < 0x110000 ) {
276 *str = 0xF0 | (Val >> 18);
278 *str = 0x80 | ((Val >> 12) & 0x3F);
280 *str = 0x80 | ((Val >> 6) & 0x3F);
282 *str = 0x80 | (Val & 0x3F);
286 // UTF-8 Doesn't support more than four bytes
291 * \fn Uint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
292 * \brief Converts a date into an Acess Timestamp
294 Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
301 stamp += day*3600*24;
302 stamp += month*DAYS_BEFORE[month]*3600*24;
304 ((year&3) == 0 || year%100 != 0)
305 || (year%100 == 0 && ((year/100)&3) == 0)
306 ) && month > 1) // Leap year and after feb
309 stamp += ((365*4+1) * ((year-2000)&~3)) * 3600*24; // Foour Year Segments
310 stamp += ((year-2000)&3) * 365*3600*24; // Inside four year segment
318 * \brief Pseudo random number generator
319 * \note Unknown effectiveness (made up on the spot)
323 Uint old = giRandomState;
324 // Get the next state value
325 giRandomState = (RANDOM_A*giRandomState + RANDOM_C) & 0xFFFFFFFF;
326 // Check if it has changed, and if it hasn't, change it
327 if(giRandomState == old) giRandomState += RANDOM_SPRUCE;
328 return giRandomState;