5 #include <Multimedia/SDX.h>
31 SDL_RWops *handle =
nullptr;
42 File::Open(ファイル名, 読み書きモード, バイナリファイル, Androidの保存先);
60 fileName = SDL_AndroidGetExternalStoragePath();
64 fileName = SDL_AndroidGetInternalStoragePath();
77 if (isBinary){ handle = SDL_RWFromFile(fileName.c_str(),
"rb"); }
78 else{ handle = SDL_RWFromFile(fileName.c_str(),
"r"); }
85 if (isBinary){ handle = SDL_RWFromFile(fileName.c_str(),
"wb"); }
86 else{ handle = SDL_RWFromFile(fileName.c_str(),
"w"); }
92 if (isBinary){ handle = SDL_RWFromFile(fileName.c_str(),
"ab"); }
93 else{ handle = SDL_RWFromFile(fileName.c_str(),
"a"); }
102 if (handle ==
nullptr)
138 return this->fileName.c_str();
146 if (!canRead)
return false;
148 SDL_RWread(handle, &読み込み先変数,
sizeof(読み込み先変数), 1);
155 bool Read(std::string &読み込み先変数)
157 if (!canRead)
return false;
160 SDL_RWread(handle, &文字数,
sizeof(
int), 1);
163 SDL_RWread(handle, (
char*)読み込み先変数.c_str(), 文字数, 1);
172 if (!canRead)
return false;
174 for (
int a = 0; a < 要素数; ++a)
176 SDL_RWread(handle, &読み込み先配列[a],
sizeof(T), 1);
182 template <
class TSaveType,
class TOutput>
185 bool Read(TOutput *読み込み先配列,
int 要素数,
int 分母)
187 if (!canRead)
return false;
191 for (
int a = 0; a < 要素数; ++a)
193 SDL_RWread(handle, &buff,
sizeof(TSaveType), 1);
194 読み込み先配列[a] = TOutput(buff) / 分母;
200 template<
class TSaveType,
class TOutput>
204 if (!canRead)
return false;
207 SDL_RWread(handle, &buff,
sizeof(TSaveType), 1);
209 読み込み先変数 = TOutput(buff);
220 if (!canWrite)
return false;
222 SDL_RWwrite(handle, &書込み元変数,
sizeof(書込み元変数), 1);
232 if (!canWrite)
return false;
234 const int 文字数 = (int)書込み元変数.size();
236 SDL_RWwrite(handle, &文字数,
sizeof(
int), 1);
237 SDL_RWwrite(handle, 書込み元変数.c_str(), 文字数, 1);
242 template <
class TSaveType,
class TInput>
245 bool Write(TInput *書き込み元配列,
int 要素数)
247 if (!canWrite)
return false;
249 for (
int a = 0; a < 要素数; ++a)
251 TSaveType buff = (TSaveType)書き込み元配列[a];
253 SDL_RWwrite(handle, &buff,
sizeof(TSaveType), 1);
268 return Write(読み書き変数);
276 std::vector<std::string> lineS;
281 unsigned int fileSize = (
unsigned int)handle->size(handle);
282 all.resize(fileSize);
283 SDL_RWread(handle, (
char*)all.c_str(), fileSize, 1);
286 std::istringstream iss(all);
288 while (std::getline(iss, buf,
'\n'))
290 lineS.push_back(buf);
299 std::vector<std::string> lineS;
304 unsigned int fileSize = (
unsigned int)handle->size(handle);
305 all.resize(fileSize);
306 SDL_RWread(handle, (
char*)all.c_str(), fileSize, 1);
311 std::istringstream iss(all);
313 while (std::getline(iss, buf,
'\n'))
315 std::istringstream iss2(buf);
316 while (std::getline(iss2, buf2,
','))
318 lineS.push_back(buf2);
329 std::vector<std::vector<std::string>> lineS;
334 unsigned int fileSize = (
unsigned int)handle->size(handle);
335 all.resize(fileSize);
336 SDL_RWread(handle, (
char*)all.c_str(), fileSize, 1);
341 std::istringstream iss(all);
343 while (std::getline(iss, buf,
'\n'))
345 std::istringstream iss2(buf);
346 lineS.push_back(std::vector<std::string>());
348 while (std::getline(iss2, buf2,
','))
350 lineS[lineNo].push_back(buf2);
361 std::vector<int> lineS;
366 unsigned int fileSize = (
unsigned int)handle->size(handle);
367 all.resize(fileSize);
368 SDL_RWread(handle, (
char*)all.c_str(), fileSize, 1);
373 std::istringstream iss(all);
375 while (std::getline(iss, buf,
'\n'))
377 std::istringstream iss2(buf);
379 while (std::getline(iss2, buf2,
','))
381 lineS.push_back(atoi(buf2.c_str()));
392 std::vector<std::vector<int>> lineS;
397 unsigned int fileSize = (
unsigned int)handle->size(handle);
398 all.resize(fileSize);
399 SDL_RWread(handle, (
char*)all.c_str(), fileSize, 1);
404 std::istringstream iss(all);
406 while (std::getline(iss, buf,
'\n'))
408 std::istringstream iss2(buf);
409 lineS.push_back(std::vector<int>());
411 while (std::getline(iss2, buf2,
','))
413 lineS[lineNo].push_back(atoi(buf2.c_str()));
424 return (SDL_RWtell(handle) == RW_SEEK_END);
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
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