diff options
| -rw-r--r-- | editor.cpp | 19 | ||||
| -rw-r--r-- | editor.h | 8 | ||||
| -rw-r--r-- | main.cpp | 10 | ||||
| -rw-r--r-- | utils.cpp | 27 | ||||
| -rw-r--r-- | utils.h | 8 |
5 files changed, 64 insertions, 8 deletions
@@ -1,9 +1,26 @@ #include "editor.h" +#include "utils.h" + +Editor::Editor(std::vector<std::string>* contents): + m_current_line(0) { -Editor::Editor(std::string* contents) { m_contents = contents; } int Editor::run_editor() { + std::vector<std::string>* 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'; + } +} @@ -2,13 +2,17 @@ #define EDITOR_H #include <string> #include <iostream> +#include <vector> class Editor { public: - Editor(std::string* contents); + Editor(std::vector<std::string>* contents); int run_editor(); private: - std::string* m_contents; + std::vector<std::string>* m_contents; + int m_current_line; + + int print_contents_on_line(int line) const; }; #endif @@ -1,19 +1,19 @@ #include <iostream> #include <fstream> #include <string> +#include <vector> #include "editor.h" -std::string* read_file(const std::string& filename) { +std::vector<std::string>* 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<std::string>* file_contents = new std::vector<std::string>(); 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<std::string>* 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 <unistd.h> //_getch +#include <termios.h> //_getch +#include <stdio.h> + +/* 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; + } @@ -0,0 +1,8 @@ +#ifndef UTILS_H +#define UTILS_H + +namespace Utils { + char getch(); +} + +#endif |
