]> git.tuebingen.mpg.de Git - adu.git/commitdiff
Make parse_format_string() return int.
authorAndre Noll <maan@systemlinux.org>
Sat, 18 Oct 2008 21:23:46 +0000 (23:23 +0200)
committerAndre Noll <maan@systemlinux.org>
Sat, 18 Oct 2008 21:23:46 +0000 (23:23 +0200)
It's much cleaner to let public functions return a negative
error code on failure.

error.h
format.c
format.h

diff --git a/error.h b/error.h
index 895f9f89bdc68d0a1242afc237fe930fc3db1786..9b0de5ac12a946412a28735ec61dd88a3d9e7142 100644 (file)
--- a/error.h
+++ b/error.h
        _ERROR(OSL, "osl error") \
        _ERROR(SIGNAL_SIG_ERR, "signal() returned SIG_ERR") \
        _ERROR(OUTPUT, "error writing output file") \
        _ERROR(OSL, "osl error") \
        _ERROR(SIGNAL_SIG_ERR, "signal() returned SIG_ERR") \
        _ERROR(OUTPUT, "error writing output file") \
+       _ERROR(MALFORMED_FORMAT, "malformed format string") \
+       _ERROR(BAD_ALIGN_SPEC, "bad alignment specifier") \
+       _ERROR(TRAILING_GARBAGE, "trailing garbage after specifier") \
+       _ERROR(UNIT, "no unit allowed here") \
+       _ERROR(BAD_UNIT, "invalid unit specifier") \
+       _ERROR(BAD_ATOM, "invalid atom") \
 
 
 /**
 
 
 /**
index 3d1daa2aca20fde5f7b993bb01d6b178818f8d3b..48aa37111c2986d6262686723dd766c9f0ed45ef 100644 (file)
--- a/format.c
+++ b/format.c
@@ -95,12 +95,12 @@ static struct format_item *make_const_string(char *cp, char *ep)
        return fi;
 }
 
        return fi;
 }
 
-static struct format_item *parse_atom(char *ap, struct atom *atoms)
+static int parse_atom(char *ap, struct atom *atoms, struct format_item **result)
 {
        struct format_item *fi = NULL;
        int i, n, len;
        char *col, *ep = strchr(ap, ')');
 {
        struct format_item *fi = NULL;
        int i, n, len;
        char *col, *ep = strchr(ap, ')');
-       char *err_msg = "malformed format string";
+       int ret = -E_MALFORMED_FORMAT;
 
        if (!ep)
                goto err;
 
        if (!ep)
                goto err;
@@ -157,7 +157,7 @@ static struct format_item *parse_atom(char *ap, struct atom *atoms)
                        col--;
                        break;
                default:
                        col--;
                        break;
                default:
-                       err_msg = "bad alignment spec";
+                       ret = -E_BAD_ALIGN_SPEC;
                        goto err;
                }
                switch (col[2]) {
                        goto err;
                }
                switch (col[2]) {
@@ -167,7 +167,7 @@ static struct format_item *parse_atom(char *ap, struct atom *atoms)
                        col += 2;
                        break;
                default:
                        col += 2;
                        break;
                default:
-                       err_msg = "trailing garbage after alignment spec";
+                       ret = -E_TRAILING_GARBAGE;
                        goto err;
                }
                /* read width */
                        goto err;
                }
                /* read width */
@@ -181,11 +181,11 @@ static struct format_item *parse_atom(char *ap, struct atom *atoms)
                        col += 1 + n;
                        break;
                default:
                        col += 1 + n;
                        break;
                default:
