The Battle for Wesnoth  1.15.7+dev
filesystem_sdl.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017-2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #include <SDL2/SDL.h>
15 #include <SDL2/SDL_rwops.h>
16 
17 #include "filesystem.hpp"
18 #include "log.hpp"
19 
20 #include <algorithm>
21 #include <cassert>
22 
23 static lg::log_domain log_filesystem("filesystem");
24 #define ERR_FS LOG_STREAM(err, log_filesystem)
25 
26 namespace filesystem {
27 
28 // Arbitrary numbers larger than 5
29 static const uint32_t read_type = 7;
30 static const uint32_t write_type = 8;
31 
32 static int64_t ifs_size (struct SDL_RWops * context);
33 static int64_t ofs_size (struct SDL_RWops * context);
34 static int64_t SDLCALL ifs_seek(struct SDL_RWops *context, int64_t offset, int whence);
35 static int64_t SDLCALL ofs_seek(struct SDL_RWops *context, int64_t offset, int whence);
36 static std::size_t SDLCALL ifs_read(struct SDL_RWops *context, void *ptr, std::size_t size, std::size_t maxnum);
37 static std::size_t SDLCALL ofs_read(struct SDL_RWops *context, void *ptr, std::size_t size, std::size_t maxnum);
38 static std::size_t SDLCALL ifs_write(struct SDL_RWops *context, const void *ptr, std::size_t size, std::size_t num);
39 static std::size_t SDLCALL ofs_write(struct SDL_RWops *context, const void *ptr, std::size_t size, std::size_t num);
40 static int SDLCALL ifs_close(struct SDL_RWops *context);
41 static int SDLCALL ofs_close(struct SDL_RWops *context);
42 
44  rwops_ptr rw(SDL_AllocRW(), &SDL_FreeRW);
45 
46  rw->size = &ifs_size;
47  rw->seek = &ifs_seek;
48  rw->read = &ifs_read;
49  rw->write = &ifs_write;
50  rw->close = &ifs_close;
51 
52  rw->type = read_type;
53 
54  scoped_istream ifs = istream_file(path);
55  if(!ifs) {
56  ERR_FS << "make_read_RWops: istream_file returned NULL on " << path << '\n';
57  rw.reset();
58  return rw;
59  }
60 
61  rw->hidden.unknown.data1 = ifs.release();
62 
63  return rw;
64 }
65 
67  rwops_ptr rw(SDL_AllocRW(), &SDL_FreeRW);
68 
69  rw->size = &ofs_size;
70  rw->seek = &ofs_seek;
71  rw->read = &ofs_read;
72  rw->write = &ofs_write;
73  rw->close = &ofs_close;
74 
75  rw->type = write_type;
76 
77  scoped_ostream ofs = ostream_file(path);
78  if(!ofs) {
79  ERR_FS << "make_write_RWops: ostream_file returned NULL on " << path << '\n';
80  rw.reset();
81  return rw;
82  }
83 
84  rw->hidden.unknown.data1 = ofs.release();
85 
86  return rw;
87 }
88 
89 static int64_t ifs_size (struct SDL_RWops * context) {
90  std::istream *ifs = static_cast<std::istream*>(context->hidden.unknown.data1);
91  std::streampos orig = ifs->tellg();
92 
93  ifs->seekg(0, std::ios::end);
94 
95  std::streampos len = ifs->tellg();
96 
97  ifs->seekg(orig);
98 
99  return len;
100 }
101 static int64_t ofs_size (struct SDL_RWops * context) {
102  std::ostream *ofs = static_cast<std::ostream*>(context->hidden.unknown.data1);
103  std::streampos orig = ofs->tellp();
104 
105  ofs->seekp(0, std::ios::end);
106 
107  std::streampos len = ofs->tellp();
108 
109  ofs->seekp(orig);
110 
111  return len;
112 }
113 
114 typedef std::pair<int64_t, std::ios_base::seekdir> offset_dir;
115 
116 static offset_dir translate_seekdir(int64_t offset, int whence) {
117  switch(whence){
118  case RW_SEEK_SET:
119  return std::make_pair(std::max<int64_t>(0, offset), std::ios_base::beg);
120  case RW_SEEK_CUR:
121  return std::make_pair(offset, std::ios_base::cur);
122  case RW_SEEK_END:
123  return std::make_pair(std::min<int64_t>(0, offset), std::ios_base::end);
124  default:
125  assert(false);
126  throw "assertion ignored";
127  }
128 }
129 static int64_t SDLCALL ifs_seek(struct SDL_RWops *context, int64_t offset, int whence) {
130  std::ios_base::seekdir seekdir;
131  std::tie(offset, seekdir) = translate_seekdir(offset, whence);
132 
133  std::istream *ifs = static_cast<std::istream*>(context->hidden.unknown.data1);
134  const std::ios_base::iostate saved_state = ifs->rdstate();
135 
136  ifs->seekg(offset, seekdir);
137 
138  if(saved_state != ifs->rdstate() && offset < 0) {
139  ifs->clear(saved_state);
140  ifs->seekg(0, std::ios_base::beg);
141  }
142 
143  std::streamsize pos = ifs->tellg();
144  return static_cast<int>(pos);
145 }
146 static int64_t SDLCALL ofs_seek(struct SDL_RWops *context, int64_t offset, int whence) {
147  std::ios_base::seekdir seekdir;
148  std::tie(offset, seekdir) = translate_seekdir(offset, whence);
149 
150  std::ostream *ofs = static_cast<std::ostream*>(context->hidden.unknown.data1);
151  const std::ios_base::iostate saved_state = ofs->rdstate();
152 
153  ofs->seekp(offset, seekdir);
154 
155  if(saved_state != ofs->rdstate() && offset < 0) {
156  ofs->clear(saved_state);
157  ofs->seekp(0, std::ios_base::beg);
158  }
159 
160  std::streamsize pos = ofs->tellp();
161  return static_cast<int>(pos);
162 }
163 
164 static std::size_t SDLCALL ifs_read(struct SDL_RWops *context, void *ptr, std::size_t size, std::size_t maxnum) {
165  std::istream *ifs = static_cast<std::istream*>(context->hidden.unknown.data1);
166 
167  // This seems overly simplistic, but it's the same as mem_read's implementation
168  ifs->read(static_cast<char*>(ptr), maxnum * size);
169  std::streamsize num = ifs->good() ? maxnum : ifs->gcount() / size;
170 
171  // EOF sticks unless we clear it. Bad is an actual I/O error
172  if(!ifs->bad())
173  ifs->clear();
174 
175  return static_cast<int>(num);
176 }
177 static std::size_t SDLCALL ofs_read(struct SDL_RWops * /*context*/, void * /*ptr*/, std::size_t /*size*/, std::size_t /*maxnum*/) {
178  SDL_SetError("Reading not implemented");
179  return 0;
180 }
181 
182 static std::size_t SDLCALL ifs_write(struct SDL_RWops * /*context*/, const void * /*ptr*/, std::size_t /*size*/, std::size_t /*num*/) {
183  SDL_SetError("Writing not implemented");
184  return 0;
185 }
186 static std::size_t SDLCALL ofs_write(struct SDL_RWops *context, const void *ptr, std::size_t size, std::size_t num) {
187  std::ostream *ofs = static_cast<std::ostream*>(context->hidden.unknown.data1);
188 
189  const std::streampos before = ofs->tellp();
190  ofs->write(static_cast<const char*>(ptr), num * size);
191  const std::streampos after = ofs->tellp();
192  const std::streamoff bytes_written = after - before;
193  const int num_written = bytes_written / size;
194 
195  return num_written;
196 }
197 
198 static int SDLCALL ifs_close(struct SDL_RWops *context) {
199  if (context) {
200  std::istream *ifs = static_cast<std::istream*>(context->hidden.unknown.data1);
201  delete ifs;
202  SDL_FreeRW(context);
203  }
204  return 0;
205 }
206 static int SDLCALL ofs_close(struct SDL_RWops *context) {
207  if (context) {
208  std::ostream *ofs = static_cast<std::ostream*>(context->hidden.unknown.data1);
209  delete ofs;
210  SDL_FreeRW(context);
211  }
212  return 0;
213 }
214 
215 }
static int SDLCALL ofs_close(struct SDL_RWops *context)
static offset_dir translate_seekdir(int64_t offset, int whence)
static int SDLCALL ifs_close(struct SDL_RWops *context)
std::pair< int64_t, std::ios_base::seekdir > offset_dir
static std::size_t SDLCALL ofs_write(struct SDL_RWops *context, const void *ptr, std::size_t size, std::size_t num)
rwops_ptr make_read_RWops(const std::string &path)
filesystem::scoped_istream istream_file(const std::string &fname, bool treat_failure_as_error)
size_t len
Definition: result_set.cpp:277
rwops_ptr make_write_RWops(const std::string &path)
static std::size_t SDLCALL ifs_read(struct SDL_RWops *context, void *ptr, std::size_t size, std::size_t maxnum)
std::unique_ptr< std::istream > scoped_istream
Definition: filesystem.hpp:39
std::string path
Definition: game_config.cpp:39
static std::size_t SDLCALL ifs_write(struct SDL_RWops *context, const void *ptr, std::size_t size, std::size_t num)
static int64_t SDLCALL ofs_seek(struct SDL_RWops *context, int64_t offset, int whence)
std::unique_ptr< std::ostream > scoped_ostream
Definition: filesystem.hpp:40
static std::size_t SDLCALL ofs_read(struct SDL_RWops *context, void *ptr, std::size_t size, std::size_t maxnum)
u64 size
Definition: statement.cpp:80
Declarations for File-IO.
std::unique_ptr< SDL_RWops, void(*)(SDL_RWops *)> rwops_ptr
Definition: filesystem.hpp:42
static int64_t ifs_size(struct SDL_RWops *context)
static const uint32_t write_type
Standard logging facilities (interface).
static const uint32_t read_type
#define ERR_FS
static int64_t SDLCALL ifs_seek(struct SDL_RWops *context, int64_t offset, int whence)
static lg::log_domain log_filesystem("filesystem")
static int64_t ofs_size(struct SDL_RWops *context)
filesystem::scoped_ostream ostream_file(const std::string &fname, bool create_directory)