X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Fstlplus%2Fpersistence%2Fpersistent_cstring.cpp;fp=src%2Fstlplus%2Fpersistence%2Fpersistent_cstring.cpp;h=a829b9af0558aafd7ded7e62b63c2ede4958b110;hb=6b0a0d0efafe34d48ab344fca3b479553bd4e62c;hp=0000000000000000000000000000000000000000;hpb=85783316365181491a3e3c0c63659972477cebba;p=chaz%2Fyoink diff --git a/src/stlplus/persistence/persistent_cstring.cpp b/src/stlplus/persistence/persistent_cstring.cpp new file mode 100644 index 0000000..a829b9a --- /dev/null +++ b/src/stlplus/persistence/persistent_cstring.cpp @@ -0,0 +1,63 @@ +//////////////////////////////////////////////////////////////////////////////// + +// Author: Andy Rushton +// Copyright: (c) Southampton University 1999-2004 +// (c) Andy Rushton 2004-2009 +// License: BSD License, see ../docs/license.html + +//////////////////////////////////////////////////////////////////////////////// + +#include "persistent_cstring.hpp" +#include "persistent_int.hpp" +#include + +//////////////////////////////////////////////////////////////////////////////// +// Null-terminated char arrays +// Format: address [ size data ] + +void stlplus::dump_cstring(stlplus::dump_context& context, const char* data) throw(stlplus::persistent_dump_failed) +{ + // register the address and get the magic key for it + std::pair mapping = context.pointer_map(data); + stlplus::dump_unsigned(context,mapping.second); + // if the address is null, then that is all that we need to do + // however, if it is non-null and this is the first sight of the address, dump the contents + if (data && !mapping.first) + { + unsigned size = strlen(data); + stlplus::dump_unsigned(context,size); + for (unsigned i = 0; i < size; i++) + stlplus::dump_char(context,data[i]); + } +} + +void stlplus::restore_cstring(restore_context& context, char*& data) throw(stlplus::persistent_restore_failed) +{ + // destroy any previous contents + if (data) + { + delete[] data; + data = 0; + } + // get the magic key + unsigned magic = 0; + stlplus::restore_unsigned(context,magic); + // now lookup the magic key to see if this pointer has already been restored + // null pointers are always flagged as already restored + std::pair address = context.pointer_map(magic); + if (!address.first) + { + // this pointer has never been seen before and is non-null + // restore the string + unsigned size = 0; + stlplus::restore_unsigned(context,size); + data = new char[size+1]; + for (unsigned i = 0; i < size; i++) + stlplus::restore_char(context,data[i]); + data[size] = '\0'; + // add this pointer to the set of already seen objects + context.pointer_add(magic,data); + } +} + +////////////////////////////////////////////////////////////////////////////////