Back to index

lightning-sunbird  0.9+nobinonly
ImportCharSet.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef ImportCharSet_h___
00039 #define ImportCharSet_h___
00040 
00041 #include "nscore.h"
00042 
00043 
00044 // Some useful ASCII values
00045 //     'A' = 65, 0x41
00046 //     'Z' = 90, 0x5a
00047 //     '_' = 95, 0x5f
00048 //     'a' = 97, 0x61
00049 //     'z' = 122, 0x7a
00050 //     '0' = 48, 0x30
00051 //     '1' = 49, 0x31
00052 //     '9' = 57, 0x39
00053 //     ' ' = 32, 0x20
00054 //     whitespace, 10, 13, 32, 9 (linefeed, cr, space, tab) - 0x0a, 0x0d, 0x20, 0x09
00055 //     ':' = 58, 0x3a
00056 
00057 
00058 // a typedef enum would be nicer but some compilers still have trouble with treating
00059 // enum's as plain numbers when needed
00060 
00061 class ImportCharSet {
00062 public:
00063        enum {
00064               cTabChar = 9,
00065               cLinefeedChar = 10,
00066               cCRChar = 13,
00067               cSpaceChar = 32,
00068               cUpperAChar = 65,
00069               cUpperZChar = 90,
00070               cUnderscoreChar = 95,
00071               cLowerAChar = 97,
00072               cLowerZChar = 122,
00073               cZeroChar = 48,
00074               cNineChar = 57,
00075 
00076               cAlphaNumChar = 1,
00077               cAlphaChar = 2,
00078               cWhiteSpaceChar = 4,
00079               cDigitChar = 8,
00080               c822SpecialChar = 16
00081        };
00082 
00083        static char                 m_upperCaseMap[256];
00084        static char                 m_Ascii[256];
00085 
00086        inline static PRBool IsUSAscii( PRUint8 ch) { return( ((ch & (PRUint8)0x80) == 0));}
00087        inline static PRBool Is822CtlChar( PRUint8 ch) { return( (ch < 32));}
00088        inline static PRBool Is822SpecialChar( PRUint8 ch) { return( (m_Ascii[ch] & c822SpecialChar) == c822SpecialChar);}
00089        inline static PRBool IsWhiteSpace( PRUint8 ch) { return( (m_Ascii[ch] & cWhiteSpaceChar) == cWhiteSpaceChar); }
00090        inline static PRBool IsAlphaNum( PRUint8 ch) { return( (m_Ascii[ch] & cAlphaNumChar) == cAlphaNumChar); }
00091        inline static PRBool IsDigit( PRUint8 ch) { return( (m_Ascii[ch] & cDigitChar) == cDigitChar); }
00092 
00093        inline static PRUint8 ToLower( PRUint8 ch) { if ((m_Ascii[ch] & cAlphaChar) == cAlphaChar) { return( cLowerAChar + (m_upperCaseMap[ch] - cUpperAChar)); } else return( ch); }
00094 
00095        inline static long AsciiToLong( const PRUint8 * pChar, PRUint32 len) {
00096               long num = 0;
00097               while (len) {
00098                      if ((m_Ascii[*pChar] & cDigitChar) == 0)
00099                             return( num);
00100                      num *= 10;
00101                      num += (*pChar - cZeroChar);
00102                      len--;
00103                      pChar++;
00104               }
00105               return( num);
00106        }
00107 
00108        inline static void ByteToHex( PRUint8 byte, PRUint8 * pHex) {
00109               PRUint8 val = byte;
00110               val /= 16;
00111               if (val < 10)
00112                      *pHex = '0' + val;
00113               else
00114                      *pHex = 'A' + (val - 10);
00115               pHex++;
00116               val = byte;
00117               val &= 0x0F;
00118               if (val < 10)
00119                      *pHex = '0' + val;
00120               else
00121                      *pHex = 'A' + (val - 10);          
00122        }
00123 
00124        inline static void   LongToHexBytes( PRUint32 type, PRUint8 * pStr) {
00125               ByteToHex( (PRUint8) (type >> 24), pStr);
00126               pStr += 2;
00127               ByteToHex( (PRUint8) ((type >> 16) & 0x0FF), pStr);
00128               pStr += 2;
00129               ByteToHex( (PRUint8) ((type >> 8) & 0x0FF), pStr);
00130               pStr += 2;
00131               ByteToHex( (PRUint8) (type & 0x0FF), pStr);
00132        }
00133 
00134        inline static void SkipWhiteSpace( const PRUint8 * & pChar, PRUint32 & pos, PRUint32 max) {
00135               while ((pos < max) && (IsWhiteSpace( *pChar))) {
00136                      pos++; pChar++;
00137               }
00138        }
00139 
00140        inline static void SkipSpaceTab( const PRUint8 * & pChar, PRUint32& pos, PRUint32 max) {
00141               while ((pos < max) && ((*pChar == (PRUint8)cSpaceChar) || (*pChar == (PRUint8)cTabChar))) {
00142                      pos++; pChar++;
00143               }
00144        }
00145 
00146        inline static void SkipTilSpaceTab( const PRUint8 * & pChar, PRUint32& pos, PRUint32 max) {
00147               while ((pos < max) && (*pChar != (PRUint8)cSpaceChar) && (*pChar != (PRUint8)cTabChar)) {
00148                      pos++;
00149                      pChar++;
00150               }
00151        }
00152 
00153        inline static PRBool StrNICmp( const PRUint8 * pChar, const PRUint8 * pSrc, PRUint32 len) {
00154               while (len && (m_upperCaseMap[*pChar] == m_upperCaseMap[*pSrc])) {
00155                      pChar++; pSrc++; len--;
00156               }
00157               return( len == 0);
00158        }
00159 
00160        inline static PRBool StrNCmp( const PRUint8 * pChar, const PRUint8 *pSrc, PRUint32 len) {
00161               while (len && (*pChar == *pSrc)) {
00162                      pChar++; pSrc++; len--;
00163               }
00164               return( len == 0);
00165        }
00166 
00167        inline static int FindChar( const PRUint8 * pChar, PRUint8 ch, PRUint32 max) {
00168               PRUint32             pos = 0;
00169               while ((pos < max) && (*pChar != ch)) {
00170                      pos++; pChar++;
00171               }
00172               if (pos < max)
00173                      return( (int) pos);
00174               else
00175                      return( -1);
00176        }
00177 
00178        inline static PRBool NextChar( const PRUint8 * & pChar, PRUint8 ch, PRUint32& pos, PRUint32 max) {
00179               if ((pos < max) && (*pChar == ch)) {
00180                      pos++;
00181                      pChar++;
00182                      return( PR_TRUE);
00183               }
00184               return( PR_FALSE);
00185        }
00186 
00187        inline static PRInt32 strcmp( const char * pS1, const char * pS2) {
00188               while (*pS1 && *pS2 && (*pS1 == *pS2)) {
00189                      pS1++;
00190                      pS2++;
00191               }
00192               return( *pS1 - *pS2);
00193        }
00194 
00195        inline static PRInt32 stricmp( const char * pS1, const char * pS2) {
00196               while (*pS1 && *pS2 && (m_upperCaseMap[*pS1] == m_upperCaseMap[*pS2])) {
00197                      pS1++;
00198                      pS2++;
00199               }
00200               return( m_upperCaseMap[*pS1] - m_upperCaseMap[*pS2]);
00201        }
00202 
00203 };
00204 
00205 
00206 #endif /* ImportCharSet_h__ */
00207