diff options
Diffstat (limited to 'src/user/lib/fwik')
-rw-r--r-- | src/user/lib/fwik/String.cpp | 157 | ||||
-rw-r--r-- | src/user/lib/fwik/include/IO/IOStream.h | 4 | ||||
-rw-r--r-- | src/user/lib/fwik/include/IO/Term.h | 5 | ||||
-rw-r--r-- | src/user/lib/fwik/include/String.h | 35 | ||||
-rw-r--r-- | src/user/lib/fwik/io/IOStream.cpp | 4 | ||||
-rw-r--r-- | src/user/lib/fwik/io/Term.cpp | 11 | ||||
-rw-r--r-- | src/user/lib/fwik/main.cpp | 11 |
7 files changed, 216 insertions, 11 deletions
diff --git a/src/user/lib/fwik/String.cpp b/src/user/lib/fwik/String.cpp index 75114c2..982ce4e 100644 --- a/src/user/lib/fwik/String.cpp +++ b/src/user/lib/fwik/String.cpp @@ -1 +1,158 @@ #include <String.h> +#include <string.h> + +String::String() { + ptr = 0; + len = 0; +} + +String::String(const String &other) { + len = other.len; + if (len == 0) { + ptr = 0; + } else { + ptr = (char*)malloc(len + 1); + libc::memcpy(ptr, other.ptr, len + 1); + } +} + +String::String(char* from) { + len = (from == 0 ? 0 : libc::strlen(from)); + if (len == 0) { + ptr = 0; + } else { + ptr = (char*)malloc(len + 1); + libc::memcpy(ptr, from, len + 1); + } +} + +String::String(char* from, int l) { + len = l; + if (len < 0) len = 0; + if (len == 0) { + ptr = 0; + } else { + ptr = (char*)malloc(len + 1); + libc::memcpy(ptr, from, len); + ptr[len] = 0; + } +} + +String::String(char c, int count) { + len = count; + if (len == 0) { + ptr = 0; + } else { + ptr = (char*)malloc(len + 1); + libc::memset(ptr, c, len); + ptr[len] = 0; + } +} + +String::~String() { + if (ptr != 0) free(ptr); +} + +void String::operator=(const String &other) { + if (ptr != 0) free(ptr); + len = other.len; + if (len == 0) { + ptr = 0; + } else { + ptr = (char*)malloc(len + 1); + libc::memcpy(ptr, other.ptr, len + 1); + } +} + +void String::operator=(char* from) { + if (ptr != 0) free(ptr); + len = (from == 0 ? 0 : libc::strlen(from)); + if (len == 0) { + ptr = 0; + } else { + ptr = (char*)malloc(len + 1); + libc::memcpy(ptr, from, len + 1); + } +} + +char* String::c_str() { + if (ptr == 0) return ""; + return ptr; +} + +bool String::operator==(const String& other) { + if (len != other.len) return false; + for (int i = 0; i < len; i++) if (ptr[i] != other.ptr[i]) return false; + return true; +} + +bool String::operator==(char* other) { + if (other == 0) return (len == 0); + if (len != libc::strlen(other)) return false; + for (int i = 0; i < len; i++) if (ptr[i] != other[i]) return false; + return true; +} + +bool String::operator<(const String& other) { + for (int i = 0; i < len && i < other.len; i++) { + if (ptr[i] > other.ptr[i]) return false; + if (ptr[i] < other.ptr[i]) return true; + } + if (len < other.len) return true; + return false; +} + +static char crap; +char &String::operator[](int pos) { + if (pos >= 0 && pos < len) return ptr[pos]; + crap = 0; + return crap; +} + +String String::substr(int start, int count) { + if (start + count > len) count = len - start; + return String(ptr + start, count); +} + +String String::operator+(const String& other) { + String ret(' ', len + other.len); + libc::memcpy(ret.ptr, ptr, len); + libc::memcpy(ret.ptr + len, other.ptr, other.len); + return ret; +} + +void String::operator+=(const String& other) { + if (other.len == 0) return; + int newlen = len + other.len; + char* newptr = (char*)malloc(newlen + 1); + libc::memcpy(newptr, ptr, len); + libc::memcpy(newptr+len, other.ptr, other.len); + newptr[newlen] = 0; + free(ptr); + ptr = newptr; + len = newlen; +} + +void String::operator+=(char c) { + char* newptr = (char*)malloc(len + 2); + libc::memcpy(newptr, ptr, len); + newptr[len] = c; + len++; + newptr[len] = 0; + free(ptr); + ptr = newptr; +} + +String String::dec(int i) { + char b[16]; + const char *e = libc::format_int(b, i); + return String(b, e - b); +} + +String String::hex(uint32_t v) { + char b[16]; + const char *e = libc::format_hex(b, v); + return String(b, e - b); +} + + diff --git a/src/user/lib/fwik/include/IO/IOStream.h b/src/user/lib/fwik/include/IO/IOStream.h index e9ae246..e381cf5 100644 --- a/src/user/lib/fwik/include/IO/IOStream.h +++ b/src/user/lib/fwik/include/IO/IOStream.h @@ -3,6 +3,8 @@ #include "Term.h" +#include <String.h> + class IOStream { public: Term *term; @@ -12,7 +14,7 @@ class IOStream { void print(char* str); void printf(char* fmt, ...); - char* readln(); + String readln(); IOStream &operator<<(char* s) { print(s); diff --git a/src/user/lib/fwik/include/IO/Term.h b/src/user/lib/fwik/include/IO/Term.h index cf90789..4fd9306 100644 --- a/src/user/lib/fwik/include/IO/Term.h +++ b/src/user/lib/fwik/include/IO/Term.h @@ -3,6 +3,7 @@ #include <stdio.h> #include "Node.h" +#include <String.h> #include <readline.h> @@ -22,8 +23,8 @@ class Term : public Node { virtual void print(char *s); virtual void printf(char* fmt, ...); virtual void vprintf(char* fmt, va_list ap); - virtual char* readln(); - char* readline(); + virtual String readln(); + String readline(); virtual Term* as_term() { return this; } }; diff --git a/src/user/lib/fwik/include/String.h b/src/user/lib/fwik/include/String.h index 2a44046..672220d 100644 --- a/src/user/lib/fwik/include/String.h +++ b/src/user/lib/fwik/include/String.h @@ -1,5 +1,40 @@ #ifndef DEF_FWIK_STRING_H #define DEF_FWIK_STRING_H +#include <cpp.h> + +class String { + private: + char *ptr; // zero-terminated for internal purposes. + int len; + + public: + String(); + String(const String& other); + String(char* ptr); + String(char* ptr, int len); + String(char c, int count); + ~String(); + void operator=(const String &string); + void operator=(char* ptr); + + char* c_str(); + + bool operator==(const String& other); + bool operator==(char* other); + bool operator<(const String& other); + char &operator[](int pos); + + int size() { return len; } + operator bool() { return len != 0; } + String substr(int start, int count); + + String operator+(const String& other); + void operator+=(const String& other); + void operator+=(char c); + + static String dec(int i); + static String hex(uint32_t v); +}; #endif diff --git a/src/user/lib/fwik/io/IOStream.cpp b/src/user/lib/fwik/io/IOStream.cpp index bcaa581..ae495c0 100644 --- a/src/user/lib/fwik/io/IOStream.cpp +++ b/src/user/lib/fwik/io/IOStream.cpp @@ -13,7 +13,7 @@ void IOStream::printf(char* fmt, ...) { va_end(ap); } -char* IOStream::readln() { - if (term == 0) return 0; +String IOStream::readln() { + if (term == 0) return ""; return term->readln(); } diff --git a/src/user/lib/fwik/io/Term.cpp b/src/user/lib/fwik/io/Term.cpp index 1c6db66..f7f28ec 100644 --- a/src/user/lib/fwik/io/Term.cpp +++ b/src/user/lib/fwik/io/Term.cpp @@ -42,10 +42,13 @@ void Term::vprintf(char* fmt, va_list ap) { libc::vfprintf(fd, fmt, ap); } -char* Term::readln() { - return libc::freadln(fd); +String Term::readln() { + char *s = libc::freadln(fd); + String ret(s); + free(s); + return ret; } -char *Term::readline() { - return libc::freadline(fd, &hist); +String Term::readline() { + return String(libc::freadline(fd, &hist)); } diff --git a/src/user/lib/fwik/main.cpp b/src/user/lib/fwik/main.cpp index 8e2e289..e26cb46 100644 --- a/src/user/lib/fwik/main.cpp +++ b/src/user/lib/fwik/main.cpp @@ -1,10 +1,11 @@ #include <IO/IOStream.h> #include <stdio.h> #include <cpp.h> +#include <String.h> IOStream stdio; -int Main(char **args); // FWIK app main +int Main(String *args); // FWIK app main extern "C" int main(char **args) { stdio.term = 0; @@ -13,7 +14,13 @@ extern "C" int main(char **args) { if (zero.info.type & FT_TERMINAL) { stdio.term = new Term(zero); } - Main(args); + + int argc = 0; + while (args[argc] != 0) argc++; + String s_args[argc+1]; + for (int i = 0; i < argc; i++) s_args[i] = args[i]; + + Main(s_args); } |