SDXFrameWork  0.07
SDXFrameWork
 全て クラス ネームスペース 関数 変数 列挙型 列挙型の値 ページ
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 
149 
150  if (canRead)
151  {
152  SDL_RWread( handle, &読み込み先変数, sizeof(読み込み先変数), 1);
153  }
154  return canRead;
155  }
156  bool Read(std::string &読み込み先変数)
157  {
158  if (canRead)
159  {
160  int 文字数 = 0;
161  SDL_RWread(handle, &文字数, sizeof(int), 1);
162 
163  読み込み先変数.resize(文字数);
164  SDL_RWread(handle, (char*)読み込み先変数.c_str(), 文字数 , 1);
165  }
166  return canWrite;
167  }
168 
172  template< class T>
173  bool Write(T& 書込み元変数)
174  {
175  if (canWrite)
176  {
177  SDL_RWwrite( handle , &書込み元変数 , sizeof(書込み元変数) , 1);
178  }
179  return canWrite;
180  }
181  bool Write(std::string &書込み元変数)
182  {
183  if (canWrite)
184  {
185  const int 文字数 = (int)書込み元変数.size();
186 
187  SDL_RWwrite(handle, &文字数, sizeof(int), 1);
188  SDL_RWwrite(handle, 書込み元変数.c_str(), 文字数, 1);
189  }
190  return canWrite;
191  }
192 
194  template< class T>
195  bool ReadWrite(T &読み書き変数 )
196  {
197  if( canRead )
198  {
199  return Read(読み書き変数);
200  }else if( canWrite ){
201  return Write(読み書き変数);
202  }
203  return false;
204  }
205 
207  std::vector<std::string> GetLineS()
208  {
209  std::vector<std::string> lineS;
210 
211  if (canRead)
212  {
213  std::string all;
214  unsigned int fileSize = (unsigned int)handle->size(handle);
215  all.resize(fileSize);
216  SDL_RWread(handle, (char*)all.c_str(), fileSize, 1);
217 
218  std::string buf;
219  std::istringstream iss(all);
220 
221  while (std::getline(iss, buf, '\n'))
222  {
223  lineS.push_back(buf);
224  }
225  }
226  return lineS;
227  }
228 
230  std::vector<std::vector<std::string>> GetCsvS()
231  {
232  std::vector<std::vector<std::string>> lineS;
233 
234  if( canRead )
235  {
236  std::string all;
237  unsigned int fileSize = (unsigned int)handle->size(handle);
238  all.resize(fileSize);
239  SDL_RWread(handle, (char*)all.c_str(), fileSize, 1);
240 
241  int lineNo = 0;
242  std::string buf;
243  std::string buf2;
244  std::istringstream iss(all);
245 
246  while (std::getline(iss, buf, '\n'))
247  {
248  std::istringstream iss2(buf);
249  lineS.push_back(std::vector<std::string>());
250 
251  while (std::getline(iss2, buf2, ','))
252  {
253  lineS[lineNo].push_back(buf2);
254  }
255  ++lineNo;
256  }
257  }
258  return lineS;
259  }
260 
262  bool CheckEOF()
263  {
264  return (SDL_RWtell(handle) == RW_SEEK_END);
265  }
266 
267 };
268 }
bool Write(T &書込み元変数)
データを書き込む.
Definition: File.h:173
File(const char *ファイル名, FileMode 読み書きモード, bool バイナリファイル=false, SaveMode Androidの保存先=SaveMode::Asset)
ファイル名とモードを指定して、ファイルを開く.
Definition: File.h:40
bool CheckEOF()
ファイルの終端判定.
Definition: File.h:262
書込時、末尾に追加
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:207
読込のみ
std::vector< std::vector< std::string > > GetCsvS()
カンマ区切りのCSVファイルを一括読込.
Definition: File.h:230
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 ReadWrite(T &読み書き変数)
FileModeがReadの場合Read、WriteかAddの場合Writeを行う.
Definition: File.h:195
開かれていない