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 888 - (hide annotations) (download) (as text)
Mon Oct 31 19:18:08 2011 UTC (13 years, 5 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 amb 450 /***************************************
2     Header file for file function prototypes
3    
4     Part of the Routino routing software.
5     ******************/ /******************
6 amb 612 This file Copyright 2008-2011 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 612 #include <assert.h>
33 amb 450 #include <unistd.h>
34     #include <sys/types.h>
35    
36    
37 amb 680 /* Functions in files.c */
38 amb 450
39     char *FileName(const char *dirname,const char *prefix, const char *name);
40    
41     void *MapFile(const char *filename);
42 amb 507 void *MapFileWriteable(const char *filename);
43 amb 450 void *UnmapFile(const char *filename);
44    
45 amb 502 int OpenFileNew(const char *filename);
46     int OpenFileAppend(const char *filename);
47 amb 450 int ReOpenFile(const char *filename);
48 amb 511 int ReOpenFileWriteable(const char *filename);
49 amb 450
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 amb 887 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 amb 450 off_t SizeFile(const char *filename);
57     int ExistsFile(const char *filename);
58    
59     static int SeekFile(int fd,off_t position);
60    
61 amb 612 int CloseFile(int fd);
62 amb 450
63     int DeleteFile(char *filename);
64    
65    
66     /* Inline the frequently called functions */
67    
68     /*++++++++++++++++++++++++++++++++++++++
69 amb 680 Write data to a file descriptor.
70 amb 450
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 amb 680 const void *address The address of the data to be written.
76 amb 450
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 amb 614 assert(fd!=-1);
83 amb 612
84 amb 450 /* Write the data */
85    
86     if(write(fd,address,length)!=length)
87     return(-1);
88    
89     return(0);
90     }
91    
92    
93     /*++++++++++++++++++++++++++++++++++++++
94 amb 680 Read data from a file descriptor.
95 amb 450
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 amb 680 void *address The address the data is to be read into.
101 amb 450
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 amb 614 assert(fd!=-1);
108 amb 450
109 amb 680 /* Read the data */
110    
111 amb 450 if(read(fd,address,length)!=length)
112     return(-1);
113    
114     return(0);
115     }
116    
117    
118     /*++++++++++++++++++++++++++++++++++++++
119 amb 680 Seek to a position in a file descriptor.
120 amb 450
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 amb 614 assert(fd!=-1);
131 amb 612
132 amb 450 /* Seek the data */
133    
134     if(lseek(fd,position,SEEK_SET)!=position)
135     return(-1);
136    
137     return(0);
138     }
139    
140    
141 amb 887 /*++++++++++++++++++++++++++++++++++++++
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 amb 888 /* Seek and write the data */
160 amb 887
161 amb 888 #if HAVE_PREAD_PWRITE
162    
163 amb 887 if(pwrite(fd,address,length,position)!=length)
164     return(-1);
165    
166 amb 888 #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 amb 887 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 amb 888 /* Seek and read the data */
199 amb 887
200 amb 888 #if HAVE_PREAD_PWRITE
201    
202 amb 887 if(pread(fd,address,length,position)!=length)
203     return(-1);
204    
205 amb 888 #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 amb 887 return(0);
216     }
217    
218    
219 amb 450 #endif /* FILES_H */

Properties

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