xml/cxmllibrary/src/utils/src/cxml_str.c
changeset 27 450972dee096
equal deleted inserted replaced
23:740e860b8acf 27:450972dee096
       
     1 /*
       
     2 * Copyright (c) 2003 - 2004 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /***************************************************************************
       
    20 **   File: cxml_str.c
       
    21 **   Purpose:  Provides the implementation to CXML string API
       
    22 **************************************************************************/
       
    23 
       
    24 /*
       
    25 **-------------------------------------------------------------------------
       
    26 **  Include Files
       
    27 **-------------------------------------------------------------------------
       
    28 */
       
    29 
       
    30 #include "cxml_internal.h"
       
    31 #include <xml/cxml/cxml_nw2cxmlTypes.h>
       
    32 
       
    33 
       
    34 static NW_Uint16 CXML_Str_Strlen(const CXML_Ucs2 *string)
       
    35 {
       
    36   NW_Uint16 len = 0;
       
    37   NW_ASSERT(string != NULL);
       
    38 
       
    39   while (*string != NULL) 
       
    40   {
       
    41     string++;
       
    42     len++;
       
    43   }
       
    44   return len;
       
    45 }
       
    46 
       
    47 static NW_Uint32 NW_Asc_strlen(const char *s)
       
    48 {
       
    49   NW_Uint32 i = 0;
       
    50   while (*s != '\0')
       
    51   {
       
    52     s++;
       
    53     i++;
       
    54   }
       
    55   return i;
       
    56 }
       
    57 
       
    58 /*****************************************************************
       
    59 **  Name:  CXML_Str_ToLower
       
    60 **  Description:  Converts a CXML_Ucs2 to lower case
       
    61 **  Parameters:   ch - the character to convert
       
    62 **  Return Value: the converted character
       
    63 ******************************************************************/
       
    64 CXML_Ucs2 CXML_Str_ToLower(const CXML_Ucs2 ch)
       
    65 {
       
    66   if((ch >= CXML_ASCII_UPPER_A) && (ch <= CXML_ASCII_UPPER_Z)) {
       
    67     return (CXML_Ucs2)(ch - CXML_ASCII_UPPER_A + CXML_ASCII_LOWER_A);
       
    68   } else {
       
    69     return ch;
       
    70   }
       
    71 }
       
    72 
       
    73 /*****************************************************************
       
    74 **  Name:  CXML_Str_ToUpper
       
    75 **  Description:  Converts a NW_Ucs2 to upper case
       
    76 **  Parameters:   ch - the character to convert
       
    77 **  Return Value: the converted character
       
    78 ******************************************************************/
       
    79 CXML_Ucs2 CXML_Str_ToUpper(const NW_Ucs2 ch)
       
    80 {
       
    81   if((ch >= CXML_ASCII_LOWER_A) && (ch <= CXML_ASCII_LOWER_Z)) {
       
    82     return (NW_Ucs2)(ch - CXML_ASCII_LOWER_A + CXML_ASCII_UPPER_A);
       
    83   } else {
       
    84     return ch;
       
    85   }
       
    86 }
       
    87 
       
    88 /*****************************************************************
       
    89 **  Name:  CXML_Str_Isspace
       
    90 **  Description:  Checks if the character is a particular 
       
    91 **                representation of a space character
       
    92 **  Parameters:   ch  - character to test
       
    93 **  Return Value: non-zero if c is a white-space character, else 0
       
    94 ******************************************************************/
       
    95 CXML_Int32 CXML_Str_Isspace(const CXML_Ucs2 ch)
       
    96 {
       
    97   return ((ch == CXML_ASCII_SPACE) || (ch == CXML_ASCII_CR) ||
       
    98           (ch == CXML_ASCII_LF) || (ch == CXML_ASCII_FF) ||
       
    99           (ch == CXML_ASCII_HT) || (ch == CXML_ASCII_VT));
       
   100 }
       
   101 
       
   102 
       
   103 /*****************************************************************
       
   104 **  Name:  CXML_Str_Isdigit
       
   105 **  Description:  Checks if the character is a particular 
       
   106 **                representation of a decimal-digit character.
       
   107 **  Parameters:   ch  - character to test
       
   108 **  Return Value: non-zero if c is a decimal-digit character, else 0
       
   109 ******************************************************************/
       
   110 CXML_Int32 CXML_Str_Isdigit(const CXML_Ucs2 ch)
       
   111 {
       
   112   return ( (ch >= CXML_ASCII_0) && (ch <= CXML_ASCII_9) );
       
   113 }
       
   114 
       
   115 
       
   116 /*****************************************************************
       
   117 **  Name:  CXML_Str_Isxdigit
       
   118 **  Description:  Checks if the character is a particular 
       
   119 **                representation of a hexadecimal-digit character.
       
   120 **  Parameters:   ch  - character to test
       
   121 **  Return Value: non-zero if c is a hexadecimal-digit character, else 0
       
   122 ******************************************************************/
       
   123 CXML_Int32 CXML_Str_Isxdigit(const CXML_Ucs2 ch)
       
   124 {
       
   125   return (CXML_Str_Isdigit(ch) ||
       
   126           ( (ch >= CXML_ASCII_UPPER_A) && (ch <= CXML_ASCII_UPPER_F) ) ||
       
   127           ( (ch >= CXML_ASCII_LOWER_A) && (ch <= CXML_ASCII_LOWER_F) ) );
       
   128 }
       
   129 
       
   130 /*****************************************************************
       
   131 **  Name:  CXML_Str_CvtToAscii
       
   132 **  Description:  Convert ucs2 string to new null-terminated ascii string
       
   133 **  Parameters:   *string - Null-terminated ucs2 string
       
   134 **  Return Value: pointer to the new ascii string or NULL
       
   135 ******************************************************************/
       
   136 CXML_Int8 *CXML_Str_CvtToAscii(const CXML_Ucs2 *string)
       
   137 {
       
   138   NW_Int32 i, size;
       
   139   CXML_Int8* res = NULL;
       
   140 
       
   141   NW_ASSERT(string);
       
   142 
       
   143   size = CXML_Str_Strlen(string) + 1;
       
   144   res = (CXML_Int8*) NW_Mem_Malloc((NW_Uint32)size * sizeof(CXML_Int8));
       
   145   /* this is a really naive conversion to ascii */
       
   146   if (res != NULL) {
       
   147     for (i=0; i < size; i++) {
       
   148       res[i] = (CXML_Int8)(string[i] & 0xFF);
       
   149     }
       
   150   }
       
   151   return res;
       
   152 }
       
   153 
       
   154 
       
   155 CXML_Uint32 CXML_Asc_strlen(const CXML_Int8 *s)
       
   156 {
       
   157   NW_Uint32 i = 0;
       
   158   while (*s != '\0')
       
   159   {
       
   160     s++;
       
   161     i++;
       
   162   }
       
   163   return i;
       
   164 }
       
   165 
       
   166 
       
   167 /*****************************************************************
       
   168 **  Name:  NW_Str_CvtFromAscii
       
   169 **  Description:  Convert null-terminated ascii string to new ucs2 string
       
   170 **  Parameters:   *string - Null-terminated ascii string
       
   171 **  Return Value: pointer to new ucs2 string or NULL
       
   172 ******************************************************************/
       
   173 CXML_Ucs2 *CXML_Str_CvtFromAscii(const CXML_Int8 *string)
       
   174 {
       
   175   NW_Uint32 i, size;
       
   176   NW_Ucs2* res = NULL;
       
   177 
       
   178   NW_ASSERT(string);
       
   179 
       
   180   size = NW_Asc_strlen((const char*) string) + 1;
       
   181   res = (NW_Ucs2*) NW_Mem_Malloc(size * sizeof(NW_Ucs2));
       
   182   /* this is a really naive conversion from ascii */
       
   183   if (res != NULL) {
       
   184     for (i=0; i < size; i++) {
       
   185       res[i] = string[i];
       
   186     }
       
   187   }
       
   188   return res;
       
   189 }
       
   190 
       
   191 
       
   192 /*****************************************************************
       
   193 **  Name:  CXML_Str_Strsize
       
   194 **  Description:  Get the size of the string
       
   195 **  Parameters:   *string - Null-terminated string
       
   196 **  Return Value: number of bytes in string including terminator
       
   197 ******************************************************************/
       
   198 CXML_Uint16 CXML_Str_Strsize(const CXML_Ucs2 *string)
       
   199 {
       
   200   NW_Uint16 len;
       
   201 
       
   202   NW_ASSERT(string != NULL);
       
   203 
       
   204   len = CXML_Str_Strlen(string);
       
   205   return NW_UINT16_CAST((len + 1) * sizeof(NW_Ucs2));
       
   206 }
       
   207 
       
   208 /*****************************************************************
       
   209 **  Name:  CXML_Str_StrcmpConst
       
   210 **  Description:  Compare a string to an Ascii string constant
       
   211 **  Parameters:   *string1 - Null-terminated string to compare
       
   212 **                *string2 - Null-terminated string to compare
       
   213 **  Return Value: <0 if string1 < string2, 0 if equal, >0 if string1 > string2
       
   214 ******************************************************************/
       
   215 
       
   216 CXML_Int32 CXML_Str_StrcmpConst(const CXML_Ucs2 *string1,const CXML_Ucs2 *string2)
       
   217 {
       
   218   NW_Int32 i = 0;
       
   219   NW_Int32 ch1;
       
   220   NW_Int32 ch2;
       
   221 
       
   222   NW_ASSERT(string1 != NULL);
       
   223   NW_ASSERT(string2 != NULL);
       
   224 
       
   225   do {
       
   226     ch1 = string1[i];
       
   227     ch2 = string2[i];
       
   228     i++;
       
   229   } while (((ch1 - ch2) == 0) && (ch1 != CXML_ASCII_NULL));
       
   230 
       
   231   return (ch1 - ch2);
       
   232 }
       
   233 
       
   234 /*****************************************************************
       
   235 **  Name:  CXML_Str_Stricmp
       
   236 **  Description:  Compare two strings, case-insensitve
       
   237 **  Parameters:   *string1 - Null-terminated string to compare
       
   238 **                *string2 - Null-terminated string to compare
       
   239 **  Return Value: <0 if string1 < string2, 0 if equal, >0 if string1 > string2
       
   240 ******************************************************************/
       
   241 CXML_Int32 CXML_Str_Stricmp(const CXML_Ucs2 *string1, 
       
   242                              const CXML_Ucs2 *string2)
       
   243 {
       
   244   NW_ASSERT(string1 != NULL);
       
   245   NW_ASSERT(string2 != NULL);
       
   246 
       
   247 
       
   248   while ((CXML_Str_ToUpper(*string1) == CXML_Str_ToUpper(*string2)) &&
       
   249          (*string1 != CXML_ASCII_NULL) && (*string2 != CXML_ASCII_NULL))
       
   250   {
       
   251     string1++;
       
   252     string2++;
       
   253   }
       
   254   return (CXML_Str_ToUpper(*string1) - CXML_Str_ToUpper(*string2));
       
   255 }
       
   256 
       
   257 /*****************************************************************
       
   258 **  Name:  CXML_Asc_strcpy
       
   259 **  Description:  Copy null terminated string 
       
   260 **  Parameters:   *s2 - Null-terminated string to copy
       
   261 **                *s1 - Resultant String
       
   262 **  Return Value: Copied string
       
   263 ******************************************************************/
       
   264 
       
   265 CXML_Int8 *CXML_Asc_strcpy(CXML_Int8 *s1, const CXML_Int8 *s2)
       
   266 {
       
   267   while (*s2 != '\0') {
       
   268     *s1 = *s2;
       
   269     s1++;
       
   270     s2++;
       
   271   }
       
   272   *s1 = *s2;
       
   273   return s1;
       
   274 }
       
   275 
       
   276 /*****************************************************************
       
   277 **  Name:  CXML_Asc_strcat
       
   278 **  Description:  Concatenate null terminated string 
       
   279 **  Parameters:   *s2 - Null-terminated string to copy
       
   280 **                *s1 - Resultant String Null terminated
       
   281 **  Return Value: Resultant string
       
   282 ******************************************************************/
       
   283 
       
   284 CXML_Int8 *CXML_Asc_strcat(CXML_Int8 *s1, const CXML_Int8 *s2)
       
   285 {
       
   286   CXML_Int8 *myDest = s1;
       
   287   while (*myDest != '\0') {
       
   288    myDest++;
       
   289   }
       
   290   CXML_Asc_strcpy(myDest, s2);
       
   291   return s1;
       
   292 }
       
   293 
       
   294 /*****************************************************************
       
   295 **  Name:  CXML_Asc_Strncpy
       
   296 **  Description:  Copy characters of one string to another
       
   297 **  Parameters:   *destination - Destination string
       
   298 **                *source - Source string
       
   299 **                count -  Maximum number of characters to be copied
       
   300 **  Return Value: pointer to destination string 
       
   301 ******************************************************************/
       
   302 CXML_Int8 *CXML_Asc_strncpy(CXML_Int8 *destination,
       
   303                           const CXML_Int8 *source,
       
   304                           const CXML_Uint32 count )
       
   305 {
       
   306   CXML_Uint32 nCopied = 0;
       
   307   
       
   308   NW_ASSERT(destination != NULL);
       
   309   NW_ASSERT(source != NULL);
       
   310 
       
   311   while ( (nCopied < count) && (*source != CXML_ASCII_NULL ) ) {
       
   312     *destination = *source;
       
   313     destination++;
       
   314     source++;
       
   315     nCopied++;
       
   316   }
       
   317 
       
   318   /*
       
   319   ** ANSI 7.11.2.4 says nulls are appended till count characters in all have
       
   320   ** been copied
       
   321   */
       
   322   while (nCopied < count) {
       
   323     *destination = CXML_ASCII_NULL;
       
   324     destination++;
       
   325     nCopied++;
       
   326   }
       
   327   return destination;
       
   328 }
       
   329 
       
   330 /*****************************************************************
       
   331 **  Name:  CXML_Asc_strncat
       
   332 **  Description: Concatenate characters of one string to another
       
   333 **  Parameters:   *destination - Destination string
       
   334 **                *source - Source string
       
   335 **                count -  Maximum number of characters to be copied
       
   336 **  Return Value: pointer to destination string
       
   337 ******************************************************************/
       
   338 CXML_Int8 *CXML_Asc_strncat(CXML_Int8 *destination,
       
   339                           const CXML_Int8 *source,
       
   340                           const NW_Uint32 count )
       
   341 {
       
   342   CXML_Uint32 nCount = 0;
       
   343   
       
   344   NW_ASSERT(destination != NULL);
       
   345   NW_ASSERT(source != NULL);
       
   346 
       
   347   while(*destination != '\0')
       
   348   {
       
   349    destination++;
       
   350   }
       
   351 
       
   352 
       
   353   while ( (nCount < count) && (*source != CXML_ASCII_NULL ) ) {
       
   354     *destination = *source;
       
   355     destination++;
       
   356     source++;
       
   357     nCount++;
       
   358   }
       
   359 
       
   360   /*
       
   361   ** ANSI 7.11.2.4 says nulls are appended till count characters in all have
       
   362   ** been copied
       
   363   */
       
   364   while (nCount < count) {
       
   365     *destination = CXML_ASCII_NULL;
       
   366     destination++;
       
   367     nCount++;
       
   368   }
       
   369   return destination;
       
   370 }
       
   371 
       
   372 
       
   373 
       
   374