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

Last change on this file since 3031 was 3031, checked in by bird, 19 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}