| 1 | /****************************************************************************** |
| 2 | * Name: src/palmos/stdall.c |
| 3 | * Purpose: the missing functions of Palm OS for wxPalm |
| 4 | * Author: Yunhui Fu |
| 5 | * Created: 2007-10-18 |
| 6 | * Modified by: |
| 7 | * RCS-ID: $Id$ |
| 8 | * Copyright: (c) 2007 Yunhui Fu |
| 9 | * Licence: wxWindows licence |
| 10 | ******************************************************************************/ |
| 11 | |
| 12 | #include <Preferences.h> // Core/System/ |
| 13 | #include <DateTime.h> //Core/System/, DateToAscii() etc. |
| 14 | #include <TimeMgr.h> //Core/System/, TimGetTicks() |
| 15 | |
| 16 | #include "wx/defs.h" |
| 17 | #define PALMOS_TRACE(msg) ErrFatalErrorInContext(__FILE__, __LINE__, msg) |
| 18 | |
| 19 | #if __WXPALMOS6__ |
| 20 | |
| 21 | #ifdef TARGET_PLATFORM |
| 22 | #if TARGET_PLATFORM != TARGET_PLATFORM_PALMSIM_WIN32 |
| 23 | void |
| 24 | exit (int exitno) |
| 25 | { |
| 26 | } |
| 27 | #endif |
| 28 | #endif |
| 29 | |
| 30 | #else |
| 31 | |
| 32 | #define SEC_1904_1970 2082844800LU//0x7C25B080 // 0x7C25B080 == 2082844800LU |
| 33 | |
| 34 | time_t |
| 35 | time (time_t *T) |
| 36 | { |
| 37 | UInt32 sec; |
| 38 | // UInt32 TimGetSeconds(void); // seconds since 1/1/1904 |
| 39 | // void TimSetSeconds (UInt32 seconds); // seconds since 1/1/1904 |
| 40 | // UInt32 TimGetTicks(void); // ticks since power on |
| 41 | sec = TimGetSeconds (); |
| 42 | if (T) { |
| 43 | *T = sec - SEC_1904_1970; |
| 44 | } |
| 45 | return (sec - SEC_1904_1970); |
| 46 | } |
| 47 | |
| 48 | static struct tm * |
| 49 | palmtimet2unixtm (const time_t t, struct tm *ptm) |
| 50 | { |
| 51 | DateTimeType dt; |
| 52 | memset (&dt, 0, sizeof (dt)); |
| 53 | memset (ptm, 0, sizeof (*ptm)); |
| 54 | TimSecondsToDateTime (t, &dt); |
| 55 | ptm->tm_sec = dt.second; |
| 56 | ptm->tm_min = dt.minute; |
| 57 | ptm->tm_hour = dt.hour; |
| 58 | ptm->tm_mday = dt.day; |
| 59 | ptm->tm_mon = dt.month; |
| 60 | ptm->tm_year = dt.year; |
| 61 | ptm->tm_wday = dt.weekDay; |
| 62 | |
| 63 | //ptm->tm_wday = DayOfWeek (dt.month, dt.day, dt.year); |
| 64 | //ptm->tm_mday = DayOfMonth (dt.month, dt.day, dt.year); |
| 65 | return ptm; |
| 66 | } |
| 67 | |
| 68 | struct tm * |
| 69 | gmtime_r (const time_t *pt, struct tm *ptm) |
| 70 | { |
| 71 | UInt32 utcTime; |
| 72 | Int16 timeZone = PrefGetPreference(prefTimeZone); |
| 73 | Int16 daylightSavingAdjustment = PrefGetPreference(prefDaylightSavingAdjustment); |
| 74 | |
| 75 | if ((NULL == pt) || (NULL == ptm)) { |
| 76 | return NULL; |
| 77 | } |
| 78 | utcTime = TimTimeZoneToUTC (*pt + SEC_1904_1970, timeZone, daylightSavingAdjustment); |
| 79 | palmtimet2unixtm (utcTime, ptm); |
| 80 | return ptm; |
| 81 | } |
| 82 | |
| 83 | struct tm * |
| 84 | localtime_r (const time_t *pt, struct tm *ptm) |
| 85 | { |
| 86 | UInt32 utcTime; |
| 87 | Int16 timeZone = PrefGetPreference(prefTimeZone); |
| 88 | Int16 daylightSavingAdjustment = PrefGetPreference(prefDaylightSavingAdjustment); |
| 89 | if ((NULL == pt) || (NULL == ptm)) { |
| 90 | return NULL; |
| 91 | } |
| 92 | utcTime = TimUTCToTimeZone (*pt + SEC_1904_1970, timeZone, daylightSavingAdjustment); |
| 93 | palmtimet2unixtm (utcTime, ptm); |
| 94 | return ptm; |
| 95 | } |
| 96 | /* |
| 97 | static struct tm g_gmtime_info; |
| 98 | |
| 99 | struct tm * |
| 100 | gmtime (const time_t *CLOCK) |
| 101 | { |
| 102 | if (NULL == CLOCK) { |
| 103 | return NULL; |
| 104 | } |
| 105 | if (NULL == gmtime_r (CLOCK, &g_gmtime_info)) { |
| 106 | return NULL; |
| 107 | } |
| 108 | return &g_gmtime_info; |
| 109 | } |
| 110 | */ |
| 111 | static struct tm g_localtime_info; |
| 112 | |
| 113 | struct tm * |
| 114 | localtime (const time_t *CLOCK) |
| 115 | { |
| 116 | if (NULL == CLOCK) { |
| 117 | return NULL; |
| 118 | } |
| 119 | if (NULL == localtime_r (CLOCK, &g_localtime_info)) { |
| 120 | return NULL; |
| 121 | } |
| 122 | return &g_localtime_info; |
| 123 | } |
| 124 | |
| 125 | static char * g_strftime_wdayconv[][2] = { |
| 126 | {"Sun", "Sunday"}, |
| 127 | {"Mon", "Monday"}, |
| 128 | {"Tue", "Tuesday"}, |
| 129 | {"Wed", "Wednesday"}, |
| 130 | {"Thu", "Thursday"}, |
| 131 | {"Fri", "Friday"}, |
| 132 | {"Sat", "Saturday"}, |
| 133 | }; |
| 134 | |
| 135 | static char * g_strftime_monconv[][2] = { |
| 136 | {"Jan", "January"}, |
| 137 | {"Feb", "Febuary"}, |
| 138 | {"Mar", "March"}, |
| 139 | {"Apr", "April"}, |
| 140 | {"May", "May"}, |
| 141 | {"Jun", "June"}, |
| 142 | {"Jul", "July"}, |
| 143 | {"Aug", "August"}, |
| 144 | {"Sep", "September"}, |
| 145 | {"Oct", "October"}, |
| 146 | {"Nov", "November"}, |
| 147 | {"Dec", "December"}, |
| 148 | }; |
| 149 | #define SFCONV_ABBR 0 |
| 150 | #define SFCONV_FULL 1 |
| 151 | |
| 152 | size_t |
| 153 | strftime (char *buf, size_t sz_buf, const char *fmt, const struct tm *ptm) |
| 154 | { |
| 155 | char *p; |
| 156 | char *pret; |
| 157 | if ((timeStringLength > sz_buf) || (strlen (fmt) < 1)) { |
| 158 | return 0; |
| 159 | } |
| 160 | memset (buf, 0, sz_buf); |
| 161 | p = fmt; |
| 162 | pret = buf; |
| 163 | for (; *p != '\0';) { |
| 164 | if ('%' == *p) { |
| 165 | p ++; |
| 166 | if (*p == '\0') { |
| 167 | break; |
| 168 | } |
| 169 | // FIXME: test the overflow of the buffer |
| 170 | switch (*p) { |
| 171 | case 'a': |
| 172 | strcpy (pret, g_strftime_wdayconv[ptm->tm_wday % 7][SFCONV_ABBR]); |
| 173 | pret += 3; |
| 174 | break; |
| 175 | case 'A': |
| 176 | strcpy (pret, g_strftime_wdayconv[ptm->tm_wday % 7][SFCONV_FULL]); |
| 177 | pret += strlen (g_strftime_wdayconv[ptm->tm_wday % 7][SFCONV_FULL]); |
| 178 | break; |
| 179 | case 'b': |
| 180 | strcpy (pret, g_strftime_monconv[ptm->tm_mon % 12][SFCONV_ABBR]); |
| 181 | pret += 3; |
| 182 | break; |
| 183 | case 'B': |
| 184 | strcpy (pret, g_strftime_monconv[ptm->tm_mon % 12][SFCONV_FULL]); |
| 185 | pret += strlen (g_strftime_monconv[ptm->tm_mon % 12][SFCONV_FULL]); |
| 186 | break; |
| 187 | case 'c': |
| 188 | strcpy (pret, g_strftime_wdayconv[ptm->tm_wday % 7][SFCONV_ABBR]); |
| 189 | pret += 3; |
| 190 | *pret = ' '; |
| 191 | pret ++; |
| 192 | strcpy (pret, g_strftime_monconv[ptm->tm_mon % 12][SFCONV_ABBR]); |
| 193 | pret += 3; |
| 194 | *pret = ' '; |
| 195 | pret ++; |
| 196 | sprintf (pret, "%d %02d:%02d:%02d %d", ptm->tm_mday, |
| 197 | ptm->tm_hour, ptm->tm_min, ptm->tm_sec, ptm->tm_year); |
| 198 | pret += strlen (pret); |
| 199 | break; |
| 200 | case 'd': |
| 201 | sprintf (pret, "%02d", ptm->tm_mday % 31); |
| 202 | pret += 2; |
| 203 | break; |
| 204 | case 'H': |
| 205 | sprintf (pret, "%02d", ptm->tm_hour % 24); |
| 206 | pret += 2; |
| 207 | break; |
| 208 | case 'I': |
| 209 | sprintf (pret, "%02d", (ptm->tm_hour % 12) + 1); |
| 210 | pret += 2; |
| 211 | break; |
| 212 | case 'j': |
| 213 | sprintf (pret, "%03d", ptm->tm_year % 400); |
| 214 | pret += 3; |
| 215 | break; |
| 216 | case 'm': |
| 217 | sprintf (pret, "%02d", (ptm->tm_mon % 12) + 1); |
| 218 | pret += 2; |
| 219 | break; |
| 220 | case 'M': |
| 221 | sprintf (pret, "%02d", ptm->tm_min % 60); |
| 222 | pret += 2; |
| 223 | break; |
| 224 | case 'p': |
| 225 | if (ptm->tm_hour > 12) { |
| 226 | strcpy (pret, "PM"); |
| 227 | break; |
| 228 | } |
| 229 | strcpy (pret, "AM"); |
| 230 | pret += 2; |
| 231 | break; |
| 232 | case 'S': |
| 233 | sprintf (pret, "%02d", ptm->tm_sec % 61); |
| 234 | pret += 2; |
| 235 | break; |
| 236 | case 'U': |
| 237 | // FIXME: Week number with the first Sunday as the first day of week one (00-53) |
| 238 | sprintf (pret, "%02d", ptm->tm_yday / 7); |
| 239 | pret += 2; |
| 240 | break; |
| 241 | case 'w': |
| 242 | sprintf (pret, "%d", ptm->tm_wday % 7); |
| 243 | pret += 1; |
| 244 | break; |
| 245 | case 'W': |
| 246 | // FIXME: Week number with the first Sunday as the first day of week one (00-53) |
| 247 | sprintf (pret, "%02d", ptm->tm_yday / 7); |
| 248 | pret += 2; |
| 249 | break; |
| 250 | case 'x': |
| 251 | sprintf (pret, "%02d/%02d/%02d", |
| 252 | ptm->tm_mon, ptm->tm_mday, ptm->tm_year - 1900); |
| 253 | pret += strlen (pret); |
| 254 | break; |
| 255 | case 'X': |
| 256 | sprintf (pret, "%02d:%02d:%02d", |
| 257 | ptm->tm_hour, ptm->tm_min, ptm->tm_sec); |
| 258 | pret += strlen (pret); |
| 259 | break; |
| 260 | case 'y': |
| 261 | sprintf (pret, "%02d", ptm->tm_year - 1900); |
| 262 | pret += 2; |
| 263 | break; |
| 264 | case 'Y': |
| 265 | sprintf (pret, "%02d", ptm->tm_year); |
| 266 | pret += strlen (pret); |
| 267 | break; |
| 268 | case 'Z': |
| 269 | { |
| 270 | LmLocaleType ll; |
| 271 | ll.language = lmAnyLanguage; |
| 272 | ll.country = 0; |
| 273 | TimeZoneToAscii (ptm->tm_gmtoff, &ll, pret); |
| 274 | } |
| 275 | pret += strlen (pret); |
| 276 | break; |
| 277 | case '%': |
| 278 | *pret = *p; |
| 279 | pret ++; |
| 280 | break; |
| 281 | } |
| 282 | } else { |
| 283 | *pret = *p; |
| 284 | pret ++; |
| 285 | } |
| 286 | p ++; |
| 287 | } |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | time_t |
| 292 | mktime (struct tm *ptm) |
| 293 | { |
| 294 | DateTimeType dt; |
| 295 | if (NULL == ptm) { |
| 296 | return 0; |
| 297 | } |
| 298 | memset (&dt, 0, sizeof (dt)); |
| 299 | dt.second = ptm->tm_sec; |
| 300 | dt.minute = ptm->tm_min; |
| 301 | dt.hour = ptm->tm_hour; |
| 302 | dt.day = ptm->tm_mday; |
| 303 | dt.month = ptm->tm_mon; |
| 304 | dt.year = ptm->tm_year; |
| 305 | dt.weekDay = ptm->tm_wday; |
| 306 | return TimDateTimeToSeconds (&dt); |
| 307 | } |
| 308 | |
| 309 | int |
| 310 | vsscanf (const char *s, const char *format, va_list param) |
| 311 | { |
| 312 | return -1; |
| 313 | } |
| 314 | |
| 315 | FILE * fopen (const char *_name, const char *_type) {return NULL;} |
| 316 | int fclose (FILE *fp) {return 0;} |
| 317 | size_t fread (void *buf, size_t _size, size_t _n, FILE *fp) {return 0;} |
| 318 | size_t fwrite (const void *buf , size_t _size, size_t _n, FILE *fp) {return 0;} |
| 319 | int fseek (FILE *fp, long offset, int whence) {return -1;} |
| 320 | long ftell (FILE *fp) {return -1;} |
| 321 | int feof (FILE *fp) {return -1;} |
| 322 | int ferror (FILE *fp) {return -1;} |
| 323 | void clearerr (FILE *fp) {} |
| 324 | |
| 325 | #endif // __WXPALMOS6__ |
| 326 | |
| 327 | #define _BIT_ALPHA 0X0001 |
| 328 | #define _BIT_BLANK 0X0002 |
| 329 | #define _BIT_CNTRL 0X0004 |
| 330 | #define _BIT_DIGIT 0X0008 |
| 331 | #define _BIT_GRAPH 0X0010 |
| 332 | #define _BIT_LOWER 0X0020 |
| 333 | #define _BIT_PRINT 0X0040 |
| 334 | #define _BIT_PUNCT 0X0080 |
| 335 | #define _BIT_SPACE 0X0100 |
| 336 | #define _BIT_UPPER 0X0200 |
| 337 | #define _BIT_XDIGIT 0X0400 |
| 338 | |
| 339 | int iswalnum(wint_t C) {return 0;} |
| 340 | int iswalpha(wint_t C) {return 0;} |
| 341 | int iswcntrl(wint_t C) {return 0;} |
| 342 | int iswdigit(wint_t C) {return 0;} |
| 343 | int iswgraph(wint_t C) {return 0;} |
| 344 | int iswlower(wint_t C) {return 0;} |
| 345 | int iswprint(wint_t C) {return 0;} |
| 346 | int iswpunct(wint_t C) {return 0;} |
| 347 | int iswspace(wint_t C) {return 0;} |
| 348 | int iswupper(wint_t C) {return 0;} |
| 349 | int iswxdigit(wint_t C) {return 0;} |
| 350 | |
| 351 | wint_t towlower(wint_t C) {return 0;} |
| 352 | wint_t towupper(wint_t C) {return 0;} |
| 353 | size_t wcsftime(wchar_t *strDest, size_t maxsize, const wchar_t *format, const struct tm *timeptr) {return 0;} |
| 354 | |
| 355 | size_t |
| 356 | wcslen (const wchar_t * str) |
| 357 | { |
| 358 | size_t i; |
| 359 | for (i = 0; str[i] != 0; i ++); |
| 360 | return i; |
| 361 | } |
| 362 | |
| 363 | wchar_t * |
| 364 | wcscpy (wchar_t * dst, const wchar_t * src) |
| 365 | { |
| 366 | size_t len; |
| 367 | len = wcslen (src); |
| 368 | if (len < 1) { |
| 369 | return NULL; |
| 370 | } |
| 371 | memmove (dst, src, len * sizeof (wchar_t)); |
| 372 | dst[len] = 0; |
| 373 | return dst; |
| 374 | } |
| 375 | |
| 376 | wchar_t * |
| 377 | wcsncpy (wchar_t * dst, const wchar_t * src, size_t len_max) |
| 378 | { |
| 379 | size_t len; |
| 380 | len = wcslen (src); |
| 381 | if (len < 1) { |
| 382 | return NULL; |
| 383 | } |
| 384 | if (len_max < len + 1) { |
| 385 | len = len_max - 1; |
| 386 | } |
| 387 | if (len > 0) { |
| 388 | memmove (dst, src, len * sizeof (wchar_t)); |
| 389 | } |
| 390 | dst[len] = 0; |
| 391 | return dst; |
| 392 | } |
| 393 | |
| 394 | wchar_t * |
| 395 | wcscat (wchar_t * dst, const wchar_t * src) |
| 396 | { |
| 397 | size_t len_dst; |
| 398 | len_dst = wcslen (dst); |
| 399 | return wcscpy (dst + len_dst, src); |
| 400 | } |
| 401 | |
| 402 | wchar_t * |
| 403 | wcsncat (wchar_t * dst, const wchar_t * src, size_t n) |
| 404 | { |
| 405 | size_t len_dst; |
| 406 | len_dst = wcslen (dst); |
| 407 | return wcsncpy (dst + len_dst, src, n); |
| 408 | } |
| 409 | |
| 410 | #define SYS_IS_BIGENDIAN 0 |
| 411 | |
| 412 | #if SYS_IS_BIGENDIAN |
| 413 | #define _wcmcmp(a,b,len) memcmp((a),(b),(len) * sizeof (wchar_t)) |
| 414 | #else // SYS_IS_BIGENDIAN |
| 415 | int |
| 416 | _wcmcmp (const wchar_t * str1, const wchar_t * str2, size_t len) |
| 417 | { |
| 418 | size_t i; |
| 419 | for (i = 0; i < len; i ++) { |
| 420 | if (str1[i] == str2[i]) { |
| 421 | continue; |
| 422 | } else if (str1[i] < str2[i]) { |
| 423 | return -1; |
| 424 | } |
| 425 | return 1; |
| 426 | } |
| 427 | return 0; |
| 428 | } |
| 429 | #endif // SYS_IS_BIGENDIAN |
| 430 | |
| 431 | int |
| 432 | wcscmp (const wchar_t * str1, const wchar_t * str2) |
| 433 | { |
| 434 | int ret; |
| 435 | size_t len; |
| 436 | size_t len1; |
| 437 | size_t len2; |
| 438 | len1 = wcslen (str1); |
| 439 | len2 = wcslen (str2); |
| 440 | len = len1; |
| 441 | if (len > len2) { |
| 442 | len = len2; |
| 443 | } |
| 444 | ret = _wcmcmp (str1, str2, len); |
| 445 | if (0 == ret) { |
| 446 | if (len1 > len2) { |
| 447 | return -1; |
| 448 | } else if (len1 == len2) { |
| 449 | return 0; |
| 450 | } |
| 451 | return 1; |
| 452 | } |
| 453 | return ret; |
| 454 | } |
| 455 | |
| 456 | int |
| 457 | wcsncmp (const wchar_t * str1, const wchar_t * str2, size_t n) |
| 458 | { |
| 459 | int ret; |
| 460 | size_t len; |
| 461 | size_t len1; |
| 462 | size_t len2; |
| 463 | len1 = wcslen (str1); |
| 464 | len2 = wcslen (str2); |
| 465 | len = len1; |
| 466 | if (len > len2) { |
| 467 | len = len2; |
| 468 | } |
| 469 | if (len > n) { |
| 470 | len = n; |
| 471 | } |
| 472 | ret = _wcmcmp (str1, str2, len); |
| 473 | if (0 == ret) { |
| 474 | if (len >= n) { |
| 475 | return 0; |
| 476 | } |
| 477 | if (len1 > len2) { |
| 478 | return -1; |
| 479 | } else if (len1 == len2) { |
| 480 | return 0; |
| 481 | } |
| 482 | return 1; |
| 483 | } |
| 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | wchar_t * |
| 488 | wcschr (const wchar_t * str, const wchar_t chr) |
| 489 | { |
| 490 | wchar_t * str2 = (wchar_t *)str; |
| 491 | size_t i; |
| 492 | size_t len; |
| 493 | len = wcslen (str2); |
| 494 | for (i = 0; i < len; i ++) { |
| 495 | if (str2[i] == chr) { |
| 496 | str2 += i; |
| 497 | return str2; |
| 498 | } |
| 499 | } |
| 500 | return NULL; |
| 501 | } |
| 502 | |
| 503 | int wcscoll (const wchar_t *str1, const wchar_t * str2) {return wcscmp(str1, str2);} |
| 504 | |
| 505 | /* |
| 506 | * wcsxfrm: Transfom the wide-char str2 and place the result into the str1, |
| 507 | * return the length of the wide-char, return -1 on error. |
| 508 | */ |
| 509 | size_t |
| 510 | wcsxfrm (wchar_t * str1, const wchar_t * str2, size_t n) |
| 511 | { |
| 512 | wcsncpy (str1, str2, n); |
| 513 | return wcslen (str1); |
| 514 | } |
| 515 | |
| 516 | wchar_t * |
| 517 | wcsrchr (const wchar_t * str, wchar_t chr) |
| 518 | { |
| 519 | wchar_t * str2 = (wchar_t *)str; |
| 520 | ssize_t i; |
| 521 | i = wcslen (str2); |
| 522 | for (; i >= 0; i ++) { |
| 523 | if (str2[i] == chr) { |
| 524 | str2 += i; |
| 525 | return str2; |
| 526 | } |
| 527 | } |
| 528 | return NULL; |
| 529 | } |
| 530 | |
| 531 | wchar_t * |
| 532 | wcspbrk (const wchar_t * str, const wchar_t * set) |
| 533 | { |
| 534 | wchar_t * str2 = (wchar_t *)str; |
| 535 | size_t i; |
| 536 | size_t j; |
| 537 | size_t len; |
| 538 | size_t len_set; |
| 539 | len = wcslen (str2); |
| 540 | len_set = wcslen (set); |
| 541 | for (i = 0; i < len; i ++) { |
| 542 | for (j = 0; j < len_set; j ++) { |
| 543 | if (str2[i] == set[j]) { |
| 544 | str2 += i; |
| 545 | return str2; |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | return NULL; |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * wcsspn: compute the maxinum initial segment of the wide-char str which consists entirely of wide-char codes from the set. |
| 554 | * returnt he length of the initial substring of str |
| 555 | * examples: |
| 556 | * str="13134abcde", set="1234567890", wcsspn(str,set)==5 |
| 557 | * str="abcde", set="1234567890", wcsspn(str,set)==0 |
| 558 | */ |
| 559 | size_t |
| 560 | wcsspn (const wchar_t * str, const wchar_t * set) |
| 561 | { |
| 562 | size_t i; |
| 563 | size_t j; |
| 564 | size_t len; |
| 565 | size_t len_set; |
| 566 | len = wcslen (str); |
| 567 | len_set = wcslen (set); |
| 568 | for (i = 0; i < len; i ++) { |
| 569 | for (j = 0; j < len_set; j ++) { |
| 570 | if (str[i] == set[j]) { |
| 571 | break; |
| 572 | } |
| 573 | } |
| 574 | if (j >= len_set) { |
| 575 | return i; |
| 576 | } |
| 577 | } |
| 578 | return i; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * wcscspn: determines the length of the initial segment of str which consists entirely of characters not found in set. |
| 583 | * examples: |
| 584 | * str="13134abcde", set="1234567890", wcsspn(str,set)==0 |
| 585 | * str="abcde123", set="1234567890", wcsspn(str,set)==5 |
| 586 | */ |
| 587 | size_t |
| 588 | wcscspn (const wchar_t * str, const wchar_t * set) |
| 589 | { |
| 590 | size_t i; |
| 591 | size_t j; |
| 592 | size_t len; |
| 593 | size_t len_set; |
| 594 | len = wcslen (str); |
| 595 | len_set = wcslen (set); |
| 596 | for (i = 0; i < len; i ++) { |
| 597 | for (j = 0; j < len_set; j ++) { |
| 598 | if (str[i] == set[j]) { |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | if (j < len_set) { |
| 603 | return i; |
| 604 | } |
| 605 | } |
| 606 | return i; |
| 607 | } |
| 608 | |
| 609 | wchar_t * |
| 610 | wcsstr (const wchar_t * str, const wchar_t * pat) |
| 611 | { |
| 612 | // TODO: improvement the alg for search |
| 613 | wchar_t *p; |
| 614 | p = wcschr (str, pat[0]); |
| 615 | for (; NULL != p;) { |
| 616 | if (0 == wcscmp (p, pat)) { |
| 617 | return p; |
| 618 | } |
| 619 | p = wcschr (p, pat[0]); |
| 620 | } |
| 621 | return NULL; |
| 622 | } |
| 623 | |
| 624 | wchar_t * |
| 625 | wcstok (wchar_t * str, const wchar_t * set, wchar_t ** a) |
| 626 | { |
| 627 | wchar_t * p; |
| 628 | if (NULL == str) { |
| 629 | if (NULL == a) { |
| 630 | return NULL; |
| 631 | } |
| 632 | str = *a; |
| 633 | } |
| 634 | if (NULL == str) { |
| 635 | return NULL; |
| 636 | } |
| 637 | p = wcspbrk (str, set); |
| 638 | if (NULL == p) { |
| 639 | return NULL; |
| 640 | } |
| 641 | *p = 0; |
| 642 | *a = p; |
| 643 | return str; |
| 644 | } |
| 645 | |
| 646 | #define iswblank iswspace |
| 647 | //#define ULONG_MAX INT_MAX |
| 648 | unsigned long |
| 649 | wcstoul (const wchar_t *nptr, wchar_t **endptr, int base) |
| 650 | { |
| 651 | int flg_overflow; |
| 652 | //int val_remain; |
| 653 | unsigned long val_ch; |
| 654 | unsigned long val_old; |
| 655 | unsigned long val; |
| 656 | /* check the base */ |
| 657 | if ((1 == base) || (base > 36) || (base < 0)) { |
| 658 | // error |
| 659 | return 0; |
| 660 | } |
| 661 | // skip blank |
| 662 | while (iswblank (*nptr)) {nptr ++;} |
| 663 | if (0 == *nptr) { |
| 664 | return 0; |
| 665 | } |
| 666 | if (0 == base) { |
| 667 | // auto detect |
| 668 | switch (*nptr) { |
| 669 | case '0': |
| 670 | if (('x' == *(nptr + 1)) || ('X' == *(nptr + 1))) { |
| 671 | base = 16; |
| 672 | nptr += 2; |
| 673 | } else { |
| 674 | nptr += 1; |
| 675 | base = 8; |
| 676 | } |
| 677 | break; |
| 678 | case '1': |
| 679 | case '2': |
| 680 | case '3': |
| 681 | case '4': |
| 682 | case '5': |
| 683 | case '6': |
| 684 | case '7': |
| 685 | case '8': |
| 686 | case '9': |
| 687 | base = 10; |
| 688 | break; |
| 689 | } |
| 690 | } else { |
| 691 | if (16 == base) { |
| 692 | // detect if it has '0x' or '0X' |
| 693 | if (('0' == *nptr) && (('x' == *(nptr + 1)) || ('x' == *(nptr + 1)))) { |
| 694 | nptr += 2; |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | if (0 == base) { |
| 699 | // error |
| 700 | return 0; |
| 701 | } |
| 702 | val = 0; |
| 703 | val_old = 0; |
| 704 | flg_overflow = 0; |
| 705 | //val_remain = ULONG_MAX % base; |
| 706 | for (; '\0' != *nptr; nptr ++) { |
| 707 | val_ch = *nptr; |
| 708 | if (('0' <= val_ch) && (val_ch <= '9')) { |
| 709 | val_ch -= '0'; |
| 710 | } else if (('a' <= val_ch) && (val_ch <= 'z')) { |
| 711 | val_ch = val_ch - 'a' + 10; |
| 712 | } else if (('A' <= val_ch) && (val_ch <= 'Z')) { |
| 713 | val_ch = val_ch - 'A' + 10; |
| 714 | } else { |
| 715 | // val_ch = base + 1; |
| 716 | break; |
| 717 | } |
| 718 | if (val_ch >= base) { |
| 719 | break; |
| 720 | } |
| 721 | if (flg_overflow) continue; |
| 722 | val_old = val; |
| 723 | val *= base; |
| 724 | val += val_ch; |
| 725 | if (val_old > val) { |
| 726 | flg_overflow = 1; |
| 727 | } |
| 728 | } |
| 729 | if (flg_overflow) { |
| 730 | val = ULONG_MAX; |
| 731 | } |
| 732 | if (0L != endptr) { |
| 733 | *endptr = (wchar_t *)nptr; |
| 734 | } |
| 735 | return val; |
| 736 | } |
| 737 | |
| 738 | long |
| 739 | wcstol (const wchar_t * str, wchar_t ** end, int base) |
| 740 | { |
| 741 | int sign = 0; |
| 742 | unsigned long val0; |
| 743 | long val; |
| 744 | wchar_t ch; |
| 745 | // skip blank |
| 746 | for (; iswblank (*str); str ++) { |
| 747 | } |
| 748 | for (ch = *str; (ch == '-') || (ch == '+'); str ++) { |
| 749 | } |
| 750 | // the sign |
| 751 | if ('-' == ch) { |
| 752 | sign = 1; |
| 753 | str ++; |
| 754 | } |
| 755 | val0 = wcstoul (str, end, base); |
| 756 | if (val0 >= LONG_MAX) { |
| 757 | // overflow |
| 758 | val = LONG_MAX; |
| 759 | if (sign) { |
| 760 | val = LONG_MIN; |
| 761 | } |
| 762 | } else { |
| 763 | val = val0; |
| 764 | if (sign) { |
| 765 | val = -val0; |
| 766 | } |
| 767 | } |
| 768 | return val; |
| 769 | } |
| 770 | |
| 771 | double |
| 772 | wcstod (const wchar_t * str, wchar_t ** end) |
| 773 | { |
| 774 | double val; |
| 775 | double mantissa; |
| 776 | unsigned long divisor; |
| 777 | unsigned long power; |
| 778 | int sign; |
| 779 | int sign_power; |
| 780 | wchar_t *pend; |
| 781 | wchar_t ch; |
| 782 | // skip blank |
| 783 | for (; iswblank (*str); str ++) { |
| 784 | } |
| 785 | for (ch = *str; (ch == '-') || (ch == '+'); str ++) { |
| 786 | } |
| 787 | // the sign |
| 788 | sign = 0; |
| 789 | if ('-' == ch) { |
| 790 | sign = 1; |
| 791 | str ++; |
| 792 | } |
| 793 | // skip leading zero |
| 794 | for (; '0' == (*str); str ++) { |
| 795 | } |
| 796 | val = 0.0; |
| 797 | mantissa = 0.0; |
| 798 | divisor = 0; |
| 799 | power = 0.0; |
| 800 | // integer part |
| 801 | for (ch = *str; ('0' <= ch) && (ch <= '9'); str ++) { |
| 802 | ch -= '0'; |
| 803 | val *= 10; |
| 804 | val += ch; |
| 805 | } |
| 806 | // floating point & mantissa |
| 807 | if ('.' == *str) { |
| 808 | str ++; |
| 809 | for (ch = *str; ('0' <= ch) && (ch <= '9'); str ++) { |
| 810 | ch -= '0'; |
| 811 | mantissa *= 10.0; |
| 812 | mantissa += ch; |
| 813 | divisor ++; |
| 814 | } |
| 815 | } |
| 816 | for (; divisor > 0; divisor --) { |
| 817 | mantissa /= 10.0; |
| 818 | } |
| 819 | val += mantissa; |
| 820 | sign_power = 0; |
| 821 | if (('e' == *str) || ('E' == *str)) { |
| 822 | str ++; |
| 823 | if ('-' == ch) { |
| 824 | sign_power = 1; |
| 825 | str ++; |
| 826 | } |
| 827 | pend = NULL; |
| 828 | power = wcstoul (str, &pend, 10); |
| 829 | if (NULL != pend) { |
| 830 | str = pend; |
| 831 | } |
| 832 | } |
| 833 | if (power > 0) { |
| 834 | if (sign_power) { |
| 835 | for (; power > 0; power --) { |
| 836 | val /= 10.0; |
| 837 | } |
| 838 | } else { |
| 839 | for (; power > 0; power --) { |
| 840 | val *= 10.0; |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | if (sign) { |
| 845 | val = - val; |
| 846 | } |
| 847 | if (end) { |
| 848 | *end = (wchar_t *)str; |
| 849 | } |
| 850 | return val; |
| 851 | } |
| 852 | |
| 853 | char * setlocale (int category, const char *locale) {return NULL;} |
| 854 | |
| 855 | int |
| 856 | eof (int fd) |
| 857 | { |
| 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | int |
| 862 | remove (const char *fn) |
| 863 | { |
| 864 | return svfs_filedelete (fn); |
| 865 | } |
| 866 | |
| 867 | // access(): check access permissions of a file or pathname |
| 868 | // R_OK: read permission |
| 869 | // W_OK: write permission |
| 870 | // X_OK: execute/search permission |
| 871 | // F_OK: existence test |
| 872 | //All components of the pathname path are checked for access permissions (including F_OK) |
| 873 | int access(const char *path, int amode) {return amode;} |
| 874 | |
| 875 | off_t lseek(int fildes, off_t offset, int whence) {return 0;} |