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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/*************************************************************************
AsciiDecoder.cpp - decoder for ASCII data
-------------------
begin : Sun Dec 03 2006
copyright : (C) 2006 by Thomas Eschenbacher
email : 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. *
* *
***************************************************************************/
#include "config.h"
#include <ctype.h>
#include <string.h>
#include <new>
#include <QDateTime>
#include <QIODevice>
#include <QLatin1Char>
#include <QLatin1String>
#include <QRegularExpression>
#include <KLocalizedString>
#include "libkwave/Label.h"
#include "libkwave/LabelList.h"
#include "libkwave/MessageBox.h"
#include "libkwave/MultiWriter.h"
#include "libkwave/Parser.h"
#include "libkwave/Sample.h"
#include "libkwave/String.h"
#include "libkwave/Writer.h"
#include "AsciiCodecPlugin.h"
#include "AsciiDecoder.h"
#define MAX_LINE_LEN 16384 /**< maximum line length in characters */
//***************************************************************************
Kwave::AsciiDecoder::AsciiDecoder()
:Kwave::Decoder(),
m_source(),
m_dest(nullptr),
m_queue_input(),
m_line_nr(0)
{
LOAD_MIME_TYPES
REGISTER_COMPRESSION_TYPES
}
//***************************************************************************
Kwave::AsciiDecoder::~AsciiDecoder()
{
if (m_source.device()) close();
}
//***************************************************************************
Kwave::Decoder *Kwave::AsciiDecoder::instance()
{
return new(std::nothrow) Kwave::AsciiDecoder();
}
//***************************************************************************
bool Kwave::AsciiDecoder::open(QWidget *widget, QIODevice &src)
{
Q_UNUSED(widget)
metaData().clear();
Q_ASSERT(!m_source.device());
if (m_source.device()) qWarning("AsciiDecoder::open(), already open !");
// try to open the source
if (!src.open(QIODevice::ReadOnly)) {
qWarning("failed to open source !");
return false;
}
// take over the source
m_source.setDevice(&src);
Kwave::FileInfo info(metaData());
Kwave::LabelList labels;
/********** Decoder setup ************/
qDebug("--- AsciiDecoder::open() ---");
// read in all metadata until start of samples, EOF or user cancel
qDebug("AsciiDecoder::open(...)");
m_line_nr = 0;
while (!m_source.atEnd()) {
QString line = m_source.readLine(MAX_LINE_LEN).simplified();
m_line_nr++;
if (!line.length())
continue; // skip empty line
QRegularExpression regex(QRegularExpression::anchoredPattern(_(
"(^##\\s*)" // 1 start of meta data line
"([\\'\\\"])?" // 2 property, quote start (' or ")
"\\s*(\\w+[\\s\\w]*\\w)\\s*" // 3 property
"(\\[\\d*\\])?" // 4 index (optional)
"(\\2)" // 5 property, quote end
"(\\s*=\\s*)" // 6 assignment '='
"(.*)" // 7 rest, up to end of line
)));
QRegularExpressionMatch regex_match{regex.match(line)};
if (regex_match.hasMatch()) {
// meta data entry: "## 'Name' = value"
QString name = Kwave::Parser::unescape(regex_match.captured(3) +
regex_match.captured(4));
QString v = regex_match.captured(7);
QString value;
if (v.length()) {
// remove quotes from the value
bool is_escaped = false;
char quote = v[0].toLatin1();
if ((quote != '\'') && (quote != '"'))
quote = -1;
for (QString::ConstIterator it = v.constBegin();
it != v.constEnd(); ++it)
{
const char c = QChar(*it).toLatin1();
if ((c == '\\') && !is_escaped) {
is_escaped = true; // next char is escaped
continue;
}
if (is_escaped) {
value += *it; // escaped char
is_escaped = false;
continue;
}
if (c == quote) {
if (!value.length())
continue; // starting quote
else
break; // ending quote
}
if ((quote == -1) && (c == '#'))
break; // comment in unquoted text
// otherwise: normal character, part of text
value += *it;
}
// if the text was unquoted, remove leading/trailing spaces
if (quote == -1)
value = value.trimmed();
}
// handle some well known aliases
if (name == _("rate")) name = info.name(INF_SAMPLE_RATE);
if (name == _("bits")) name = info.name(INF_BITS_PER_SAMPLE);
// handle labels
QRegularExpression regex_label(
QRegularExpression::anchoredPattern(_("label\\[(\\d*)\\]")));
QRegularExpressionMatch regex_label_match{regex_label.match(name)};
if (regex_label_match.hasMatch()) {
bool ok = false;
sample_index_t pos =
regex_label_match.captured(1).toULongLong(&ok);
if (!ok) {
qWarning("line %llu: malformed label position: '%s'",
m_line_nr, DBG(name));
continue; // skip it
}
Kwave::Label label(pos, value);
labels.append(label);
continue;
}
bool found = false;
foreach (const Kwave::FileProperty &p, info.allKnownProperties()) {
if (info.name(p).toLower() == name.toLower()) {
found = true;
info.set(p, QVariant(value));
}
}
if (!found) {
qWarning("line %llu: unknown meta data entry: '%s' = '%s'",
m_line_nr, DBG(name), DBG(value));
}
} else if (line.startsWith(QLatin1Char('#'))) {
continue; // skip comment lines
} else {
// reached end of metadata:
// -> push back the line into the queue
m_queue_input.enqueue(line);
break;
}
}
// if the number of channels is not known, but "tracks" is given and
// "track" is not present: old syntax has been used
if ((info.tracks() < 1) && info.contains(INF_TRACKS) &&
!info.contains(INF_TRACK))
{
info.set(INF_CHANNELS, info.get(INF_TRACKS));
info.set(INF_TRACKS, QVariant());
}
metaData().replace(Kwave::MetaDataList(info));
metaData().add(labels.toMetaDataList());
return (info.tracks() >= 1);
}
//***************************************************************************
bool Kwave::AsciiDecoder::readNextLine()
{
if (!m_queue_input.isEmpty())
return true; // there is still something in the queue
while (!m_source.atEnd()) {
QString line = m_source.readLine(MAX_LINE_LEN).simplified();
m_line_nr++;
if (!line.length()) {
continue; // skip empty line
} else if (line.startsWith(QLatin1Char('#'))) {
continue; // skip comment lines
} else {
// -> push back the line into the queue
m_queue_input.enqueue(line);
return true;
}
}
return false;
}
//***************************************************************************
bool Kwave::AsciiDecoder::decode(QWidget *widget,
Kwave::MultiWriter &dst)
{
Q_UNUSED(widget)
Q_ASSERT(m_source.device());
if (!m_source.device()) return false;
m_dest = &dst;
// for the moment: use a comma as separator <= TODO
const char separators[] = {',', '\0' };
Kwave::FileInfo info(metaData());
unsigned int channels = info.tracks();
QVector<sample_t> frame(channels);
// read in all remaining data until EOF or user cancel
qDebug("AsciiDecoder::decode(...)");
while (readNextLine() && !dst.isCanceled()) {
QByteArray d = m_queue_input.dequeue().toLatin1();
char *line = d.data();
char *saveptr = nullptr;
frame.fill(0);
for (unsigned int channel = 0; channel < channels; channel++) {
sample_t s = 0;
char *token = strtok_r(line, separators, &saveptr);
line = nullptr;
if (token) {
// skip whitespace at the start
while (*token && isspace(*token)) ++token;
if (*token) {
char *p = token + 1;
while (isdigit(*p) || (*p == '+') || (*p == '-')) ++p;
*p = 0;
s = atoi(token);
Kwave::Writer *w = dst[channel];
if (w) (*w) << s;
}
}
}
}
m_dest = nullptr;
info.setLength(dst.last() ? (dst.last() + 1) : 0);
metaData().replace(Kwave::MetaDataList(info));
// return with a valid Signal, even if the user pressed cancel !
return true;
}
//***************************************************************************
void Kwave::AsciiDecoder::close()
{
m_source.reset();
m_source.setDevice(nullptr);
}
//***************************************************************************
//***************************************************************************
|