LMMS
Loading...
Searching...
No Matches
LocaleHelper.h
Go to the documentation of this file.
1/*
2 * LocaleHelper.h - compatibility functions for handling decimal separators
3 * Providing helper functions which handle both periods and commas
4 * for decimal separators to load old projects correctly
5 *
6 * Copyright (c) 2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
7 * Copyright (c) 2018 Hyunjin Song <tteu.ingog/at/gmail.com>
8 *
9 * This file is part of LMMS - https://lmms.io
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public
22 * License along with this program (see COPYING); if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301 USA.
25 *
26 */
27
28// NOTE: The LMMS/zynaddsubfx repo contains a copy of this header.
29// If you modify this file, consider modifying it there as well.
30
31#ifndef LMMS_LOCALEHELPER_H
32#define LMMS_LOCALEHELPER_H
33
34#include <QLocale>
35
36#include <limits>
37#include <cmath>
38
40{
41
42inline double toDouble(const QString& str, bool* ok = nullptr)
43{
44 bool isOkay;
45 QLocale c(QLocale::C);
46 c.setNumberOptions(QLocale::RejectGroupSeparator);
47 double value = c.toDouble(str, &isOkay);
48 if (!isOkay)
49 {
50 QLocale german(QLocale::German);
51 german.setNumberOptions(QLocale::RejectGroupSeparator);
52 value = german.toDouble(str, &isOkay);
53 }
54 if (ok != nullptr) { *ok = isOkay; }
55 return value;
56}
57
58inline float toFloat(const QString& str, bool* ok = nullptr)
59{
60 double d = toDouble(str, ok);
61 if (!std::isinf(d) && std::fabs(d) > std::numeric_limits<float>::max())
62 {
63 if (ok != nullptr) { *ok = false; }
64 return 0.0f;
65 }
66 return static_cast<float>(d);
67}
68
69
70} // namespace lmms::LocaleHelper
71
72#endif // LMMS_LOCALEHELPER_H
unsigned d
Definition inflate.c:940
static PuglViewHint int value
Definition pugl.h:1708
Definition LocaleHelper.h:40
float toFloat(const QString &str, bool *ok=nullptr)
Definition LocaleHelper.h:58
double toDouble(const QString &str, bool *ok=nullptr)
Definition LocaleHelper.h:42
return c
Definition crypt.c:175