From 64fc3862f602750733b7dc0447d22ae5d4146821 Mon Sep 17 00:00:00 2001 From: Alexis211 Date: Sat, 19 Sep 2009 19:21:28 +0200 Subject: Implemented ByteArray and wf command. --- Source/Kernel/Core/kmain.wtf.cpp | 19 ++++++++- Source/Kernel/Library/ByteArray.class.cpp | 63 ++++++++++++++++++++++++++++++ Source/Kernel/Library/ByteArray.class.h | 24 ++++++++++++ Source/Kernel/Library/WChar.class.h | 9 +++++ Source/Kernel/Makefile | 1 + Source/Kernel/Melon.ke | Bin 160963 -> 162952 bytes Source/Kernel/VFS/File.class.cpp | 27 ++++++++++++- Source/Kernel/VFS/File.class.h | 7 +++- 8 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 Source/Kernel/Library/ByteArray.class.cpp create mode 100644 Source/Kernel/Library/ByteArray.class.h (limited to 'Source/Kernel') diff --git a/Source/Kernel/Core/kmain.wtf.cpp b/Source/Kernel/Core/kmain.wtf.cpp index e263393..11fbbea 100644 --- a/Source/Kernel/Core/kmain.wtf.cpp +++ b/Source/Kernel/Core/kmain.wtf.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -211,7 +212,23 @@ void kmain(multiboot_info_t* mbd, u32int magic) { *kvt << "No argument specified.\n"; } } else if (tokens[0] == "wf") { - *kvt << "Sorry, this command isn't implemented yet.\n"; + //*kvt << "Sorry, this command isn't implemented yet.\n"; + if (tokens.size() == 1) { + *kvt << "No file to write !\n"; + } else { + File f(tokens[1], FM_TRUNCATE, cwd); + if (f.valid()) { + String t = kvt->readLine(); + while (t != ".") { + t += "\n"; + ByteArray temp(t); + f.write(temp); + t = kvt->readLine(); + } + } else { + *kvt << "Error openning file.\n"; + } + } } else if (tokens[0] == "devices") { Vector dev = Dev::findDevices(); *kvt << " - Detected devices :\n"; diff --git a/Source/Kernel/Library/ByteArray.class.cpp b/Source/Kernel/Library/ByteArray.class.cpp new file mode 100644 index 0000000..b987817 --- /dev/null +++ b/Source/Kernel/Library/ByteArray.class.cpp @@ -0,0 +1,63 @@ +#include "ByteArray.class.h" + +//Define size of a uchar_repr_t +#define CHARSZ(x) (x.c[0] == 0 ? 0 : (x.c[1] == 0 ? 1 : (x.c[2] == 0 ? 2 : (x.c[3] == 0 ? 3 : 4)))) + +using namespace CMem; + +ByteArray::ByteArray(const char* c) : BasicString() { + m_length = strlen(c); + memcpy(m_string, (u8int*)c, m_length); +} + +void ByteArray::affect(const String &string, u8int encoding) { + m_length = 0; + for (u32int i = 0; i < string.size(); i++) { + uchar_repr_t a = string[i].encode(encoding); + m_length += CHARSZ(a); + } + if (m_string != 0) delete m_string; + if (m_length == 0) { + m_string = 0; + return; + } + m_string = new u8int[m_length]; + u32int x = 0; + for (u32int i = 0; i < string.size(); i++) { + uchar_repr_t a = string[i].encode(encoding); + memcpy(m_string + x, (u8int*)a.c, CHARSZ(a)); + x += CHARSZ(a); + } +} + +void ByteArray::resize(u32int size) { + if (size == m_length) return; + if (size == 0) { + delete m_string; + m_length = 0; + m_string = 0; + } + u8int *nd = new u8int[size]; + if (size < m_length) { + memcpy(nd, m_string, size); + } else { + memcpy(nd, m_string, m_length); + memset(nd + m_length, 0, size - m_length); + } + delete m_string; + m_string = nd; + m_length = size; +} + +void ByteArray::dump(VirtualTerminal *vt) { + vt->hexDump(m_string, m_length); +} + +ByteArray::operator String () { + char* c = new char[m_length + 1]; + memcpy((u8int*)c, m_string, m_length); + c[m_length] = 0; //Add NULL terminator + String r(c); + delete c; + return r; +} diff --git a/Source/Kernel/Library/ByteArray.class.h b/Source/Kernel/Library/ByteArray.class.h new file mode 100644 index 0000000..4b2dbed --- /dev/null +++ b/Source/Kernel/Library/ByteArray.class.h @@ -0,0 +1,24 @@ +#ifndef DEF_BYTEARRAY_CLASS_H +#define DEF_BYTEARRAY_CLASS_H + +#include +#include + +class ByteArray : public BasicString { + public: + ByteArray() : BasicString() {} + ByteArray(const ByteArray& other) : BasicString(other) {} + ByteArray(const char* c); + ByteArray(u32int size) : BasicString((u8int)0, size) {} + ByteArray(const String &string, u8int encoding = UE_UTF8) : BasicString() { affect(string, encoding); } + + void affect(const String& string, u8int encoding = UE_UTF8); + void resize(u32int size); + + void dump(VirtualTerminal *vt); + + operator String (); + operator u8int* () { return m_string; } +}; + +#endif diff --git a/Source/Kernel/Library/WChar.class.h b/Source/Kernel/Library/WChar.class.h index 63f1ea3..62226f5 100644 --- a/Source/Kernel/Library/WChar.class.h +++ b/Source/Kernel/Library/WChar.class.h @@ -43,6 +43,15 @@ struct WChar { uchar_repr_t toUtf16(); uchar_repr_t toUtf32(); + uchar_repr_t encode(u8int encoding = UE_UTF8) { + if (encoding == UE_UTF8) return toUtf8(); + //if (encoding == UE_UTF16) return toUtf16(); + if (encoding == UE_UTF32) return toUtf32(); + uchar_repr_t x; + x.c[0] = toAscii(); + return x; + } + inline WChar operator+ (u32int other) { WChar r; r.value = value + other; diff --git a/Source/Kernel/Makefile b/Source/Kernel/Makefile index 3dbd125..67e9e39 100644 --- a/Source/Kernel/Makefile +++ b/Source/Kernel/Makefile @@ -35,6 +35,7 @@ Objects = Core/loader.wtf.o \ VTManager/VT.ns.o \ Library/Bitset.class.o \ Library/String.class.o \ + Library/ByteArray.class.o \ Library/WChar.class.o \ VFS/Partition.class.o \ VFS/Part.ns.o \ diff --git a/Source/Kernel/Melon.ke b/Source/Kernel/Melon.ke index 6ece4a6..605b45a 100755 Binary files a/Source/Kernel/Melon.ke and b/Source/Kernel/Melon.ke differ diff --git a/Source/Kernel/VFS/File.class.cpp b/Source/Kernel/VFS/File.class.cpp index ba837e9..f3c7d54 100644 --- a/Source/Kernel/VFS/File.class.cpp +++ b/Source/Kernel/VFS/File.class.cpp @@ -18,7 +18,11 @@ bool File::open(String filename, u8int mode, FSNode* start) { if (m_valid) return false; FSNode* node = VFS::find(filename, start); - if (node == NULL) return false; + if (node == NULL){ + if (mode == FM_READ) return false; + node = VFS::createFile(filename, start); + if (node == 0) return false; + } if (node->type() != NT_FILE) return false; m_file = (FileNode*) node; @@ -62,6 +66,27 @@ bool File::write(u32int length, u8int *data) { return false; } +u32int File::read(ByteArray &data) { + if (!m_valid) { + data.clear(); + return 0; + } + u32int l = m_file->read(m_position, data.size(), (u8int*)data); + m_position += l; + if (l != data.size()) data.resize(l); + return l; +} + +bool File::write(ByteArray &data) { + if (!m_valid) return false; + if (!m_writable) return false; + if (m_file->write(m_position, data.size(), (u8int*)data)) { + m_position += data.size(); + return true; + } + return false; +} + bool File::seek(u64int count, u8int mode) { if (!m_valid) return false; if (mode == SM_FORWARD) { diff --git a/Source/Kernel/VFS/File.class.h b/Source/Kernel/VFS/File.class.h index 955db85..460036c 100644 --- a/Source/Kernel/VFS/File.class.h +++ b/Source/Kernel/VFS/File.class.h @@ -2,6 +2,7 @@ #define DEF_FILE_CLASS_H #include +#include enum { FM_READ = 0, //Open for read, put cursor at beginning @@ -30,12 +31,16 @@ class File { ~File(); bool open(String filename, u8int mode = FM_READ, FSNode* start = 0); + void close(bool unregisterFD = true); //unregisterFD = whether or not we must unregister the file descriptor from process + u32int read(u32int max_length, u8int *data); bool write(u32int length, u8int *data); + u32int read(ByteArray &data); //Fills ByteArray at its current length or shrinks it if necessary + bool write(ByteArray &data); + bool seek(u64int count, u8int mode); u64int position() { return m_position; } u64int length() { return m_file->getLength(); } - void close(bool unregisterFD = true); //unregisterFD = whether or not we must unregister the file descriptor from process bool valid() { return m_valid; } }; -- cgit v1.2.3