File: parameters.cpp

package info (click to toggle)
audex 0.78-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,844 kB
  • ctags: 1,461
  • sloc: cpp: 10,604; makefile: 7; sh: 3
file content (106 lines) | stat: -rw-r--r-- 2,705 bytes parent folder | download | duplicates (2)
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
/* AUDEX CDDA EXTRACTOR
 * Copyright (C) 2007-2014 Marco Nelles (audex@maniatek.com)
 * <http://kde.maniatek.com/audex>
 *
 * 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "parameters.h"

Parameters::Parameters() {

}

Parameters::Parameters(const Parameters& other) {
  p_parameters = other.p_parameters;
}

Parameters& Parameters::operator=(const Parameters& other) {
  p_parameters = other.p_parameters;
  return *this;
}

Parameters::~Parameters() {

}

void Parameters::fromString(const QString& string, const QChar& sep) {

  if (string.isEmpty()) return;

  p_parameters.clear();

  QString option;
  bool is_in_quote = FALSE;
  for (int i = 0; i < string.length(); ++i) {
    if (string[i]=='\'') {
      is_in_quote = !is_in_quote;
    } else if ((string[i]==sep) && (!is_in_quote)) {
      if (option.isEmpty()) continue;
      int equalPos = option.indexOf('=');
      if (equalPos==-1) {
        p_parameters[option] = "";
      } else {
        QString ropt = option.left(equalPos);
        QString val = option.mid(equalPos+1);
        p_parameters[ropt] = val.trimmed();
      }
      option.clear();
    } else {
      option += string[i];
    }
  }

  int equalPos = option.indexOf('=');
  if (equalPos==-1) {
    p_parameters[option] = "";
  } else {
    QString ropt = option.left(equalPos);
    QString val = option.mid(equalPos+1);
    p_parameters[ropt] = val.trimmed();
  }

}

const QString Parameters::toString(const QChar& sep) {

  QString string;

  QHash<QString,QString>::const_iterator i = p_parameters.constBegin();
  bool first = TRUE;
  while (i != p_parameters.constEnd()) {
    QString s = i.value();
    s.replace("'", "\\'");
    if (!first) {
      string += sep+i.key()+"='"+s+"'";
    } else {
      string += i.key()+"='"+s+"'";
      first = FALSE;
    }
    ++i;
  }

  return string;

}

void Parameters::fromBase64(const QByteArray& bytearray) {
  QByteArray ba = QByteArray::fromBase64(bytearray);
  fromString(QString::fromUtf8(ba.data()));
}

const QByteArray Parameters::toBase64() {
  QString s = toString();
  return s.toUtf8().toBase64();
}