• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • kasten
  • controllers
  • view
  • structures
  • parsers
parserutils.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Okteta Kasten Framework, made within the KDE community.
3  *
4  * Copyright 2012 Alex Richardson <alex.richardson@gmx.de>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) version 3, or any
10  * later version accepted by the membership of KDE e.V. (or its
11  * successor approved by the membership of KDE e.V.), which shall
12  * act as a proxy defined in Section 6 of version 3 of the license.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21  */
22 #include "parserutils.h"
23 
24 #include <QScriptValue>
25 #include <QScriptEngine>
26 
27 ParsedNumber<int> ParserUtils::intFromString(const QString& str)
28 {
29  int value = 0;
30  bool okay = false;
31  if (str.startsWith(QLatin1String("0x")))
32  value = str.mid(2).toInt(&okay, 16);
33  else if (str.startsWith(QLatin1String("-0x")))
34  {
35  //special case for minimum possible value
36  if (str == QLatin1String("-0x80000000"))
37  return ParsedNumber<int>(-0x80000000, str, true);
38  value = -str.mid(3).toInt(&okay, 16);
39  }
40  else
41  value = str.toInt(&okay, 10);
42  return ParsedNumber<int>(value, str, okay);
43 }
44 
45 ParsedNumber<uint> ParserUtils::uintFromString(const QString& str)
46 {
47  uint value = 0;
48  bool okay;
49  if (str.startsWith(QLatin1String("0x")))
50  value = str.mid(2).toUInt(&okay, 16);
51  else
52  value = str.toUInt(&okay, 10);
53  return ParsedNumber<uint>(value, str, okay);
54 }
55 
56 ParsedNumber<quint64> ParserUtils::uint64FromString(const QString& str)
57 {
58  quint64 value = 0;
59  bool okay;
60  if (str.startsWith(QLatin1String("0x")))
61  value = str.mid(2).toULongLong(&okay, 16);
62  else
63  value = str.toULongLong(&okay, 10);
64  return ParsedNumber<quint64>(value, str, okay);
65 }
66 
67 DataInformation::DataInformationEndianess ParserUtils::byteOrderFromString(const QString& string,
68  const LoggerWithContext& logger)
69 {
70  const QString lower = string.toLower();
71  if (lower == QLatin1String("bigendian") || lower == QLatin1String("big-endian"))
72  return DataInformation::EndianessBig;
73  else if (lower == QLatin1String("littleendian") || lower == QLatin1String("little-endian"))
74  return DataInformation::EndianessLittle;
75  else if (lower == QLatin1String("fromsettings") || lower == QLatin1String("from-settings"))
76  return DataInformation::EndianessFromSettings;
77  else if (lower == QLatin1String("inherit"))
78  return DataInformation::EndianessInherit;
79  else
80  {
81  logger.warn().nospace() << "Unrecognized byte order '" << string << "', defaulting to 'inherit'";
82  return DataInformation::EndianessInherit;
83  }
84 }
85 
86 ParsedNumber<int> ParserUtils::intFromScriptValue(const QScriptValue& val)
87 {
88  if (val.isNumber())
89  {
90  //check whether it is in range
91  const qsreal doubleVal = val.toNumber();
92  const int value = val.toInt32();
93  if (doubleVal != qsreal(value))
94  return ParsedNumber<int>::badInput(val.toString());
95  return ParsedNumber<int>(value, val.toString(), true);
96  }
97  else if (val.isString())
98  return intFromString(val.toString());
99  else
100  return ParsedNumber<int>::badInput(val.toString());
101 }
102 
103 ParsedNumber<uint> ParserUtils::uintFromScriptValue(const QScriptValue& val)
104 {
105  if (val.isNumber())
106  {
107  //check whether it is in range
108  const uint value = val.toUInt32();
109  const qsreal doubleVal = val.toNumber();
110  if (doubleVal != qsreal(value))
111  return ParsedNumber<uint>::badInput(val.toString());
112  return ParsedNumber<uint>(value, val.toString(), true);
113  }
114  else if (val.isString())
115  return uintFromString(val.toString());
116  else
117  return ParsedNumber<uint>::badInput(val.toString());
118 }
119 
120 ParsedNumber<quint64> ParserUtils::uint64FromScriptValue(const QScriptValue& val)
121 {
122  if (val.isNumber())
123  {
124  //check whether it is in range
125  const uint value = val.toUInt32();
126  const qsreal doubleVal = val.toNumber();
127  if (doubleVal != qsreal(value))
128  return ParsedNumber<quint64>::badInput(val.toString());
129  return ParsedNumber<quint64>(value, val.toString(), true);
130  }
131  else if (val.isString())
132  return uint64FromString(val.toString());
133  else
134  return ParsedNumber<quint64>::badInput(val.toString());
135 }
136 
137 QString ParserUtils::byteOrderToString(DataInformation::DataInformationEndianess order)
138 {
139  if (order == DataInformation::EndianessLittle)
140  return QLatin1String("littleEndian");
141  if (order == DataInformation::EndianessBig)
142  return QLatin1String("bigEndian");
143  if (order == DataInformation::EndianessFromSettings)
144  return QLatin1String("fromSettings");
145  return QLatin1String("inherit");
146 }
147 
148 StringDataInformation::StringType ParserUtils::toStringEncoding(const QString& str, const LoggerWithContext& logger)
149 {
150  QString enc = str.toLower();
151  if (enc == QLatin1String("ascii"))
152  return StringDataInformation::ASCII;
153  else if (enc == QLatin1String("latin1") || enc == QLatin1String("latin-1"))
154  return StringDataInformation::Latin1;
155  else if (enc.startsWith(QLatin1String("utf")))
156  {
157  QStringRef ref = enc.midRef(3);
158  if (ref.at(0) == QLatin1Char('-'))
159  ref = enc.midRef(4); //strip '-'
160  if (ref == QLatin1String("8"))
161  return StringDataInformation::UTF8;
162 
163  if (ref == QLatin1String("16") || ref == QLatin1String("16le") || ref == QLatin1String("16-le"))
164  {
165  return StringDataInformation::UTF16_LE;
166  }
167  if (ref == QLatin1String("16be") || ref == QLatin1String("16-be"))
168  {
169  return StringDataInformation::UTF16_BE;
170  }
171  if (ref == QLatin1String("32") || ref == QLatin1String("32le") || ref == QLatin1String("32-le"))
172  {
173  return StringDataInformation::UTF32_LE;
174  }
175  if (ref == QLatin1String("32be") || ref == QLatin1String("32-be"))
176  {
177  return StringDataInformation::UTF32_BE;
178  }
179  }
180  logger.warn() << "Unrecognized string encoding: " << enc;
181  return StringDataInformation::InvalidEncoding;
182 }
183 
184 QScriptValue ParserUtils::functionSafeEval(QScriptEngine* engine, const QString& str)
185 {
186  if (str.isEmpty())
187  return QScriptValue();
188  //must wrap in parentheses, see https://bugreports.qt-project.org/browse/QTBUG-5757
189  QScriptValue ret = engine->evaluate(QLatin1Char('(') + str + QLatin1Char(')'));
190  if (!ret.isFunction())
191  return QScriptValue(str);
192  return ret;
193 }
StringDataInformation::UTF8
Definition: stringdatainformation.h:44
LoggerWithContext
Definition: scriptlogger.h:94
StringDataInformation::UTF16_BE
Definition: stringdatainformation.h:44
LoggerWithContext::warn
QDebug warn() const
Definition: scriptlogger.h:98
ParserUtils::intFromScriptValue
ParsedNumber< int > intFromScriptValue(const QScriptValue &val)
Checks whether the value is a number, and if it is converts it.
Definition: parserutils.cpp:86
ParserUtils::functionSafeEval
QScriptValue functionSafeEval(QScriptEngine *engine, const QString &str)
This essentially calls engine->evaluate(str), but ensures it can be a function (QTBUG-5757) ...
Definition: parserutils.cpp:184
ParsedNumber< int >
DataInformation::DataInformationEndianess
DataInformationEndianess
Definition: datainformation.h:86
StringDataInformation::Latin1
Definition: stringdatainformation.h:44
StringDataInformation::ASCII
Definition: stringdatainformation.h:44
ParserUtils::intFromString
ParsedNumber< int > intFromString(const QString &str)
If string starts with 0x, the remainder is interpreted as a hexadecimal (unsigned) number otherwise i...
Definition: parserutils.cpp:27
parserutils.h
DataInformation::EndianessBig
Definition: datainformation.h:88
ParsedNumber::badInput
static ParsedNumber< T > badInput(const QString &str)
Definition: parserutils.h:90
ParserUtils::uintFromScriptValue
ParsedNumber< uint > uintFromScriptValue(const QScriptValue &val)
Definition: parserutils.cpp:103
StringDataInformation::InvalidEncoding
Definition: stringdatainformation.h:44
ParserUtils::toStringEncoding
StringDataInformation::StringType toStringEncoding(const QString &str, const LoggerWithContext &logger)
Definition: parserutils.cpp:148
StringDataInformation::StringType
StringType
Definition: stringdatainformation.h:43
DataInformation::EndianessFromSettings
Definition: datainformation.h:88
ParserUtils::byteOrderToString
QString byteOrderToString(DataInformation::DataInformationEndianess order)
Definition: parserutils.cpp:137
ParserUtils::uint64FromString
ParsedNumber< quint64 > uint64FromString(const QString &str)
Definition: parserutils.cpp:56
ParserUtils::uintFromString
ParsedNumber< uint > uintFromString(const QString &str)
Definition: parserutils.cpp:45
DataInformation::EndianessLittle
Definition: datainformation.h:88
StringDataInformation::UTF32_LE
Definition: stringdatainformation.h:44
ParserUtils::byteOrderFromString
DataInformation::DataInformationEndianess byteOrderFromString(const QString &string, const LoggerWithContext &logger)
Definition: parserutils.cpp:67
StringDataInformation::UTF32_BE
Definition: stringdatainformation.h:44
ParserUtils::uint64FromScriptValue
ParsedNumber< quint64 > uint64FromScriptValue(const QScriptValue &val)
Definition: parserutils.cpp:120
DataInformation::EndianessInherit
Definition: datainformation.h:88
StringDataInformation::UTF16_LE
Definition: stringdatainformation.h:44
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal