From b639b99b3e8f4cf77560d8d473b13d992ac8eb10 Mon Sep 17 00:00:00 2001 From: Alexis211 Date: Sat, 24 Oct 2009 18:24:46 +0200 Subject: More work on userland syscalls : Files are implemented. TextFile now is a common (= kernel and userland) library. --- Source/Library/Common/TextFile.class.cpp | 30 ++++++++++++++++++++++++++++++ Source/Library/Common/TextFile.class.h | 31 +++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 Source/Library/Common/TextFile.class.cpp create mode 100644 Source/Library/Common/TextFile.class.h (limited to 'Source/Library/Common') diff --git a/Source/Library/Common/TextFile.class.cpp b/Source/Library/Common/TextFile.class.cpp new file mode 100644 index 0000000..040e2d7 --- /dev/null +++ b/Source/Library/Common/TextFile.class.cpp @@ -0,0 +1,30 @@ +#include "TextFile.class.h" + +bool TextFile::write(String str, bool addnl) { + ByteArray a(str, m_encoding); + if (addnl) a += (u8int)'\n'; + return File::write(a); +} + +String TextFile::readLine(char separator) { + const u32int bufflen = 512; + String ret; + ByteArray temp; + while (1) { + temp.resize(bufflen); + u32int r = read(temp); + u32int l = r; + for (u32int i = 0; i < r; i++) { + if (temp[i] == separator) { + l = i; + temp.resize(i); + break; + } + } + ret += temp.toString(m_encoding); + if (l != r or r != bufflen) { + if (l != r) seek((r - l) - 1, SM_BACKWARD); + return ret; + } + } +} diff --git a/Source/Library/Common/TextFile.class.h b/Source/Library/Common/TextFile.class.h new file mode 100644 index 0000000..3749b6a --- /dev/null +++ b/Source/Library/Common/TextFile.class.h @@ -0,0 +1,31 @@ +#ifndef DEF_TEXTFILE_CLASS_H +#define DEF_TEXTFILE_CLASS_H + +#ifdef THIS_IS_MELON_KERNEL +#include +#else +#include +#endif + +class TextFile : public File { + private: + u8int m_encoding; + + public: +#ifdef THIS_IS_MELON_KERNEL + TextFile(u8int encoding = UE_UTF8) : File() { m_encoding = encoding; } + TextFile(String filename, u8int mode = FM_READ, FSNode* start = 0, u8int encoding = UE_UTF8) + : File(filename, mode, start) { m_encoding = encoding; } +#else + TextFile(u32int id, u8int encoding = UE_UTF8) : File(id) { m_encoding = encoding; } + TextFile(String filename, u8int mode = FM_READ, FSNode start = FSNode(0), u8int encoding = UE_UTF8) + : File(filename, mode, start) { m_encoding = encoding; } +#endif + ~TextFile() {} + + void setEncoding(u8int encoding = UE_UTF8) { m_encoding = encoding; } + bool write(String str, bool addnl = false); // Addnl = wether or not to add \n at end + String readLine(char separator = '\n'); +}; + +#endif -- cgit v1.2.3