1 /******************************************************************************
2 * Name: src/palmos/stdall.c
3 * Purpose: the missing functions of Palm OS for wxPalm
8 * Copyright: (c) 2007 Yunhui Fu
9 * Licence: wxWindows licence
10 ******************************************************************************/
12 #include <Preferences.h> // Core/System/
13 #include <DateTime.h> //Core/System/, DateToAscii() etc.
14 #include <TimeMgr.h> //Core/System/, TimGetTicks()
17 #define PALMOS_TRACE(msg) ErrFatalErrorInContext(__FILE__, __LINE__, msg)
21 #ifdef TARGET_PLATFORM
22 #if TARGET_PLATFORM != TARGET_PLATFORM_PALMSIM_WIN32
32 #define SEC_1904_1970 2082844800LU//0x7C25B080 // 0x7C25B080 == 2082844800LU
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 ();
43 *T
= sec
- SEC_1904_1970
;
45 return (sec
- SEC_1904_1970
);
49 palmtimet2unixtm (const time_t t
, struct tm
*ptm
)
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
;
63 //ptm->tm_wday = DayOfWeek (dt.month, dt.day, dt.year);
64 //ptm->tm_mday = DayOfMonth (dt.month, dt.day, dt.year);
69 gmtime_r (const time_t *pt
, struct tm
*ptm
)
72 Int16 timeZone
= PrefGetPreference(prefTimeZone
);
73 Int16 daylightSavingAdjustment
= PrefGetPreference(prefDaylightSavingAdjustment
);
75 if ((NULL
== pt
) || (NULL
== ptm
)) {
78 utcTime
= TimTimeZoneToUTC (*pt
+ SEC_1904_1970
, timeZone
, daylightSavingAdjustment
);
79 palmtimet2unixtm (utcTime
, ptm
);
84 localtime_r (const time_t *pt
, struct tm
*ptm
)
87 Int16 timeZone
= PrefGetPreference(prefTimeZone
);
88 Int16 daylightSavingAdjustment
= PrefGetPreference(prefDaylightSavingAdjustment
);
89 if ((NULL
== pt
) || (NULL
== ptm
)) {
92 utcTime
= TimUTCToTimeZone (*pt
+ SEC_1904_1970
, timeZone
, daylightSavingAdjustment
);
93 palmtimet2unixtm (utcTime
, ptm
);
97 static struct tm g_gmtime_info;
100 gmtime (const time_t *CLOCK)
105 if (NULL == gmtime_r (CLOCK, &g_gmtime_info)) {
108 return &g_gmtime_info;
111 static struct tm g_localtime_info
;
114 localtime (const time_t *CLOCK
)
119 if (NULL
== localtime_r (CLOCK
, &g_localtime_info
)) {
122 return &g_localtime_info
;
125 static char * g_strftime_wdayconv
[][2] = {
129 {"Wed", "Wednesday"},
135 static char * g_strftime_monconv
[][2] = {
144 {"Sep", "September"},
149 #define SFCONV_ABBR 0
150 #define SFCONV_FULL 1
153 strftime (char *buf
, size_t sz_buf
, const char *fmt
, const struct tm
*ptm
)
157 if ((timeStringLength
> sz_buf
) || (strlen (fmt
) < 1)) {
160 memset (buf
, 0, sz_buf
);
163 for (; *p
!= '\0';) {
169 // FIXME: test the overflow of the buffer
172 strcpy (pret
, g_strftime_wdayconv
[ptm
->tm_wday
% 7][SFCONV_ABBR
]);
176 strcpy (pret
, g_strftime_wdayconv
[ptm
->tm_wday
% 7][SFCONV_FULL
]);
177 pret
+= strlen (g_strftime_wdayconv
[ptm
->tm_wday
% 7][SFCONV_FULL
]);
180 strcpy (pret
, g_strftime_monconv
[ptm
->tm_mon
% 12][SFCONV_ABBR
]);
184 strcpy (pret
, g_strftime_monconv
[ptm
->tm_mon
% 12][SFCONV_FULL
]);
185 pret
+= strlen (g_strftime_monconv
[ptm
->tm_mon
% 12][SFCONV_FULL
]);
188 strcpy (pret
, g_strftime_wdayconv
[ptm
->tm_wday
% 7][SFCONV_ABBR
]);
192 strcpy (pret
, g_strftime_monconv
[ptm
->tm_mon
% 12][SFCONV_ABBR
]);
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
);
201 sprintf (pret
, "%02d", ptm
->tm_mday
% 31);
205 sprintf (pret
, "%02d", ptm
->tm_hour
% 24);
209 sprintf (pret
, "%02d", (ptm
->tm_hour
% 12) + 1);
213 sprintf (pret
, "%03d", ptm
->tm_year
% 400);
217 sprintf (pret
, "%02d", (ptm
->tm_mon
% 12) + 1);
221 sprintf (pret
, "%02d", ptm
->tm_min
% 60);
225 if (ptm
->tm_hour
> 12) {
233 sprintf (pret
, "%02d", ptm
->tm_sec
% 61);
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);
242 sprintf (pret
, "%d", ptm
->tm_wday
% 7);
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);
251 sprintf (pret
, "%02d/%02d/%02d",
252 ptm
->tm_mon
, ptm
->tm_mday
, ptm
->tm_year
- 1900);
253 pret
+= strlen (pret
);
256 sprintf (pret
, "%02d:%02d:%02d",
257 ptm
->tm_hour
, ptm
->tm_min
, ptm
->tm_sec
);
258 pret
+= strlen (pret
);
261 sprintf (pret
, "%02d", ptm
->tm_year
- 1900);
265 sprintf (pret
, "%02d", ptm
->tm_year
);
266 pret
+= strlen (pret
);
271 ll
.language
= lmAnyLanguage
;
273 TimeZoneToAscii (ptm
->tm_gmtoff
, &ll
, pret
);
275 pret
+= strlen (pret
);
292 mktime (struct tm
*ptm
)
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
);
310 vsscanf (const char *s
, const char *format
, va_list param
)
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
) {}
325 #endif // __WXPALMOS6__
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
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;}
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;}
356 wcslen (const wchar_t * str
)
359 for (i
= 0; str
[i
] != 0; i
++);
364 wcscpy (wchar_t * dst
, const wchar_t * src
)
371 memmove (dst
, src
, len
* sizeof (wchar_t));
377 wcsncpy (wchar_t * dst
, const wchar_t * src
, size_t len_max
)
384 if (len_max
< len
+ 1) {
388 memmove (dst
, src
, len
* sizeof (wchar_t));
395 wcscat (wchar_t * dst
, const wchar_t * src
)
398 len_dst
= wcslen (dst
);
399 return wcscpy (dst
+ len_dst
, src
);
403 wcsncat (wchar_t * dst
, const wchar_t * src
, size_t n
)
406 len_dst
= wcslen (dst
);
407 return wcsncpy (dst
+ len_dst
, src
, n
);
410 #define SYS_IS_BIGENDIAN 0
413 #define _wcmcmp(a,b,len) memcmp((a),(b),(len) * sizeof (wchar_t))
414 #else // SYS_IS_BIGENDIAN
416 _wcmcmp (const wchar_t * str1
, const wchar_t * str2
, size_t len
)
419 for (i
= 0; i
< len
; i
++) {
420 if (str1
[i
] == str2
[i
]) {
422 } else if (str1
[i
] < str2
[i
]) {
429 #endif // SYS_IS_BIGENDIAN
432 wcscmp (const wchar_t * str1
, const wchar_t * str2
)
438 len1
= wcslen (str1
);
439 len2
= wcslen (str2
);
444 ret
= _wcmcmp (str1
, str2
, len
);
448 } else if (len1
== len2
) {
457 wcsncmp (const wchar_t * str1
, const wchar_t * str2
, size_t n
)
463 len1
= wcslen (str1
);
464 len2
= wcslen (str2
);
472 ret
= _wcmcmp (str1
, str2
, len
);
479 } else if (len1
== len2
) {
488 wcschr (const wchar_t * str
, const wchar_t chr
)
490 wchar_t * str2
= (wchar_t *)str
;
494 for (i
= 0; i
< len
; i
++) {
495 if (str2
[i
] == chr
) {
503 int wcscoll (const wchar_t *str1
, const wchar_t * str2
) {return wcscmp(str1
, str2
);}
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.
510 wcsxfrm (wchar_t * str1
, const wchar_t * str2
, size_t n
)
512 wcsncpy (str1
, str2
, n
);
513 return wcslen (str1
);
517 wcsrchr (const wchar_t * str
, wchar_t chr
)
519 wchar_t * str2
= (wchar_t *)str
;
522 for (; i
>= 0; i
++) {
523 if (str2
[i
] == chr
) {
532 wcspbrk (const wchar_t * str
, const wchar_t * set
)
534 wchar_t * str2
= (wchar_t *)str
;
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
]) {
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
556 * str="13134abcde", set="1234567890", wcsspn(str,set)==5
557 * str="abcde", set="1234567890", wcsspn(str,set)==0
560 wcsspn (const wchar_t * str
, const wchar_t * set
)
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
]) {
582 * wcscspn: determines the length of the initial segment of str which consists entirely of characters not found in set.
584 * str="13134abcde", set="1234567890", wcsspn(str,set)==0
585 * str="abcde123", set="1234567890", wcsspn(str,set)==5
588 wcscspn (const wchar_t * str
, const wchar_t * set
)
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
]) {
610 wcsstr (const wchar_t * str
, const wchar_t * pat
)
612 // TODO: improvement the alg for search
614 p
= wcschr (str
, pat
[0]);
616 if (0 == wcscmp (p
, pat
)) {
619 p
= wcschr (p
, pat
[0]);
625 wcstok (wchar_t * str
, const wchar_t * set
, wchar_t ** a
)
637 p
= wcspbrk (str
, set
);
646 #define iswblank iswspace
647 //#define ULONG_MAX INT_MAX
649 wcstoul (const wchar_t *nptr
, wchar_t **endptr
, int base
)
653 unsigned long val_ch
;
654 unsigned long val_old
;
657 if ((1 == base
) || (base
> 36) || (base
< 0)) {
662 while (iswblank (*nptr
)) {nptr
++;}
670 if (('x' == *(nptr
+ 1)) || ('X' == *(nptr
+ 1))) {
692 // detect if it has '0x' or '0X'
693 if (('0' == *nptr
) && (('x' == *(nptr
+ 1)) || ('x' == *(nptr
+ 1)))) {
705 //val_remain = ULONG_MAX % base;
706 for (; '\0' != *nptr
; nptr
++) {
708 if (('0' <= val_ch
) && (val_ch
<= '9')) {
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;
715 // val_ch = base + 1;
718 if (val_ch
>= base
) {
721 if (flg_overflow
) continue;
733 *endptr
= (wchar_t *)nptr
;
739 wcstol (const wchar_t * str
, wchar_t ** end
, int base
)
746 for (; iswblank (*str
); str
++) {
748 for (ch
= *str
; (ch
== '-') || (ch
== '+'); str
++) {
755 val0
= wcstoul (str
, end
, base
);
756 if (val0
>= LONG_MAX
) {
772 wcstod (const wchar_t * str
, wchar_t ** end
)
776 unsigned long divisor
;
783 for (; iswblank (*str
); str
++) {
785 for (ch
= *str
; (ch
== '-') || (ch
== '+'); str
++) {
794 for (; '0' == (*str
); str
++) {
801 for (ch
= *str
; ('0' <= ch
) && (ch
<= '9'); str
++) {
806 // floating point & mantissa
809 for (ch
= *str
; ('0' <= ch
) && (ch
<= '9'); str
++) {
816 for (; divisor
> 0; divisor
--) {
821 if (('e' == *str
) || ('E' == *str
)) {
828 power
= wcstoul (str
, &pend
, 10);
835 for (; power
> 0; power
--) {
839 for (; power
> 0; power
--) {
848 *end
= (wchar_t *)str
;
853 char * setlocale (int category
, const char *locale
) {return NULL
;}
862 remove (const char *fn
)
864 return svfs_filedelete (fn
);
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
;}
875 off_t
lseek(int fildes
, off_t offset
, int whence
) {return 0;}