diff options
| author | Crystalwarrior <varsash@gmail.com> | 2019-09-16 01:16:37 +0300 |
|---|---|---|
| committer | Crystalwarrior <varsash@gmail.com> | 2019-09-16 01:16:37 +0300 |
| commit | bf999f195a0be20519356644df1940cf18d905e7 (patch) | |
| tree | 15c9bc9220ef38147578ceaab0dd164685fdc468 /src/text_file_functions.cpp | |
| parent | 1b36be9dbc0cc665ddb69e1e1ee70267612b5d6c (diff) | |
Add file reading, writing and appending functions that create folders if bool is true
Fix server_address not being properly created in packet distribution
Create a log file when you join a server in the logs/<server name>/<logname>.log and update it every time there's a new chat entry
minor refactor of chatlogpiece
Diffstat (limited to 'src/text_file_functions.cpp')
| -rw-r--r-- | src/text_file_functions.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/text_file_functions.cpp b/src/text_file_functions.cpp index d286c032..ae17ac53 100644 --- a/src/text_file_functions.cpp +++ b/src/text_file_functions.cpp @@ -95,6 +95,74 @@ QStringList AOApplication::get_call_words() return return_value; } +QString AOApplication::read_file(QString filename) +{ + QFile f_log(filename); + + if(!f_log.open(QIODevice::ReadOnly | QIODevice::Text)) + { + qDebug() << "Couldn't open" << filename; + return ""; + } + + QTextStream in(&f_log); + QString text = in.readAll(); + f_log.close(); + return text; +} + +bool AOApplication::write_to_file(QString p_text, QString p_file, bool make_dir) +{ + QString path = QFileInfo(p_file).path(); + if(make_dir) + { + //Create the dir if it doesn't exist yet + QDir dir(path); + if (!dir.exists()) + if (!dir.mkpath(".")) + return false; + } + + QFile f_log(p_file); + if(f_log.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) + { + QTextStream out(&f_log); + + out << p_text; + + f_log.flush(); + f_log.close(); + return true; + } + return false; +} + +bool AOApplication::append_to_file(QString p_text, QString p_file, bool make_dir) +{ + QString path = QFileInfo(p_file).path(); + //Create the dir if it doesn't exist yet + if(make_dir) + { + QDir dir(path); + if (!dir.exists()) + if (!dir.mkpath(".")) + return false; + } + + QFile f_log(p_file); + if(f_log.open(QIODevice::WriteOnly | QIODevice::Append)) + { + QTextStream out(&f_log); + + out << "\r\n" << p_text; + + f_log.flush(); + f_log.close(); + return true; + } + return false; +} + void AOApplication::write_to_serverlist_txt(QString p_line) { QFile serverlist_txt; |
