source: trunk/src/mesh/node3d.cc @ 829

Last change on this file since 829 was 249, checked in by cxsong, 18 years ago

modified serialization functions

File size: 1.6 KB
Line 
1#include "node3d.h"
2
3//
4// - serialize node (x y z)
5// - store byte stream in buf
6//
7// Return:
8//      NULL: fail to allocate memory
9//      valid pointer: point to buffer holding the byte stream
10//
11char*
12RpNode3d::serialize()
13{
14        int len = 3 * sizeof(int);
15
16        char * buf = new char[len];
17        serialize(buf);
18
19        return buf;
20}
21
22//
23// actual function that does the serialization
24//     serialization order:
25//      x y z x y z ...
26//
27// Input:
28//      buf: pointer to valid memory
29//      len: number of bytes in buf
30// Return:
31//      error code for null pointer (defined in "util.h")
32//      success
33//
34RP_ERROR
35RpNode3d::serialize(char* buf)
36{
37        if (buf == NULL) {
38                RpAppendErr("RpElement::deserialize: null buffer");
39                RpPrintErr();
40                return RP_ERR_NULL_PTR;
41        }
42
43        char * ptr = buf;
44        memcpy((void*)ptr, (void *)&m_x, sizeof(int));
45        ptr += sizeof(int);
46        memcpy((void*)ptr, (void *)&m_y, sizeof(int));
47        ptr += sizeof(int);
48        memcpy((void*)ptr, (void *)&m_z, sizeof(int));
49
50        return RP_SUCCESS;
51}
52
53RP_ERROR
54RpNode3d::deserialize(const char* buf)
55{
56        if (buf == NULL) {
57                RpAppendErr("RpElement::deserialize: null buffer");
58                RpPrintErr();
59                return RP_ERR_NULL_PTR;
60        }
61
62        char * ptr = (char*)buf;
63        memcpy((void*)&m_x, (void *)ptr, sizeof(int));
64        ptr += sizeof(int);
65        memcpy((void*)&m_y, (void *)ptr, sizeof(int));
66        ptr += sizeof(int);
67        memcpy((void*)&m_z, (void *)ptr, sizeof(int));
68       
69        return RP_SUCCESS;
70}
71
72void
73RpNode3d::xmlString(std::string& str)
74{
75        char cstr[512];
76        sprintf(cstr, "<node id=\"%d\"> %d %d %d</node>\n",m_id,m_x,m_y,m_z);
77        str.append(cstr);
78}
79
80void
81RpNode3d::print()
82{
83        printf("<node id=\"%d\"> %d %d %d</node>\n",m_id,m_x,m_y,m_z);
84}
85
Note: See TracBrowser for help on using the repository browser.