VirtualBox

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

Last change on this file since 3662 was 2981, checked in by vboxsync, 18 years ago

InnoTek -> innotek: all the headers and comments.

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