]> git.buserror.net Git - polintos/scott/priv.git/blob - include/c++/stl/stl/_time_facets.c
Add STLport 5.1.4
[polintos/scott/priv.git] / include / c++ / stl / stl / _time_facets.c
1 /*
2  * Copyright (c) 1999
3  * Silicon Graphics Computer Systems, Inc.
4  *
5  * Copyright (c) 1999
6  * Boris Fomitchev
7  *
8  * This material is provided "as is", with absolutely no warranty expressed
9  * or implied. Any use is at your own risk.
10  *
11  * Permission to use or copy this software for any purpose is hereby granted
12  * without fee, provided the above notices are retained on all copies.
13  * Permission to modify the code and to distribute modified code is granted,
14  * provided the above notices are retained, and a notice that the code was
15  * modified is included with the above copyright notice.
16  *
17  */
18 #ifndef _STLP_TIME_FACETS_C
19 #define _STLP_TIME_FACETS_C
20
21 #ifndef _STLP_INTERNAL_TIME_FACETS_H
22 #  include <stl/_time_facets.h>
23 #endif
24
25 #ifndef _STLP_INTERNAL_NUM_PUT_H
26 #  include <stl/_num_put.h>
27 #endif
28
29 #ifndef _STLP_INTERNAL_NUM_GET_H
30 #  include <stl/_num_get.h>
31 #endif
32
33 _STLP_BEGIN_NAMESPACE
34
35 //----------------------------------------------------------------------
36 // Declarations of static template members.
37 #if (_STLP_STATIC_TEMPLATE_DATA > 0)
38
39 #  if !defined (__BORLANDC__)
40 template <class _CharT, class _InputIterator>
41 locale::id time_get<_CharT, _InputIterator>::id;
42
43 template <class _CharT, class _OutputIterator>
44 locale::id time_put<_CharT, _OutputIterator>::id;
45 #  endif
46
47 #  if (defined (__CYGWIN__) || defined (__MINGW32__)) && \
48        defined (_STLP_USE_DYNAMIC_LIB) && !defined (__BUILDING_STLPORT)
49 /*
50  * Under cygwin, when STLport is used as a shared library, the id needs
51  * to be specified as imported otherwise they will be duplicated in the
52  * calling executable.
53  */
54 template <>
55 _STLP_DECLSPEC locale::id time_get<char, istreambuf_iterator<char, char_traits<char> > >::id;
56 /*
57 template <>
58 _STLP_DECLSPEC locale::id time_get<char, const char*>::id;
59 */
60
61 template <>
62 _STLP_DECLSPEC locale::id time_put<char, ostreambuf_iterator<char, char_traits<char> > >::id;
63 /*
64 template <>
65 _STLP_DECLSPEC locale::id time_put<char, char*>::id;
66 */
67
68 #    ifndef _STLP_NO_WCHAR_T
69 template <>
70 _STLP_DECLSPEC locale::id time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id;
71 /*
72 template <>
73 _STLP_DECLSPEC locale::id time_get<wchar_t, const wchar_t*>::id;
74 */
75
76 template <>
77 _STLP_DECLSPEC locale::id time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id;
78 /*
79 template <>
80 _STLP_DECLSPEC locale::id time_put<wchar_t, wchar_t*>::id;
81 */
82 #    endif /* _STLP_NO_WCHAR_T */
83 #  endif /* __CUGWIN__ && _STLP_USE_DYNAMIC_LIB */
84
85 #else /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
86
87 //typedef time_get<char, const char*> time_get_char;
88 typedef time_get<char, istreambuf_iterator<char, char_traits<char> > > time_get_char_2;
89 //typedef time_put<char, char*> time_put_char;
90 typedef time_put<char, ostreambuf_iterator<char, char_traits<char> > > time_put_char_2;
91
92 //__DECLARE_INSTANCE(locale::id, time_get_char::id, );
93 __DECLARE_INSTANCE(locale::id, time_get_char_2::id, );
94 //__DECLARE_INSTANCE(locale::id, time_put_char::id, );
95 __DECLARE_INSTANCE(locale::id, time_put_char_2::id, );
96
97 #  if !defined (_STLP_NO_WCHAR_T)
98
99 //typedef time_get<wchar_t, const wchar_t*> time_get_wchar_t;
100 typedef time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > > time_get_wchar_t_2;
101 //typedef time_put<wchar_t, wchar_t*> time_put_wchar_t;
102 typedef time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > > time_put_wchar_t_2;
103
104 //__DECLARE_INSTANCE(locale::id, time_get_wchar_t::id, );
105 __DECLARE_INSTANCE(locale::id, time_get_wchar_t_2::id, );
106 //__DECLARE_INSTANCE(locale::id, time_put_wchar_t::id, );
107 __DECLARE_INSTANCE(locale::id, time_put_wchar_t_2::id, );
108
109 #  endif
110
111 #endif /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
112
113 _STLP_MOVE_TO_PRIV_NAMESPACE
114
115 template <class _InIt, class _CharT>
116 const string* _STLP_CALL
117 __match(_InIt& __first, _InIt& __last, const string *__name, const string *__name_end,
118         const ctype<_CharT>& __ct) {
119   typedef ptrdiff_t difference_type;
120   difference_type __n = __name_end - __name;
121   difference_type __i;
122   size_t __pos = 0;
123   difference_type __check_count = __n;
124   bool __do_check[_MAXNAMES];
125   const string* __matching_name[_MAX_NAME_LENGTH];
126
127   for (__i = 0; __i < _MAXNAMES; ++__i)
128     __do_check[__i] = true;
129
130   for (__i = 0; __i < _MAX_NAME_LENGTH; ++__i)
131     __matching_name[__i] = __name_end;
132
133   while (__first != __last) {
134     for (__i = 0; __i < __n; ++__i) {
135       if (__do_check[__i]) {
136         if (*__first == __ct.widen(__name[__i][__pos])) {
137           if (__pos == (__name[__i].size() - 1)) {
138             __do_check[__i] = 0;
139             __matching_name[__pos + 1] = __name + __i;
140             --__check_count;
141             if (__check_count == 0) {
142               ++__first;
143               return __name + __i;
144             }
145           }
146         }
147         else {
148           __do_check[__i] = 0;
149           --__check_count;
150           if (__check_count == 0)
151             return __matching_name[__pos];
152         }
153       }
154     }
155
156     ++__first; ++__pos;
157   }
158
159   return __matching_name[__pos];
160 }
161
162 // __get_formatted_time reads input that is assumed to be formatted
163 // according to the rules for the C strftime function (C standard,
164 // 7.12.3.5).  This function is used to implement the do_get_time
165 // and do_get_date virtual functions, which depend on the locale
166 // specifications for the time and day formats respectively.
167 // Note the catchall default case, intended mainly for the '%Z'
168 // format designator, which does not make sense here since the
169 // representation of timezones is not part of the locale.
170 //
171 // The case branches are implemented either by doing a match using
172 // the appopriate name table or by doing a __get_integer_nogroup.
173 //
174 // 'y' format is assumed to mean that the input represents years
175 // since 1900.  That is, 2002 should be represented as 102.  There
176 // is no century-guessing.
177 //
178 // The match is successful if and only if the second component of the
179 // return value is format_end.
180
181 // Note that the antepenultimate parameter is being used only to determine
182 // the correct overloading for the calls to __get_integer_nogroup.
183 template <class _InIt1, class _Ch>
184 string::const_iterator _STLP_CALL
185 __get_formatted_time _STLP_WEAK (_InIt1 __first,  _InIt1 __last,
186                                  string::const_iterator __format, string::const_iterator __format_end,
187                                  _Ch*, const _Time_Info& __table,
188                                  const ios_base& __s, ios_base::iostate& __err, tm* __t) {
189   const ctype<_Ch>& __ct = *__STATIC_CAST(const ctype<_Ch>*, __s._M_ctype_facet());
190   while (__first != __last && __format != __format_end) {
191     if (*__format == '%') {
192       ++__format;
193       char __c = *__format;
194       if (__c == '#') { //MS extension
195         ++__format;
196         __c = *__format;
197       }
198
199       switch (__c) {
200         case 'a': {
201           const string* __pr = __match(__first, __last,
202                                        __table._M_dayname + 0, __table._M_dayname + 7,
203                                        __ct);
204           if (__pr == __table._M_dayname + 7)
205             return __format;
206           __t->tm_wday = __STATIC_CAST(int, __pr - __table._M_dayname);
207           break;
208         }
209
210         case 'A': {
211           const string* __pr = __match(__first, __last,
212                                        __table._M_dayname + 7, __table._M_dayname + 14,
213                                        __ct);
214           if (__pr == __table._M_dayname + 14)
215             return __format;
216           __t->tm_wday = __STATIC_CAST(int, __pr - __table._M_dayname - 7);
217           break;
218         }
219
220         case 'b': {
221           const string* __pr = __match(__first, __last,
222                                        __table._M_monthname + 0, __table._M_monthname + 12,
223                                        __ct);
224           if (__pr == __table._M_monthname + 12)
225             return __format;
226           __t->tm_mon = __STATIC_CAST(int, __pr - __table._M_monthname);
227           break;
228         }
229
230         case 'B': {
231           const string* __pr = __match(__first, __last,
232                                        __table._M_monthname + 12, __table._M_monthname + 24,
233                                        __ct);
234           if (__pr == __table._M_monthname + 24)
235             return __format;
236           __t->tm_mon = __STATIC_CAST(int, __pr - __table._M_monthname - 12);
237           break;
238         }
239
240         case 'd': {
241           bool __pr = __get_decimal_integer(__first, __last, __t->tm_mday, __STATIC_CAST(_Ch*, 0));
242           if (!__pr || __t->tm_mday < 1 || __t->tm_mday > 31) {
243             __err |= ios_base::failbit;
244             return __format;
245           }
246           break;
247         }
248
249         case 'H': case 'I': {
250           bool __pr = __get_decimal_integer(__first, __last, __t->tm_hour, __STATIC_CAST(_Ch*, 0));
251           if (!__pr)
252             return __format;
253           break;
254         }
255
256         case 'j': {
257           bool __pr = __get_decimal_integer(__first, __last, __t->tm_yday, __STATIC_CAST(_Ch*, 0));
258           if (!__pr)
259             return __format;
260           break;
261         }
262
263         case 'm': {
264           bool __pr = __get_decimal_integer(__first, __last, __t->tm_mon, __STATIC_CAST(_Ch*, 0));
265           --__t->tm_mon;
266           if (!__pr || __t->tm_mon < 0 || __t->tm_mon > 11) {
267             __err |= ios_base::failbit;
268             return __format;
269           }
270           break;
271         }
272
273         case 'M': {
274           bool __pr = __get_decimal_integer(__first, __last, __t->tm_min, __STATIC_CAST(_Ch*, 0));
275           if (!__pr)
276             return __format;
277           break;
278         }
279
280         case 'p': {
281           const string* __pr = __match(__first, __last,
282                                        __table._M_am_pm + 0, __table._M_am_pm + 2, __ct);
283           if (__pr == __table._M_am_pm + 2)
284             return __format;
285           // 12:00 PM <=> 12:00, 12:00 AM <=> 00:00
286           if (__pr == __table._M_am_pm + 1 && __t->tm_hour != 12 )
287             __t->tm_hour += 12;
288           if (__pr == __table._M_am_pm && __t->tm_hour == 12 )
289             __t->tm_hour = 0;
290           break;
291         }
292
293         case 'S': {
294           bool __pr = __get_decimal_integer(__first, __last, __t->tm_sec, __STATIC_CAST(_Ch*, 0));
295           if (!__pr)
296             return __format;
297           break;
298         }
299
300         case 'y': {
301           bool __pr = __get_decimal_integer(__first, __last, __t->tm_year, __STATIC_CAST(_Ch*, 0));
302           if (!__pr)
303             return __format;
304           break;
305         }
306
307         case 'Y': {
308           bool __pr = __get_decimal_integer(__first, __last, __t->tm_year, __STATIC_CAST(_Ch*, 0));
309           __t->tm_year -= 1900;
310           if (!__pr)
311             return __format;
312           break;
313         }
314
315         default:
316           break;
317       }
318     }
319     else {
320       if (*__first++ != __ct.widen(*__format)) break;
321     }
322
323     ++__format;
324   }
325
326   return __format;
327 }
328
329 template <class _InIt, class _CharT>
330 bool _STLP_CALL
331 __get_short_or_long_dayname(_InIt& __first, _InIt& __last, const ctype<_CharT>& __ct,
332                             const _Time_Info& __table, tm* __t) {
333   const string* __pr =
334     __match(__first, __last, __table._M_dayname + 0, __table._M_dayname + 14, __ct);
335   __t->tm_wday = __STATIC_CAST(int, (__pr - __table._M_dayname) % 7);
336   return __pr != __table._M_dayname + 14;
337 }
338
339 template <class _InIt, class _CharT>
340 bool _STLP_CALL
341 __get_short_or_long_monthname(_InIt& __first, _InIt& __last, const ctype<_CharT>& __ct,
342                               const _Time_Info& __table, tm* __t) {
343   const string* __pr =
344     __match(__first, __last, __table._M_monthname + 0, __table._M_monthname + 24, __ct);
345   __t->tm_mon = __STATIC_CAST(int, (__pr - __table._M_monthname) % 12);
346   return __pr != __table._M_monthname + 24;
347 }
348
349 #if !defined (_STLP_NO_WCHAR_T)
350 template <class _OuIt>
351 _OuIt _STLP_CALL
352 __put_time(char * __first, char * __last, _OuIt __out_ite,
353            const ios_base& __s, wchar_t) {
354     const ctype<wchar_t>& __ct = *__STATIC_CAST(const ctype<wchar_t>*, __s._M_ctype_facet());
355     wchar_t __wbuf[64];
356     __ct.widen(__first, __last, __wbuf);
357     ptrdiff_t __len = __last - __first;
358     wchar_t * __eend = __wbuf + __len;
359     return copy((wchar_t*)__wbuf, __eend, __out_ite);
360 }
361 #endif
362
363 _STLP_MOVE_TO_STD_NAMESPACE
364
365 template <class _Ch, class _InIt>
366 _InIt
367 time_get<_Ch, _InIt>::do_get_date(_InIt __s, _InIt  __end,
368                                   ios_base& __str, ios_base::iostate&  __err,
369                                   tm* __t) const {
370   typedef string::const_iterator string_iterator;
371
372   string_iterator __format = _M_timeinfo._M_date_format.begin();
373   string_iterator __format_end = _M_timeinfo._M_date_format.end();
374
375   string_iterator __result
376     = _STLP_PRIV __get_formatted_time(__s, __end, __format, __format_end,
377                                       __STATIC_CAST(_Ch*, 0), _M_timeinfo,
378                                       __str, __err, __t);
379   if (__result == __format_end)
380     __err = ios_base::goodbit;
381   else {
382     __err = ios_base::failbit;
383     if (__s == __end)
384       __err |= ios_base::eofbit;
385   }
386   return __s;
387 }
388
389 template <class _Ch, class _InIt>
390 _InIt
391 time_get<_Ch, _InIt>::do_get_time(_InIt __s, _InIt  __end,
392                                   ios_base& __str, ios_base::iostate&  __err,
393                                   tm* __t) const {
394   typedef string::const_iterator string_iterator;
395   string_iterator __format = _M_timeinfo._M_time_format.begin();
396   string_iterator __format_end = _M_timeinfo._M_time_format.end();
397
398   string_iterator __result
399     = _STLP_PRIV __get_formatted_time(__s, __end, __format, __format_end,
400                                       __STATIC_CAST(_Ch*, 0), _M_timeinfo,
401                                       __str, __err, __t);
402   __err = __result == __format_end ? ios_base::goodbit
403                                    : ios_base::failbit;
404   if (__s == __end)
405     __err |= ios_base::eofbit;
406   return __s;
407 }
408
409 template <class _Ch, class _InIt>
410 _InIt
411 time_get<_Ch, _InIt>::do_get_year(_InIt __s, _InIt  __end,
412                                   ios_base&, ios_base::iostate&  __err,
413                                   tm* __t) const {
414   if (__s == __end) {
415     __err = ios_base::failbit | ios_base::eofbit;
416     return __s;
417   }
418
419   bool __pr =  _STLP_PRIV __get_decimal_integer(__s, __end, __t->tm_year, __STATIC_CAST(_Ch*, 0));
420   __t->tm_year -= 1900;
421   __err = __pr ? ios_base::goodbit : ios_base::failbit;
422   if (__s == __end)
423     __err |= ios_base::eofbit;
424
425   return __s;
426 }
427
428 template <class _Ch, class _InIt>
429 _InIt
430 time_get<_Ch, _InIt>::do_get_weekday(_InIt __s, _InIt  __end,
431                                      ios_base &__str, ios_base::iostate &__err,
432                                      tm *__t) const {
433   const ctype<_Ch>& __ct = *__STATIC_CAST(const ctype<_Ch>*, __str._M_ctype_facet());
434   bool __result =
435     _STLP_PRIV __get_short_or_long_dayname(__s, __end, __ct, _M_timeinfo, __t);
436   if (__result)
437     __err = ios_base::goodbit;
438   else {
439     __err = ios_base::failbit;
440     if (__s == __end)
441       __err |= ios_base::eofbit;
442   }
443   return __s;
444 }
445
446 template <class _Ch, class _InIt>
447 _InIt
448 time_get<_Ch, _InIt>::do_get_monthname(_InIt __s, _InIt  __end,
449                                        ios_base &__str, ios_base::iostate &__err,
450                                        tm *__t) const {
451   const ctype<_Ch>& __ct = *__STATIC_CAST(const ctype<_Ch>*, __str._M_ctype_facet());
452   bool __result =
453     _STLP_PRIV __get_short_or_long_monthname(__s, __end, __ct, _M_timeinfo, __t);
454   if (__result)
455     __err = ios_base::goodbit;
456   else {
457     __err = ios_base::failbit;
458     if (__s == __end)
459       __err |= ios_base::eofbit;
460   }
461   return __s;
462 }
463
464 template<class _Ch, class _OutputIter>
465 _OutputIter
466 time_put<_Ch,_OutputIter>::put(_OutputIter __s, ios_base& __f, _Ch __fill,
467                                const tm* __tmb, const _Ch* __pat,
468                                const _Ch* __pat_end) const {
469   //  locale __loc = __f.getloc();
470   //  const ctype<_Ch>& _Ct = use_facet<ctype<_Ch> >(__loc);
471   const ctype<_Ch>& _Ct = *__STATIC_CAST(const ctype<_Ch>*, __f._M_ctype_facet());
472   while (__pat != __pat_end) {
473     char __c = _Ct.narrow(*__pat, 0);
474     if (__c == '%') {
475       char __mod = 0;
476       ++__pat;
477       __c = _Ct.narrow(*__pat++, 0);
478       if (__c == '#') { // MS extension
479         __mod = __c;
480         __c = _Ct.narrow(*__pat++, 0);
481       }
482       __s = do_put(__s, __f, __fill, __tmb, __c, __mod);
483     }
484     else
485       *__s++ = *__pat++;
486   }
487   return __s;
488 }
489
490 template<class _Ch, class _OutputIter>
491 _OutputIter
492 time_put<_Ch,_OutputIter>::do_put(_OutputIter __s, ios_base& __f, _Ch /* __fill */,
493                                   const tm* __tmb, char __format,
494                                   char __modifier ) const {
495   char __buf[64];
496   char * __iend = _STLP_PRIV __write_formatted_time(_STLP_ARRAY_AND_SIZE(__buf),
497                                                     __format, __modifier, _M_timeinfo, __tmb);
498   //  locale __loc = __f.getloc();
499   return _STLP_PRIV __put_time(__buf, __iend, __s, __f, _Ch());
500 }
501
502 _STLP_END_NAMESPACE
503
504 #endif /* _STLP_TIME_FACETS_C */
505
506 // Local Variables:
507 // mode:C++
508 // End: