Adjust the C++ preprocessor to include /usr/include/c++ by default for
[dragonfly.git] / contrib / gcc / f / sts.c
1 /* sts.c -- Implementation File (module.c template V1.0)
2    Copyright (C) 1995 Free Software Foundation, Inc.
3    Contributed by James Craig Burley.
4
5 This file is part of GNU Fortran.
6
7 GNU Fortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Fortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Fortran; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21
22    Related Modules:
23       None (despite the name, it doesn't really depend on ffest*)
24
25    Description:
26       Provides an arbitrary-length string facility for the limited needs of
27       GNU Fortran FORMAT statement generation.
28
29    Modifications:
30 */
31
32 /* Include files. */
33
34 #include "proj.h"
35 #include "sts.h"
36 #include "com.h"
37 #include "malloc.h"
38
39 /* Externals defined here. */
40
41
42 /* Simple definitions and enumerations. */
43
44
45 /* Internal typedefs. */
46
47
48 /* Private include files. */
49
50
51 /* Internal structure definitions. */
52
53
54 /* Static objects accessed by functions in this module. */
55
56
57 /* Static functions (internal). */
58
59
60 /* Internal macros. */
61 \f
62
63 /* ffests_kill -- Kill a varying-length string
64
65    ffests s;
66    ffests_kill(s);
67
68    The storage associated with the string <s> is freed.  */
69
70 void
71 ffests_kill (ffests s)
72 {
73   if (s->text_ != NULL)
74     malloc_kill_ksr (s->pool_, s->text_, s->max_);
75 }
76
77 /* ffests_new -- Make a varying-length string
78
79    ffests s;
80    ffests_new(s,malloc_pool_image(),0);
81
82    The string is initialized to hold, in this case, 0 characters, and
83    current and future heap manipulations to hold the string will use
84    the image pool.  */
85
86 void
87 ffests_new (ffests s, mallocPool pool, ffestsLength size)
88 {
89   s->pool_ = pool;
90   s->len_ = 0;
91   s->max_ = size;
92
93   if (size == 0)
94     s->text_ = NULL;
95   else
96     s->text_ = malloc_new_ksr (pool, "ffests", size);
97 }
98
99 /* ffests_printf_1D -- printf("...%ld...",(long)) to a string
100
101    ffests s;
102    ffests_printf_1D(s,"...%ld...",1);
103
104    Like printf, but into a string.  */
105
106 void
107 ffests_printf_1D (ffests s, const char *ctl, long arg1)
108 {
109   char quickbuf[40];
110   char *buff;
111   ffestsLength len;
112
113   if ((len = strlen (ctl) + 21) < ARRAY_SIZE (quickbuf))
114     /* No # bigger than 20 digits. */
115     {
116       sprintf (&quickbuf[0], ctl, arg1);
117       ffests_puttext (s, &quickbuf[0], strlen (quickbuf));
118     }
119   else
120     {
121       buff = malloc_new_ks (malloc_pool_image (), "ffests_printf_1D", len);
122       sprintf (buff, ctl, arg1);
123       ffests_puttext (s, buff, strlen (buff));
124       malloc_kill_ks (malloc_pool_image (), buff, len);
125     }
126 }
127
128 /* ffests_printf_1U -- printf("...%lu...",(unsigned long)) to a string
129
130    ffests s;
131    ffests_printf_1U(s,"...%lu...",1);
132
133    Like printf, but into a string.  */
134
135 void
136 ffests_printf_1U (ffests s, const char *ctl, unsigned long arg1)
137 {
138   char quickbuf[40];
139   char *buff;
140   ffestsLength len;
141
142   if ((len = strlen (ctl) + 21) < ARRAY_SIZE (quickbuf))
143     /* No # bigger than 20 digits. */
144     {
145       sprintf (&quickbuf[0], ctl, arg1);
146       ffests_puttext (s, &quickbuf[0], strlen (quickbuf));
147     }
148   else
149     {
150       buff = malloc_new_ks (malloc_pool_image (), "ffests_printf_1U", len);
151       sprintf (buff, ctl, arg1);
152       ffests_puttext (s, buff, strlen (buff));
153       malloc_kill_ks (malloc_pool_image (), buff, len);
154     }
155 }
156
157 /* ffests_printf_1s -- printf("...%s...",(char *)) to a string
158
159    ffests s;
160    ffests_printf_1s(s,"...%s...","hi there!");
161
162    Like printf, but into a string.  */
163
164 void
165 ffests_printf_1s (ffests s, const char *ctl, const char *arg1)
166 {
167   char quickbuf[40];
168   char *buff;
169   ffestsLength len;
170
171   if ((len = strlen (ctl) + strlen (arg1) - 1) < ARRAY_SIZE (quickbuf))
172     {
173       sprintf (&quickbuf[0], ctl, arg1);
174       ffests_puttext (s, &quickbuf[0], strlen (quickbuf));
175     }
176   else
177     {
178       buff = malloc_new_ks (malloc_pool_image (), "ffests_printf_1s", len);
179       sprintf (buff, ctl, arg1);
180       ffests_puttext (s, buff, strlen (buff));
181       malloc_kill_ks (malloc_pool_image (), buff, len);
182     }
183 }
184
185 /* ffests_printf_2Us -- printf("...%lu...%s...",...) to a string
186
187    ffests s;
188    ffests_printf_2Us(s,"...%lu...%s...",1,"hi there!");
189
190    Like printf, but into a string.  */
191
192 void
193 ffests_printf_2Us (ffests s, const char *ctl, unsigned long arg1, const char *arg2)
194 {
195   char quickbuf[60];
196   char *buff;
197   ffestsLength len;
198
199   if ((len = strlen (ctl) + 21 + strlen (arg2) - 1) < ARRAY_SIZE (quickbuf))
200     /* No # bigger than 20 digits. */
201     {
202       sprintf (&quickbuf[0], ctl, arg1, arg2);
203       ffests_puttext (s, &quickbuf[0], strlen (quickbuf));
204     }
205   else
206     {
207       buff = malloc_new_ks (malloc_pool_image (), "ffests_printf_2Us", len);
208       sprintf (buff, ctl, arg1, arg2);
209       ffests_puttext (s, buff, strlen (buff));
210       malloc_kill_ks (malloc_pool_image (), buff, len);
211     }
212 }
213
214 /* ffests_putc -- Put a single character into string
215
216    ffests s;
217    ffests_putc(s,'*');  */
218
219 void
220 ffests_putc (ffests s, char c)
221 {
222   ffests_puttext (s, &c, 1);
223 }
224
225 /* ffests_puts -- Put a zero-terminated (C-style) string into string
226
227    ffests s;
228    ffests_puts(s,"append me");  */
229
230 void
231 ffests_puts (ffests s, const char *string)
232 {
233   ffests_puttext (s, string, strlen (string));
234 }
235
236 /* ffests_puttext -- Put a number of characters into string
237
238    ffests s;
239    ffests_puttext(s,"hi there",8);
240
241    The string need not be 0-terminated, because the passed length is used,
242    and may be 0.  */
243
244 void
245 ffests_puttext (ffests s, const char *text, ffestsLength length)
246 {
247   ffestsLength newlen;
248   ffestsLength newmax;
249
250   if (length <= 0)
251     return;
252
253   newlen = s->len_ + length;
254   if (newlen > s->max_)
255     {
256       if (s->text_ == NULL)
257         {
258           s->max_ = 40;
259           s->text_ = malloc_new_ksr (s->pool_, "ffests", s->max_);
260         }
261       else
262         {
263           newmax = s->max_ << 1;
264           while (newmax < newlen)
265             newmax <<= 1;
266           s->text_ = malloc_resize_ksr (s->pool_, s->text_, newmax, s->max_);
267           s->max_ = newmax;
268         }
269     }
270
271   memcpy (s->text_ + s->len_, text, length);
272   s->len_ = newlen;
273 }