root/branches/newmole/source/container_classes.cpp

Revision 1780, 2.0 kB (checked in by rjrw, 11 months ago)

Merged from trunk r1738:1779

  • Property svn:eol-style set to native
Line 
1/* This file is part of Cloudy and is copyright (C)1978-2008 by Gary J. Ferland and
2 * others.  For conditions of distribution and use see copyright notice in license.txt */
3
4#include "cddefines.h"
5
6/** dump the array to a file in binary format; the file must
7 *  already have been opened prior to calling this method */
8void do_dump_state(const void* buf, size_t nelem, size_t size, FILE* out, int32 magic)
9{
10        DEBUG_ENTRY( "do_dump_state()" );
11
12        bool lgErr = ( fwrite( &magic, sizeof(int32), 1, out ) != 1 );
13        int32 help = (int32)sizeof(size_t);
14        lgErr = lgErr || ( fwrite( &help, sizeof(int32), 1, out ) != 1 );
15        lgErr = lgErr || ( fwrite( &size, sizeof(size_t), 1, out ) != 1 );
16        lgErr = lgErr || ( fwrite( buf, size, nelem, out ) != nelem );
17        if( lgErr )
18        {
19                fprintf( ioQQQ, " I/O error while dumping state!\n" );
20                cdEXIT(EXIT_FAILURE);
21        }
22}
23
24/** restore the array from a file in binary format; the file must already
25 *  have been opened prior to calling this method and the array must already
26 *  have been allocated in exactly the same way as when it was dumped; some
27 *  checks are performed, but not every error is excluded */
28void do_restore_state(void* buf, size_t nelem, size_t size, FILE *in, int32 magic)
29{
30        DEBUG_ENTRY( "do_restore_state()" );
31
32        int32 help = 0;
33        size_t help2 = 0;
34        bool lgErr = ( fread( &help, sizeof(int32), 1, in ) != 1 );
35        // this checks for correct version and prevents mixing up old style and new style data
36        // it also prevents mixing up data from big-endian and little-endian machines.
37        lgErr = lgErr || ( help != magic );
38        lgErr = lgErr || ( fread( &help, sizeof(int32), 1, in ) != 1 );
39        // this prevents mixing up data from 32-bit and 64-bit systems
40        lgErr = lgErr || ( help != (int32)sizeof(size_t) );
41        lgErr = lgErr || ( fread( &help2, sizeof(size_t), 1, in ) != 1 );
42        // this may guard against reading an older, incompatible version of the array
43        lgErr = lgErr || ( help2 != size );
44        lgErr = lgErr || ( fread( buf, size, nelem, in ) != nelem );
45        if( lgErr )
46        {
47                fprintf( ioQQQ, " Error while restoring state!\n" );
48                cdEXIT(EXIT_FAILURE);
49        }
50}
Note: See TracBrowser for help on using the browser.