SDXFrameWork  0.09
SDXFrameWork
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
File.h
1 #pragma once//☀SDL
2 #include <fstream>
3 #include <sstream>
4 #include <iostream>
5 #include <Multimedia/System.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 
54 #ifdef __ANDROID__
55  switch(Androidの保存先)
56  {
57  case SaveMode::Asset:
58  fileName = ファイル名;
59  break;
60  case SaveMode::External:
61  fileName = SDL_AndroidGetExternalStoragePath();
62  fileName += ファイル名;
63  break;
64  case SaveMode::Internal:
65  fileName = SDL_AndroidGetInternalStoragePath();
66  fileName += ファイル名;
67  break;
68  }
69 #else
70  fileName = ファイル名;
71 #endif
72 
73  isBinary = バイナリファイル;
74 
75  switch(読み書きモード)
76  {
77  case FileMode::Read:
78  if (isBinary) handle = SDL_RWFromFile( fileName.c_str() , "rb" );
79  else handle = SDL_RWFromFile(fileName.c_str(), "r");
80 
81  canRead = true;
82  canWrite = false;
83  canAdd = false;
84  break;
85  case FileMode::Write:
86  if (isBinary) handle = SDL_RWFromFile(fileName.c_str(), "wb");
87  else handle = SDL_RWFromFile(fileName.c_str(), "w");
88  canWrite = true;
89  canRead = false;
90  canAdd = false;
91  break;
92  case FileMode::Add:
93  if (isBinary) handle = SDL_RWFromFile(fileName.c_str(), "ab");
94  else handle = SDL_RWFromFile(fileName.c_str(), "a");
95  canWrite = true;
96  canAdd = true;
97  canRead = false;
98  break;
99  case FileMode::None:
100  break;
101  }
102 
103  if ( handle == nullptr )
104  {
105  canRead = false;
106  canWrite = false;
107  canAdd = false;
108  return false;
109  }
110 
111  return true;
112  }
113 
115  void Close()
116  {
117  if (handle)
118  {
119  SDL_RWclose(handle);
120  handle = nullptr;
121  }
122  canRead = false;
123  canWrite = false;
124  canAdd = false;
125  }
126 
129  {
130  if( this->canAdd ) return FileMode::Add;
131  if( this->canWrite) return FileMode::Write;
132  if( this->canRead ) return FileMode::Read;
133  return FileMode::None;
134  }
135 
137  const char* GetFileName()
138  {
139  return this->fileName.c_str();
140  }
141 
145  template< class T>
146  bool Read(T &読み込み先変数 )
147  {
148  if (!canRead) return false;
149 
150  SDL_RWread( handle, &読み込み先変数, sizeof(読み込み先変数), 1);
151 
152  return true;
153  }
154  bool Read(std::string &読み込み先変数)
155  {
156  if (!canRead) return false;
157 
158  int 文字数 = 0;
159  SDL_RWread(handle, &文字数, sizeof(int), 1);
160 
161  読み込み先変数.resize(文字数);
162  SDL_RWread(handle, (char*)読み込み先変数.c_str(), 文字数 , 1);
163 
164  return true;
165  }
166 
167  template< class T >
168  bool Read(T *読み込み先配列, int 要素数)
169  {
170  if (!canRead) return false;
171 
172  for (int a = 0; a < 要素数; ++a)
173  {
174  SDL_RWread(handle, &読み込み先配列[a], sizeof(T), 1);
175  }
176 
177  return true;
178  }
179 
181  template <class TSaveType,class TOutput>
182  bool Read(TOutput *読み込み先配列, int 要素数 , int 分母)
183  {
184  if (!canRead) return false;
185 
186  TSaveType buff;
187 
188  for (int a = 0; a < 要素数; ++a)
189  {
190  SDL_RWread(handle, &buff, sizeof(TSaveType), 1);
191  読み込み先配列[a] = TOutput(buff) / 分母;
192  }
193 
194  return true;
195  }
196 
197  template< class TSaveType, class TOutput>
198  bool Read(TOutput &読み込み先変数)
199  {
200  if (!canRead) return false;
201 
202  TSaveType buff;
203  SDL_RWread(handle, &buff, sizeof(TSaveType), 1);
204 
205  読み込み先変数 = TOutput(buff);
206 
207  return true;
208  }
209 
210 
211 
215  template< class T>
216  bool Write(T& 書込み元変数)
217  {
218  if (!canWrite) return false;
219 
220  SDL_RWwrite( handle , &書込み元変数 , sizeof(書込み元変数) , 1);
221 
222  return canWrite;
223  }
224  bool Write(std::string &書込み元変数)
225  {
226  if (!canWrite) return false;
227 
228  const int 文字数 = (int)書込み元変数.size();
229 
230  SDL_RWwrite(handle, &文字数, sizeof(int), 1);
231  SDL_RWwrite(handle, 書込み元変数.c_str(), 文字数, 1);
232 
233  return canWrite;
234  }
235 
238  template <class TSaveType, class TInput>
239  bool Write(TInput *書き込み元配列 , int 要素数 )
240  {
241  if (!canWrite) return false;
242 
243  for (int a = 0; a < 要素数; ++a)
244  {
245  TSaveType buff = (TSaveType)書き込み元配列[a];
246 
247  SDL_RWwrite(handle, &buff , sizeof(TSaveType), 1);
248 
249  }
250 
251  return true;
252  }
253 
255  template< class T>
256  bool ReadWrite(T &読み書き変数 )
257  {
258  if( canRead )
259  {
260  return Read(読み書き変数);
261  }else if( canWrite ){
262  return Write(読み書き変数);
263  }
264  return false;
265  }
266 
268  std::vector<std::string> GetLineS()
269  {
270  std::vector<std::string> lineS;
271 
272  if (canRead)
273  {
274  std::string all;
275  unsigned int fileSize = (unsigned int)handle->size(handle);
276  all.resize(fileSize);
277  SDL_RWread(handle, (char*)all.c_str(), fileSize, 1);
278 
279  std::string buf;
280  std::istringstream iss(all);
281 
282  while (std::getline(iss, buf, '\n'))
283  {
284  lineS.push_back(buf);
285  }
286  }
287  return lineS;
288  }
289 
291  std::vector<std::vector<std::string>> GetCsvS()
292  {
293  std::vector<std::vector<std::string>> lineS;
294 
295  if( canRead )
296  {
297  std::string all;
298  unsigned int fileSize = (unsigned int)handle->size(handle);
299  all.resize(fileSize);
300  SDL_RWread(handle, (char*)all.c_str(), fileSize, 1);
301 
302  int lineNo = 0;
303  std::string buf;
304  std::string buf2;
305  std::istringstream iss(all);
306 
307  while (std::getline(iss, buf, '\n'))
308  {
309  std::istringstream iss2(buf);
310  lineS.push_back(std::vector<std::string>());
311 
312  while (std::getline(iss2, buf2, ','))
313  {
314  lineS[lineNo].push_back(buf2);
315  }
316  ++lineNo;
317  }
318  }
319  return lineS;
320  }
321 
323  bool CheckEOF()
324  {
325  return (SDL_RWtell(handle) == RW_SEEK_END);
326  }
327 
328 };
329 }
bool Write(T &書込み元変数)
データを書き込む.
Definition: File.h:216
File(const char *ファイル名, FileMode 読み書きモード, bool バイナリファイル=false, SaveMode Androidの保存先=SaveMode::Asset)
ファイル名とモードを指定して、ファイルを開く.
Definition: File.h:40
bool CheckEOF()
ファイルの終端判定.
Definition: File.h:323
書込時、末尾に追加
bool Read(T &読み込み先変数)
データを読み込む.
Definition: File.h:146
SaveMode
Androidでの保存先.
Definition: File.h:19
void Close()
ファイルを閉じる.
Definition: File.h:115
const char * GetFileName()
ファイル名を取得.
Definition: File.h:137
入出力可能なテキストかバイナリファイルを表すクラス.
Definition: File.h:28
std::vector< std::string > GetLineS()
ファイルを改行区切りで一括して読み込む.
Definition: File.h:268
読込のみ
std::vector< std::vector< std::string > > GetCsvS()
カンマ区切りのCSVファイルを一括読込.
Definition: File.h:291
FileMode GetFileMode()
ファイルモードを取得.
Definition: File.h:128
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:182
bool ReadWrite(T &読み書き変数)
FileModeがReadの場合Read、WriteかAddの場合Writeを行う.
Definition: File.h:256
開かれていない
bool Write(TInput *書き込み元配列, int 要素数)
型変換をして書き込む.
Definition: File.h:239