VirtualBox

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

Last change on this file since 48097 was 47783, checked in by vboxsync, 12 years ago

IPRT/RTFileModeToFlags.cpp: Set default sharing modes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.7 KB
Line 
1/* $Id: RTFileModeToFlags.cpp 47783 2013-08-16 08:37:29Z 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 ( RT_SUCCESS(rc)
186 && (uMode & RTFILE_O_ACTION_MASK) == 0)
187 rc = VERR_INVALID_PARAMETER;
188
189 /** @todo Handle sharing mode. */
190 uMode |= RTFILE_O_DENY_NONE;
191
192 if (RT_SUCCESS(rc))
193 *puMode = uMode;
194
195 return rc;
196}
197RT_EXPORT_SYMBOL(RTFileModeToFlags);
198
199
200RTR3DECL(int) RTFileModeToFlagsEx(const char *pszAccess, const char *pszDisposition,
201 const char *pszSharing, uint64_t *puMode)
202{
203 AssertPtrReturn(pszAccess, VERR_INVALID_POINTER);
204 AssertPtrReturn(pszDisposition, VERR_INVALID_POINTER);
205 /* pszSharing is not used yet. */
206 AssertPtrReturn(puMode, VERR_INVALID_POINTER);
207
208 int rc = VINF_SUCCESS;
209
210 const char *pszCur = pszAccess;
211 uint64_t uMode = 0;
212 char chPrev = 0;
213
214 if (*pszCur == '\0')
215 return VERR_INVALID_PARAMETER;
216
217 /*
218 * Handle open mode.
219 */
220 while ( pszCur
221 && *pszCur != '\0')
222 {
223 bool fSkip = false;
224 switch (*pszCur)
225 {
226 case 'b': /* Binary mode. */
227 /* Just skip as being valid. */
228 fSkip = true;
229 break;
230
231 case 'r': /* Read. */
232 uMode |= RTFILE_O_READ;
233 break;
234
235 case 't': /* Text mode. */
236 /* Just skip as being valid. */
237 fSkip = true;
238 break;
239
240 case 'w': /* Write. */
241 uMode |= RTFILE_O_WRITE;
242 break;
243
244 case '+':
245 {
246 switch (chPrev)
247 {
248 case 'w':
249 /* Also use read access in write mode. */
250 uMode |= RTFILE_O_READ;
251 break;
252
253 case 'r':
254 /* Also use write access in read mode. */
255 uMode |= RTFILE_O_WRITE;
256 break;
257
258 case 'b':
259 case 't':
260 /* Silently eat skipped parameters. */
261 fSkip = true;
262 break;
263
264 case 0: /* No previous character yet. */
265 case '+':
266 /* Eat plusses which don't belong to a command. */
267 fSkip = true;
268 break;
269
270 default:
271 rc = VERR_INVALID_PARAMETER;
272 break;
273 }
274
275 break;
276 }
277
278 default:
279 rc = VERR_INVALID_PARAMETER;
280 break;
281 }
282
283 if (RT_FAILURE(rc))
284 break;
285
286 if (!fSkip)
287 chPrev = *pszCur;
288 pszCur++;
289 }
290
291 if (RT_FAILURE(rc))
292 return rc;
293
294 /*
295 * Handle disposition.
296 */
297 pszCur = pszDisposition;
298
299 /* Create a new file, always, overwrite an existing file. */
300 if (!RTStrCmp(pszCur, "ca"))
301 uMode |= RTFILE_O_CREATE_REPLACE;
302 /* Create a new file if it does not exist, fail if exist. */
303 else if (!RTStrCmp(pszCur, "ce"))
304 uMode |= RTFILE_O_CREATE;
305 /* Open existing file, create file if does not exist. */
306 else if (!RTStrCmp(pszCur, "oc"))
307 uMode |= RTFILE_O_OPEN_CREATE;
308 /* Open existing, fail if does not exist. */
309 else if (!RTStrCmp(pszCur, "oe"))
310 uMode |= RTFILE_O_OPEN;
311 /* Open and truncate existing, fail of not exist. */
312 else if (!RTStrCmp(pszCur, "ot"))
313 uMode |= RTFILE_O_OPEN | RTFILE_O_TRUNCATE;
314 else
315 rc = VERR_INVALID_PARAMETER;
316
317 /* No action mask set? */
318 if ( RT_SUCCESS(rc)
319 && (uMode & RTFILE_O_ACTION_MASK) == 0)
320 rc = VERR_INVALID_PARAMETER;
321
322 /** @todo Handle sharing mode. */
323 uMode |= RTFILE_O_DENY_NONE;
324
325 if (RT_SUCCESS(rc))
326 *puMode = uMode;
327
328 return rc;
329}
330RT_EXPORT_SYMBOL(RTFileModeToFlagsEx);
331
Note: See TracBrowser for help on using the repository browser.

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