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

Last change on this file since 236 was 230, checked in by cxsong, 18 years ago

added xmlString

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, len);
18
19        return buf;
20}
21
22//
23// actual function that does the serialization
24//
25// Input:
26//      buf: pointer to valid memory
27//      len: number of bytes in buf
28// Return:
29//      error code for null pointer (defined in "util.h")
30//      success
31//
32RP_ERROR
33RpNode3d::serialize(char* buf, int buflen)
34{
35        if (buf == NULL || buflen < 3*((signed)sizeof(int))) {
36                RpAppendErr("RpElement::deserialize: null buffer");
37                RpPrintErr();
38                return RP_ERR_NULL_PTR;
39        }
40
41        char * ptr = buf;
42        memcpy((void*)ptr, (void *)&m_x, sizeof(int));
43        ptr += sizeof(int);
44        memcpy((void*)ptr, (void *)&m_y, sizeof(int));
45        ptr += sizeof(int);
46        memcpy((void*)ptr, (void *)&m_z, sizeof(int));
47
48        return RP_SUCCESS;
49}
50
51RP_ERROR
52RpNode3d::deserialize(const char* buf)
53{
54        if (buf == NULL) {
55                RpAppendErr("RpElement::deserialize: null buffer");
56                RpPrintErr();
57                return RP_ERR_NULL_PTR;
58        }
59
60        char * ptr = (char*)buf;
61        memcpy((void*)&m_x, (void *)ptr, sizeof(int));
62        ptr += sizeof(int);
63        memcpy((void*)&m_y, (void *)ptr, sizeof(int));
64        ptr += sizeof(int);
65        memcpy((void*)&m_z, (void *)ptr, sizeof(int));
66       
67        return RP_SUCCESS;
68}
69
70void
71RpNode3d::xmlString(std::string& str)
72{
73        char cstr[512];
74        sprintf(cstr, "<node id=\"%d\"> %d %d %d</node>\n",m_id,m_x,m_y,m_z);
75        str.append(cstr);
76}
77
78void
79RpNode3d::print()
80{
81        printf("<node id=\"%d\"> %d %d %d</node>\n",m_id,m_x,m_y,m_z);
82}
83
Note: See TracBrowser for help on using the repository browser.