1 //--------------------------------------------------------------------
4 // Copyright 1997-2005 by Christopher J. Madsen
6 // Support functions for Posix file I/O
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of
11 // the License, or (at your option) any later version.
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 General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 //--------------------------------------------------------------------
22 #ifndef INCLUDED_FILEIO_HPP
24 #define INCLUDED_FILEIO_HPP
38 const File InvalidFile
= -1;
40 //--------------------------------------------------------------------
41 inline const char* ErrorMsg()
43 return strerror(errno
);
46 //--------------------------------------------------------------------
47 inline File
OpenFile(const char* path
, bool writable
=false)
49 return open(path
, (writable
? O_RDWR
: O_RDONLY
));
52 //--------------------------------------------------------------------
53 inline void CloseFile(File file
)
58 //--------------------------------------------------------------------
59 bool WriteFile(File file
, const void* buffer
, Size count
)
61 const char* ptr
= reinterpret_cast<const char*>(buffer
);
64 Size bytesWritten
= write(file
, ptr
, count
);
65 if (bytesWritten
< 1) {
73 count
-= bytesWritten
;
74 } // end while more to write
79 //--------------------------------------------------------------------
80 inline Size
ReadFile(File file
, void* buffer
, Size count
)
82 return read(file
, buffer
, count
);
85 //--------------------------------------------------------------------
86 inline FPos
SeekFile(File file
, FPos position
, int whence
=SeekPos
)
88 return lseek(file
, position
, whence
);
91 #endif // INCLUDED_FILEIO_HPP
94 // c-file-style: "cjm"