-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutility.h
More file actions
118 lines (108 loc) · 2.7 KB
/
utility.h
File metadata and controls
118 lines (108 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* Copyright (C) 2014 InfiniDB, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2 of
the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA. */
#ifndef UTILITY_H_
#define UTILITY_H_
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string>
namespace idbdatafile
{
/**
* Take an fopen() style mode string and return the corresponding flags
* ala open()
*/
inline
int modeStrToFlags(const char* mode)
{
std::string modestr = mode;
// this could easily be migrated to a lookup table if performance
// ever became a concern, but for now this is fine.
if( modestr == "r" || modestr == "rb" )
{
return O_RDONLY;
}
else if( modestr == "r+" || modestr == "r+b" )
{
return O_RDWR;
}
else if( modestr == "w" || modestr == "wb" )
{
return O_WRONLY|O_CREAT|O_TRUNC;
}
else if( modestr == "w+" || modestr == "w+b" )
{
return O_RDWR|O_CREAT|O_TRUNC;
}
else if( modestr == "a" || modestr == "ab" )
{
return O_WRONLY|O_CREAT|O_APPEND;
}
else if( modestr == "a+" || modestr == "a+b" )
{
return O_RDWR|O_CREAT|O_APPEND;
}
else
{
// error
return -1;
}
}
#ifdef _MSC_VER
inline
int modeStrToFlags(const char* mode, int& createflags)
{
std::string modestr = mode;
createflags = 0;
// this could easily be migrated to a lookup table if performance
// ever became a concern, but for now this is fine.
if( modestr == "r" || modestr == "rb" )
{
createflags = OPEN_EXISTING;
return GENERIC_READ;
}
else if( modestr == "r+" || modestr == "r+b" )
{
createflags = OPEN_EXISTING;
return GENERIC_READ | GENERIC_WRITE;
}
else if( modestr == "w" || modestr == "wb" )
{
createflags = CREATE_ALWAYS;
return GENERIC_WRITE;
}
else if( modestr == "w+" || modestr == "w+b" )
{
createflags = CREATE_ALWAYS;
return GENERIC_READ | GENERIC_WRITE;
}
else if( modestr == "a" || modestr == "ab" )
{
createflags = OPEN_ALWAYS;
return FILE_APPEND_DATA;
}
else if( modestr == "a+" || modestr == "a+b" )
{
createflags = OPEN_ALWAYS;
return GENERIC_READ | FILE_APPEND_DATA;
}
else
{
// error
return -1;
}
}
#endif
}
#endif /* UTILITY_H_ */