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 1404 -
(show annotations)
(download)
(as text)
Thu Jun 20 18:35:57 2013 UTC (11 years, 8 months ago) by amb
File MIME type: text/x-chdr
File size: 6022 byte(s)
Thu Jun 20 18:35:57 2013 UTC (11 years, 8 months ago) by amb
File MIME type: text/x-chdr
File size: 6022 byte(s)
Add new functions for buffering when reading and also allow seeking in one of these buffered files.
1 | /*************************************** |
2 | Header file for file function prototypes |
3 | |
4 | Part of the Routino routing software. |
5 | ******************/ /****************** |
6 | This file Copyright 2008-2013 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 <unistd.h> |
33 | #include <sys/types.h> |
34 | |
35 | #include "logging.h" |
36 | |
37 | |
38 | /* Functions in files.c */ |
39 | |
40 | char *FileName(const char *dirname,const char *prefix, const char *name); |
41 | |
42 | void *MapFile(const char *filename); |
43 | void *MapFileWriteable(const char *filename); |
44 | |
45 | void *UnmapFile(const void *address); |
46 | |
47 | int OpenFileNew(const char *filename); |
48 | int OpenFileAppend(const char *filename); |
49 | |
50 | int OpenFileBufferedNew(const char *filename); |
51 | int OpenFileBufferedAppend(const char *filename); |
52 | |
53 | int ReOpenFile(const char *filename); |
54 | int ReOpenFileWriteable(const char *filename); |
55 | |
56 | int ReOpenFileBuffered(const char *filename); |
57 | |
58 | int WriteFileBuffered(int fd,const void *address,size_t length); |
59 | int ReadFileBuffered(int fd,void *address,size_t length); |
60 | |
61 | int SeekFileBuffered(int fd,off_t offset); |
62 | |
63 | int CloseFile(int fd); |
64 | int CloseFileBuffered(int fd); |
65 | |
66 | off_t SizeFile(const char *filename); |
67 | int ExistsFile(const char *filename); |
68 | |
69 | int DeleteFile(const char *filename); |
70 | |
71 | int RenameFile(const char *oldfilename,const char *newfilename); |
72 | |
73 | /* Functions in files.h */ |
74 | |
75 | static inline int WriteFile(int fd,const void *address,size_t length); |
76 | static inline int ReadFile(int fd,void *address,size_t length); |
77 | |
78 | static inline int SeekWriteFile(int fd,const void *address,size_t length,off_t position); |
79 | static inline int SeekReadFile(int fd,void *address,size_t length,off_t position); |
80 | |
81 | static inline int SeekFile(int fd,off_t position); |
82 | |
83 | |
84 | /* Inline the frequently called functions */ |
85 | |
86 | /*++++++++++++++++++++++++++++++++++++++ |
87 | Write data to a file descriptor. |
88 | |
89 | int WriteFile Returns 0 if OK or something else in case of an error. |
90 | |
91 | int fd The file descriptor to write to. |
92 | |
93 | const void *address The address of the data to be written. |
94 | |
95 | size_t length The length of data to write. |
96 | ++++++++++++++++++++++++++++++++++++++*/ |
97 | |
98 | static inline int WriteFile(int fd,const void *address,size_t length) |
99 | { |
100 | logassert(fd!=-1,"File descriptor is in error - report a bug"); |
101 | |
102 | /* Write the data */ |
103 | |
104 | if(write(fd,address,length)!=(ssize_t)length) |
105 | return(-1); |
106 | |
107 | return(0); |
108 | } |
109 | |
110 | |
111 | /*++++++++++++++++++++++++++++++++++++++ |
112 | Read data from a file descriptor. |
113 | |
114 | int ReadFile Returns 0 if OK or something else in case of an error. |
115 | |
116 | int fd The file descriptor to read from. |
117 | |
118 | void *address The address the data is to be read into. |
119 | |
120 | size_t length The length of data to read. |
121 | ++++++++++++++++++++++++++++++++++++++*/ |
122 | |
123 | static inline int ReadFile(int fd,void *address,size_t length) |
124 | { |
125 | logassert(fd!=-1,"File descriptor is in error - report a bug"); |
126 | |
127 | /* Read the data */ |
128 | |
129 | if(read(fd,address,length)!=(ssize_t)length) |
130 | return(-1); |
131 | |
132 | return(0); |
133 | } |
134 | |
135 | |
136 | /*++++++++++++++++++++++++++++++++++++++ |
137 | Seek to a position in a file descriptor. |
138 | |
139 | int SeekFile Returns 0 if OK or something else in case of an error. |
140 | |
141 | int fd The file descriptor to seek within. |
142 | |
143 | off_t position The position to seek to. |
144 | ++++++++++++++++++++++++++++++++++++++*/ |
145 | |
146 | static inline int SeekFile(int fd,off_t position) |
147 | { |
148 | logassert(fd!=-1,"File descriptor is in error - report a bug"); |
149 | |
150 | /* Seek the data */ |
151 | |
152 | if(lseek(fd,position,SEEK_SET)!=position) |
153 | return(-1); |
154 | |
155 | return(0); |
156 | } |
157 | |
158 | |
159 | /*++++++++++++++++++++++++++++++++++++++ |
160 | Write data to a file descriptor after seeking to a position. |
161 | |
162 | int SeekWriteFile Returns 0 if OK or something else in case of an error. |
163 | |
164 | int fd The file descriptor to write to. |
165 | |
166 | const void *address The address of the data to be written. |
167 | |
168 | size_t length The length of data to write. |
169 | |
170 | off_t position The position to seek to. |
171 | ++++++++++++++++++++++++++++++++++++++*/ |
172 | |
173 | static inline int SeekWriteFile(int fd,const void *address,size_t length,off_t position) |
174 | { |
175 | logassert(fd!=-1,"File descriptor is in error - report a bug"); |
176 | |
177 | /* Seek and write the data */ |
178 | |
179 | #if HAVE_PREAD_PWRITE |
180 | |
181 | if(pwrite(fd,address,length,position)!=(ssize_t)length) |
182 | return(-1); |
183 | |
184 | #else |
185 | |
186 | if(lseek(fd,position,SEEK_SET)!=position) |
187 | return(-1); |
188 | |
189 | if(write(fd,address,length)!=length) |
190 | return(-1); |
191 | |
192 | #endif |
193 | |
194 | return(0); |
195 | } |
196 | |
197 | |
198 | /*++++++++++++++++++++++++++++++++++++++ |
199 | Read data from a file descriptor after seeking to a position. |
200 | |
201 | int SeekReadFile Returns 0 if OK or something else in case of an error. |
202 | |
203 | int fd The file descriptor to read from. |
204 | |
205 | void *address The address the data is to be read into. |
206 | |
207 | size_t length The length of data to read. |
208 | |
209 | off_t position The position to seek to. |
210 | ++++++++++++++++++++++++++++++++++++++*/ |
211 | |
212 | static inline int SeekReadFile(int fd,void *address,size_t length,off_t position) |
213 | { |
214 | logassert(fd!=-1,"File descriptor is in error - report a bug"); |
215 | |
216 | /* Seek and read the data */ |
217 | |
218 | #if HAVE_PREAD_PWRITE |
219 | |
220 | if(pread(fd,address,length,position)!=(ssize_t)length) |
221 | return(-1); |
222 | |
223 | #else |
224 | |
225 | if(lseek(fd,position,SEEK_SET)!=position) |
226 | return(-1); |
227 | |
228 | if(read(fd,address,length)!=length) |
229 | return(-1); |
230 | |
231 | #endif |
232 | |
233 | return(0); |
234 | } |
235 | |
236 | |
237 | #endif /* FILES_H */ |
Properties
Name | Value |
---|---|
cvs:description | A new header file for files.c functions and includes some inline functions. |