Evo C++ Library v0.5.1
file.h
Go to the documentation of this file.
1 // Evo C++ Library
2 /* Copyright 2019 Justin Crowell
3 Distributed under the BSD 2-Clause License -- see included file LICENSE.txt for details.
4 */
6 
7 #pragma once
8 #ifndef INCL_evo_file_h
9 #define INCL_evo_file_h
10 
11 #include "iobase.h"
12 
13 // Namespace: evo
14 namespace evo {
15 
18 
20 
77 class File : public Stream<IoFile> {
78 public:
79  typedef Stream<IoFile> Base;
80 
87  File(Newline nl=NL_SYS, bool exceptions=EVO_EXCEPTIONS) : Base(nl)
88  { excep(exceptions); }
89 
100  File(const char* path, Open mode=oREAD, bool flushlines=false, Newline nl=NL_SYS, bool exceptions=EVO_EXCEPTIONS) : Base(nl)
101  { excep(exceptions); open(path, mode, flushlines); }
102 
112  File(const char* path, Open mode, Newline nl, bool exceptions=EVO_EXCEPTIONS) : Base(nl)
113  { excep(exceptions); open(path, mode, false); }
114 
124  bool open(const char* path, Open mode=oREAD, bool flushlines=false) {
125  const bool result = ((error_=device_.open(path, mode)) == ENone);
126  if (result) {
127  Base::init(mode, flushlines);
128  owned_ = true;
129  } else if (excep_ && error_ != ENone) {
130  EVO_THROW_ERR(evo::ExceptionFileOpen, "File::open() failed", error_);
131  }
132  return result;
133  }
134 
140  ulongl pos() {
141  assert( bufrd_.curbuf_offset <= bufrd_.readbuf.used );
142  if (savepos_ > 0)
143  return device_.pos(error_) + bufwr_.used;
145  }
146 
154  ulongl seek(ulongl offset, Seek start=sBegin) {
155  if (bufwr_.used > 0) {
156  if (!flush())
157  return 0;
158  savepos_ = 0;
159  }
160  if (bufrd_.readbuf.used > 0)
162  return device_.seek(error_, offset, start);
163  }
164 
165 private:
166  File(const File&);
167  File& operator=(const File&);
168 };
169 
171 
172 }
173 #endif
void init(Open mode, bool flushlines=false)
Initialize and reset buffers for a new stream.
Definition: iobase.h:1695
#define EVO_THROW_ERR(TYPE, MSG, ERROR)
Throw an Evo exception with error code.
Definition: sys.h:1475
Seek
Seek starting position.
Definition: sysio.h:203
bool excep() const
Get whether exceptions are enabled.
Definition: iobase.h:52
File(const char *path, Open mode, Newline nl, bool exceptions=1)
Constructor to open file and set default newline.
Definition: file.h:112
Error error_
Last error code.
Definition: iobase.h:133
Open
Open mode for files and streams.
Definition: sysio.h:190
bool excep_
Whether to throw exceptions.
Definition: iobase.h:134
Newline
Newline type.
Definition: sys.h:748
IoFile device_
I/O device.
Definition: iobase.h:1687
ulongl seek(Error &err, ulongl offset, Seek start=sBegin)
Seek to file position.
Definition: sysio.h:548
IoWriter bufwr_
Buffered writer.
Definition: iobase.h:1690
No error.
Definition: sys.h:1115
#define EVO_EXCEPTIONS
Whether to throw exceptions on error by default.
Definition: evo_config.h:35
File(Newline nl=NL_SYS, bool exceptions=1)
Constructor.
Definition: file.h:87
Seek from beginning.
Definition: sysio.h:204
Evo base I/O stream classes.
Read only.
Definition: sysio.h:191
File open exception for errors opening a file, see Exception.
Definition: sys.h:1406
bool open(const char *path, Open mode=oREAD, bool flushlines=false)
Open file for read and/or writing.
Definition: file.h:124
RawBuffer readbuf
Primary read buffer – filtering may involve additional buffers.
Definition: sysio.h:661
File I/O stream.
Definition: file.h:77
Error open(const char *path, Open mode, int perm=DEFPERM)
Open file for access.
Definition: sysio.h:462
File(const char *path, Open mode=oREAD, bool flushlines=false, Newline nl=NL_SYS, bool exceptions=1)
Constructor to open file.
Definition: file.h:100
ulongl seek(ulongl offset, Seek start=sBegin)
Seek to file position.
Definition: file.h:154
ulongl pos()
Get current file position.
Definition: file.h:140
Evo C++ Library namespace.
Definition: alg.h:11
Input/Output stream implementation.
Definition: iobase.h:791
Stream< IoFile > Base
Base class alias.
Definition: file.h:79
static const Newline NL_SYS
Current system newline type.
Definition: sys.h:763
bool owned_
Whether handle is owned (to be closed here)
Definition: iobase.h:1688
ulong curbuf_offset
Bytes read from curbuf, i.e. buffer start offset.
Definition: sysio.h:663
ulongl pos(Error &err)
Get current file position.
Definition: sysio.h:527
ulongl savepos_
Read/Write: Used to save buffered read position when switching between read/write.
Definition: iobase.h:1691
bool flush()
Definition: iobase.h:937
IoReader bufrd_
Buffered reader.
Definition: iobase.h:1689
ulong used
Buffer size in use in bytes.
Definition: rawbuffer.h:37