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