484
bool FS::readBuf(FileHandle *pfh,char *pcBuf,int nBufSize) {
485
bool FS::readBuf(FileHandle *pfh,char *pcBuf, unsigned int nBufSize) {
485
486
if(!pfh->bRead) _ThrowFileError(pfh,"readBuf -> write-only");
486
487
if(pfh->Type == FHT_STDIO) {
487
488
if(fread(pcBuf,1,nBufSize,pfh->fp) != nBufSize) return false;
502
bool FS::writeBuf(FileHandle *pfh,char *pcBuf,int nBufSize) {
503
bool FS::writeBuf(FileHandle *pfh, char *pcBuf, unsigned int nBufSize) {
503
504
if(!pfh->bWrite) _ThrowFileError(pfh,"writeBuf -> read-only");
504
505
if(pfh->Type == FHT_STDIO) {
505
506
if(fwrite(pcBuf,1,nBufSize,pfh->fp) != nBufSize) return false;
636
637
std::string FS::readLongString(FileHandle *pfh) {
637
638
unsigned short v;
639
640
char cBuf[65536];
640
641
if(!readBuf(pfh,(char *)cBuf,v)) _ThrowFileError(pfh,"readLongString -> failed");
642
643
return std::string(cBuf);
646
// We use one-byte bools
645
647
bool FS::readBool(FileHandle *pfh) {
647
if(!readBuf(pfh,(char *)&v,sizeof(v))) _ThrowFileError(pfh,"readBool -> failed");
648
return static_cast<bool>(readByte(pfh));
651
651
void FS::writeByte(FileHandle *pfh,unsigned char v) {
676
676
void FS::writeLongString(FileHandle *pfh,std::string v) {
677
writeShort(pfh,v.length());
677
writeShort_LE(pfh,v.length());
678
678
if(!writeBuf(pfh,(char *)v.c_str(),v.length())) _ThrowFileError(pfh,"writeLongString -> failed");
681
681
void FS::writeBool(FileHandle *pfh,bool v) {
682
if(!writeBuf(pfh,(char *)&v,sizeof(v))) _ThrowFileError(pfh,"writeBool -> failed");
682
writeByte(pfh,static_cast<unsigned char>(v));
685
685
void FS::writeLine(FileHandle *pfh,std::string Line) {
838
842
/*===========================================================================
840
844
===========================================================================*/
841
bool FS::copyFile(const std::string &From,const std::string &To) {
845
bool FS::copyFile(const std::string &From,const std::string &To, std::string &To_really_done) {
842
846
/* All file copying must happen inside the user directory... */
843
847
if(m_UserDir == "") {
844
848
Log("** Warning ** : No user directory, can't copy file '%s' to '%s'",From.c_str(),To.c_str());
1135
1142
Endian-safe I/O helpers
1136
1143
===========================================================================*/
1137
1144
void FS::writeShort_LE(FileHandle *pfh,short v) {
1138
writeByte(pfh,v&0xff);
1139
writeByte(pfh,(v&0xff00)>>8);
1145
writeShort(pfh, SwapEndian::LittleShort(v));
1142
1148
void FS::writeInt_LE(FileHandle *pfh,int v) {
1143
writeByte(pfh,v&0xff);
1144
writeByte(pfh,(v&0xff00)>>8);
1145
writeByte(pfh,(v&0xff0000)>>16);
1146
writeByte(pfh,(v&0xff000000)>>24);
1149
writeInt(pfh, SwapEndian::LittleLong(v));
1149
1152
void FS::writeFloat_LE(FileHandle *pfh,float v) {
1150
writeInt_LE(pfh,*((int *)&v));
1151
//unsigned char *pc=(unsigned char *)&v;
1152
//for(int i=0;i<4;i++)
1153
// writeByte(pfh,pc[i]);
1153
writeFloat(pfh, SwapEndian::LittleFloat(v));
1156
1156
int FS::readShort_LE(FileHandle *pfh) {
1158
readBuf(pfh,(char *)c,2);
1159
return c[0]|(c[1]<<8);
1157
short v = readShort(pfh);
1158
return SwapEndian::LittleShort(v);
1162
1161
int FS::readInt_LE(FileHandle *pfh) {
1164
readBuf(pfh,(char *)c,4);
1165
return c[0]|(c[1]<<8)|(c[2]<<16)|(c[3]<<24);
1162
int v = readInt(pfh);
1163
return SwapEndian::LittleLong(v);
1168
1166
float FS::readFloat_LE(FileHandle *pfh) {
1171
unsigned char *pc=(unsigned char *)&res;
1172
for(int i=0;i<4;i++)
1173
pc[i]=readByte(pfh);
1167
float v = readFloat(pfh);
1168
return SwapEndian::LittleFloat(v);
1171
int FS::readShort_MaybeLE(FileHandle *pfh, bool big) {
1172
short v = readShort(pfh);
1173
return big ? SwapEndian::BigShort(v) : SwapEndian::LittleShort(v);
1176
int FS::readInt_MaybeLE(FileHandle *pfh, bool big) {
1177
int v = readInt(pfh);
1178
return big ? SwapEndian::BigLong(v) : SwapEndian::LittleLong(v);
1181
float FS::readFloat_MaybeLE(FileHandle *pfh, bool big) {
1182
float v = readFloat(pfh);
1183
return big ? SwapEndian::BigFloat(v) : SwapEndian::LittleFloat(v);
1187
bool FS::isInUserDir(std::string p_filepath) {
1188
std::string v_userDir;
1189
std::string v_fileDir;
1191
v_userDir = getUserDir();
1192
v_fileDir = getFileDir(p_filepath);
1194
if(v_userDir.length() > v_fileDir.length()) {
1198
return v_fileDir.substr(0, v_userDir.length()) == v_userDir;
1201
bool FS::doesDirectoryExist(std::string p_path) {
1203
return stat(p_path.c_str(), &S) == 0;
1206
bool FS::isFileReadable(std::string p_filename) {
1207
FileHandle *fh = openIFile(p_filename);
1215
bool FS::fileExists(std::string p_filename) {
1216
return isFileReadable(p_filename);
1219
void FS::mkArborescence(std::string v_filepath) {
1220
std::string v_parentDir = getFileDir(v_filepath);
1222
if(doesDirectoryExist(v_parentDir)) {
1226
mkArborescence(v_parentDir);
1227
if(mkDir(v_parentDir.c_str()) != 0) {
1228
throw Exception("Can't create directory " + v_parentDir);