home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / tools / genrb / ustr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  3.4 KB  |  142 lines

  1. /*
  2. *******************************************************************************
  3. *                                                                             *
  4. * COPYRIGHT:                                                                  *
  5. *   (C) Copyright International Business Machines Corporation, 1998, 1999     *
  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 ustr.c
  13. *
  14. * Modification History:
  15. *
  16. *   Date        Name        Description
  17. *   05/28/99    stephen     Creation.
  18. *******************************************************************************
  19. */
  20.  
  21. #include "ustr.h"
  22. #include "cmemory.h"
  23.  
  24. /* Protos */
  25. static void ustr_resize(struct UString *s, int32_t len, UErrorCode *status);
  26.  
  27. /* Macros */
  28. #define ALLOCATION(minSize) (minSize < 0x80 ? 0x80 : (2 * minSize + 0x80) & ~(0x80 - 1))
  29.  
  30. void
  31. ustr_init(struct UString *s)
  32. {
  33.   s->fChars = 0;
  34.   s->fLength = s->fCapacity = 0;
  35. }
  36.  
  37. void
  38. ustr_deinit(struct UString *s)
  39. {
  40.   icu_free(s->fChars);
  41.   s->fChars = 0;
  42.   s->fLength = s->fCapacity = 0;
  43. }
  44.                
  45. void
  46. ustr_cpy(struct UString *dst, 
  47.      const struct UString *src, 
  48.      UErrorCode *status)
  49. {
  50.   if(U_FAILURE(*status) || dst == src) return;
  51.   
  52.   if(dst->fCapacity < src->fLength) {
  53.     ustr_resize(dst, ALLOCATION(src->fLength), status);
  54.     if(U_FAILURE(*status)) return;
  55.   }
  56.  
  57.   icu_memcpy(dst->fChars, src->fChars, sizeof(UChar) * src->fLength);
  58.   dst->fLength = src->fLength;
  59.   dst->fChars[dst->fLength] = 0x0000;
  60. }
  61.  
  62. void
  63. ustr_setlen(struct UString *s, 
  64.         int32_t len,
  65.         UErrorCode *status)
  66. {
  67.   if(U_FAILURE(*status)) return;
  68.  
  69.   if(s->fCapacity < (len + 1)) {
  70.     ustr_resize(s, ALLOCATION(len), status);
  71.     if(U_FAILURE(*status)) return;
  72.   }
  73.  
  74.   s->fLength = len;
  75.   s->fChars[len] = 0x0000;
  76. }
  77.  
  78. void
  79. ustr_cat(struct UString *dst, 
  80.      const struct UString *src, 
  81.      UErrorCode *status)
  82. {
  83.   ustr_ncat(dst, src, src->fLength, status);
  84. }
  85.  
  86. void
  87. ustr_ncat(struct UString *dst, 
  88.       const struct UString *src, 
  89.       int32_t n, 
  90.       UErrorCode *status)
  91. {
  92.   if(U_FAILURE(*status) || dst == src) return;
  93.   
  94.   if(dst->fCapacity < (dst->fLength + n)) {
  95.     ustr_resize(dst, ALLOCATION(dst->fLength + n), status);
  96.     if(U_FAILURE(*status)) return;
  97.   }
  98.   
  99.   icu_memcpy(dst->fChars + dst->fLength, src->fChars, 
  100.          sizeof(UChar) * n);
  101.   dst->fLength += src->fLength;
  102.   dst->fChars[dst->fLength] = 0x0000;
  103. }
  104.  
  105. void
  106. ustr_ucat(struct UString *dst, 
  107.       UChar c, 
  108.       UErrorCode *status)
  109. {
  110.   if(U_FAILURE(*status)) return;
  111.  
  112.   if(dst->fCapacity < (dst->fLength + 1)) {
  113.     ustr_resize(dst, ALLOCATION(dst->fLength + 1), status);
  114.     if(U_FAILURE(*status)) return;
  115.   }
  116.   
  117.   icu_memcpy(dst->fChars + dst->fLength, &c, 
  118.          sizeof(UChar) * 1);
  119.   dst->fLength += 1;
  120.   dst->fChars[dst->fLength] = 0x0000;
  121. }
  122.  
  123. /* Destroys data in the string */
  124. static void
  125. ustr_resize(struct UString *s, 
  126.         int32_t len, 
  127.         UErrorCode *status)
  128. {
  129.   if(U_FAILURE(*status)) return;
  130.  
  131.   /* +1 for trailing 0x0000 */
  132.   s->fChars = (UChar*) icu_realloc(s->fChars, sizeof(UChar) * (len + 1));
  133.   if(s->fChars == 0) {
  134.     *status = U_MEMORY_ALLOCATION_ERROR;
  135.     s->fChars = 0;
  136.     s->fLength = s->fCapacity = 0;
  137.     return;
  138.   }
  139.  
  140.   s->fCapacity = len;
  141. }
  142.