[PATCH] new function to work with few options in one string

Peter Hutterer peter.hutterer at who-t.net
Sun Feb 15 21:17:57 PST 2009


Sorry about the delay here.

On Mon, Jan 19, 2009 at 03:10:45PM +0600, Fedor P. Goncharov wrote:
> From 863b0ab64ab1a9ad3c971112179d13922d0eb388 Mon Sep 17 00:00:00 2001
> From: Fedor P. Goncharov (Fredy) <fedgo at gorodok.net>
> Date: Mon, 22 Dec 2008 03:37:46 +0600
> Subject: [PATCH] New functions for work with substring (Bug#18745).
> 
> New functions added:
>  xf86SetIntSubstrOption
>  xf86SetIntSubstrOption
> You can use them to work with a few options, whitch
> assembled in one string.

I wonder if it is really necessary to add this API, given that the granularity
of the "markUsed" parameter doesn't really allow for it. Also, users of this
API are likely to be using the whole string/all components anyway.
Instead, it might be better to automatically split the string and return an
array instead.


> ---
>  hw/xfree86/common/xf86Opt.h    |    2 +
>  hw/xfree86/common/xf86Option.c |   54 ++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 56 insertions(+), 0 deletions(-)
> 
> diff --git a/hw/xfree86/common/xf86Opt.h b/hw/xfree86/common/xf86Opt.h
> index ce3d767..255f413 100644
> --- a/hw/xfree86/common/xf86Opt.h
> +++ b/hw/xfree86/common/xf86Opt.h
> @@ -71,6 +71,8 @@ typedef struct {
>  extern _X_EXPORT int xf86SetIntOption(pointer optlist, const char *name, int deflt);
>  extern _X_EXPORT double xf86SetRealOption(pointer optlist, const char *name, double deflt);
>  extern _X_EXPORT char *xf86SetStrOption(pointer optlist, const char *name, char *deflt);
> +extern _X_EXPORT char *xf86SetStrSubstrOption(pointer optlist, const char *name, int order, char *deflt);
> +extern _X_EXPORT int   xf86SetIntSubstrOption(pointer optlist, const char *name, int order, int deflt);
>  extern _X_EXPORT int xf86SetBoolOption(pointer list, const char *name, int deflt );
>  extern _X_EXPORT int xf86CheckIntOption(pointer optlist, const char *name, int deflt);
>  extern _X_EXPORT double xf86CheckRealOption(pointer optlist, const char *name, double deflt);
> diff --git a/hw/xfree86/common/xf86Option.c b/hw/xfree86/common/xf86Option.c
> index ad8d1c4..126003c 100644
> --- a/hw/xfree86/common/xf86Option.c
> +++ b/hw/xfree86/common/xf86Option.c
> @@ -210,6 +210,38 @@ LookupStrOption(pointer optlist, const char *name, char *deflt, Bool markUsed)
>  }
>  
>  
> +static char *
> +LookupSubstrOption(pointer optlist, const char *name, int order, Bool markUsed)

If you're trying to add a new API to the server, please document accordingly.
I wasn't sure what you were referring to with "order".

> +{
> +    OptionInfoRec o;
> +    size_t a_size=0, w_size=0;
> +
> +    o.name = name;
> +    o.type = OPTV_STRING;
> +
> +    if (ParseOptionValue(-1, optlist, &o, markUsed)) {
> +      char *str = o.value.str;
> +      /* find substring, which have right order and return it */
> +      while(*str !='\0') {
> +	if(*(str + w_size) != ' ' && *(str + w_size) != '\0')

use isspace() instead of ' '.

> +	  w_size++;
> +	else if(w_size > 0) {
> +	  a_size++;
> +	  if(order == a_size) {
> +	    char* tmp = malloc(sizeof(char) * (w_size + 1));
> +	    strncpy(tmp,str,w_size);
> +	    return tmp;
> +	  }
> +	  str += w_size;
> +	  w_size = 0;
> +	} else
> +	  str++;
> +      }
> +    } 
> +    return NULL;
> +}
> +
> +
>  static int
>  LookupBoolOption(pointer optlist, const char *name, int deflt, Bool markUsed)
>  {
> @@ -245,6 +277,28 @@ xf86SetStrOption(pointer optlist, const char *name, char *deflt)
>  }
>  
>  
> +char *
> +xf86SetStrSubstrOption(pointer optlist, const char *name, int order, char *deflt)
> +{
> +  char *tmp = LookupSubstrOption(optlist, name, order, TRUE);
> +  if(tmp != NULL)
> +    return tmp;
> +  return xstrdup(deflt);
> +}
> +
> +
> +int
> +xf86SetIntSubstrOption(pointer optlist, const char *name, int order, int deflt)
> +{
> +  char *tmp = LookupSubstrOption(optlist, name, order, TRUE);
> +  if(tmp != NULL) {
> +    deflt = atoi(tmp);
> +    free(tmp);
> +  }
> +  return deflt;
> +}
> +
> +
>  int
>  xf86SetBoolOption(pointer optlist, const char *name, int deflt)
>  {
> -- 
> 1.5.6.5

Cheers,
  Peter



More information about the xorg mailing list