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 887 - (show annotations) (download) (as text)
Mon Oct 31 19:09:40 2011 UTC (13 years, 4 months ago) by amb
File MIME type: text/x-chdr
File size: 4702 byte(s)
Use pread() and pwrite() functions instead of seek() followed by read() or
write().

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 #include <assert.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29
30
31 /* Functions in files.c */
32
33 char *FileName(const char *dirname,const char *prefix, const char *name);
34
35 void *MapFile(const char *filename);
36 void *MapFileWriteable(const char *filename);
37 void *UnmapFile(const char *filename);
38
39 int OpenFileNew(const char *filename);
40 int OpenFileAppend(const char *filename);
41 int ReOpenFile(const char *filename);
42 int ReOpenFileWriteable(const char *filename);
43
44 static int WriteFile(int fd,const void *address,size_t length);
45 static int ReadFile(int fd,void *address,size_t length);
46
47 static int SeekWriteFile(int fd,const void *address,size_t length,off_t position);
48 static int SeekReadFile(int fd,void *address,size_t length,off_t position);
49
50 off_t SizeFile(const char *filename);
51 int ExistsFile(const char *filename);
52
53 static int SeekFile(int fd,off_t position);
54
55 int CloseFile(int fd);
56
57 int DeleteFile(char *filename);
58
59
60 /* Inline the frequently called functions */
61
62 /*++++++++++++++++++++++++++++++++++++++
63 Write data to a file descriptor.
64
65 int WriteFile Returns 0 if OK or something else in case of an error.
66
67 int fd The file descriptor to write to.
68
69 const void *address The address of the data to be written.
70
71 size_t length The length of data to write.
72 ++++++++++++++++++++++++++++++++++++++*/
73
74 static inline int WriteFile(int fd,const void *address,size_t length)
75 {
76 assert(fd!=-1);
77
78 /* Write the data */
79
80 if(write(fd,address,length)!=length)
81 return(-1);
82
83 return(0);
84 }
85
86
87 /*++++++++++++++++++++++++++++++++++++++
88 Read data from a file descriptor.
89
90 int ReadFile Returns 0 if OK or something else in case of an error.
91
92 int fd The file descriptor to read from.
93
94 void *address The address the data is to be read into.
95
96 size_t length The length of data to read.
97 ++++++++++++++++++++++++++++++++++++++*/
98
99 static inline int ReadFile(int fd,void *address,size_t length)
100 {
101 assert(fd!=-1);
102
103 /* Read the data */
104
105 if(read(fd,address,length)!=length)
106 return(-1);
107
108 return(0);
109 }
110
111
112 /*++++++++++++++++++++++++++++++++++++++
113 Seek to a position in a file descriptor.
114
115 int SeekFile Returns 0 if OK or something else in case of an error.
116
117 int fd The file descriptor to seek within.
118
119 off_t position The position to seek to.
120 ++++++++++++++++++++++++++++++++++++++*/
121
122 static inline int SeekFile(int fd,off_t position)
123 {
124 assert(fd!=-1);
125
126 /* Seek the data */
127
128 if(lseek(fd,position,SEEK_SET)!=position)
129 return(-1);
130
131 return(0);
132 }
133
134
135 /*++++++++++++++++++++++++++++++++++++++
136 Write data to a file descriptor after seeking to a position.
137
138 int WriteFile Returns 0 if OK or something else in case of an error.
139
140 int fd The file descriptor to write to.
141
142 const void *address The address of the data to be written.
143
144 size_t length The length of data to write.
145
146 off_t position The position to seek to.
147 ++++++++++++++++++++++++++++++++++++++*/
148
149 static inline int SeekWriteFile(int fd,const void *address,size_t length,off_t position)
150 {
151 assert(fd!=-1);
152
153 /* Write the data */
154
155 if(pwrite(fd,address,length,position)!=length)
156 return(-1);
157
158 return(0);
159 }
160
161
162 /*++++++++++++++++++++++++++++++++++++++
163 Read data from a file descriptor after seeking to a position.
164
165 int SeekReadFile Returns 0 if OK or something else in case of an error.
166
167 int fd The file descriptor to read from.
168
169 void *address The address the data is to be read into.
170
171 size_t length The length of data to read.
172
173 off_t position The position to seek to.
174 ++++++++++++++++++++++++++++++++++++++*/
175
176 static inline int SeekReadFile(int fd,void *address,size_t length,off_t position)
177 {
178 assert(fd!=-1);
179
180 /* Read the data */
181
182 if(pread(fd,address,length,position)!=length)
183 return(-1);
184
185 return(0);
186 }
187
188
189 #endif /* FILES_H */

Properties

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