|
| 1 | +// I hate multiple inheritance, but I hate this problem even more. |
| 2 | +// So be it. |
| 3 | +#include <map> |
| 4 | +#include <string> |
| 5 | +#include <vector> |
| 6 | +using std::map; |
| 7 | +using std::string; |
| 8 | +using std::vector; |
| 9 | + |
| 10 | +static const int TYPE_FILE = 1; |
| 11 | +static const int TYPE_DIR = 2; |
| 12 | + |
| 13 | +class Node { |
| 14 | +public: |
| 15 | + Node(const string &_name): name(_name) {} |
| 16 | + |
| 17 | + string getName() { |
| 18 | + return name; |
| 19 | + } |
| 20 | + |
| 21 | + virtual int type() = 0; |
| 22 | +protected: |
| 23 | + string name; |
| 24 | +}; |
| 25 | + |
| 26 | +class FileInterface { |
| 27 | +public: |
| 28 | + virtual string getContent() = 0; |
| 29 | + virtual void appendContent(const string &s) = 0; |
| 30 | + virtual void setContent(const string &s) = 0; |
| 31 | +}; |
| 32 | + |
| 33 | +class FileNode: public Node, public FileInterface { |
| 34 | +public: |
| 35 | + FileNode(const string &_name): Node(_name), content("") {} |
| 36 | + |
| 37 | + virtual int type() { |
| 38 | + return TYPE_FILE; |
| 39 | + } |
| 40 | + |
| 41 | + virtual string getContent() { |
| 42 | + return content; |
| 43 | + } |
| 44 | + |
| 45 | + virtual void appendContent(const string &s) { |
| 46 | + content += s; |
| 47 | + } |
| 48 | + |
| 49 | + virtual void setContent(const string &s) { |
| 50 | + content = s; |
| 51 | + } |
| 52 | +protected: |
| 53 | + string content; |
| 54 | +}; |
| 55 | + |
| 56 | +class DirInterface { |
| 57 | +public: |
| 58 | + virtual Node *getChild(const string &_name) = 0; |
| 59 | + virtual void addChild(const string &_name, Node *_node) = 0; |
| 60 | + virtual vector<string> getList() = 0; |
| 61 | +}; |
| 62 | + |
| 63 | +class DirNode: public Node, public DirInterface { |
| 64 | +public: |
| 65 | + DirNode(const string &_name): Node(_name) {} |
| 66 | + |
| 67 | + virtual int type() { |
| 68 | + return TYPE_DIR; |
| 69 | + } |
| 70 | + |
| 71 | + virtual Node *getChild(const string &_name) { |
| 72 | + if (child.find(_name) != child.end()) { |
| 73 | + return child[_name]; |
| 74 | + } else { |
| 75 | + return NULL; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + virtual void addChild(const string &_name, Node *_node) { |
| 80 | + if (_name == "" || _node == NULL) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + Node *ptr = getChild(_name); |
| 85 | + if (ptr != NULL) { |
| 86 | + delete ptr; |
| 87 | + } |
| 88 | + child[_name] = _node; |
| 89 | + } |
| 90 | + |
| 91 | + virtual vector<string> getList() { |
| 92 | + vector<string> res; |
| 93 | + for (auto &p: child) { |
| 94 | + res.push_back(p.first); |
| 95 | + } |
| 96 | + return res; |
| 97 | + } |
| 98 | +protected: |
| 99 | + map<string, Node *> child; |
| 100 | +}; |
| 101 | + |
| 102 | +class FileSystem { |
| 103 | +public: |
| 104 | + FileSystem() { |
| 105 | + root = new DirNode(""); |
| 106 | + } |
| 107 | + |
| 108 | + vector<string> ls(string path) { |
| 109 | + vector<string> vt = tokenize(path); |
| 110 | + Node *ptr = root; |
| 111 | + Node *ptr1; |
| 112 | + |
| 113 | + // Needed for type casting |
| 114 | + DirInterface *pd; |
| 115 | + FileInterface *pf; |
| 116 | + |
| 117 | + for (auto &tk: vt) { |
| 118 | + if (ptr->type() != TYPE_DIR) { |
| 119 | + return vector<string>(); |
| 120 | + } |
| 121 | + pd = (DirNode *)ptr; |
| 122 | + if ((ptr1 = pd->getChild(tk)) == NULL) { |
| 123 | + return vector<string>(); |
| 124 | + } |
| 125 | + ptr = ptr1; |
| 126 | + } |
| 127 | + if (ptr->type() == TYPE_DIR) { |
| 128 | + pd = (DirNode *)ptr; |
| 129 | + return pd->getList(); |
| 130 | + } else { |
| 131 | + vector<string> res; |
| 132 | + res.push_back(ptr->getName()); |
| 133 | + return res; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + void mkdir(string path) { |
| 138 | + vector<string> vt = tokenize(path); |
| 139 | + Node *ptr = root; |
| 140 | + Node *ptr1; |
| 141 | + |
| 142 | + // Needed for type casting |
| 143 | + DirInterface *pd; |
| 144 | + FileInterface *pf; |
| 145 | + |
| 146 | + for (auto &tk: vt) { |
| 147 | + pd = (DirNode *)ptr; |
| 148 | + if ((ptr1 = pd->getChild(tk)) == NULL) { |
| 149 | + ptr1 = new DirNode(tk); |
| 150 | + pd->addChild(tk, ptr1); |
| 151 | + } |
| 152 | + ptr = ptr1; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + void addContentToFile(string filePath, string content) { |
| 157 | + vector<string> vt = tokenize(filePath); |
| 158 | + Node *ptr = root; |
| 159 | + Node *ptr1; |
| 160 | + |
| 161 | + // Needed for type casting |
| 162 | + DirInterface *pd; |
| 163 | + FileInterface *pf; |
| 164 | + |
| 165 | + int n = vt.size(); |
| 166 | + int i; |
| 167 | + for (i = 0; i < n - 1; ++i) { |
| 168 | + pd = (DirNode *)ptr; |
| 169 | + if ((ptr1 = pd->getChild(vt[i])) == NULL) { |
| 170 | + ptr1 = new DirNode(vt[i]); |
| 171 | + pd->addChild(vt[i], ptr1); |
| 172 | + } |
| 173 | + ptr = ptr1; |
| 174 | + } |
| 175 | + pd = (DirNode *)ptr; |
| 176 | + if ((ptr1 = pd->getChild(vt[n - 1])) == NULL) { |
| 177 | + ptr1 = new FileNode(vt[n - 1]); |
| 178 | + pd->addChild(vt[n - 1], ptr1); |
| 179 | + } |
| 180 | + ptr = ptr1; |
| 181 | + |
| 182 | + if (ptr->type() == TYPE_DIR) { |
| 183 | + // Error |
| 184 | + return; |
| 185 | + } |
| 186 | + pf = (FileNode *)ptr; |
| 187 | + pf->appendContent(content); |
| 188 | + } |
| 189 | + |
| 190 | + string readContentFromFile(string filePath) { |
| 191 | + vector<string> vt = tokenize(filePath); |
| 192 | + Node *ptr = root; |
| 193 | + Node *ptr1; |
| 194 | + |
| 195 | + // Needed for type casting |
| 196 | + DirInterface *pd; |
| 197 | + FileInterface *pf; |
| 198 | + |
| 199 | + for (auto &tk: vt) { |
| 200 | + pd = (DirNode *)ptr; |
| 201 | + if ((ptr1 = pd->getChild(tk)) == NULL) { |
| 202 | + // Error |
| 203 | + return ""; |
| 204 | + } |
| 205 | + ptr = ptr1; |
| 206 | + } |
| 207 | + if (ptr->type() != TYPE_FILE) { |
| 208 | + // Error |
| 209 | + return ""; |
| 210 | + } |
| 211 | + pf = (FileNode *)ptr; |
| 212 | + return pf->getContent(); |
| 213 | + } |
| 214 | +private: |
| 215 | + Node *root; |
| 216 | + |
| 217 | + vector<string> tokenize(const string &s) { |
| 218 | + vector<string> v; |
| 219 | + string tk; |
| 220 | + |
| 221 | + int ls = s.size(); |
| 222 | + int i = 0; |
| 223 | + while (true) { |
| 224 | + while (i < ls && s[i] == '/') { |
| 225 | + ++i; |
| 226 | + } |
| 227 | + if (i >= ls) { |
| 228 | + break; |
| 229 | + } |
| 230 | + while (i < ls && s[i] != '/') { |
| 231 | + tk.push_back(s[i++]); |
| 232 | + } |
| 233 | + v.push_back(tk); |
| 234 | + tk.clear(); |
| 235 | + if (i >= ls) { |
| 236 | + break; |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + return v; |
| 241 | + } |
| 242 | +}; |
| 243 | + |
| 244 | +/** |
| 245 | + * Your FileSystem object will be instantiated and called as such: |
| 246 | + * FileSystem obj = new FileSystem(); |
| 247 | + * vector<string> param_1 = obj.ls(path); |
| 248 | + * obj.mkdir(path); |
| 249 | + * obj.addContentToFile(filePath,content); |
| 250 | + * string param_4 = obj.readContentFromFile(filePath); |
| 251 | + */ |
0 commit comments