Routino SVN Repository Browser

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

ViewVC logotype

Contents of /branches/libroutino/src/files.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1742 - (show annotations) (download) (as text)
Sat Jul 11 15:42:22 2015 UTC (9 years, 9 months ago) by amb
File MIME type: text/x-chdr
File size: 4810 byte(s)
Merge change from MS-Windows branch (size_t and offset_t).

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

Properties

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