File: Parser.h

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (205 lines) | stat: -rw-r--r-- 6,822 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/***************************************************************************
                Parser.h -  parser for Kwave's internal commands
                             -------------------
    begin                : Sat Feb  3 2001
    copyright            : (C) 2001 by Thomas Eschenbacher
    email                : Thomas Eschenbacher <thomas.eschenbacher@gmx.de>

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef PARSER_H
#define PARSER_H

#include "config.h"
#include "libkwave_export.h"

#include <QtGlobal>
#include <QString>
#include <QStringList>
#include <QUrl>

#include "libkwave/Sample.h" // for sample_index_t

//*****************************************************************************
namespace Kwave
{
    class LIBKWAVE_EXPORT Parser
    {
    public:
        /**
         * Constructor. Parses the given string into an optional
         * command part and a list of parameters.
         */
        Parser(const QString &init);

        /** Destructor. */
        virtual ~Parser();

        /** Returns the command part of the line */
        inline QString command() {
            return m_command;
        }

        /** Returns the complete list of parameters */
        inline const QStringList &params() {
            return m_param;
        }

        /** Returns true if the parameter list is not empty. */
        inline bool hasParams() {
            return (m_param.count() != 0);
        }

        /** Returns true if a list of commands was parsed */
        inline bool hasMultipleCommands() {
            return (m_commands.count() > 1);
        }

        /** Returns the list of commands */
        inline QStringList commandList() { return m_commands; }

        /** Returns true if the end of the parameter list has been reached. */
        inline bool isDone () const {
            return (static_cast<int>(m_current) >= m_param.count());
        }

        /** Returns the number of parameters. */
        inline unsigned int count() const {
            return static_cast<unsigned int>(m_param.count());
        }

        /**
         * Returns the first parameter and sets the current
         * position to the next.
         */
        const QString &firstParam();

        /**
         * Returns the next parameter and increases the current
         * position if not already at the end of the parameter
         * list. If the end of the parameter list has been
         * reached, the return value will be a zero-length string.
         */
        const QString &nextParam();

        /** Returns all remaining parameters in a string list */
        QStringList remainingParams();

        /**
         * Skips a parameter and advances the current position
         * by one if the end has not already been reached.
         */
        void skipParam();

        /**
         * Gets the next parameter through calling nextParam() and
         * interpretes it as a "bool" value. It will recognize the
         * strings "true" and "false" (not case-sensitive) and
         * numeric values (true means not zero). On errors the
         * return value will be false.
         */
        bool toBool();

        /**
         * Gets the next parameter through calling nextParam() and
         * interpretes it as an "int" value. On errors the
         * return value will be zero.
         */
        int toInt();

        /**
         * Gets the next parameter through calling nextParam() and
         * interpretes it as an "unsigned int" value. On errors the
         * return value will be zero.
         */
        unsigned int toUInt();

        /**
         * Gets the next parameter through calling nextParam() and
         * interpretes it as a "sample_index_t" value. On errors the
         * return value will be zero.
         */
        sample_index_t toSampleIndex();

        /**
         * Gets the next parameter through calling nextParam() and
         * interpretes it as a "double" value. On errors the
         * return value will be zero.
         */
        double toDouble();

        /**
         * Escapes all characters that might be critical when parsing
         *
         * @param text a unicode text to escape
         * @return text with certain characters escaped
         */
        static QString escape(const QString &text);

        /**
         * Escape all parts of a string that might be dangerous for using
         * within a file name, like "/" and similar.
         *
         * @param text a unicode text to escape
         * @return escaped string, which should be safe to use in a file name
         */
        static QString escapeForFileName(const QString &text);

        /**
         * Un-escapes all characters in a string previously escaped with
         * escape()
         *
         * @param text a unicode text to un-escape
         * @return the original text without escaped characters
         */
        static QString unescape(const QString &text);

        /**
         * Converts a string command into a URL suitable for passing to
         * Kwave as command line parameter
         * @param command the string command to encode as URL
         * @return a QUrl with the kwave:// scheme
         */
        static QUrl toUrl(const QString &command);

        /**
         * Converts a kwave:// URL into a string command
         * @param url a URL using the kwave:// scheme
         * @return a Kwave string command parsed from the URL
         */
        static QString fromUrl(const QUrl &url);

    protected:

        /** Splits a line into a list of commands */
        QStringList splitCommands(QString &line);

    private:
        /** the command part of the line */
        QString m_command;

        /** list of parsed parameters */
        QStringList m_param;

        /** number of the "current" parameter */
        unsigned int m_current;

        /** if it has multiple commands, the list of command strings */
        QStringList m_commands;

    };
}

#endif /* PARSER_H */

//***************************************************************************
//***************************************************************************