Panda3D

windowsGuid.cxx

00001 // Filename: windowsGuid.cxx
00002 // Created by:  drose (03Oct04)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "windowsGuid.h"
00016 #include "pnotify.h"
00017 
00018 #include <stdio.h>  // for sscanf, sprintf
00019 
00020 ////////////////////////////////////////////////////////////////////
00021 //     Function: WindowsGuid::parse_string
00022 //       Access: Public
00023 //  Description: Parses the hex representation in the indicated string
00024 //               and stores it in the WindowsGuid object.  Returns
00025 //               true if successful, false if the string
00026 //               representation is malformed.
00027 ////////////////////////////////////////////////////////////////////
00028 bool WindowsGuid::
00029 parse_string(const string &str) {
00030   unsigned long data1;
00031   unsigned int data2, data3;
00032   unsigned int b1, b2, b3, b4, b5, b6, b7, b8;
00033   int result = sscanf(str.c_str(),
00034                       "%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
00035                       &data1, &data2, &data3,
00036                       &b1, &b2, &b3, &b4, &b5, &b6, &b7, &b8);
00037   if (result != 11) {
00038     return false;
00039   }
00040 
00041   _data1 = data1;
00042   _data2 = data2;
00043   _data3 = data3;
00044   _b1 = b1;
00045   _b2 = b2;
00046   _b3 = b3;
00047   _b4 = b4;
00048   _b5 = b5;
00049   _b6 = b6;
00050   _b7 = b7;
00051   _b8 = b8;
00052 
00053   return true;
00054 }
00055 
00056 ////////////////////////////////////////////////////////////////////
00057 //     Function: WindowsGuid::format_string
00058 //       Access: Public
00059 //  Description: Returns a hex representation of the GUID.
00060 ////////////////////////////////////////////////////////////////////
00061 string WindowsGuid::
00062 format_string() const {
00063   static const int buf_length = 128;  // Actually, we only need 36 + 1 == 37.
00064   char buffer[buf_length];
00065   sprintf(buffer, 
00066           "%08lx-%04hx-%04hx-%02x%02x-%02x%02x%02x%02x%02x%02x",
00067           _data1, _data2, _data3,
00068           _b1, _b2, _b3, _b4, _b5, _b6, _b7, _b8);
00069   nassertr((int)strlen(buffer) < buf_length, string());
00070 
00071   return string(buffer);
00072 }
00073 
00074 ////////////////////////////////////////////////////////////////////
00075 //     Function: WindowsGuid::output
00076 //       Access: Public
00077 //  Description: Outputs a hex representation of the GUID.
00078 ////////////////////////////////////////////////////////////////////
00079 void WindowsGuid::
00080 output(ostream &out) const {
00081   out << format_string();
00082 }
 All Classes Functions Variables Enumerations