Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

mbuf.c

Go to the documentation of this file.
00001 /*****************************************************************************\
00002  *  Copyright  1994  The Board of Trustees of the University of Illinois.
00003  *  All rights reserved.
00004  *
00005  *  mbuf.c, part of the IMPACT in-memory file interface
00006  *  John C. Gyllenhaal, Wen-mei Hwu, and The IMPACT Research Group
00007  *  Coordinated Science Laboratory
00008  *  University of Illinois at Urbana-Champaign
00009  *
00010  *  The IMPACT Research Group may be contacted at impact@crhc.uiuc.edu.
00011  *
00012  *  mbuf.c (part of the IMPACT in-memory file interface),
00013  *  including both binary and source (hereafter, Software) is copyrighted by
00014  *  The Board of Trustees of the University of Illinois (UI),
00015  *  and ownership remains with the UI.
00016  *
00017  *  The UI grants you (hereafter, Licensee) a license to use the Software for
00018  *  academic, research and internal business purposes only, without a fee.
00019  *  Licensee may distribute the Software to third parties provided that the
00020  *  copyright notice and this statement appears on all copies and that no
00021  *  charge is associated with such copies.
00022  *
00023  *  Licensee may make derivative works.  However, if Licensee distributes
00024  *  any derivative work based on or derived from the Software, then Licensee
00025  *  will clearly notify users that such derivative work is a modified
00026  *  version and not the original Software distributed by the UI.
00027  *
00028  *  Any Licensee wishing to make commercial use of the Software should contact
00029  *  the UI, c/o the Research and Technology Management Office [rtmo@uiuc.edu],
00030  *  to negotiate an appropriate license for such commercial use.  Commercial
00031  *  use includes (1) integration of all or part of the source code into a
00032  *  product for sale or license by or on behalf of Licensee to third parties,
00033  *  or (2) distribution of the Software to third parties that need it to
00034  *  utilize a commercial product sold or licensed by or on behalf of Licensee.
00035  *
00036  *  UI MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR
00037  *  ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
00038  *  THE UI SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY THE USERS OF THIS
00039  *  SOFTWARE.
00040  *
00041  *  By using or copying this Software, Licensee agrees to abide by the
00042  *  copyright law and all other applicable laws of the U.S. including, but
00043  *  not limited to, export control laws, and the terms of this license.
00044  *  UI shall have the right to terminate this license immediately by written
00045  *  notice upon Licensee's breach of, or non-compliance with, any of its terms.
00046  *  Licensee may be held legally responsible for any copyright infringement
00047  *  that is caused or encouraged by Licensee's failure to abide by the terms
00048  *  of this license.
00049  *
00050  *  Form approved by University Counsel, M.A.R., 05/10/93
00051 \*****************************************************************************/
00052 /*****************************************************************************\
00053  *
00054  *  File:  mbuf.c
00055  *
00056  *  Description:  Self-resizing buffer useful for reading in files
00057  *
00058  *  Creation Date :  October 1994
00059  *
00060  *  Authors:  John C. Gyllenhaal, Wen-mei Hwu
00061  *
00062  *  Revisions:
00063  *
00064 \*****************************************************************************/
00065 
00066 #ifndef lint
00067 #define lint
00068 static char copyright[] =
00069 "@(#) Copyright (c) 1994 The Board of Trustees of the University of Illinois.\
00070 \nAll rights reserved.\n";
00071 #endif
00072 
00073 #include <stdio.h>
00074 #include <stdlib.h>
00075 #include <string.h>
00076 #include <ctype.h>
00077 
00078 #include "mbuf.h"
00079 #include "l_punt.h"
00080 
00081 /* Declare external functions that must be defined by user */
00082 void L_punt (char *msg, ...);
00083 
00084 L_Alloc_Pool *Mbuf_pool = NULL;
00085 
00086 Mbuf *create_Mbuf()
00087 {
00088     Mbuf *mbuf;
00089 
00090     /* Get memory for mbuf, alloc pool on first use */
00091     if (Mbuf_pool == NULL)
00092     {
00093         Mbuf_pool = L_create_alloc_pool ("Mbuf", sizeof (Mbuf), 16);
00094     }
00095     mbuf = (Mbuf *) L_alloc (Mbuf_pool);
00096 
00097     /* Initialize fields */
00098     mbuf->buf = NULL;
00099 
00100     clear_Mbuf (mbuf);
00101 
00102     /* Return mbuf ready to go */
00103     return (mbuf);
00104 }
00105 
00106 void free_Mbuf(Mbuf *mbuf)
00107 {
00108     /* Free buffer, then mbuf */
00109     free (mbuf->buf);
00110     L_free (Mbuf_pool, mbuf);
00111 }
00112 
00113 
00114 void clear_Mbuf(Mbuf *mbuf)
00115 {
00116     /* If never initialized before, malloc initial buffer */
00117     if (mbuf->buf == NULL)
00118     {
00119         mbuf->max_len = 2;
00120         if ((mbuf->buf = (char *) malloc (mbuf->max_len)) == NULL)
00121             L_punt ("Out of memory in clear_Mbuf");
00122     }
00123 
00124     /* Clear out buffer */
00125     mbuf->cur_len = 0;
00126     mbuf->buf[0] = 0;
00127 }
00128 
00129 void addc_to_Mbuf(Mbuf *mbuf, char ch)
00130 {
00131     char *old_buf;
00132 
00133     /* If at end of current buffer, malloc new one and copy over
00134      * contents.
00135      */
00136     if ((mbuf->cur_len + 1) >= mbuf->max_len)
00137     {
00138         old_buf = mbuf->buf;
00139 
00140         /* Double size of buffer */
00141         mbuf->max_len = mbuf->max_len * 2;
00142 
00143         /* Malloc new buffer */
00144         if ((mbuf->buf = (char *) malloc (mbuf->max_len)) == NULL)
00145             L_punt ("Out of memory in addc_to_Mbuf");
00146 
00147         /* Copy over old buffer */
00148         strcpy (mbuf->buf, old_buf);
00149 
00150         /* Free old buffer */
00151         free (old_buf);
00152    }
00153 
00154     /* Add character to buf (and terminate) */
00155     mbuf->buf[mbuf->cur_len] = ch;
00156     mbuf->buf[mbuf->cur_len + 1] = 0;
00157     mbuf->cur_len++;
00158 }
00159 
00160 void adds_to_Mbuf (Mbuf *mbuf, char *str)
00161 {
00162     char *ptr;
00163 
00164     for (ptr = str; *ptr != 0; ptr++)
00165         addc_to_Mbuf (mbuf, *ptr);
00166 }
00167 
00168 /* Pads buffer until length specified.  Does nothing if aleady longer 
00169  * than desired length
00170  */
00171 void pad_Mbuf_to_len (Mbuf *mbuf, int desired_len, char pad_ch)
00172 {
00173     while (desired_len > mbuf->cur_len)
00174         addc_to_Mbuf (mbuf, pad_ch);
00175 }
00176 
00177 /* Strips trailing whitespace from mbuf */
00178 void strip_Mbuf (Mbuf *mbuf)
00179 {
00180     int i;
00181 
00182     /* Find last non-whitespace */
00183     for (i=mbuf->cur_len - 1; i>= 0; i--)
00184     {
00185         if (!isspace(mbuf->buf[i]))
00186             break;
00187     }
00188 
00189     /* Adjust length and terminator */
00190     mbuf->cur_len = i + 1;
00191     mbuf->buf[i+1] = 0;
00192 }
00193 
00194 /* return's a pointer to the actual mbuf (must not write to or free) */
00195 char *get_Mbuf_buf (Mbuf *mbuf)
00196 {
00197     return (mbuf->buf);
00198 }
00199 
00200 /* Copy mbuf's contents, may have NULLs in string so use len info */
00201 char *copy_Mbuf_buf(Mbuf *mbuf)
00202 {
00203     char *buf, *src_buf;
00204     int i, len;
00205 
00206     len = mbuf->cur_len;
00207 
00208     if ((buf = (char *) malloc (len + 1)) == NULL)
00209         L_punt ("copy_Mbuf_buf: Out of memory");
00210 
00211     src_buf = mbuf->buf;
00212     for (i=0; i < len; i++)
00213         buf[i] = src_buf[i];
00214 
00215     /* Terminate string */
00216     buf[len] = 0;
00217 
00218     return (buf);
00219 }
00220 
00221 
00222 /* Returns length of mbuf */
00223 int len_of_Mbuf (Mbuf *mbuf)
00224 {
00225     return (mbuf->cur_len);
00226 }
00227 
00228 /* Returns 1 if string matches mbuf, 0 otherwise */
00229 int match_Mbuf (Mbuf *mbuf, char *string)
00230 {
00231     if (strcmp (mbuf->buf, string) == 0)
00232         return (1);
00233     else
00234         return (0);
00235 }

Generated on Mon Jul 21 20:28:00 2003 for TINKER LEGO DOC by doxygen 1.3.2