5 #include <Multimedia/System.h>
31 SDL_RWops *handle =
nullptr;
40 File(
const char* ファイル名 ,
FileMode 読み書きモード ,
bool バイナリファイル =
false ,
SaveMode Androidの保存先 = SaveMode::Asset)
42 File::Open( ファイル名 , 読み書きモード , バイナリファイル , Androidの保存先);
51 bool Open(
const char* ファイル名 ,
FileMode 読み書きモード ,
bool バイナリファイル =
false ,
SaveMode Androidの保存先 = SaveMode::Asset )
60 case SaveMode::External:
61 fileName = SDL_AndroidGetExternalStoragePath();
64 case SaveMode::Internal:
65 fileName = SDL_AndroidGetInternalStoragePath();
78 if (isBinary) handle = SDL_RWFromFile( fileName.c_str() ,
"rb" );
79 else handle = SDL_RWFromFile(fileName.c_str(),
"r");
86 if (isBinary) handle = SDL_RWFromFile(fileName.c_str(),
"wb");
87 else handle = SDL_RWFromFile(fileName.c_str(),
"w");
93 if (isBinary) handle = SDL_RWFromFile(fileName.c_str(),
"ab");
94 else handle = SDL_RWFromFile(fileName.c_str(),
"a");
103 if ( handle ==
nullptr )
139 return this->fileName.c_str();
148 if (!canRead)
return false;
150 SDL_RWread( handle, &読み込み先変数,
sizeof(読み込み先変数), 1);
154 bool Read(std::string &読み込み先変数)
156 if (!canRead)
return false;
159 SDL_RWread(handle, &文字数,
sizeof(
int), 1);
162 SDL_RWread(handle, (
char*)読み込み先変数.c_str(), 文字数 , 1);
168 bool Read(T *読み込み先配列,
int 要素数)
170 if (!canRead)
return false;
172 for (
int a = 0; a < 要素数; ++a)
174 SDL_RWread(handle, &読み込み先配列[a],
sizeof(T), 1);
181 template <
class TSaveType,
class TOutput>
182 bool Read(TOutput *読み込み先配列,
int 要素数 ,
int 分母)
184 if (!canRead)
return false;
188 for (
int a = 0; a < 要素数; ++a)
190 SDL_RWread(handle, &buff,
sizeof(TSaveType), 1);
191 読み込み先配列[a] = TOutput(buff) / 分母;
197 template<
class TSaveType,
class TOutput>
198 bool Read(TOutput &読み込み先変数)
200 if (!canRead)
return false;
203 SDL_RWread(handle, &buff,
sizeof(TSaveType), 1);
205 読み込み先変数 = TOutput(buff);
218 if (!canWrite)
return false;
220 SDL_RWwrite( handle , &書込み元変数 ,
sizeof(書込み元変数) , 1);
224 bool Write(std::string &書込み元変数)
226 if (!canWrite)
return false;
228 const int 文字数 = (int)書込み元変数.size();
230 SDL_RWwrite(handle, &文字数,
sizeof(
int), 1);
231 SDL_RWwrite(handle, 書込み元変数.c_str(), 文字数, 1);
238 template <
class TSaveType,
class TInput>
239 bool Write(TInput *書き込み元配列 ,
int 要素数 )
241 if (!canWrite)
return false;
243 for (
int a = 0; a < 要素数; ++a)
245 TSaveType buff = (TSaveType)書き込み元配列[a];
247 SDL_RWwrite(handle, &buff ,
sizeof(TSaveType), 1);
261 }
else if( canWrite ){
262 return Write(読み書き変数);
270 std::vector<std::string> lineS;
275 unsigned int fileSize = (
unsigned int)handle->size(handle);
276 all.resize(fileSize);
277 SDL_RWread(handle, (
char*)all.c_str(), fileSize, 1);
280 std::istringstream iss(all);
282 while (std::getline(iss, buf,
'\n'))
284 lineS.push_back(buf);
291 std::vector<std::vector<std::string>>
GetCsvS()
293 std::vector<std::vector<std::string>> lineS;
298 unsigned int fileSize = (
unsigned int)handle->size(handle);
299 all.resize(fileSize);
300 SDL_RWread(handle, (
char*)all.c_str(), fileSize, 1);
305 std::istringstream iss(all);
307 while (std::getline(iss, buf,
'\n'))
309 std::istringstream iss2(buf);
310 lineS.push_back(std::vector<std::string>());
312 while (std::getline(iss2, buf2,
','))
314 lineS[lineNo].push_back(buf2);
325 return (SDL_RWtell(handle) == RW_SEEK_END);
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