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