MyGUI 3.0.1

MyGUI_Guid.cpp

Go to the documentation of this file.
00001 
00007 /*
00008     This file is part of MyGUI.
00009 
00010     MyGUI is free software: you can redistribute it and/or modify
00011     it under the terms of the GNU Lesser General Public License as published by
00012     the Free Software Foundation, either version 3 of the License, or
00013     (at your option) any later version.
00014 
00015     MyGUI is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018     GNU Lesser General Public License for more details.
00019 
00020     You should have received a copy of the GNU Lesser General Public License
00021     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00022 */
00023 #include "MyGUI_Precompiled.h"
00024 #include "MyGUI_Guid.h"
00025 #if   MYGUI_PLATFORM == MYGUI_PLATFORM_LINUX || MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
00026 #include <uuid/uuid.h>
00027 #elif MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
00028 #include <objbase.h>
00029 #endif
00030 
00031 namespace MyGUI
00032 {
00033     const char Guid::convert_hex[64] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 64, 64, 64, 64, 64, 64,
00034                                         64, 10, 11, 12, 13, 14, 15, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00035                                         64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00036                                         64, 10, 11, 12, 13, 14, 15, 64, 64, 64, 64, 64, 64, 64, 64, 64
00037                                        };
00038 
00039     Guid Guid::parse(const std::string& _value)
00040     {
00041         Guid ret;
00042         size_t start=0;
00043         // формат со скобками { ... }
00044         if (_value.size() == 38)
00045         {
00046             start ++;
00047 #if MYGUI_DEBUG_MODE == 1
00048             if ((_value[0] != '{') || (_value[37] != '}'))
00049             {
00050                 return ret;
00051             }
00052 #endif
00053         }
00054         // формат без скобок ...
00055         else if (_value.size() != 36)
00056         {
00057             return ret;
00058         }
00059 
00060 #if MYGUI_DEBUG_MODE == 1
00061         if ((_value[start + 8] != '-') || (_value[start + 13] != '-') || (_value[start + 18] != '-') || (_value[start + 23] != '-'))
00062         {
00063             return ret;
00064         }
00065 #endif
00066 
00067 #define MYGUI_CONVERT_HEX(value) ((convert_hex[ ((value) - 48) & 0x3F]) & 0x3F)
00068 
00069 #if MYGUI_DEBUG_MODE == 1
00070     #define MYGUI_CHECK_CONVERT_HEX(value) \
00071         { \
00072             char tmp = ((value) - 48); \
00073             if ((tmp > 63) || (tmp < 0)) \
00074             { \
00075                 /*MYGUI_LOG(Error, "error parse guid'" << _value << "'");*/ \
00076                 return Guid(); \
00077             } \
00078             tmp = convert_hex[(int)tmp]; \
00079             if (tmp > 63) \
00080             { \
00081                 /*MYGUI_LOG(Error, "error parse guid'" << _value << "'");*/ \
00082                 return Guid(); \
00083             } \
00084         }
00085 #else
00086     #define MYGUI_CHECK_CONVERT_HEX(value)
00087 #endif
00088 
00089 
00090         size_t count = 8;
00091         size_t pos = start;
00092         while (count > 0)
00093         {
00094             MYGUI_CHECK_CONVERT_HEX(_value[pos]);
00095             ret.original.data1 <<= 4;
00096             ret.original.data1 += MYGUI_CONVERT_HEX(_value[pos]);
00097             count --;
00098             pos ++;
00099         }
00100 
00101         count = 4;
00102         pos ++;
00103         while (count > 0)
00104         {
00105             MYGUI_CHECK_CONVERT_HEX(_value[pos]);
00106             ret.original.data2 <<= 4;
00107             ret.original.data2 += MYGUI_CONVERT_HEX(_value[pos]);
00108             count --;
00109             pos ++;
00110         }
00111 
00112         count = 4;
00113         pos ++;
00114         while (count > 0)
00115         {
00116             MYGUI_CHECK_CONVERT_HEX(_value[pos]);
00117             ret.original.data3 <<= 4;
00118             ret.original.data3 += MYGUI_CONVERT_HEX(_value[pos]);
00119             count --;
00120             pos ++;
00121         }
00122 
00123         count = 2; // здесь по два байта парсится
00124         pos ++;
00125         size_t num = 0;
00126         while (count > 0)
00127         {
00128             MYGUI_CHECK_CONVERT_HEX(_value[pos]);
00129             ret.original.data4[num] = MYGUI_CONVERT_HEX(_value[pos++]) << 4;
00130             MYGUI_CHECK_CONVERT_HEX(_value[pos]);
00131             ret.original.data4[num++] += MYGUI_CONVERT_HEX(_value[pos++]);
00132             count --;
00133         }
00134 
00135         count = 6; // здесь по два байта парсится
00136         pos ++;
00137         while (count > 0)
00138         {
00139             MYGUI_CHECK_CONVERT_HEX(_value[pos]);
00140             ret.original.data4[num] = MYGUI_CONVERT_HEX(_value[pos++]) << 4;
00141             MYGUI_CHECK_CONVERT_HEX(_value[pos]);
00142             ret.original.data4[num++] += MYGUI_CONVERT_HEX(_value[pos++]);
00143             count --;
00144         }
00145 
00146 #undef MYGUI_CHECK_CONVERT_HEX
00147 #undef MYGUI_CONVERT_HEX
00148 
00149         return ret;
00150     }
00151 
00152     std::string Guid::print() const
00153     {
00154         const size_t SIZE = 39;
00155         char buff[SIZE];
00156 
00157         sprintf(buff, "{%.8X-%.4X-%.4X-%.2X%.2X-%.2X%.2X%.2X%.2X%.2X%.2X}", (int)(original.data1), (int)(original.data2), (int)(original.data3),
00158             (int)(original.data4[0]), (int)(original.data4[1]),
00159             (int)(original.data4[2]), (int)(original.data4[3]), (int)(original.data4[4]), (int)(original.data4[5]), (int)(original.data4[6]), (int)(original.data4[7])
00160             );
00161 
00162         return buff;
00163     }
00164 
00165     Guid Guid::generate()
00166     {
00167         Guid ret;
00168 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
00169         HRESULT result = CoCreateGuid((GUID*)&ret.original.data1);
00170         MYGUI_ASSERT(S_OK == result, "Error generate GUID");
00171 #elif MYGUI_PLATFORM == MYGUI_PLATFORM_LINUX
00172         uuid_generate(ret.vec._data1);
00173 #else
00174         //FIXME
00175         uuid_generate(ret.vec._data1);// or what else?
00176 #endif
00177         return ret;
00178     }
00179 
00180 } // namespace MyGUI
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines