Blame src/buf.c

Packit f00812
/* flex - tool to generate fast lexical analyzers */
Packit f00812
Packit f00812
/*  Copyright (c) 1990 The Regents of the University of California. */
Packit f00812
/*  All rights reserved. */
Packit f00812
Packit f00812
/*  This code is derived from software contributed to Berkeley by */
Packit f00812
/*  Vern Paxson. */
Packit f00812
Packit f00812
/*  The United States Government has rights in this work pursuant */
Packit f00812
/*  to contract no. DE-AC03-76SF00098 between the United States */
Packit f00812
/*  Department of Energy and the University of California. */
Packit f00812
Packit f00812
/*  This file is part of flex. */
Packit f00812
Packit f00812
/*  Redistribution and use in source and binary forms, with or without */
Packit f00812
/*  modification, are permitted provided that the following conditions */
Packit f00812
/*  are met: */
Packit f00812
Packit f00812
/*  1. Redistributions of source code must retain the above copyright */
Packit f00812
/*     notice, this list of conditions and the following disclaimer. */
Packit f00812
/*  2. Redistributions in binary form must reproduce the above copyright */
Packit f00812
/*     notice, this list of conditions and the following disclaimer in the */
Packit f00812
/*     documentation and/or other materials provided with the distribution. */
Packit f00812
Packit f00812
/*  Neither the name of the University nor the names of its contributors */
Packit f00812
/*  may be used to endorse or promote products derived from this software */
Packit f00812
/*  without specific prior written permission. */
Packit f00812
Packit f00812
/*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
Packit f00812
/*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
Packit f00812
/*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
Packit f00812
/*  PURPOSE. */
Packit f00812

Packit f00812
#include "flexdef.h"
Packit f00812
Packit f00812
/* Take note: The buffer object is sometimes used as a String buffer (one
Packit f00812
 * continuous string), and sometimes used as a list of strings, usually line by
Packit f00812
 * line.
Packit f00812
 * 
Packit f00812
 * The type is specified in buf_init by the elt_size. If the elt_size is
Packit f00812
 * sizeof(char), then the buffer should be treated as string buffer. If the
Packit f00812
 * elt_size is sizeof(char*), then the buffer should be treated as a list of
Packit f00812
 * strings.
Packit f00812
 *
Packit f00812
 * Certain functions are only appropriate for one type or the other. 
Packit f00812
 */
Packit f00812
Packit f00812
/* global buffers. */
Packit f00812
struct Buf userdef_buf;		/**< for user #definitions triggered by cmd-line. */
Packit f00812
struct Buf defs_buf;		/**< for #define's autogenerated. List of strings. */
Packit f00812
struct Buf yydmap_buf;		/**< string buffer to hold yydmap elements */
Packit f00812
struct Buf m4defs_buf;          /**< m4 definitions. List of strings. */
Packit f00812
struct Buf top_buf;             /**< contains %top code. String buffer. */
Packit f00812
Packit f00812
struct Buf *buf_print_strings(struct Buf * buf, FILE* out)
Packit f00812
{
Packit f00812
    int i;
Packit f00812
Packit f00812
    if(!buf || !out)
Packit f00812
        return buf;
Packit f00812
Packit f00812
    for (i=0; i < buf->nelts; i++){
Packit f00812
        const char * s = ((char**)buf->elts)[i];
Packit f00812
        if(s)
Packit f00812
            fprintf(out, "%s", s);
Packit f00812
    }
Packit f00812
    return buf;
Packit f00812
}
Packit f00812
Packit f00812
/* Append a "%s" formatted string to a string buffer */
Packit f00812
struct Buf *buf_prints (struct Buf *buf, const char *fmt, const char *s)
Packit f00812
{
Packit f00812
	char   *t;
Packit f00812
        size_t tsz;
Packit f00812
Packit f00812
	tsz = strlen(fmt) + strlen(s) + 1;
Packit f00812
	t = malloc(tsz);
Packit f00812
	if (!t)
Packit f00812
	    flexfatal (_("Allocation of buffer to print string failed"));
Packit f00812
	snprintf (t, tsz, fmt, s);
Packit f00812
	buf = buf_strappend (buf, t);
Packit f00812
	free(t);
Packit f00812
	return buf;
Packit f00812
}
Packit f00812
Packit f00812
/** Append a line directive to the string buffer.
Packit f00812
 * @param buf A string buffer.
Packit f00812
 * @param filename file name
Packit f00812
 * @param lineno line number
Packit f00812
 * @return buf
Packit f00812
 */
