vrfileman

changeset 1:9e3d77dad51b

moving on...
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 31 Jan 2015 20:01:35 +0200
parents dca518e371cf
children 282da6123fd4
files src/fs.cc src/fs.h
diffstat 2 files changed, 211 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/fs.cc	Sat Jan 31 20:01:35 2015 +0200
     1.3 @@ -0,0 +1,146 @@
     1.4 +#include <stdio.h>
     1.5 +#include <string.h>
     1.6 +#include <ctype.h>
     1.7 +#include <errno.h>
     1.8 +#include <alloca.h>
     1.9 +#include <unistd.h>
    1.10 +#include <dirent.h>
    1.11 +#include "fs.h"
    1.12 +
    1.13 +static char *clean_path(char *path);
    1.14 +static char *filename(char *path);
    1.15 +
    1.16 +FSNode::FSNode()
    1.17 +{
    1.18 +	init();
    1.19 +}
    1.20 +
    1.21 +FSNode::~FSNode()
    1.22 +{
    1.23 +	destroy();
    1.24 +}
    1.25 +
    1.26 +void FSNode::init()
    1.27 +{
    1.28 +	path = name = 0;
    1.29 +	parent = 0;
    1.30 +	expanded = false;
    1.31 +	uid = gid = mode = 0;
    1.32 +}
    1.33 +
    1.34 +void FSNode::destroy()
    1.35 +{
    1.36 +	delete [] path;
    1.37 +	children.clear();
    1.38 +	init();
    1.39 +}
    1.40 +
    1.41 +void FSNode::destroy_tree()
    1.42 +{
    1.43 +	for(size_t i=0; i<children.size(); i++) {
    1.44 +		children[i]->destroy_tree();
    1.45 +		delete children[i];
    1.46 +	}
    1.47 +	destroy();
    1.48 +}
    1.49 +
    1.50 +void FSNode::set_path(const char *path)
    1.51 +{
    1.52 +	delete [] this->path;
    1.53 +
    1.54 +	char *buf = new char[strlen(path) + 1];
    1.55 +	strcpy(buf, path);
    1.56 +
    1.57 +	char *tmp = clean_path(buf);
    1.58 +	if(tmp == buf) {
    1.59 +		this->path = tmp;
    1.60 +	} else {
    1.61 +		this->path = new char[strlen(tmp) + 1];
    1.62 +		strcpy(this->path, tmp);
    1.63 +		delete [] buf;
    1.64 +	}
    1.65 +
    1.66 +	name = filename(this->path);
    1.67 +}
    1.68 +
    1.69 +void FSNode::set_name(const char *name)
    1.70 +{
    1.71 +	delete [] path;
    1.72 +
    1.73 +	if(parent) {
    1.74 +		char *path = new char[strlen(name) + strlen(parent->path) + 2];
    1.75 +		sprintf(path, "%s/%s", parent->path, name);
    1.76 +		set_path(path);
    1.77 +	} else {
    1.78 +		path = this->name = new char[strlen(name) + 1];
    1.79 +		strcpy(path, name);
    1.80 +	}
    1.81 +}
    1.82 +
    1.83 +#if 0
    1.84 +FSDir *create_fsdir(const char *path)
    1.85 +{
    1.86 +	char *pathbuf;
    1.87 +
    1.88 +	FSDir *node = new FSDir;
    1.89 +	node->name = std::string(filename(path));
    1.90 +
    1.91 +	DIR *dir = opendir(path);
    1.92 +	if(!dir) {
    1.93 +		fprintf(stderr, "failed to open dir: %s: %s\n", path, strerror(errno));
    1.94 +		return 0;
    1.95 +	}
    1.96 +
    1.97 +	pathbuf = (char*)alloca(strlen(path) + NAME_MAX + 2);
    1.98 +
    1.99 +	struct dirent *ent;
   1.100 +	while((ent = readdir(dir))) {
   1.101 +		sprintf(pathbuf, "%s/%s", path, ent->d_ent);
   1.102 +
   1.103 +		struct stat st;
   1.104 +		if(stat(pathbuf, &st) == -1) {
   1.105 +			fprintf(stderr, "failed to stat: %s: %s\n", pathbuf, strerror(errno));
   1.106 +			continue;
   1.107 +		}
   1.108 +
   1.109 +		if(S_ISDIR(st.st_type)) {
   1.110 +			FSDir *subdir = new FSDir;
   1.111 +			subdir->name = std::string(ent->d_ent);
   1.112 +			add_subdir(node, subdir);
   1.113 +		} else {
   1.114 +			FSFile *file =  new FSFile;
   1.115 +			file->name = std::string(ent->d_ent);
   1.116 +			file->parent = node;
   1.117 +		}
   1.118 +	}
   1.119 +}
   1.120 +#endif
   1.121 +
   1.122 +static char *clean_path(char *path)
   1.123 +{
   1.124 +	while(*path && isspace(*path)) {
   1.125 +		path++;
   1.126 +	}
   1.127 +
   1.128 +	char *end = path + strlen(path) - 1;
   1.129 +	while(end >= path && isspace(*end)) {
   1.130 +		*end-- = 0;
   1.131 +	}
   1.132 +
   1.133 +	char *ptr = path - 1;
   1.134 +	while(*++ptr) {
   1.135 +		if(*ptr == '\\') {
   1.136 +			*ptr = '/';
   1.137 +		}
   1.138 +	}
   1.139 +	return path;
   1.140 +}
   1.141 +
   1.142 +static char *filename(char *path)
   1.143 +{
   1.144 +	char *ptr = strrchr(path, '/');
   1.145 +	if(ptr) {
   1.146 +		return ptr + 1;
   1.147 +	}
   1.148 +	return path;
   1.149 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/fs.h	Sat Jan 31 20:01:35 2015 +0200
     2.3 @@ -0,0 +1,65 @@
     2.4 +#ifndef FS_H_
     2.5 +#define FS_H_
     2.6 +
     2.7 +#include <vector>
     2.8 +
     2.9 +class FSNode {
    2.10 +protected:
    2.11 +	char *path, *name;
    2.12 +	FSNode *parent;
    2.13 +
    2.14 +	std::vector<FSNode*> children;
    2.15 +	bool expanded;
    2.16 +
    2.17 +	unsigned int uid, gid, mode;
    2.18 +
    2.19 +private:
    2.20 +	FSNode(const FSNode&);
    2.21 +	FSNode &operator =(const FSNode&);
    2.22 +
    2.23 +public:
    2.24 +	FSNode();
    2.25 +	virtual ~FSNode();
    2.26 +
    2.27 +	void init();
    2.28 +	void destroy();
    2.29 +	void destroy_tree();
    2.30 +
    2.31 +	virtual void set_path(const char *path);
    2.32 +	virtual void set_name(const char *name);
    2.33 +
    2.34 +	virtual const char *get_path() const;
    2.35 +	virtual const char *get_name() const;
    2.36 +
    2.37 +	virtual void add_child(FSNode *node);
    2.38 +
    2.39 +	virtual FSNode *get_parent();
    2.40 +	virtual const FSNode *get_parent() const;
    2.41 +
    2.42 +	virtual int get_child_count() const;
    2.43 +	virtual FSNode *get_child(int n);
    2.44 +	virtual const FSNode *get_child(int n) const;
    2.45 +
    2.46 +	virtual void expand();
    2.47 +	virtual bool is_expanded() const;
    2.48 +};
    2.49 +
    2.50 +class FSDir : public FSNode {
    2.51 +public:
    2.52 +	FSDir();
    2.53 +
    2.54 +	virtual void expand();
    2.55 +};
    2.56 +
    2.57 +class FSFile : public FSNode {
    2.58 +protected:
    2.59 +	unsigned long size;
    2.60 +
    2.61 +public:
    2.62 +	FSFile();
    2.63 +
    2.64 +	void set_size(unsigned long s);
    2.65 +	unsigned long get_size() const;
    2.66 +};
    2.67 +
    2.68 +#endif	// FS_H_