file.cpp
Go to the documentation of this file.
1 #include <imagezero/file.h>
2 
3 #ifdef HAVE_CONFIG_H
4 #include "iz_config.h"
5 #endif
6 
7 #if defined(HAVE_MMAP)
8 
10 
11 #include <cstdlib>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include <sys/mman.h>
15 #include <sys/stat.h>
16 
17 namespace IZ
18 {
19  class InputFile::Private
20  {
21  public:
22  Private() : addr(MAP_FAILED)
23  {}
24 
25  void* addr;
26  size_t size;
27  };
28 
29  InputFile::InputFile(const char* filename)
30  : d(new Private)
31  {
32  int fd = open(filename, O_RDONLY);
33  if (fd != -1)
34  {
35  struct stat sb;
36  fstat(fd, &sb);
37  d->size = sb.st_size;
38  d->addr = mmap(0, d->size, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd, 0);
39  if (d->addr == MAP_FAILED)
40  {
41  d->addr = mmap(0, d->size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
42  if (d->addr != MAP_FAILED)
43  {
44  read(fd, d->addr, d->size);
45  }
46  }
47  close(fd);
48  }
49  }
50 
52  {
53  if (d->addr != MAP_FAILED)
54  {
55  munmap(d->addr, d->size);
56  }
57  delete d;
58  }
59 
60  bool InputFile::isReadable() const
61  {
62  return d->addr != MAP_FAILED;
63  }
64 
65  const unsigned char* InputFile::data() const
66  {
67  return (const unsigned char*) d->addr;
68  }
69 
70  size_t InputFile::dataSize() const
71  {
72  return d->size;
73  }
74 
75 
77  {
78  public:
79  Private()
80  {}
81 
82  int fd;
83  size_t mapSize;
84  };
85 
86  OutputFile::OutputFile(const char* filename)
87  : d(new Private)
88  {
89  d->fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644);
90  }
91 
93  {
94  if (d->fd != -1)
95  {
96  close(d->fd);
97  }
98  delete d;
99  }
100 
101  bool OutputFile::isWritable() const
102  {
103  if (d->fd != -1)
104  {
105  return true;
106  }
107  return false;
108  }
109 
110  unsigned char* OutputFile::prepareData(size_t maxSize)
111  {
112  if (d->fd != -1)
113  {
114  ftruncate(d->fd, maxSize);
115  d->mapSize = maxSize;
116  void* addr = mmap(0, d->mapSize, PROT_WRITE | PROT_READ, MAP_SHARED, d->fd, 0);
117  if (addr != MAP_FAILED)
118  {
119  return (unsigned char*) addr;
120  }
121  ftruncate(d->fd, 0);
122  d->mapSize = 0;
123  return (unsigned char*) malloc(maxSize);
124  }
125  return 0;
126  }
127 
128  void OutputFile::commitData(unsigned char* data, size_t size)
129  {
130  if (data)
131  {
132  if (d->mapSize)
133  {
134  munmap(data, d->mapSize);
135  ftruncate(d->fd, size);
136  }
137  else
138  {
139  write(d->fd, data, size);
140  free(data);
141  }
142  }
143  }
144 
145 #else // defined(HAVE_MMAP)
146 
148 
149 #include <cstdio>
150 #include <cstdlib>
151 
153  {
154  public:
155  Private() : buffer(0), size(0) { }
156 
157  unsigned char *buffer;
158  size_t size;
159  };
160 
161  InputFile::InputFile(const char *filename)
162  : d(new Private)
163  {
164  FILE *fp = fopen(filename, "rb");
165  if (fp) {
166  setbuf(fp, 0);
167  fseek(fp, 0, SEEK_END);
168  d->size = ftell(fp);
169  fseek(fp, 0, SEEK_SET);
170  if (d->size > 0) {
171  d->buffer = (unsigned char *) malloc(d->size);
172  if (d->buffer) {
173  d->size = fread(d->buffer, 1, d->size, fp);
174  }
175  }
176  fclose(fp);
177  }
178  }
179 
180  InputFile::~InputFile()
181  {
182  if (d->buffer) {
183  free(d->buffer);
184  }
185  delete d;
186  }
187 
188  bool InputFile::isReadable() const
189  {
190  return d->buffer != 0;
191  }
192 
193  const unsigned char *InputFile::data() const
194  {
195  return d->buffer;
196  }
197 
198  size_t InputFile::dataSize() const
199  {
200  return d->size;
201  }
202 
203 
205  {
206  public:
207  Private() { }
208 
209  FILE *fp;
210  };
211 
212  OutputFile::OutputFile(const char *filename)
213  : d(new Private)
214  {
215  d->fp = fopen(filename, "wb");
216  if (d->fp) {
217  setbuf(d->fp, 0);
218  }
219  }
220 
221  OutputFile::~OutputFile()
222  {
223  if (d->fp) {
224  fclose(d->fp);
225  }
226  delete d;
227  }
228 
229  bool OutputFile::isWritable() const
230  {
231  if (d->fp) {
232  return true;
233  }
234  return false;
235  }
236 
237  unsigned char *OutputFile::prepareData(size_t maxSize)
238  {
239  return (unsigned char *) malloc(maxSize);
240  }
241 
242  void OutputFile::commitData(unsigned char *data, size_t size)
243  {
244  if (data) {
245  if (d->fp) {
246  fwrite(data, 1, size, d->fp);
247  }
248  free(data);
249  }
250  }
251 
252 #endif // defined(HAVE_MMAP)
253 
254 }
Definition: bitcoder.h:6
OutputFile(const char *filename)
Private *const d
Definition: file.h:22
void commitData(unsigned char *data, size_t size)
InputFile(const char *filename)
unsigned char * buffer
Definition: file.cpp:157
bool isWritable() const
const unsigned char * data() const
bool isReadable() const
size_t dataSize() const
unsigned char * prepareData(size_t maxSize)
STDC implementation.
Definition: file.cpp:152


imagezero
Author(s):
autogenerated on Mon Jun 10 2019 13:37:21