3 * Common Library Functions
8 // Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec
9 const short DAYS_BEFORE[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
10 #define UNIX_TO_2K ((30*365*3600*24) + (7*3600*24)) //Normal years + leap years
13 int ReadUTF8(Uint8 *str, Uint32 *Val);
16 static const char cUCDIGITS[] = "0123456789ABCDEF";
18 * \fn static void itoa(char *buf, Uint num, int base, int minLength, char pad)
19 * \brief Convert an integer into a character string
21 void itoa(char *buf, Uint num, int base, int minLength, char pad)
30 if(base > 16 || base < 2) {
37 tmpBuf[pos] = cUCDIGITS[ num % base ];
38 num /= (Uint)base; // Shift `num` right 1 digit
41 tmpBuf[pos++] = cUCDIGITS[ num % base ]; // Last digit of `num`
46 while(minLength-- > 0) buf[i++] = pad;
47 while(pos-- > 0) buf[i++] = tmpBuf[pos]; // Reverse the order of characters
52 * \fn int tolower(int __c)
53 * \brief Converts a character to lower case
57 if('A' <= c && c <= 'Z')
63 * \fn int strucmp(char *Str1, char *Str2)
64 * \brief Compare \a Str1 and \a Str2 case-insensitively
66 int strucmp(char *Str1, char *Str2)
68 while(*Str1 && tolower(*Str1) == tolower(*Str2))
70 return tolower(*Str1) - tolower(*Str2);
74 * \fn int strpos(char *Str, char Ch)
75 * \brief Search a string for an ascii character
77 int strpos(char *Str, char Ch)
80 for(pos=0;Str[pos];pos++)
82 if(Str[pos] == Ch) return pos;
89 int ByteSum(void *Ptr, int Size)
92 while(Size--) sum += *(Uint8*)Ptr++;
97 * \fn Uint strlen(char *__str)
98 * \brief Get the length of string
100 Uint strlen(char *__str)
103 while(*__str++) ret++;
108 * \fn char *strcpy(char *__str1, char *__str2)
109 * \brief Copy a string to a new location
111 char *strcpy(char *__str1, char *__str2)
114 *__str1++ = *__str2++;
115 *__str1 = '\0'; // Terminate String
120 * \fn int strcmp(char *str1, char *str2)
121 * \brief Compare two strings return the difference between
122 * the first non-matching characters.
124 int strcmp(char *str1, char *str2)
126 while(*str1 && *str1 == *str2)
128 return *str1 - *str2;
132 * \fn int strncmp(char *Str1, char *Str2, size_t num)
133 * \brief Compare strings \a Str1 and \a Str2 to a maximum of \a num characters
135 int strncmp(char *Str1, char *Str2, size_t num)
137 while(num-- && *Str1 && *Str1 == *Str2)
143 * \fn int strpos8(char *str, Uint32 search)
144 * \brief Search a string for a UTF-8 character
146 int strpos8(char *str, Uint32 Search)
150 for(pos=0;str[pos];pos++)
154 if(str[pos] == Search) return pos;
157 if(*(Uint8*)(str+pos) < 128) continue;
159 pos += ReadUTF8( (Uint8*)&str[pos], &val );
160 if(val == Search) return pos;
166 * \fn int ReadUTF8(Uint8 *str, Uint32 *Val)
167 * \brief Read a UTF-8 character from a string
169 int ReadUTF8(Uint8 *str, Uint32 *Val)
171 *Val = 0xFFFD; // Assume invalid character
174 if( !(*str & 0x80) ) {
179 // Middle of a sequence
180 if( (*str & 0xC0) == 0x80 ) {
185 if( (*str & 0xE0) == 0xC0 ) {
186 *Val = (*str & 0x1F) << 6; // Upper 6 Bits
188 if( (*str & 0xC0) != 0x80) return -1; // Validity check
189 *Val |= (*str & 0x3F); // Lower 6 Bits
194 if( (*str & 0xF0) == 0xE0 ) {
195 *Val = (*str & 0x0F) << 12; // Upper 4 Bits
197 if( (*str & 0xC0) != 0x80) return -1; // Validity check
198 *Val |= (*str & 0x3F) << 6; // Middle 6 Bits
200 if( (*str & 0xC0) != 0x80) return -1; // Validity check
201 *Val |= (*str & 0x3F); // Lower 6 Bits
206 if( (*str & 0xF1) == 0xF0 ) {
207 *Val = (*str & 0x07) << 18; // Upper 3 Bits
209 if( (*str & 0xC0) != 0x80) return -1; // Validity check
210 *Val |= (*str & 0x3F) << 12; // Middle-upper 6 Bits
212 if( (*str & 0xC0) != 0x80) return -1; // Validity check
213 *Val |= (*str & 0x3F) << 6; // Middle-lower 6 Bits
215 if( (*str & 0xC0) != 0x80) return -1; // Validity check
216 *Val |= (*str & 0x3F); // Lower 6 Bits
220 // UTF-8 Doesn't support more than four bytes
225 * \fn int WriteUTF8(Uint8 *str, Uint32 Val)
226 * \brief Write a UTF-8 character sequence to a string
228 int WriteUTF8(Uint8 *str, Uint32 Val)
238 *str = 0xC0 | (Val >> 6);
240 *str = 0x80 | (Val & 0x3F);
245 if( Val < 0x10000 ) {
246 *str = 0xE0 | (Val >> 12);
248 *str = 0x80 | ((Val >> 6) & 0x3F);
250 *str = 0x80 | (Val & 0x3F);
255 if( Val < 0x110000 ) {
256 *str = 0xF0 | (Val >> 18);
258 *str = 0x80 | ((Val >> 12) & 0x3F);
260 *str = 0x80 | ((Val >> 6) & 0x3F);
262 *str = 0x80 | (Val & 0x3F);
266 // UTF-8 Doesn't support more than four bytes
271 * \fn Uint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
272 * \brief Converts a date into an Acess Timestamp
274 Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
281 stamp += day*3600*24;
282 stamp += month*DAYS_BEFORE[month]*3600*24;
284 ((year&3) == 0 || year%100 != 0)
285 || (year%100 == 0 && ((year/100)&3) == 0)
286 ) && month > 1) // Leap year and after feb
289 stamp += ((365*4+1) * ((year-2000)&~3)) * 3600*24; // Foour Year Segments
290 stamp += ((year-2000)&3) * 365*3600*24; // Inside four year segment