1 /**
2  * dialog-bi-import-helper.c -- Helper functions
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, contact:
16  *
17  * Free Software Foundation           Voice:  +1-617-542-5942
18  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
19  * Boston, MA  02110-1301,  USA       gnu@gnu.org
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 #include "dialog-bi-import-helper.h"
27 #include <ctype.h>
28 #include <time.h>
29 #ifndef HAVE_STRPTIME
30 #include <strptime.h>
31 #endif
32 
33 //! \brief helper function
text2bool(const gchar * text)34 gboolean text2bool( const gchar *text )
35 {
36     gboolean erg = FALSE;
37     gchar *temp;
38 
39     if (!text)
40         return erg;
41 
42     temp = g_strdup( text );
43     g_strstrip( temp );
44     if ((g_ascii_strncasecmp( temp, "y",1 ) == 0) || (g_ascii_strncasecmp( temp, "t",1 ) == 0) ||
45             (g_ascii_strcasecmp( temp, "1" ) == 0) || (g_ascii_strcasecmp( temp, "x" ) == 0))
46         erg = TRUE;
47     g_free( temp );
48     return erg;
49 }
50 
51 
52 //! \brief helper function
53 // Try to make a valid tm using the user set date format.  Return false if it fails.
54 gboolean
isDateValid(char * date_string)55 isDateValid(char * date_string)
56 {
57     char  *tmp;
58     const gchar* date_format_string = qof_date_format_get_string (qof_date_format_get()); // Get the user set date format string
59 
60     struct tm time_struct;
61     memset(&time_struct, 0, sizeof(struct tm));
62 
63    tmp = strptime(date_string, date_format_string, &time_struct);
64    if (tmp == NULL) return FALSE;
65    return TRUE;
66 }
67 
68 //! \brief helper function
text2disc_type(const gchar * text)69 GncAmountType text2disc_type( const gchar *text )
70 {
71     GncAmountType type = GNC_AMT_TYPE_PERCENT;
72     gchar *temp;
73 
74     if (!text)
75         return type;
76 
77     temp = g_strdup( text );
78     g_strstrip( temp );
79     if ((strlen(temp) > 0) && (g_ascii_strcasecmp( temp, "%" ) != 0))
80         type = GNC_AMT_TYPE_VALUE;
81     g_free( temp );
82     return type;
83 }
84 
85 //! \brief helper function
text2disc_how(const gchar * text)86 GncDiscountHow text2disc_how( const gchar *text )
87 {
88     GncDiscountHow how = GNC_DISC_PRETAX;
89     gchar *temp;
90 
91     if (!text)
92         return how;
93 
94     temp = g_strdup( text );
95     g_strstrip( temp );
96     if (g_ascii_strcasecmp( temp, "=" ) == 0)
97         how = GNC_DISC_SAMETIME;
98     else if (g_ascii_strcasecmp( temp, ">" ) == 0)
99         how = GNC_DISC_POSTTAX;
100     g_free( temp );
101     return how;
102 }
103