|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Nick Egorrov, nicegorov@yandex.ru |
| 3 | + * |
| 4 | + * This file is part of GCodeWorkShop. |
| 5 | + * |
| 6 | + * GCodeWorkShop is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 2 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <vector> // for vector |
| 21 | + |
| 22 | +#include <QByteArray> // for QByteArray |
| 23 | +#include <QSettings> // for QSettings |
| 24 | +#include <QString> // for QString |
| 25 | +#include <QVariant> // for QVariant |
| 26 | +#include <QTextCodec> // for QTextCodec |
| 27 | + |
| 28 | +#include <utils/gcode-converter.h> // IWYU pragma: associated |
| 29 | + |
| 30 | + |
| 31 | +namespace GCode { |
| 32 | +namespace Private { |
| 33 | + |
| 34 | +// see https://en.wikipedia.org/wiki/Euclidean_algorithm |
| 35 | +int greatest_common_divisor(int a, int b) |
| 36 | +{ |
| 37 | + while (a != b) { |
| 38 | + if (a > b) { |
| 39 | + a -= b; |
| 40 | + } else { |
| 41 | + b -= a; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return a; |
| 46 | +} |
| 47 | + |
| 48 | +int greatest_common_divisor(const std::vector<int>& values) |
| 49 | +{ |
| 50 | + if (values.empty()) { |
| 51 | + return 1; |
| 52 | + } |
| 53 | + |
| 54 | + int result = values.front(); |
| 55 | + |
| 56 | + for (int a : values) { |
| 57 | + result = greatest_common_divisor(a, result); |
| 58 | + } |
| 59 | + |
| 60 | + return result; |
| 61 | +} |
| 62 | + |
| 63 | +void add_unique(std::vector<int>& values, int v) |
| 64 | +{ |
| 65 | + for (int i : values) { |
| 66 | + if (i == v) { |
| 67 | + return; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + values.push_back(v); |
| 72 | +} |
| 73 | + |
| 74 | +QByteArray filterData(const QByteArray& data, |
| 75 | + bool dropEmptyLine, |
| 76 | + bool dropExtented, |
| 77 | + bool dropControll) |
| 78 | +{ |
| 79 | + int newLineCount = 0; |
| 80 | + int mergedLine = 1; |
| 81 | + |
| 82 | + if (dropEmptyLine) { |
| 83 | + std::vector<int> values; |
| 84 | + newLineCount = 0; |
| 85 | + |
| 86 | + for (char c : data) { |
| 87 | + if (c == '\n' || c == '\r') { |
| 88 | + ++newLineCount; |
| 89 | + } else { |
| 90 | + if (newLineCount != 0) { |
| 91 | + add_unique(values, newLineCount); |
| 92 | + newLineCount = 0; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + mergedLine = greatest_common_divisor(values); |
| 98 | + } |
| 99 | + |
| 100 | + QByteArray handled; |
| 101 | + newLineCount = 0; |
| 102 | + |
| 103 | + for (char c : data) { |
| 104 | + if (c == '\n' || c == '\r') { |
| 105 | + if (++newLineCount > mergedLine) { |
| 106 | + newLineCount = 1; |
| 107 | + } |
| 108 | + |
| 109 | + if (newLineCount > 1) { |
| 110 | + continue; |
| 111 | + } |
| 112 | + } else if (c < 0) { |
| 113 | + newLineCount = 0; |
| 114 | + |
| 115 | + if (dropExtented) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + } else if (c < 0x20) { |
| 119 | + newLineCount = 0; |
| 120 | + |
| 121 | + if (dropControll) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + } else { |
| 125 | + newLineCount = 0; |
| 126 | + } |
| 127 | + |
| 128 | + handled.push_back(c); |
| 129 | + } |
| 130 | + |
| 131 | + return handled; |
| 132 | +} |
| 133 | + |
| 134 | +QString fromRawData(const QByteArray& data, |
| 135 | + bool dropEmptyLine, |
| 136 | + bool dropExtented, |
| 137 | + bool dropControll, |
| 138 | + const QByteArray& codec) |
| 139 | +{ |
| 140 | + const QByteArray& handled = filterData(data, |
| 141 | + dropEmptyLine, |
| 142 | + dropExtented, |
| 143 | + dropControll); |
| 144 | + QTextCodec* textCodec = QTextCodec::codecForName(codec); |
| 145 | + |
| 146 | + if (textCodec != nullptr) { |
| 147 | + return textCodec->toUnicode(handled); |
| 148 | + } else { |
| 149 | + return QString::fromLocal8Bit(handled); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +QByteArray toRawData(const QString& text, const QByteArray& codec) |
| 154 | +{ |
| 155 | + QTextCodec* textCodec = QTextCodec::codecForName(codec); |
| 156 | + |
| 157 | + if (textCodec != nullptr) { |
| 158 | + return textCodec->fromUnicode(text); |
| 159 | + } else { |
| 160 | + return text.toLocal8Bit(); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +Converter::Options s_defaultOptions{}; |
| 165 | + |
| 166 | +} // namespase Private |
| 167 | +} // namespase GCode |
| 168 | + |
| 169 | + |
| 170 | +#define CFG_KEY_CODEC_NAME "CodecName" |
| 171 | +#define CFG_KEY_DROP_CONTROLL "DropControll" |
| 172 | +#define CFG_KEY_DROP_EMPTY_LINE "DropEmptyLine" |
| 173 | +#define CFG_KEY_DROP_EXTENDED "DropExtented" |
| 174 | + |
| 175 | +void GCode::Converter::Options::load(QSettings* settings) |
| 176 | +{ |
| 177 | + codecName = settings->value(CFG_KEY_CODEC_NAME, codecName).toByteArray(); |
| 178 | + dropControll = settings->value(CFG_KEY_DROP_CONTROLL, dropControll).toBool(); |
| 179 | + dropEmptyLine = settings->value(CFG_KEY_DROP_EMPTY_LINE, dropEmptyLine).toBool(); |
| 180 | + dropExtented = settings->value(CFG_KEY_DROP_EXTENDED, dropExtented).toBool(); |
| 181 | +} |
| 182 | + |
| 183 | +void GCode::Converter::Options::save(QSettings* settings) |
| 184 | +{ |
| 185 | + settings->setValue(CFG_KEY_CODEC_NAME, codecName); |
| 186 | + settings->setValue(CFG_KEY_DROP_CONTROLL, dropControll); |
| 187 | + settings->setValue(CFG_KEY_DROP_EMPTY_LINE, dropEmptyLine); |
| 188 | + settings->setValue(CFG_KEY_DROP_EXTENDED, dropExtented); |
| 189 | +} |
| 190 | + |
| 191 | +GCode::Converter::Options GCode::Converter::defaultOptions() |
| 192 | +{ |
| 193 | + return Private::s_defaultOptions; |
| 194 | +} |
| 195 | + |
| 196 | +void GCode::Converter::setDefaultOptions(const Options& options) |
| 197 | +{ |
| 198 | + Private::s_defaultOptions = options; |
| 199 | +} |
| 200 | + |
| 201 | +GCode::Converter::Converter() : |
| 202 | + options{defaultOptions()} |
| 203 | +{ |
| 204 | +} |
| 205 | + |
| 206 | +QString GCode::Converter::fromRawData(const QByteArray& data) const |
| 207 | +{ |
| 208 | + return Private::fromRawData(data, |
| 209 | + options.dropEmptyLine, |
| 210 | + options.dropExtented, |
| 211 | + options.dropControll, |
| 212 | + options.codecName); |
| 213 | +} |
| 214 | + |
| 215 | +QByteArray GCode::Converter::toRawData(const QString& text) const |
| 216 | +{ |
| 217 | + return Private::toRawData(text, options.codecName); |
| 218 | +} |
0 commit comments