home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / common / ustring.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-26  |  4.0 KB  |  183 lines

  1. /*
  2. *******************************************************************************
  3. *                                                                             *
  4. * COPYRIGHT:                                                                  *
  5. *   (C) Copyright International Business Machines Corporation, 1998           *
  6. *   Licensed Material - Program-Property of IBM - All Rights Reserved.        *
  7. *   US Government Users Restricted Rights - Use, duplication, or disclosure   *
  8. *   restricted by GSA ADP Schedule Contract with IBM Corp.                    *
  9. *                                                                             *
  10. *******************************************************************************
  11. *
  12. * File ustring.h
  13. *
  14. * Modification History:
  15. *
  16. *   Date        Name        Description
  17. *   12/07/98    bertrand    Creation.
  18. *******************************************************************************
  19. */
  20.  
  21. #include "ustring.h"
  22. #include "utypes.h"
  23. #include "cstring.h"
  24. #include "ucnv.h"
  25.  
  26.  
  27. static UConverter* _defaultConverter = NULL;
  28. static UErrorCode gErr = U_ZERO_ERROR;
  29.  
  30. #define MAX_STRLEN 0x0FFFFFFF
  31.  
  32. /*Lazy evaluating macro for the default converter*/
  33. #define defaultConverter (_defaultConverter==NULL)?_defaultConverter=ucnv_open(NULL, &gErr):_defaultConverter
  34.  
  35. UChar*
  36. u_strcat(UChar     *dst, 
  37.     const UChar     *src)
  38. {
  39.   UChar *anchor = dst;       /* save a pointer to start of dst */
  40.  
  41.   while(*dst++);               /* To end of first string          */
  42.   dst--;                       /* Return to the null              */
  43.   while(*dst++ = *src++);      /* copy string 2 over              */
  44.   return anchor;
  45. }
  46.  
  47. UChar* 
  48. u_strncat(UChar     *dst, 
  49.      const UChar     *src, 
  50.      int32_t     n ) 
  51. {
  52.   UChar *anchor = dst;       /* save a pointer to start of dst */
  53.   
  54.   if (!n) return dst;
  55.   while(*dst++);               /* To end of first string          */
  56.   dst--;                       /* Return to the null              */
  57.   while((*dst++ = *src++) && --n);    /* copy string 2 over              */
  58.   *dst = 0x0000;
  59.   
  60.   return anchor;
  61. }
  62.  
  63. UChar*
  64. u_strchr(const UChar     *s, 
  65.     UChar     c) 
  66. {
  67.   while((*s != c) && *s) 
  68.     s++;
  69.   
  70.   if(*s == c)
  71.     return (UChar*) s;
  72.   return NULL;
  73. }
  74.  
  75. int32_t  
  76. u_strcmp(const UChar *s1, 
  77.     const UChar *s2) 
  78. {
  79.   while((*s1 == *s2) && *s1) {
  80.     s1++;
  81.     s2++;
  82.   }
  83.  
  84.   return (int32_t)*s1 - (int32_t)*s2;
  85. }
  86.  
  87. int32_t  
  88. u_strncmp(const UChar     *s1, 
  89.      const UChar     *s2, 
  90.      int32_t     n) 
  91. {
  92.   if (!n) return 0;
  93.   while((*s1 == *s2) && *s1 && --n) {
  94.     s1++;
  95.     s2++;
  96.   }
  97.   return  (int32_t)*s1 - (int32_t)*s2;
  98. }
  99.  
  100. UChar*
  101. u_strcpy(UChar     *dst, 
  102.     const UChar     *src) 
  103. {
  104.   UChar *anchor = dst;     /* save the start of result string */
  105.   
  106.   while(*dst++ = *src++);
  107.   return anchor;
  108. }
  109.  
  110. UChar* 
  111. u_strncpy(UChar     *dst, 
  112.      const UChar     *src, 
  113.      int32_t     n) 
  114. {
  115.   UChar *anchor = dst;     /* save the start of result string */
  116.   
  117.   if (!n) return dst;
  118.   while((*dst++ = *src++) && --n);
  119.   *dst = 0x0000;
  120.   return anchor;
  121. }
  122.  
  123. int32_t  
  124. u_strlen(const UChar *s) 
  125. {
  126.   int32_t  i = 0;
  127.   
  128.   while(*s++)
  129.     i++;
  130.   return  i;
  131. }
  132.  
  133.  
  134. UChar* u_uastrcpy(UChar *ucs1,
  135.           const char *s2 )
  136. {
  137.   UErrorCode err = U_ZERO_ERROR;
  138.   ucnv_toUChars(defaultConverter,
  139.         ucs1,
  140.         MAX_STRLEN,
  141.         s2,
  142.         icu_strlen(s2),
  143.         &err);
  144.   
  145.   return ucs1;
  146. }
  147.  
  148. UChar* u_uastrncpy(UChar *ucs1,
  149.            const char *s2 ,
  150.            int32_t n)
  151. {
  152.   UErrorCode err = U_ZERO_ERROR;
  153.   int32_t end = ucnv_toUChars(defaultConverter,
  154.                   ucs1,
  155.                   n,
  156.                   s2,
  157.                   icu_strlen(s2),
  158.                   &err);
  159.   
  160.   ucs1[icu_min(end,n)] = 0x0000;
  161.   return ucs1;
  162. }
  163.  
  164. char* u_austrcpy(char *s1,
  165.          const UChar *ucs2 )
  166. {
  167.   char * anchor = s1;     /* save the start of result string */
  168.   UErrorCode err = U_ZERO_ERROR;
  169.   int32_t len = ucnv_fromUChars(defaultConverter,
  170.                 s1,
  171.                 MAX_STRLEN,
  172.                 ucs2,
  173.                 &err);
  174.   
  175.   s1[len] = '\0';
  176.   return s1;
  177.   
  178. }
  179.  
  180.  
  181.  
  182.  
  183.