root/libutil/strbuf.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


   1 /*
   2  * Copyright (c) 1997, 1998, 1999, 2000, 2002, 2005, 2006, 2010
   3  *      Tama Communications Corporation
   4  *
   5  * This file is part of GNU GLOBAL.
   6  *
   7  * This program is free software: you can redistribute it and/or modify
   8  * it under the terms of the GNU General Public License as published by
   9  * the Free Software Foundation, either version 3 of the License, or
  10  * (at your option) any later version.
  11  * 
  12  * This program is distributed in the hope that it will be useful,
  13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  * GNU General Public License for more details.
  16  * 
  17  * You should have received a copy of the GNU General Public License
  18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19  */
  20 
  21 #ifndef _STRBUF_H
  22 #define _STRBUF_H
  23 
  24 #include <stdio.h>
  25 #ifdef HAVE_STRING_H
  26 #include <string.h>
  27 #else
  28 #include <strings.h>
  29 #endif
  30 #include <stdarg.h>
  31 
  32 /** @file */
  33 
  34 #ifndef __attribute__
  35 /* This feature is available in gcc versions 2.5 and later.  */
  36 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
  37 #  define __attribute__(x)
  38 # endif
  39 /* The __-protected variants of `format' and `printf' attributes
  40    are accepted by gcc versions 2.6.4 (effectively 2.7) and later.  */
  41 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
  42 #  define __format__ format
  43 #  define __printf__ printf
  44 # endif
  45 #endif
  46 
  47 #define INITIALSIZE 80
  48 #define EXPANDSIZE 80
  49 
  50 /**
  51  * @name Defines for strbuf_fgets()
  52  * See: strbuf_fgets()
  53  */
  54 /** @{ */
  55                         /** append next record to existing data. */
  56 #define STRBUF_APPEND           1
  57                         /** remove last @CODE{'\\n'} and/or @CODE{'\\r'}, if exists. */
  58 #define STRBUF_NOCRLF           2
  59                         /** skip lines which start with @CODE{'\#'} */
  60 #define STRBUF_SHARPSKIP        4
  61 /** @} */
  62 
  63 typedef struct _strbuf {
  64         char *name;
  65         char *sbuf;
  66         char *endp;
  67         char *curp;
  68         int sbufsize;
  69         int alloc_failed;
  70 } STRBUF;
  71 
  72 /**
  73  * STATIC_STRBUF(sb):
  74  *
  75  * This macro is used for static string buffer which is suitable for
  76  * work area and(or) return value of function. The area allocated once
  77  * is repeatedly used though the area is never released. <br>
  78  *
  79  * @attention
  80  * You must call strbuf_clear() every time before using. <br>
  81  * You must @STRONG{not} call strbuf_close() for it.
  82  *
  83  * @par Usage:
  84  * @code
  85  *      function(...) {
  86  *              STATIC_STRBUF(sb);
  87  *
  88  *              strbuf_clear(sb);
  89  *              ...
  90  *              strbuf_puts(sb, "xxxxx");
  91  *              ...
  92  *              return strbuf_value(sb);
  93  *      }
  94  * @endcode
  95  */
  96 #define STATIC_STRBUF(sb) static STRBUF sb[1]
  97 
  98 #define strbuf_empty(sb) (sb->sbufsize == 0)
  99 
 100 #define strbuf_putc(sb, c)      do {\
 101         if (!sb->alloc_failed) {\
 102                 if (sb->curp >= sb->endp)\
 103                         __strbuf_expandbuf(sb, 0);\
 104                 *sb->curp++ = c;\
 105         }\
 106 } while (0)
 107 
 108 #define strbuf_puts0(sb, s) do {\
 109         strbuf_puts(sb, s);\
 110         strbuf_putc(sb, '\0');\
 111 } while (0)
 112 
 113 #define strbuf_getlen(sb) (sb->curp - sb->sbuf)
 114 #define strbuf_setlen(sb, len) do {\
 115         unsigned int _length = len;\
 116         if (!sb->alloc_failed) {\
 117                 if (_length < strbuf_getlen(sb))\
 118                         sb->curp = sb->sbuf + _length;\
 119                 else if (_length > strbuf_getlen(sb))\
 120                         __strbuf_expandbuf(sb, _length - strbuf_getlen(sb));\
 121         }\
 122 } while (0)
 123 #define strbuf_lastchar(sb) (*(sb->curp - 1))
 124 
 125 #ifdef DEBUG
 126 void strbuf_dump(char *);
 127 #endif
 128 void __strbuf_expandbuf(STRBUF *, int);
 129 STRBUF *strbuf_open(int);
 130 void strbuf_reset(STRBUF *);
 131 void strbuf_clear(STRBUF *);
 132 void strbuf_nputs(STRBUF *, const char *, int);
 133 void strbuf_nputc(STRBUF *, int, int);
 134 void strbuf_puts(STRBUF *, const char *);
 135 void strbuf_puts_withterm(STRBUF *, const char *, int);
 136 void strbuf_puts_nl(STRBUF *, const char *);
 137 void strbuf_putn(STRBUF *, int);
 138 int strbuf_unputc(STRBUF *, int);
 139 char *strbuf_value(STRBUF *);
 140 void strbuf_trim(STRBUF *);
 141 void strbuf_close(STRBUF *);
 142 char *strbuf_fgets(STRBUF *, FILE *, int);
 143 void strbuf_sprintf(STRBUF *, const char *, ...)
 144         __attribute__ ((__format__ (__printf__, 2, 3)));
 145 void strbuf_vsprintf(STRBUF *, const char *, va_list)
 146         __attribute__ ((__format__ (__printf__, 2, 0)));
 147 STRBUF *strbuf_open_tempbuf(void);
 148 void strbuf_release_tempbuf(STRBUF *);
 149 
 150 #endif /* ! _STRBUF_H */

/* [previous][next][first][last][top][bottom][index][help] */