Packit f00812
struct Buf *buf_linedir (struct Buf *buf, const char* filename, int lineno)
Packit f00812
{
Packit f00812
    char *dst, *t;
Packit f00812
    const char *src;
Packit f00812
    size_t tsz;
Packit f00812
Packit f00812
    if (gen_line_dirs)
Packit f00812
	return buf;
Packit f00812
Packit f00812
    tsz = strlen("#line \"\"\n")                +   /* constant parts */
Packit f00812
               2 * strlen (filename)            +   /* filename with possibly all backslashes escaped */
Packit f00812
               (int) (1 + log10 (abs (lineno))) +   /* line number */
Packit f00812
               1;                                   /* NUL */
Packit f00812
    t = malloc(tsz);
Packit f00812
    if (!t)
Packit f00812
      flexfatal (_("Allocation of buffer for line directive failed"));
Packit f00812
    for (dst = t + snprintf (t, tsz, "#line %d \"", lineno), src = filename; *src; *dst++ = *src++)
Packit f00812
      if (*src == '\\')   /* escape backslashes */
Packit f00812
        *dst++ = '\\';
Packit f00812
    *dst++ = '"';
Packit f00812
    *dst++ = '\n';
Packit f00812
    *dst   = '\0';
Packit f00812
    buf = buf_strappend (buf, t);
Packit f00812
    free(t);
Packit f00812
    return buf;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/** Append the contents of @a src to @a dest.
Packit f00812
 * @param @a dest the destination buffer
Packit f00812
 * @param @a dest the source buffer
Packit f00812
 * @return @a dest
Packit f00812
 */
Packit f00812
struct Buf *buf_concat(struct Buf* dest, const struct Buf* src)
Packit f00812
{
Packit f00812
    buf_append(dest, src->elts, src->nelts);
Packit f00812
    return dest;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* Appends n characters in str to buf. */
Packit f00812
struct Buf *buf_strnappend (struct Buf *buf, const char *str, int n)
Packit f00812
{
Packit f00812
	buf_append (buf, str, n + 1);
Packit f00812
Packit f00812
	/* "undo" the '\0' character that buf_append() already copied. */
Packit f00812
	buf->nelts--;
Packit f00812
Packit f00812
	return buf;
Packit f00812
}
Packit f00812
Packit f00812
/* Appends characters in str to buf. */
Packit f00812
struct Buf *buf_strappend (struct Buf *buf, const char *str)
Packit f00812
{
Packit f00812
	return buf_strnappend (buf, str, (int) strlen (str));
Packit f00812
}
Packit f00812
Packit f00812
/* appends "#define str def\n" */
Packit f00812
struct Buf *buf_strdefine (struct Buf *buf, const char *str, const char *def)
Packit f00812
{
Packit f00812
	buf_strappend (buf, "#define ");
Packit f00812
	buf_strappend (buf, " ");
Packit f00812
	buf_strappend (buf, str);
Packit f00812
	buf_strappend (buf, " ");
Packit f00812
	buf_strappend (buf, def);
Packit f00812
	buf_strappend (buf, "\n");
Packit f00812
	return buf;
Packit f00812
}
Packit f00812
Packit f00812
/** Pushes "m4_define( [[def]], [[val]])m4_dnl" to end of buffer.
Packit f00812
 * @param buf A buffer as a list of strings.
Packit f00812
 * @param def The m4 symbol to define.
Packit f00812
 * @param val The definition; may be NULL.
Packit f00812
 * @return buf
Packit f00812
 */
Packit f00812
struct Buf *buf_m4_define (struct Buf *buf, const char* def, const char* val)
Packit f00812
{
Packit f00812
    const char * fmt = "m4_define( [[%s]], [[%s]])m4_dnl\n";
Packit f00812
    char * str;
Packit f00812
    size_t strsz;
Packit f00812
Packit f00812
    val = val?val:"";
Packit f00812
    strsz = strlen(fmt) + strlen(def) + strlen(val) + 2;
Packit f00812
    str = malloc(strsz);
Packit f00812
    if (!str)
Packit f00812
        flexfatal (_("Allocation of buffer for m4 def failed"));
Packit f00812
Packit f00812
    snprintf(str, strsz, fmt, def, val);
Packit f00812
    buf_append(buf, &str, 1);
Packit f00812
    return buf;
Packit f00812
}
Packit f00812
Packit f00812
/** Pushes "m4_undefine([[def]])m4_dnl" to end of buffer.
Packit f00812
 * @param buf A buffer as a list of strings.
Packit f00812
 * @param def The m4 symbol to undefine.
Packit f00812
 * @return buf
Packit f00812
 */
Packit f00812
struct Buf *buf_m4_undefine (struct Buf *buf, const char* def)
Packit f00812
{
Packit f00812
    const char * fmt = "m4_undefine( [[%s]])m4_dnl\n";
Packit f00812
    char * str;
Packit f00812
    size_t strsz;
Packit f00812
Packit f00812
    strsz = strlen(fmt) + strlen(def) + 2;
Packit f00812
    str = malloc(strsz);
Packit f00812
    if (!str)
Packit f00812
        flexfatal (_("Allocation of buffer for m4 undef failed"));
Packit f00812
Packit f00812
    snprintf(str, strsz, fmt, def);
Packit f00812
    buf_append(buf, &str, 1);
Packit f00812
    return buf;
Packit f00812
}
Packit f00812
Packit f00812
/* create buf with 0 elements, each of size elem_size. */
Packit f00812
void buf_init (struct Buf *buf, size_t elem_size)
Packit f00812
{
Packit f00812
	buf->elts = NULL;
Packit f00812
	buf->nelts = 0;
Packit f00812
	buf->elt_size = elem_size;
Packit f00812
	buf->nmax = 0;
Packit f00812
}
Packit f00812
Packit f00812
/* frees memory */
Packit f00812
void buf_destroy (struct Buf *buf)
Packit f00812
{
Packit f00812
	if (buf) {
Packit f00812
		free(buf->elts);
Packit f00812
		buf->elts = NULL;
Packit f00812
	}
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* appends ptr[] to buf, grow if necessary.
Packit f00812
 * n_elem is number of elements in ptr[], NOT bytes.
Packit f00812
 * returns buf.
Packit f00812
 * We grow by mod(512) boundaries.
Packit f00812
 */
Packit f00812
Packit f00812
struct Buf *buf_append (struct Buf *buf, const void *ptr, int n_elem)
Packit f00812
{
Packit f00812
	int     n_alloc = 0;
Packit f00812
Packit f00812
	if (!ptr || n_elem == 0)
Packit f00812
		return buf;
Packit f00812
Packit f00812
	/* May need to alloc more. */
Packit f00812
	if (n_elem + buf->nelts > buf->nmax) {
Packit f00812
Packit f00812
		/* exact count needed... */
Packit f00812
		n_alloc = n_elem + buf->nelts;
Packit f00812
Packit f00812
		/* ...plus some extra */
Packit f00812
		if (((n_alloc * buf->elt_size) % 512) != 0
Packit f00812
		    && buf->elt_size < 512)
Packit f00812
			n_alloc +=
Packit f00812
				(512 -
Packit f00812
				 ((n_alloc * buf->elt_size) % 512)) /
Packit f00812
				buf->elt_size;
Packit f00812
Packit f00812
		if (!buf->elts)
Packit f00812
			buf->elts =
Packit f00812
				allocate_array (n_alloc, buf->elt_size);
Packit f00812
		else
Packit f00812
			buf->elts =
Packit f00812
				reallocate_array (buf->elts, n_alloc,
Packit f00812
						  buf->elt_size);
Packit f00812
Packit f00812
		buf->nmax = n_alloc;
Packit f00812
	}
Packit f00812
Packit f00812
	memcpy ((char *) buf->elts + buf->nelts * buf->elt_size, ptr,
Packit f00812
		n_elem * buf->elt_size);
Packit f00812
	buf->nelts += n_elem;
Packit f00812
Packit f00812
	return buf;
Packit f00812
}
Packit f00812
Packit f00812
/* vim:set tabstop=8 softtabstop=4 shiftwidth=4: */