grim/gplate

Thought I already added these... whoops...
trying_to_get_includes_working
2008-02-19, grim
963f73ea13a5
Parents d5876c6d72cc
Children 00fd8a59b7b2
Thought I already added these... whoops...
--- /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)
+ * any later version.
+ *
+ * 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
+ * more details.
+ *
+ * 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>
+
+/******************************************************************************
+ * Globals
+ *****************************************************************************/
+static GRegex *quoted_string_regex = NULL;
+
+/******************************************************************************
+ * 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 free'd with #g_free.
+ *
+ * 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) {
+ GMatchInfo *info = NULL;
+ gboolean ret = FALSE;
+
+ g_return_val_if_fail(str, FALSE);
+
+ if(!quoted_string_regex) {
+ quoted_string_regex = g_regex_new("\\s*(\"(.+)\"|'(.+)')\\s*",
+ 0, 0, NULL);
+ }
+
+ ret = g_regex_match(quoted_string_regex, str, 0, &info);
+
+ if(ret && contents)
+ *contents = g_match_info_fetch(info, 1);
+
+ g_match_info_free(info);
+
+ return ret;
+}
--- /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)
+ * any later version.
+ *
+ * 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
+ * more details.
+ *
+ * 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.
+ *
+ */
+#ifndef GPLATE_UTIL_H
+#define GPLATE_UTIL_H
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+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);
+
+G_END_DECLS
+
+#endif /* GPLATE_UTIL_H */