On Fri, Mar 11, 2011 at 5:13 PM, Jason Gerecke <killert...@gmail.com> wrote:
> Have it return true/false to indicate conversion success/failure.
> Note that in its current state the function cannot tell if there
> was a conversion failure...
>
> Signed-off-by: Jason Gerecke <killert...@gmail.com>
> ---
>  tools/xsetwacom.c |   47 +++++++++++++++++++++++++++++++++++++++--------
>  1 files changed, 39 insertions(+), 8 deletions(-)
>
> diff --git a/tools/xsetwacom.c b/tools/xsetwacom.c
> index 3435389..4497e72 100644
> --- a/tools/xsetwacom.c
> +++ b/tools/xsetwacom.c
> @@ -1425,18 +1425,29 @@ error:
>        return;
>  }
>
> -static int convert_value_from_user(param_t *param, char *value)
> -{
> -       int val;
>
> +/**
> + * Performs intelligent string->int conversion. In addition to converting 
> strings
> + * of digits into their corresponding integer values, it converts special 
> string
> + * constants such as "off" (0) and "on" (1).
> + *
> + * The caller is expected to allocate and free memory for return_value.
> + *
> + * @param      param        the property paramaters
> + * @param      value        the string to be converted
> + * @param[out] return_value the integer representation of the 'value' 
> parameter
> + * @return TRUE if the conversion succeeded, FALSE otherwise
> + */
> +static Bool convert_value_from_user(param_t *param, char *value, int 
> *return_value)
> +{
>        if ((param->prop_flags & PROP_FLAG_BOOLEAN) && strcmp(value, "off") == 
> 0)
> -                       val = 0;
> +                       *return_value = 0;
>        else if ((param->prop_flags & PROP_FLAG_BOOLEAN) && strcmp(value, 
> "on") == 0)
> -                       val = 1;
> +                       *return_value = 1;
>        else
> -               val = atoi(value);
> +               *return_value = atoi(value);
>
> -       return val;
> +       return True;
>  }
>
>  static void set(Display *dpy, int argc, char **argv)
> @@ -1512,7 +1523,27 @@ static void set(Display *dpy, int argc, char **argv)
>
>        for (i = 0; i < nvals; i++)
>        {
> -               val = convert_value_from_user(param, values[i]);
> +               Bool success;
> +               int val;
> +               int *retval;
> +
> +               retval = calloc(1, sizeof(int));
> +               if (!retval)
> +               {
> +                       fprintf(stderr, "calloc failed\n");
> +                       exit(EXIT_FAILURE);
> +               }

Why the trouble of alloc'ing an int?  Just pass in &retval.

Chris

> +
> +               success = convert_value_from_user(param, values[i], retval);
> +               val = *retval;
> +               free(retval);
> +
> +               if (!success)
> +               {
> +                       fprintf(stderr, "'%s' is not a valid value for the 
> '%s' property.\n",
> +                               values[i], param->name);
> +                       goto out;
> +               }
>
>                switch(param->prop_format)
>                {
> --
> 1.7.1
>
>
> ------------------------------------------------------------------------------
> Colocation vs. Managed Hosting
> A question and answer guide to determining the best fit
> for your organization - today and in the future.
> http://p.sf.net/sfu/internap-sfd2d
> _______________________________________________
> Linuxwacom-devel mailing list
> Linuxwacom-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linuxwacom-devel
>

------------------------------------------------------------------------------
Colocation vs. Managed Hosting
A question and answer guide to determining the best fit
for your organization - today and in the future.
http://p.sf.net/sfu/internap-sfd2d
_______________________________________________
Linuxwacom-devel mailing list
Linuxwacom-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxwacom-devel

Reply via email to