]> git.stg.codes - stg.git/blob - stglibs/ibpp.lib/dbkey.cpp
Merge branch 'stg-2.409-radius'
[stg.git] / stglibs / ibpp.lib / dbkey.cpp
1 ///////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 //      File    : $Id: dbkey.cpp,v 1.1 2007/05/05 17:00:42 faust Exp $\r
4 //      Subject : IBPP, DBKey class implementation\r
5 //\r
6 ///////////////////////////////////////////////////////////////////////////////\r
7 //\r
8 //      (C) Copyright 2000-2006 T.I.P. Group S.A. and the IBPP Team (www.ibpp.org)\r
9 //\r
10 //      The contents of this file are subject to the IBPP License (the "License");\r
11 //      you may not use this file except in compliance with the License.  You may\r
12 //      obtain a copy of the License at http://www.ibpp.org or in the 'license.txt'\r
13 //      file which must have been distributed along with this file.\r
14 //\r
15 //      This software, distributed under the License, is distributed on an "AS IS"\r
16 //      basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the\r
17 //      License for the specific language governing rights and limitations\r
18 //      under the License.\r
19 //\r
20 ///////////////////////////////////////////////////////////////////////////////\r
21 //\r
22 //      COMMENTS\r
23 //      * Tabulations should be set every four characters when editing this file.\r
24 //\r
25 ///////////////////////////////////////////////////////////////////////////////\r
26 \r
27 #ifdef _MSC_VER\r
28 #pragma warning(disable: 4786 4996)\r
29 #ifndef _DEBUG\r
30 #pragma warning(disable: 4702)\r
31 #endif\r
32 #endif\r
33 \r
34 #include "_ibpp.h"\r
35 \r
36 #ifdef HAS_HDRSTOP\r
37 #pragma hdrstop\r
38 #endif\r
39 \r
40 #include <iostream>\r
41 #include <sstream>\r
42 #include <iomanip>\r
43 \r
44 using namespace ibpp_internals;\r
45 \r
46 //      Private implementation\r
47 \r
48 //      Public implementation\r
49 \r
50 void IBPP::DBKey::Clear()\r
51 {\r
52         mDBKey.erase();\r
53         mString.erase();\r
54 }\r
55 \r
56 void IBPP::DBKey::SetKey(const void* key, int size)\r
57 {\r
58         if (key == 0)\r
59                 throw LogicExceptionImpl("IBPP::DBKey::SetKey", _("Null DBKey reference detected."));\r
60         if (size <= 0 || ((size >> 3) << 3) != size)\r
61                 throw LogicExceptionImpl("IBPP::DBKey::SetKey", _("Invalid DBKey size."));\r
62 \r
63         mDBKey.assign((const char*)key, (size_t)size);\r
64         mString.erase();\r
65 }\r
66 \r
67 void IBPP::DBKey::GetKey(void* key, int size) const\r
68 {\r
69         if (mDBKey.empty())\r
70                 throw LogicExceptionImpl("IBPP::DBKey::GetKey", _("DBKey not assigned."));\r
71         if (key == 0)\r
72                 throw LogicExceptionImpl("IBPP::DBKey::GetKey", _("Null DBKey reference detected."));\r
73         if (size != (int)mDBKey.size())\r
74                 throw LogicExceptionImpl("IBPP::DBKey::GetKey", _("Incompatible DBKey size detected."));\r
75 \r
76         mDBKey.copy((char*)key, mDBKey.size());\r
77 }\r
78 \r
79 const char* IBPP::DBKey::AsString() const\r
80 {\r
81         if (mDBKey.empty())\r
82                 throw LogicExceptionImpl("IBPP::DBKey::GetString", _("DBKey not assigned."));\r
83 \r
84         if (mString.empty())\r
85         {\r
86                 std::ostringstream hexkey;\r
87                 hexkey.setf(std::ios::hex, std::ios::basefield);\r
88                 hexkey.setf(std::ios::uppercase);\r
89 \r
90                 const uint32_t* key = reinterpret_cast<const uint32_t*>(mDBKey.data());\r
91                 int n = (int)mDBKey.size() / 8;\r
92                 for (int i = 0; i < n; i++)\r
93                 {\r
94                         if (i != 0) hexkey<< "-";\r
95                         hexkey<< std::setw(4)<< key[i*2]<< ":";\r
96                         hexkey<< std::setw(8)<< key[i*2+1];\r
97                 }\r
98 \r
99                 mString = hexkey.str();\r
100         }\r
101 \r
102         return mString.c_str();\r
103 }\r
104 \r
105 IBPP::DBKey::DBKey(const DBKey& copied)\r
106 {\r
107         mDBKey = copied.mDBKey;\r
108         mString = copied.mString;\r
109 }\r
110 \r
111 IBPP::DBKey& IBPP::DBKey::operator=(const IBPP::DBKey& assigned)\r
112 {\r
113         mDBKey = assigned.mDBKey;\r
114         mString = assigned.mString;\r
115         return *this;\r
116 }\r
117 \r
118 //\r
119 //      EOF\r
120 //\r