1 /*
2  * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3  *           (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
4  *
5  * This file is part of lsp-plugins
6  * Created on: 13 июл. 2019 г.
7  *
8  * lsp-plugins is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * any later version.
12  *
13  * lsp-plugins is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with lsp-plugins. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <core/parse.h>
23 
24 namespace lsp
25 {
parse_float(const char * variable,float * res)26     bool parse_float(const char *variable, float *res)
27     {
28         UPDATE_LOCALE(saved_locale, LC_NUMERIC, "C");
29         errno = 0;
30         char *end   = NULL;
31         float value = strtof(variable, &end);
32 
33         bool success = (errno == 0);
34         if ((end != NULL) && (success))
35         {
36             // Skip spaces
37             while ((*end) == ' ')
38                 ++ end;
39             if (((end[0] == 'd') || (end[0] == 'D')) &&
40                 ((end[1] == 'b') || (end[1] == 'B')))
41                 value   = expf(value * M_LN10 * 0.05);
42         }
43 
44         if (saved_locale != NULL)
45             setlocale(LC_NUMERIC, saved_locale);
46 
47         if (res != NULL)
48             *res        = value;
49         return success;
50     }
51 
parse_double(const char * variable,double * res)52     bool parse_double(const char *variable, double *res)
53     {
54         UPDATE_LOCALE(saved_locale, LC_NUMERIC, "C");
55         errno = 0;
56         char *end       = NULL;
57         double value    = strtod(variable, &end);
58 
59         bool success    = (errno == 0);
60         if ((end != NULL) && (success))
61         {
62             // Skip spaces
63             while ((*end) == ' ')
64                 ++ end;
65             if (((end[0] == 'd') || (end[0] == 'D')) &&
66                 ((end[1] == 'b') || (end[1] == 'B')))
67                 value   = expf(value * M_LN10 * 0.05);
68         }
69 
70         if (saved_locale != NULL)
71             setlocale(LC_NUMERIC, saved_locale);
72 
73         if (res != NULL)
74             *res        = value;
75         return success;
76     }
77 }
78 
79 
80