Merge branch 'upstream'
[routino] / src / files.h
1 /***************************************
2  $Header: /home/amb/routino/src/RCS/files.h,v 1.4 2010/10/09 18:20:18 amb Exp $
3
4  Header file for file function prototypes
5
6  Part of the Routino routing software.
7  ******************/ /******************
8  This file Copyright 2008-2010 Andrew M. Bishop
9
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Affero General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU Affero General Public License for more details.
19
20  You should have received a copy of the GNU Affero General Public License
21  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  ***************************************/
23
24
25 #ifndef FILES_H
26 #define FILES_H    /*+ To stop multiple inclusions. +*/
27
28 #include <unistd.h>
29 #include <sys/types.h>
30
31
32 /* In files.c */
33
34 char *FileName(const char *dirname,const char *prefix, const char *name);
35
36 void *MapFile(const char *filename);
37 void *MapFileWriteable(const char *filename);
38 void *UnmapFile(const char *filename);
39
40 int OpenFileNew(const char *filename);
41 int OpenFileAppend(const char *filename);
42 int ReOpenFile(const char *filename);
43 int ReOpenFileWriteable(const char *filename);
44
45 static int WriteFile(int fd,const void *address,size_t length);
46 static int ReadFile(int fd,void *address,size_t length);
47
48 off_t SizeFile(const char *filename);
49 int ExistsFile(const char *filename);
50
51 static int SeekFile(int fd,off_t position);
52
53 void CloseFile(int fd);
54
55 int DeleteFile(char *filename);
56
57
58 /* Inline the frequently called functions */
59
60 /*++++++++++++++++++++++++++++++++++++++
61   Write data to a file on disk.
62
63   int WriteFile Returns 0 if OK or something else in case of an error.
64
65   int fd The file descriptor to write to.
66
67   const void *address The address of the data to be written from.
68
69   size_t length The length of data to write.
70   ++++++++++++++++++++++++++++++++++++++*/
71
72 static inline int WriteFile(int fd,const void *address,size_t length)
73 {
74  /* Write the data */
75
76  if(write(fd,address,length)!=length)
77     return(-1);
78
79  return(0);
80 }
81
82
83 /*++++++++++++++++++++++++++++++++++++++
84   Read data from a file on disk.
85
86   int ReadFile Returns 0 if OK or something else in case of an error.
87
88   int fd The file descriptor to read from.
89
90   void *address The address of the data to be read into.
91
92   size_t length The length of data to read.
93   ++++++++++++++++++++++++++++++++++++++*/
94
95 static inline int ReadFile(int fd,void *address,size_t length)
96 {
97  /* Read the data */
98
99  if(read(fd,address,length)!=length)
100     return(-1);
101
102  return(0);
103 }
104
105
106 /*++++++++++++++++++++++++++++++++++++++
107   Seek to a position in a file on disk.
108
109   int SeekFile Returns 0 if OK or something else in case of an error.
110
111   int fd The file descriptor to seek within.
112
113   off_t position The position to seek to.
114   ++++++++++++++++++++++++++++++++++++++*/
115
116 static inline int SeekFile(int fd,off_t position)
117 {
118  /* Seek the data */
119
120  if(lseek(fd,position,SEEK_SET)!=position)
121     return(-1);
122
123  return(0);
124 }
125
126
127 #endif /* FILES_H */