grim/gplate
Clone
Summary
Browse
Changes
Graph
I thought I already added this.. my bad
trying_to_get_includes_working
2008-02-20, grim
545b7214a0de
I thought I already added this.. my bad
/*
* 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>
/******************************************************************************
* 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 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
)
{
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
)
*
contents
=
g_match_info_fetch
(
info
,
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
;
}