]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/uuid.cpp
cleanup - more reformatting; fixed bug introduced in prior commit
[wxWidgets.git] / src / msw / ole / uuid.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: ole/uuid.cpp
3 // Purpose: implements Uuid class, see uuid.h for details
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 12.09.96
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // Declarations
14 // ============================================================================
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #if defined(__BORLANDC__)
20 #pragma hdrstop
21 #endif
22
23 #include "wx/setup.h"
24
25 #if wxUSE_OLE && ( wxUSE_DRAG_AND_DROP || (defined(__WXDEBUG__) && wxUSE_DATAOBJ) )
26
27 // standard headers
28 #if wxCHECK_W32API_VERSION( 1, 0 )
29 #include "wx/msw/wrapwin.h"
30 #endif
31 #include <rpc.h> // UUID related functions
32
33 #include "wx/msw/ole/uuid.h"
34
35
36
37 // ============================================================================
38 // Implementation
39 // ============================================================================
40
41 // length of UUID in C format
42 #define UUID_CSTRLEN 100 // real length is 66
43
44 // copy ctor
45 Uuid::Uuid(const Uuid& uuid)
46 {
47 // bitwise copy Ok for UUIDs
48 m_uuid = uuid.m_uuid;
49
50 // force the string to be allocated by RPC
51 // (we free it later with RpcStringFree)
52 #ifdef _UNICODE
53 UuidToString(&m_uuid, (unsigned short **)&m_pszUuid);
54 #else
55 UuidToString(&m_uuid, &m_pszUuid);
56 #endif
57
58 // allocate new buffer
59 m_pszCForm = new wxChar[UUID_CSTRLEN];
60 // and fill it
61 memcpy(m_pszCForm, uuid.m_pszCForm, UUID_CSTRLEN*sizeof(wxChar));
62 }
63
64 // assignment operator
65 Uuid& Uuid::operator=(const Uuid& uuid)
66 {
67 m_uuid = uuid.m_uuid;
68
69 // force the string to be allocated by RPC
70 // (we free it later with RpcStringFree)
71 #ifdef _UNICODE
72 UuidToString(&m_uuid, (unsigned short **)&m_pszUuid);
73 #else
74 UuidToString(&m_uuid, &m_pszUuid);
75 #endif
76
77 // allocate new buffer if not done yet
78 if ( !m_pszCForm )
79 m_pszCForm = new wxChar[UUID_CSTRLEN];
80
81 // and fill it
82 memcpy(m_pszCForm, uuid.m_pszCForm, UUID_CSTRLEN*sizeof(wxChar));
83
84 return *this;
85 }
86
87 // dtor
88 Uuid::~Uuid()
89 {
90 // this string must be allocated by RPC!
91 // (otherwise you get a debug breakpoint deep inside RPC DLL)
92 if ( m_pszUuid )
93 #ifdef _UNICODE
94 RpcStringFree((unsigned short **)&m_pszUuid);
95 #else
96 RpcStringFree(&m_pszUuid);
97 #endif
98
99 // perhaps we should just use a static buffer and not bother
100 // with new and delete?
101 if ( m_pszCForm )
102 delete [] m_pszCForm;
103 }
104
105 // update string representation of new UUID
106 void Uuid::Set(const UUID &uuid)
107 {
108 m_uuid = uuid;
109
110 // get string representation
111 #ifdef _UNICODE
112 UuidToString(&m_uuid, (unsigned short **)&m_pszUuid);
113 #else
114 UuidToString(&m_uuid, &m_pszUuid);
115 #endif
116
117 // cache UUID in C format
118 UuidToCForm();
119 }
120
121 // create a new UUID
122 void Uuid::Create()
123 {
124 UUID uuid;
125
126 // can't fail
127 UuidCreate(&uuid);
128
129 Set(uuid);
130 }
131
132 // set the value
133 bool Uuid::Set(const wxChar *pc)
134 {
135 // get UUID from string
136 #ifdef _UNICODE
137 if ( UuidFromString((unsigned short *)pc, &m_uuid) != RPC_S_OK)
138 #else
139 if ( UuidFromString((wxUChar *)pc, &m_uuid) != RPC_S_OK)
140 #endif
141 // failed: probably invalid string
142 return false;
143
144 // transform it back to string to normalize it
145 #ifdef _UNICODE
146 UuidToString(&m_uuid, (unsigned short **)&m_pszUuid);
147 #else
148 UuidToString(&m_uuid, &m_pszUuid);
149 #endif
150
151 // update m_pszCForm
152 UuidToCForm();
153
154 return true;
155 }
156
157 // stores m_uuid in m_pszCForm in a format required by
158 // DEFINE_GUID macro: i.e. something like
159 // 0x7D8A2281L,0x4C61,0x11D0,0xBA,0xBD,0x00,0x00,0xC0,0x18,0xBA,0x27
160 // m_pszUuid is of the form (no, it's not quite the same UUID :-)
161 // 6aadc650-67b0-11d0-bac8-0000c018ba27
162 void Uuid::UuidToCForm()
163 {
164 if ( m_pszCForm == NULL )
165 m_pszCForm = new wxChar[UUID_CSTRLEN];
166
167 wsprintf(m_pszCForm, wxT("0x%8.8X,0x%4.4X,0x%4.4X,0x%2.2X,0x2.2%X,0x2.2%X,0x2.2%X,0x2.2%X,0x2.2%X,0x2.2%X,0x2.2%X"),
168 m_uuid.Data1, m_uuid.Data2, m_uuid.Data3,
169 m_uuid.Data4[0], m_uuid.Data4[1], m_uuid.Data4[2], m_uuid.Data4[3],
170 m_uuid.Data4[4], m_uuid.Data4[5], m_uuid.Data4[6], m_uuid.Data4[7]);
171 }
172
173 #endif
174 // wxUSE_DRAG_AND_DROP