/[gnats]/gnats/libiberty/concat.c
ViewVC logotype

Diff of /gnats/libiberty/concat.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1.1.1 by brendan, Thu Nov 5 19:54:15 1998 UTC revision 1.2 by chewie, Sat Nov 13 05:14:17 2004 UTC
# Line 1  Line 1 
1  /* Concatenate variable number of strings.  /* Concatenate variable number of strings.
2     Copyright (C) 1991, 1994 Free Software Foundation, Inc.     Copyright (C) 1991, 1994, 2001 Free Software Foundation, Inc.
3     Written by Fred Fish @ Cygnus Support     Written by Fred Fish @ Cygnus Support
4    
5  This file is part of the libiberty library.  This file is part of the libiberty library.
# Line 21  Boston, MA 02111-1307, USA.  */ Line 21  Boston, MA 02111-1307, USA.  */
21    
22  /*  /*
23    
24  NAME  @deftypefn Extension char* concat (const char *@var{s1}, const char *@var{s2}, @dots{}, @code{NULL})
25    
26          concat -- concatenate a variable number of strings  Concatenate zero or more of strings and return the result in freshly
27    @code{xmalloc}ed memory.  Returns @code{NULL} if insufficient memory is
28    available.  The argument list is terminated by the first @code{NULL}
29    pointer encountered.  Pointers to empty strings are ignored.
30    
31  SYNOPSIS  @end deftypefn
   
         #include <varargs.h>  
   
         char *concat (s1, s2, s3, ..., NULL)  
   
 DESCRIPTION  
   
         Concatenate a variable number of strings and return the result  
         in freshly malloc'd memory.  
   
         Returns NULL if insufficient memory is available.  The argument  
         list is terminated by the first NULL pointer encountered.  Pointers  
         to empty strings are ignored.  
32    
33  NOTES  NOTES
34    
# Line 50  NOTES Line 40  NOTES
40          deal with low memory situations itself, it should supply an xmalloc          deal with low memory situations itself, it should supply an xmalloc
41          that just directly invokes malloc and blindly returns whatever          that just directly invokes malloc and blindly returns whatever
42          malloc returns.          malloc returns.
43    
44  */  */
45    
46    
47    #ifdef HAVE_CONFIG_H
48    #include "config.h"
49    #endif
50  #include "ansidecl.h"  #include "ansidecl.h"
51  #include "libiberty.h"  #include "libiberty.h"
52    #include <sys/types.h>          /* size_t */
53    
54  #ifdef ANSI_PROTOTYPES  #ifdef ANSI_PROTOTYPES
55  #include <stdarg.h>  #include <stdarg.h>
# Line 62  NOTES Line 57  NOTES
57  #include <varargs.h>  #include <varargs.h>
58  #endif  #endif
59    
60  #ifdef __STDC__  # if HAVE_STRING_H
61  #include <stddef.h>  #  include <string.h>
62  extern size_t strlen (const char *s);  # else
63  #else  #  if HAVE_STRINGS_H
64  extern int strlen ();  #   include <strings.h>
65  #endif  #  endif
66    # endif
67    
68    #if HAVE_STDLIB_H
69    #include <stdlib.h>
70    #endif
71    
72    static inline unsigned long vconcat_length PARAMS ((const char *, va_list));
73    static inline unsigned long
74    vconcat_length (first, args)
75         const char *first;
76         va_list args;
77    {
78      unsigned long length = 0;
79      const char *arg;
80    
81  #define NULLP (char *)0    for (arg = first; arg ; arg = va_arg (args, const char *))
82        length += strlen (arg);
83    
84      return length;
85    }
86    
87    static inline char *vconcat_copy PARAMS ((char *, const char *, va_list));
88    static inline char *
89    vconcat_copy (dst, first, args)
90         char *dst;
91         const char *first;
92         va_list args;
93    {
94      char *end = dst;
95      const char *arg;
96    
97      for (arg = first; arg ; arg = va_arg (args, const char *))
98        {
99          unsigned long length = strlen (arg);
100          memcpy (end, arg, length);
101          end += length;
102        }
103      *end = '\000';
104    
105      return dst;
106    }
107    
108    /* @undocumented concat_length */
109    
110    unsigned long
111    concat_length VPARAMS ((const char *first, ...))
112    {
113      unsigned long length;
114    
115      VA_OPEN (args, first);
116      VA_FIXEDARG (args, const char *, first);
117      length = vconcat_length (first, args);
118      VA_CLOSE (args);
119    
120      return length;
121    }
122    
123    /* @undocumented concat_copy */
124    
 /* VARARGS */  
 #ifdef ANSI_PROTOTYPES  
