LCOV - code coverage report
Current view: top level - common - strlist.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 30 82 36.6 %
Date: 2015-11-05 17:10:59 Functions: 6 12 50.0 %

          Line data    Source code
       1             : /* strlist.c -  string helpers
       2             :  * Copyright (C) 1998, 2000, 2001, 2006 Free Software Foundation, Inc.
       3             :  * Copyright (C) 2015  g10 Code GmbH
       4             :  *
       5             :  * This file is part of GnuPG.
       6             :  *
       7             :  * GnuPG is free software; you can redistribute it and/or modify it
       8             :  * under the terms of either
       9             :  *
      10             :  *   - the GNU Lesser General Public License as published by the Free
      11             :  *     Software Foundation; either version 3 of the License, or (at
      12             :  *     your option) any later version.
      13             :  *
      14             :  * or
      15             :  *
      16             :  *   - the GNU General Public License as published by the Free
      17             :  *     Software Foundation; either version 2 of the License, or (at
      18             :  *     your option) any later version.
      19             :  *
      20             :  * or both in parallel, as here.
      21             :  *
      22             :  * GnuPG is distributed in the hope that it will be useful, but
      23             :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      24             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      25             :  * General Public License for more details.
      26             :  *
      27             :  * You should have received a copies of the GNU General Public License
      28             :  * and the GNU Lesser General Public License along with this program;
      29             :  * if not, see <http://www.gnu.org/licenses/>.
      30             :  */
      31             : 
      32             : #include <config.h>
      33             : #include <stdlib.h>
      34             : #include <string.h>
      35             : #include <stdarg.h>
      36             : #include <ctype.h>
      37             : 
      38             : #include "util.h"
      39             : #include "common-defs.h"
      40             : #include "strlist.h"
      41             : #include "utf8conv.h"
      42             : 
      43             : void
      44        4857 : free_strlist( strlist_t sl )
      45             : {
      46             :     strlist_t sl2;
      47             : 
      48        6168 :     for(; sl; sl = sl2 ) {
      49        1311 :         sl2 = sl->next;
      50        1311 :         xfree(sl);
      51             :     }
      52        4857 : }
      53             : 
      54             : 
      55             : /* Add STRING to the LIST at the front.  This function terminates the
      56             :    process on memory shortage.  */
      57             : strlist_t
      58        1229 : add_to_strlist( strlist_t *list, const char *string )
      59             : {
      60             :     strlist_t sl;
      61             : 
      62        1229 :     sl = xmalloc( sizeof *sl + strlen(string));
      63        1229 :     sl->flags = 0;
      64        1229 :     strcpy(sl->d, string);
      65        1229 :     sl->next = *list;
      66        1229 :     *list = sl;
      67        1229 :     return sl;
      68             : }
      69             : 
      70             : 
      71             : /* Add STRING to the LIST at the front.  This function returns NULL
      72             :    and sets ERRNO on memory shortage.  */
      73             : strlist_t
      74           0 : add_to_strlist_try (strlist_t *list, const char *string)
      75             : {
      76             :   strlist_t sl;
      77             : 
      78           0 :   sl = xtrymalloc (sizeof *sl + strlen (string));
      79           0 :   if (sl)
      80             :     {
      81           0 :       sl->flags = 0;
      82           0 :       strcpy (sl->d, string);
      83           0 :       sl->next = *list;
      84           0 :       *list = sl;
      85             :     }
      86           0 :   return sl;
      87             : }
      88             : 
      89             : 
      90             : /* Same as add_to_strlist() but if IS_UTF8 is *not* set, a conversion
      91             :    to UTF-8 is done.  This function terminates the process on memory
      92             :    shortage.  */
      93             : strlist_t
      94         378 : add_to_strlist2( strlist_t *list, const char *string, int is_utf8 )
      95             : {
      96             :   strlist_t sl;
      97             : 
      98         378 :   if (is_utf8)
      99           0 :     sl = add_to_strlist( list, string );
     100             :   else
     101             :     {
     102         378 :       char *p = native_to_utf8( string );
     103         378 :       sl = add_to_strlist( list, p );
     104         378 :       xfree ( p );
     105             :     }
     106         378 :   return sl;
     107             : }
     108             : 
     109             : 
     110             : /* Add STRING to the LIST at the end.  This function terminates the
     111             :    process on memory shortage.  */
     112             : strlist_t
     113           0 : append_to_strlist( strlist_t *list, const char *string )
     114             : {
     115             :     strlist_t r, sl;
     116             : 
     117           0 :     sl = xmalloc( sizeof *sl + strlen(string));
     118           0 :     sl->flags = 0;
     119           0 :     strcpy(sl->d, string);
     120           0 :     sl->next = NULL;
     121           0 :     if( !*list )
     122           0 :         *list = sl;
     123             :     else {
     124           0 :         for( r = *list; r->next; r = r->next )
     125             :             ;
     126           0 :         r->next = sl;
     127             :     }
     128           0 :     return sl;
     129             : }
     130             : 
     131             : 
     132             : strlist_t
     133           0 : append_to_strlist2( strlist_t *list, const char *string, int is_utf8 )
     134             : {
     135             :   strlist_t sl;
     136             : 
     137           0 :   if( is_utf8 )
     138           0 :     sl = append_to_strlist( list, string );
     139             :   else
     140             :     {
     141           0 :       char *p = native_to_utf8 (string);
     142           0 :       sl = append_to_strlist( list, p );
     143           0 :       xfree( p );
     144             :     }
     145           0 :   return sl;
     146             : }
     147             : 
     148             : 
     149             : /* Return a copy of LIST.  This function terminates the process on
     150             :    memory shortage.*/
     151             : strlist_t
     152           0 : strlist_copy (strlist_t list)
     153             : {
     154           0 :   strlist_t newlist = NULL, sl, *last;
     155             : 
     156           0 :   last = &newlist;
     157           0 :   for (; list; list = list->next)
     158             :     {
     159           0 :       sl = xmalloc (sizeof *sl + strlen (list->d));
     160           0 :       sl->flags = list->flags;
     161           0 :       strcpy(sl->d, list->d);
     162           0 :       sl->next = NULL;
     163           0 :       *last = sl;
     164           0 :       last = &sl;
     165             :     }
     166           0 :   return newlist;
     167             : }
     168             : 
     169             : 
     170             : 
     171             : strlist_t
     172          14 : strlist_prev( strlist_t head, strlist_t node )
     173             : {
     174             :     strlist_t n;
     175             : 
     176          56 :     for(n=NULL; head && head != node; head = head->next )
     177          42 :         n = head;
     178          14 :     return n;
     179             : }
     180             : 
     181             : strlist_t
     182           2 : strlist_last( strlist_t node )
     183             : {
     184           2 :     if( node )
     185           2 :         for( ; node->next ; node = node->next )
     186             :             ;
     187           2 :     return node;
     188             : }
     189             : 
     190             : 
     191             : /* Remove the first item from LIST and return its content in an
     192             :    allocated buffer.  This function terminates the process on memory
     193             :    shortage.  */
     194             : char *
     195           0 : strlist_pop (strlist_t *list)
     196             : {
     197           0 :   char *str=NULL;
     198           0 :   strlist_t sl=*list;
     199             : 
     200           0 :   if(sl)
     201             :     {
     202           0 :       str = xmalloc(strlen(sl->d)+1);
     203           0 :       strcpy(str,sl->d);
     204             : 
     205           0 :       *list=sl->next;
     206           0 :       xfree(sl);
     207             :     }
     208             : 
     209           0 :   return str;
     210             : }
     211             : 
     212             : /* Return the first element of the string list HAYSTACK whose string
     213             :    matches NEEDLE.  If no elements match, return NULL.  */
     214             : strlist_t
     215           0 : strlist_find (strlist_t haystack, const char *needle)
     216             : {
     217           0 :   for (;
     218             :        haystack;
     219           0 :        haystack = haystack->next)
     220           0 :     if (strcmp (haystack->d, needle) == 0)
     221           0 :       return haystack;
     222           0 :   return NULL;
     223             : }
     224             : 
     225             : int
     226         534 : strlist_length (strlist_t list)
     227             : {
     228             :   int i;
     229        1590 :   for (i = 0; list; list = list->next)
     230        1056 :     i ++;
     231             : 
     232         534 :   return i;
     233             : }

Generated by: LCOV version 1.11