diff options
author | Alex AUVOLAT <alexis211@gmail.com> | 2012-05-19 14:07:01 +0200 |
---|---|---|
committer | Alex AUVOLAT <alexis211@gmail.com> | 2012-05-19 14:07:01 +0200 |
commit | 0d2f7645c3fb45d83497faf2a4b6fff8c3f175d1 (patch) | |
tree | a9038fce4cf81556fbdd5589caee8f4e9bfc5185 /src/user/lib | |
parent | 499ca6c243b05da176a2d4bd9a2317f0b28afc7f (diff) | |
download | TCE-0d2f7645c3fb45d83497faf2a4b6fff8c3f175d1.tar.gz TCE-0d2f7645c3fb45d83497faf2a4b6fff8c3f175d1.zip |
Added string class for FWIK.
Diffstat (limited to 'src/user/lib')
-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 | ||||
-rw-r--r-- | src/user/lib/libc/include/string.h | 3 | ||||
-rw-r--r-- | src/user/lib/libc/std/stdio.c | 40 | ||||
-rw-r--r-- | src/user/lib/libc/std/string.c | 40 |
10 files changed, 259 insertions, 51 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); } diff --git a/src/user/lib/libc/include/string.h b/src/user/lib/libc/include/string.h index fbe3ade..54fdf7a 100644 --- a/src/user/lib/libc/include/string.h +++ b/src/user/lib/libc/include/string.h @@ -18,6 +18,9 @@ char *strchr(const char *str, int c); char *strcat(char *dest, const char *src); int strcmp(const char *s1, const char *s2); +char* format_int(char* buf, int number); +char* format_hex(char *buf, unsigned v); + #ifdef __cplusplus } } #endif diff --git a/src/user/lib/libc/std/stdio.c b/src/user/lib/libc/std/stdio.c index 3595622..da2f094 100644 --- a/src/user/lib/libc/std/stdio.c +++ b/src/user/lib/libc/std/stdio.c @@ -20,46 +20,6 @@ void fprintf(FILE f, char* format, ...) { va_end(ap); } -// INTERNAL, FOR FORMATTING - -static char* format_int(char* buf, int number) { - if (number == 0) { - *(buf++) = '0'; - return buf; - } - if (number < 0) { - *(buf++) = '-'; - number = 0 - number; - } - - int order = 0, temp = number, i; - char numbers[] = "0123456789"; - while (temp > 0) { - order++; - temp /= 10; - } - - for (i = order; i > 0; i--) { - buf[i - 1] = numbers[number % 10]; - number /= 10; - } - return buf + order; -} - -static char* format_hex(char *buf, unsigned v) { - *(buf++) = '0'; - *(buf++) = 'x'; - - int i; - char hexdigits[] = "0123456789ABCDEF"; - for (i = 0; i < 8; i++) { - *(buf++) = hexdigits[v >> 28]; - v = v << 4; - } - return buf; -} - - // FUNCTIONS void fprint(FILE f, char *s) { diff --git a/src/user/lib/libc/std/string.c b/src/user/lib/libc/std/string.c index 5a41b7e..d8e4a48 100644 --- a/src/user/lib/libc/std/string.c +++ b/src/user/lib/libc/std/string.c @@ -79,3 +79,43 @@ uint16_t *memsetw(uint16_t *dest, uint16_t val, int count) { } return dest; } + + +// Formatting + +char* format_int(char* buf, int number) { + if (number == 0) { + *(buf++) = '0'; + return buf; + } + if (number < 0) { + *(buf++) = '-'; + number = 0 - number; + } + + int order = 0, temp = number, i; + char numbers[] = "0123456789"; + while (temp > 0) { + order++; + temp /= 10; + } + + for (i = order; i > 0; i--) { + buf[i - 1] = numbers[number % 10]; + number /= 10; + } + return buf + order; +} + +char* format_hex(char *buf, unsigned v) { + *(buf++) = '0'; + *(buf++) = 'x'; + + int i; + char hexdigits[] = "0123456789ABCDEF"; + for (i = 0; i < 8; i++) { + *(buf++) = hexdigits[v >> 28]; + v = v << 4; + } + return buf; +} |