|
|
1.1 root 1: /*
2: * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3: *
4: * @APPLE_LICENSE_HEADER_START@
5: *
6: * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7: * Reserved. This file contains Original Code and/or Modifications of
8: * Original Code as defined in and that are subject to the Apple Public
9: * Source License Version 1.0 (the 'License'). You may not use this file
10: * except in compliance with the License. Please obtain a copy of the
11: * License at http://www.apple.com/publicsource and read it before using
12: * this file.
13: *
14: * The Original Code and all software distributed under the License are
15: * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16: * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17: * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19: * License for the specific language governing rights and limitations
20: * under the License."
21: *
22: * @APPLE_LICENSE_HEADER_END@
23: */
24: /*
25: NXReadOnlyString.m
26: Copyright 1991, NeXT, Inc.
27: Responsibility:
28: */
29:
30: #ifndef KERNEL
31: #ifdef SHLIB
32: #import "shlib.h"
33: #endif SHLIB
34:
35: #import "NXStringPrivate.h"
36:
37: @implementation NXReadOnlyString
38:
39: + alloc
40: {
41: return [super allocFromZone:stringZone];
42: }
43:
44: + allocFromZone:(NXZone *)zone
45: {
46: return [super allocFromZone:stringZone];
47: }
48:
49: - initFromCharactersNoCopy:(unichar *)chars length:(unsigned)len
50: {
51: return [self initFromCharactersNoCopy:chars length:len freeWhenDone:YES];
52: }
53:
54: - initFromCharactersNoCopy:(unichar *)chars length:(unsigned)len freeWhenDone:(BOOL)flag
55: {
56: [super initFromCharactersNoCopy:chars length:len];
57: _flags.notCopied = !flag;
58: _flags.refs = 1;
59: return self;
60: }
61:
62: - (unichar *)allocateCharacterBuffer:(unsigned)nChars
63: {
64: return NX_CHARALLOC(stringZone, nChars);
65: }
66:
67: - copyFromZone:(NXZone *)zone
68: {
69: if (_flags.refs < NX_STRING_MAXREFS) _flags.refs += 1;
70: return self;
71: }
72:
73: - (NXString *)copySubstring:(NXRange)range fromZone:(NXZone *)zone
74: {
75: if (RNGLOC(range) + RNGLEN(range) > _length) BOUNDSERROR;
76: if (RNGLOC(range) == 0 && RNGLEN(range) == _length) {
77: return [self copyFromZone:zone];
78: } else {
79: id newInstance = [[NXReadOnlySubstring allocFromZone:zone] initFromCharactersNoCopy:characters + RNGLOC(range) length:RNGLEN(range) freeWhenDone:NO];
80: ((struct {@defs(NXReadOnlySubstring);} *)newInstance)->_referenceString = [self copy];
81: return newInstance;
82: }
83: }
84:
85: - mutableCopyFromZone:(NXZone *)zone
86: {
87: id newInstance = [NXReadWriteString allocFromZone:zone];
88: ((struct {@defs(NXReadWriteString);} *)newInstance)->actualString = [self copyFromZone:zone];
89: return newInstance;
90: }
91:
92: - free
93: {
94: if ((_flags.refs < NX_STRING_MAXREFS) && (--_flags.refs == 0)) {
95: if (_flags.notCopied) {
96: characters = NULL; // So the super won't attempt to free the buffer...
97: }
98: return [super free];
99: } else {
100: return nil;
101: }
102: }
103:
104: // We want to make sure the object itself comes out of the string zone
105: // and not some random area which might be deallocated later...
106:
107: - finishUnarchiving
108: {
109: id actual = nil;
110: if ([self zone] != stringZone) {
111: actual = object_copyFromZone (self, 0, stringZone);
112: object_dispose(self);
113: }
114: return actual;
115: }
116:
117: - write:(NXTypedStream *)s
118: {
119: unsigned int refs = _flags.refs;
120: [super write:s];
121: NXWriteTypes (s, "i", &refs);
122: return self;
123: }
124:
125: - read:(NXTypedStream *)s
126: {
127: unsigned int refs;
128: [super read:s];
129: NXReadTypes (s, "i", &refs);
130: _flags.refs = refs;
131: return self;
132: }
133:
134: @end
135: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.