Back to index

tetex-bin  3.0
str-list.c
Go to the documentation of this file.
00001 /* str-list.c: define routines for string lists.
00002 
00003 Copyright (C) 1993 Karl Berry.
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Library General Public
00007 License as published by the Free Software Foundation; either
00008 version 2 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Library General Public License for more details.
00014 
00015 You should have received a copy of the GNU Library General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00018 
00019 #include <kpathsea/config.h>
00020 
00021 #include <kpathsea/str-list.h>
00022 
00023 
00024 /* See the .h file for comments.  */
00025 
00026 
00027 str_list_type
00028 str_list_init P1H(void)
00029 {
00030   str_list_type ret;
00031   
00032   STR_LIST_LENGTH (ret) = 0;
00033   STR_LIST (ret) = NULL;
00034   
00035   return ret;
00036 }
00037 
00038 
00039 void
00040 str_list_add P2C(str_list_type *, l,  string, s)
00041 {
00042   STR_LIST_LENGTH (*l)++;
00043   XRETALLOC (STR_LIST (*l), STR_LIST_LENGTH (*l), string);
00044   STR_LIST_LAST_ELT (*l) = s;
00045 }
00046 
00047 
00048 /* May as well save some reallocations and do everything in a chunk
00049    instead of calling str_list_add on each element.  */
00050    
00051 void
00052 str_list_concat P2C(str_list_type *, target,  str_list_type, more)
00053 {
00054   unsigned e;
00055   unsigned prev_len = STR_LIST_LENGTH (*target);
00056 
00057   STR_LIST_LENGTH (*target) += STR_LIST_LENGTH (more);
00058   XRETALLOC (STR_LIST (*target), STR_LIST_LENGTH (*target), string);
00059   
00060   for (e = 0; e < STR_LIST_LENGTH (more); e++)
00061     STR_LIST_ELT (*target, prev_len + e) = STR_LIST_ELT (more, e);
00062 }
00063 
00064 
00065 /* Concatenate the elements of more to each element of target.  This
00066    _must_ be done with the first index varying fastest. */
00067 /* Note that we free the old elements of target as well. */
00068 
00069 void
00070 str_list_concat_elements P2C(str_list_type *, target,  str_list_type, more)
00071 {
00072     if (STR_LIST_LENGTH(more) == 0) {
00073         return;
00074     } else if (STR_LIST_LENGTH(*target) == 0) {
00075         unsigned int i;
00076         STR_LIST_LENGTH(*target) = STR_LIST_LENGTH(more);
00077         STR_LIST(*target) =
00078                 (string*)xmalloc(STR_LIST_LENGTH(more)*sizeof(char*));
00079         for (i=0;i!=STR_LIST_LENGTH(more);++i) {
00080             STR_LIST_ELT(*target,i)=xstrdup(STR_LIST_ELT(more,i));
00081         }
00082         return;
00083     } else {
00084         unsigned new_len;
00085         char ** new_list;
00086         unsigned int i,j;
00087         new_list = (string*)xmalloc(STR_LIST_LENGTH (*target)
00088                                     * STR_LIST_LENGTH (more) * sizeof(char*));
00089 
00090         new_len = 0;
00091         for (j = 0; j != STR_LIST_LENGTH(more); ++j) {
00092             for (i = 0; i != STR_LIST_LENGTH(*target); ++i) {
00093                 new_list[new_len] = concat(STR_LIST_ELT(*target,i),
00094                                            STR_LIST_ELT(more,j));
00095                 ++new_len;
00096             }
00097         }
00098         for (i = 0; i != STR_LIST_LENGTH(*target); ++i)
00099             free(STR_LIST_ELT(*target, i));
00100         free(STR_LIST(*target));
00101         STR_LIST_LENGTH(*target) = new_len;
00102         STR_LIST(*target) = new_list;
00103     }
00104 }
00105 
00106 
00107 /* Free the list (but not the elements within it).  */
00108 
00109 void
00110 str_list_free P1C(str_list_type *, l)
00111 {
00112   if (STR_LIST (*l))
00113     {
00114       free (STR_LIST (*l));
00115       STR_LIST (*l) = NULL;
00116     }
00117 }