-                       err_msg = "trailing garbage after width spec";
+                       ret = -E_TRAILING_GARBAGE;
                        goto err;
                }
                if (a->type != AT_SIZE && a->type != AT_COUNT) {
                        goto err;
                }
                if (a->type != AT_SIZE && a->type != AT_COUNT) {
-                       err_msg = "no unit allowed here";
+                       ret = -E_UNIT;
                        goto err;
                }
                if (col[1] == '*') {
                        goto err;
                }
                if (col[1] == '*') {
@@ -199,28 +199,28 @@ static struct format_item *parse_atom(char *ap, struct atom *atoms)
                        break;
                }
                if (!units[j]) {
                        break;
                }
                if (!units[j]) {
-                       err_msg = "bad unit spec";
+                       ret = -E_BAD_UNIT;
                        goto err;
                }
                if (col + 2 != ep) {
                        goto err;
                }
                if (col + 2 != ep) {
-                       err_msg = "trailing garbage after unit spec";
+                       ret = -E_TRAILING_GARBAGE;
                        goto err;
                }
                goto success;
        }
                        goto err;
                }
                goto success;
        }
-       err_msg = "invalid atom";
+       ret = -E_BAD_ATOM;
 err:
 err:
-       ERROR_LOG("%s\n", err_msg);
        free(fi);
        free(fi);
-       return NULL;
+       return ret;
 success:
 success:
-       return fi;
+       *result = fi;
+       return 1;
 }
 
 }
 
-struct format_info *parse_format_string(char *fmt, struct atom *atoms)
+int parse_format_string(char *fmt, struct atom *atoms, struct format_info **result)
 {
        char *cp, *ap, *ep;
 {
        char *cp, *ap, *ep;
-       int num_items;
+       int num_items, ret;
        struct format_info *info = malloc(sizeof(*info));
 
        info->atoms = atoms;
        struct format_info *info = malloc(sizeof(*info));
 
        info->atoms = atoms;
@@ -234,8 +234,8 @@ struct format_info *parse_format_string(char *fmt, struct atom *atoms)
                        num_items++;
                }
                info->items = realloc(info->items, (num_items + 1) * sizeof(struct format_info *));
                        num_items++;
                }
                info->items = realloc(info->items, (num_items + 1) * sizeof(struct format_info *));
-               info->items[num_items] = parse_atom(ap + 2, atoms);
-               if (!info->items[num_items]) {
+               ret = parse_atom(ap + 2, atoms, info->items + num_items);
+               if (ret < 0) {
                        num_items--;
                        goto err;
                }
                        num_items--;
                        goto err;
                }
@@ -249,7 +249,8 @@ struct format_info *parse_format_string(char *fmt, struct atom *atoms)
        }
        info->items = realloc(info->items, (num_items + 1) * sizeof(struct format_info *));
        info->items[num_items] = NULL;
        }
        info->items = realloc(info->items, (num_items + 1) * sizeof(struct format_info *));
        info->items[num_items] = NULL;
-       return info;
+       *result = info;
+       return 1;
 err:
        for (; num_items >= 0; num_items--) {
                if (!info->items[num_items]->atom_ptr)
 err:
        for (; num_items >= 0; num_items--) {
                if (!info->items[num_items]->atom_ptr)
@@ -258,7 +259,7 @@ err:
        }
        free(info->items);
        free(info);
        }
        free(info->items);
        free(info);
-       return NULL;
+       return ret;
 }
 
 void free_format_info(struct format_info *info)
 }
 
 void free_format_info(struct format_info *info)
index 4929bff1c29246389addba390a9ccb9d75e3d73d..2b8b7f20e5999c8b27a44a2c9c8c4640ca3ae81b 100644 (file)
--- a/format.h
+++ b/format.h
@@ -15,6 +15,6 @@ union atom_value {
 };
 
 struct format_info; /* opaque */
 };
 
 struct format_info; /* opaque */
-struct format_info *parse_format_string(char *fmt, struct atom *atoms);
+int parse_format_string(char *fmt, struct atom *atoms, struct format_info **result);
 char *format_items(struct format_info *info, union atom_value *values);
 void free_format_info(struct format_info *info);
 char *format_items(struct format_info *info, union atom_value *values);
 void free_format_info(struct format_info *info);