]> Dogcows Code - chaz/yoink/blob - src/stlplus/persistence/persistent_cstring.cpp
testing new non-autotools build system
[chaz/yoink] / src / stlplus / persistence / persistent_cstring.cpp
1 ////////////////////////////////////////////////////////////////////////////////
2
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
7
8 ////////////////////////////////////////////////////////////////////////////////
9
10 #include "persistent_cstring.hpp"
11 #include "persistent_int.hpp"
12 #include <string.h>
13
14 ////////////////////////////////////////////////////////////////////////////////
15 // Null-terminated char arrays
16 // Format: address [ size data ]
17
18 void stlplus::dump_cstring(stlplus::dump_context& context, const char* data) throw(stlplus::persistent_dump_failed)
19 {
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)
26 {
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]);
31 }
32 }
33
34 void stlplus::restore_cstring(restore_context& context, char*& data) throw(stlplus::persistent_restore_failed)
35 {
36 // destroy any previous contents
37 if (data)
38 {
39 delete[] data;
40 data = 0;
41 }
42 // get the magic key
43 unsigned magic = 0;
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);
48 if (!address.first)
49 {
50 // this pointer has never been seen before and is non-null
51 // restore the string
52 unsigned size = 0;
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]);
57 data[size] = '\0';
58 // add this pointer to the set of already seen objects
59 context.pointer_add(magic,data);
60 }
61 }
62
63 ////////////////////////////////////////////////////////////////////////////////
This page took 0.030794 seconds and 4 git commands to generate.