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 1745 - (show annotations) (download) (as text)
Tue Jul 14 17:26:47 2015 UTC (9 years, 8 months ago) by amb
File MIME type: text/x-chdr
File size: 4862 byte(s)
Merge changes from MS-Windows branch (changes for stati64/fstati64/lseeki64).

1 /***************************************
2 Header file for file function prototypes
3
4 Part of the Routino routing software.
5 ******************/ /******************
6 This file Copyright 2008-2015 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
30 #if defined(_MSC_VER) || defined(__MINGW32__)
31 #define HAVE_PREAD_PWRITE 0
32 #else
33 #define HAVE_PREAD_PWRITE 1
34 #endif
35
36 #if defined(_MSC_VER)
37 #include <io.h>
38 #include <basetsd.h>
39 #define read(fd,address,length) _read(fd,address,(unsigned int)(length))
40 #define write(fd,address,length) _write(fd,address,(unsigned int)(length))
41 #define ssize_t SSIZE_T
42 #else
43 #include <unistd.h>
44 #endif
45
46 #if defined(_MSC_VER) || defined(__MINGW32__)
47 #define lseek _lseeki64
48 #endif
49
50 #include <sys/types.h>
51 #include <inttypes.h>
52
53 #include "logging.h"
54
55
56 /* Types */
57
58 /*+ A 64-bit file offset since a 32-bit off_t (which is signed) is smaller than a
59 32-bit size_t (which is unsigned) that can be writtento or read from a file. +*/
60 typedef int64_t offset_t;
61
62
63 /* Functions in files.c */
64
65 char *FileName(const char *dirname,const char *prefix, const char *name);
66
67 void *MapFile(const char *filename);
68 void *MapFileWriteable(const char *filename);
69
70 void *UnmapFile(const void *address);
71
72 int SlimMapFile(const char *filename);
73 int SlimMapFileWriteable(const char *filename);
74
75 int SlimUnmapFile(int fd);
76
77 int OpenFileBufferedNew(const char *filename);
78 int OpenFileBufferedAppend(const char *filename);
79
80 int ReOpenFileBuffered(const char *filename);
81
82 int ReplaceFileBuffered(const char *filename,int *oldfd);
83
84 int WriteFileBuffered(int fd,const void *address,size_t length);
85 int ReadFileBuffered(int fd,void *address,size_t length);
86
87 int SeekFileBuffered(int fd,offset_t position);
88 int SkipFileBuffered(int fd,offset_t skip);
89
90 int CloseFileBuffered(int fd);
91
92 int OpenFile(const char *filename);
93
94 void CloseFile(int fd);
95
96 offset_t SizeFile(const char *filename);
97 offset_t SizeFileFD(int fd);
98 int ExistsFile(const char *filename);
99
100 int DeleteFile(const char *filename);
101
102 int RenameFile(const char *oldfilename,const char *newfilename);
103
104 /* Functions in files.h */
105
106 static inline int SlimReplace(int fd,const void *address,size_t length,offset_t position);
107 static inline int SlimFetch(int fd,void *address,size_t length,offset_t position);
108
109
110 /* Inline the frequently called functions */
111
112 /*++++++++++++++++++++++++++++++++++++++
113 Write data to a file that has been opened for slim mode access.
114
115 int SlimReplace Returns 0 if OK or something else in case of an error.
116
117 int fd The file descriptor to write to.
118
119 const void *address The address of the data to be written.
120
121 size_t length The length of data to write.
122
123 offset_t position The position in the file to seek to.
124 ++++++++++++++++++++++++++++++++++++++*/
125
126 static inline int SlimReplace(int fd,const void *address,size_t length,offset_t position)
127 {
128 /* Seek and write the data */
129
130 #if HAVE_PREAD_PWRITE
131
132 if(pwrite(fd,address,length,position)!=(ssize_t)length)
133 return(-1);
134
135 #else
136
137 if(lseek(fd,position,SEEK_SET)!=position)
138 return(-1);
139
140 if(write(fd,address,length)!=(ssize_t)length)
141 return(-1);
142
143 #endif
144
145 return(0);
146 }
147
148
149 /*++++++++++++++++++++++++++++++++++++++
150 Read data from a file that has been opened for slim mode access.
151
152 int SlimFetch Returns 0 if OK or something else in case of an error.
153
154 int fd The file descriptor to read from.
155
156 void *address The address the data is to be read into.
157
158 size_t length The length of data to read.
159
160 offset_t position The position in the file to seek to.
161 ++++++++++++++++++++++++++++++++++++++*/
162
163 static inline int SlimFetch(int fd,void *address,size_t length,offset_t position)
164 {
165 /* Seek and read the data */
166
167 #if HAVE_PREAD_PWRITE
168
169 if(pread(fd,address,length,position)!=(ssize_t)length)
170 return(-1);
171
172 #else
173
174 if(lseek(fd,position,SEEK_SET)!=position)
175 return(-1);
176
177 if(read(fd,address,length)!=(ssize_t)length)
178 return(-1);
179
180 #endif
181
182 return(0);
183 }
184
185
186 #endif /* FILES_H */

Properties

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