~gnomefreak/firefox-extensions/firegpg.ubuntu

« back to all changes in this revision

Viewing changes to FireGPGCall/nsCRT.h

  • Committer: John Vivirito
  • Date: 2008-08-12 11:47:33 UTC
  • Revision ID: gnomefreak@ubuntu.com-20080812114733-hn73tjxi26ylibrf
* import of upstream source version 0.5.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#  if defined(XP_UNIX) || defined(XP_BEOS)
 
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 NS_COM 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
  static PRUint32 strlen(const char* s) {
 
130
    return PRUint32(::strlen(s));
 
131
  }
 
132
 
 
133
  /// Compare s1 and s2.
 
134
  static PRInt32 strcmp(const char* s1, const char* s2) {
 
135
    return PRInt32(PL_strcmp(s1, s2));
 
136
  }
 
137
 
 
138
  static PRInt32 strncmp(const char* s1, const char* s2,
 
139
                         PRUint32 aMaxLen) {
 
140
    return PRInt32(PL_strncmp(s1, s2, aMaxLen));
 
141
  }
 
142
 
 
143
  /// Case-insensitive string comparison.
 
144
  static PRInt32 strcasecmp(const char* s1, const char* s2) {
 
145
    return PRInt32(PL_strcasecmp(s1, s2));
 
146
  }
 
147
 
 
148
  /// Case-insensitive string comparison with length
 
149
  static PRInt32 strncasecmp(const char* s1, const char* s2, PRUint32 aMaxLen) {
 
150
    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
    if (result<0)
 
154
      result=-1;
 
155
    return result;
 
156
  }
 
157
 
 
158
  static PRInt32 strncmp(const char* s1, const char* s2, PRInt32 aMaxLen) {
 
159
    // inline the first test (assumes strings are not null):
 
160
    PRInt32 diff = ((const unsigned char*)s1)[0] - ((const unsigned char*)s2)[0];
 
161
    if (diff != 0) return diff;
 
162
    return PRInt32(PL_strncmp(s1,s2,unsigned(aMaxLen)));
 
163
  }
 
164
 
 
165
  static char* strdup(const char* str) {
 
166
    return PL_strdup(str);
 
167
  }
 
168
 
 
169
  static char* strndup(const char* str, PRUint32 len) {
 
170
    return PL_strndup(str, len);
 
171
  }
 
172
 
 
173
  static void free(char* str) {
 
174
    PL_strfree(str);
 
175
  }
 
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
  static PRUint32 strlen(const PRUnichar* s) {
 
199
    // XXXbsmedberg: remove this null-check at some point
 
200
    if (!s) {
 
201
      //NS_ERROR("Passing null to nsCRT::strlen");
 
202
      return 0;
 
203
    }
 
204
    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
  // You must use nsCRT::free(PRUnichar*) to free memory allocated
 
214
  // by nsCRT::strdup(PRUnichar*).
 
215
  static PRUnichar* strdup(const PRUnichar* str);
 
216
 
 
217
  // You must use nsCRT::free(PRUnichar*) to free memory allocated
 
218
  // by strndup(PRUnichar*, PRUint32).
 
219
  static PRUnichar* strndup(const PRUnichar* str, PRUint32 len);
 
220
 
 
221
  static void free(PRUnichar* str) {
 
222
        nsCppSharedAllocator<PRUnichar> shared_allocator;
 
223
        shared_allocator.deallocate(str, 0 /*we never new or kept the size*/);
 
224
  }
 
225
 
 
226
  // Computes the hashcode for a c-string, returns the string length as
 
227
  // an added bonus.
 
228
  static PRUint32 HashCode(const char* str,
 
229
                           PRUint32* resultingStrLen = nsnull);
 
230
 
 
231
  // Computes the hashcode for a length number of bytes of c-string data.
 
232
  static PRUint32 HashCode(const char* start, PRUint32 length);
 
233
 
 
234
  // Computes the hashcode for a ucs2 string, returns the string length
 
235
  // as an added bonus.
 
236
  static PRUint32 HashCode(const PRUnichar* str,
 
237
                           PRUint32* resultingStrLen = nsnull);
 
238
 
 
239
  // Computes a hashcode for a length number of UTF16
 
240
  // characters. Returns the same hash code as the HashCode method
 
241
  // taking a |char*| would if the string were converted to UTF8. This
 
242
  // hash function treats invalid UTF16 data as 0xFFFD (0xEFBFBD in
 
243
  // UTF-8).
 
244
  static PRUint32 HashCodeAsUTF8(const PRUnichar* start, PRUint32 length);
 
245
 
 
246
  // Computes the hashcode for a buffer with a specified length.
 
247
  static PRUint32 BufferHashCode(const PRUnichar* str, PRUint32 strLen);
 
248
 
 
249
  // String to longlong
 
250
  static PRInt64 atoll(const char *str);
 
251
 
 
252
  static char ToUpper(char aChar) { return NS_ToUpper(aChar); }
 
253
  static char ToLower(char aChar) { return NS_ToLower(aChar); }
 
254
 
 
255
  static PRBool IsUpper(char aChar) { return NS_IsUpper(aChar); }
 
256
  static PRBool IsLower(char aChar) { return NS_IsLower(aChar); }
 
257
 
 
258
  static PRBool IsAscii(PRUnichar aChar) { return NS_IsAscii(aChar); }
 
259
  static PRBool IsAscii(const PRUnichar* aString) { return NS_IsAscii(aString); }
 
260
  static PRBool IsAsciiAlpha(PRUnichar aChar) { return NS_IsAsciiAlpha(aChar); }
 
261
  static PRBool IsAsciiDigit(PRUnichar aChar) { return NS_IsAsciiDigit(aChar); }
 
262
  static PRBool IsAsciiSpace(PRUnichar aChar) { return NS_IsAsciiWhitespace(aChar); }
 
263
  static PRBool IsAscii(const char* aString) { return NS_IsAscii(aString); }
 
264
  static PRBool IsAscii(const char* aString, PRUint32 aLength) { return NS_IsAscii(aString, aLength); }
 
265
};
 
