--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/gplate/gplate-util.c Tue Feb 19 23:51:18 2008 -0600
@@ -0,0 +1,103 @@
+ * Copyright (C) 2007-2008 Gary Kramlich <grim@reaperworld.com> + * This program 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 2 of the License, or (at your option) + * This program 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 + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +#include <gplate/gplate-util.h> +#include <gplate/gplate-errors.h> +/****************************************************************************** + *****************************************************************************/ +static GRegex *quoted_string_regex = NULL; +/****************************************************************************** + *****************************************************************************/ + * 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 free'd with #g_free. + * Return Value: TRUE on success, otherwise FALSE with @error set. +gplate_util_read_file(const gchar *filename, gchar **contents, gsize *length, + g_return_val_if_fail(filename, FALSE); + if(!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) { + *error = g_error_new(GPLATE_DOMAIN, + GPLATE_ERROR_UTIL_FILE_DOES_NOT_EXIST, + "File '%s' does not exist.", + if(!g_file_get_contents(filename, contents, length, error)) + * 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 + * Return Value: TRUE if @str is quoted, otherwise FALSE. +gplate_util_is_quoted_string(const gchar *str, gchar **contents) { + GMatchInfo *info = NULL; + g_return_val_if_fail(str, FALSE); + if(!quoted_string_regex) { + quoted_string_regex = g_regex_new("\\s*(\"(.+)\"|'(.+)')\\s*", + ret = g_regex_match(quoted_string_regex, str, 0, &info); + *contents = g_match_info_fetch(info, 1); + g_match_info_free(info); --- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/gplate/gplate-util.h Tue Feb 19 23:51:18 2008 -0600
@@ -0,0 +1,32 @@
+ * Copyright (C) 2007-2008 Gary Kramlich <grim@reaperworld.com> + * This program 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 2 of the License, or (at your option) + * This program 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 + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +gboolean gplate_util_read_file(const gchar *filename, gchar **contents, gsize *length, GError **error); +gboolean gplate_util_is_quoted_string(const gchar *str, gchar **contents); +#endif /* GPLATE_UTIL_H */