VirtualBox

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

Last change on this file since 4071 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

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