home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / auucp+-1.02 / fuucp_plus_src.lzh / sendmail / dys.h < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-21  |  3.1 KB  |  107 lines

  1. /* @(#)dys.h    3.7 8/18/88 01:46:12 */
  2.  
  3. /*
  4.  *    Copyright (C) 1987, 1988 Ronald S. Karr and Landon Curt Noll
  5.  * 
  6.  * See the file COPYING, distributed with smail, for restriction
  7.  * and warranty information.
  8.  *
  9.  * namei master id: @(#)dys.h    3.7 8/18/88 01:46:12
  10.  */
  11.  
  12. /*
  13.  * dys.h:
  14.  *    macros for dynamic string region functions.
  15.  *
  16.  *    The macros in this file provide a simple method of
  17.  *    building strings in an environment where the final
  18.  *    length requirements are not known.  Thus, these
  19.  *    macros automatically support growing of strings
  20.  *    with xrealloc() when the current allocation is deemed
  21.  *    insufficient.
  22.  *
  23.  *    All the macros take, as the first three arguments,
  24.  *    a character pointer, an index and an allocation size.
  25.  *    The index is an offset from the character pointer
  26.  *    to the current location being copied into and the
  27.  *    allocation size is the current limit for the index,
  28.  *    beyond which a call to xrealloc() is required.
  29.  *    The specific macros are described when they are
  30.  *    defined
  31.  */
  32.  
  33. /* type for a dynamic string region */
  34. struct str {
  35.     char *p;                /* xmalloc'd text region */
  36.     unsigned int i;            /* index into text region */
  37.     unsigned int a;            /* current allocation size */
  38. };
  39.  
  40.  
  41. /* STR_CHECK - call X_CHECK (from alloc.h) for a string */
  42. #define STR_CHECK(sp) X_CHECK((sp)->p)
  43.  
  44. /* STR_BUMP - the basic quantum of allocation space */
  45. #define STR_BUMP    64
  46.  
  47. /*
  48.  * STR_INIT - initialize the variables for a dynamic string region
  49.  * this macro should be called with the variables to be passed to
  50.  * other macros in this package before those other macros are used
  51.  */
  52. #define STR_INIT(sp)                    \
  53.     (((sp)->a = STR_BUMP - sizeof(long)),        \
  54.      ((sp)->i = 0),                    \
  55.      ((sp)->p = xmalloc((sp)->a)))
  56.  
  57. /*
  58.  * STR_NEXT - allow access to the next character in a dynamic string
  59.  * region, growing the region if no successor character currently
  60.  * is allocated.  This can be used in the form:
  61.  *
  62.  *    STR_NEXT(p, character expression);
  63.  *
  64.  * to load successive characters into the string.
  65.  */
  66. #define STR_NEXT(sp, c)                 \
  67.     {                        \
  68.         if ((sp)->i >= (sp)->a) {            \
  69.         (sp)->a += STR_BUMP;            \
  70.         (sp)->p = xrealloc((sp)->p, (sp)->a);    \
  71.         }                        \
  72.         (sp)->p[(sp)->i++] = (c);            \
  73.     }
  74.  
  75. /*
  76.  * STR_CAT - concatenate a string onto the end of a dynamic string
  77.  * region, growing as necessary.
  78.  *
  79.  * This is now implemented as a function in string.c.
  80.  */
  81. #define STR_CAT(sp, s)     str_cat((sp), (s))
  82.  
  83. /*
  84.  * STR_DONE - finish building a dynamic string region.  This is not
  85.  * required, though it will xrealloc a region to minimum length, which
  86.  * may be useful if xmalloc and xrealloc call something besides the
  87.  * stock malloc and realloc functions.
  88.  */
  89. #define STR_DONE(sp)    ((sp)->p = xrealloc((sp)->p, (sp)->i),    \
  90.              (sp)->a = (sp)->i)
  91.  
  92. /*
  93.  * STR_FREE - free a region, returning its storage to the free pool
  94.  */
  95. #define STR_FREE(sp)    (free((sp)->p))
  96.  
  97. /*
  98.  * STR_ALIGN - if region index is not aligned add bytes to align it.
  99.  */
  100. #define STR_ALIGN(sp)    { while ((sp)->i%BYTES_PER_ALIGN) STR_NEXT(sp, 0); }
  101.  
  102. /*
  103.  * COPY_STRING - copy a C-style string to a new xmalloc'd region
  104.  */
  105. #define COPY_STRING(s)    (strcpy(xmalloc(strlen(s) + 1), s))
  106.  
  107.