SDXFrameWork  0.13
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 
29  class File
30  {
31  private:
32  SDL_RWops *handle = nullptr;
33  bool canRead;
34  bool canWrite;
35  bool canAdd;
36  bool isBinary;
37  std::string fileName;
38  public:
39 
41  File(const char* ファイル名, FileMode 読み書きモード, bool バイナリファイル = false, SaveMode Androidの保存先 = SaveMode::Asset)
42  {
43  File::Open(ファイル名, 読み書きモード, バイナリファイル, Androidの保存先);
44  }
45 
46  ~File()
47  {
48  Close();
49  }
50 
52  bool Open(const char* ファイル名, FileMode 読み書きモード, bool バイナリファイル = false, SaveMode Androidの保存先 = SaveMode::Asset)
53  {
54 
55 #ifdef __ANDROID__
56  switch (Androidの保存先)
57  {
58  case SaveMode::Asset:
59  fileName = ファイル名;
60  break;
61  case SaveMode::External:
62  fileName = SDL_AndroidGetExternalStoragePath();
63  fileName += ファイル名;
64  break;
65  case SaveMode::Internal:
66  fileName = SDL_AndroidGetInternalStoragePath();
67  fileName += ファイル名;
68  break;
69  }
70 #else
71  #ifdef __WINDOWS__
72  fileName = ファイル名;
73  #else
74  fileName = SDL_GetBasePath();
75  fileName += "/";
76  fileName += ファイル名;
77  #endif
78 #endif
79 
80  isBinary = バイナリファイル;
81 
82  switch (読み書きモード)
83  {
84  case FileMode::Read:
85  if (isBinary){ handle = SDL_RWFromFile(fileName.c_str(), "rb"); }
86  else{ handle = SDL_RWFromFile(fileName.c_str(), "r"); }
87 
88  canRead = true;
89  canWrite = false;
90  canAdd = false;
91  break;
92  case FileMode::Write:
93  if (isBinary){ handle = SDL_RWFromFile(fileName.c_str(), "wb"); }
94  else{ handle = SDL_RWFromFile(fileName.c_str(), "w"); }
95  canWrite = true;
96  canRead = false;
97  canAdd = false;
98  break;
99  case FileMode::Add:
100  if (isBinary){ handle = SDL_RWFromFile(fileName.c_str(), "ab"); }
101  else{ handle = SDL_RWFromFile(fileName.c_str(), "a"); }
102  canWrite = true;
103  canAdd = true;
104  canRead = false;
105  break;
106  case FileMode::None:
107  break;
108  }
109 
110  if (handle == nullptr)
111  {
112  canRead = false;
113  canWrite = false;
114  canAdd = false;
115  return false;
116  }
117 
118  return true;
119  }
120 
122  void Close()
123  {
124  if (handle)
125  {
126  SDL_RWclose(handle);
127  handle = nullptr;
128  }
129  canRead = false;
130  canWrite = false;
131  canAdd = false;
132  }
133 
136  {
137  if (this->canAdd) return FileMode::Add;
138  if (this->canWrite) return FileMode::Write;
139  if (this->canRead) return FileMode::Read;
140  return FileMode::None;
141  }
142 
144  const char* GetFileName()
145  {
146  return this->fileName.c_str();
147  }
148 
149  template< class T>
152  bool Read(T &読み込み先変数)
153  {
154  if (!canRead) return false;
155 
156  SDL_RWread(handle, &読み込み先変数, sizeof(読み込み先変数), 1);
157 
158  return true;
159  }
160 
161  template < class T >
165  bool Read(std::vector<T> &読み込み元配列)
166  {
167  if (!canRead) return false;
168 
169  int 要素数;
170  SDL_RWread(handle, &要素数, sizeof(int), 1);
171 
172  読み込み元配列.clear();
173 
174  for (int a = 0; a < 要素数; ++a)
175  {
176  T 値;
177  SDL_RWread(handle, &値, sizeof(T), 1);
178  読み込み元配列.push_back(値);
179  }
180 
181  return true;
182  }
183 
187  bool Read(std::string &読み込み先変数)
188  {
189  if (!canRead) return false;
190 
191  int 文字数 = 0;
192  SDL_RWread(handle, &文字数, sizeof(int), 1);
193 
194  if (文字数 == 0)
195  {
196  読み込み先変数 = "";
197  return true;
198  }
199 
200  読み込み先変数.resize(文字数);
201  SDL_RWread(handle, (char*)読み込み先変数.c_str(), 文字数, 1);
202 
203  return true;
204  }
205 
206  template< class T >
208  bool Read(T *読み込み先配列, int 要素数)
209  {
210  if (!canRead) return false;
211 
212  for (int a = 0; a < 要素数; ++a)
213  {
214  SDL_RWread(handle, &読み込み先配列[a], sizeof(T), 1);
215  }
216 
217  return true;
218  }
219 
220  template <class TSaveType, class TOutput>
223  bool Read(TOutput *読み込み先配列, int 要素数, int 分母)
224  {
225  if (!canRead) return false;
226 
227  TSaveType buff;
228 
229  for (int a = 0; a < 要素数; ++a)
230  {
231  SDL_RWread(handle, &buff, sizeof(TSaveType), 1);
232  読み込み先配列[a] = TOutput(buff) / 分母;
233  }
234 
235  return true;
236  }
237 
238  template< class TSaveType, class TOutput>
240  bool Read(TOutput &読み込み先変数)
241  {
242  if (!canRead) return false;
243 
244  TSaveType buff;
245  SDL_RWread(handle, &buff, sizeof(TSaveType), 1);
246 
247  読み込み先変数 = TOutput(buff);
248 
249  return true;
250  }
251 
252  template< class T>
256  bool Write(T& 書込み元変数)
257  {
258  if (!canWrite) return false;
259 
260  SDL_RWwrite(handle, &書込み元変数, sizeof(書込み元変数), 1);
261 
262  return canWrite;
263  }
264 
269  bool Write(const std::string &書込み元変数 , bool is文字数 = true)
270  {
271  if (!canWrite) return false;
272 
273  const int 文字数 = (int)書込み元変数.size();
274 
275  if (is文字数)
276  {
277  SDL_RWwrite(handle, &文字数, sizeof(int), 1);
278  }
279 
280  if (文字数 == 0){ return canWrite; }
281 
282  SDL_RWwrite(handle, 書込み元変数.c_str(), 文字数, 1);
283 
284  return canWrite;
285  }
286 
287  bool Write(std::string &書込み元変数, bool is文字数 = true)
288  {
289  if (!canWrite) return false;
290 
291  const int 文字数 = (int)書込み元変数.size();
292 
293  if (is文字数)
294  {
295  SDL_RWwrite(handle, &文字数, sizeof(int), 1);
296  }
297 
298  if (文字数 == 0){ return canWrite; }
299 
300  SDL_RWwrite(handle, 書込み元変数.c_str(), 文字数, 1);
301 
302  return canWrite;
303  }
304 
305  template < class T >
309  bool Write(std::vector<T> &書込み元配列)
310  {
311  if (!canWrite) return false;
312 
313  int 要素数 = 書込み元配列.size();
314  SDL_RWwrite(handle, &要素数, sizeof(int), 1);
315 
316  for (int a = 0; a < 要素数; ++a)
317  {
318  SDL_RWwrite(handle, &書込み元配列[a], sizeof(T), 1);
319  }
320 
321  return true;
322  }
323 
324  template <class TSaveType, class TInput>
327  bool Write(TInput *書き込み元配列, int 要素数)
328  {
329  if (!canWrite) return false;
330 
331  for (int a = 0; a < 要素数; ++a)
332  {
333  TSaveType buff = (TSaveType)書き込み元配列[a];
334 
335  SDL_RWwrite(handle, &buff, sizeof(TSaveType), 1);
336  }
337 
338  return true;
339  }
340 
341  template< class T>
344  bool ReadWrite(T &読み書き変数)
345  {
346  if (canRead)
347  {
348  return Read(読み書き変数);
349  }
350  else if (canWrite){
351  return Write(読み書き変数);
352  }
353  return false;
354  }
355 
356  template< class T>
359  bool ReadWrite(T *読み書き変数 , int 要素数)
360  {
361  if (canRead)
362  {
363  return Read(読み書き変数 , 要素数);
364  }
365  else if (canWrite){
366  return Write(読み書き変数);
367  }
368  return false;
369  }
370 
372  bool AddLine(VariadicStream 文字列)
373  {
374  if (!canWrite) return false;
375 
376  文字列.StringS[0] += "\r\n";
377 
378  const int 文字数 = (int)文字列.StringS[0].size();
379 
380  if (文字数 == 0)
381  {
382  return false;
383  }
384  SDL_RWwrite(handle, 文字列.StringS[0].c_str(), 文字数, 1);
385 
386  return true;
387  }
388 
390  std::vector<std::string> GetLineS()
391  {
392  std::vector<std::string> 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  std::string buf;
402  std::istringstream iss(all);
403  int numA,numB;
404 
405  while (std::getline(iss, buf, '\n'))
406  {
407  numA = buf.find_first_of('\r');
408  numB = buf.find_first_of('\n');
409  if (numA < numB){ numA = numB; };
410 
411  if (numA != std::string::npos)
412  {
413  buf = buf.substr(0, numA);
414  }
415 
416  lineS.push_back(buf.substr());
417  }
418  }
419  return lineS;
420  }
421 
423  std::vector<std::string> GetCsvToString()
424  {
425  std::vector<std::string> lineS;
426 
427  if (canRead)
428  {
429  std::string all;
430  unsigned int fileSize = (unsigned int)handle->size(handle);
431  all.resize(fileSize);
432  SDL_RWread(handle, (char*)all.c_str(), fileSize, 1);
433 
434  int lineNo = 0;
435  std::string buf;
436  std::string buf2;
437  std::istringstream iss(all);
438 
439  while (std::getline(iss, buf, '\n'))
440  {
441  //\rがあれば消す
442  if (buf.find("\r") != std::string::npos)
443  {
444  buf.replace(buf.find("\r"), 1, "");
445  }
446 
447  std::istringstream iss2(buf);
448  while (std::getline(iss2, buf2, ','))
449  {
450  lineS.push_back(buf2);
451  }
452  ++lineNo;
453  }
454  }
455  return lineS;
456  }
457 
459  std::vector<std::vector<std::string>> GetCsvToString2()
460  {
461  std::vector<std::vector<std::string>> lineS;
462 
463  if (canRead)
464  {
465  std::string all;
466  unsigned int fileSize = (unsigned int)handle->size(handle);
467  all.resize(fileSize);
468  SDL_RWread(handle, (char*)all.c_str(), fileSize, 1);
469 
470  int lineNo = 0;
471  std::string buf;
472  std::string buf2;
473  std::istringstream iss(all);
474 
475  while (std::getline(iss, buf, '\n'))
476  {
477  //\rがあれば消す
478  if (buf.find("\r") != std::string::npos)
479  {
480  buf.replace(buf.find("\r"), 1, "");
481  }
482 
483  std::istringstream iss2(buf);
484  lineS.push_back(std::vector<std::string>());
485 
486  while (std::getline(iss2, buf2, ','))
487  {
488  lineS[lineNo].push_back(buf2);
489  }
490  ++lineNo;
491  }
492  }
493  return lineS;
494  }
495 
497  std::vector<int> GetCsvToInt()
498  {
499  std::vector<int> lineS;
500 
501  if (canRead)
502  {
503  std::string all;
504  unsigned int fileSize = (unsigned int)handle->size(handle);
505  all.resize(fileSize);
506  SDL_RWread(handle, (char*)all.c_str(), fileSize, 1);
507 
508  int lineNo = 0;
509  std::string buf;
510  std::string buf2;
511  std::istringstream iss(all);
512 
513  while (std::getline(iss, buf, '\n'))
514  {
515  std::istringstream iss2(buf);
516 
517  while (std::getline(iss2, buf2, ','))
518  {
519  lineS.push_back(atoi(buf2.c_str()));
520  }
521  ++lineNo;
522  }
523  }
524  return lineS;
525  }
526 
528  std::vector<std::vector<int>> GetCsvToInt2()
529  {
530  std::vector<std::vector<int>> lineS;
531 
532  if (canRead)
533  {
534  std::string all;
535  unsigned int fileSize = (unsigned int)handle->size(handle);
536  all.resize(fileSize);
537  SDL_RWread(handle, (char*)all.c_str(), fileSize, 1);
538 
539  int lineNo = 0;
540  std::string buf;
541  std::string buf2;
542  std::istringstream iss(all);
543 
544  while (std::getline(iss, buf, '\n'))
545  {
546  std::istringstream iss2(buf);
547  lineS.push_back(std::vector<int>());
548 
549  while (std::getline(iss2, buf2, ','))
550  {
551  lineS[lineNo].push_back(atoi(buf2.c_str()));
552  }
553  ++lineNo;
554  }
555  }
556  return lineS;
557  }
558 
560  bool CheckEOF()
561  {
562  return (SDL_RWtell(handle) == RW_SEEK_END);
563  }
564  };
565 }
bool Write(T &書込み元変数)
データを書き込む.
Definition: File.h:256
std::vector< std::string > GetCsvToString()
カンマ区切りのCSVファイルを配列に文字列として一括読込.
Definition: File.h:423
File(const char *ファイル名, FileMode 読み書きモード, bool バイナリファイル=false, SaveMode Androidの保存先=SaveMode::Asset)
ファイル名とモードを指定して、ファイルを開く.
Definition: File.h:41
bool CheckEOF()
ファイルの終端判定.
Definition: File.h:560
各アプリケーションのフォルダ
書込時、末尾に追加
bool Read(T &読み込み先変数)
データを読み込む.
Definition: File.h:152
SaveMode
Androidでの保存先.
Definition: File.h:19
void Close()
ファイルを閉じる.
Definition: File.h:122
内部ストレージ
外部ストレージ(SDカード)
const char * GetFileName()
ファイル名を取得.
Definition: File.h:144
bool ReadWrite(T *読み書き変数, int 要素数)
配列に読み書きする.
Definition: File.h:359
入出力可能なテキストかバイナリファイルを表すクラス.
Definition: File.h:29
std::vector< std::string > GetLineS()
ファイルを改行区切りで一括して読み込む.
Definition: File.h:390
bool Read(TOutput &読み込み先変数)
型変換をしつつ読み込む.
Definition: File.h:240
std::vector< std::string > StringS
一行ずつの文字列.
Definition: VariadicStream.h:53
読込のみ
bool Read(T *読み込み先配列, int 要素数)
データを読み込む.
Definition: File.h:208
bool Read(std::vector< T > &読み込み元配列)
可変長配列を読み込む.
Definition: File.h:165
bool AddLine(VariadicStream 文字列)
改行付きの文字列を一行書き込む.
Definition: File.h:372
FileMode GetFileMode()
ファイルモードを取得.
Definition: File.h:135
bool Open(const char *ファイル名, FileMode 読み書きモード, bool バイナリファイル=false, SaveMode Androidの保存先=SaveMode::Asset)
ファイル名とモードを指定して、ファイルを開く.
Definition: File.h:52
bool Write(std::vector< T > &書込み元配列)
可変長配列を読み込む.
Definition: File.h:309
可変数引数な文字列を処理するクラス.
Definition: VariadicStream.h:25
bool Write(const std::string &書込み元変数, bool is文字数=true)
文字列を書き込む.
Definition: File.h:269
FileMode
ファイルの読込書込モード.
Definition: File.h:10
bool Read(TOutput *読み込み先配列, int 要素数, int 分母)
型変換をしつつ配列に読み込む.
Definition: File.h:223
std::vector< std::vector< std::string > > GetCsvToString2()
カンマ区切りのCSVファイルを二次元配列に文字列として一括読込.
Definition: File.h:459
bool ReadWrite(T &読み書き変数)
変数を読み書きする.
Definition: File.h:344
std::vector< std::vector< int > > GetCsvToInt2()
カンマ区切りのCSVファイルを二次元配列に整数として一括読込.
Definition: File.h:528
std::vector< int > GetCsvToInt()
カンマ区切りのCSVファイルを配列に整数として一括読込.
Definition: File.h:497
開かれていない
bool Write(TInput *書き込み元配列, int 要素数)
型変換をして書き込む.
Definition: File.h:327
bool Read(std::string &読み込み先変数)
文字列を読み込む.
Definition: File.h:187