/*
*
* ProLinga-Data
*
* Copyright (C) 2002-2009 Xobas Software.
* All rights reserved.
*
* This file is part of ProLinga-Data.
*
* ProLinga-Data 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.
*
* ProLinga-Data 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 ProLinga-Data. If not, see .
*
* More information is available at the following addresses:
*
* Website : http://www.prolinga.org
*
* Email : prolinga-list@prolinga.org
*
*
*/
#include "DatCommon.h"
#include
#include "Utility.hpp"
/* Check if a file exists */
bool FileExists(const char * fileName)
{
if (FILE * file = fopen(fileName, "r"))
{
fclose(file);
return true;
}
return false;
}
/* Scans a set of characters and returns 0 if the set mismatches at this */
/* position in the teststring and 1 if it is matching */
/* wildcard is set to the closing ] and test is unmodified if mismatched */
/* and otherwise the char pointer is pointing to the next character */
int set (char **wildcard, char **test)
{
int fit = 0;
int negation = 0;
int at_beginning = 1;
if ('!' == **wildcard)
{
negation = 1;
(*wildcard)++;
}
while ((']' != **wildcard) || (1 == at_beginning))
{
if (0 == fit)
{
if (('-' == **wildcard)
&& ((*(*wildcard - 1)) < (*(*wildcard + 1)))
&& (']' != *(*wildcard + 1))
&& (0 == at_beginning))
{
if (((**test) >= (*(*wildcard - 1)))
&& ((**test) <= (*(*wildcard + 1))))
{
fit = 1;
(*wildcard)++;
}
}
else if ((**wildcard) == (**test))
{
fit = 1;
}
}
(*wildcard)++;
at_beginning = 0;
}
if (1 == negation)
/* change from zero to one and vice versa */
fit = 1 - fit;
if (1 == fit)
(*test)++;
return (fit);
}
/* scans an asterisk */
int asterisk (char **wildcard, char **test)
{
/* Warning: uses multiple returns */
int fit = 1;
/* erase the leading asterisk */
(*wildcard)++;
while (('\000' != (**test))
&& (('?' == **wildcard)
|| ('*' == **wildcard)))
{
if ('?' == **wildcard)
(*test)++;
(*wildcard)++;
}
/* Now it could be that test is empty and wildcard contains */
/* aterisks. Then we delete them to get a proper state */
while ('*' == (**wildcard))
(*wildcard)++;
if (('\0' == (**test)) && ('\0' != (**wildcard)))
return (fit = 0);
if (('\0' == (**test)) && ('\0' == (**wildcard)))
return (fit = 1);
else
{
/* Neither test nor wildcard are empty! */
/* the first character of wildcard isn't in [*?] */
if (0 == WildcardMatch(*wildcard, (*test)))
{
do
{
(*test)++;
/* skip as much characters as possible in the teststring */
/* stop if a character match occurs */
while (((**wildcard) != (**test))
&& ('[' != (**wildcard))
&& ('\0' != (**test)))
(*test)++;
}
while ((('\0' != **test))?
(0 == WildcardMatch (*wildcard, (*test)))
: (0 != (fit = 0)));
}
if (('\0' == **test) && ('\0' == **wildcard))
fit = 1;
return (fit);
}
}
int WildcardMatch (char *wildcard, char *test)
{
int fit = 1;
for (; ('\000' != *wildcard) && (1 == fit) && ('\000' != *test); wildcard++)
{
switch (*wildcard)
{
case '[':
wildcard++; /* leave out the opening square bracket */
fit = set (&wildcard, &test);
/* we don't need to decrement the wildcard as in case */
/* of asterisk because the closing ] is still there */
break;
case '?':
test++;
break;
case '*':
fit = asterisk (&wildcard, &test);
/* the asterisk was skipped by asterisk() but the loop will */
/* increment by itself. So we have to decrement */
wildcard--;
break;
default:
fit = (int) (*wildcard == *test);
test++;
}
}
while ((*wildcard == '*') && (1 == fit))
/* here the teststring is empty otherwise you cannot */
/* leave the previous loop */
wildcard++;
return (int) ((1 == fit) && ('\0' == *test) && ('\0' == *wildcard));
}
GValue *GValueFromString(const char *string, GType type)
{
GValue *ret_value = NULL;
gchar *endptr [1];
glong lvalue;
gulong ulvalue;
gdouble dvalue;
switch (type)
{
case G_TYPE_CHAR:
ret_value = gda_value_new (G_TYPE_CHAR);
g_value_set_char (ret_value, (gchar) string[0]);
break;
case G_TYPE_UCHAR:
ret_value = gda_value_new (G_TYPE_UCHAR);
g_value_set_char (ret_value, (guchar) string[0]);
break;
case G_TYPE_STRING:
ret_value = gda_value_new (G_TYPE_STRING);
g_value_set_string (ret_value, string);
break;
case G_TYPE_BOOLEAN:
ret_value = gda_value_new (G_TYPE_BOOLEAN);
if (*string != '\0')
{
if (g_strcasecmp (string, "true") == 0)
g_value_set_boolean (ret_value, TRUE);
else if (g_strcasecmp (string, "false") == 0)
g_value_set_boolean (ret_value, FALSE);
}
break;
case G_TYPE_INT:
ret_value = gda_value_new (G_TYPE_INT);
lvalue = strtol (string, endptr, 10);
if (*string != '\0' && **endptr == '\0')
g_value_set_int (ret_value, (gint32) lvalue);
break;
case G_TYPE_UINT:
ret_value = gda_value_new (G_TYPE_UINT);
ulvalue = strtoul (string, endptr, 10);
if (*string != '\0' && **endptr == '\0')
g_value_set_uint (ret_value, (guint32) ulvalue);
break;
case G_TYPE_LONG:
ret_value = gda_value_new (G_TYPE_LONG);
lvalue = strtol (string, endptr, 10);
if (*string != '\0' && **endptr == '\0')
g_value_set_long (ret_value, lvalue);
break;
case G_TYPE_ULONG:
ret_value = gda_value_new (G_TYPE_ULONG);
ulvalue = strtoul (string, endptr, 10);
if (*string != '\0' && **endptr == '\0')
g_value_set_ulong (ret_value, ulvalue);
break;
case G_TYPE_INT64:
ret_value = gda_value_new (G_TYPE_INT64);
dvalue = g_strtod (string, endptr);
if (*string != '\0' && **endptr == '\0')
g_value_set_int64 (ret_value, (gint64) dvalue);
break;
case G_TYPE_UINT64:
ret_value = gda_value_new (G_TYPE_UINT64);
dvalue = g_strtod (string, endptr);
if (*string != '\0' && **endptr == '\0')
g_value_set_uint64 (ret_value, (guint64) dvalue);
break;
case G_TYPE_ENUM:
ret_value = gda_value_new (G_TYPE_ENUM);
lvalue = strtol (string, endptr, 10);
if (*string != '\0' && **endptr == '\0')
g_value_set_enum (ret_value, (gint32) lvalue);
break;
case G_TYPE_FLAGS:
/* FIX ME */
ret_value = NULL;
break;
case G_TYPE_FLOAT:
ret_value = gda_value_new (G_TYPE_FLOAT);
dvalue = g_strtod (string, endptr);
if (*string != '\0' && **endptr == '\0')
g_value_set_float (ret_value, (gfloat) dvalue);
break;
case G_TYPE_DOUBLE:
ret_value = gda_value_new (G_TYPE_DOUBLE);
dvalue = g_strtod (string, endptr);
if (*string != '\0' && **endptr == '\0')
g_value_set_double (ret_value, dvalue);
break;
default:
ret_value = NULL;
}
/* Return Value */
return ret_value;
}