VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/RTFileModeToFlags.cpp@ 47762

Last change on this file since 47762 was 47762, checked in by vboxsync, 11 years ago

IPRT: Added RTFileModeToFlags* APIs + testcase.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.5 KB
Line 
1/* $Id: RTFileModeToFlags.cpp 47762 2013-08-15 13:04:51Z vboxsync $ */
2/** @file
3 * IPRT - RTFileModeToFlags.
4 */
5
6/*
7 * Copyright (C) 2013 Oracle Corporation
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 (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include <iprt/assert.h>
32#include <iprt/err.h>
33#include <iprt/file.h>
34#include <iprt/string.h>
35#include "internal/iprt.h"
36
37
38RTR3DECL(int) RTFileModeToFlags(const char *pszMode, uint64_t *puMode)
39{
40 AssertPtrReturn(pszMode, VERR_INVALID_POINTER);
41 AssertPtrReturn(puMode, VERR_INVALID_POINTER);
42
43 int rc = VINF_SUCCESS;
44
45 const char *pszCur = pszMode;
46 uint64_t uMode = 0;
47 char chPrev = 0;
48
49 if (*pszCur == '\0')
50 return VERR_INVALID_PARAMETER;
51
52 while ( pszCur
53 && *pszCur != '\0')
54 {
55 bool fSkip = false;
56 switch (*pszCur)
57 {
58 /* Opens an existing file for reading and places the
59 * file pointer at the end of the file. */
60 case 'a':
61 if ((uMode & RTFILE_O_ACTION_MASK) == 0)
62 {
63 uMode |= RTFILE_O_OPEN
64 | RTFILE_O_READ
65 | RTFILE_O_APPEND;
66 }
67 else
68 rc = VERR_INVALID_PARAMETER;
69 break;
70
71 case 'b': /* Binary mode. */
72 /* Just skip as being valid. */
73 fSkip = true;
74 break;
75
76 /* Creates a file or open an existing one for
77 * writing only. The file pointer will be placed
78 * at the beginning of the file.*/
79 case 'c':
80 if ((uMode & RTFILE_O_ACTION_MASK) == 0)
81 {
82 uMode |= RTFILE_O_OPEN_CREATE
83 | RTFILE_O_WRITE;
84 }
85 else
86 rc = VERR_INVALID_PARAMETER;
87 break;
88
89 /* Opens an existing file for reading and places the
90 * file pointer at the beginning of the file. If the
91 * file does not exist an error will be returned. */
92 case 'r':
93 if ((uMode & RTFILE_O_ACTION_MASK) == 0)
94 {
95 uMode |= RTFILE_O_OPEN
96 | RTFILE_O_READ;
97 }
98 else
99 rc = VERR_INVALID_PARAMETER;
100 break;
101
102 case 't': /* Text mode. */
103 /* Just skip as being valid. */
104 fSkip = true;
105 break;
106
107 /* Creates a new file or replaces an existing one
108 * for writing. Places the file pointer at the beginning.
109 * An existing file will be truncated to 0 bytes. */
110 case 'w':
111 if ((uMode & RTFILE_O_ACTION_MASK) == 0)
112 {
113 uMode |= RTFILE_O_CREATE_REPLACE
114 | RTFILE_O_WRITE
115 | RTFILE_O_TRUNCATE;
116 }
117 else
118 rc = VERR_INVALID_PARAMETER;
119 break;
120
121 /* Creates a new file and opens it for writing. Places
122 * the file pointer at the beginning. If the file
123 * exists an error will be returned. */
124 case 'x':
125 if ((uMode & RTFILE_O_ACTION_MASK) == 0)
126 {
127 uMode |= RTFILE_O_CREATE
128 | RTFILE_O_WRITE;
129 }
130 else
131 rc = VERR_INVALID_PARAMETER;
132 break;
133
134 case '+':
135 {
136 switch (chPrev)
137 {
138 case 'c':
139 case 'w':
140 case 'x':
141 /* Also open / create file with read access. */
142 uMode |= RTFILE_O_READ;
143 break;
144
145 case 'a':
146 case 'r':
147 /* Also open / create file with write access. */
148 uMode |= RTFILE_O_WRITE;
149 break;
150
151 case 'b':
152 case 't':
153 /* Silently eat skipped parameters. */
154 fSkip = true;
155 break;
156
157 case 0: /* No previous character yet. */
158 case '+':
159 /* Eat plusses which don't belong to a command. */
160 fSkip = true;
161 break;
162
163 default:
164 rc = VERR_INVALID_PARAMETER;
165 break;
166 }
167
168 break;
169 }
170
171 default:
172 rc = VERR_INVALID_PARAMETER;
173 break;
174 }
175
176 if (RT_FAILURE(rc))
177 break;
178
179 if (!fSkip)
180 chPrev = *pszCur;
181 pszCur++;
182 }
183
184 /* No action mask set? */
185 if ((uMode & RTFILE_O_ACTION_MASK) == 0)
186 rc = VERR_INVALID_PARAMETER;
187
188 if (RT_SUCCESS(rc))
189 *puMode = uMode;
190
191 return rc;
192}
193RT_EXPORT_SYMBOL(RTFileModeToFlags);
194
195
196RTR3DECL(int) RTFileModeToFlagsEx(const char *pszAccess, const char *pszDisposition,
197 const char *pszSharing, uint64_t *puMode)
198{
199 AssertPtrReturn(pszAccess, VERR_INVALID_POINTER);
200 AssertPtrReturn(pszDisposition, VERR_INVALID_POINTER);
201 /* pszSharing is not used yet. */
202 AssertPtrReturn(puMode, VERR_INVALID_POINTER);
203
204 int rc = VINF_SUCCESS;
205
206 const char *pszCur = pszAccess;
207 uint64_t uMode = 0;
208 char chPrev = 0;
209
210 if (*pszCur == '\0')
211 return VERR_INVALID_PARAMETER;
212
213 /*
214 * Handle open mode.
215 */
216 while ( pszCur
217 && *pszCur != '\0')
218 {
219 bool fSkip = false;
220 switch (*pszCur)
221 {
222 case 'b': /* Binary mode. */
223 /* Just skip as being valid. */
224 fSkip = true;
225 break;
226
227 case 'r': /* Read. */
228 uMode |= RTFILE_O_READ;
229 break;
230
231 case 't': /* Text mode. */
232 /* Just skip as being valid. */
233 fSkip = true;
234 break;
235
236 case 'w': /* Write. */
237 uMode |= RTFILE_O_WRITE;
238 break;
239
240 case '+':
241 {
242 switch (chPrev)
243 {
244 case 'w':
245 /* Also use read access in write mode. */
246 uMode |= RTFILE_O_READ;
247 break;
248
249 case 'r':
250 /* Also use write access in read mode. */
251 uMode |= RTFILE_O_WRITE;
252 break;
253
254 case 'b':
255 case 't':
256 /* Silently eat skipped parameters. */
257 fSkip = true;
258 break;
259
260 case 0: /* No previous character yet. */
261 case '+':
262 /* Eat plusses which don't belong to a command. */
263 fSkip = true;
264 break;
265
266 default:
267 rc = VERR_INVALID_PARAMETER;
268 break;
269 }
270
271 break;
272 }
273
274 default:
275 rc = VERR_INVALID_PARAMETER;
276 break;
277 }
278
279 if (RT_FAILURE(rc))
280 break;
281
282 if (!fSkip)
283 chPrev = *pszCur;
284 pszCur++;
285 }
286
287 if (RT_FAILURE(rc))
288 return rc;
289
290 /*
291 * Handle disposition.
292 */
293 pszCur = pszDisposition;
294
295 /* Create a new file, always, overwrite an existing file. */
296 if (!RTStrCmp(pszCur, "ca"))
297 uMode |= RTFILE_O_CREATE_REPLACE;
298 /* Create a new file if it does not exist, fail if exist. */
299 else if (!RTStrCmp(pszCur, "ce"))
300 uMode |= RTFILE_O_CREATE;
301 /* Open existing file, create file if does not exist. */
302 else if (!RTStrCmp(pszCur, "oc"))
303 uMode |= RTFILE_O_OPEN_CREATE;
304 /* Open existing, fail if does not exist. */
305 else if (!RTStrCmp(pszCur, "oe"))
306 uMode |= RTFILE_O_OPEN;
307 /* Open and truncate existing, fail of not exist. */
308 else if (!RTStrCmp(pszCur, "ot"))
309 uMode |= RTFILE_O_OPEN | RTFILE_O_TRUNCATE;
310
311 /* No action mask set? */
312 if ((uMode & RTFILE_O_ACTION_MASK) == 0)
313 rc = VERR_INVALID_PARAMETER;
314
315 if (RT_FAILURE(rc))
316 return rc;
317
318 /** @todo Handle sharing mode. */
319
320 *puMode = uMode;
321 return rc;
322}
323RT_EXPORT_SYMBOL(RTFileModeToFlagsEx);
324
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