1 /*
2    Drawpile - a collaborative drawing program.
3 
4    Copyright (C) 2017-2019 Calle Laakkonen
5 
6    Drawpile 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 3 of the License, or
9    (at your option) any later version.
10 
11    Drawpile 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 Drawpile.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef DP_NET_TEXTMODE_H
20 #define DP_NET_TEXTMODE_H
21 
22 #include "message.h"
23 
24 namespace protocol {
25 namespace text {
26 
27 /**
28  * Text mode file parser
29  */
30 class Parser {
31 public:
32 	struct Result {
33 		enum {
34 			Ok,
35 			Skip,
36 			Error,
37 			NeedMore
38 		} status;
39 		NullableMessageRef msg;
40 	};
41 
42 	Result parseLine(const QString &line);
errorString()43 	QString errorString() const { return m_error; }
44 
metadata()45 	Kwargs metadata() const { return m_metadata; }
46 
Parser()47 	Parser() : m_state(ExpectCommand), m_ctx(0) { }
48 
49 private:
50 	enum {
51 		ExpectCommand,
52 		ExpectKwargLine,
53 		ExpectDab,
54 	} m_state;
55 
56 	Kwargs m_metadata;
57 	QString m_error;
58 	QString m_cmd;
59 	Kwargs m_kwargs;
60 	QStringList m_dabs;
61 	int m_ctx;
62 };
63 
64 // Formatting helper functions
idString(uint16_t id)65 inline QString idString(uint16_t id) { return QStringLiteral("0x%1").arg(id, 4, 16, QLatin1Char('0')); }
66 QString idListString(const QList<uint8_t> ids);
67 QString idListString(const QList<uint16_t> ids);
68 QString rgbString(quint32 color);
69 QString argbString(quint32 color);
decimal(uint8_t value)70 inline QString decimal(uint8_t value) { return QString::number(value/255.0*100.0, 'f', 2); }
71 
72 // Parsing helper functions
73 uint16_t parseIdString16(const QString &id, bool *ok=nullptr);
74 QList<uint8_t> parseIdListString8(const QString &ids);
75 QList<uint16_t> parseIdListString16(const QString &ids);
76 quint32 parseColor(const QString &color);
parseDecimal8(const QString & str)77 inline uint8_t parseDecimal8(const QString &str) { return qBound(0, qRound(str.toFloat() / 100.0 * 255), 255); }
78 
79 }
80 }
81 
82 #endif
83 
84