00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef BVIRTUALFILE_H
00013 #define BVIRTUALFILE_H
00014
00015
00016 #include <nds.h>
00017 #include <stdio.h>
00018 #include <vector>
00019 #include <string>
00020
00021
00022
00023
00025 class BVirtualFile {
00026 public:
00027 typedef enum {
00028 WHENCE_START,
00029 WHENCE_CURRENT,
00030 WHENCE_END
00031 } Whence;
00032
00033 virtual ~BVirtualFile() {}
00034
00035 virtual int read(void* buf, unsigned int nbytes) = 0;
00036 std::string read();
00037
00038 virtual int write(const void* buf, unsigned int nbytes) = 0;
00039 int write(const std::string& str) {
00040 return write(str.c_str(), str.size());
00041 }
00042
00043 virtual long tell() = 0;
00044 virtual int seek(long offset, Whence whence) = 0;
00045 virtual int eof() = 0;
00046
00047 void rewind() { seek(0, WHENCE_START); }
00048 };
00049
00051 class BFATFile: public BVirtualFile {
00052 public:
00053 BFATFile(const char* filename, const char* mode="r");
00054 BFATFile(FILE* file, bool closeWhenDone);
00055 ~BFATFile();
00056
00057 virtual int read(void* buf, unsigned int nbytes);
00058 std::string read();
00059
00060 virtual int write(const void* buf, unsigned int nbytes);
00061 int write(const std::string& str) {
00062 return write(str.c_str(), str.size());
00063 }
00064 virtual long tell();
00065 virtual int seek(long offset, Whence whence);
00066 virtual int eof();
00067
00068 private:
00069 FILE* _file;
00070 bool _close;
00071 };
00072
00074 class BMemFile: public BVirtualFile {
00075 public:
00076 BMemFile(u32 address, u32 len, bool freeWhenDone = false);
00077 ~BMemFile();
00078
00079 virtual int read(void* buf, unsigned int nbytes);
00080 std::string read();
00081
00082 virtual int write(const void* buf, unsigned int nbytes);
00083 int write(const std::string& str) {
00084 return write(str.c_str(), str.size());
00085 }
00086 virtual long tell();
00087 virtual int seek(long offset, Whence whence);
00088 virtual int eof();
00089
00090 private:
00091 u32 _adr; u32 _len; u32 _cur; bool _free;
00092 };
00093
00095 class BFileManager {
00096 public:
00097 static BFileManager* get();
00098
00099 typedef enum {
00100 TYPE_DIRECTORY,
00101 TYPE_FILE,
00102 TYPE_NOEXIST
00103 } FileType;
00104
00105 typedef struct {
00106 std::string filename;
00107 FileType filetype;
00108 } FileAndType;
00109
00110 std::vector<FileAndType> directoryContents(const std::string& dir);
00111 std::vector<FileAndType> directoryContents() {
00112 return directoryContents(_curdir);
00113 }
00114 FileType typeOfFile(const std::string& filename);
00115
00117 int sizeOfFile(const std::string& filename);
00118
00119 bool changeDirectory(const std::string& dir);
00120 std::string currentDirectory();
00121
00122 bool makeDirectory(const std::string& dir);
00123 bool renameFile(const std::string& fname1, const std::string& fname2);
00124 bool removeFile(const std::string& fname);
00125
00126 std::string normalizePath(const std::string& str);
00127 std::string lastPathComponent(const std::string& str);
00128 std::string absolutePath(const std::string& str);
00129
00131 std::string filenameExtension(const std::string& str);
00132 std::string filenameWithoutExtension(const std::string& str);
00133
00134 private:
00135 BFileManager();
00136 static BFileManager* _mgr;
00137 std::string _curdir;
00138 };
00139 #endif