From 7c9146efca9b565777e1093e65272f710ae5d0d9 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sun, 9 Oct 2016 12:56:59 +0300 Subject: Add editor content outputting --- editor.cpp | 19 ++++++++++++++++++- editor.h | 8 ++++++-- main.cpp | 10 +++++----- utils.cpp | 27 +++++++++++++++++++++++++++ utils.h | 8 ++++++++ 5 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 utils.cpp create mode 100644 utils.h diff --git a/editor.cpp b/editor.cpp index b7ed487..9c35249 100644 --- a/editor.cpp +++ b/editor.cpp @@ -1,9 +1,26 @@ #include "editor.h" +#include "utils.h" + +Editor::Editor(std::vector* contents): + m_current_line(0) { -Editor::Editor(std::string* contents) { m_contents = contents; } int Editor::run_editor() { + std::vector* c = m_contents; + + bool stopped = false; + while (!stopped) { + print_contents_on_line(m_current_line); + + char input = Utils::getch(); + } return 0; } + +int Editor::print_contents_on_line(int line) const { + for (int i = line; i < m_contents->size(); i++) { + std::cout << i << " " << m_contents->at(i) << '\n'; + } +} diff --git a/editor.h b/editor.h index 491c3fb..43cfe77 100644 --- a/editor.h +++ b/editor.h @@ -2,13 +2,17 @@ #define EDITOR_H #include #include +#include class Editor { public: - Editor(std::string* contents); + Editor(std::vector* contents); int run_editor(); private: - std::string* m_contents; + std::vector* m_contents; + int m_current_line; + + int print_contents_on_line(int line) const; }; #endif diff --git a/main.cpp b/main.cpp index 1389efd..6378f1e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,19 +1,19 @@ #include #include #include +#include #include "editor.h" -std::string* read_file(const std::string& filename) { +std::vector* read_file(const std::string& filename) { std::ifstream file(filename); if (file.fail()) return nullptr; std::string str; - std::string* file_contents = new std::string(); + std::vector* file_contents = new std::vector(); while (std::getline(file, str)) { - *file_contents += str; - file_contents->push_back('\n'); + file_contents->push_back(str); } return file_contents; @@ -26,7 +26,7 @@ int main(int argc, char** argv) { } std::string filepath(argv[1]); - std::string* contents = read_file(filepath); + std::vector* contents = read_file(filepath); if (contents == nullptr) { std::cout << "Error reading file.\n"; return 1; diff --git a/utils.cpp b/utils.cpp new file mode 100644 index 0000000..f118fc5 --- /dev/null +++ b/utils.cpp @@ -0,0 +1,27 @@ +#include "utils.h" +#include //_getch +#include //_getch +#include + +/* Thanks to StackOverflow user mf_ */ +char Utils::getch(){ + char buf=0; + struct termios old={0}; + fflush(stdout); + if(tcgetattr(0, &old)<0) + perror("tcsetattr()"); + old.c_lflag&=~ICANON; + old.c_lflag&=~ECHO; + old.c_cc[VMIN]=1; + old.c_cc[VTIME]=0; + if(tcsetattr(0, TCSANOW, &old)<0) + perror("tcsetattr ICANON"); + if(read(0,&buf,1)<0) + perror("read()"); + old.c_lflag|=ICANON; + old.c_lflag|=ECHO; + if(tcsetattr(0, TCSADRAIN, &old)<0) + perror ("tcsetattr ~ICANON"); + //printf("%c\n",buf); + return buf; + } diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..35c600a --- /dev/null +++ b/utils.h @@ -0,0 +1,8 @@ +#ifndef UTILS_H +#define UTILS_H + +namespace Utils { + char getch(); +} + +#endif -- cgit v1.3