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 | // |
---|
11 | char* |
---|
12 | RpNode3d::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 | // |
---|
32 | RP_ERROR |
---|
33 | RpNode3d::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 | |
---|
51 | RP_ERROR |
---|
52 | RpNode3d::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 | |
---|
70 | void |
---|
71 | RpNode3d::print() |
---|
72 | { |
---|
73 | printf("<node id=\"%d\"> %d %d %d</node>\n",m_id,m_x,m_y,m_z); |
---|
74 | } |
---|
75 | |
---|