Enabled g_mem_set_vtable through the configure option --with-overridable-allocators...
[mono.git] / eglib / src / gmarkup.c
index 5fcf0a786eb01db8e957048bbb597dd41796616e..4e6c6641fef542ac4fb758a51d27281baf556340 100644 (file)
@@ -40,6 +40,7 @@
  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 #include <stdio.h>
+#include <ctype.h>
 #include <glib.h>
 
 #define set_error(msg, ...) do { if (error != NULL) *error = g_error_new (GINT_TO_POINTER (1), 1, msg, __VA_ARGS__); } while (0);
@@ -99,10 +100,37 @@ g_markup_parse_context_free (GMarkupParseContext *context)
        g_free (context);
 }
 
+static gboolean
+my_isspace (char c)
+{
+       if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\v')
+               return TRUE;
+       return FALSE;
+}
+
+static gboolean
+my_isalnum (char c)
+{
+       if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
+               return TRUE;
+       if (c >= '0' && c <= '9')
+               return TRUE;
+
+       return FALSE;
+}
+
+static gboolean
+my_isalpha (char c)
+{
+       if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
+               return TRUE;
+       return FALSE;
+}
+
 static const char *
 skip_space (const char *p, const char *end)
 {
-       for (; p < end && isspace (*p); p++)
+       for (; p < end && my_isspace (*p); p++)
                ;
        return p;
 }
@@ -124,7 +152,7 @@ parse_value (const char *p, const char *end, char **value, GError **error)
                return end;
        l = (int)(p - start);
        p++;
-       *value = malloc (l + 1);
+       *value = g_malloc (l + 1);
        if (*value == NULL)
                return end;
        strncpy (*value, start, l);
@@ -138,13 +166,13 @@ parse_name (const char *p, const char *end, char **value)
        const char *start = p;
        int l;
        
-       for (; p < end && isalnum (*p); p++)
+       for (; p < end && my_isalnum (*p); p++)
                ;
        if (p == end)
                return end;
 
        l = (int)(p - start);
-       *value = malloc (l + 1);
+       *value = g_malloc (l + 1);
        if (*value == NULL)
                return end;
        strncpy (*value, start, l);
@@ -183,24 +211,24 @@ parse_attributes (const char *p, const char *end, char ***names, char ***values,
 
                        p = skip_space (p, end);
                        if (p == end){
-                               free (name);
+                               g_free (name);
                                return p;
                        }
                        if (*p != '='){
                                set_error ("Expected an = after the attribute name `%s'", name);
-                               free (name);
+                               g_free (name);
                                return end;
                        }
                        p++;
                        p = skip_space (p, end);
                        if (p == end){
-                               free (name);
+                               g_free (name);
                                return end;
                        }
 
                        p = parse_value (p, end, &value, error);
                        if (p == end){
-                               free (name);
+                               g_free (name);
                                return p;
                        }
 
@@ -248,7 +276,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
 
                switch (context->state){
                case START:
-                       if (c == ' ' || c == '\t' || c == '\f' || c == '\n')
+                       if (c == ' ' || c == '\t' || c == '\f' || c == '\n' || (c & 0x80))
                                continue;
                        if (c == '<'){
                                if (p+1 < end && p [1] == '?'){
@@ -268,7 +296,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                        int full_stop = 0, l;
                        gchar **names = NULL, **values = NULL;
 
-                       for (; p < end && isspace (*p); p++)
+                       for (; p < end && my_isspace (*p); p++)
                                ;
                        if (p == end){
                                set_error ("%s", "Unfinished element");
@@ -281,12 +309,12 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                                break;
                        }
                        
-                       if (!(isascii (*p) && isalpha (*p))){
+                       if (!my_isalpha (*p)){
                                set_error ("%s", "Expected an element name");
                                goto fail;
                        }
                        
-                       for (++p; p < end && (isalnum (*p) || (*p == '.')); p++)
+                       for (++p; p < end && (my_isalnum (*p) || (*p == '.')); p++)
                                ;
                        if (p == end){
                                set_error ("%s", "Expected an element");
@@ -294,7 +322,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                        }
                        element_end = p;
                        
-                       for (; p < end && isspace (*p); p++)
+                       for (; p < end && my_isspace (*p); p++)
                                ;
                        if (p == end){
                                set_error ("%s", "Unfinished element");
@@ -312,7 +340,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                                goto fail;
                        }
                        l = (int)(element_end - element_start);
-                       ename = malloc (l + 1);
+                       ename = g_malloc (l + 1);
                        if (ename == NULL)
                                goto fail;
                        strncpy (ename, element_start, l);
@@ -331,7 +359,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                        }
 
                        if (error != NULL && *error != NULL){
-                               free (ename);
+                               g_free (ename);
                                goto fail;
                        }
                        
@@ -339,11 +367,11 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                                if (context->parser.end_element != NULL &&  context->state == START_ELEMENT){
                                        context->parser.end_element (context, ename, context->user_data, error);
                                        if (error != NULL && *error != NULL){
-                                               free (ename);
+                                               g_free (ename);
                                                goto fail;
                                        }
                                }
-                               free (ename);
+                               g_free (ename);
                        } else {
                                context->level = g_slist_prepend (context->level, ename);
                        }
@@ -376,7 +404,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                        break;
                        
                case FLUSH_TEXT:
-                       if (context->parser.text != NULL){
+                       if (context->parser.text != NULL && context->text != NULL){
                                context->parser.text (context, context->text->str, context->text->len,
                                                      context->user_data, error);
                                if (error != NULL && *error != NULL)
@@ -404,11 +432,11 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                        if (context->parser.end_element != NULL){
                                context->parser.end_element (context, text, context->user_data, error);
                                if (error != NULL && *error != NULL){
-                                       free (text);
+                                       g_free (text);
                                        goto fail;
                                }
                        }
-                       free (text);
+                       g_free (text);
 
                        while (p < end && *p != '>')
                                p++;