Routino SVN Repository Browser

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

ViewVC logotype

Contents of /trunk/src/files.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1410 - (show annotations) (download) (as text)
Fri Jun 21 15:48:19 2013 UTC (11 years, 8 months ago) by amb
File MIME type: text/x-chdr
File size: 5727 byte(s)
Rename the functions for unbuffered file access to make this clear.

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 OpenFileUnbufferedNew(const char *filename);
48 int OpenFileUnbufferedAppend(const char *filename);
49
50 int OpenFileBufferedNew(const char *filename);
51 int OpenFileBufferedAppend(const char *filename);
52
53 int ReOpenFileUnbuffered(const char *filename);
54 int ReOpenFileUnbufferedWriteable(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 SeekFileUnbuffered(int fd,off_t position);
62
63 int SeekFileBuffered(int fd,off_t position);
64 int SkipFileBuffered(int fd,off_t skip);
65
66 int CloseFileUnbuffered(int fd);
67 int CloseFileBuffered(int fd);
68
69 off_t SizeFile(const char *filename);
70 int ExistsFile(const char *filename);
71
72 int DeleteFile(const char *filename);
73
74 int RenameFile(const char *oldfilename,const char *newfilename);
75
76 /* Functions in files.h */
77
78 static inline int WriteFileUnbuffered(int fd,const void *address,size_t length);
79 static inline int ReadFileUnbuffered(int fd,void *address,size_t length);
80
81 static inline int SeekWriteFileUnbuffered(int fd,const void *address,size_t length,off_t position);
82 static inline int SeekReadFileUnbuffered(int fd,void *address,size_t length,off_t position);
83
84
85 /* Inline the frequently called functions */
86
87 /*++++++++++++++++++++++++++++++++++++++
88 Write data to a file descriptor.
89
90 int WriteFileUnbuffered Returns 0 if OK or something else in case of an error.
91
92 int fd The file descriptor to write to.
93
94 const void *address The address of the data to be written.
95
96 size_t length The length of data to write.
97 ++++++++++++++++++++++++++++++++++++++*/
98
99 static inline int WriteFileUnbuffered(int fd,const void *address,size_t length)
100 {
101 logassert(fd!=-1,"File descriptor is in error - report a bug");
102
103 /* Write the data */
104
105 if(write(fd,address,length)!=(ssize_t)length)
106 return(-1);
107
108 return(0);
109 }
110
111
112 /*++++++++++++++++++++++++++++++++++++++
113 Read data from a file descriptor.
114
115 int ReadFileUnbuffered Returns 0 if OK or something else in case of an error.
116
117 int fd The file descriptor to read from.
118
119 void *address The address the data is to be read into.
120
121 size_t length The length of data to read.
122 ++++++++++++++++++++++++++++++++++++++*/
123
124 static inline int ReadFileUnbuffered(int fd,void *address,size_t length)
125 {
126 logassert(fd!=-1,"File descriptor is in error - report a bug");
127
128 /* Read the data */
129
130 if(read(fd,address,length)!=(ssize_t)length)
131 return(-1);
132
133 return(0);
134 }
135
136
137 /*++++++++++++++++++++++++++++++++++++++
138 Write data to a file descriptor after seeking to a position.
139
140 int SeekWriteFileUnbuffered Returns 0 if OK or something else in case of an error.
141
142 int fd The file descriptor to write to.
143
144 const void *address The address of the data to be written.
145
146 size_t length The length of data to write.
147
148 off_t position The position to seek to.
149 ++++++++++++++++++++++++++++++++++++++*/
150
151 static inline int SeekWriteFileUnbuffered(int fd,const void *address,size_t length,off_t position)
152 {
153 logassert(fd!=-1,"File descriptor is in error - report a bug");
154
155 /* Seek and write the data */
156
157 #if HAVE_PREAD_PWRITE
158
159 if(pwrite(fd,address,length,position)!=(ssize_t)length)
160 return(-1);
161
162 #else
163
164 if(lseek(fd,position,SEEK_SET)!=position)
165 return(-1);
166
167 if(write(fd,address,length)!=length)
168 return(-1);
169
170 #endif
171
172 return(0);
173 }
174
175
176 /*++++++++++++++++++++++++++++++++++++++
177 Read data from a file descriptor after seeking to a position.
178
179 int SeekReadFileUnbuffered Returns 0 if OK or something else in case of an error.
180
181 int fd The file descriptor to read from.
182
183 void *address The address the data is to be read into.
184
185 size_t length The length of data to read.
186
187 off_t position The position to seek to.
188 ++++++++++++++++++++++++++++++++++++++*/
189
190 static inline int SeekReadFileUnbuffered(int fd,void *address,size_t length,off_t position)
191 {
192 logassert(fd!=-1,"File descriptor is in error - report a bug");
193
194 /* Seek and read the data */
195
196 #if HAVE_PREAD_PWRITE
197
198 if(pread(fd,address,length,position)!=(ssize_t)length)
199 return(-1);
200
201 #else
202
203 if(lseek(fd,position,SEEK_SET)!=position)
204 return(-1);
205
206 if(read(fd,address,length)!=length)
207 return(-1);
208
209 #endif
210
211 return(0);
212 }
213
214
215 #endif /* FILES_H */

Properties

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