OS_NS_wchar.cpp

Go to the documentation of this file.
00001 // OS_NS_wchar.cpp,v 1.7 2005/10/28 16:14:54 ossama Exp
00002 
00003 #include "ace/OS_NS_wchar.h"
00004 
00005 ACE_RCSID(ace, OS_NS_wchar, "OS_NS_wchar.cpp,v 1.7 2005/10/28 16:14:54 ossama Exp")
00006 
00007 #if !defined (ACE_HAS_INLINED_OSCALLS)
00008 # include "ace/OS_NS_wchar.inl"
00009 #endif /* ACE_HAS_INLINED_OS_CALLS */
00010 
00011 #if defined (ACE_HAS_WCHAR)
00012 #  include "ace/OS_NS_ctype.h"
00013 #  include "ace/OS_NS_string.h"
00014 #endif /* ACE_HAS_WCHAR */
00015 
00016 // The following wcs*_emulation methods were created based on BSD code:
00017 /*-
00018  * Copyright (c) 1991, 1993
00019  *      The Regents of the University of California.  All rights reserved.
00020  *
00021  * This code is derived from software contributed to Berkeley by
00022  * James W. Williams of NASA Goddard Space Flight Center.
00023  *
00024  * Redistribution and use in source and binary forms, with or without
00025  * modification, are permitted provided that the following conditions
00026  * are met:
00027  * 1. Redistributions of source code must retain the above copyright
00028  *    notice, this list of conditions and the following disclaimer.
00029  * 2. Redistributions in binary form must reproduce the above copyright
00030  *    notice, this list of conditions and the following disclaimer in the
00031  *    documentation and/or other materials provided with the distribution.
00032  * 3. All advertising materials mentioning features or use of this software
00033  *    must display the following acknowledgement:
00034  *      This product includes software developed by the University of
00035  *      California, Berkeley and its contributors.
00036  * 4. Neither the name of the University nor the names of its contributors
00037  *    may be used to endorse or promote products derived from this software
00038  *    without specific prior written permission.
00039  *
00040  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00041  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00042  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00043  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00044  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00045  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00046  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00047  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00048  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00049  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00050  * SUCH DAMAGE.
00051  */
00052 
00053 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00054 
00055 #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSCAT)
00056 wchar_t *
00057 ACE_OS::wcscat_emulation (wchar_t *destination,
00058                           const wchar_t *source)
00059 {
00060   wchar_t *save = destination;
00061 
00062   for (; *destination; ++destination);
00063   while ((*destination++ = *source++));
00064   return save;
00065 }
00066 #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSCAT */
00067 
00068 #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSCHR)
00069 wchar_t *
00070 ACE_OS::wcschr_emulation (const wchar_t *string, wint_t c)
00071 {
00072   for (;*string ; ++string)
00073     if (*string == static_cast<wchar_t> (c))
00074       return const_cast<wchar_t *> (string);
00075 
00076   return 0;
00077 }
00078 #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSCHR */
00079 
00080 #if !defined (ACE_HAS_WCHAR) || defined (ACE_LACKS_WCSCMP)
00081 int
00082 ACE_OS::wcscmp_emulation (const ACE_WCHAR_T *string1,
00083                           const ACE_WCHAR_T *string2)
00084 {
00085   while (*string1 == *string2++)
00086     if (*string1++ == 0)
00087       return (0);
00088   return (*string1 - *--string2);
00089 }
00090 #endif /* !ACE_HAS_WCHAR || ACE_LACKS_WCSCMP */
00091 
00092 #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSCPY)
00093 wchar_t *
00094 ACE_OS::wcscpy_emulation (wchar_t *destination,
00095                           const wchar_t *source)
00096 {
00097   wchar_t *save = destination;
00098 
00099   for (; (*destination = *source); ++source, ++destination);
00100   return save;
00101 }
00102 #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSCPY */
00103 
00104 #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSCSPN)
00105 size_t
00106 ACE_OS::wcscspn_emulation (const wchar_t *s, const wchar_t *reject)
00107 {
00108   const wchar_t *scan;
00109   const wchar_t *rej_scan;
00110   int count = 0;
00111 
00112   for (scan = s; *scan; scan++)
00113     {
00114 
00115       for (rej_scan = reject; *rej_scan; rej_scan++)
00116         if (*scan == *rej_scan)
00117           return count;
00118 
00119       count++;
00120     }
00121 
00122   return count;
00123 }
00124 #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSCSPN */
00125 
00126 #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSICMP)
00127 int
00128 ACE_OS::wcsicmp_emulation (const wchar_t *s, const wchar_t *t)
00129 {
00130   const wchar_t *scan1 = s;
00131   const wchar_t *scan2 = t;
00132 
00133   while (*scan1 != 0
00134          && ACE_OS::ace_towlower (*scan1)
00135             == ACE_OS::ace_towlower (*scan2))
00136     {
00137       ++scan1;
00138       ++scan2;
00139     }
00140 
00141   // The following case analysis is necessary so that characters which
00142   // look negative collate low against normal characters but high
00143   // against the end-of-string NUL.
00144 
00145   if (*scan1 == '\0' && *scan2 == '\0')
00146     return 0;
00147   else if (*scan1 == '\0')
00148     return -1;
00149   else if (*scan2 == '\0')
00150     return 1;
00151   else
00152     return ACE_OS::ace_tolower (*scan1) - ACE_OS::ace_towlower (*scan2);
00153 }
00154 #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSICMP */
00155 
00156 #if !defined (ACE_HAS_WCHAR) || defined (ACE_LACKS_WCSLEN)
00157 size_t
00158 ACE_OS::wcslen_emulation (const ACE_WCHAR_T *string)
00159 {
00160   const ACE_WCHAR_T *s;
00161 
00162   for (s = string; *s; ++s)
00163     continue;
00164 
00165   return s - string;
00166 }
00167 #endif /* !ACE_HAS_WCHAR || ACE_LACKS_WCSLEN */
00168 
00169 #if !defined (ACE_HAS_WCHAR) || defined (ACE_LACKS_WCSNCAT)
00170 ACE_WCHAR_T *
00171 ACE_OS::wcsncat_emulation (ACE_WCHAR_T *destination,
00172                            const ACE_WCHAR_T *source,
00173                            size_t count)
00174 {
00175   if (count != 0)
00176     {
00177       ACE_WCHAR_T *d = destination;
00178       const ACE_WCHAR_T *s = source;
00179 
00180       while (*d != 0)
00181         d++;
00182 
00183       do
00184         {
00185           if ((*d = *s++) == 0)
00186             break;
00187 
00188           d++;
00189         } while (--count != 0);
00190 
00191       *d = 0;
00192     }
00193 
00194   return destination;
00195 }
00196 #endif /* !ACE_HAS_WCHAR || ACE_LACKS_WCSCAT */
00197 
00198 #if !defined (ACE_HAS_WCHAR) || defined (ACE_LACKS_WCSNCMP)
00199 int
00200 ACE_OS::wcsncmp_emulation (const ACE_WCHAR_T *s1,
00201                            const ACE_WCHAR_T *s2,
00202                            size_t len)
00203 {
00204   if (len == 0)
00205     return 0;
00206 
00207   do
00208     {
00209       if (*s1 != *s2++)
00210         return (*s1 - *--s2);
00211       if (*s1++ == 0)
00212         break;
00213     } while (--len != 0);
00214 
00215   return 0;
00216 }
00217 #endif /* !ACE_HAS_WCHAR || ACE_LACKS_WCSNCMP */
00218 
00219 #if !defined (ACE_HAS_WCHAR) || defined (ACE_LACKS_WCSNCPY)
00220 ACE_WCHAR_T *
00221 ACE_OS::wcsncpy_emulation (ACE_WCHAR_T *destination,
00222                            const ACE_WCHAR_T *source,
00223                            size_t len)
00224 {
00225   if (len != 0)
00226     {
00227       ACE_WCHAR_T *d = destination;
00228       const ACE_WCHAR_T *s = source;
00229 
00230       do
00231         {
00232           if ((*d++ = *s++) == 0)
00233             {
00234               // NUL pad the remaining n-1 bytes
00235               while (--len != 0)
00236                 *d++ = 0;
00237               break;
00238             }
00239         } while (--len != 0);
00240     }
00241 
00242   return destination;
00243 }
00244 #endif /* !ACE_HAS_WCHAR || ACE_LACKS_WCSNCPY */
00245 
00246 #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSNICMP)
00247 int
00248 ACE_OS::wcsnicmp_emulation (const wchar_t *s,
00249                             const wchar_t *t,
00250                             size_t len)
00251 {
00252   const wchar_t *scan1 = s;
00253   const wchar_t *scan2 = t;
00254   size_t count = 0;
00255 
00256   while (count++ < len
00257          && *scan1 != 0
00258          && ACE_OS::ace_towlower (*scan1)
00259             == ACE_OS::ace_towlower (*scan2))
00260     {
00261       ++scan1;
00262       ++scan2;
00263     }
00264 
00265   if (count > len)
00266     return 0;
00267 
00268   // The following case analysis is necessary so that characters which
00269   // look negative collate low against normal characters but high
00270   // against the end-of-string NUL.
00271 
00272   if (*scan1 == '\0' && *scan2 == '\0')
00273     return 0;
00274   else if (*scan1 == '\0')
00275     return -1;
00276   else if (*scan2 == '\0')
00277     return 1;
00278   else
00279     return ACE_OS::ace_towlower (*scan1) - ACE_OS::ace_towlower (*scan2);
00280 }
00281 #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSNICMP */
00282 
00283 #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSPBRK)
00284 wchar_t *
00285 ACE_OS::wcspbrk_emulation (const wchar_t *string,
00286                            const wchar_t *charset)
00287 {
00288   const wchar_t *scanp;
00289   int c, sc;
00290 
00291   while ((c = *string++) != 0)
00292     {
00293       for (scanp = charset; (sc = *scanp++) != 0;)
00294         if (sc == c)
00295           return const_cast<wchar_t *> (string - 1);
00296     }
00297 
00298   return 0;
00299 }
00300 #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSPBRK */
00301 
00302 #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSRCHR)
00303 const wchar_t *
00304 ACE_OS::wcsrchr_emulation (const wchar_t *s, wint_t c)
00305 {
00306   const wchar_t *p = s + ACE_OS::strlen (s);
00307 
00308   while (*p != static_cast<wchar_t> (c))
00309     if (p == s)
00310       return 0;
00311     else
00312       p--;
00313 
00314   return p;
00315 }
00316 
00317 wchar_t *
00318 ACE_OS::wcsrchr_emulation (wchar_t *s, wint_t c)
00319 {
00320   wchar_t *p = s + ACE_OS::strlen (s);
00321 
00322   while (*p != static_cast<wchar_t> (c))
00323     if (p == s)
00324       return 0;
00325     else
00326       p--;
00327 
00328   return p;
00329 }
00330 #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSRCHR */
00331 
00332 #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSSPN)
00333 size_t
00334 ACE_OS::wcsspn_emulation (const wchar_t *string,
00335                           const wchar_t *charset)
00336 {
00337   const wchar_t *p = string;
00338   const wchar_t *spanp;
00339   wchar_t c, sc;
00340 
00341   // Skip any characters in charset, excluding the terminating \0.
00342 cont:
00343   c = *p++;
00344   for (spanp = charset; (sc = *spanp++) != 0;)
00345     if (sc == c)
00346       goto cont;
00347   return (p - 1 - string);
00348 }
00349 #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSSPN */
00350 
00351 #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSSTR)
00352 wchar_t *
00353 ACE_OS::wcsstr_emulation (const wchar_t *string,
00354                           const wchar_t *charset)
00355 {
00356   wchar_t c, sc;
00357   size_t len;
00358 
00359   if ((c = *charset++) != 0)
00360     {
00361       len = ACE_OS::strlen (charset);
00362       do
00363         {
00364           do
00365             {
00366               if ((sc = *string++) == 0)
00367                 return 0;
00368             } while (sc != c);
00369         } while (ACE_OS::strncmp (string, charset, len) != 0);
00370       string--;
00371     }
00372 
00373   return const_cast<wchar_t *> (string);
00374 }
00375 #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSSTR */
00376 
00377 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 09:41:58 2006 for ACE by doxygen 1.3.6