Routino SVN Repository Browser

Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino

ViewVC logotype

Annotation of /trunk/src/files.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1401 - (hide annotations) (download) (as text)
Wed Jun 19 18:57:15 2013 UTC (11 years, 8 months ago) by amb
File MIME type: text/x-chdr
File size: 5931 byte(s)
Add functions to read and write file descriptors with buffering.

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

Properties

Name Value
cvs:description A new header file for files.c functions and includes some inline functions.