VirtualBox

source: vbox/trunk/src/VBox/Main/include/cfgldrhlp.h@ 1313

Last change on this file since 1313 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 KB
Line 
1/** @file
2 *
3 * CFGLDRHLP - Configuration Loader Helpers
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#ifndef __VBox_cfgldrhlp_h__
23#define __VBox_cfgldrhlp_h__
24
25#include <iprt/alloc.h>
26#include <VBox/cdefs.h>
27#include <VBox/types.h>
28#include <iprt/string.h>
29#include <VBox/err.h>
30
31#include <string.h>
32
33#ifndef IN_RING3
34# error "There are no configuration loader helper APIs available in Ring-0 Context!"
35#else /* IN_RING3 */
36
37
38//@@TODO (dmik): remove
39//__BEGIN_DECLS
40
41int cfgldrhlp_strtouint32(PRTUCS2 puszValue, uint32_t *pvalue);
42int cfgldrhlp_strtouint64(PRTUCS2 puszValue, uint64_t *pvalue);
43int cfgldrhlp_uint32tostr(uint32_t ulValue, char *pszValue);
44int cfgldrhlp_uint64tostr(uint64_t ullValue, char *pszValue);
45
46// converts an UCS2 string of decimal digits to an unsigned integral value.
47// UT must be an unsigned integral type.
48template<class UT> int cfgldrhlp_ustr_to_uinteger (PRTUCS2 puszValue, UT *pValue)
49{
50 if (!puszValue || !pValue)
51 {
52 return VERR_INVALID_POINTER;
53 }
54 if (!(*puszValue))
55 {
56 return VERR_CFG_INVALID_FORMAT;
57 }
58
59 PRTUCS2 p = puszValue;
60 UT v = 0;
61
62 int rc = VINF_SUCCESS;
63
64 while (*p)
65 {
66 if( *p >= '0' && *p <= '9' )
67 {
68 register UT vt = v * 10 + (*p - '0');
69 if (vt < v)
70 {
71 rc = VERR_CFG_INVALID_FORMAT;
72 break;
73 }
74 v = vt;
75 }
76 else
77 {
78 rc = VERR_CFG_INVALID_FORMAT;
79 break;
80 }
81 p++;
82 }
83
84 if (VBOX_SUCCESS(rc))
85 {
86 *pValue = v;
87 }
88
89 return rc;
90}
91
92// converts an UCS2 string of decimal digits to a signed integral value.
93// T must be a signed integral type, UT msut be a corresponding unsigned type.
94template<class T, class UT> int cfgldrhlp_ustr_to_integer (PRTUCS2 puszValue, T *pValue )
95{
96 if (!puszValue || !pValue)
97 {
98 return VERR_INVALID_POINTER;
99 }
100 if (!(*puszValue))
101 {
102 return VERR_CFG_INVALID_FORMAT;
103 }
104
105 PRTUCS2 p = puszValue;
106 bool negative = false;
107
108 if (*p == '-')
109 {
110 negative = true;
111 p++;
112 }
113 else if (*p == '+')
114 {
115 negative = false;
116 p++;
117 }
118
119 UT uv;
120
121 int rc = cfgldrhlp_ustr_to_uinteger<UT> (p, &uv);
122
123 if (VBOX_SUCCESS(rc))
124 {
125 register T iv = (T) uv;
126 if (iv < 0 )
127 {
128 if (negative && !(iv << 1))
129 {
130 *pValue = iv;
131 }
132 else
133 {
134 rc = VERR_CFG_INVALID_FORMAT;
135 }
136 }
137 else
138 {
139 *pValue = negative ? -iv : iv;
140 }
141 }
142
143 return rc;
144}
145
146// converts an unsigned integral value to an UCS2 string of decimal digits.
147// UT must be an unsigned integral type. the pointer to the resulting string
148// is returned via ppuszValue. if the function returns VBOX_SUCCESS
149// the string must be freed by the caller using cfgldrhlp_release_ustr().
150// withPlus prepends the string with a plus char.
151template<class UT> int cfgldrhlp_uinteger_to_ustr (
152 UT value, PRTUCS2 *ppuszValue, bool withPlus = false
153)
154{
155 if (!ppuszValue)
156 {
157 return VERR_INVALID_POINTER;
158 }
159
160 // calculated for octal, which is sufficient for decimal
161 const int NumDigitsPlusTwo = ((sizeof(UT) * 8) + 2) / 3 + 2;
162
163 PRTUCS2 puszValue = (PRTUCS2) RTMemTmpAllocZ (sizeof(RTUCS2) * NumDigitsPlusTwo);
164 if (!puszValue)
165 {
166 return VERR_NO_MEMORY;
167 }
168
169 PRTUCS2 p = puszValue + NumDigitsPlusTwo;
170 *(--p) = 0; // end of string
171
172 do {
173 *(--p) = '0' + (value % 10);
174 value /= 10;
175 }
176 while (value);
177
178 memmove(
179 puszValue + (withPlus ? 1 : 0), p,
180 sizeof(RTUCS2) * (puszValue + NumDigitsPlusTwo - p)
181 );
182
183 if (withPlus)
184 {
185 *puszValue = '+';
186 }
187
188 *ppuszValue = puszValue;
189
190 return VINF_SUCCESS;
191}
192
193// converts a signed integral value to an UCS2 string of decimal digits.
194// T must be a signed integral type, UT msut be a corresponding unsigned type.
195// the pointer to the resulting string is returned via ppuszValue.
196// if the function returns VBOX_SUCCESS the string must be freed by the
197// caller using cfgldrhlp_release_ustr().
198template<class T, class UT> int cfgldrhlp_integer_to_ustr (T value, PRTUCS2 *ppuszValue)
199{
200 UT uv = (UT) value;
201
202 bool negative = false;
203
204 if ( value < 0 )
205 {
206 negative = true;
207 if ( (((UT) value) << 1) )
208 {
209 uv = -value;
210 }
211 }
212
213 PRTUCS2 puszValue = 0;
214
215 int rc = cfgldrhlp_uinteger_to_ustr<UT> (uv, &puszValue, negative);
216
217 if (VBOX_SUCCESS(rc))
218 {
219 if (negative)
220 {
221 *puszValue = '-';
222 }
223 *ppuszValue = puszValue;
224 }
225
226 return rc;
227}
228
229inline void cfgldrhlp_release_ustr (PRTUCS2 pucszValue)
230{
231}
232
233int cfgldrhlp_strtobin(PCRTUCS2 puszValue, void *pvValue, unsigned cbValue, unsigned *pcbValue);
234int cfgldrhlp_bintostr(const void *pvValue, unsigned cbValue, char **ppszValue);
235void cfgldrhlp_releasestr(char *pszValue);
236
237#endif /* IN_RING3 */
238
239//@@TODO (dmik): remove
240//__END_DECLS
241
242#endif /* __VBox_cfgldrhlp_h__ */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette