-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLBPRh_XMLUserData.cpp
109 lines (85 loc) · 2.27 KB
/
LBPRh_XMLUserData.cpp
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
#include "stdafx.h"
#ifdef LBPRHLIB
#include "LBPRh_XMLUserData.h"
//#include "LBPRhUtilities.h"
#include "LBP_XML.h"
#include "LBP_UTF.h"
#include "ILBPRhWrapper.h"
#ifdef RHINO_V6_READY
#define ON_BOOL32 bool
#endif
ON_BOOL32 CLBPRh_XMLUserData::Read(ON_BinaryArchive& archive)
{
// Read the version number.
int iVersion = 0;
if (!archive.ReadInt(&iVersion))
return FALSE;
// Cannot load user data with a version number greater than mine.
if (iVersion > Version())
{
ReportVersionError();
return FALSE;
}
// Clear any existing XML data.
XMLRootForWrite().Clear();
if (1 == iVersion)
{
// Original version.
// LBPRH_TRACE("RHLIB: Begin version 1 (uncompressed) loading\n");
// Read the archive into a string.
ON_wString s;
if (!archive.ReadString(s))
return FALSE;
// Read the string into the XML root.
XMLRootForWrite().ReadFromStreamNoThrow(s);
}
else
{
// UTF8 version.
// LBPRH_TRACE("RHLIB: Begin version 2 (UTF8) loading\n");
// Read the length of the UTF8 buffer from the archive.
int iLength = 0;
if (!archive.ReadInt(&iLength))
return FALSE;
// Read the UTF8 buffer from the archive.
BYTE* pBuffer = new BYTE[iLength+1];
bool bOk = archive.ReadChar(iLength, pBuffer);
if (bOk)
{
// Convert the UTF8 data to UTF16 and read it into the root node.
CLBP_UTF u;
pBuffer[iLength] = 0; // Terminator.
u.SetUTF8(pBuffer);
XMLRootForWrite().ReadFromStreamNoThrow(u.AsWide());
}
delete[] pBuffer;
if (!bOk)
return FALSE;
}
// LBPRH_TRACE("RHLIB: Loading complete\n");
return TRUE;
}
ON_BOOL32 CLBPRh_XMLUserData::Write(ON_BinaryArchive& archive) const
{
// LBPRH_TRACE("RHLIB: Begin version 2 (UTF8) saving\n");
// Write the version number to the archive.
if (!archive.WriteInt(Version()))
return FALSE;
// Convert the XML string to UTF8.
CLBP_UTF u;
u.SetString(XMLRootForRead().String());
const BYTE* pUTF8 = u.UTF8();
// Write the length of the UTF8 buffer to the archive.
const int iLength = (int)(strlen(reinterpret_cast<const char*>(pUTF8)));
if (!archive.WriteInt(iLength))
return FALSE;
// Write the UTF8 buffer to the archive.
if (!archive.WriteChar(iLength, pUTF8))
return FALSE;
// LBPRH_TRACE("RHLIB: Saving complete\n");
return TRUE;
}
#ifdef RHINO_V6_READY
#undef ON_BOOL32
#endif
#endif