/** *The test of the cr_input_read_byte() method. *Reads the each byte of a_file_uri using the *cr_input_read_byte() method. Each byte is send to *stdout. *@param a_file_uri the file to read. *@return CR_OK upon successfull completion of the *function, an error code otherwise. */ static enum CRStatus test_cr_parser_parse (guchar * a_file_uri) { enum CRStatus status = CR_OK; CRParser *parser = NULL; g_return_val_if_fail (a_file_uri, CR_BAD_PARAM_ERROR); gv_test_handler = cr_doc_handler_new (); init_test_sac_handler (gv_test_handler); parser = cr_parser_new (NULL); status = cr_parser_set_sac_handler (parser, gv_test_handler); if (status != CR_OK) { cr_parser_destroy (parser); g_return_val_if_fail (status == CR_OK, CR_ERROR); } status = cr_parser_parse_file (parser, a_file_uri, CR_ASCII); cr_parser_destroy (parser); gv_test_handler = NULL; return status; }
/** *Parses an expresion as defined by the css2 spec *and builds the expression as a list of terms. *@param a_buf the buffer to parse. *@return a pointer to the first term of the expression or *NULL if parsing failed. */ CRTerm * cr_term_parse_expression_from_buf (const guchar * a_buf, enum CREncoding a_encoding) { CRTerm *result = NULL; enum CRStatus status = CR_OK; g_return_val_if_fail (a_buf, NULL); CRParser *parser = cr_parser_new_from_buf ( (guchar*)a_buf, strlen ((char *)a_buf), a_encoding, FALSE); g_return_val_if_fail (parser, NULL); status = cr_parser_try_to_skip_spaces_and_comments (parser); if (status != CR_OK) { goto cleanup; } status = cr_parser_parse_expr (parser, &result); if (status != CR_OK) { if (result) { cr_term_destroy (result); result = NULL; } } cleanup: if (parser) { cr_parser_destroy (parser); parser = NULL; } return result; }
/** *Parses a text buffer that contains *a css declaration. * *@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, const guchar * a_str, enum CREncoding a_enc) { enum CRStatus status = CR_OK; CRTerm *value = NULL; CRString *property = NULL; CRDeclaration *result = NULL; gboolean important = FALSE; g_return_val_if_fail (a_str, NULL); if (a_statement) g_return_val_if_fail (a_statement->type == RULESET_STMT, NULL); 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); if (status != CR_OK) goto cleanup; status = cr_parser_parse_declaration (parser, &property, &value, &important); if (status != CR_OK || !property) goto cleanup; result = cr_declaration_new (a_statement, property, value); if (result) { property = NULL; value = NULL; result->important = important; } cleanup: if (parser) { cr_parser_destroy (parser); parser = NULL; } if (property) { cr_string_destroy (property); property = NULL; } if (value) { cr_term_destroy (value); value = NULL; } return result; }
/** * cr_om_parser_destroy: *@a_this: the current instance of #CROMParser. * *Destructor of the #CROMParser. */ void cr_om_parser_destroy (CROMParser * a_this) { g_return_if_fail (a_this && PRIVATE (a_this)); if (PRIVATE (a_this)->parser) { cr_parser_destroy (PRIVATE (a_this)->parser); PRIVATE (a_this)->parser = NULL; } if (PRIVATE (a_this)) { g_free (PRIVATE (a_this)); PRIVATE (a_this) = NULL; } if (a_this) { g_free (a_this); a_this = NULL; } }
/** * cr_rgb_parse_from_buf: *@a_str: a string that contains a color description *@a_enc: the encoding of a_str * *Parses a text buffer that contains a rgb color * *Returns the parsed color, or NULL in case of error */ CRRgb * cr_rgb_parse_from_buf (const guchar *a_str, enum CREncoding a_enc) { enum CRStatus status = CR_OK ; CRTerm *value = NULL ; CRParser * parser = NULL; CRRgb *result = NULL; g_return_val_if_fail (a_str, NULL); parser = cr_parser_new_from_buf ((guchar*)a_str, strlen (a_str), a_enc, FALSE) ; g_return_val_if_fail (parser, NULL); status = cr_parser_try_to_skip_spaces_and_comments (parser) ; if (status != CR_OK) goto cleanup; status = cr_parser_parse_term (parser, &value); if (status != CR_OK) goto cleanup; result = cr_rgb_new (); if (!result) goto cleanup; status = cr_rgb_set_from_term (result, value); cleanup: if (parser) { cr_parser_destroy (parser); parser = NULL; } if (value) { cr_term_destroy(value); value = NULL; } return result ; }
/** * 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. */ CRDeclaration * cr_declaration_parse_list_from_buf (const guchar * a_str, enum CREncoding a_enc) { enum CRStatus status = CR_OK; CRTerm *value = NULL; 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); g_return_val_if_fail (parser, NULL); status = cr_parser_get_tknzr (parser, &tokenizer); if (status != CR_OK || !tokenizer) { if (status == CR_OK) status = CR_ERROR; goto cleanup; } status = cr_parser_try_to_skip_spaces_and_comments (parser); if (status != CR_OK) goto cleanup; status = cr_parser_parse_declaration (parser, &property, &value, &important); if (status != CR_OK || !property) { if (status != CR_OK) status = CR_ERROR; goto cleanup; } result = cr_declaration_new (NULL, property, value); if (result) { property = NULL; value = NULL; result->important = important; } /*now, go parse the other declarations */ for (;;) { guint32 c = 0; 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) status = CR_OK; goto cleanup; } if (c == ';') { status = cr_tknzr_read_char (tokenizer, &c); } else { break; } important = FALSE; cr_parser_try_to_skip_spaces_and_comments (parser); status = cr_parser_parse_declaration (parser, &property, &value, &important); if (status != CR_OK || !property) { if (status == CR_END_OF_INPUT_ERROR) { status = CR_OK; } break; } cur_decl = cr_declaration_new (NULL, property, value); if (cur_decl) { cur_decl->important = important; result = cr_declaration_append (result, cur_decl); property = NULL; value = NULL; cur_decl = NULL; } else { break; } } cleanup: if (parser) { cr_parser_destroy (parser); parser = NULL; } if (property) { cr_string_destroy (property); property = NULL; } if (value) { cr_term_destroy (value); value = NULL; } if (status != CR_OK && result) { cr_declaration_destroy (result); result = NULL; } return result; }
/** *Parses a ';' separated list of properties declaration. *@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, enum CREncoding a_enc) { enum CRStatus status = CR_OK; CRTerm *value = NULL; CRString *property = NULL; CRDeclaration *result = NULL, *cur_decl = NULL; CRTknzr *tokenizer = NULL; gboolean important = FALSE; g_return_val_if_fail (a_str, NULL); 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) { if (status == CR_OK) status = CR_ERROR; goto cleanup; } status = cr_parser_try_to_skip_spaces_and_comments (parser); if (status != CR_OK) goto cleanup; status = cr_parser_parse_declaration (parser, &property, &value, &important); if (status != CR_OK || !property) { if (status != CR_OK) status = CR_ERROR; goto cleanup; } result = cr_declaration_new (NULL, property, value); if (result) { property = NULL; value = NULL; result->important = important; } /*now, go parse the other declarations */ for (;;) { guint32 c = 0; 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) status = CR_OK; goto cleanup; } if (c == ';') { status = cr_tknzr_read_char (tokenizer, &c); } else { 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); status = cr_parser_parse_declaration (parser, &property, &value, &important); if (status != CR_OK || !property) { if (status == CR_END_OF_INPUT_ERROR) { 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) { cur_decl->important = important; result = cr_declaration_append (result, cur_decl); property = NULL; value = NULL; cur_decl = NULL; } else { break; } } cleanup: if (parser) { cr_parser_destroy (parser); parser = NULL; } if (property) { cr_string_destroy (property); property = NULL; } if (value) { cr_term_destroy (value); value = NULL; } if (status != CR_OK && result) { cr_declaration_destroy (result); result = NULL; } return result; }