diff options
Diffstat (limited to 'src/cr-declaration.c')
-rw-r--r-- | src/cr-declaration.c | 284 |
1 files changed, 129 insertions, 155 deletions
diff --git a/src/cr-declaration.c b/src/cr-declaration.c index 8079aa0..530085b 100644 --- a/src/cr-declaration.c +++ b/src/cr-declaration.c @@ -1,4 +1,4 @@ -/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8 -*- */ +/* -*- Mode: C; indent-tabs-mode: ni; c-basic-offset: 8 -*- */ /* * This file is part of The Croco Library @@ -28,27 +28,22 @@ #include "cr-parser.h" /** - *@CRDeclaration: - * + *@file *The definition of the #CRDeclaration class. */ /** - * dump: - *@a_this: the current instance of #CRDeclaration. - *@a_fp: the destination file pointer. - *@a_indent: the number of indentation white char. - * *Dumps (serializes) one css declaration to a file. + *@param a_this the current instance of #CRDeclaration. + *@param a_fp the destination file pointer. + *@param a_indent the number of indentation white char. */ static void -dump (CRDeclaration const * a_this, FILE * a_fp, glong a_indent) +dump (CRDeclaration * a_this, FILE * a_fp, glong a_indent) { - guchar *str = NULL; - g_return_if_fail (a_this); - str = cr_declaration_to_string (a_this, a_indent); + gchar *str = cr_declaration_to_string (a_this, a_indent); if (str) { fprintf (a_fp, "%s", str); g_free (str); @@ -57,25 +52,16 @@ dump (CRDeclaration const * a_this, FILE * a_fp, glong a_indent) } /** - * cr_declaration_new: - * @a_statement: the statement this declaration belongs to. can be NULL. - *@a_property: the property string of the declaration - *@a_value: the value expression of the declaration. *Constructor of #CRDeclaration. - * - *Returns the newly built instance of #CRDeclaration, or NULL in + *@param a_property the property string of the declaration + *@param a_value the value expression of the declaration. + *@return the newly built instance of #CRDeclaration, or NULL in *case of error. - * - *The returned CRDeclaration takes ownership of @a_property and @a_value. - *(E.g. cr_declaration_destroy on this CRDeclaration will also free - *@a_property and @a_value.) */ CRDeclaration * cr_declaration_new (CRStatement * a_statement, CRString * a_property, CRTerm * a_value) { - CRDeclaration *result = NULL; - g_return_val_if_fail (a_property, NULL); if (a_statement) @@ -86,7 +72,7 @@ cr_declaration_new (CRStatement * a_statement, || (a_statement->type == AT_PAGE_RULE_STMT)), NULL); - result = g_try_malloc (sizeof (CRDeclaration)); + CRDeclaration * result = (CRDeclaration *)g_try_malloc (sizeof (CRDeclaration)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; @@ -103,16 +89,15 @@ cr_declaration_new (CRStatement * a_statement, } /** - * cr_declaration_parse_from_buf: - *@a_statement: the parent css2 statement of this - *this declaration. Must be non NULL and of type - *RULESET_STMT (must be a ruleset). - *@a_str: the string that contains the statement. - *@a_enc: the encoding of a_str. - * *Parses a text buffer that contains *a css declaration. - *Returns the parsed declaration, or NULL in case of error. + * + *@param a_statement the parent css2 statement of this + *this declaration. Must be non NULL and of type + *RULESET_STMT (must be a ruleset). + *@param a_str the string that contains the statement. + *@param a_enc the encoding of a_str. + *@return the parsed declaration, or NULL in case of error. */ CRDeclaration * cr_declaration_parse_from_buf (CRStatement * a_statement, @@ -122,7 +107,6 @@ cr_declaration_parse_from_buf (CRStatement * a_statement, CRTerm *value = NULL; CRString *property = NULL; CRDeclaration *result = NULL; - CRParser *parser = NULL; gboolean important = FALSE; g_return_val_if_fail (a_str, NULL); @@ -130,7 +114,9 @@ cr_declaration_parse_from_buf (CRStatement * a_statement, g_return_val_if_fail (a_statement->type == RULESET_STMT, NULL); - parser = cr_parser_new_from_buf ((guchar*)a_str, strlen (a_str), a_enc, FALSE); + CRParser *parser = (CRParser *) + cr_parser_new_from_buf ((guchar*)a_str, + strlen ((char *)a_str), a_enc, FALSE); g_return_val_if_fail (parser, NULL); status = cr_parser_try_to_skip_spaces_and_comments (parser); @@ -170,13 +156,11 @@ cr_declaration_parse_from_buf (CRStatement * a_statement, } /** - * cr_declaration_parse_list_from_buf: - *@a_str: the input buffer that contains the list of declaration to - *parse. - *@a_enc: the encoding of a_str - * *Parses a ';' separated list of properties declaration. - *Returns the parsed list of declaration, NULL if parsing failed. + *@param a_str the input buffer that contains the list of declaration to + *parse. + *@param a_enc the encoding of a_str + *@return the parsed list of declaration, NULL if parsing failed. */ CRDeclaration * cr_declaration_parse_list_from_buf (const guchar * a_str, @@ -188,13 +172,13 @@ cr_declaration_parse_list_from_buf (const guchar * a_str, CRString *property = NULL; CRDeclaration *result = NULL, *cur_decl = NULL; - CRParser *parser = NULL; CRTknzr *tokenizer = NULL; gboolean important = FALSE; g_return_val_if_fail (a_str, NULL); - parser = cr_parser_new_from_buf ((guchar*)a_str, strlen (a_str), a_enc, FALSE); + CRParser *parser = (CRParser *)cr_parser_new_from_buf + ((guchar*)a_str, strlen ((char *)a_str), a_enc, FALSE); g_return_val_if_fail (parser, NULL); status = cr_parser_get_tknzr (parser, &tokenizer); if (status != CR_OK || !tokenizer) { @@ -226,14 +210,15 @@ cr_declaration_parse_list_from_buf (const guchar * a_str, cr_parser_try_to_skip_spaces_and_comments (parser); status = cr_tknzr_peek_char (tokenizer, &c); if (status != CR_OK) { - if (status == CR_END_OF_INPUT_ERROR) + if (status == CR_END_OF_INPUT_ERROR) status = CR_OK; goto cleanup; } if (c == ';') { status = cr_tknzr_read_char (tokenizer, &c); } else { - break; + cr_tknzr_read_char (tokenizer, &c); + continue; // try to keep reading until we reach the end or a ; } important = FALSE; cr_parser_try_to_skip_spaces_and_comments (parser); @@ -241,9 +226,11 @@ cr_declaration_parse_list_from_buf (const guchar * a_str, &value, &important); if (status != CR_OK || !property) { if (status == CR_END_OF_INPUT_ERROR) { - status = CR_OK; - } - break; + status = CR_OK; // simply the end of input, do not delete what we got so far, just finish + break; + } else { + continue; // even if one declaration is broken, it's no reason to discard others (see http://www.w3.org/TR/CSS21/syndata.html#declaration) + } } cur_decl = cr_declaration_new (NULL, property, value); if (cur_decl) { @@ -282,12 +269,10 @@ cr_declaration_parse_list_from_buf (const guchar * a_str, } /** - * cr_declaration_append: - *@a_this: the current declaration list. - *@a_new: the declaration to append. - * *Appends a new declaration to the current declarations list. - *Returns the declaration list with a_new appended to it, or NULL + *@param a_this the current declaration list. + *@param a_new the declaration to append. + *@return the declaration list with a_new appended to it, or NULL *in case of error. */ CRDeclaration * @@ -309,13 +294,10 @@ cr_declaration_append (CRDeclaration * a_this, CRDeclaration * a_new) } /** - * cr_declaration_unlink: - *@a_decls: the declaration to unlink. - * *Unlinks the declaration from the declaration list. + *@param a_decl the declaration to unlink. + *@return a pointer to the unlinked declaration in *case of a successfull completion, NULL otherwise. - * - *Returns a pointer to the unlinked declaration in */ CRDeclaration * cr_declaration_unlink (CRDeclaration * a_decl) @@ -387,13 +369,10 @@ cr_declaration_unlink (CRDeclaration * a_decl) } /** - * cr_declaration_prepend: - * @a_this: the current declaration list. - * @a_new: the declaration to prepend. - * - * prepends a declaration to the current declaration list. - * - * Returns the list with a_new prepended or NULL in case of error. + *prepends a declaration to the current declaration list. + *@param a_this the current declaration list. + *@param a_new the declaration to prepend. + *@return the list with a_new prepended or NULL in case of error. */ CRDeclaration * cr_declaration_prepend (CRDeclaration * a_this, CRDeclaration * a_new) @@ -414,13 +393,11 @@ cr_declaration_prepend (CRDeclaration * a_this, CRDeclaration * a_new) } /** - * cr_declaration_append2: - *@a_this: the current declaration list. - *@a_prop: the property string of the declaration to append. - *@a_value: the value of the declaration to append. - * *Appends a declaration to the current declaration list. - *Returns the list with the new property appended to it, or NULL in + *@param a_this the current declaration list. + *@param a_prop the property string of the declaration to append. + *@param a_value the value of the declaration to append. + *@return the list with the new property appended to it, or NULL in *case of an error. */ CRDeclaration * @@ -442,20 +419,16 @@ cr_declaration_append2 (CRDeclaration * a_this, } /** - * cr_declaration_dump: - *@a_this: the current instance of #CRDeclaration. - *@a_fp: the destination file. - *@a_indent: the number of indentation white char. - *@a_one_per_line: whether to put one declaration per line of not . - * - * *Dumps a declaration list to a file. + *@param a_this the current instance of #CRDeclaration. + *@param a_fp the destination file. + *@param a_indent the number of indentation white char. */ void -cr_declaration_dump (CRDeclaration const * a_this, FILE * a_fp, glong a_indent, +cr_declaration_dump (CRDeclaration * a_this, FILE * a_fp, glong a_indent, gboolean a_one_per_line) { - CRDeclaration const *cur = NULL; + CRDeclaration *cur = NULL; g_return_if_fail (a_this); @@ -471,15 +444,13 @@ cr_declaration_dump (CRDeclaration const * a_this, FILE * a_fp, glong a_indent, } /** - * cr_declaration_dump_one: - *@a_this: the current instance of #CRDeclaration. - *@a_fp: the destination file. - *@a_indent: the number of indentation white char. - * *Dumps the first declaration of the declaration list to a file. + *@param a_this the current instance of #CRDeclaration. + *@param a_fp the destination file. + *@param a_indent the number of indentation white char. */ void -cr_declaration_dump_one (CRDeclaration const * a_this, FILE * a_fp, glong a_indent) +cr_declaration_dump_one (CRDeclaration * a_this, FILE * a_fp, glong a_indent) { g_return_if_fail (a_this); @@ -487,21 +458,17 @@ cr_declaration_dump_one (CRDeclaration const * a_this, FILE * a_fp, glong a_inde } /** - * cr_declaration_to_string: - *@a_this: the current instance of #CRDeclaration. - *@a_indent: the number of indentation white char - *to put before the actual serialisation. - * *Serializes the declaration into a string - *Returns the serialized form the declaration. The caller must - *free the string using g_free(). + *@param a_this the current instance of #CRDeclaration. + *@param a_indent the number of indentation white char + *to put before the actual serialisation. */ gchar * -cr_declaration_to_string (CRDeclaration const * a_this, gulong a_indent) +cr_declaration_to_string (CRDeclaration * a_this, gulong a_indent) { GString *stringue = NULL; - guchar *str = NULL, + gchar *str = NULL, *result = NULL; g_return_val_if_fail (a_this, NULL); @@ -558,19 +525,17 @@ cr_declaration_to_string (CRDeclaration const * a_this, gulong a_indent) } /** - * cr_declaration_list_to_string: - *@a_this: the current instance of #CRDeclaration. - *@a_indent: the number of indentation white char - *to put before the actual serialisation. - * *Serializes the declaration list into a string + *@param a_this the current instance of #CRDeclaration. + *@param a_indent the number of indentation white char + *to put before the actual serialisation. */ guchar * -cr_declaration_list_to_string (CRDeclaration const * a_this, gulong a_indent) +cr_declaration_list_to_string (CRDeclaration * a_this, gulong a_indent) { - CRDeclaration const *cur = NULL; + CRDeclaration *cur = NULL; GString *stringue = NULL; - guchar *str = NULL, + gchar *str = NULL, *result = NULL; g_return_val_if_fail (a_this, NULL); @@ -590,26 +555,22 @@ cr_declaration_list_to_string (CRDeclaration const * a_this, gulong a_indent) g_string_free (stringue, FALSE); } - return result; + return (guchar *)result; } /** - * cr_declaration_list_to_string2: - *@a_this: the current instance of #CRDeclaration. - *@a_indent: the number of indentation white char - *@a_one_decl_per_line: whether to output one doc per line or not. - *to put before the actual serialisation. - * *Serializes the declaration list into a string - *Returns the serialized form the declararation. + *@param a_this the current instance of #CRDeclaration. + *@param a_indent the number of indentation white char + *to put before the actual serialisation. */ guchar * -cr_declaration_list_to_string2 (CRDeclaration const * a_this, +cr_declaration_list_to_string2 (CRDeclaration * a_this, gulong a_indent, gboolean a_one_decl_per_line) { - CRDeclaration const *cur = NULL; + CRDeclaration *cur = NULL; GString *stringue = NULL; - guchar *str = NULL, + gchar *str = NULL, *result = NULL; g_return_val_if_fail (a_this, NULL); @@ -643,18 +604,18 @@ cr_declaration_list_to_string2 (CRDeclaration const * a_this, g_string_free (stringue, FALSE); } - return result; + return (guchar *)result; } /** - * cr_declaration_nr_props: - *@a_this: the current instance of #CRDeclaration. - *Return the number of properties in the declaration + *Return the number of properties in the declaration; + *@param a_this the current instance of #CRDeclaration. + *@return number of properties in the declaration list. */ gint -cr_declaration_nr_props (CRDeclaration const * a_this) +cr_declaration_nr_props (CRDeclaration * a_this) { - CRDeclaration const *cur = NULL; + CRDeclaration *cur = NULL; int nr = 0; g_return_val_if_fail (a_this, -1); @@ -665,14 +626,10 @@ cr_declaration_nr_props (CRDeclaration const * a_this) } /** - * cr_declaration_get_from_list: - *@a_this: the current instance of #CRDeclaration. - *@itemnr: the index into the declaration list. - * *Use an index to get a CRDeclaration from the declaration list. - * - *Returns #CRDeclaration at position itemnr, - *if itemnr > number of declarations - 1, + *@param a_this the current instance of #CRDeclaration. + *@param itemnr the index into the declaration list. + *@return CRDeclaration at position itemnr, if itemnr > number of declarations - 1, *it will return NULL. */ CRDeclaration * @@ -690,12 +647,10 @@ cr_declaration_get_from_list (CRDeclaration * a_this, int itemnr) } /** - * cr_declaration_get_by_prop_name: - *@a_this: the current instance of #CRDeclaration. - *@a_prop: the property name to search for. - * *Use property name to get a CRDeclaration from the declaration list. - *Returns #CRDeclaration with property name a_prop, or NULL if not found. + *@param a_this the current instance of #CRDeclaration. + *@param a_prop the property name to search for. + *@return CRDeclaration with property name a_prop, or NULL if not found. */ CRDeclaration * cr_declaration_get_by_prop_name (CRDeclaration * a_this, @@ -711,7 +666,7 @@ cr_declaration_get_by_prop_name (CRDeclaration * a_this, && cur->property->stryng && cur->property->stryng->str) { if (!strcmp (cur->property->stryng->str, - a_prop)) { + (char *)a_prop)) { return cur; } } @@ -720,10 +675,8 @@ cr_declaration_get_by_prop_name (CRDeclaration * a_this, } /** - * cr_declaration_ref: - *@a_this: the current instance of #CRDeclaration. - * *Increases the ref count of the current instance of #CRDeclaration. + *@param a_this the current instance of #CRDeclaration. */ void cr_declaration_ref (CRDeclaration * a_this) @@ -734,14 +687,12 @@ cr_declaration_ref (CRDeclaration * a_this) } /** - * cr_declaration_unref: - *@a_this: the current instance of #CRDeclaration. - * *Decrements the ref count of the current instance of #CRDeclaration. *If the ref count reaches zero, the current instance of #CRDeclaration *if destroyed. - *Returns TRUE if @a_this was destroyed (ref count reached zero), - *FALSE otherwise. + *@param a_this the current instance of #CRDeclaration. + *@return TRUE if the current instance of #CRDeclaration has been destroyed + *(ref count reached zero), FALSE otherwise. */ gboolean cr_declaration_unref (CRDeclaration * a_this) @@ -760,10 +711,8 @@ cr_declaration_unref (CRDeclaration * a_this) } /** - * cr_declaration_destroy: - *@a_this: the current instance of #CRDeclaration. - * *Destructor of the declaration list. + *@param a_this the current instance of #CRDeclaration. */ void cr_declaration_destroy (CRDeclaration * a_this) @@ -773,19 +722,22 @@ cr_declaration_destroy (CRDeclaration * a_this) g_return_if_fail (a_this); /* - * Go to the last element of the list. + *Go get the tail of the list. + *Meanwhile, free each property/value pair contained in the list. */ - for (cur = a_this; cur->next; cur = cur->next) - g_assert (cur->next->prev == cur); + for (cur = a_this; cur && cur->next; cur = cur->next) { + if (cur->property) { + cr_string_destroy (cur->property); + cur->property = NULL; + } - /* - * Walk backward the list and free each "next" element. - * Meanwhile, free each property/value pair contained in the list. - */ - for (; cur; cur = cur->prev) { - g_free (cur->next); - cur->next = NULL; + if (cur->value) { + cr_term_destroy (cur->value); + cur->value = NULL; + } + } + if (cur) { if (cur->property) { cr_string_destroy (cur->property); cur->property = NULL; @@ -797,5 +749,27 @@ cr_declaration_destroy (CRDeclaration * a_this) } } - g_free (a_this); + /*in case the list contains only one element */ + if (cur && !cur->prev) { + g_free (cur); + return; + } + + /*walk backward the list and free each "next" element */ + for (cur = cur->prev; cur && cur->prev; cur = cur->prev) { + if (cur->next) { + g_free (cur->next); + cur->next = NULL; + } + } + + if (!cur) + return; + + if (cur->next) { + g_free (cur->next); + cur->next = NULL; + } + + g_free (cur); } |