mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Alexander Aring <a.aring@phytec.de>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH 1/2] stringlist-functions: add sorted insert
Date: Mon, 12 Dec 2011 09:48:33 +0100	[thread overview]
Message-ID: <20111212084833.GN27267@pengutronix.de> (raw)
In-Reply-To: <1323424372-8142-2-git-send-email-a.aring@phytec.de>

On Fri, Dec 09, 2011 at 10:52:47AM +0100, Alexander Aring wrote:
> Add sorted insert in stringlist functions.
> Also added function to checked if string is already
> in string list.
> 
> Signed-off-by: Alexander Aring <a.aring@phytec.de>
> ---
>  include/stringlist.h |    2 ++
>  lib/stringlist.c     |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 51 insertions(+), 0 deletions(-)
> 
> diff --git a/include/stringlist.h b/include/stringlist.h
> index 3453e9a..c923542 100644
> --- a/include/stringlist.h
> +++ b/include/stringlist.h
> @@ -9,6 +9,8 @@ struct string_list {
>  };
>  
>  int string_list_add(struct string_list *sl, char *str);
> +int string_list_add_sorted(struct string_list *sl, char *str);
> +int string_list_contains(struct string_list *sl, char *str);
>  void string_list_print_by_column(struct string_list *sl);
>  
>  static inline void string_list_init(struct string_list *sl)
> diff --git a/lib/stringlist.c b/lib/stringlist.c
> index 9ccf8fa..40a0fc9 100644
> --- a/lib/stringlist.c
> +++ b/lib/stringlist.c
> @@ -3,6 +3,29 @@
>  #include <malloc.h>
>  #include <stringlist.h>
>  
> +static int string_list_compare(struct list_head *a, struct list_head *b)
> +{
> +	int result = 0;
> +	char *achr, *bchr;
> +	achr = ((struct string_list *)(a))->str;
> +	bchr = ((struct string_list *)(b))->str;
> +
> +	while (*achr != '\0' || *bchr != '\0') {
> +		result = *achr - *bchr;
> +		if (result < 0)
> +			return -1;
> +		if (result > 0)
> +			return 0;
> +		achr++;
> +		bchr++;
> +	}

Can't you use strcmp here instead?

> +
> +	/* Avoid a endless loop because >=0 insert str in list
> +	 * Insert string if strings are equal or is equal,
> +	 * but lengths are not the same */
> +	return 0;
> +}
> +
>  int string_list_add(struct string_list *sl, char *str)
>  {
>  	struct string_list *new;
> @@ -16,6 +39,31 @@ int string_list_add(struct string_list *sl, char *str)
>  	return 0;
>  }
>  
> +int string_list_add_sorted(struct string_list *sl, char *str)
> +{
> +	struct string_list *new;
> +
> +	new = xmalloc(sizeof(struct string_list) + strlen(str) + 1);
> +
> +	strcpy(new->str, str);
> +
> +	list_add_sort(&new->list, &sl->list, string_list_compare);
> +
> +	return 0;
> +}
> +
> +int string_list_contains(struct string_list *sl, char *str)
> +{
> +	struct string_list *entry;
> +
> +	list_for_each_entry(entry, &sl->list, list) {
> +		if (!strcmp(str, entry->str))
> +			return 1;
> +	}
> +
> +	return 0;
> +}
> +
>  void string_list_print_by_column(struct string_list *sl)
>  {
>  	int len = 0, num, i;
> @@ -44,3 +92,4 @@ void string_list_print_by_column(struct string_list *sl)
>  	if (i % num)
>  		printf("\n");
>  }
> +

Please don't add trailing new lines.

Sascha


-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

       reply	other threads:[~2011-12-12  8:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1323424372-8142-1-git-send-email-a.aring@phytec.de>
     [not found] ` <1323424372-8142-2-git-send-email-a.aring@phytec.de>
2011-12-12  8:48   ` Sascha Hauer [this message]
     [not found] ` <1323424372-8142-3-git-send-email-a.aring@phytec.de>
2011-12-12  9:08   ` [PATCH 2/2] auto-completion: add auto-completion for path files Sascha Hauer
     [not found] ` <1323424372-8142-4-git-send-email-a.aring@phytec.de>
2011-12-12  9:38   ` [PATCH 2/5] twlcore: rename twl4030 to twlcore driver Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20111212084833.GN27267@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=a.aring@phytec.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox