From 6a873aa2b8a578dcea19f4dc44bcce76ddd76721 Mon Sep 17 00:00:00 2001 From: Daniel Knittl-Frank Date: Mon, 4 Oct 2010 20:19:12 +0200 Subject: [PATCH] internally use a vector to store keyboard map --- C/Layout.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++---- C/create_dummy_file.cpp | 8 ++++--- 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/C/Layout.cpp b/C/Layout.cpp index 10c7113..5066bdb 100644 --- a/C/Layout.cpp +++ b/C/Layout.cpp @@ -1,24 +1,75 @@ #pragma once #include +#include +#include +#include class Layout { + typedef Glib::ustring string; + typedef std::vector vector; public: + typedef vector::iterator iterator; + typedef vector::reverse_iterator reverse_iterator; + Layout(const std::string &s = "") { this->layout.reserve(11*3+4); // 11 chars per row average + newlines - this->layout = s; + + this->layout.clear(); + this->add(s); } virtual ~Layout() {} //Layout(const Layout &l) {} //Layout &operator=(const Layout &l) {} + void add(const std::string &s) { + string us(s); + copy(us.begin(), us.end(), std::back_inserter(this->layout)); + } void addLine(const std::string &s) { - this->layout.append(s+"\n"); + this->add(s); + this->layout.push_back('\n'); + } + + // TODO write a back_inserter + const std::string toString() const { + std::string s; + for(vector::const_iterator it = this->layout.begin(); + it != this->layout.end(); ++it) { + s.append(string(1, *it)); + } + return s; + } + const string toUString() const { + string s; + for(vector::const_iterator it = this->layout.begin(); + it != this->layout.end(); ++it) { + s.append(string(1, *it)); + } + return s; } - const std::string &toString() const { - return this->layout; + // random access + string::reference operator[](const int i) { + return this->layout[i]; + } + + // return an iterator on the vector + // iterates gunichars, need to be converted before sending to stream + iterator begin() { + return this->layout.begin(); + } + iterator end() { + return this->layout.end(); + } + reverse_iterator rbegin() { + return this->layout.rbegin(); + } + reverse_iterator rend() { + return this->layout.rend(); } private: - std::string layout; + // a vector storing keys to allow random (write) access + // only stores a single char per position + vector layout; }; diff --git a/C/create_dummy_file.cpp b/C/create_dummy_file.cpp index f56cb7f..f680179 100644 --- a/C/create_dummy_file.cpp +++ b/C/create_dummy_file.cpp @@ -4,19 +4,21 @@ #include +#include "Layout.cpp" + using namespace std; using namespace Glib; int main(int argc, char **argv) { // dummy neo string - ustring us("# Evolved Layout\nxvlcw khgfqß´\nuiaeo snrtdy\nüöäpz bm,.j\n"); + Layout neo("# Evolved Layout\nxvlcw khgfqß´\nuiaeo snrtdy\nüöäpz bm,.j\n"); ofstream f("test.txt"); // print each char on its own line - for(ustring::iterator it = us.begin(); - it != us.end(); ++it) { + for(Layout::iterator it = neo.begin(); + it != neo.end(); ++it) { f << '"' << ustring(1, *it).raw() << '"' << endl; } -- 2.11.4.GIT