~ubuntu-branches/ubuntu/karmic/gnustep-base/karmic

« back to all changes in this revision

Viewing changes to Headers/Foundation/NSData.h

  • Committer: Bazaar Package Importer
  • Author(s): Eric Heintzmann
  • Date: 2005-04-17 00:14:38 UTC
  • mfrom: (1.2.1 upstream) (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050417001438-enf0y07c9tku85z1
Tags: 1.10.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Interface for NSData for GNUStep
 
2
   Copyright (C) 1995 Free Software Foundation, Inc.
 
3
 
 
4
   Written by:  Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
 
5
   Date: 1995
 
6
   
 
7
   This file is part of the GNUstep Base Library.
 
8
   
 
9
   This library is free software; you can redistribute it and/or
 
10
   modify it under the terms of the GNU Library General Public
 
11
   License as published by the Free Software Foundation; either
 
12
   version 2 of the License, or (at your option) any later version.
 
13
   
 
14
   This library is distributed in the hope that it will be useful,
 
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
   Library General Public License for more details.
 
18
   
 
19
   You should have received a copy of the GNU Library General Public
 
20
   License along with this library; if not, write to the Free
 
21
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
 
22
   */ 
 
23
 
 
24
#ifndef __NSData_h_GNUSTEP_BASE_INCLUDE
 
25
#define __NSData_h_GNUSTEP_BASE_INCLUDE
 
26
 
 
27
#include <Foundation/NSObject.h>
 
28
#include <Foundation/NSRange.h>
 
29
#include <Foundation/NSSerialization.h>
 
30
 
 
31
#ifndef STRICT_OPENSTEP
 
32
@class  NSURL;
 
33
#endif
 
34
 
 
35
@interface NSData : NSObject <NSCoding, NSCopying, NSMutableCopying>
 
36
 
 
37
// Allocating and Initializing a Data Object
 
38
 
 
39
+ (id) data;
 
40
+ (id) dataWithBytes: (const void*)bytes
 
41
              length: (unsigned int)length;
 
42
+ (id) dataWithBytesNoCopy: (void*)bytes
 
43
                    length: (unsigned int)length;
 
44
#ifndef STRICT_OPENSTEP
 
45
+ (id) dataWithBytesNoCopy: (void*)aBuffer
 
46
                    length: (unsigned int)bufferSize
 
47
              freeWhenDone: (BOOL)shouldFree;
 
48
#endif
 
49
+ (id) dataWithContentsOfFile: (NSString*)path;
 
50
+ (id) dataWithContentsOfMappedFile: (NSString*)path;
 
51
#ifndef STRICT_OPENSTEP
 
52
+ (id) dataWithContentsOfURL: (NSURL*)url;
 
53
#endif
 
54
+ (id) dataWithData: (NSData*)data;
 
55
- (id) initWithBytes: (const void*)aBuffer
 
56
              length: (unsigned int)bufferSize;
 
57
- (id) initWithBytesNoCopy: (void*)aBuffer
 
58
                    length: (unsigned int)bufferSize;
 
59
#ifndef STRICT_OPENSTEP
 
60
- (id) initWithBytesNoCopy: (void*)aBuffer
 
61
                    length: (unsigned int)bufferSize
 
62
              freeWhenDone: (BOOL)shouldFree;
 
63
#endif
 
64
- (id) initWithContentsOfFile: (NSString*)path;
 
65
- (id) initWithContentsOfMappedFile: (NSString*)path;
 
66
#ifndef STRICT_OPENSTEP
 
67
- (id) initWithContentsOfURL: (NSURL*)url;
 
68
#endif
 
69
- (id) initWithData: (NSData*)data;
 
70
 
 
71
// Accessing Data 
 
72
 
 
73
- (const void*) bytes;
 
74
- (NSString*) description;
 
75
- (void) getBytes: (void*)buffer;
 
76
- (void) getBytes: (void*)buffer
 
77
           length: (unsigned int)length;
 
78
- (void) getBytes: (void*)buffer
 
79
            range: (NSRange)aRange;
 
80
- (NSData*) subdataWithRange: (NSRange)aRange;
 
81
 
 
82
// Querying a Data Object
 
83
 
 
84
- (BOOL) isEqualToData: (NSData*)other;
 
85
- (unsigned int) length;
 
86
 
 
87
// Storing Data
 
88
 
 
89
- (BOOL) writeToFile: (NSString*)path
 
90
          atomically: (BOOL)useAuxiliaryFile;
 
91
 
 
92
#ifndef STRICT_OPENSTEP
 
93
- (BOOL) writeToURL: (NSURL*)anURL atomically: (BOOL)flag;
 
94
#endif
 
95
 
 
96
// Deserializing Data
 
97
 
 
98
- (unsigned int) deserializeAlignedBytesLengthAtCursor: (unsigned int*)cursor;
 
99
- (void) deserializeBytes: (void*)buffer
 
100
                   length: (unsigned int)bytes
 
101
                 atCursor: (unsigned int*)cursor;
 
102
- (void) deserializeDataAt: (void*)data
 
103
                ofObjCType: (const char*)type
 
104
                  atCursor: (unsigned int*)cursor
 
105
                   context: (id <NSObjCTypeSerializationCallBack>)callback;
 
106
- (int) deserializeIntAtCursor: (unsigned int*)cursor;
 
107
- (int) deserializeIntAtIndex: (unsigned int)index;
 
108
- (void) deserializeInts: (int*)intBuffer
 
109
                   count: (unsigned int)numInts
 
110
                atCursor: (unsigned int*)cursor;
 
111
- (void) deserializeInts: (int*)intBuffer
 
112
                   count: (unsigned int)numInts
 
113
                 atIndex: (unsigned int)index;
 
114
 
 
115
@end
 
116
 
 
117
#ifndef NO_GNUSTEP
 
118
 
 
119
/**
 
120
 * Extension methods for the NSData class- mainly conversion utilities.
 
121
 */
 
122
@interface NSData (GSCategories)
 
123
 
 
124
/**
 
125
 * Returns an NSString object containing an ASCII hexadecimal representation
 
126
 * of the receiver.  This means that the returned object will contain
 
127
 * exactly twice as many characters as there are bytes as the receiver,
 
128
 * as each byte in the receiver is represented by two hexadecimal digits.<br />
 
129
 * The high order four bits of each byte is encoded before the low
 
130
 * order four bits.  Capital letters 'A' to 'F' are used to represent
 
131
 * values from 10 to 15.<br />
 
132
 * If you need the hexadecimal representation as raw byte data, use code
 
133
 * like -
 
134
 * <example>
 
135
 *   hexData = [[sourceData hexadecimalRepresentation]
 
136
 *     dataUsingEncoding: NSASCIIStringEncoding];
 
137
 * </example>
 
138
 */
 
139
- (NSString*) hexadecimalRepresentation;
 
140
 
 
141
/**
 
142
 * Initialises the receiver with the supplied string data which contains
 
143
 * a hexadecimal coding of the bytes.  The parsing of the string is
 
144
 * fairly tolerant, ignoring whitespace and permitting both upper and
 
145
 * lower case hexadecimal digits (the -hexadecimalRepresentation method
 
146
 * produces a string using only uppercase digits with no white spaqce).<br />
 
147
 * If the string does not contain one or more pairs of hexadecimal digits
 
148
 * then an exception is raised. 
 
149
 */
 
150
- (id) initWithHexadecimalRepresentation: (NSString*)string;
 
151
 
 
152
/**
 
153
 * Creates an MD5 digest of the information stored in the receiver and
 
154
 * returns it as an autoreleased 16 byte NSData object.<br />
 
155
 * If you need to produce a digest of string information, you need to
 
156
 * decide what character encoding is to be used and convert your string
 
157
 * to a data object of that encoding type first using the
 
158
 * [NSString-dataUsingEncoding:] method -
 
159
 * <example>
 
160
 *   myDigest = [[myString dataUsingEncoding: NSUTF8StringEncoding] md5Digest];
 
161
 * </example>
 
162
 * If you need to use the digest in a human readable form, you will
 
163
 * probably want it to be seen as 32 hexadecimal digits, and can do that
 
164
 * using the -hexadecimalRepresentation method.
 
165
 */
 
166
- (NSData*) md5Digest;
 
167
 
 
168
/**
 
169
 * Decodes the source data from uuencoded and return the result.<br />
 
170
 * Returns the encoded file name in namePtr if it is not null.
 
171
 * Returns the encoded file mode in modePtr if it is not null.
 
172
 */
 
173
- (BOOL) uudecodeInto: (NSMutableData*)decoded
 
174
                 name: (NSString**)namePtr
 
175
                 mode: (int*)modePtr;
 
176
 
 
177
/**
 
178
 * Encode the source data to uuencoded.<br />
 
179
 * Uses the supplied name as the filename in the encoded data,
 
180
 * and says that the file mode is as specified.<br />
 
181
 * If no name is supplied, uses <code>untitled</code> as the name.
 
182
 */
 
183
- (BOOL) uuencodeInto: (NSMutableData*)encoded
 
184
                 name: (NSString*)name
 
185
                 mode: (int)mode;
 
186
@end
 
187
 
 
188
/*
 
189
 *      We include special support for coding/decoding - adding methods for
 
190
 *      serializing/deserializing type-tags and cross-references.
 
191
 *
 
192
 *      A type-tag is a byte containing -
 
193
 *      Bit7    Set to indicate that the tag is for a cross-reference.
 
194
 *      Bit5-6  A value for the size of the type or cross-reference.
 
195
 *      Bit0-4  A value representing an Objective-C type.
 
196
 */
 
197
 
 
198
#define _GSC_NONE       0x00            /* No type information.         */
 
199
#define _GSC_XREF       0x80            /* Cross reference to an item.  */
 
200
#define _GSC_SIZE       0x60            /* Type-size info mask.         */
 
201
#define _GSC_MASK       0x1f            /* Basic type info mask.        */
 
202
 
 
203
/*
 
204
 *      If the tag is for a cross-reference, the size field defines the
 
205
 *      size of the cross-reference value -
 
206
 *      _GSC_X_0 (no crossref), _GSC_X_1, _GSC_X_2, _GSC_X_4
 
207
 */
 
208
#define _GSC_X_0        0x00            /* nil or null pointer          */
 
209
#define _GSC_X_1        0x20            /* 8-bit cross-ref              */
 
210
#define _GSC_X_2        0x40            /* 16-bit cross-ref             */
 
211
#define _GSC_X_4        0x60            /* 32-bit cross-ref             */
 
212
 
 
213
/*
 
214
 *      If the tag is for an integer value, the size field defines the
 
215
 *      size of the the encoded integer -
 
216
 *      _GSC_I16, _GSC_I32, _GSC_I64, _GSC_I128
 
217
 *      The file GSConfig.h (produced by the configure script) defines the
 
218
 *      size codes for this machines 'natural' integers -
 
219
 *      _GSC_S_SHT, _GSC_S_INT, _GSC_S_LNG, _GSC_S_LNG_LNG
 
220
 */
 
221
#define _GSC_I16        0x00
 
222
#define _GSC_I32        0x20
 
223
#define _GSC_I64        0x40
 
224
#define _GSC_I128       0x60
 
225
 
 
226
/*
 
227
 *      For the first sixteen types, the size information applies to the
 
228
 *      size of the type, for the second sixteen it applies to the
 
229
 *      following cross-reference number (or is zero if no crossref follows).
 
230
 */
 
231
#define _GSC_MAYX       0x10            /* Item may have crossref.      */
 
232
 
 
233
/*
 
234
 *      These are the types that can be archived -
 
235
 */
 
236
#define _GSC_CHR        0x01
 
237
#define _GSC_UCHR       0x02
 
238
#define _GSC_SHT        0x03
 
239
#define _GSC_USHT       0x04
 
240
#define _GSC_INT        0x05
 
241
#define _GSC_UINT       0x06
 
242
#define _GSC_LNG        0x07
 
243
#define _GSC_ULNG       0x08
 
244
#define _GSC_LNG_LNG    0x09
 
245
#define _GSC_ULNG_LNG   0x0a
 
246
#define _GSC_FLT        0x0b
 
247
#define _GSC_DBL        0x0c
 
248
 
 
249
#define _GSC_ID         0x10
 
250
#define _GSC_CLASS      0x11
 
251
#define _GSC_SEL        0x12
 
252
#define _GSC_PTR        0x13
 
253
#define _GSC_CHARPTR    0x14
 
254
#define _GSC_ARY_B      0x15
 
255
#define _GSC_STRUCT_B   0x16
 
256
#define _GSC_CID        0x17    /* Class encoded as id  */
 
257
 
 
258
@interface NSData (GNUstepExtensions)
 
259
+ (id) dataWithShmID: (int)anID length: (unsigned int) length;
 
260
+ (id) dataWithSharedBytes: (const void*)bytes length: (unsigned int) length;
 
261
 
 
262
/*
 
263
 *      -deserializeTypeTag:andCrossRef:atCursor:
 
264
 *      This method is provided in order to give the GNUstep version of
 
265
 *      NSUnarchiver maximum possible performance.
 
266
 */
 
267
- (void) deserializeTypeTag: (unsigned char*)tag
 
268
                andCrossRef: (unsigned int*)ref
 
269
                   atCursor: (unsigned int*)cursor;
 
270
@end
 
271
#endif
 
272
 
 
273
@interface NSMutableData :  NSData
 
274
 
 
275
+ (id) dataWithCapacity: (unsigned int)numBytes;
 
276
+ (id) dataWithLength: (unsigned int)length;
 
277
- (id) initWithCapacity: (unsigned int)capacity;
 
278
- (id) initWithLength: (unsigned int)length;
 
279
 
 
280
// Adjusting Capacity
 
281
 
 
282
- (void) increaseLengthBy: (unsigned int)extraLength;
 
283
- (void) setLength: (unsigned int)size;
 
284
- (void*) mutableBytes;
 
285
 
 
286
// Appending Data
 
287
 
 
288
- (void) appendBytes: (const void*)aBuffer
 
289
              length: (unsigned int)bufferSize;
 
290
- (void) appendData: (NSData*)other;
 
291
 
 
292
// Modifying Data
 
293
 
 
294
- (void) replaceBytesInRange: (NSRange)aRange
 
295
                   withBytes: (const void*)bytes;
 
296
#ifndef STRICT_OPENSTEP
 
297
- (void) replaceBytesInRange: (NSRange)aRange
 
298
                   withBytes: (const void*)bytes
 
299
                      length: (unsigned int)length;
 
300
#endif
 
301
- (void) resetBytesInRange: (NSRange)aRange;
 
302
- (void) setData: (NSData*)data;
 
303
 
 
304
// Serializing Data
 
305
 
 
306
- (void) serializeAlignedBytesLength: (unsigned int)length;
 
307
- (void) serializeDataAt: (const void*)data
 
308
              ofObjCType: (const char*)type
 
309
                 context: (id <NSObjCTypeSerializationCallBack>)callback;
 
310
- (void) serializeInt: (int)value;
 
311
- (void) serializeInt: (int)value
 
312
              atIndex: (unsigned int)index;
 
313
- (void) serializeInts: (int*)intBuffer
 
314
                 count: (unsigned int)numInts;
 
315
- (void) serializeInts: (int*)intBuffer
 
316
                 count: (unsigned int)numInts
 
317
               atIndex: (unsigned int)index;
 
318
 
 
319
@end
 
320
 
 
321
#ifndef NO_GNUSTEP
 
322
 
 
323
@interface NSMutableData (GNUstepExtensions)
 
324
/*
 
325
 *      Capacity management - GNUstep gives you control over the size of
 
326
 *      the data buffer as well as the 'length' of valid data in it.
 
327
 */
 
328
- (unsigned int) capacity;
 
329
- (id) setCapacity: (unsigned int)newCapacity;
 
330
 
 
331
- (int) shmID;  /* Shared memory ID for data buffer (if any)    */
 
332
 
 
333
/*
 
334
 *      -serializeTypeTag:
 
335
 *      -serializeTypeTag:andCrossRef:
 
336
 *      These methods are provided in order to give the GNUstep version of
 
337
 *      NSArchiver maximum possible performance.
 
338
 */
 
339
- (void) serializeTypeTag: (unsigned char)tag;
 
340
- (void) serializeTypeTag: (unsigned char)tag
 
341
              andCrossRef: (unsigned int)xref;
 
342
 
 
343
@end
 
344
#endif
 
345
 
 
346
/*
 
347
  Local Variables:
 
348
  mode: ObjC
 
349
  End:
 
350
  */
 
351
 
 
352
#endif /* __NSData_h_GNUSTEP_BASE_INCLUDE */