Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Contents of /trunk/src/files.h
Parent Directory
|
Revision Log
Revision 888 -
(show annotations)
(download)
(as text)
Mon Oct 31 19:18:08 2011 UTC (13 years, 4 months ago) by amb
File MIME type: text/x-chdr
File size: 5260 byte(s)
Mon Oct 31 19:18:08 2011 UTC (13 years, 4 months ago) by amb
File MIME type: text/x-chdr
File size: 5260 byte(s)
Add a #define to disable the use of pread()/pwrite() but this must be manually configured, there is no configure script.
1 | /*************************************** |
2 | Header file for file function prototypes |
3 | |
4 | Part of the Routino routing software. |
5 | ******************/ /****************** |
6 | This file Copyright 2008-2011 Andrew M. Bishop |
7 | |
8 | This program is free software: you can redistribute it and/or modify |
9 | it under the terms of the GNU Affero General Public License as published by |
10 | the Free Software Foundation, either version 3 of the License, or |
11 | (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU Affero General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Affero General Public License |
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | ***************************************/ |
21 | |
22 | |
23 | #ifndef FILES_H |
24 | #define FILES_H /*+ To stop multiple inclusions. +*/ |
25 | |
26 | /* If your system does not have the pread() and pwrite() system calls then you |
27 | * will need to change this line to the value 0 so that seek() and |
28 | * read()/write() are used instead of pread()/pwrite(). */ |
29 | #define HAVE_PREAD_PWRITE 1 |
30 | |
31 | |
32 | #include <assert.h> |
33 | #include <unistd.h> |
34 | #include <sys/types.h> |
35 | |
36 | |
37 | /* Functions in files.c */ |
38 | |
39 | char *FileName(const char *dirname,const char *prefix, const char *name); |
40 | |
41 | void *MapFile(const char *filename); |
42 | void *MapFileWriteable(const char *filename); |
43 | void *UnmapFile(const char *filename); |
44 | |
45 | int OpenFileNew(const char *filename); |
46 | int OpenFileAppend(const char *filename); |
47 | int ReOpenFile(const char *filename); |
48 | int ReOpenFileWriteable(const char *filename); |
49 | |
50 | static int WriteFile(int fd,const void *address,size_t length); |
51 | static int ReadFile(int fd,void *address,size_t length); |
52 | |
53 | static int SeekWriteFile(int fd,const void *address,size_t length,off_t position); |
54 | static int SeekReadFile(int fd,void *address,size_t length,off_t position); |
55 | |
56 | off_t SizeFile(const char *filename); |
57 | int ExistsFile(const char *filename); |
58 | |
59 | static int SeekFile(int fd,off_t position); |
60 | |
61 | int CloseFile(int fd); |
62 | |
63 | int DeleteFile(char *filename); |
64 | |
65 | |
66 | /* Inline the frequently called functions */ |
67 | |
68 | /*++++++++++++++++++++++++++++++++++++++ |
69 | Write data to a file descriptor. |
70 | |
71 | int WriteFile Returns 0 if OK or something else in case of an error. |
72 | |
73 | int fd The file descriptor to write to. |
74 | |
75 | const void *address The address of the data to be written. |
76 | |
77 | size_t length The length of data to write. |
78 | ++++++++++++++++++++++++++++++++++++++*/ |
79 | |
80 | static inline int WriteFile(int fd,const void *address,size_t length) |
81 | { |
82 | assert(fd!=-1); |
83 | |
84 | /* Write the data */ |
85 | |
86 | if(write(fd,address,length)!=length) |
87 | return(-1); |
88 | |
89 | return(0); |
90 | } |
91 | |
92 | |
93 | /*++++++++++++++++++++++++++++++++++++++ |
94 | Read data from a file descriptor. |
95 | |
96 | int ReadFile Returns 0 if OK or something else in case of an error. |
97 | |
98 | int fd The file descriptor to read from. |
99 | |
100 | void *address The address the data is to be read into. |
101 | |
102 | size_t length The length of data to read. |
103 | ++++++++++++++++++++++++++++++++++++++*/ |
104 | |
105 | static inline int ReadFile(int fd,void *address,size_t length) |
106 | { |
107 | assert(fd!=-1); |
108 | |
109 | /* Read the data */ |
110 | |
111 | if(read(fd,address,length)!=length) |
112 | return(-1); |
113 | |
114 | return(0); |
115 | } |
116 | |
117 | |
118 | /*++++++++++++++++++++++++++++++++++++++ |
119 | Seek to a position in a file descriptor. |
120 | |
121 | int SeekFile Returns 0 if OK or something else in case of an error. |
122 | |
123 | int fd The file descriptor to seek within. |
124 | |
125 | off_t position The position to seek to. |
126 | ++++++++++++++++++++++++++++++++++++++*/ |
127 | |
128 | static inline int SeekFile(int fd,off_t position) |
129 | { |
130 | assert(fd!=-1); |
131 | |
132 | /* Seek the data */ |
133 | |
134 | if(lseek(fd,position,SEEK_SET)!=position) |
135 | return(-1); |
136 | |
137 | return(0); |
138 | } |
139 | |
140 | |
141 | /*++++++++++++++++++++++++++++++++++++++ |
142 | Write data to a file descriptor after seeking to a position. |
143 | |
144 | int WriteFile Returns 0 if OK or something else in case of an error. |
145 | |
146 | int fd The file descriptor to write to. |
147 | |
148 | const void *address The address of the data to be written. |
149 | |
150 | size_t length The length of data to write. |
151 | |
152 | off_t position The position to seek to. |
153 | ++++++++++++++++++++++++++++++++++++++*/ |
154 | |
155 | static inline int SeekWriteFile(int fd,const void *address,size_t length,off_t position) |
156 | { |
157 | assert(fd!=-1); |
158 | |
159 | /* Seek and write the data */ |
160 | |
161 | #if HAVE_PREAD_PWRITE |
162 | |
163 | if(pwrite(fd,address,length,position)!=length) |
164 | return(-1); |
165 | |
166 | #else |
167 | |
168 | if(lseek(fd,position,SEEK_SET)!=position) |
169 | return(-1); |
170 | |
171 | if(write(fd,address,length)!=length) |
172 | return(-1); |
173 | |
174 | #endif |
175 | |
176 | return(0); |
177 | } |
178 | |
179 | |
180 | /*++++++++++++++++++++++++++++++++++++++ |
181 | Read data from a file descriptor after seeking to a position. |
182 | |
183 | int SeekReadFile Returns 0 if OK or something else in case of an error. |
184 | |
185 | int fd The file descriptor to read from. |
186 | |
187 | void *address The address the data is to be read into. |
188 | |
189 | size_t length The length of data to read. |
190 | |
191 | off_t position The position to seek to. |
192 | ++++++++++++++++++++++++++++++++++++++*/ |
193 | |
194 | static inline int SeekReadFile(int fd,void *address,size_t length,off_t position) |
195 | { |
196 | assert(fd!=-1); |
197 | |
198 | /* Seek and read the data */ |
199 | |
200 | #if HAVE_PREAD_PWRITE |
201 | |
202 | if(pread(fd,address,length,position)!=length) |
203 | return(-1); |
204 | |
205 | #else |
206 | |
207 | if(lseek(fd,position,SEEK_SET)!=position) |
208 | return(-1); |
209 | |
210 | if(read(fd,address,length)!=length) |
211 | return(-1); |
212 | |
213 | #endif |
214 | |
215 | return(0); |
216 | } |
217 | |
218 | |
219 | #endif /* FILES_H */ |
Properties
Name | Value |
---|---|
cvs:description | A new header file for files.c functions and includes some inline functions. |