libUTL++
BufferedFileStream.h
1 #pragma once
2 
4 
5 #include <libutl/BufferedFDstream.h>
6 #include <libutl/FileStream.h>
7 
9 
10 UTL_NS_BEGIN;
11 
13 
24 
27 {
29 
30 public:
31  BufferedFileStream(FileStream* fileStream)
32  {
33  setStream(fileStream);
34  }
35 
36  BufferedFileStream(int fd, uint_t mode = io_rdwr)
37  {
38  setStream(new FileStream(fd, mode));
39  }
40 
41  BufferedFileStream(const Pathname& path, uint_t mode = io_rdwr, uint_t createMode = uint_t_max)
42  {
43  setStream(new FileStream(path, mode, createMode));
44  }
45 
46  size_t
47  length() const
48  {
49  return pget()->length();
50  }
51 
52  void
53  open(int fd, uint_t mode = io_rdwr)
54  {
55  setStream(new FileStream(fd, mode));
56  }
57 
58  void
59  open(const Pathname& path, uint_t mode = io_rdwr, uint_t createMode = uint_t_max)
60  {
61  setStream(new FileStream(path, mode, createMode));
62  }
63 
64  void
65  rewind()
66  {
67  seek(0);
68  }
69 
70  ssize_t
71  seek(size_t offset)
72  {
73  return seekStart(offset);
74  }
75 
76  ssize_t
77  seekCur(ssize_t offset)
78  {
79  flush(io_rdwr);
80  return pget()->seekCur(offset);
81  }
82 
83  ssize_t
84  seekStart(ssize_t offset = 0)
85  {
86  flush(io_rdwr);
87  return pget()->seekStart(offset);
88  }
89 
90  ssize_t
91  seekEnd(long offset = 0)
92  {
93  flush(io_rdwr);
94  return pget()->seekEnd(offset);
95  }
96 
97  ssize_t
98  tell() const
99  {
100  return pget()->tell();
101  }
102 
103  void
104  truncate(ssize_t length = ssize_t_max)
105  {
106  flush(io_rdwr);
107  return pget()->truncate(length);
108  }
109 
110 private:
111  void
112  init()
113  {
114  setStream(new FileStream());
115  }
116 
117  void
118  deInit()
119  {
120  }
121 
122  const FileStream*
123  pget() const
124  {
125  ASSERTD(_stream != nullptr);
126  return utl::cast<FileStream>(_stream);
127  }
128 
129  FileStream*
130  pget()
131  {
132  ASSERTD(_stream != nullptr);
133  return utl::cast<FileStream>(_stream);
134  }
135 };
136 
138 
139 UTL_NS_END;
utl::BufferedStream & flush(utl::BufferedStream &stream)
Flush the stream&#39;s output.
void deInit()
De-initialize UTL++.
Filesystem pathname.
Definition: Pathname.h:32
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
read/write
Definition: util.h:60
Disk file stream.
Definition: FileStream.h:37
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
const uint_t uint_t_max
Maximum uint_t value.
Buffered disk file stream.
const ssize_t ssize_t_max
Maximum ssize_t value.
void init()
Initialize UTL++.
#define ASSERTD
Do an assertion in DEBUG mode only.
Buffered stream with file descriptor.