Uuid.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   (c) Jürgen Riegel (juergen.riegel@web.de) 2008                        *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 # ifdef FC_OS_WIN32
00028 #  include <Rpc.h>
00029 # endif
00030 #endif
00031 
00033 #include "Uuid.h"
00034 #include "Exception.h"
00035 #include "Interpreter.h"
00036 #include <CXX/Objects.hxx>
00037 
00038 
00039 using namespace Base;
00040 
00041 
00042 //**************************************************************************
00043 // Construction/Destruction
00044 
00049 Uuid::Uuid()
00050 {
00051     UuidStr = CreateUuid();
00052 }
00053 
00058 Uuid::~Uuid()
00059 {
00060 }
00061 
00062 //**************************************************************************
00063 //**************************************************************************
00064 // Get the UUID
00065 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00066 
00067 std::string Uuid::CreateUuid(void)
00068 {
00069 #ifdef FC_OS_WIN32
00070     RPC_STATUS rstat;
00071     UUID uuid;
00072     unsigned char *uuidStr;
00073 
00074     rstat = UuidCreate(&uuid);
00075     if (rstat != RPC_S_OK) throw Base::Exception("Cannot convert a unique Windows UUID to a string");
00076 
00077     rstat = UuidToString(&uuid, &uuidStr);
00078     if (rstat != RPC_S_OK) throw Base::Exception("Cannot convert a unique Windows UUID to a string");
00079 
00080     std::string Uuid((char *)uuidStr);
00081 
00082     /* convert it from rcp memory to our own */
00083     //container = nssUTF8_Duplicate(uuidStr, NULL);
00084     RpcStringFree(&uuidStr);
00085 #else
00086     // use Python's implemententation
00087     std::string Uuid;
00088     PyGILStateLocker lock;
00089     try {
00090         Py::Module module(PyImport_ImportModule("uuid"),true);
00091         Py::Callable method(module.getAttr("uuid4"));
00092         Py::Tuple arg;
00093         Py::Object guid = method.apply(arg);
00094         Uuid = guid.as_string();
00095     }
00096     catch (Py::Exception& e) {
00097         e.clear();
00098         throw Base::Exception("Creation of UUID failed");
00099     }
00100 #endif 
00101     return Uuid;
00102 }

Generated on Wed Nov 23 19:00:56 2011 for FreeCAD by  doxygen 1.6.1