Set svn:keywords property.
[libfirm] / ir / libcore / lc_printf.c
index f311fae..433e179 100644 (file)
@@ -23,6 +23,7 @@
  * @author Sebastian Hack
  * @date 4.12.2005
  */
+#include "config.h"
 
 #include <stdlib.h>
 #include <stdio.h>
@@ -58,9 +59,9 @@ struct _lc_arg_env_t {
 /** The default argument environment. */
 static lc_arg_env_t *default_env = NULL;
 
-static INLINE lc_arg_env_t *_lc_arg_get_default_env(void)
+static inline lc_arg_env_t *_lc_arg_get_default_env(void)
 {
-       if(!default_env)
+       if (!default_env)
                default_env = lc_arg_add_std(lc_arg_new_env());
 
        return default_env;
@@ -81,8 +82,7 @@ static int lc_arg_cmp(const void *p1, const void *p2, UNUSED(size_t size))
 
 lc_arg_env_t *lc_arg_new_env(void)
 {
-       lc_arg_env_t *env = xmalloc(sizeof(*env));
-       memset(env, 0, sizeof(*env));
+       lc_arg_env_t *env = XMALLOCZ(lc_arg_env_t);
        env->args = new_set(lc_arg_cmp, 16);
        return env;
 }
@@ -104,18 +104,18 @@ int lc_arg_register(lc_arg_env_t *env, const char *name, char letter, const lc_a
        arg.letter = letter;
        arg.handler = handler;
 
-       if(isupper(letter)) {
+       if (isupper(letter)) {
                map = env->upper;
                base = 'A';
        }
-       else if(islower(letter)) {
+       else if (islower(letter)) {
                map = env->lower;
                base = 'a';
        }
 
        ent = set_insert(env->args, &arg, sizeof(arg), HASH_STR(name, strlen(name)));
 
-       if(ent && base != 0)
+       if (ent && base != 0)
                map[letter - base] = ent;
 
        return ent != NULL;
@@ -130,8 +130,8 @@ int lc_arg_append(lc_appendable_t *app, const lc_arg_occ_t *occ, const char *str
        char pad = ' ';
 
        /* Set the padding to zero, if the zero is given and we are not left
-        * justified. (A minus ovverides the zero). See printf(3). */
-       if(!occ->flag_minus && occ->flag_zero)
+        * justified. (A minus overrides the zero). See printf(3). */
+       if (!occ->flag_minus && occ->flag_zero)
                pad = '0';
 
        return lc_appendable_snwadd(app, str, len, LC_MAX(0, occ->width), occ->flag_minus, pad);
@@ -143,7 +143,7 @@ static int dispatch_snprintf(char *buf, size_t len, const char *fmt,
 {
        int res = 0;
 
-       switch(lc_arg_type) {
+       switch (lc_arg_type) {
 #define LC_ARG_TYPE(type,name) \
                case lc_arg_type_ ## name: res = snprintf(buf, len, fmt, val->v_ ## name); break;
 #include "lc_printf_arg_types.def"
@@ -162,10 +162,10 @@ static char *make_fmt(char *buf, size_t len, const lc_arg_occ_t *occ)
        prec[0] = '\0';
        width[0] = '\0';
 
-       if(occ->precision > 0)
+       if (occ->precision > 0)
                snprintf(prec, sizeof(prec), ".%d", occ->precision);
 
-       if(occ->width > 0)
+       if (occ->width > 0)
                snprintf(width, sizeof(width), "%d", occ->width);
 
        assert(occ->modifier && "modifier must not be NULL");
@@ -184,23 +184,20 @@ static char *make_fmt(char *buf, size_t len, const lc_arg_occ_t *occ)
        return buf;
 }
 
-/*
+/**
  * Standard argument handler.
  */
 static int std_get_lc_arg_type(const lc_arg_occ_t *occ)
 {
        int modlen = occ->modifier_length;
-       const char *mod = occ->modifier;
-       int res = -1;
-
 
        /* check, if the type can be derived from the modifier */
-       if(modlen > 0) {
-               switch(mod[0]) {
+       if (modlen > 0) {
+               const char *mod = occ->modifier;
+               switch (mod[0]) {
                        case 'l':
-                               res = modlen > 1 && mod[1] == 'l' ? lc_arg_type_long_long : lc_arg_type_long;
-                               break;
-#define TYPE_CASE(letter,type) case letter: res = lc_arg_type_ ## type; break
+                               return modlen > 1 && mod[1] == 'l' ? lc_arg_type_long_long : lc_arg_type_long;
+#define TYPE_CASE(letter,type) case letter: return lc_arg_type_ ## type;
 #if 0
                        TYPE_CASE('j', intmax_t);
                        TYPE_CASE('z', size_t);
@@ -213,27 +210,21 @@ static int std_get_lc_arg_type(const lc_arg_occ_t *occ)
 
        /* The type is given by the conversion specifier and cannot be
         * determined from the modifier. */
-       if(res == -1) {
-               switch(occ->conversion) {
-                       case 'e':
-                       case 'E':
-                       case 'f':
-                       case 'F':
-                       case 'g':
-                       case 'G':
-                               res = lc_arg_type_double;
-                               break;
-                       case 's':
-                       case 'n':
-                       case 'p':
-                               res = lc_arg_type_ptr;
-                               break;
-                       default:
-                               res = lc_arg_type_int;
-               }
+       switch (occ->conversion) {
+               case 'e':
+               case 'E':
+               case 'f':
+               case 'F':
+               case 'g':
+               case 'G':
+                       return lc_arg_type_double;
+               case 's':
+               case 'n':
+               case 'p':
+                       return lc_arg_type_ptr;
+               default:
+                       return lc_arg_type_int;
        }
-
-       return res;
 }
 
 static int std_emit(lc_appendable_t *app, const lc_arg_occ_t *occ, const lc_arg_value_t *val)
@@ -243,7 +234,7 @@ static int std_emit(lc_appendable_t *app, const lc_arg_occ_t *occ, const lc_arg_
 
        make_fmt(fmt, sizeof(fmt), occ);
 
-       switch(occ->conversion) {
+       switch (occ->conversion) {
 
                /* Store the number of written characters in the given
                 * int pointer location */
@@ -266,10 +257,10 @@ static int std_emit(lc_appendable_t *app, const lc_arg_occ_t *occ, const lc_arg_
                default:
                        {
                                int len = LC_MAX(128, occ->width + 1);
-                               char *buf = malloc(len * sizeof(char));
+                               char *buf = XMALLOCN(char, len);
                                res = dispatch_snprintf(buf, len, fmt, occ->lc_arg_type, val);
                                res = lc_appendable_snadd(app, buf, res);
-                               free(buf);
+                               xfree(buf);
                        }
        }
 
@@ -321,13 +312,13 @@ int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
        const char *s;
        const char *last = fmt + strlen(fmt);
 
-       /* Find the fisrt % */
+       /* Find the first % */
        s = strchr(fmt, '%');
 
        /* Emit the text before the first % was found */
        lc_appendable_snadd(app, fmt, (s ? s : last) - fmt);
 
-       while(s != NULL) {
+       while (s != NULL) {
                lc_arg_occ_t occ;
                lc_arg_value_t val;
                const lc_arg_t *arg = NULL;
@@ -341,8 +332,8 @@ int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
                memset(&occ, 0, sizeof(occ));
 
                /* Eat all flags and set the corresponding flags in the occ struct */
-               for(++s; strchr("#0-+", *s); ++s) {
-                       switch(*s) {
+               for (++s; strchr("#0-+", *s); ++s) {
+                       switch (*s) {
                                case '#':
                                        occ.flag_hash = 1;
                                        break;
@@ -367,7 +358,7 @@ int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
                occ.precision = -1;
 
                /* read the precision if given */
-               if(*s == '.') {
+               if (*s == '.') {
                        int val;
                        s = read_int(s + 1, &val);
 
@@ -383,7 +374,7 @@ int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
                 * - or some other character, which ends this format invalidly
                 */
                ch = *s;
-               switch(ch) {
+               switch (ch) {
                        case '%':
                                s++;
                                res += lc_appendable_chadd(app, '%');
@@ -393,9 +384,9 @@ int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
                                        const char *named = ++s;
 
                                        /* Read until the closing brace or end of the string. */
-                                       for(ch = *s; ch != '}' && ch != '\0'; ch = *++s);
+                                       for (ch = *s; ch != '}' && ch != '\0'; ch = *++s);
 
-                                       if(s - named) {
+                                       if (s - named) {
                                                int n = s - named;
                                                char *name;
                                                lc_arg_t tmp;
@@ -411,14 +402,14 @@ int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
 
                                                /* Set the conversion specifier of the occurrence to the
                                                 * letter specified in the argument description. */
-                                               if(arg)
+                                               if (arg)
                                                        occ.conversion = arg->letter;
 
                                                free(name);
 
                                                /* If we ended with a closing brace, move the current
                                                 * pointer after it, since it is not to be dumped. */
-                                               if(ch == '}')
+                                               if (ch == '}')
                                                        s++;
                                        }
                                }
@@ -429,18 +420,18 @@ int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
                                        const char *mod = s;
 
                                        /* Read, as long there are letters */
-                                       while(isalpha(ch) && !arg) {
+                                       while (isalpha(ch) && !arg) {
                                                int base = 'a';
                                                lc_arg_t * const *map = env->lower;
 
                                                /* If uppercase, select the uppercase map from the environment */
-                                               if(isupper(ch)) {
+                                               if (isupper(ch)) {
                                                        base = 'A';
                                                        map = env->upper;
                                                }
 
-                                               if(map[ch - base] != NULL) {
-                                                       occ.modifier = s;
+                                               if (map[ch - base] != NULL) {
+                                                       occ.modifier = mod;
                                                        occ.modifier_length = s - mod;
                                                        occ.conversion = ch;
                                                        arg = map[ch - base];
@@ -452,7 +443,7 @@ int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
                }
 
                /* Call the handler if an argument was determined */
-               if(arg != NULL && arg->handler != NULL) {
+               if (arg != NULL && arg->handler != NULL) {
                        const lc_arg_handler_t *handler = arg->handler;
 
                        /* Let the handler determine the type of the argument based on the
@@ -460,7 +451,7 @@ int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
                        occ.lc_arg_type = handler->get_lc_arg_type(&occ);
 
                        /* Store the value according to argument information */
-                       switch(occ.lc_arg_type) {
+                       switch (occ.lc_arg_type) {
 #define LC_ARG_TYPE(type,name) case lc_arg_type_ ## name: val.v_ ## name = va_arg(args, type); break;
 #include "lc_printf_arg_types.def"
 #undef LC_ARG_TYPE