home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libi18n / nscstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.6 KB  |  172 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "intlpriv.h"
  20. #include "libi18n.h"
  21.  
  22. /*     Private Function    */
  23. PRIVATE void                
  24. INTL_CompoundStrAddSeg_p(
  25.     INTL_CompoundStr* This, 
  26.     INTL_Encoding_ID inencoding, 
  27.     unsigned char* intext);
  28. PRIVATE INTL_CompoundStr*     INTL_CompoundStrNewSeg_p(INTL_Encoding_ID inencoding, unsigned char* intext);
  29.  
  30.  
  31. /*    
  32.     Not Support Function - Too Complex to Implement!!! Believe it or not.
  33.     INTL_CompoundStrComp    : We currently dont implement this funciton. If we decide to implement it. We should use the following algorithm
  34.     (1) Compare each segment. If all match, return 0.
  35.     (2) Convert both string to Unicode and them compare. If it equal in Unicode, return 0
  36.     (3) If they are not the same in Unicode, we have to pick one sorting sequence to decide wheather it should return 1 or -1.
  37.         The problem is which sorting order should we follow ? We need to decide :
  38.             (a) the sorting order in one script,
  39.             (b) the sorting order between scripts
  40. */
  41.  
  42. /*    Implementation */
  43. PUBLIC INTL_CompoundStr*     INTL_CompoundStrFromStr(INTL_Encoding_ID inencoding, unsigned char* intext)
  44. {
  45.     return INTL_CompoundStrNewSeg_p(inencoding, intext);
  46. }
  47.  
  48. #define TMPLEN    256
  49. PUBLIC INTL_CompoundStr*
  50. INTL_CompoundStrFromUnicode(INTL_Unicode* inunicode, uint32 inunicodelen)
  51. {    
  52.     INTL_Encoding_ID    encoding;
  53.     unsigned char TMP[TMPLEN];    
  54.     INTL_UnicodeToStrIterator iterator;
  55.     if((iterator = INTL_UnicodeToStrIteratorCreate(inunicode, 
  56.                     inunicodelen,
  57.                     &encoding, 
  58.                     &TMP[0], TMPLEN))!=NULL)
  59.     {
  60.         INTL_CompoundStr *This;
  61.         This = INTL_CompoundStrNewSeg_p(encoding, &TMP[0]);
  62.         if(This != NULL)
  63.         {
  64.             while(INTL_UnicodeToStrIterate(iterator, &encoding, &TMP[0], TMPLEN))
  65.                 INTL_CompoundStrAddSeg_p(This, encoding, &TMP[0]);
  66.         }
  67.         INTL_UnicodeToStrIteratorDestroy(iterator);
  68.         return This;
  69.     }
  70.     return NULL;
  71. }
  72.  
  73. PUBLIC INTL_CompoundStrIterator 
  74. INTL_CompoundStrFirstStr(INTL_CompoundStr* This, INTL_Encoding_ID *outencoding, unsigned char** outtext)
  75. {
  76.     if(This == NULL)
  77.         return NULL;
  78.     else
  79.     {
  80.         *outencoding = This->encoding;
  81.         *outtext = This->text;
  82.         return ((INTL_CompoundStrIterator)This);
  83.     }
  84. }
  85. PUBLIC INTL_CompoundStrIterator 
  86. INTL_CompoundStrNextStr(INTL_CompoundStrIterator iterator, INTL_Encoding_ID *outencoding, unsigned char** outtext)
  87. {
  88.     INTL_CompoundStr* This = (INTL_CompoundStr*)iterator;
  89.     return INTL_CompoundStrFirstStr(
  90.             This->next, 
  91.             outencoding, outtext);
  92. }
  93.  
  94. PUBLIC void             
  95. INTL_CompoundStrDestroy(INTL_CompoundStr* This)
  96. {
  97.     INTL_CompoundStr* Next;
  98.     for(; (This != NULL); This=Next)
  99.     {
  100.         if(This->next)
  101.             Next=This->next;
  102.         else
  103.             Next=NULL;
  104.         XP_FREE(This->text);
  105.         XP_FREE(This);    
  106.     }
  107. }
  108.  
  109. PUBLIC INTL_CompoundStr*     
  110. INTL_CompoundStrClone(INTL_CompoundStr* s2)
  111. {
  112.     if(s2 != NULL)
  113.     {
  114.         INTL_CompoundStr* This;    
  115.         This=INTL_CompoundStrNewSeg_p(s2->encoding, s2->text);
  116.         This->next=INTL_CompoundStrClone(s2->next);  
  117.         return This;
  118.     }
  119.     return NULL;
  120. }    
  121.  
  122.  
  123. PRIVATE
  124. void
  125. INTL_CompoundStrCat_p(INTL_CompoundStr* s1, INTL_CompoundStr* s2)
  126. {
  127.     if(s1->next != NULL)
  128.         INTL_CompoundStrCat_p(s1->next, s2);
  129.     else
  130.         s1->next = INTL_CompoundStrClone(s2);
  131. }
  132. PUBLIC void
  133. INTL_CompoundStrCat(INTL_CompoundStr* s1, INTL_CompoundStr* s2)
  134. {
  135.     if((s2 != NULL) && (s2->text[0])) 
  136.         INTL_CompoundStrCat_p(s1, s2);
  137. }
  138.  
  139. PRIVATE
  140. INTL_CompoundStr*     
  141. INTL_CompoundStrNewSeg_p(INTL_Encoding_ID inencoding, unsigned char* intext)
  142. {
  143.     INTL_CompoundStr *This;
  144.     
  145.     This = XP_ALLOC(sizeof(INTL_CompoundStr));
  146.     if(This != NULL)
  147.     {
  148.         char *p_text=0;
  149.         StrAllocCopy(p_text, (char*)intext);
  150.         This->text = (unsigned char*)p_text;
  151.         
  152.         This->next = NULL;
  153.         This->encoding = inencoding;
  154.     }
  155.     return This;
  156. }
  157.  
  158. PRIVATE 
  159. void
  160. INTL_CompoundStrAddSeg_p(
  161.     INTL_CompoundStr* This, 
  162.     INTL_Encoding_ID inencoding, 
  163.     unsigned char* intext)
  164. {
  165.     if(*intext)
  166.     {
  167.         for(;This->next;This=This->next)
  168.             ;
  169.         This->next = INTL_CompoundStrNewSeg_p(inencoding,intext);
  170.     }
  171. }
  172.