string_ex.c
上传用户:super_houu
上传日期:2008-09-21
资源大小:4099k
文件大小:23k
- /* **************************************************************************************
- * Copyright (c) 2000 ZORAN Corporation, All Rights Reserved
- * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF ZORAN CORPORATION
- *
- * File: "string_ex.c" 4/12/01
- *
- * Description:
- * ============
- * Special (non-ANSI) string operations
- *
- * Log:
- * ====
- * $Revision: 2 $
- * Last Modified by $Author: Leslie $ at $Modtime: 03-01-07 16:14 $
- ****************************************************************************************
- * Updates:
- ****************************************************************************************
- * $Log: /SourceCode/I64_Common/I64_Reference/Services/string_ex.c $
- *
- * 2 03-01-10 12:25 Leslie
- * Added _wcsnicmp(), _wcs2str(), _str2wcs()
- *
- * 7 23/04/02 9:38 Nirm
- * - Added dependency in "Config.h".
- *
- * 6 4/10/02 4:59a Tomasp
- * Added C locale table.
- *
- * 5 4/10/02 4:41a Tomasp
- * Added function for replacing unprintable characters.
- *
- * 2 1/13/02 18:18 Ettim
- * Moved the _isSimilarStrings() function to this file
- *
- * 1 12/26/01 18:54 Ettim
- * After updating nav_svcd.c & pe_cd.c
- *
- * 3 17/04/01 15:02 Nirm
- * Fixed a bug in _strnicmp().
- *
- * 2 15/04/01 10:38 Nirm
- * Fixed a bug in _strnicmp().
- *
- * 1 12/04/01 18:45 Nirm
- * Added string_ex_API.
- **************************************************************************************** */
- #include "Config.h" // Global Configuration - do not remove!
- //Leslie_0521_2004_C: Following lines will block the release code comilation. We need them for both Debug and Release Projects
- //#if defined(_DEBUG) || defined(D_RELEASE_TRACE_ENABLED) // ZKR GL051004
- #include "IncludeSysDefs.h"
- #include "Includemath-macro.h"
- #include "Includestring_ex.h"
- /*
- * The "C" locale ctype array
- */
- CONST unsigned char CharType[ 257 ] =
- {
- 0x00, /* 0xffff (-1) EOF */
- _IS_CTL, /* 0x00 */
- _IS_CTL, /* 0x01 */
- _IS_CTL, /* 0x02 */
- _IS_CTL, /* 0x03 */
- _IS_CTL, /* 0x04 */
- _IS_CTL, /* 0x05 */
- _IS_CTL, /* 0x06 */
- _IS_CTL, /* 0x07 */
- _IS_CTL, /* 0x08 */
- _IS_SP | _IS_CTL, /* 0x09 */
- _IS_SP | _IS_CTL, /* 0x0a */
- _IS_SP | _IS_CTL, /* 0x0b */
- _IS_SP | _IS_CTL, /* 0x0c */
- _IS_SP | _IS_CTL, /* 0x0d */
- _IS_CTL, /* 0x0e */
- _IS_CTL, /* 0x0f */
- _IS_CTL, /* 0x10 */
- _IS_CTL, /* 0x11 */
- _IS_CTL, /* 0x12 */
- _IS_CTL, /* 0x13 */
- _IS_CTL, /* 0x14 */
- _IS_CTL, /* 0x15 */
- _IS_CTL, /* 0x16 */
- _IS_CTL, /* 0x17 */
- _IS_CTL, /* 0x18 */
- _IS_CTL, /* 0x19 */
- _IS_CTL, /* 0x1a */
- _IS_CTL, /* 0x1b */
- _IS_CTL, /* 0x1c */
- _IS_CTL, /* 0x1d */
- _IS_CTL, /* 0x1e */
- _IS_CTL, /* 0x1f */
- _IS_SP | _IS_BLK, /* 0x20 */
- _IS_PUN, /* 0x21 */
- _IS_PUN, /* 0x22 */
- _IS_PUN, /* 0x23 */
- _IS_PUN, /* 0x24 */
- _IS_PUN, /* 0x25 */
- _IS_PUN, /* 0x26 */
- _IS_PUN, /* 0x27 */
- _IS_PUN, /* 0x28 */
- _IS_PUN, /* 0x29 */
- _IS_PUN, /* 0x2a */
- _IS_PUN, /* 0x2b */
- _IS_PUN, /* 0x2c */
- _IS_PUN, /* 0x2d */
- _IS_PUN, /* 0x2e */
- _IS_PUN, /* 0x2f */
- _IS_DIG | _IS_HEX, /* 0x30 */
- _IS_DIG | _IS_HEX, /* 0x31 */
- _IS_DIG | _IS_HEX, /* 0x32 */
- _IS_DIG | _IS_HEX, /* 0x33 */
- _IS_DIG | _IS_HEX, /* 0x34 */
- _IS_DIG | _IS_HEX, /* 0x35 */
- _IS_DIG | _IS_HEX, /* 0x36 */
- _IS_DIG | _IS_HEX, /* 0x37 */
- _IS_DIG | _IS_HEX, /* 0x38 */
- _IS_DIG | _IS_HEX, /* 0x39 */
- _IS_PUN, /* 0x3a */
- _IS_PUN, /* 0x3b */
- _IS_PUN, /* 0x3c */
- _IS_PUN, /* 0x3d */
- _IS_PUN, /* 0x3e */
- _IS_PUN, /* 0x3f */
- _IS_PUN, /* 0x40 */
- _IS_UPP | _IS_HEX, /* 0x41 */
- _IS_UPP | _IS_HEX, /* 0x42 */
- _IS_UPP | _IS_HEX, /* 0x43 */
- _IS_UPP | _IS_HEX, /* 0x44 */
- _IS_UPP | _IS_HEX, /* 0x45 */
- _IS_UPP | _IS_HEX, /* 0x46 */
- _IS_UPP, /* 0x47 */
- _IS_UPP, /* 0x48 */
- _IS_UPP, /* 0x49 */
- _IS_UPP, /* 0x4a */
- _IS_UPP, /* 0x4b */
- _IS_UPP, /* 0x4c */
- _IS_UPP, /* 0x4d */
- _IS_UPP, /* 0x4e */
- _IS_UPP, /* 0x4f */
- _IS_UPP, /* 0x50 */
- _IS_UPP, /* 0x51 */
- _IS_UPP, /* 0x52 */
- _IS_UPP, /* 0x53 */
- _IS_UPP, /* 0x54 */
- _IS_UPP, /* 0x55 */
- _IS_UPP, /* 0x56 */
- _IS_UPP, /* 0x57 */
- _IS_UPP, /* 0x58 */
- _IS_UPP, /* 0x59 */
- _IS_UPP, /* 0x5a */
- _IS_PUN, /* 0x5b */
- _IS_PUN, /* 0x5c */
- _IS_PUN, /* 0x5d */
- _IS_PUN, /* 0x5e */
- _IS_PUN, /* 0x5f */
- _IS_PUN, /* 0x60 */
- _IS_LOW | _IS_HEX, /* 0x61 */
- _IS_LOW | _IS_HEX, /* 0x62 */
- _IS_LOW | _IS_HEX, /* 0x63 */
- _IS_LOW | _IS_HEX, /* 0x64 */
- _IS_LOW | _IS_HEX, /* 0x65 */
- _IS_LOW | _IS_HEX, /* 0x66 */
- _IS_LOW, /* 0x67 */
- _IS_LOW, /* 0x68 */
- _IS_LOW, /* 0x69 */
- _IS_LOW, /* 0x6a */
- _IS_LOW, /* 0x6b */
- _IS_LOW, /* 0x6c */
- _IS_LOW, /* 0x6d */
- _IS_LOW, /* 0x6e */
- _IS_LOW, /* 0x6f */
- _IS_LOW, /* 0x70 */
- _IS_LOW, /* 0x71 */
- _IS_LOW, /* 0x72 */
- _IS_LOW, /* 0x73 */
- _IS_LOW, /* 0x74 */
- _IS_LOW, /* 0x75 */
- _IS_LOW, /* 0x76 */
- _IS_LOW, /* 0x77 */
- _IS_LOW, /* 0x78 */
- _IS_LOW, /* 0x79 */
- _IS_LOW, /* 0x7a */
- _IS_PUN, /* 0x7b */
- _IS_PUN, /* 0x7c */
- _IS_PUN, /* 0x7d */
- _IS_PUN, /* 0x7e */
- _IS_CTL, /* 0x7f */
- 0x00, /* 0x80 */
- 0x00, /* 0x81 */
- 0x00, /* 0x82 */
- 0x00, /* 0x83 */
- 0x00, /* 0x84 */
- 0x00, /* 0x85 */
- 0x00, /* 0x86 */
- 0x00, /* 0x87 */
- 0x00, /* 0x88 */
- 0x00, /* 0x89 */
- 0x00, /* 0x8a */
- 0x00, /* 0x8b */
- 0x00, /* 0x8c */
- 0x00, /* 0x8d */
- 0x00, /* 0x8e */
- 0x00, /* 0x8f */
- 0x00, /* 0x90 */
- 0x00, /* 0x91 */
- 0x00, /* 0x92 */
- 0x00, /* 0x93 */
- 0x00, /* 0x94 */
- 0x00, /* 0x95 */
- 0x00, /* 0x96 */
- 0x00, /* 0x97 */
- 0x00, /* 0x98 */
- 0x00, /* 0x99 */
- 0x00, /* 0x9a */
- 0x00, /* 0x9b */
- 0x00, /* 0x9c */
- 0x00, /* 0x9d */
- 0x00, /* 0x9e */
- 0x00, /* 0x9f */
- 0x00, /* 0xa0 */
- 0x00, /* 0xa1 */
- 0x00, /* 0xa2 */
- 0x00, /* 0xa3 */
- 0x00, /* 0xa4 */
- 0x00, /* 0xa5 */
- 0x00, /* 0xa6 */
- 0x00, /* 0xa7 */
- 0x00, /* 0xa8 */
- 0x00, /* 0xa9 */
- 0x00, /* 0xaa */
- 0x00, /* 0xab */
- 0x00, /* 0xac */
- 0x00, /* 0xad */
- 0x00, /* 0xae */
- 0x00, /* 0xaf */
- 0x00, /* 0xb0 */
- 0x00, /* 0xb1 */
- 0x00, /* 0xb2 */
- 0x00, /* 0xb3 */
- 0x00, /* 0xb4 */
- 0x00, /* 0xb5 */
- 0x00, /* 0xb6 */
- 0x00, /* 0xb7 */
- 0x00, /* 0xb8 */
- 0x00, /* 0xb9 */
- 0x00, /* 0xba */
- 0x00, /* 0xbb */
- 0x00, /* 0xbc */
- 0x00, /* 0xbd */
- 0x00, /* 0xbe */
- 0x00, /* 0xbf */
- 0x00, /* 0xc0 */
- 0x00, /* 0xc1 */
- 0x00, /* 0xc2 */
- 0x00, /* 0xc3 */
- 0x00, /* 0xc4 */
- 0x00, /* 0xc5 */
- 0x00, /* 0xc6 */
- 0x00, /* 0xc7 */
- 0x00, /* 0xc8 */
- 0x00, /* 0xc9 */
- 0x00, /* 0xca */
- 0x00, /* 0xcb */
- 0x00, /* 0xcc */
- 0x00, /* 0xcd */
- 0x00, /* 0xce */
- 0x00, /* 0xcf */
- 0x00, /* 0xd0 */
- 0x00, /* 0xd1 */
- 0x00, /* 0xd2 */
- 0x00, /* 0xd3 */
- 0x00, /* 0xd4 */
- 0x00, /* 0xd5 */
- 0x00, /* 0xd6 */
- 0x00, /* 0xd7 */
- 0x00, /* 0xd8 */
- 0x00, /* 0xd9 */
- 0x00, /* 0xda */
- 0x00, /* 0xdb */
- 0x00, /* 0xdc */
- 0x00, /* 0xdd */
- 0x00, /* 0xde */
- 0x00, /* 0xdf */
- 0x00, /* 0xe0 */
- 0x00, /* 0xe1 */
- 0x00, /* 0xe2 */
- 0x00, /* 0xe3 */
- 0x00, /* 0xe4 */
- 0x00, /* 0xe5 */
- 0x00, /* 0xe6 */
- 0x00, /* 0xe7 */
- 0x00, /* 0xe8 */
- 0x00, /* 0xe9 */
- 0x00, /* 0xea */
- 0x00, /* 0xeb */
- 0x00, /* 0xec */
- 0x00, /* 0xed */
- 0x00, /* 0xee */
- 0x00, /* 0xef */
- 0x00, /* 0xf0 */
- 0x00, /* 0xf1 */
- 0x00, /* 0xf2 */
- 0x00, /* 0xf3 */
- 0x00, /* 0xf4 */
- 0x00, /* 0xf5 */
- 0x00, /* 0xf6 */
- 0x00, /* 0xf7 */
- 0x00, /* 0xf8 */
- 0x00, /* 0xf9 */
- 0x00, /* 0xfa */
- 0x00, /* 0xfb */
- 0x00, /* 0xfc */
- 0x00, /* 0xfd */
- 0x00, /* 0xfe */
- 0x00 /* 0xff */
- };
- /////////////////////////////////////////////////////////////////////////////
- // _strnicmp()
- //
- // Description: Compares two single-byte strings in a case-insensitive fashion.
- //
- // Input: string1 - Points to a constant NULL-terminated string
- // string2 - Points to a constant NULL-terminated string
- // count - The maximal number of characters to compare
- // Output: None
- // In/Out: None
- // Return: Zero if the two strings match;
- // Negative if string1 < string2
- // Positive if string1 > string2
- //
- // Remarks:
- // The comparison is done lexicographically.
- int _strnicmp(const char *string1, const char *string2, int count)
- {
- unsigned int uPos= 0;
- int iAllowedDifference= ('A' - 'a');
- int iResult= 0;
- while ((uPos < count) && (0 != string1[uPos]) && (0 != string2[uPos])) {
- iResult= (string1[uPos] - string2[uPos]);
- if ((0 != iResult) && (iAllowedDifference != iResult) && (iAllowedDifference != -iResult))
- break;
- else
- iResult= 0;
- uPos++;
- }
- // Check if a match was found
- if ((0 == iResult) && (uPos != count)) {
- // The strings match, but are of different length - no real match
- if ((0 == string1[uPos]) && (0 != string2[uPos]))
- iResult= -1;
- else if ((0 == string2[uPos]) && (0 != string1[uPos]))
- iResult= 1;
- }
- return iResult;
- }
- /////////////////////////////////////////////////////////////////////////////
- // _wcsnicmp()
- //
- // Description: Compares two wide-character strings in a case-insensitive fashion.
- //
- // Input: wstring1 - Points to a constant NULL-terminated string
- // wstring2 - Points to a constant NULL-terminated string
- // count - The maximal number of characters to compare
- // Output: None
- // In/Out: None
- // Return: Zero if the two strings match;
- // Negative if string1 < string2
- // Positive if string1 > string2
- //
- // Remarks:
- // The comparison is done lexicographically.
- int _wcsnicmp(const wchar_t *wstring1, const wchar_t *wstring2, size_t count)
- {
- unsigned int uPos= 0;
- int iAllowedDifference= ('A' - 'a');
- int iResult= 0;
- while ((uPos < count) && (0 != wstring1[uPos]) && (0 != wstring2[uPos])) {
- iResult= (wstring1[uPos] - wstring2[uPos]);
- // MikeX_0203_2005: check the string must be character.
- if ((0 != iResult) && (((iAllowedDifference != iResult) && (iAllowedDifference != -iResult)) ||
- (((*wstring1<'a')||(*wstring1>'z'))&&((*wstring1<'A')||(*wstring1>'Z')))
- || (((*wstring2<'a')||(*wstring2>'z'))&&((*wstring2<'A')||(*wstring2>'Z')))) )
- break;
- else
- iResult= 0;
- uPos++;
- }
- // Check if a match was found
- if ((0 == iResult) && (uPos != count)) {
- // The strings match, but are of different length - no real match
- if ((0 == wstring1[uPos]) && (0 != wstring2[uPos]))
- iResult= -1;
- else if ((0 == wstring2[uPos]) && (0 != wstring1[uPos]))
- iResult= 1;
- }
- return iResult;
- }
- /////////////////////////////////////////////////////////////////////////////
- // _str2wcs()
- //
- // Description: Converts a single-byte string into a wide-character string.
- //
- // Input: pszSource - Points to the single-byte string to convert
- // cbDestSize - The size of the destination buffer, in bytes
- // Output: pszDest - Points to a wide-character buffer to receive the result
- // In/Out: None
- // Return: The destination buffer.
- //
- // Remarks:
- // - The source string and destination buffer must NOT point to the same
- // physical memory.
- // - The source string is assumed to be ANSI-ASCII, and is converted
- // into UNICODE CS-0.
- // - The destination buffer must be at least twice the size of the
- // source-string, in order to accomodate for the entire result.
- // - If the destination buffer is smaller than required, then only the
- // portion of the source-string that fits into the buffer will be
- // produced.
- // - The result is NULL-terminated.
- LPWSTR _str2wcs(LPCSTR pszSource, LPWSTR pszDest, unsigned int cbDestSize)
- {
- unsigned int uPos;
- // Copy the source string to the destination, one character at a time
- for (uPos=0; uPos < ((cbDestSize / sizeof(WCHAR)) - 1); uPos++) {
- if (0 == pszSource[uPos])
- break;
- pszDest[uPos]= (WCHAR)pszSource[uPos];
- }
- // NULL-terminate the destination
- pszDest[uPos]= 0;
- return pszDest;
- }
- /////////////////////////////////////////////////////////////////////////////
- // _wcs2str()
- //
- // Description: Converts a wide-character string into a single-byte string.
- //
- // Input: pszSource - Points to the wide-character string to convert
- // cbDestSize - The size of the destination buffer, in bytes
- // Output: pszDest - Points to a character buffer to receive the result
- // In/Out: None
- // Return: The destination buffer.
- //
- // Remarks:
- // - The source string and destination buffer must may point to the same
- // physical memory.
- // - The source string is assumed to be UNICODE CS-0, and is converted
- // into ANSI-ASCII.
- // - The destination buffer must be at least half the size of the
- // source-string (in bytes), in order to accomodate for the entire result.
- // - If the destination buffer is smaller than required, then only the
- // portion of the source-string that fits into the buffer will be
- // produced.
- // - The result is NULL-terminated.
- LPSTR _wcs2str(LPCWSTR pszSource, LPSTR pszDest, unsigned int cbDestSize)
- {
- unsigned int uPos;
- // Convert each wide-character into a single-byte by taking the LS Byte
- for (uPos=0; uPos < (cbDestSize - 1); uPos++) {
- if (0 == pszSource[uPos])
- break;
- pszDest[uPos]= (char)(pszSource[uPos] & 0xFF);
- }
- // NULL-terminate the destination
- pszDest[uPos]= 0;
- return pszDest;
- }
- /////////////////////////////////////////////////////////////////////////////
- // _isSimilarStrings()
- //
- // Description: Compares a string against a reference-string, to determine
- // their similarity.
- //
- // Input: pszString - Points to a constant NULL-terminated string to test
- // pszRefString - Points to a constant NULL-terminated string, which
- // is the reference to test against.
- // Output: None
- // In/Out: None
- // Return: TRUE if the supplied string is similar to the reference-string;
- // FALSE otherwise.
- //
- // Remarks:
- // - The comparison is case-sensitive;
- // - Similarity is determined by a predefined tolerance.
- // - Two strings are considered similar if they have at least
- // SIMILARITY_TOLERANCE consecutive characters which are identical.
- #define SIMILARITY_TOLERANCE 3
- BOOL _isSimilarStrings(LPCSTR pszString, LPCSTR pszRefString)
- {
- UINT16 uRefLength= strlen(pszRefString);
- UINT16 uCurrPos;
- LPCSTR pszStrPos= pszString;
- // First of all, try a perfect match
- if (0 == strncmp(pszString, pszRefString, SIMILARITY_TOLERANCE))
- return TRUE;
- // Try partial matching (similarity only)
- for (uCurrPos=0; uCurrPos < (uRefLength - SIMILARITY_TOLERANCE + 1); uCurrPos++) {
- if (0 == strncmp(pszStrPos++, pszRefString++, SIMILARITY_TOLERANCE))
- return TRUE;
- }
- return FALSE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // convertStringToPrintable ()
- //
- // Description: function replaces all non printable characters in string
- // with character defined by NON_PRINTABLE_CHARACTER macro
- //
- // Input: None
- // Output: None
- // In/Out: pszString - pointer to NULL-terminated string
- // Return: None
- // Remarks:
- void convertStringToPrintable (LPSTR pszString)
- {
- LPSTR pszName;
- for (pszName = pszString;*pszName;pszName++)
- {
- if (!IsPrint(*((unsigned char*)pszName)))
- *pszName = NON_PRINTABLE_CHARACTER;
- }
- }
- //Leslie_0521_2004_C: Comment Out
- //#endif // #if defined(_DEBUG) || defined(D_RELEASE_TRACE_ENABLED) // ZKR GL051004