grim/gplate

some more testing migration stuff.

2012-09-04, Gary Kramlich
7f8e0e4f250f
some more testing migration stuff.
Variables aren't done yet
/*
* GPlate - GObject based templating library
* Copyright (C) 2007-2012 Gary Kramlich <grim@reaperworld.com>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gplate/gplate-util.h>
#include <gplate/gplate-errors.h>
/******************************************************************************
* Regex's
*****************************************************************************/
#define QUOTED_REGEX "^\\s*(\"(.+)\"|'(.+)')\\s*$"
#define FIRST_WORD_REGEX "\\s*([a-zA-Z_][a-zA-Z0-9_\\.]*)(\\s*(.*))?"
/******************************************************************************
* API
*****************************************************************************/
/**
* gplate_util_read_file:
* @filename: The name of the file to read. Can be relative or absolute path.
* @contents: The return address for the contents for the file.
* @length: The return address for the length of the file.
* @error: The return address for any errors.
*
* Reads the contents of @filename and stores then in @contents, as well as
* the length of the contents in @length.
*
* If @contents is provided, it must be #g_free()'d.
*
* Return Value: TRUE on success, otherwise FALSE with @error set.
*/
gboolean
gplate_util_read_file(const gchar *filename, gchar **contents, gsize *length,
GError **error)
{
g_return_val_if_fail(filename, FALSE);
if(!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
if(error) {
*error = g_error_new(GPLATE_DOMAIN,
GPLATE_ERROR_UTIL_FILE_DOES_NOT_EXIST,
"File '%s' does not exist.",
filename);
}
return FALSE;
}
if(!g_file_get_contents(filename, contents, length, error))
return FALSE;
return TRUE;
}
/**
* gplate_util_is_quoted_string:
* @str: The string to check if it's quoted.
* @contents: The return address for the contents of @str.
*
* Checks if @str is quoted in either single or double quotes. Whitespace
* before and after the quotes is ignored.
*
* If @contents is provided, it will be populated with the @str with the quotes
* removed.
*
* Return Value: TRUE if @str is quoted, otherwise FALSE.
*/
gboolean
gplate_util_is_quoted_string(const gchar *str, gchar **contents) {
static GRegex *regex = NULL;
GMatchInfo *info = NULL;
gboolean ret = FALSE;
g_return_val_if_fail(str, FALSE);
if(!regex)
regex = g_regex_new(QUOTED_REGEX, G_REGEX_OPTIMIZE, 0, NULL);
ret = g_regex_match(regex, str, 0, &info);
if(ret && contents) {
/* due the manner in our regex had to be crafted, our last group is
* also the unquoted one. So we find the last group and return it.
*/
gint count = g_match_info_get_match_count(info);
*contents = g_match_info_fetch(info, count - 1);
}
g_match_info_free(info);
return ret;
}
/**
* gplate_util_get_first_word:
* @str: The string who's first word to get.
* @leftovers: The return address for everything after the first word.
*
* Gets the first word of a @str and returns it. The remaining contents of
* @str are stored in @leftovers if provided.
*
* The use of "word" is a bit of a misnomer. A word in this sense is a series
* of characters that begins with an letter or underscore and is followed by
* any number of letters, numbers, periods, or underscores.
*
* Return Value: The first word of @str, or NULL.
*/
gchar *
gplate_util_get_first_word(const gchar *str, gchar **leftovers) {
static GRegex *regex = NULL;
GMatchInfo *info = NULL;
gchar *ret = NULL;
g_return_val_if_fail(str, NULL);
if(!regex)
regex = g_regex_new(FIRST_WORD_REGEX, G_REGEX_OPTIMIZE, 0, NULL);
if(!g_regex_match(regex, str, 0, &info))
return NULL;
ret = g_match_info_fetch(info, 1);
if(leftovers && g_match_info_get_match_count(info) > 2)
*leftovers = g_match_info_fetch(info, 3);
g_match_info_free(info);
return ret;
}