2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* OSSerialize.cpp created by rsulack on Wen 25-Nov-1998 */
24 #include <libkern/c++/OSContainers.h>
25 #include <libkern/c++/OSLib.h>
26 #include <libkern/c++/OSDictionary.h>
28 #define super OSObject
30 OSDefineMetaClassAndStructors(OSSerialize
, OSObject
)
31 OSMetaClassDefineReservedUnused(OSSerialize
, 0);
32 OSMetaClassDefineReservedUnused(OSSerialize
, 1);
33 OSMetaClassDefineReservedUnused(OSSerialize
, 2);
34 OSMetaClassDefineReservedUnused(OSSerialize
, 3);
35 OSMetaClassDefineReservedUnused(OSSerialize
, 4);
36 OSMetaClassDefineReservedUnused(OSSerialize
, 5);
37 OSMetaClassDefineReservedUnused(OSSerialize
, 6);
38 OSMetaClassDefineReservedUnused(OSSerialize
, 7);
42 extern int debug_container_malloc_size
;
44 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
49 char * OSSerialize::text() const
54 void OSSerialize::clearText()
56 bzero((void *)data
, capacity
);
59 tags
->flushCollection();
62 bool OSSerialize::previouslySerialized(const OSMetaClassBase
*o
)
68 tagString
= (OSString
*)tags
->getObject((const OSSymbol
*) o
);
72 addString("<reference IDREF=\"");
73 addString(tagString
->getCStringNoCopy());
79 sprintf(temp
, "%u", tag
++);
80 tagString
= OSString::withCString(temp
);
82 // add to tag dictionary
83 tags
->setObject((const OSSymbol
*) o
, tagString
);// XXX check return
89 bool OSSerialize::addXMLStartTag(const OSMetaClassBase
*o
, const char *tagString
)
92 if (!addChar('<')) return false;
93 if (!addString(tagString
)) return false;
94 if (!addString(" ID=\"")) return false;
95 if (!addString(((OSString
*)tags
->getObject((const OSSymbol
*)o
))->getCStringNoCopy()))
97 if (!addChar('\"')) return false;
98 if (!addChar('>')) return false;
102 bool OSSerialize::addXMLEndTag(const char *tagString
)
105 if (!addChar('<')) return false;
106 if (!addChar('/')) return false;
107 if (!addString(tagString
)) return false;
108 if (!addChar('>')) return false;
112 bool OSSerialize::addChar(const char c
)
114 // add char, possibly extending our capacity
115 if (length
>= capacity
&& length
>=ensureCapacity(capacity
+capacityIncrement
))
118 data
[length
- 1] = c
;
124 bool OSSerialize::addString(const char *s
)
128 while (*s
&& (rc
= addChar(*s
++))) ;
133 bool OSSerialize::initWithCapacity(unsigned int inCapacity
)
138 tags
= OSDictionary::withCapacity(32);
145 capacity
= inCapacity
;
146 capacityIncrement
= (capacity
)? capacity
: 256;
148 capacity
= (((capacity
- 1) / capacityIncrement
) + 1)
150 data
= (char *) kalloc(capacity
);
156 bzero((void *)data
, capacity
);
164 OSSerialize
*OSSerialize::withCapacity(unsigned int inCapacity
)
166 OSSerialize
*me
= new OSSerialize
;
168 if (me
&& !me
->initWithCapacity(inCapacity
)) {
176 unsigned int OSSerialize::getLength() const { return length
; }
177 unsigned int OSSerialize::getCapacity() const { return capacity
; }
178 unsigned int OSSerialize::getCapacityIncrement() const { return capacityIncrement
; }
179 unsigned int OSSerialize::setCapacityIncrement(unsigned int increment
)
181 capacityIncrement
= (increment
)? increment
: 256;
182 return capacityIncrement
;
185 unsigned int OSSerialize::ensureCapacity(unsigned int newCapacity
)
188 unsigned int oldCapacity
;
190 if (newCapacity
<= capacity
)
194 newCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
196 newData
= (char *) kalloc(newCapacity
);
198 oldCapacity
= capacity
;
200 ACCUMSIZE(newCapacity
- oldCapacity
);
202 bcopy(data
, newData
, oldCapacity
);
203 bzero(&newData
[capacity
], newCapacity
- oldCapacity
);
204 kfree((vm_offset_t
)data
, oldCapacity
);
206 capacity
= newCapacity
;
212 void OSSerialize::free()
218 kfree((vm_offset_t
)data
, capacity
);
219 ACCUMSIZE( -capacity
);
225 OSDefineMetaClassAndStructors(OSSerializer
, OSObject
)
227 OSSerializer
* OSSerializer::forTarget( void * target
,
228 OSSerializerCallback callback
, void * ref
= 0 )
230 OSSerializer
* thing
;
232 thing
= new OSSerializer
;
233 if( thing
&& !thing
->init()) {
239 thing
->target
= target
;
241 thing
->callback
= callback
;
246 bool OSSerializer::serialize( OSSerialize
* s
) const
248 return( (*callback
)(target
, ref
, s
) );