1 ////////////////////////////////////////////////////////////////////////////////
3 // Author: Andy Rushton
4 // Copyright: (c) Southampton University 1999-2004
5 // (c) Andy Rushton 2004-2009
6 // License: BSD License, see ../docs/license.html
8 ////////////////////////////////////////////////////////////////////////////////
10 #include "persistent_cstring.hpp"
11 #include "persistent_int.hpp"
14 ////////////////////////////////////////////////////////////////////////////////
15 // Null-terminated char arrays
16 // Format: address [ size data ]
18 void stlplus::dump_cstring(stlplus::dump_context
& context
, const char* data
) throw(stlplus::persistent_dump_failed
)
20 // register the address and get the magic key for it
21 std::pair
<bool,unsigned> mapping
= context
.pointer_map(data
);
22 stlplus::dump_unsigned(context
,mapping
.second
);
23 // if the address is null, then that is all that we need to do
24 // however, if it is non-null and this is the first sight of the address, dump the contents
25 if (data
&& !mapping
.first
)
27 unsigned size
= strlen(data
);
28 stlplus::dump_unsigned(context
,size
);
29 for (unsigned i
= 0; i
< size
; i
++)
30 stlplus::dump_char(context
,data
[i
]);
34 void stlplus::restore_cstring(restore_context
& context
, char*& data
) throw(stlplus::persistent_restore_failed
)
36 // destroy any previous contents
44 stlplus::restore_unsigned(context
,magic
);
45 // now lookup the magic key to see if this pointer has already been restored
46 // null pointers are always flagged as already restored
47 std::pair
<bool,void*> address
= context
.pointer_map(magic
);
50 // this pointer has never been seen before and is non-null
53 stlplus::restore_unsigned(context
,size
);
54 data
= new char[size
+1];
55 for (unsigned i
= 0; i
< size
; i
++)
56 stlplus::restore_char(context
,data
[i
]);
58 // add this pointer to the set of already seen objects
59 context
.pointer_add(magic
,data
);
63 ////////////////////////////////////////////////////////////////////////////////