125  char *  char *
126  concat (const char *first, ...)  concat_copy VPARAMS ((char *dst, const char *first, ...))
127  #else  {
128      char *save_dst;
129    
130      VA_OPEN (args, first);
131      VA_FIXEDARG (args, char *, dst);
132      VA_FIXEDARG (args, const char *, first);
133      vconcat_copy (dst, first, args);
134      save_dst = dst; /* With K&R C, dst goes out of scope here.  */
135      VA_CLOSE (args);
136    
137      return save_dst;
138    }
139    
140    char *libiberty_concat_ptr;
141    
142    /* @undocumented concat_copy2 */
143    
144  char *  char *
145  concat (va_alist)  concat_copy2 VPARAMS ((const char *first, ...))
      va_dcl  
 #endif  
146  {  {
147    register int length;    VA_OPEN (args, first);
148    register char *newstr;    VA_FIXEDARG (args, const char *, first);
149    register char *end;    vconcat_copy (libiberty_concat_ptr, first, args);
150    register const char *arg;    VA_CLOSE (args);
   va_list args;  
 #ifndef ANSI_PROTOTYPES  
   const char *first;  
 #endif  
151    
152    /* First compute the size of the result and get sufficient memory. */    return libiberty_concat_ptr;
153    }
154    
155  #ifdef ANSI_PROTOTYPES  char *
156    va_start (args, first);  concat VPARAMS ((const char *first, ...))
157  #else  {
158    va_start (args);    char *newstr;
   first = va_arg (args, const char *);  
 #endif  
159    
160    if (first == NULLP)    /* First compute the size of the result and get sufficient memory.  */
161      length = 0;    VA_OPEN (args, first);
162    else    VA_FIXEDARG (args, const char *, first);
163      {    newstr = (char *) xmalloc (vconcat_length (first, args) + 1);
164        length = strlen (first);    VA_CLOSE (args);
       while ((arg = va_arg (args, const char *)) != NULLP)  
         {  
           length += strlen (arg);  
         }  
     }  
   newstr = (char *) xmalloc (length + 1);  
   va_end (args);  
165    
166    /* Now copy the individual pieces to the result string. */    /* Now copy the individual pieces to the result string. */
167      VA_OPEN (args, first);
168      VA_FIXEDARG (args, const char *, first);
169      vconcat_copy (newstr, first, args);
170      VA_CLOSE (args);
171    
172    if (newstr != NULLP)    return newstr;
173      {  }
174  #ifdef ANSI_PROTOTYPES  
175        va_start (args, first);  /*
176  #else  
177        va_start (args);  @deftypefn Extension char* reconcat (char *@var{optr}, const char *@var{s1}, @dots{}, @code{NULL})
178        first = va_arg (args, const char *);  
179  #endif  Same as @code{concat}, except that if @var{optr} is not @code{NULL} it
180        end = newstr;  is freed after the string is created.  This is intended to be useful
181        if (first != NULLP)  when you're extending an existing string or building up a string in a
182          {  loop:
183            arg = first;  
184            while (*arg)  @example
185              {    str = reconcat (str, "pre-", str, NULL);
186                *end++ = *arg++;  @end example
187              }  
188            while ((arg = va_arg (args, const char *)) != NULLP)  @end deftypefn
189              {  
190                while (*arg)  */
191                  {  
192                    *end++ = *arg++;  char *
193                  }  reconcat VPARAMS ((char *optr, const char *first, ...))
194              }  {
195          }    char *newstr;
       *end = '\000';  
       va_end (args);  
     }  
196    
197    return (newstr);    /* First compute the size of the result and get sufficient memory.  */
198      VA_OPEN (args, first);
199      VA_FIXEDARG (args, char *, optr);
200      VA_FIXEDARG (args, const char *, first);
201      newstr = (char *) xmalloc (vconcat_length (first, args) + 1);
202      VA_CLOSE (args);
203    
204      /* Now copy the individual pieces to the result string. */
205      VA_OPEN (args, first);
206      VA_FIXEDARG (args, char *, optr);
207      VA_FIXEDARG (args, const char *, first);
208      vconcat_copy (newstr, first, args);
209      if (optr) /* Done before VA_CLOSE so optr stays in scope for K&R C.  */
210        free (optr);
211      VA_CLOSE (args);
212    
213      return newstr;
214  }  }
215    
216  #ifdef MAIN  #ifdef MAIN
217    #define NULLP (char *)0
218    
219  /* Simple little test driver. */  /* Simple little test driver. */
220    

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26