Panda3D
windowsGuid.cxx
1 // Filename: windowsGuid.cxx
2 // Created by: drose (03Oct04)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "windowsGuid.h"
16 #include "pnotify.h"
17 
18 #include <stdio.h> // for sscanf, sprintf
19 
20 ////////////////////////////////////////////////////////////////////
21 // Function: WindowsGuid::parse_string
22 // Access: Public
23 // Description: Parses the hex representation in the indicated string
24 // and stores it in the WindowsGuid object. Returns
25 // true if successful, false if the string
26 // representation is malformed.
27 ////////////////////////////////////////////////////////////////////
28 bool WindowsGuid::
29 parse_string(const string &str) {
30  unsigned long data1;
31  unsigned int data2, data3;
32  unsigned int b1, b2, b3, b4, b5, b6, b7, b8;
33  int result = sscanf(str.c_str(),
34  "%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
35  &data1, &data2, &data3,
36  &b1, &b2, &b3, &b4, &b5, &b6, &b7, &b8);
37  if (result != 11) {
38  return false;
39  }
40 
41  _data1 = data1;
42  _data2 = data2;
43  _data3 = data3;
44  _b1 = b1;
45  _b2 = b2;
46  _b3 = b3;
47  _b4 = b4;
48  _b5 = b5;
49  _b6 = b6;
50  _b7 = b7;
51  _b8 = b8;
52 
53  return true;
54 }
55 
56 ////////////////////////////////////////////////////////////////////
57 // Function: WindowsGuid::format_string
58 // Access: Public
59 // Description: Returns a hex representation of the GUID.
60 ////////////////////////////////////////////////////////////////////
61 string WindowsGuid::
62 format_string() const {
63  static const int buf_length = 128; // Actually, we only need 36 + 1 == 37.
64  char buffer[buf_length];
65  sprintf(buffer,
66  "%08lx-%04hx-%04hx-%02x%02x-%02x%02x%02x%02x%02x%02x",
67  _data1, _data2, _data3,
68  _b1, _b2, _b3, _b4, _b5, _b6, _b7, _b8);
69  nassertr((int)strlen(buffer) < buf_length, string());
70 
71  return string(buffer);
72 }
73 
74 ////////////////////////////////////////////////////////////////////
75 // Function: WindowsGuid::output
76 // Access: Public
77 // Description: Outputs a hex representation of the GUID.
78 ////////////////////////////////////////////////////////////////////
79 void WindowsGuid::
80 output(ostream &out) const {
81  out << format_string();
82 }
string format_string() const
Returns a hex representation of the GUID.
Definition: windowsGuid.cxx:62
bool parse_string(const string &str)
Parses the hex representation in the indicated string and stores it in the WindowsGuid object...
Definition: windowsGuid.cxx:29
void output(ostream &out) const
Outputs a hex representation of the GUID.
Definition: windowsGuid.cxx:80