summaryrefslogtreecommitdiff
path: root/src/user/lib/fwik/io
diff options
context:
space:
mode:
Diffstat (limited to 'src/user/lib/fwik/io')
-rw-r--r--src/user/lib/fwik/io/Dir.cpp37
-rw-r--r--src/user/lib/fwik/io/Node.cpp24
-rw-r--r--src/user/lib/fwik/io/Term.cpp12
3 files changed, 64 insertions, 9 deletions
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) {