LCOV - app.info - objdir/dist/include/nsCRT.h

LCOV - code coverage report
| Current view: | directory - objdir/dist/include - nsCRT.h (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 36 | 26 | 72.2 %
| Date: | 2012-06-02 | Functions: | 18 | 12 | 66.7 %

           1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
           2                 : /* ***** BEGIN LICENSE BLOCK *****
           3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
           4                 :  *
           5                 :  * The contents of this file are subject to the Mozilla Public License Version
           6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
           7                 :  * the License. You may obtain a copy of the License at
           8                 :  * http://www.mozilla.org/MPL/
           9                 :  *
          10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
          11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
          12                 :  * for the specific language governing rights and limitations under the
          13                 :  * License.
          14                 :  *
          15                 :  * The Original Code is mozilla.org code.
          16                 :  *
          17                 :  * The Initial Developer of the Original Code is
          18                 :  * Netscape Communications Corporation.
          19                 :  * Portions created by the Initial Developer are Copyright (C) 1998
          20                 :  * the Initial Developer. All Rights Reserved.
          21                 :  *
          22                 :  * Contributor(s):
          23                 :  *
          24                 :  * Alternatively, the contents of this file may be used under the terms of
          25                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
          26                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
          27                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
          28                 :  * of those above. If you wish to allow use of your version of this file only
          29                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
          30                 :  * use your version of this file under the terms of the MPL, indicate your
          31                 :  * decision by deleting the provisions above and replace them with the notice
          32                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
          33                 :  * the provisions above, a recipient may use your version of this file under
          34                 :  * the terms of any one of the MPL, the GPL or the LGPL.
          35                 :  *
          36                 :  * ***** END LICENSE BLOCK ***** */
          37                 : #ifndef nsCRT_h___
          38                 : #define nsCRT_h___
          39                 :
          40                 : #include <stdlib.h>
          41                 : #include <string.h>
          42                 : #include <ctype.h>
          43                 : #include "plstr.h"
          44                 : #include "nscore.h"
          45                 : #include "prtypes.h"
          46                 : #include "nsCppSharedAllocator.h"
          47                 : #include "nsCRTGlue.h"
          48                 :
          49                 : #if defined(XP_WIN) || defined(XP_OS2)
          50                 : #  define NS_LINEBREAK           "\015\012"
          51                 : #  define NS_LINEBREAK_LEN       2
          52                 : #else
          53                 : #  ifdef XP_UNIX
          54                 : #    define NS_LINEBREAK         "\012"
          55                 : #    define NS_LINEBREAK_LEN     1
          56                 : #  endif /* XP_UNIX */
          57                 : #endif /* XP_WIN || XP_OS2 */
          58                 :
          59                 : extern const PRUnichar kIsoLatin1ToUCS2[256];
          60                 :
          61                 : // This macro can be used in a class declaration for classes that want
          62                 : // to ensure that their instance memory is zeroed.
          63                 : #define NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW   \
          64                 :   void* operator new(size_t sz) CPP_THROW_NEW { \
          65                 :     void* rv = ::operator new(sz);              \
          66                 :     if (rv) {                                   \
          67                 :       memset(rv, 0, sz);                        \
          68                 :     }                                           \
          69                 :     return rv;                                  \
          70                 :   }                                             \
          71                 :   void operator delete(void* ptr) {             \
          72                 :     ::operator delete(ptr);                     \
          73                 :   }
          74                 :
          75                 : // This macro works with the next macro to declare a non-inlined
          76                 : // version of the above.
          77                 : #define NS_DECL_ZEROING_OPERATOR_NEW           \
          78                 :   void* operator new(size_t sz) CPP_THROW_NEW; \
          79                 :   void operator delete(void* ptr);
          80                 :
          81                 : #define NS_IMPL_ZEROING_OPERATOR_NEW(_class)            \
          82                 :   void* _class::operator new(size_t sz) CPP_THROW_NEW { \
          83                 :     void* rv = ::operator new(sz);                      \
          84                 :     if (rv) {                                           \
          85                 :       memset(rv, 0, sz);                                \
          86                 :     }                                                   \
          87                 :     return rv;                                          \
          88                 :   }                                                     \
          89                 :   void _class::operator delete(void* ptr) {             \
          90                 :     ::operator delete(ptr);                             \
          91                 :   }
          92                 :
          93                 : // Freeing helper
          94                 : #define CRTFREEIF(x) if (x) { nsCRT::free(x); x = 0; }
          95                 :
          96                 : /// This is a wrapper class around all the C runtime functions.
          97                 :
          98                 : class nsCRT {
          99                 : public:
         100                 :   enum {
         101                 :     LF='\n'   /* Line Feed */,
         102                 :     VTAB='\v' /* Vertical Tab */,
         103                 :     CR='\r'   /* Carriage Return */
         104                 :   };
         105                 :
         106                 :   /***
         107                 :    ***  The following nsCRT::mem* functions are no longer
         108                 :    ***  supported, please use the corresponding lib C
         109                 :    ***  functions instead.
         110                 :    ***
         111                 :    ***  nsCRT::memcpy()
         112                 :    ***  nsCRT::memcmp()
         113                 :    ***  nsCRT::memmove()
         114                 :    ***  nsCRT::memset()
         115                 :    ***  nsCRT::zero()
         116                 :    ***
         117                 :    ***  Additionally, the following char* string utilities
         118                 :    ***  are no longer supported, please use the
         119                 :    ***  corresponding lib C functions instead.
         120                 :    ***
         121                 :    ***  nsCRT::strlen()
         122                 :    ***
         123                 :    ***/
         124                 :
         125                 :   /** Compute the string length of s
         126                 :    @param s the string in question
         127                 :    @return the length of s
         128                 :    */
         129             106 :   static PRUint32 strlen(const char* s) {
         130             106 :     return PRUint32(::strlen(s));
         131                 :   }
         132                 :
         133                 :   /// Compare s1 and s2.
         134               0 :   static PRInt32 strcmp(const char* s1, const char* s2) {
         135               0 :     return PRInt32(PL_strcmp(s1, s2));
         136                 :   }
         137                 :
         138               0 :   static PRInt32 strncmp(const char* s1, const char* s2,
         139                 :                          PRUint32 aMaxLen) {
         140               0 :     return PRInt32(PL_strncmp(s1, s2, aMaxLen));
         141                 :   }
         142                 :
         143                 :   /// Case-insensitive string comparison.
         144         1303125 :   static PRInt32 strcasecmp(const char* s1, const char* s2) {
         145         1303125 :     return PRInt32(PL_strcasecmp(s1, s2));
         146                 :   }
         147                 :
         148                 :   /// Case-insensitive string comparison with length
         149             841 :   static PRInt32 strncasecmp(const char* s1, const char* s2, PRUint32 aMaxLen) {
         150             841 :     PRInt32 result=PRInt32(PL_strncasecmp(s1, s2, aMaxLen));
         151                 :     //Egads. PL_strncasecmp is returning *very* negative numbers.
         152                 :     //Some folks expect -1,0,1, so let's temper its enthusiasm.
         153             841 :     if (result<0)
         154              23 :       result=-1;
         155             841 :     return result;
         156                 :   }
         157                 :
         158            2193 :   static PRInt32 strncmp(const char* s1, const char* s2, PRInt32 aMaxLen) {
         159                 :     // inline the first test (assumes strings are not null):
         160            2193 :     PRInt32 diff = ((const unsigned char*)s1)[0] - ((const unsigned char*)s2)[0];
         161            2193 :     if (diff != 0) return diff;
         162             186 :     return PRInt32(PL_strncmp(s1,s2,unsigned(aMaxLen)));
         163                 :   }
         164                 :
         165             706 :   static char* strdup(const char* str) {
         166             706 :     return PL_strdup(str);
         167                 :   }
         168                 :
         169                 :   static char* strndup(const char* str, PRUint32 len) {
         170                 :     return PL_strndup(str, len);
         171                 :   }
         172                 :
         173               4 :   static void free(char* str) {
         174               4 :     PL_strfree(str);
         175               4 :   }
         176                 :
         177                 :   /**
         178                 :
         179                 :     How to use this fancy (thread-safe) version of strtok:
         180                 :
         181                 :     void main(void) {
         182                 :       printf("%s\n\nTokens:\n", string);
         183                 :       // Establish string and get the first token:
         184                 :       char* newStr;
         185                 :       token = nsCRT::strtok(string, seps, &newStr);
         186                 :       while (token != NULL) {
         187                 :         // While there are tokens in "string"
         188                 :         printf(" %s\n", token);
         189                 :         // Get next token:
         190                 :         token = nsCRT::strtok(newStr, seps, &newStr);
         191                 :       }
         192                 :     }
         193                 :     * WARNING - STRTOK WHACKS str THE FIRST TIME IT IS CALLED *
         194                 :     * MAKE A COPY OF str IF YOU NEED TO USE IT AFTER strtok() *
         195                 :   */
         196                 :   static char* strtok(char* str, const char* delims, char* *newStr);
         197                 :
         198           49534 :   static PRUint32 strlen(const PRUnichar* s) {
         199                 :     // XXXbsmedberg: remove this null-check at some point
         200           49534 :     if (!s) {
         201               0 :       NS_ERROR("Passing null to nsCRT::strlen");
         202               0 :       return 0;
         203                 :     }
         204           49534 :     return NS_strlen(s);
         205                 :   }
         206                 :
         207                 :   /// Like strcmp except for ucs2 strings
         208                 :   static PRInt32 strcmp(const PRUnichar* s1, const PRUnichar* s2);
         209                 :   /// Like strcmp except for ucs2 strings
         210                 :   static PRInt32 strncmp(const PRUnichar* s1, const PRUnichar* s2,
         211                 :                          PRUint32 aMaxLen);
         212                 :
         213                 :   // The GNU libc has memmem, which is strstr except for binary data
         214                 :   // This is our own implementation that uses memmem on platforms
         215                 :   // where it's available.
         216                 :   static const char* memmem(const char* haystack, PRUint32 haystackLen,
         217                 :                             const char* needle, PRUint32 needleLen);
         218                 :
         219                 :   // You must use nsCRT::free(PRUnichar*) to free memory allocated
         220                 :   // by nsCRT::strdup(PRUnichar*).
         221                 :   static PRUnichar* strdup(const PRUnichar* str);
         222                 :
         223                 :   // You must use nsCRT::free(PRUnichar*) to free memory allocated
         224                 :   // by strndup(PRUnichar*, PRUint32).
         225                 :   static PRUnichar* strndup(const PRUnichar* str, PRUint32 len);
         226                 :
         227                 :   static void free(PRUnichar* str) {
         228                 :         nsCppSharedAllocator<PRUnichar> shared_allocator;
         229                 :         shared_allocator.deallocate(str, 0 /*we never new or kept the size*/);
         230                 :   }
         231                 :
         232                 :   // String to longlong
         233                 :   static PRInt64 atoll(const char *str);
         234                 :
         235               0 :   static char ToUpper(char aChar) { return NS_ToUpper(aChar); }
         236          916862 :   static char ToLower(char aChar) { return NS_ToLower(aChar); }
         237                 :
         238                 :   static bool IsUpper(char aChar) { return NS_IsUpper(aChar); }
         239               0 :   static bool IsLower(char aChar) { return NS_IsLower(aChar); }
         240                 :
         241               0 :   static bool IsAscii(PRUnichar aChar) { return NS_IsAscii(aChar); }
         242                 :   static bool IsAscii(const PRUnichar* aString) { return NS_IsAscii(aString); }
         243         5130224 :   static bool IsAsciiAlpha(PRUnichar aChar) { return NS_IsAsciiAlpha(aChar); }
         244          526764 :   static bool IsAsciiDigit(PRUnichar aChar) { return NS_IsAsciiDigit(aChar); }
         245         1118457 :   static bool IsAsciiSpace(PRUnichar aChar) { return NS_IsAsciiWhitespace(aChar); }
         246         1502514 :   static bool IsAscii(const char* aString) { return NS_IsAscii(aString); }
         247               0 :   static bool IsAscii(const char* aString, PRUint32 aLength) { return NS_IsAscii(aString, aLength); }
         248                 : };
         249                 :
         250                 :
         251                 : #define NS_IS_SPACE(VAL) \
         252                 :   (((((intn)(VAL)) & 0x7f) == ((intn)(VAL))) && isspace((intn)(VAL)) )
         253                 :
         254                 : #define NS_IS_CNTRL(i)   ((((unsigned int) (i)) > 0x7f) ? (int) 0 : iscntrl(i))
         255                 : #define NS_IS_DIGIT(i)   ((((unsigned int) (i)) > 0x7f) ? (int) 0 : isdigit(i))
         256                 : #if defined(XP_WIN) || defined(XP_OS2)
         257                 : #define NS_IS_ALPHA(VAL) (isascii((int)(VAL)) && isalpha((int)(VAL)))
         258                 : #else
         259                 : #define NS_IS_ALPHA(VAL) ((((unsigned int) (VAL)) > 0x7f) ? (int) 0 : isalpha((int)(VAL)))
         260                 : #endif
         261                 :
         262                 :
         263                 : #endif /* nsCRT_h___ */

Generated by: LCOV version 1.7