diff options
Diffstat (limited to 'src/user/lib/fwik/io')
-rw-r--r-- | src/user/lib/fwik/io/IOStream.cpp | 4 | ||||
-rw-r--r-- | src/user/lib/fwik/io/Node.cpp | 87 | ||||
-rw-r--r-- | src/user/lib/fwik/io/Term.cpp | 8 |
3 files changed, 92 insertions, 7 deletions
diff --git a/src/user/lib/fwik/io/IOStream.cpp b/src/user/lib/fwik/io/IOStream.cpp index ae495c0..9d13251 100644 --- a/src/user/lib/fwik/io/IOStream.cpp +++ b/src/user/lib/fwik/io/IOStream.cpp @@ -1,11 +1,11 @@ #include <IO/IOStream.h> -void IOStream::print(char* str) { +void IOStream::print(const char* str) { if (term == 0) return; term->print(str); } -void IOStream::printf(char* fmt, ...) { +void IOStream::printf(const char* fmt, ...) { if (term == 0) return; va_list ap; va_start(ap, fmt); diff --git a/src/user/lib/fwik/io/Node.cpp b/src/user/lib/fwik/io/Node.cpp index 1d8b7f5..daccc18 100644 --- a/src/user/lib/fwik/io/Node.cpp +++ b/src/user/lib/fwik/io/Node.cpp @@ -6,7 +6,7 @@ Node::Node(FILE f) { valid = (i == 0); } -Node::Node(char* filename, int mode) { +Node::Node(const char* filename, int mode) { fd = libc::open(filename, mode); if (fd < 0) { valid = false; @@ -21,3 +21,88 @@ void Node::close() { if (valid) libc::close(fd); valid = false; } + +//////// + +static void simplify_path(char* p) { + char *it = p; + char *member = it; + while (*it != 0) { + if (*it == '/') { + if (it == member && it != p) { + // two consecutive slashes + char *i = member; + while (1) { + i[0] = i[1]; + if (i[0] == 0) break; + i++; + } + } else { + *it = 0; + if (libc::strcmp(member, ".") == 0) { + char *i = member; + while (1) { + i[0] = i[2]; + if (i[0] == 0) break; + i++; + } + } else if (libc::strcmp(member, "..") == 0) { + *it = '/'; + char* start = member - 2; + char* next = member + 3; + while (start > p && *start != '/') { + start--; + } + start++; + it = member = start; + while (1) { + *start = *next; + if (*start == 0) break; + start++; + next++; + } + } else { + *it = '/'; + it++; + member = it; + } + } + } else { + it++; + } + } +} + +String path_cat(const String &a, const String &b, bool trailing_slash) { + int len, la = a.size(), lb = b.size(); + if (b[0] == '/') { + len = lb + 2; + } else { + len = la + lb + 3; + } + char buf[len]; + if (b[0] == '/') { + libc::memcpy(buf, b.c_str(), lb); + if (buf[lb-1] != '/') { + buf[lb++] = '/'; + } + buf[lb] = 0; + } else { + libc::memcpy(buf, a.c_str(), la); + if (buf[la-1] != '/') { + buf[la++] = '/'; + } + libc::memcpy(buf + la, b.c_str(), lb); + if (buf[la + lb - 1] != '/') { + buf[la + lb] = '/'; + lb++; + } + buf[la + lb] = 0; + } + simplify_path(buf); + if (!trailing_slash) { + int l = libc::strlen(buf); + if (buf[l-1] == '/') buf[l-1] = 0; + } + return String(buf); +} diff --git a/src/user/lib/fwik/io/Term.cpp b/src/user/lib/fwik/io/Term.cpp index f7f28ec..1540b9a 100644 --- a/src/user/lib/fwik/io/Term.cpp +++ b/src/user/lib/fwik/io/Term.cpp @@ -8,7 +8,7 @@ Term::Term(FILE f) : Node(f) { _init(); } -Term::Term(char* filename, int mode) : Node(filename, mode) { +Term::Term(const char* filename, int mode) : Node(filename, mode) { _init(); } @@ -27,18 +27,18 @@ Term::~Term() { //TODO : free readline history } -void Term::print(char *s) { +void Term::print(const char *s) { libc::fprint(fd, s); } -void Term::printf(char* fmt, ...) { +void Term::printf(const char* fmt, ...) { va_list ap; va_start(ap, fmt); libc::vfprintf(fd, fmt, ap); va_end(ap); } -void Term::vprintf(char* fmt, va_list ap) { +void Term::vprintf(const char* fmt, va_list ap) { libc::vfprintf(fd, fmt, ap); } |