From c6639e3359c8da5a79de284e43e8f5cac0f67861 Mon Sep 17 00:00:00 2001 From: Hamatoma Date: Sun, 21 Jun 2015 10:43:02 +0200 Subject: [PATCH] reditor work under win with "open file" --- appl/reditor/mainwindow.cpp | 18 ++++ appl/reditor/mainwindow.hpp | 2 + appl/reditor/{ReEditor.pro => reditor.pro} | 0 base/ReFile.cpp | 112 ++++++++++++--------- base/ReFile.hpp | 54 +++++----- 5 files changed, 115 insertions(+), 71 deletions(-) rename appl/reditor/{ReEditor.pro => reditor.pro} (100%) diff --git a/appl/reditor/mainwindow.cpp b/appl/reditor/mainwindow.cpp index dafe856..a9064a0 100644 --- a/appl/reditor/mainwindow.cpp +++ b/appl/reditor/mainwindow.cpp @@ -13,6 +13,7 @@ #include "gui/regui.hpp" #include "../reditor/mainwindow.hpp" #include "ui_mainwindow.h" +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ @@ -22,9 +23,26 @@ MainWindow::MainWindow(QWidget *parent) : edit->setLines(m_file); edit->setCursorLine(0); edit->paragraphs().load(0, 5, edit); + connect(ui->actionOpen, SIGNAL(triggered()), this, + SLOT(openFile())); } MainWindow::~MainWindow(){ delete ui; } + +/** + * Calls the file selection dialog. + */ +void MainWindow::openFile(){ + QString name = m_file == NULL ? "" : m_file->filename(); + name = QFileDialog::getOpenFileName(this, tr("Select File"), + name); + if (!name.isEmpty()){ + delete m_file; + m_file = new ReFile(name, false); + ui->widget->setLines(m_file); + } + +} diff --git a/appl/reditor/mainwindow.hpp b/appl/reditor/mainwindow.hpp index b4f4380..eb31030 100644 --- a/appl/reditor/mainwindow.hpp +++ b/appl/reditor/mainwindow.hpp @@ -27,6 +27,8 @@ public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); +protected slots: + void openFile(); private: Ui::MainWindow *ui; ReFile* m_file; diff --git a/appl/reditor/ReEditor.pro b/appl/reditor/reditor.pro similarity index 100% rename from appl/reditor/ReEditor.pro rename to appl/reditor/reditor.pro diff --git a/base/ReFile.cpp b/base/ReFile.cpp index 8b5e26d..6ff13ab 100644 --- a/base/ReFile.cpp +++ b/base/ReFile.cpp @@ -11,6 +11,35 @@ #include "base/rebase.hpp" +#if defined __linux__ || defined WIN32 +void* memichr(void* heap, int cc, size_t length){ + const char* heap2 = reinterpret_cast(heap); + int cc2 = tolower(cc); + void* rc = NULL; + while(length > 0){ + if (cc2 == tolower(*heap2++)){ + rc = (void*)(heap2 - 1); + break; + } + } + return rc; +} + +int memicmp(const void* str1, const void* str2, size_t length){ + const char* str12 = reinterpret_cast(str1); + const char* str22 = reinterpret_cast(str2); + int rc = 0; + for (size_t ix = 0; ix < length; ix++){ + int diff = tolower(*str12++) - tolower(*str22++); + if (diff != 0){ + rc = diff; + break; + } + } + return rc; +} +#endif /* __linux__ */ + /** * Constructor. */ @@ -191,35 +220,16 @@ void ReFile::close(){ clear(); m_file.close(); } -#if defined __linux__ || defined WIN32 -void* memichr(void* heap, int cc, size_t length){ - const char* heap2 = reinterpret_cast(heap); - int cc2 = tolower(cc); - void* rc = NULL; - while(length > 0){ - if (cc2 == tolower(*heap2++)){ - rc = (void*)(heap2 - 1); - break; - } - } - return rc; -} -int memicmp(const void* str1, const void* str2, size_t length){ - const char* str12 = reinterpret_cast(str1); - const char* str22 = reinterpret_cast(str2); - int rc = 0; - for (size_t ix = 0; ix < length; ix++){ - int diff = tolower(*str12++) - tolower(*str22++); - if (diff != 0){ - rc = diff; - break; - } - } - return rc; +/** + * Returns the current filename. + * + * @return the filename (with path) + */ +QString ReFile::filename() const +{ + return m_filename; } -#endif /* __linux__ */ - /** * Finds the next line with the string. * @@ -290,6 +300,25 @@ bool ReFile::findLine(const QString& includePattern, bool includeIsRegExpr, } return rc; } + +int ReFile::hasMoreLines(int index){ + bool rc = false; +#if 0 + if (m_countLines >= 0){ + rc = index < m_countLines - 1; + } else{ + seek(m_lastLineNo); + while (index > m_lastLineNo + 1){ + if (! nextLine()){ + break; + } + } + rc = index < m_lastLineNo; + } +#endif + return rc; +} + /** * @brief Gets the line behind the current line. * @@ -464,24 +493,6 @@ void ReFile::rewind(){ m_startOfLine = NULL; } -int ReFile::hasMoreLines(int index){ - bool rc = false; -#if 0 - if (m_countLines >= 0){ - rc = index < m_countLines - 1; - } else{ - seek(m_lastLineNo); - while (index > m_lastLineNo + 1){ - if (! nextLine()){ - break; - } - } - rc = index < m_lastLineNo; - } -#endif - return rc; -} - /** * Sets the internal blocksize. * @@ -493,6 +504,16 @@ void ReFile::setBlocksize(const int64_t& blocksize){ m_maxLineLength = blocksize / 2; } +/** + * Sets the current filename. + * + * @param filename the new filename + */ +void ReFile::setFilename(const QString &filename) +{ + m_filename = filename; +} + /** * @brief Returns the name of a directory in the temp dir. * @@ -609,3 +630,4 @@ void ReFile::writeToFile(const char* filename, const char* content, } } + diff --git a/base/ReFile.hpp b/base/ReFile.hpp index 98b5aab..cba9c8d 100644 --- a/base/ReFile.hpp +++ b/base/ReFile.hpp @@ -57,27 +57,41 @@ public: public: int64_t blocksize() const; void close(); - bool findLine(const char* toFind, bool ignoreCase, int& lineNo, - QString* line); - bool findLine(const QString& includePattern, bool includeIsRegExpr, + /** + * Returns the current line number. + * @return the current line number + */ + inline uint32_t currentLineNo() const{ + return m_currentLineNo; + } + /** Returns the end of line separator. + * @return the end of line separator. Default: windows: "\r\n" linux: "\n" + */ + const QByteArray& endOfLine() const{ + return m_endOfLine; + } + QString filename() const; + bool findLine(const char* toFind, bool ignoreCase, int& lineNo, + QString* line); + bool findLine(const QString& includePattern, bool includeIsRegExpr, bool includeIgnoreCase, const QString& excludePattern, bool excludeIsRegExpr, bool excludeIgnoreCase, int& lineNo, QString* line); virtual int hasMoreLines(int index); - /** - * Returns the current line number. - * @return the current line number - */ - inline uint32_t currentLineNo() const{ - return m_currentLineNo; - } char* nextLine(int& length); char* previousLine(int& length); bool read(const QString& filename = ""); char* remap(int64_t offset, int size, int& length); void rewind(); void setBlocksize(const int64_t& blocksize); - bool write(const QString& filename = ""); + /**Sets the end of line separator. + * @param endOfLine the end of line separator, usually "\n" or "\r\n" + */ + void setEndOfLine(const char* endOfLine){ + m_endOfLine = endOfLine; + } + void setFilename(const QString &filename); + bool write(const QString& filename = ""); public: static QByteArray tempDir(const char* node, const char* parent = NULL, @@ -87,23 +101,11 @@ public: static QByteArray& readFromFile(const char* filename, QByteArray& buffer); static void writeToFile(const char* filename, const char* content, size_t contentLength = (size_t) - 1, const char* mode = "w"); - /** Returns the end of line separator. - * @return the end of line separator. Default: windows: "\r\n" linux: "\n" - */ - const QByteArray& endOfLine() const{ - return m_endOfLine; - } - /**Sets the end of line separator. - * @param endOfLine the end of line separator, usually "\n" or "\r\n" - */ - void setEndOfLine(const char* endOfLine){ - m_endOfLine = endOfLine; - } private: - QByteArray m_endOfLine; - QString m_filename; - QFile m_file; + QByteArray m_endOfLine; + QString m_filename; + QFile m_file; char* m_block; int64_t m_blocksize; int64_t m_blockOffset; -- 2.39.5