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