~ubuntu-branches/ubuntu/wily/xmoto/wily-proposed

« back to all changes in this revision

Viewing changes to src/VFileIO.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Mimram
  • Date: 2006-09-14 21:01:20 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060914210120-bvr7yu9rafb3fivp
Tags: 0.2.1-1
* New upstream release.
* Removed manpages and desktop files from the Debian package as they are now
  provided upstream.
* Make the package binNMUable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#include "VExcept.h"
37
37
#include "VApp.h"
38
38
#include "VFileIO.h"
 
39
#include "arch/SwapEndian.h"
39
40
 
40
41
namespace vapp {
41
42
 
52
53
  #endif
53
54
  #if !defined(_MSC_VER)
54
55
    void strlwr(char *pc) {
55
 
      for(int i=0;i<strlen(pc);i++) pc[i] = tolower(pc[i]);
 
56
      for(unsigned int i=0; i<strlen(pc); i++) pc[i] = tolower(pc[i]);
56
57
    }
57
58
  #endif
58
59
  mbool str_match_wildcard(char *pcMWildcard,char *pcMString,mbool CaseSensitive) {
59
 
          int i,nPos=0;
 
60
          int nPos=0;
60
61
          mbool PrevIsWildcard=FALSE;
61
62
          char c1[256],c2[256];
62
63
          char *pcWildcard,*pcString;
75
76
        
76
77
          if(pcWildcard[0]=='\0') return TRUE;
77
78
        
78
 
          for(i=0;i<strlen(pcWildcard);i++) {
 
79
          for(unsigned i=0; i<strlen(pcWildcard); i++) {
79
80
                  /* If end of string is reached, we have a match */
80
81
                  if(pcString[nPos] == '\0' && pcWildcard[i] == '\0')
81
82
                          return TRUE;
191
192
              /* Match! */
192
193
              if(strcmp(dp->d_name,".") && strcmp(dp->d_name,"..")) {
193
194
                                              bool bFound = false;
194
 
                                              for(int k = 0;k<List.size();k++) {
 
195
                                              for(unsigned int k = 0;k<List.size();k++) {
195
196
                                              if(FS::getFileBaseName(List[k]) == FS::getFileBaseName(F)) {  
196
197
                                                  bFound = true;
197
198
                                                  break;
294
295
              if(strcmp(dp->d_name,".") && strcmp(dp->d_name,"..")) {
295
296
                                              std::string F = DirToSearch + std::string(dp->d_name);
296
297
                                              bool bFound = false;
297
 
                                              for(int k = 0;k<Result.size();k++) {
 
298
                                              for(unsigned int k = 0;k<Result.size();k++) {
298
299
                                              if(FS::getFileBaseName(Result[k]) == FS::getFileBaseName(F)) {  
299
300
                                                  bFound = true;
300
301
                                                  break;
327
328
                if(strcmp(dp->d_name,".") && strcmp(dp->d_name,"..")) {
328
329
                                                std::string F = DirToSearch + std::string(dp->d_name);
329
330
                                                bool bFound = false;
330
 
                                                for(int k = 0;k<Result.size();k++) {
 
331
                                                for(unsigned int k = 0;k<Result.size();k++) {
331
332
                                              if(FS::getFileBaseName(Result[k]) == FS::getFileBaseName(F)) {  
332
333
                                                    bFound = true;
333
334
                                                    break;
361
362
                if(strcmp(dp->d_name,".") && strcmp(dp->d_name,"..")) {
362
363
                                                std::string F = DirToSearch + std::string(dp->d_name);
363
364
                                                bool bFound = false;
364
 
                                                for(int k = 0;k<Result.size();k++) {
 
365
                                                for(unsigned int k = 0;k<Result.size();k++) {
365
366
                                              if(FS::getFileBaseName(Result[k]) == FS::getFileBaseName(F)) {  
366
367
                                                    bFound = true;
367
368
                                                    break;
481
482
    delete pfh;
482
483
  } 
483
484
 
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;
499
500
    return true;
500
501
  } 
501
502
  
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;
595
596
  }   
596
597
  
597
598
  int FS::readByte(FileHandle *pfh) {
598
 
    char v;
 
599
    signed char v;
599
600
    if(!readBuf(pfh,(char *)&v,sizeof(v))) _ThrowFileError(pfh,"readByte -> failed");
600
601
    return v;
601
602
  } 
635
636
  
636
637
  std::string FS::readLongString(FileHandle *pfh) {
637
638
    unsigned short v;
638
 
    v=readShort(pfh);
 
639
    v=readShort_LE(pfh);
639
640
    char cBuf[65536];  
640
641
    if(!readBuf(pfh,(char *)cBuf,v)) _ThrowFileError(pfh,"readLongString -> failed");
641
642
    cBuf[v] = '\0';
642
643
    return std::string(cBuf);  
643
644
  }
644
645
  
 
646
  // We use one-byte bools
645
647
  bool FS::readBool(FileHandle *pfh) {
646
 
    bool v;
647
 
    if(!readBuf(pfh,(char *)&v,sizeof(v))) _ThrowFileError(pfh,"readBool -> failed");
648
 
    return v;
 
648
    return static_cast<bool>(readByte(pfh));
649
649
  }   
650
650
 
651
651
  void FS::writeByte(FileHandle *pfh,unsigned char v) {
674
674
  }
675
675
  
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");
679
679
  }
680
680
  
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));
683
683
  }
684
684
  
685
685
  void FS::writeLine(FileHandle *pfh,std::string Line) {
751
751
    if(n<0) n = Path.find_last_of("\\");
752
752
    if(n<0) {
753
753
      /* No directory info */
754
 
      return std::string("./");
755
 
    }
756
 
    return Path.substr(0,n+1);
 
754
      return std::string(".");
 
755
    }
 
756
    std::string v_fileDir = Path.substr(0,n);
 
757
    if(v_fileDir == "") {
 
758
      v_fileDir = "/";
 
759
    }
 
760
    return v_fileDir;
757
761
  }
758
762
  
759
763
  /*===========================================================================
838
842
  /*===========================================================================
839
843
  Copy file
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());
875
879
        if(fp == NULL) {
876
880
          /* Good one */
877
881
          FullTo = cTemp;
878
 
          break;        
879
 
        }
880
 
        fclose(fp);
881
 
        
 
882
          break;
 
883
        } else {
 
884
          fclose(fp);
 
885
          i++;
 
886
        }
882
887
        /* Next */
883
888
      }      
884
889
    }
888
893
    if(in != NULL) {
889
894
      FILE *out = fopen(FullTo.c_str(),"wb");
890
895
      if(out != NULL) {
 
896
        To_really_done = FullTo;
 
897
 
891
898
        /* Get input file size */
892
899
        fseek(in,0,SEEK_END);
893
900
        int nFileSize = ftell(in);
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));
1140
1146
  }
1141
1147
  
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));
1147
1150
  }
1148
1151
  
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));
1154
1154
  }
1155
1155
  
1156
1156
  int FS::readShort_LE(FileHandle *pfh) {
1157
 
    unsigned char c[2];
1158
 
    readBuf(pfh,(char *)c,2);
1159
 
    return c[0]|(c[1]<<8);
 
1157
    short v = readShort(pfh);
 
1158
    return SwapEndian::LittleShort(v);
1160
1159
  }
1161
1160
  
1162
1161
  int FS::readInt_LE(FileHandle *pfh) {
1163
 
    unsigned char c[4];
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);
1166
1164
  }
