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);
156 bool Read(std::string &読み込み先変数)
158 if (!canRead)
return false;
161 SDL_RWread(handle, &文字数,
sizeof(
int), 1);
170 SDL_RWread(handle, (
char*)読み込み先変数.c_str(), 文字数, 1);
179 if (!canRead)
return false;
181 for (
int a = 0; a < 要素数; ++a)
183 SDL_RWread(handle, &読み込み先配列[a],
sizeof(T), 1);
189 template <
class TSaveType,
class TOutput>
192 bool Read(TOutput *読み込み先配列,
int 要素数,
int 分母)
194 if (!canRead)
return false;
198 for (
int a = 0; a < 要素数; ++a)
200 SDL_RWread(handle, &buff,
sizeof(TSaveType), 1);
201 読み込み先配列[a] = TOutput(buff) / 分母;
207 template<
class TSaveType,
class TOutput>
211 if (!canRead)
return false;
214 SDL_RWread(handle, &buff,
sizeof(TSaveType), 1);
216 読み込み先変数 = TOutput(buff);
227 if (!canWrite)
return false;
229 SDL_RWwrite(handle, &書込み元変数,
sizeof(書込み元変数), 1);
240 if (!canWrite)
return false;
242 const int 文字数 = (int)書込み元変数.size();
244 SDL_RWwrite(handle, &文字数,
sizeof(
int), 1);
245 SDL_RWwrite(handle, 書込み元変数.c_str(), 文字数, 1);
250 template <
class TSaveType,
class TInput>
253 bool Write(TInput *書き込み元配列,
int 要素数)
255 if (!canWrite)
return false;
257 for (
int a = 0; a < 要素数; ++a)
259 TSaveType buff = (TSaveType)書き込み元配列[a];
261 SDL_RWwrite(handle, &buff,
sizeof(TSaveType), 1);
276 return Write(読み書き変数);
284 std::vector<std::string> lineS;
289 unsigned int fileSize = (
unsigned int)handle->size(handle);
290 all.resize(fileSize);
291 SDL_RWread(handle, (
char*)all.c_str(), fileSize, 1);
294 std::istringstream iss(all);
297 while (std::getline(iss, buf,
'\n'))
299 numA = buf.find_first_of(
'\r');
300 numB = buf.find_first_of(
'\n');
301 if (numA < numB){ numA = numB; };
303 if (numA != std::string::npos)
305 buf = buf.substr(0, numA);
308 lineS.push_back(buf.substr());
317 std::vector<std::string> lineS;
322 unsigned int fileSize = (
unsigned int)handle->size(handle);
323 all.resize(fileSize);
324 SDL_RWread(handle, (
char*)all.c_str(), fileSize, 1);
329 std::istringstream iss(all);
331 while (std::getline(iss, buf,
'\n'))
333 std::istringstream iss2(buf);
334 while (std::getline(iss2, buf2,
','))
336 lineS.push_back(buf2);
347 std::vector<std::vector<std::string>> lineS;
352 unsigned int fileSize = (
unsigned int)handle->size(handle);
353 all.resize(fileSize);
354 SDL_RWread(handle, (
char*)all.c_str(), fileSize, 1);
359 std::istringstream iss(all);
361 while (std::getline(iss, buf,
'\n'))
363 std::istringstream iss2(buf);
364 lineS.push_back(std::vector<std::string>());
366 while (std::getline(iss2, buf2,
','))
368 lineS[lineNo].push_back(buf2);
379 std::vector<int> lineS;
384 unsigned int fileSize = (
unsigned int)handle->size(handle);
385 all.resize(fileSize);
386 SDL_RWread(handle, (
char*)all.c_str(), fileSize, 1);
391 std::istringstream iss(all);
393 while (std::getline(iss, buf,
'\n'))
395 std::istringstream iss2(buf);
397 while (std::getline(iss2, buf2,
','))
399 lineS.push_back(atoi(buf2.c_str()));
410 std::vector<std::vector<int>> lineS;
415 unsigned int fileSize = (
unsigned int)handle->size(handle);
416 all.resize(fileSize);
417 SDL_RWread(handle, (
char*)all.c_str(), fileSize, 1);
422 std::istringstream iss(all);
424 while (std::getline(iss, buf,
'\n'))
426 std::istringstream iss2(buf);
427 lineS.push_back(std::vector<int>());
429 while (std::getline(iss2, buf2,
','))
431 lineS[lineNo].push_back(atoi(buf2.c_str()));
442 return (SDL_RWtell(handle) == RW_SEEK_END);
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
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