source: vendor/flex/2.5.33/buf.c@ 3031

Last change on this file since 3031 was 3031, checked in by bird, 18 years ago

flex 2.5.33.

File size: 6.7 KB
Line 
1/* flex - tool to generate fast lexical analyzers */
2
3/* Copyright (c) 1990 The Regents of the University of California. */
4/* All rights reserved. */
5
6/* This code is derived from software contributed to Berkeley by */
7/* Vern Paxson. */
8
9/* The United States Government has rights in this work pursuant */
10/* to contract no. DE-AC03-76SF00098 between the United States */
11/* Department of Energy and the University of California. */
12
13/* This file is part of flex. */
14
15/* Redistribution and use in source and binary forms, with or without */
16/* modification, are permitted provided that the following conditions */
17/* are met: */
18
19/* 1. Redistributions of source code must retain the above copyright */
20/* notice, this list of conditions and the following disclaimer. */
21/* 2. Redistributions in binary form must reproduce the above copyright */
22/* notice, this list of conditions and the following disclaimer in the */
23/* documentation and/or other materials provided with the distribution. */
24
25/* Neither the name of the University nor the names of its contributors */
26/* may be used to endorse or promote products derived from this software */
27/* without specific prior written permission. */
28
29/* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
30/* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
31/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
32/* PURPOSE. */
33
34
35#include "flexdef.h"
36
37/* Take note: The buffer object is sometimes used as a String buffer (one
38 * continuous string), and sometimes used as a list of strings, usually line by
39 * line.
40 *
41 * The type is specified in buf_init by the elt_size. If the elt_size is
42 * sizeof(char), then the buffer should be treated as string buffer. If the
43 * elt_size is sizeof(char*), then the buffer should be treated as a list of
44 * strings.
45 *
46 * Certain functions are only appropriate for one type or the other.
47 */
48
49/* global buffers. */
50struct Buf userdef_buf; /**< for user #definitions triggered by cmd-line. */
51struct Buf defs_buf; /**< for #define's autogenerated. List of strings. */
52struct Buf yydmap_buf; /**< string buffer to hold yydmap elements */
53struct Buf m4defs_buf; /**< m4 definitions. List of strings. */
54struct Buf top_buf; /**< contains %top code. String buffer. */
55
56struct Buf *buf_print_strings(struct Buf * buf, FILE* out)
57{
58 int i;
59
60 if(!buf || !out)
61 return buf;
62
63 for (i=0; i < buf->nelts; i++){
64 const char * s = ((char**)buf->elts)[i];
65 if(s)
66 fprintf(out, "%s", s);
67 }
68 return buf;
69}
70
71/* Append a "%s" formatted string to a string buffer */
72struct Buf *buf_prints (struct Buf *buf, const char *fmt, const char *s)
73{
74 char *t;
75
76 t = flex_alloc (strlen (fmt) + strlen (s) + 1);
77 sprintf (t, fmt, s);
78 buf = buf_strappend (buf, t);
79 flex_free (t);
80 return buf;
81}
82
83/** Append a line directive to the string buffer.
84 * @param buf A string buffer.
85 * @param filename file name
86 * @param lineno line number
87 * @return buf
88 */
89struct Buf *buf_linedir (struct Buf *buf, const char* filename, int lineno)
90{
91 char *t, *fmt = "#line %d \"%s\"\n";
92
93 t = flex_alloc (strlen (fmt) + strlen (filename) + (int)(1 + log10(lineno>=0?lineno:-lineno)) + 1);
94 sprintf (t, fmt, lineno, filename);
95 buf = buf_strappend (buf, t);
96 flex_free (t);
97 return buf;
98}
99
100
101/** Append the contents of @a src to @a dest.
102 * @param @a dest the destination buffer
103 * @param @a dest the source buffer
104 * @return @a dest
105 */
106struct Buf *buf_concat(struct Buf* dest, const struct Buf* src)
107{
108 buf_append(dest, src->elts, src->nelts);
109 return dest;
110}
111
112
113/* Appends n characters in str to buf. */
114struct Buf *buf_strnappend (buf, str, n)
115 struct Buf *buf;
116 const char *str;
117 int n;
118{
119 buf_append (buf, str, n + 1);
120
121 /* "undo" the '\0' character that buf_append() already copied. */
122 buf->nelts--;
123
124 return buf;
125}
126
127/* Appends characters in str to buf. */
128struct Buf *buf_strappend (buf, str)
129 struct Buf *buf;
130 const char *str;
131{
132 return buf_strnappend (buf, str, strlen (str));
133}
134
135/* appends "#define str def\n" */
136struct Buf *buf_strdefine (buf, str, def)
137 struct Buf *buf;
138 const char *str;
139 const char *def;
140{
141 buf_strappend (buf, "#define ");
142 buf_strappend (buf, " ");
143 buf_strappend (buf, str);
144 buf_strappend (buf, " ");
145 buf_strappend (buf, def);
146 buf_strappend (buf, "\n");
147 return buf;
148}
149
150/** Pushes "m4_define( [[def]], [[val]])m4_dnl" to end of buffer.
151 * @param buf A buffer as a list of strings.
152 * @param def The m4 symbol to define.
153 * @param val The definition; may be NULL.
154 * @return buf
155 */
156struct Buf *buf_m4_define (struct Buf *buf, const char* def, const char* val)
157{
158 const char * fmt = "m4_define( [[%s]], [[%s]])m4_dnl\n";
159 char * str;
160
161 val = val?val:"";
162 str = (char*)flex_alloc(strlen(fmt) + strlen(def) + strlen(val) + 2);
163
164 sprintf(str, fmt, def, val);
165 buf_append(buf, &str, 1);
166 return buf;
167}
168
169/** Pushes "m4_undefine([[def]])m4_dnl" to end of buffer.
170 * @param buf A buffer as a list of strings.
171 * @param def The m4 symbol to undefine.
172 * @return buf
173 */
174struct Buf *buf_m4_undefine (struct Buf *buf, const char* def)
175{
176 const char * fmt = "m4_undefine( [[%s]])m4_dnl\n";
177 char * str;
178
179 str = (char*)flex_alloc(strlen(fmt) + strlen(def) + 2);
180
181 sprintf(str, fmt, def);
182 buf_append(buf, &str, 1);
183 return buf;
184}
185
186/* create buf with 0 elements, each of size elem_size. */
187void buf_init (buf, elem_size)
188 struct Buf *buf;
189 size_t elem_size;
190{
191 buf->elts = (void *) 0;
192 buf->nelts = 0;
193 buf->elt_size = elem_size;
194 buf->nmax = 0;
195}
196
197/* frees memory */
198void buf_destroy (buf)
199 struct Buf *buf;
200{
201 if (buf && buf->elts)
202 flex_free (buf->elts);
203 buf->elts = (void *) 0;
204}
205
206
207/* appends ptr[] to buf, grow if necessary.
208 * n_elem is number of elements in ptr[], NOT bytes.
209 * returns buf.
210 * We grow by mod(512) boundaries.
211 */
212
213struct Buf *buf_append (buf, ptr, n_elem)
214 struct Buf *buf;
215 const void *ptr;
216 int n_elem;
217{
218 int n_alloc = 0;
219
220 if (!ptr || n_elem == 0)
221 return buf;
222
223 /* May need to alloc more. */
224 if (n_elem + buf->nelts > buf->nmax) {
225
226 /* exact amount needed... */
227 n_alloc = (n_elem + buf->nelts) * buf->elt_size;
228
229 /* ...plus some extra */
230 if (((n_alloc * buf->elt_size) % 512) != 0
231 && buf->elt_size < 512)
232 n_alloc +=
233 (512 -
234 ((n_alloc * buf->elt_size) % 512)) /
235 buf->elt_size;
236
237 if (!buf->elts)
238 buf->elts =
239 allocate_array (n_alloc, buf->elt_size);
240 else
241 buf->elts =
242 reallocate_array (buf->elts, n_alloc,
243 buf->elt_size);
244
245 buf->nmax = n_alloc;
246 }
247
248 memcpy ((char *) buf->elts + buf->nelts * buf->elt_size, ptr,
249 n_elem * buf->elt_size);
250 buf->nelts += n_elem;
251
252 return buf;
253}
254
255/* vim:set tabstop=8 softtabstop=4 shiftwidth=4: */
Note: See TracBrowser for help on using the repository browser.