From 978f6c1cc18d0cf71f387e19c1f05136a9ee2330 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Sun, 11 Feb 2024 23:00:56 +0300 Subject: [PATCH] init --- .gitignore | 18 +++++ .vscode/launch.json | 17 +++++ data/database.db | Bin 0 -> 73728 bytes data/sample.test | 26 ++++++++ database.db | Bin 0 -> 73728 bytes dub.json | 31 +++++++++ dub.selections.json | 10 +++ dub.settings.json | 4 ++ sample.test | 24 +++++++ source/app.d | 155 ++++++++++++++++++++++++++++++++++++++++++++ source/database.d | 14 ++++ source/dblite.d | 38 +++++++++++ 12 files changed, 337 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 data/database.db create mode 100644 data/sample.test create mode 100644 database.db create mode 100644 dub.json create mode 100644 dub.selections.json create mode 100644 dub.settings.json create mode 100644 sample.test create mode 100644 source/app.d create mode 100644 source/database.d create mode 100644 source/dblite.d diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..409d7bc --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +.dub +docs.json +__dummy.html +bin/ +docs/ +/shkolaspo +shkolaspo.log +shkolaspo.so +shkolaspo.dylib +shkolaspo.dll +shkolaspo.a +shkolaspo.lib +shkolaspo-test-* +*.exe +*.pdb +*.o +*.obj +*.lst diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..1e8f990 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Используйте IntelliSense, чтобы узнать о возможных атрибутах. + // Наведите указатель мыши, чтобы просмотреть описания существующих атрибутов. + // Для получения дополнительной информации посетите: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "code-d", + "request": "launch", + "dubBuild": true, + "name": "Build & Debug DUB project", + "cwd": "${command:dubWorkingDirectory}", + "program": "bin/${command:dubTarget}", + "args": ["./data/database.db", "./data/sample.test"] + } + ] +} diff --git a/data/database.db b/data/database.db new file mode 100644 index 0000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tg_000IagfB*srAb0.5.0", + "readconf": "~>0.4.1", + "arsd-official:sqlite": "~>11.4.0" + }, + "buildTypes": { + "debug": { + "buildOptions": [ + "debugMode", + "debugInfo" + ] + }, + "release": { + "buildOptions": [ + "releaseMode", + "inline", + "optimize" + ] + } + }, + "targetPath": "bin", + "targetType": "executable" +} diff --git a/dub.selections.json b/dub.selections.json new file mode 100644 index 0000000..5169ada --- /dev/null +++ b/dub.selections.json @@ -0,0 +1,10 @@ +{ + "fileVersion": 1, + "versions": { + "arsd-official": "11.4.0", + "datefmt": "1.0.4", + "readconf": "0.4.1", + "silly": "1.2.0-dev.2", + "singlog": "0.5.0" + } +} diff --git a/dub.settings.json b/dub.settings.json new file mode 100644 index 0000000..9da886c --- /dev/null +++ b/dub.settings.json @@ -0,0 +1,4 @@ +{ + "defaultArchitecture": "x86_64", + "defaultCompiler": "ldc2" +} diff --git a/sample.test b/sample.test new file mode 100644 index 0000000..6cb1e38 --- /dev/null +++ b/sample.test @@ -0,0 +1,24 @@ +# Двоичная система. Двоичная арифметика +? Двоичная системой счисления является +- Унарная система счисления +- Непозиционная система счисления +- Позиционная система счисления +? Для записи в двоичной системе счисления используют только две цифры. Какие? +- 2 и 3 +- 2 и 10 +- 0 и 1 +? Переведите число 1010100 из двоичной системы в десятичную +- 84 +- 83 +- 85 ++ 86 +? Найдите сумму двух чисел в двоичной системе счисления 11001 + 11100 = +- 110111 +- 0111101 ++ 111011 +- 110101 +? Вычислите 11001 - 1111 = +- 1010 +- 1110 +- 1011 +- 0110 diff --git a/source/app.d b/source/app.d new file mode 100644 index 0000000..df3e6ff --- /dev/null +++ b/source/app.d @@ -0,0 +1,155 @@ +import singlog; + +import core.stdc.stdlib : exit, EXIT_SUCCESS, EXIT_FAILURE; +import std.stdio, std.conv, std.path, std.file, std.format, std.regex, std.array, std.algorithm; + +import source.database; + +enum { + GROUP_THEME = 2, + GROUP_THEME_TEXT = 5, + GROUP_QUESTION = 7, + GROUP_QUESTION_TEXT = 10, + GROUP_ANSWER = 12, + GROUP_ANSWER_TEXT = 15, + GROUP_ANSWER_RIGHT = 17, + GROUP_ANSWER_RIGHT_TEXT = 20, +} + +int main(string[] args) +{ + log.level(log.level.debugging) + .output(log.output.stdout.stderr) + .color(true); + + string testFilePath; + string databaseFile; + + if (args.length != 3) { + log.e("Не было передано необходимое количество параметров для запуска программы"); + return EXIT_FAILURE; + } + + databaseFile = args[1]; + testFilePath = args[2]; + + const string pattern = "^((( |\\t)*\\#( |\\t)*)(.*[^\\s]))|((( |\\t)*\\?( |\\t)*)(.*[^\\s]))" + ~ "|((( |\\t)*-( |\\t)*)(.*[^\\s]))|((( |\\t)*\\+( |\\t)*)(.*[^\\s]))$"; + + File testFile; + + try { + testFile = File(testFilePath, "r"); + } catch (Exception e) { + log.w("Unable to open the test file " ~ testFilePath); + log.e(e); + return EXIT_FAILURE; + } + + dbliteConnect(databaseFile); + + auto regular = regex(pattern, "m"); + + struct Answer { + string text; + bool right; + + // ToDo + bool addToDB() { + return false; + } + } + + struct Question { + string text; + Answer[int] answers; + + ulong getCount() { + return answers.length; + } + + bool isValid() { + return answers.length > 1; + } + // ToDo + bool addToDB() { + return false; + } + } + + struct Theme { + string text; + Question[int] questions; + + ulong getCount() { + return questions.length; + } + + bool isValid() { + return questions.length > 1; + } + + // ToDo + bool addToDB() { + return false; + } + + void print() { + writeln("Количество вопросов: %s".format(this.getCount())); + + foreach (question; this.questions.byKeyValue.array.sort!((a, b) => a.key < b.key)) { + writeln("\tВопрос №%d: %s".format(question.key, question.value.text)); + writeln("\tКоличество ответов: %d".format(question.value.getCount())); + if (question.value.isValid()) + foreach (answer; question.value.answers.byKeyValue.array.sort!((a, b) => a.key < b.key)) { + writeln("\t\tОтвет №%d: %s".format(answer.key, answer.value.text)); + } + else + writeln("\t\tНедостаточно количества ответов"); + } + } + } + + Theme theme; + + int numQuestion; + int numAnswer; + + while (!testFile.eof()) + { + string line = testFile.readln(); + auto match = matchFirst(line, regular); + + if (match.length == 0) + continue; + + if (match[GROUP_THEME].length) + { + theme.text = match[GROUP_THEME_TEXT]; + continue; + } + + if (match[GROUP_QUESTION].length) + { + numAnswer = 0; + theme.questions[++numQuestion] = Question(match[GROUP_QUESTION_TEXT]); + continue; + } + + if (match[GROUP_ANSWER].length) + { + theme.questions[numQuestion].answers[++numAnswer] = Answer(match[GROUP_ANSWER_TEXT], false); + continue; + } + + if (match[GROUP_ANSWER_RIGHT].length) + { + theme.questions[numQuestion].answers[++numAnswer] = Answer(match[GROUP_ANSWER_RIGHT_TEXT], true); + continue; + } + } + + theme.print(); + + return EXIT_SUCCESS; +} diff --git a/source/database.d b/source/database.d new file mode 100644 index 0000000..ff891d2 --- /dev/null +++ b/source/database.d @@ -0,0 +1,14 @@ +module source.database; + +import singlog; +import core.stdc.stdlib : exit; +import source.dblite; + +void dbliteConnect(string path) { + try { + dblite(path); + } catch (Exception e) { + log.c(e.msg); + exit(1); + } +} diff --git a/source/dblite.d b/source/dblite.d new file mode 100644 index 0000000..ddd7bd0 --- /dev/null +++ b/source/dblite.d @@ -0,0 +1,38 @@ +module source.dblite; + +import arsd.sqlite; +import std.stdio; + +alias dblite = DBLite.getConnection; + +class DBLite : Sqlite { +private: + static DBLite _dblite; + this(string database) { + super(database); + } +public: + @property static DBLite getConnection(string database = null) { + if (this._dblite is null) { + try { + this._dblite = new DBLite(database); + } catch (Exception e) { + throw new Exception(e.msg); + } + } + + return this._dblite; + } + + SqliteResult sql(T...)(string query, T t) { + return cast(SqliteResult)this.query(query, t); + } + + void commit() { + this.query("COMMIT"); + } + + void rollback() { + this.query("ROLLBACK"); + } +}