SDXFrameWork  0.11
SDXFrameWork
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
File.h
1 //Copyright © 2014 SDXFramework
2 //[License]GNU Affero General Public License, version 3
3 //[Contact]http://sourceforge.jp/projects/dxframework/
4 #pragma once
5 #include <Multimedia/SDX.h>
6 
7 namespace SDX
8 {
10  enum class FileMode
11  {
12  Read,
13  Write,
14  Add,
15  None,
16  };
17 
19  enum class SaveMode
20  {
21  Asset,
22  Internal,
23  External,
24  };
25 
28  class File
29  {
30  private:
31  SDL_RWops *handle = nullptr;
32  bool canRead;
33  bool canWrite;
34  bool canAdd;
35  bool isBinary;
36  std::string fileName;
37  public:
38 
40  File(const char* ファイル名, FileMode 読み書きモード, bool バイナリファイル = false, SaveMode Androidの保存先 = SaveMode::Asset)
41  {
42  File::Open(ファイル名, 読み書きモード, バイナリファイル, Androidの保存先);
43  }
44 
45  ~File()
46  {
47  Close();
48  }
49 
51  bool Open(const char* ファイル名, FileMode 読み書きモード, bool バイナリファイル = false, SaveMode Androidの保存先 = SaveMode::Asset)
52  {
53 #ifdef __ANDROID__
54  switch (Androidの保存先)
55  {
56  case SaveMode::Asset:
57  fileName = ファイル名;
58  break;
59  case SaveMode::External:
60  fileName = SDL_AndroidGetExternalStoragePath();
61  fileName += ファイル名;
62  break;
63  case SaveMode::Internal:
64  fileName = SDL_AndroidGetInternalStoragePath();
65  fileName += ファイル名;
66  break;
67  }
68 #else
69  fileName = ファイル名;
70 #endif
71 
72  isBinary = バイナリファイル;
73 
74  switch (読み書きモード)
75  {
76  case FileMode::Read:
77  if (isBinary){ handle = SDL_RWFromFile(fileName.c_str(), "rb"); }
78  else{ handle = SDL_RWFromFile(fileName.c_str(), "r"); }
79 
80  canRead = true;
81  canWrite = false;
82  canAdd = false;
83  break;
84  case FileMode::Write:
85  if (isBinary){ handle = SDL_RWFromFile(fileName.c_str(), "wb"); }
86  else{ handle = SDL_RWFromFile(fileName.c_str(), "w"); }
87  canWrite = true;
88  canRead = false;
89  canAdd = false;
90  break;
91  case FileMode::Add:
92  if (isBinary){ handle = SDL_RWFromFile(fileName.c_str(), "ab"); }
93  else{ handle = SDL_RWFromFile(fileName.c_str(), "a"); }
94  canWrite = true;
95  canAdd = true;
96  canRead = false;
97  break;
98  case FileMode::None:
99  break;
100  }
101 
102  if (handle == nullptr)
103  {
104  canRead = false;
105  canWrite = false;
106  canAdd = false;
107  return false;
108  }
109 
110  return true;
111  }
112 
114  void Close()
115  {
116  if (handle)
117  {
118  SDL_RWclose(handle);
119  handle = nullptr;
120  }
121  canRead = false;
122  canWrite = false;
123  canAdd = false;
124  }
125 
128  {
129  if (this->canAdd) return FileMode::Add;
130  if (this->canWrite) return FileMode::Write;
131  if (this->canRead) return FileMode::Read;
132  return FileMode::None;
133  }
134 
136  const char* GetFileName()
137  {
138  return this->fileName.c_str();
139  }
140 
141  template< class T>
144  bool Read(T &読み込み先変数)
145  {
146  if (!canRead) return false;
147 
148  SDL_RWread(handle, &読み込み先変数, sizeof(読み込み先変数), 1);
149 
150  return true;
151  }
152 
155  bool Read(std::string &読み込み先変数)
156  {
157  if (!canRead) return false;
158 
159  int 文字数 = 0;
160  SDL_RWread(handle, &文字数, sizeof(int), 1);
161 
162  読み込み先変数.resize(文字数);
163  SDL_RWread(handle, (char*)読み込み先変数.c_str(), 文字数, 1);
164 
165  return true;
166  }
167 
168  template< class T >
170  bool Read(T *読み込み先配列, int 要素数)
171  {
172  if (!canRead) return false;
173 
174  for (int a = 0; a < 要素数; ++a)
175  {
176  SDL_RWread(handle, &読み込み先配列[a], sizeof(T), 1);
177  }
178 
179  return true;
180  }
181 
182  template <class TSaveType, class TOutput>
185  bool Read(TOutput *読み込み先配列, int 要素数, int 分母)
186  {
187  if (!canRead) return false;
188 
189  TSaveType buff;
190 
191  for (int a = 0; a < 要素数; ++a)
192  {
193  SDL_RWread(handle, &buff, sizeof(TSaveType), 1);
194  読み込み先配列[a] = TOutput(buff) / 分母;
195  }
196 
197  return true;
198  }
199 
200  template< class TSaveType, class TOutput>
202  bool Read(TOutput &読み込み先変数)
203  {
204  if (!canRead) return false;
205 
206  TSaveType buff;
207  SDL_RWread(handle, &buff, sizeof(TSaveType), 1);
208 
209  読み込み先変数 = TOutput(buff);
210 
211  return true;
212  }
213 
214  template< class T>
218  bool Write(T& 書込み元変数)
219  {
220  if (!canWrite) return false;
221 
222  SDL_RWwrite(handle, &書込み元変数, sizeof(書込み元変数), 1);
223 
224  return canWrite;
225  }
226 
230  bool Write(std::string &書込み元変数)
231  {
232  if (!canWrite) return false;
233 
234  const int 文字数 = (int)書込み元変数.size();
235 
236  SDL_RWwrite(handle, &文字数, sizeof(int), 1);
237  SDL_RWwrite(handle, 書込み元変数.c_str(), 文字数, 1);
238 
239  return canWrite;
240  }
241 
242  template <class TSaveType, class TInput>
245  bool Write(TInput *書き込み元配列, int 要素数)
246  {
247  if (!canWrite) return false;
248 
249  for (int a = 0; a < 要素数; ++a)
250  {
251  TSaveType buff = (TSaveType)書き込み元配列[a];
252 
253  SDL_RWwrite(handle, &buff, sizeof(TSaveType), 1);
254  }
255 
256  return true;
257  }
258 
259  template< class T>
261  bool ReadWrite(T &読み書き変数)
262  {
263  if (canRead)
264  {
265  return Read(読み書き変数);
266  }
267  else if (canWrite){
268  return Write(読み書き変数);
269  }
270  return false;
271  }
272 
274  std::vector<std::string> GetLineS()
275  {
276  std::vector<std::string> lineS;
277 
278  if (canRead)
279  {
280  std::string all;
281  unsigned int fileSize = (unsigned int)handle->size(handle);
282  all.resize(fileSize);
283  SDL_RWread(handle, (char*)all.c_str(), fileSize, 1);
284 
285  std::string buf;
286  std::istringstream iss(all);
287 
288  while (std::getline(iss, buf, '\n'))
289  {
290  lineS.push_back(buf);
291  }
292  }
293  return lineS;
294  }
295 
297  std::vector<std::string> GetCsvToString()
298  {
299  std::vector<std::string> lineS;
300 
301  if (canRead)
302  {
303  std::string all;
304  unsigned int fileSize = (unsigned int)handle->size(handle);
305  all.resize(fileSize);
306  SDL_RWread(handle, (char*)all.c_str(), fileSize, 1);
307 
308  int lineNo = 0;
309  std::string buf;
310  std::string buf2;
311  std::istringstream iss(all);
312 
313  while (std::getline(iss, buf, '\n'))
314  {
315  std::istringstream iss2(buf);
316  while (std::getline(iss2, buf2, ','))
317  {
318  lineS.push_back(buf2);
319  }
320  ++lineNo;
321  }
322  }
323  return lineS;
324  }
325 
327  std::vector<std::vector<std::string>> GetCsvToString2()
328  {
329  std::vector<std::vector<std::string>> lineS;
330 
331  if (canRead)
332  {
333  std::string all;
334  unsigned int fileSize = (unsigned int)handle->size(handle);
335  all.resize(fileSize);
336  SDL_RWread(handle, (char*)all.c_str(), fileSize, 1);
337 
338  int lineNo = 0;
339  std::string buf;
340  std::string buf2;
341  std::istringstream iss(all);
342 
343  while (std::getline(iss, buf, '\n'))
344  {
345  std::istringstream iss2(buf);
346  lineS.push_back(std::vector<std::string>());
347 
348  while (std::getline(iss2, buf2, ','))
349  {
350  lineS[lineNo].push_back(buf2);
351  }
352  ++lineNo;
353  }
354  }
355  return lineS;
356  }
357 
359  std::vector<int> GetCsvToInt()
360  {
361  std::vector<int> lineS;
362 
363  if (canRead)
364  {
365  std::string all;
366  unsigned int fileSize = (unsigned int)handle->size(handle);
367  all.resize(fileSize);
368  SDL_RWread(handle, (char*)all.c_str(), fileSize, 1);
369 
370  int lineNo = 0;
371  std::string buf;
372  std::string buf2;
373  std::istringstream iss(all);
374 
375  while (std::getline(iss, buf, '\n'))
376  {
377  std::istringstream iss2(buf);
378 
379  while (std::getline(iss2, buf2, ','))
380  {
381  lineS.push_back(atoi(buf2.c_str()));
382  }
383  ++lineNo;
384  }
385  }
386  return lineS;
387  }
388 
390  std::vector<std::vector<int>> GetCsvToInt2()
391  {
392  std::vector<std::vector<int>> lineS;
393 
394  if (canRead)
395  {
396  std::string all;
397  unsigned int fileSize = (unsigned int)handle->size(handle);
398  all.resize(fileSize);
399  SDL_RWread(handle, (char*)all.c_str(), fileSize, 1);
400 
401  int lineNo = 0;
402  std::string buf;
403  std::string buf2;
404  std::istringstream iss(all);
405 
406  while (std::getline(iss, buf, '\n'))
407  {
408  std::istringstream iss2(buf);
409  lineS.push_back(std::vector<int>());
410 
411  while (std::getline(iss2, buf2, ','))
412  {
413  lineS[lineNo].push_back(atoi(buf2.c_str()));
414  }
415  ++lineNo;
416  }
417  }
418  return lineS;
419  }
420 
422  bool CheckEOF()
423  {
424  return (SDL_RWtell(handle) == RW_SEEK_END);
425  }
426  };
427 }
bool Write(T &書込み元変数)
データを書き込む.
Definition: File.h:218
std::vector< std::string > GetCsvToString()
カンマ区切りのCSVファイルを配列に文字列として一括読込.
Definition: File.h:297
File(const char *ファイル名, FileMode 読み書きモード, bool バイナリファイル=false, SaveMode Androidの保存先=SaveMode::Asset)
ファイル名とモードを指定して、ファイルを開く.
Definition: File.h:40
bool CheckEOF()
ファイルの終端判定.
Definition: File.h:422
各アプリケーションのフォルダ
書込時、末尾に追加
bool Read(T &読み込み先変数)
データを読み込む.
Definition: File.h:144
SaveMode
Androidでの保存先.
Definition: File.h:19
void Close()
ファイルを閉じる.
Definition: File.h:114
内部ストレージ
外部ストレージ(SDカード)
const char * GetFileName()
ファイル名を取得.
Definition: File.h:136
入出力可能なテキストかバイナリファイルを表すクラス.
Definition: File.h:28
bool Write(std::string &書込み元変数)
文字列を書き込む.
Definition: File.h:230
std::vector< std::string > GetLineS()
ファイルを改行区切りで一括して読み込む.
Definition: File.h:274
bool Read(TOutput &読み込み先変数)
型変換をしつつ読み込む.
Definition: File.h:202
読込のみ
bool Read(T *読み込み先配列, int 要素数)
データを読み込む.
Definition: File.h:170
FileMode GetFileMode()
ファイルモードを取得.
Definition: File.h:127
bool Open(const char *ファイル名, FileMode 読み書きモード, bool バイナリファイル=false, SaveMode Androidの保存先=SaveMode::Asset)
ファイル名とモードを指定して、ファイルを開く.
Definition: File.h:51
FileMode
ファイルの読込書込モード.
Definition: File.h:10
bool Read(TOutput *読み込み先配列, int 要素数, int 分母)
型変換をしつつ配列に読み込む.
Definition: File.h:185
std::vector< std::vector< std::string > > GetCsvToString2()
カンマ区切りのCSVファイルを二次元配列に文字列として一括読込.
Definition: File.h:327
bool ReadWrite(T &読み書き変数)
FileModeがReadの場合Read、WriteかAddの場合Writeを行う.
Definition: File.h:261
std::vector< std::vector< int > > GetCsvToInt2()
カンマ区切りのCSVファイルを二次元配列に整数として一括読込.
Definition: File.h:390
std::vector< int > GetCsvToInt()
カンマ区切りのCSVファイルを配列に整数として一括読込.
Definition: File.h:359
開かれていない
bool Write(TInput *書き込み元配列, int 要素数)
型変換をして書き込む.
Definition: File.h:245
bool Read(std::string &読み込み先変数)
文字列を読み込む.
Definition: File.h:155