1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.1 (2010/07/01)
#include "Wm5CorePCH.h"
#include "Wm5InStream.h"
#include "Wm5FileIO.h"
#include "Wm5Object.h"
using namespace Wm5;
//----------------------------------------------------------------------------
InStream::InStream ()
{
}
//----------------------------------------------------------------------------
InStream::~InStream ()
{
}
//----------------------------------------------------------------------------
int InStream::GetNumObjects () const
{
return (int)mTopLevel.size();
}
//----------------------------------------------------------------------------
Object* InStream::GetObjectAt (int i) const
{
if (0 <= i && i < (int)mTopLevel.size())
{
return mTopLevel[i];
}
return 0;
}
//----------------------------------------------------------------------------
void InStream::Load (int bufferSize, char* buffer, int mode)
{
// Create a reader for the incoming buffer.
mSource.Open(bufferSize, buffer, mode);
// Load the set of unique objects.
std::string topLevel("Top Level");
Object* object;
while (mSource.GetNumBytesProcessed() < mSource.GetNumBytesTotal())
{
// Read the "Top Level" name or RTTI name.
std::string name;
ReadString(name);
bool isTopLevel = (name == topLevel);
if (isTopLevel)
{
// Read the RTTI name.
ReadString(name);
}
// Get the factory function for the type of object about to be read.
// If the assert in Object::Find(name) fails, make sure you have
// called InitTerm::ExecuteInitializers() in your 'main' or 'WinMain'
// before you make calls to the engine code. The initialization
// allocates the factories map and populates it with the factory
// functions.
Object::FactoryFunction factory = Object::Find(name);
if (!factory)
{
// Cannot find the factory function. Make sure you have added
// WM5_REGISTER_STREAM(someclass) to the header file for each
// 'someclass'. This macro registers the factory function for
// the class.
assertion(false, "Cannot find factory for %s.\n", name.c_str());
return;
}
// Load the object from the source buffer.
object = (*factory)(*this);
// Keep track of all top level objects for use by the application.
if (isTopLevel)
{
mTopLevel.push_back(object);
}
}
// Link the objects. This process replaces the old addresses stored in
// any Object* data members with the new addresses of objects created
// during the current application run.
std::vector<Object*>::iterator iter = mOrdered.begin();
std::vector<Object*>::iterator end = mOrdered.end();
for (/**/; iter != end; ++iter)
{
(*iter)->Link(*this);
}
// Allow the objects to perform post-link semantics. In the paradigm of
// a loader-linker, the default constructor is used for creating an object
// into which data is loaded. The linker connects up the objects. The
// post-link function can do some of the work that the nondefault
// constructors do when creating objects during the application runtime.
iter = mOrdered.begin();
end = mOrdered.end();
for (/**/; iter != end; ++iter)
{
(*iter)->PostLink();
}
// Empty the containers in preparation for another save.
mLinked.clear();
mOrdered.clear();
mSource.Close();
}
//----------------------------------------------------------------------------
bool InStream::Load (const std::string& name, std::string version, int mode)
{
// Load the stream from disk to memory.
int bufferSize;
char* buffer;
if (!FileIO::Load(name, true, bufferSize, buffer))
{
return false;
}
// Get the file version.
int length = (int)version.length();
if (bufferSize < length)
{
// The file does not exist or is not large enough to store the
// version string.
delete1(buffer);
return false;
}
// Compare to the requested file version.
if (strncmp(version.c_str(), buffer, length) != 0)
{
// The version strings do not match.
delete1(buffer);
return false;
}
// Reconstruct the scene graph from the buffer.
bufferSize -= length;
Load(bufferSize, buffer + length, mode);
delete1(buffer);
return true;
}
//----------------------------------------------------------------------------
bool InStream::ReadBool (bool& datum)
{
unsigned int value;
if (!mSource.Read(sizeof(unsigned int), &value))
{
return false;
}
datum = (value != 0);
return true;
}
//----------------------------------------------------------------------------
bool InStream::ReadBoolVV (int numElements, bool* data)
{
if (numElements > 0)
{
for (int i = 0; i < numElements; ++i)
{
if (!ReadBool(data[i]))
{
return false;
}
}
}
return true;
}
//----------------------------------------------------------------------------
bool InStream::ReadBoolVR (int numElements, bool*& data)
{
if (numElements > 0)
{
data = new1<bool>(numElements);
for (int i = 0; i < numElements; ++i)
{
if (!ReadBool(data[i]))
{
return false;
}
}
}
else
{
data = 0;
}
return true;
}
//----------------------------------------------------------------------------
bool InStream::ReadBoolRR (int& numElements, bool*& data)
{
if (!mSource.Read(sizeof(int), &numElements))
{
return false;
}
if (numElements > 0)
{
data = new1<bool>(numElements);
for (int i = 0; i < numElements; ++i)
{
if (!ReadBool(data[i]))
{
return false;
}
}
}
else
{
data = 0;
}
return true;
}
//----------------------------------------------------------------------------
bool InStream::ReadString (std::string& datum)
{
int length;
mSource.Read(sizeof(int), &length);
if (length <= 0)
{
datum.clear();
return false;
}
// Strings are written in multiples of four bytes.
int padding = (length % 4);
if (padding > 0)
{
padding = 4 - padding;
}
const char* text = mSource.GetBuffer() + mSource.GetNumBytesProcessed();
datum.assign(text, length);
mSource.IncrementNumBytesProcessed(length + padding);
return true;
}
//----------------------------------------------------------------------------
bool InStream::ReadStringVV (int numElements, std::string* data)
{
if (numElements > 0)
{
for (int i = 0; i < numElements; ++i)
{
if (!ReadString(data[i]))
{
return false;
}
}
}
return true;
}
//----------------------------------------------------------------------------
bool InStream::ReadStringVR (int numElements, std::string*& data)
{
if (numElements > 0)
{
data = new1<std::string>(numElements);
for (int i = 0; i < numElements; ++i)
{
if (!ReadString(data[i]))
{
return false;
}
}
}
else
{
data = 0;
}
return true;
}
//----------------------------------------------------------------------------
bool InStream::ReadStringRR (int& numElements, std::string*& data)
{
if (!mSource.Read(sizeof(int), &numElements))
{
return false;
}
if (numElements > 0)
{
data = new1<std::string>(numElements);
for (int i = 0; i < numElements; ++i)
{
if (!ReadString(data[i]))
{
return false;
}
}
}
else
{
data = 0;
}
return true;
}
//----------------------------------------------------------------------------
int InStream::GetBytesRead () const
{
return mSource.GetNumBytesProcessed();
}
//----------------------------------------------------------------------------
void InStream::ReadUniqueID (Object* object)
{
unsigned int uniqueID;
if (mSource.Read(sizeof(unsigned int), &uniqueID))
{
mLinked.insert(std::make_pair((uint64_t)uniqueID, object));
mOrdered.push_back(object);
}
}
//----------------------------------------------------------------------------
|