2 Copyright (c) 2003-2005, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 \brief Cross-platform, simple file accessors
36 #include <AS_DCP_system.h>
43 #include <sys/types.h>
48 typedef __int64 fsize_t;
49 typedef __int64 fpos_t;
52 SP_BEGIN = FILE_BEGIN,
53 SP_POS = FILE_CURRENT,
57 typedef off_t fsize_t;
60 const HANDLE INVALID_HANDLE_VALUE = -1L;
69 bool PathIsFile(const char* pathname);
70 bool PathIsDirectory(const char* pathname);
71 fsize_t FileSize(const char* pathname);
76 ASDCP_NO_COPY_CONSTRUCT(FileReader);
79 std::string m_Filename;
83 FileReader() : m_Handle(INVALID_HANDLE_VALUE) {}
84 virtual ~FileReader() { Close(); }
86 Result_t OpenRead(const char*) const; // open the file for reading
87 Result_t Close() const; // close the file
88 fsize_t Size() const; // returns the file's current size
89 Result_t Seek(ASDCP::fpos_t = 0, SeekPos_t = SP_BEGIN) const; // move the file pointer
90 Result_t Tell(ASDCP::fpos_t* pos) const; // report the file pointer's location
91 Result_t Read(byte_t*, ui32_t, ui32_t* = 0) const; // read a buffer of data
93 inline ASDCP::fpos_t Tell() const // report the file pointer's location
95 ASDCP::fpos_t tmp_pos;
100 inline bool IsOpen() { // returns true if the file is open
101 return (m_Handle != INVALID_HANDLE_VALUE);
107 class FileWriter : public FileReader
110 mem_ptr<h__iovec> m_IOVec;
111 ASDCP_NO_COPY_CONSTRUCT(FileWriter);
115 virtual ~FileWriter();
117 Result_t OpenWrite(const char*); // open a new file, overwrites existing
118 Result_t OpenModify(const char*); // open a file for read/write
120 // this part of the interface takes advantage of the iovec structure on
121 // platforms that support it. For each call to Writev(const byte_t*, ui32_t, ui32_t*),
122 // the given buffer is added to an internal iovec struct. All items on the list
123 // are written to disk by a call to Writev();
124 Result_t Writev(const byte_t*, ui32_t); // queue buffer for write
125 Result_t Writev(ui32_t* = 0); // write all queued buffers
127 // if you call this while there are unwritten items on the iovec list,
128 // the iovec list will be written to disk before the givn buffer,as though
129 // you had called Writev() first.
130 Result_t Write(const byte_t*, ui32_t, ui32_t* = 0); // write buffer to disk
138 #endif // _FILEREADER_H_