266
 
 
267
#define FF '\f'
 
268
#define TAB '\t'
 
269
 
 
270
#define CRSTR "\015"
 
271
#define LFSTR "\012"
 
272
#define CRLF "\015\012"     /* A CR LF equivalent string */
 
273
 
 
274
 
 
275
#if defined(XP_MACOSX)
 
276
  #define FILE_PATH_SEPARATOR        "/"
 
277
  #define OS_FILE_ILLEGAL_CHARACTERS ":"
 
278
#elif defined(XP_WIN) || defined(XP_OS2)
 
279
  #define FILE_PATH_SEPARATOR        "\\"
 
280
  #define OS_FILE_ILLEGAL_CHARACTERS "/:*?\"<>|"
 
281
#elif defined(XP_UNIX) || defined(XP_BEOS)
 
282
  #define FILE_PATH_SEPARATOR        "/"
 
283
  #define OS_FILE_ILLEGAL_CHARACTERS ""
 
284
#else
 
285
  #error need_to_define_your_file_path_separator_and_illegal_characters
 
286
#endif
 
287
 
 
288
// Not all these control characters are illegal in all OSs, but we don't really
 
289
// want them appearing in filenames
 
290
#define CONTROL_CHARACTERS     "\001\002\003\004\005\006\007" \
 
291
                           "\010\011\012\013\014\015\016\017" \
 
292
                           "\020\021\022\023\024\025\026\027" \
 
293
                           "\030\031\032\033\034\035\036\037"
 
294
 
 
295
#define FILE_ILLEGAL_CHARACTERS CONTROL_CHARACTERS OS_FILE_ILLEGAL_CHARACTERS
 
296
 
 
297
#define NS_IS_SPACE(VAL) \
 
298
  (((((intn)(VAL)) & 0x7f) == ((intn)(VAL))) && isspace((intn)(VAL)) )
 
299
 
 
300
#define NS_IS_CNTRL(i)   ((((unsigned int) (i)) > 0x7f) ? (int) 0 : iscntrl(i))
 
301
#define NS_IS_DIGIT(i)   ((((unsigned int) (i)) > 0x7f) ? (int) 0 : isdigit(i))
 
302
#if defined(XP_WIN) || defined(XP_OS2)
 
303
#define NS_IS_ALPHA(VAL) (isascii((int)(VAL)) && isalpha((int)(VAL)))
 
304
#else
 
305
#define NS_IS_ALPHA(VAL) ((((unsigned int) (VAL)) > 0x7f) ? (int) 0 : isalpha((int)(VAL)))
 
306
#endif
 
307
 
 
308
 
 
309
#endif /* nsCRT_h___ */