diff options
author | Alex AUVOLAT <alexis211@gmail.com> | 2012-05-19 16:38:56 +0200 |
---|---|---|
committer | Alex AUVOLAT <alexis211@gmail.com> | 2012-05-19 16:38:56 +0200 |
commit | d502fce7d4db492690e39c72fc029aa05a65057d (patch) | |
tree | a5797a97212fff8142dc7f61792facca07c904eb /src/user/lib/fwik | |
parent | 8e07c1db6ba4bedd0f8fe537a6fb0ca80e5d25f4 (diff) | |
download | TCE-d502fce7d4db492690e39c72fc029aa05a65057d.tar.gz TCE-d502fce7d4db492690e39c72fc029aa05a65057d.zip |
More improvements in FWIK - more strings, Dir class, ...
Diffstat (limited to 'src/user/lib/fwik')
-rw-r--r-- | src/user/lib/fwik/Makefile | 2 | ||||
-rw-r--r-- | src/user/lib/fwik/include/IO/Dir.h | 24 | ||||
-rw-r--r-- | src/user/lib/fwik/include/IO/Node.h | 5 | ||||
-rw-r--r-- | src/user/lib/fwik/io/Dir.cpp | 37 | ||||
-rw-r--r-- | src/user/lib/fwik/io/Node.cpp | 24 | ||||
-rw-r--r-- | src/user/lib/fwik/io/Term.cpp | 12 |
6 files changed, 93 insertions, 11 deletions
diff --git a/src/user/lib/fwik/Makefile b/src/user/lib/fwik/Makefile index cf8896a..c99b288 100644 --- a/src/user/lib/fwik/Makefile +++ b/src/user/lib/fwik/Makefile @@ -1,5 +1,5 @@ Out = _fwik.o -Obj = String.o io/Node.o io/Term.o io/IOStream.o main.o +Obj = String.o io/Node.o io/Term.o io/Dir.o io/IOStream.o main.o ExtObj = $(SrcPath)/user/lib/libc/_libc.o diff --git a/src/user/lib/fwik/include/IO/Dir.h b/src/user/lib/fwik/include/IO/Dir.h new file mode 100644 index 0000000..bbfe3ed --- /dev/null +++ b/src/user/lib/fwik/include/IO/Dir.h @@ -0,0 +1,24 @@ +#ifndef DEF_FWIK_IO_DIR_H +#define DEF_FWIK_IO_DIR_H + +#include "Node.h" +#include <String.h> + +class Dir : public Node { + void _init(); + + public: + int pos; + + Dir(FILE f); + Dir(const char* file, int mode); + Dir(const Node &n); + virtual ~Dir(); + + String read_ent(); + + virtual Dir* as_dir() { return this; } +}; + +#endif + diff --git a/src/user/lib/fwik/include/IO/Node.h b/src/user/lib/fwik/include/IO/Node.h index ed8290f..6b5b063 100644 --- a/src/user/lib/fwik/include/IO/Node.h +++ b/src/user/lib/fwik/include/IO/Node.h @@ -9,19 +9,22 @@ #include <String.h> class Term; +class Dir; class Node { public: FILE fd; file_info info; - bool valid; + int error; // will be 0 if this is a valid file descriptor Node(FILE f); Node(const char* filename, int mode); + Node(FILE parent, const char* filename, int mode); virtual ~Node() {} void close(); virtual Term* as_term() { return 0; } + virtual Dir* as_dir() { return 0; } }; String path_cat(const String &a, const String &b, bool trailing_slash = true); diff --git a/src/user/lib/fwik/io/Dir.cpp b/src/user/lib/fwik/io/Dir.cpp new file mode 100644 index 0000000..cfcc77b --- /dev/null +++ b/src/user/lib/fwik/io/Dir.cpp @@ -0,0 +1,37 @@ +#include <IO/Dir.h> + +Dir::Dir(const Node &n) : Node(n) { + _init(); +} + +Dir::Dir(FILE f) : Node(f) { + _init(); + if (error == E_INVALID_TYPE) libc::close(fd); +} + +Dir::Dir(const char* filename, int mode) : Node(filename, mode) { + _init(); + if (error == E_INVALID_TYPE) libc::close(fd); +} + +void Dir::_init() { + if (error < 0) return; + pos = 0; + if ((info.type & FT_DIR) == 0) { + error = E_INVALID_TYPE; + } +} + +Dir::~Dir() { +} + +String Dir::read_ent() { + char buf[256]; + int l = libc::read(fd, pos, 256, buf); + if (l > 0) { + pos++; + return String(buf, l); + } else { + return ""; + } +} diff --git a/src/user/lib/fwik/io/Node.cpp b/src/user/lib/fwik/io/Node.cpp index daccc18..5585aa8 100644 --- a/src/user/lib/fwik/io/Node.cpp +++ b/src/user/lib/fwik/io/Node.cpp @@ -2,24 +2,34 @@ Node::Node(FILE f) { fd = f; - int i = libc::statf(f, &info); - valid = (i == 0); + error = libc::statf(f, &info); } Node::Node(const char* filename, int mode) { fd = libc::open(filename, mode); if (fd < 0) { - valid = false; + if (fd != E_NOT_FOUND) error = libc::stat(filename, &info); } else { int i = libc::statf(fd, &info); - valid = (i == 0); - if (!valid) libc::close(fd); + error = i; + if (error < 0) libc::close(fd); + } +} + +Node::Node(FILE parent, const char* filename, int mode) { + fd = libc::open_relative(parent, filename, mode); + if (fd < 0) { + if (fd != E_NOT_FOUND) error = libc::stat_relative(parent, filename, &info); + } else { + int i = libc::statf(fd, &info); + error = i; + if (error < 0) libc::close(fd); } } void Node::close() { - if (valid) libc::close(fd); - valid = false; + if (error == 0) libc::close(fd); + error = E_INVALID_FD; } //////// diff --git a/src/user/lib/fwik/io/Term.cpp b/src/user/lib/fwik/io/Term.cpp index 1540b9a..f8f686e 100644 --- a/src/user/lib/fwik/io/Term.cpp +++ b/src/user/lib/fwik/io/Term.cpp @@ -6,25 +6,33 @@ Term::Term(const Node &n) : Node(n) { Term::Term(FILE f) : Node(f) { _init(); + if (error E_INVALID_TYPE) libc::close(fd); } Term::Term(const char* filename, int mode) : Node(filename, mode) { _init(); + if (error == E_INVALID_TYPE) libc::close(fd); } void Term::_init() { + if (error < 0) return; if (info.type & FT_TERMINAL) { w = info.size >> 16; h = info.size & 0xFFFF; } else { - valid = false; + error = E_INVALID_TYPE; } hist.str = 0; hist.max = 12; } Term::~Term() { - //TODO : free readline history + if (hist.str != 0) { + for (int i = 0; i < hist.max; i++) { + if (hist.str[i] != 0) free(hist.str[i]); + } + free(hist.str); + } } void Term::print(const char *s) { |