Back to index

tetex-bin  3.0
trace_buf.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998-2002,2003 Free Software Foundation, Inc.              *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Thomas E. Dickey <dickey@clark.net> 1997                        *
00031  ****************************************************************************/
00032 /*
00033  *     trace_buf.c - Tracing/Debugging buffers (attributes)
00034  */
00035 
00036 #include <curses.priv.h>
00037 
00038 MODULE_ID("$Id: trace_buf.c,v 1.12 2003/03/15 21:21:36 tom Exp $")
00039 
00040 typedef struct {
00041     char *text;
00042     size_t size;
00043 } LIST;
00044 
00045 static char *
00046 _nc_trace_alloc(int bufnum, size_t want)
00047 {
00048     char *result = 0;
00049     static LIST *list;
00050     static size_t have;
00051 
00052     if (bufnum >= 0) {
00053        if ((size_t) (bufnum + 1) > have) {
00054            size_t need = (bufnum + 1) * 2;
00055            if ((list = typeRealloc(LIST, need, list)) == 0)
00056               return (0);
00057            while (need > have)
00058               list[have++].text = 0;
00059        }
00060 
00061        if (list[bufnum].text == 0
00062            || want > list[bufnum].size) {
00063            if ((list[bufnum].text = typeRealloc(char, want, list[bufnum].text))
00064               != 0)
00065                 list[bufnum].size = want;
00066        }
00067 
00068        result = list[bufnum].text;
00069     }
00070 #if NO_LEAKS
00071     else {
00072        if (have) {
00073            while (have--) {
00074               if (list[have].text != 0)
00075                   free(list[have].text);
00076            }
00077            free(list);
00078        }
00079     }
00080 #endif
00081     return result;
00082 }
00083 
00084 /*
00085  * (re)Allocate a buffer big enough for the caller's wants.
00086  */
00087 NCURSES_EXPORT(char *)
00088 _nc_trace_buf(int bufnum, size_t want)
00089 {
00090     char *result = _nc_trace_alloc(bufnum, want);
00091     if (result != 0)
00092        *result = '\0';
00093     return result;
00094 }
00095 
00096 /*
00097  * Append a new string to an existing buffer.
00098  */
00099 NCURSES_EXPORT(char *)
00100 _nc_trace_bufcat(int bufnum, const char *value)
00101 {
00102     char *buffer = _nc_trace_alloc(bufnum, 0);
00103     size_t have = strlen(buffer);
00104 
00105     buffer = _nc_trace_alloc(bufnum, 1 + have + strlen(value));
00106     (void) strcpy(buffer + have, value);
00107 
00108     return buffer;
00109 }