[libcroco-list] Patch: cr_rgb_parse_from_buf



This patch adds two functions, cr_rgb_parse_from_buf and
cr_rgb_set_from_term and modifies the function set_prop_color and
set_prop_background_color in cr-style.c.
Index: cr-parser.c
===================================================================
RCS file: /cvs/gnome/libcroco/src/cr-parser.c,v
retrieving revision 1.23
diff -u -r1.23 cr-parser.c
--- cr-parser.c	6 Mar 2004 22:49:55 -0000	1.23
+++ cr-parser.c	7 Mar 2004 18:57:15 -0000
@@ -394,9 +394,6 @@
 cr_parser_parse_uri (CRParser *a_this, GString **a_str) ;
 
 static enum CRStatus
-cr_parser_parse_term (CRParser *a_this, CRTerm **a_term) ;
-
-static enum CRStatus
 cr_parser_parse_function (CRParser *a_this, GString **a_func_name,
                           CRTerm **a_expr) ;
 static enum CRStatus
@@ -2022,7 +2019,7 @@
  * param a_term out parameter. The successfully parsed term.
  * return CR_OK upon successfull completion, an error code otherwise.
  */
-static enum CRStatus
+enum CRStatus
 cr_parser_parse_term (CRParser *a_this, CRTerm **a_term)
 {
         enum CRStatus status = CR_PARSING_ERROR ;
Index: cr-parser.h
===================================================================
RCS file: /cvs/gnome/libcroco/src/cr-parser.h,v
retrieving revision 1.8
diff -u -r1.8 cr-parser.h
--- cr-parser.h	4 Mar 2004 23:35:59 -0000	1.8
+++ cr-parser.h	7 Mar 2004 18:57:16 -0000
@@ -110,6 +110,9 @@
 cr_parser_set_default_sac_handler (CRParser *a_this) ;
 
 enum CRStatus
+cr_parser_parse_term (CRParser *a_this, CRTerm **a_term) ;
+
+enum CRStatus
 cr_parser_parse_expr (CRParser *a_this, CRTerm **a_expr) ;
 
 enum CRStatus
Index: cr-rgb.c
===================================================================
RCS file: /cvs/gnome/libcroco/src/cr-rgb.c,v
retrieving revision 1.11
diff -u -r1.11 cr-rgb.c
--- cr-rgb.c	6 Mar 2004 23:33:10 -0000	1.11
+++ cr-rgb.c	7 Mar 2004 18:57:17 -0000
@@ -27,6 +27,8 @@
 #include <stdio.h>
 #include <string.h>
 #include "cr-rgb.h"
+#include "cr-term.h"
+#include "cr-parser.h"
 
 static CRRgb gv_standard_colors[] = 
 {
@@ -476,6 +478,44 @@
 }
 
 /**
+ *Set the rgb from a terminal symbol
+ * param a_this the instance of #CRRgb to set
+ * param a_value the terminal from which to set
+ */
+enum CRStatus
+cr_rgb_set_from_term (CRRgb *a_this, const struct _CRTerm *a_value)
+{
+        enum CRStatus status = CR_OK ;
+        g_return_val_if_fail (a_this && a_value,
+                              CR_BAD_PARAM_ERROR) ;
+
+	switch(a_value->type) {
+	case TERM_RGB:
+                if (a_value->content.rgb)
+                {
+                        cr_rgb_set_from_rgb
+                                (a_this,
+                                 a_value->content.rgb) ;
+                }
+		break ;
+	case TERM_IDENT:
+	        status = cr_rgb_set_from_name 
+                        (a_this,
+                         a_value->content.str->str) ;
+		break ;
+	case TERM_HASH:
+                status = cr_rgb_set_from_hex_str
+                        (a_this, 
+                         a_value->content.str->str) ;
+                break ;
+	default:
+                status =  CR_UNKNOWN_TYPE_ERROR ;
+	}
+
+        return status ;
+}
+
+/**
  *Destructor of #CRRgb.
  * param a_this the "this pointer" of the
  *current instance of #CRRgb.
@@ -487,3 +527,58 @@
         
         g_free (a_this) ;
 }
+
+/**
+ *Parses a text buffer that contains a rgb color
+ *
+ * param a_str a string that contains a color description
+ * param a_enc the encoding of a_str
+ * return 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(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;
+}
+		  
+	
+	  
+
+	
Index: cr-rgb.h
===================================================================
RCS file: /cvs/gnome/libcroco/src/cr-rgb.h,v
retrieving revision 1.8
diff -u -r1.8 cr-rgb.h
--- cr-rgb.h	6 Mar 2004 23:33:10 -0000	1.8
+++ cr-rgb.h	7 Mar 2004 18:57:17 -0000
@@ -24,13 +24,13 @@
  *$Id: cr-rgb.h,v 1.8 2004/03/06 23:33:10 dodji Exp $
  */
 
+#ifndef __CR_RGB_H__
+#define __CR_RGB_H__
+
 #include <stdio.h>
 #include <glib.h>
 #include "cr-utils.h"
 
-#ifndef __CR_RGB_H__
-#define __CR_RGB_H__
-
 G_BEGIN_DECLS
 
 
@@ -54,6 +54,9 @@
 CRRgb * cr_rgb_new_with_vals (gulong a_red, gulong a_green, 
                               gulong a_blue, gboolean a_is_percentage) ;
 
+CRRgb *cr_rgb_parse_from_buf(const guchar *a_str,
+                             enum CREncoding a_enc);
+
 enum CRStatus cr_rgb_compute_from_percentage (CRRgb *a_this) ;
 
 enum CRStatus cr_rgb_set (CRRgb *a_this, gulong a_red,
@@ -65,6 +68,10 @@
 enum CRStatus cr_rgb_set_from_name (CRRgb *a_this, const guchar *a_color_name) ;
 
 enum CRStatus cr_rgb_set_from_hex_str (CRRgb *a_this, const guchar * a_hex_value) ;
+
+struct _CRTerm;
+
+enum CRStatus cr_rgb_set_from_term (CRRgb *a_this, const struct _CRTerm *a_value);
 
 guchar * cr_rgb_to_string (CRRgb *a_this) ;
 
Index: cr-style.c
===================================================================
RCS file: /cvs/gnome/libcroco/src/cr-style.c,v
retrieving revision 1.21
diff -u -r1.21 cr-style.c
--- cr-style.c	29 Feb 2004 00:18:00 -0000	1.21
+++ cr-style.c	7 Mar 2004 18:57:23 -0000
@@ -279,7 +279,7 @@
 set_prop_width (CRStyle *a_style, CRTerm *a_value) ;
 
 static enum CRStatus
-set_prop_color_rgb (CRStyle *a_style, CRTerm *a_value) ;
+set_prop_color (CRStyle *a_style, CRTerm *a_value) ;
 
 static enum CRStatus
 set_prop_background_color (CRStyle *a_style, CRTerm *a_value) ;
@@ -1158,63 +1158,29 @@
 }
 
 static enum CRStatus
-set_prop_color_rgb (CRStyle *a_style, CRTerm *a_value)
+set_prop_color (CRStyle *a_style, CRTerm *a_value)
 {
-        g_return_val_if_fail (a_style && a_value,
-                              CR_BAD_PARAM_ERROR) ;
+        enum CRStatus status = CR_OK;
+	CRRgb *a_rgb = &a_style->rgb_props[RGB_PROP_COLOR].sv;
+	
+        g_return_val_if_fail (a_style && a_value, CR_BAD_PARAM_ERROR) ;
 
-        if (a_value->type == TERM_RGB)
-        {
-                if (a_value->content.rgb)
-                {
-                        cr_rgb_set_from_rgb
-                                (&a_style->rgb_props[RGB_PROP_COLOR].sv,
-                                 a_value->content.rgb) ;
-                }
+        status = cr_rgb_set_from_term(a_rgb, a_value) ;
 
-        }
-
-        return CR_OK ;
+        return status ;
 }
 
 static enum CRStatus
 set_prop_background_color (CRStyle *a_style, CRTerm *a_value)
 {
-        enum CRStatus status = CR_OK ;
-
-        g_return_val_if_fail (a_style && a_value,
-                              CR_BAD_PARAM_ERROR) ;
+        enum CRStatus status = CR_OK;
+	CRRgb *a_rgb = &a_style->rgb_props[RGB_PROP_BACKGROUND_COLOR].sv;
 
-        switch (a_value->type)
-        {
-        case TERM_RGB:
-                if (a_value->content.rgb)
-                {
-                        status = cr_rgb_set_from_rgb
-                                (&a_style->
-                                 rgb_props[RGB_PROP_BACKGROUND_COLOR].sv,
-                                 a_value->content.rgb) ;
-                }
-                break ;
+        g_return_val_if_fail (a_style && a_value, CR_BAD_PARAM_ERROR);
 
-        case TERM_IDENT:
-                status = cr_rgb_set_from_name 
-                        (&a_style->rgb_props[RGB_PROP_BACKGROUND_COLOR].sv,
-                         a_value->content.str->str) ;
-                break ;
+        status = cr_rgb_set_from_term(a_rgb, a_value);
 
-        case TERM_HASH:
-                status = cr_rgb_set_from_hex_str
-                        (&a_style->rgb_props[RGB_PROP_BACKGROUND_COLOR].sv, 
-                         a_value->content.str->str) ;
-                break ;
-
-        default:
-                status =  CR_UNKNOWN_TYPE_ERROR ;
-                break ;
-        }
-
-        return status ;
+        return status;
 }
 
 /**
@@ -2214,7 +2180,7 @@
                 break ;
 
         case PROP_ID_COLOR:
-                status = set_prop_color_rgb (a_this, value) ;
+                status = set_prop_color (a_this, value) ;
                 break ;
 
         case PROP_ID_BACKGROUND_COLOR:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]