A terminal style view onto the filesystem, with the concept of current working directory. File systems can be mounted to root using libraries such as LocalFileSystem and SDFileSystem. Initial version only allows changing the current working directory and retrieving a directory listing. Required libraries: <<library /users/Phlaphead/libraries/Util/latest>> library.
FileSystemInterface.cpp@0:63761ecf509c, 2011-06-19 (annotated)
- Committer:
- Phlaphead
- Date:
- Sun Jun 19 13:35:48 2011 +0000
- Revision:
- 0:63761ecf509c
Initial revision. Incomplete version, allows manipulation of working directory and listing files only.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Phlaphead | 0:63761ecf509c | 1 | |
Phlaphead | 0:63761ecf509c | 2 | #include "FileSystemInterface.h" |
Phlaphead | 0:63761ecf509c | 3 | #include "mbed.h" |
Phlaphead | 0:63761ecf509c | 4 | #include "stringUtils.h" |
Phlaphead | 0:63761ecf509c | 5 | |
Phlaphead | 0:63761ecf509c | 6 | #define __DEBUG |
Phlaphead | 0:63761ecf509c | 7 | #include "dbg/dbg.h" |
Phlaphead | 0:63761ecf509c | 8 | |
Phlaphead | 0:63761ecf509c | 9 | FileSystemInterface::FileSystemInterface() |
Phlaphead | 0:63761ecf509c | 10 | { |
Phlaphead | 0:63761ecf509c | 11 | m_workDirString = getPathAsString(m_workDir); |
Phlaphead | 0:63761ecf509c | 12 | } |
Phlaphead | 0:63761ecf509c | 13 | |
Phlaphead | 0:63761ecf509c | 14 | string FileSystemInterface::getPathAsString(vector<string>& path) |
Phlaphead | 0:63761ecf509c | 15 | { |
Phlaphead | 0:63761ecf509c | 16 | string s_path = "/"; |
Phlaphead | 0:63761ecf509c | 17 | |
Phlaphead | 0:63761ecf509c | 18 | for(vector<string>::iterator it = path.begin(); it != path.end(); it++) |
Phlaphead | 0:63761ecf509c | 19 | { |
Phlaphead | 0:63761ecf509c | 20 | string& directory = *it; |
Phlaphead | 0:63761ecf509c | 21 | s_path += directory; |
Phlaphead | 0:63761ecf509c | 22 | s_path += "/"; |
Phlaphead | 0:63761ecf509c | 23 | } |
Phlaphead | 0:63761ecf509c | 24 | |
Phlaphead | 0:63761ecf509c | 25 | return s_path; |
Phlaphead | 0:63761ecf509c | 26 | } |
Phlaphead | 0:63761ecf509c | 27 | |
Phlaphead | 0:63761ecf509c | 28 | |
Phlaphead | 0:63761ecf509c | 29 | string FileSystemInterface::getWorkingDirectory() |
Phlaphead | 0:63761ecf509c | 30 | { |
Phlaphead | 0:63761ecf509c | 31 | return m_workDirString; |
Phlaphead | 0:63761ecf509c | 32 | } |
Phlaphead | 0:63761ecf509c | 33 | |
Phlaphead | 0:63761ecf509c | 34 | bool FileSystemInterface::changeWorkingDirectory(const string& dir) |
Phlaphead | 0:63761ecf509c | 35 | { |
Phlaphead | 0:63761ecf509c | 36 | bool successful = false; |
Phlaphead | 0:63761ecf509c | 37 | |
Phlaphead | 0:63761ecf509c | 38 | DBG("%d\r\n", dir.find_first_of("/")); |
Phlaphead | 0:63761ecf509c | 39 | |
Phlaphead | 0:63761ecf509c | 40 | if(dir.compare("/") == 0) |
Phlaphead | 0:63761ecf509c | 41 | { |
Phlaphead | 0:63761ecf509c | 42 | //Root path |
Phlaphead | 0:63761ecf509c | 43 | m_workDir.clear(); |
Phlaphead | 0:63761ecf509c | 44 | successful = true; |
Phlaphead | 0:63761ecf509c | 45 | } |
Phlaphead | 0:63761ecf509c | 46 | else if(dir.find_first_of("/") == 0) |
Phlaphead | 0:63761ecf509c | 47 | { |
Phlaphead | 0:63761ecf509c | 48 | //Absolute path |
Phlaphead | 0:63761ecf509c | 49 | if(isDir(dir)) |
Phlaphead | 0:63761ecf509c | 50 | { |
Phlaphead | 0:63761ecf509c | 51 | m_workDir.clear(); |
Phlaphead | 0:63761ecf509c | 52 | m_workDir = tokenize(dir, "/"); |
Phlaphead | 0:63761ecf509c | 53 | successful = true; |
Phlaphead | 0:63761ecf509c | 54 | } |
Phlaphead | 0:63761ecf509c | 55 | } |
Phlaphead | 0:63761ecf509c | 56 | else if(dir.compare("..") == 0) |
Phlaphead | 0:63761ecf509c | 57 | { |
Phlaphead | 0:63761ecf509c | 58 | //Parent directory |
Phlaphead | 0:63761ecf509c | 59 | if(!m_workDir.empty()) |
Phlaphead | 0:63761ecf509c | 60 | { |
Phlaphead | 0:63761ecf509c | 61 | m_workDir.pop_back(); |
Phlaphead | 0:63761ecf509c | 62 | successful = true; |
Phlaphead | 0:63761ecf509c | 63 | } |
Phlaphead | 0:63761ecf509c | 64 | } |
Phlaphead | 0:63761ecf509c | 65 | else |
Phlaphead | 0:63761ecf509c | 66 | { |
Phlaphead | 0:63761ecf509c | 67 | //Relative Path |
Phlaphead | 0:63761ecf509c | 68 | string path = m_workDirString + dir; |
Phlaphead | 0:63761ecf509c | 69 | if(isDir(path)) |
Phlaphead | 0:63761ecf509c | 70 | { |
Phlaphead | 0:63761ecf509c | 71 | m_workDir.push_back(dir); |
Phlaphead | 0:63761ecf509c | 72 | successful = true; |
Phlaphead | 0:63761ecf509c | 73 | } |
Phlaphead | 0:63761ecf509c | 74 | } |
Phlaphead | 0:63761ecf509c | 75 | |
Phlaphead | 0:63761ecf509c | 76 | if(successful) |
Phlaphead | 0:63761ecf509c | 77 | { |
Phlaphead | 0:63761ecf509c | 78 | m_workDirString = getPathAsString(m_workDir); |
Phlaphead | 0:63761ecf509c | 79 | } |
Phlaphead | 0:63761ecf509c | 80 | |
Phlaphead | 0:63761ecf509c | 81 | return successful; |
Phlaphead | 0:63761ecf509c | 82 | } |
Phlaphead | 0:63761ecf509c | 83 | |
Phlaphead | 0:63761ecf509c | 84 | |
Phlaphead | 0:63761ecf509c | 85 | list<File>& FileSystemInterface::getFileList() |
Phlaphead | 0:63761ecf509c | 86 | { |
Phlaphead | 0:63761ecf509c | 87 | DIR* directory; |
Phlaphead | 0:63761ecf509c | 88 | struct dirent* entry; |
Phlaphead | 0:63761ecf509c | 89 | FileType type; |
Phlaphead | 0:63761ecf509c | 90 | m_ls.clear(); |
Phlaphead | 0:63761ecf509c | 91 | |
Phlaphead | 0:63761ecf509c | 92 | directory = opendir(m_workDirString.c_str()); |
Phlaphead | 0:63761ecf509c | 93 | if (directory != NULL) |
Phlaphead | 0:63761ecf509c | 94 | { |
Phlaphead | 0:63761ecf509c | 95 | while ((entry = readdir(directory)) != NULL) |
Phlaphead | 0:63761ecf509c | 96 | { |
Phlaphead | 0:63761ecf509c | 97 | string name = entry->d_name; |
Phlaphead | 0:63761ecf509c | 98 | string path = m_workDirString + name; |
Phlaphead | 0:63761ecf509c | 99 | |
Phlaphead | 0:63761ecf509c | 100 | if(isFile(path)) |
Phlaphead | 0:63761ecf509c | 101 | { |
Phlaphead | 0:63761ecf509c | 102 | type = TYPE_FILE; |
Phlaphead | 0:63761ecf509c | 103 | } |
Phlaphead | 0:63761ecf509c | 104 | else |
Phlaphead | 0:63761ecf509c | 105 | { |
Phlaphead | 0:63761ecf509c | 106 | type = TYPE_DIR; |
Phlaphead | 0:63761ecf509c | 107 | } |
Phlaphead | 0:63761ecf509c | 108 | |
Phlaphead | 0:63761ecf509c | 109 | m_ls.push_back(File(name, type)); |
Phlaphead | 0:63761ecf509c | 110 | } |
Phlaphead | 0:63761ecf509c | 111 | } |
Phlaphead | 0:63761ecf509c | 112 | else |
Phlaphead | 0:63761ecf509c | 113 | { |
Phlaphead | 0:63761ecf509c | 114 | //printf("Could not open directory!\n"); |
Phlaphead | 0:63761ecf509c | 115 | } |
Phlaphead | 0:63761ecf509c | 116 | |
Phlaphead | 0:63761ecf509c | 117 | closedir(directory); |
Phlaphead | 0:63761ecf509c | 118 | return m_ls; |
Phlaphead | 0:63761ecf509c | 119 | } |
Phlaphead | 0:63761ecf509c | 120 | |
Phlaphead | 0:63761ecf509c | 121 | |
Phlaphead | 0:63761ecf509c | 122 | bool FileSystemInterface::isFile(const string& path) |
Phlaphead | 0:63761ecf509c | 123 | { |
Phlaphead | 0:63761ecf509c | 124 | FILE *fp = fopen(path.c_str(), "r"); |
Phlaphead | 0:63761ecf509c | 125 | DBG("%s : %d\r\n", path.c_str(), fp); |
Phlaphead | 0:63761ecf509c | 126 | bool isFile = (fp != NULL); |
Phlaphead | 0:63761ecf509c | 127 | if(fp) |
Phlaphead | 0:63761ecf509c | 128 | { |
Phlaphead | 0:63761ecf509c | 129 | fclose(fp); |
Phlaphead | 0:63761ecf509c | 130 | } |
Phlaphead | 0:63761ecf509c | 131 | return isFile; |
Phlaphead | 0:63761ecf509c | 132 | } |
Phlaphead | 0:63761ecf509c | 133 | |
Phlaphead | 0:63761ecf509c | 134 | bool FileSystemInterface::isDir(const string& path) |
Phlaphead | 0:63761ecf509c | 135 | { |
Phlaphead | 0:63761ecf509c | 136 | DIR *dir = opendir(path.c_str()); |
Phlaphead | 0:63761ecf509c | 137 | bool isDir = (dir != NULL); |
Phlaphead | 0:63761ecf509c | 138 | if(dir) |
Phlaphead | 0:63761ecf509c | 139 | { |
Phlaphead | 0:63761ecf509c | 140 | closedir(dir); |
Phlaphead | 0:63761ecf509c | 141 | } |
Phlaphead | 0:63761ecf509c | 142 | return isDir; |
Phlaphead | 0:63761ecf509c | 143 | } |