SDXFrameWork  0.12
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 
156  bool Read(std::string &読み込み先変数)
157  {
158  if (!canRead) return false;
159 
160  int 文字数 = 0;
161  SDL_RWread(handle, &文字数, sizeof(int), 1);
162 
163  if (文字数 == 0)
164  {
165  読み込み先変数 = "";
166  return true;
167  }
168 
169  読み込み先変数.resize(文字数);
170  SDL_RWread(handle, (char*)読み込み先変数.c_str(), 文字数, 1);
171 
172  return true;
173  }
174 
175  template< class T >
177  bool Read(T *読み込み先配列, int 要素数)
178  {
179  if (!canRead) return false;
180 
181  for (int a = 0; a < 要素数; ++a)
182  {
183  SDL_RWread(handle, &読み込み先配列[a], sizeof(T), 1);
184  }
185 
186  return true;
187  }
188 
189  template <class TSaveType, class TOutput>
192  bool Read(TOutput *読み込み先配列, int 要素数, int 分母)
193  {
194  if (!canRead) return false;
195 
196  TSaveType buff;
197 
198  for (int a = 0; a < 要素数; ++a)
199  {
200  SDL_RWread(handle, &buff, sizeof(TSaveType), 1);
201  読み込み先配列[a] = TOutput(buff) / 分母;
202  }
203 
204  return true;
205  }
206 
207  template< class TSaveType, class TOutput>
209  bool Read(TOutput &読み込み先変数)
210  {
211  if (!canRead) return false;
212 
213  TSaveType buff;
214  SDL_RWread(handle, &buff, sizeof(TSaveType), 1);
215 
216  読み込み先変数 = TOutput(buff);
217 
218  return true;
219  }
220 
221  template< class T>
225  bool Write(T& 書込み元変数)
226  {
227  if (!canWrite) return false;
228 
229  SDL_RWwrite(handle, &書込み元変数, sizeof(書込み元変数), 1);
230 
231  return canWrite;
232  }
233 
238  bool Write(std::string &書込み元変数)
239  {
240  if (!canWrite) return false;
241 
242  const int 文字数 = (int)書込み元変数.size();
243 
244  SDL_RWwrite(handle, &文字数, sizeof(int), 1);
245  SDL_RWwrite(handle, 書込み元変数.c_str(), 文字数, 1);
246 
247  return canWrite;
248  }
249 
250  template <class TSaveType, class TInput>
253  bool Write(TInput *書き込み元配列, int 要素数)
254  {
255  if (!canWrite) return false;
256 
257  for (int a = 0; a < 要素数; ++a)
258  {
259  TSaveType buff = (TSaveType)書き込み元配列[a];
260 
261  SDL_RWwrite(handle, &buff, sizeof(TSaveType), 1);
262  }
263 
264  return true;
265  }
266 
267  template< class T>
269  bool ReadWrite(T &読み書き変数)
270  {
271  if (canRead)
272  {
273  return Read(読み書き変数);
274  }
275  else if (canWrite){
276  return Write(読み書き変数);
277  }
278  return false;
279  }
280 
282  std::vector<std::string> GetLineS()
283  {
284  std::vector<std::string> lineS;
285 
286  if (canRead)
287  {
288  std::string all;
289  unsigned int fileSize = (unsigned int)handle->size(handle);
290  all.resize(fileSize);
291  SDL_RWread(handle, (char*)all.c_str(), fileSize, 1);
292 
293  std::string buf;
294  std::istringstream iss(all);
295  int numA,numB;
296 
297  while (std::getline(iss, buf, '\n'))
298  {
299  numA = buf.find_first_of('\r');
300  numB = buf.find_first_of('\n');
301  if (numA < numB){ numA = numB; };
302 
303  if (numA != std::string::npos)
304  {
305  buf = buf.substr(0, numA);
306  }
307 
308  lineS.push_back(buf.substr());
309  }
310  }
311  return lineS;
312  }
313 
315  std::vector<std::string> GetCsvToString()
316  {
317  std::vector<std::string> lineS;
318 
319  if (canRead)
320  {
321  std::string all;
322  unsigned int fileSize = (unsigned int)handle->size(handle);
323  all.resize(fileSize);
324  SDL_RWread(handle, (char*)all.c_str(), fileSize, 1);
325 
326  int lineNo = 0;
327  std::string buf;
328  std::string buf2;
329  std::istringstream iss(all);
330 
331  while (std::getline(iss, buf, '\n'))
332  {
333  std::istringstream iss2(buf);
334  while (std::getline(iss2, buf2, ','))
335  {
336  lineS.push_back(buf2);
337  }
338  ++lineNo;
339  }
340  }
341  return lineS;
342  }
343 
345  std::vector<std::vector<std::string>> GetCsvToString2()
346  {
347  std::vector<std::vector<std::string>> lineS;
348 
349  if (canRead)
350  {
351  std::string all;
352  unsigned int fileSize = (unsigned int)handle->size(handle);
353  all.resize(fileSize);
354  SDL_RWread(handle, (char*)all.c_str(), fileSize, 1);
355 
356  int lineNo = 0;
357  std::string buf;
358  std::string buf2;
359  std::istringstream iss(all);
360 
361  while (std::getline(iss, buf, '\n'))
362  {
363  std::istringstream iss2(buf);
364  lineS.push_back(std::vector<std::string>());
365 
366  while (std::getline(iss2, buf2, ','))
367  {
368  lineS[lineNo].push_back(buf2);
369  }
370  ++lineNo;
371  }
372  }
373  return lineS;
374  }
375 
377  std::vector<int> GetCsvToInt()
378  {
379  std::vector<int> lineS;
380 
381  if (canRead)
382  {
383  std::string all;
384  unsigned int fileSize = (unsigned int)handle->size(handle);
385  all.resize(fileSize);
386  SDL_RWread(handle, (char*)all.c_str(), fileSize, 1);
387 
388  int lineNo = 0;
389  std::string buf;
390  std::string buf2;
391  std::istringstream iss(all);
392 
393  while (std::getline(iss, buf, '\n'))
394  {
395  std::istringstream iss2(buf);
396 
397  while (std::getline(iss2, buf2, ','))
398  {
399  lineS.push_back(atoi(buf2.c_str()));
400  }
401  ++lineNo;
402  }
403  }
404  return lineS;
405  }
406 
408  std::vector<std::vector<int>> GetCsvToInt2()
409  {
410  std::vector<std::vector<int>> lineS;
411 
412  if (canRead)
413  {
414  std::string all;
415  unsigned int fileSize = (unsigned int)handle->size(handle);
416  all.resize(fileSize);
417  SDL_RWread(handle, (char*)all.c_str(), fileSize, 1);
418 
419  int lineNo = 0;
420  std::string buf;
421  std::string buf2;
422  std::istringstream iss(all);
423 
424  while (std::getline(iss, buf, '\n'))
425  {
426  std::istringstream iss2(buf);
427  lineS.push_back(std::vector<int>());
428 
429  while (std::getline(iss2, buf2, ','))
430  {
431  lineS[lineNo].push_back(atoi(buf2.c_str()));
432  }
433  ++lineNo;
434  }
435  }
436  return lineS;
437  }
438 
440  bool CheckEOF()
441  {
442  return (SDL_RWtell(handle) == RW_SEEK_END);
443  }
444  };
445 }
bool Write(T &書込み元変数)
データを書き込む.
Definition: File.h:225
std::vector< std::string > GetCsvToString()
カンマ区切りのCSVファイルを配列に文字列として一括読込.
Definition: File.h:315
File(const char *ファイル名, FileMode 読み書きモード, bool バイナリファイル=false, SaveMode Androidの保存先=SaveMode::Asset)
ファイル名とモードを指定して、ファイルを開く.
Definition: File.h:40
bool CheckEOF()
ファイルの終端判定.
Definition: File.h:440
各アプリケーションのフォルダ
書込時、末尾に追加
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:238
std::vector< std::string > GetLineS()
ファイルを改行区切りで一括して読み込む.
Definition: File.h:282
bool Read(TOutput &読み込み先変数)
型変換をしつつ読み込む.
Definition: File.h:209
読込のみ
bool Read(T *読み込み先配列, int 要素数)
データを読み込む.
Definition: File.h:177
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:192
std::vector< std::vector< std::string > > GetCsvToString2()
カンマ区切りのCSVファイルを二次元配列に文字列として一括読込.
Definition: File.h:345
bool ReadWrite(T &読み書き変数)
FileModeがReadの場合Read、WriteかAddの場合Writeを行う.
Definition: File.h:269
std::vector< std::vector< int > > GetCsvToInt2()
カンマ区切りのCSVファイルを二次元配列に整数として一括読込.
Definition: File.h:408
std::vector< int > GetCsvToInt()
カンマ区切りのCSVファイルを配列に整数として一括読込.
Definition: File.h:377
開かれていない
bool Write(TInput *書き込み元配列, int 要素数)
型変換をして書き込む.
Definition: File.h:253
bool Read(std::string &読み込み先変数)
文字列を読み込む.
Definition: File.h:156