1167
1165
 
1168
1166
  float FS::readFloat_LE(FileHandle *pfh) {
1169
 
    float res;
1170
 
 
1171
 
    unsigned char *pc=(unsigned char *)&res;
1172
 
    for(int i=0;i<4;i++)
1173
 
    pc[i]=readByte(pfh);
1174
 
 
1175
 
    return res;
 
1167
    float v = readFloat(pfh);
 
1168
    return SwapEndian::LittleFloat(v);
1176
1169
  }  
1177
1170
  
1178
 
};
 
1171
  int FS::readShort_MaybeLE(FileHandle *pfh, bool big) {
 
1172
    short v = readShort(pfh);
 
1173
    return big ? SwapEndian::BigShort(v) : SwapEndian::LittleShort(v);
 
1174
  }
 
1175
  
 
1176
  int FS::readInt_MaybeLE(FileHandle *pfh, bool big) {
 
1177
    int v = readInt(pfh);
 
1178
    return big ? SwapEndian::BigLong(v) : SwapEndian::LittleLong(v);
 
1179
  }
 
1180
  
 
1181
  float FS::readFloat_MaybeLE(FileHandle *pfh, bool big) {
 
1182
    float v = readFloat(pfh);
 
1183
    return big ? SwapEndian::BigFloat(v) : SwapEndian::LittleFloat(v);
 
1184
  }
 
1185
  
 
1186
  
 
1187
  bool FS::isInUserDir(std::string p_filepath) {
 
1188
    std::string v_userDir;
 
1189
    std::string v_fileDir;
 
1190
 
 
1191
    v_userDir = getUserDir();
 
1192
    v_fileDir = getFileDir(p_filepath);
 
1193
 
 
1194
    if(v_userDir.length() > v_fileDir.length()) {
 
1195
      return false;
 
1196
    }
 
1197
 
 
1198
    return v_fileDir.substr(0, v_userDir.length()) == v_userDir;
 
1199
  }
 
1200
 
 
1201
  bool FS::doesDirectoryExist(std::string p_path) {
 
1202
    struct stat S;
 
1203
    return stat(p_path.c_str(), &S) == 0;
 
1204
  }
 
1205
 
 
1206
  bool FS::isFileReadable(std::string p_filename) {
 
1207
    FileHandle *fh = openIFile(p_filename);
 
1208
    if(fh == NULL) {
 
1209
      return false;
 
1210
    }
 
1211
    closeFile(fh);
 
1212
    return true;
 
1213
  }
 
1214
 
 
1215
  bool FS::fileExists(std::string p_filename) {
 
1216
    return isFileReadable(p_filename);
 
1217
  }
 
1218
 
 
1219
  void FS::mkArborescence(std::string v_filepath) {
 
1220
    std::string v_parentDir = getFileDir(v_filepath);
 
1221
 
 
1222
    if(doesDirectoryExist(v_parentDir)) {
 
1223
      return;
 
1224
    }
 
1225
 
 
1226
    mkArborescence(v_parentDir);
 
1227
    if(mkDir(v_parentDir.c_str()) != 0) {
 
1228
      throw Exception("Can't create directory " + v_parentDir);
 
1229
    }
 
1230
  }
 
1231
 
 
1232
}
1179
1233