# HG changeset patch # User John Tsiombikas # Date 1422727295 -7200 # Node ID 9e3d77dad51b02bcbd369240e0eff741c6cbb4c7 # Parent dca518e371cf8f3d8504147cbe30bc18f3f256fa moving on... diff -r dca518e371cf -r 9e3d77dad51b src/fs.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fs.cc Sat Jan 31 20:01:35 2015 +0200 @@ -0,0 +1,146 @@ +#include +#include +#include +#include +#include +#include +#include +#include "fs.h" + +static char *clean_path(char *path); +static char *filename(char *path); + +FSNode::FSNode() +{ + init(); +} + +FSNode::~FSNode() +{ + destroy(); +} + +void FSNode::init() +{ + path = name = 0; + parent = 0; + expanded = false; + uid = gid = mode = 0; +} + +void FSNode::destroy() +{ + delete [] path; + children.clear(); + init(); +} + +void FSNode::destroy_tree() +{ + for(size_t i=0; idestroy_tree(); + delete children[i]; + } + destroy(); +} + +void FSNode::set_path(const char *path) +{ + delete [] this->path; + + char *buf = new char[strlen(path) + 1]; + strcpy(buf, path); + + char *tmp = clean_path(buf); + if(tmp == buf) { + this->path = tmp; + } else { + this->path = new char[strlen(tmp) + 1]; + strcpy(this->path, tmp); + delete [] buf; + } + + name = filename(this->path); +} + +void FSNode::set_name(const char *name) +{ + delete [] path; + + if(parent) { + char *path = new char[strlen(name) + strlen(parent->path) + 2]; + sprintf(path, "%s/%s", parent->path, name); + set_path(path); + } else { + path = this->name = new char[strlen(name) + 1]; + strcpy(path, name); + } +} + +#if 0 +FSDir *create_fsdir(const char *path) +{ + char *pathbuf; + + FSDir *node = new FSDir; + node->name = std::string(filename(path)); + + DIR *dir = opendir(path); + if(!dir) { + fprintf(stderr, "failed to open dir: %s: %s\n", path, strerror(errno)); + return 0; + } + + pathbuf = (char*)alloca(strlen(path) + NAME_MAX + 2); + + struct dirent *ent; + while((ent = readdir(dir))) { + sprintf(pathbuf, "%s/%s", path, ent->d_ent); + + struct stat st; + if(stat(pathbuf, &st) == -1) { + fprintf(stderr, "failed to stat: %s: %s\n", pathbuf, strerror(errno)); + continue; + } + + if(S_ISDIR(st.st_type)) { + FSDir *subdir = new FSDir; + subdir->name = std::string(ent->d_ent); + add_subdir(node, subdir); + } else { + FSFile *file = new FSFile; + file->name = std::string(ent->d_ent); + file->parent = node; + } + } +} +#endif + +static char *clean_path(char *path) +{ + while(*path && isspace(*path)) { + path++; + } + + char *end = path + strlen(path) - 1; + while(end >= path && isspace(*end)) { + *end-- = 0; + } + + char *ptr = path - 1; + while(*++ptr) { + if(*ptr == '\\') { + *ptr = '/'; + } + } + return path; +} + +static char *filename(char *path) +{ + char *ptr = strrchr(path, '/'); + if(ptr) { + return ptr + 1; + } + return path; +} diff -r dca518e371cf -r 9e3d77dad51b src/fs.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fs.h Sat Jan 31 20:01:35 2015 +0200 @@ -0,0 +1,65 @@ +#ifndef FS_H_ +#define FS_H_ + +#include + +class FSNode { +protected: + char *path, *name; + FSNode *parent; + + std::vector children; + bool expanded; + + unsigned int uid, gid, mode; + +private: + FSNode(const FSNode&); + FSNode &operator =(const FSNode&); + +public: + FSNode(); + virtual ~FSNode(); + + void init(); + void destroy(); + void destroy_tree(); + + virtual void set_path(const char *path); + virtual void set_name(const char *name); + + virtual const char *get_path() const; + virtual const char *get_name() const; + + virtual void add_child(FSNode *node); + + virtual FSNode *get_parent(); + virtual const FSNode *get_parent() const; + + virtual int get_child_count() const; + virtual FSNode *get_child(int n); + virtual const FSNode *get_child(int n) const; + + virtual void expand(); + virtual bool is_expanded() const; +}; + +class FSDir : public FSNode { +public: + FSDir(); + + virtual void expand(); +}; + +class FSFile : public FSNode { +protected: + unsigned long size; + +public: + FSFile(); + + void set_size(unsigned long s); + unsigned long get_size() const; +}; + +#endif // FS_H_