| 1 |
/* |
| 2 |
* $Id: vstring.h 719 2009-07-07 03:46:59Z dhiebert $ |
| 3 |
* |
| 4 |
* Copyright (c) 1998-2002, Darren Hiebert |
| 5 |
* |
| 6 |
* This source code is released for free distribution under the terms of the |
| 7 |
* GNU General Public License. |
| 8 |
* |
| 9 |
* Provides the external interface for resizeable strings. |
| 10 |
*/ |
| 11 |
#ifndef _VSTRING_H |
| 12 |
#define _VSTRING_H |
| 13 |
|
| 14 |
/* |
| 15 |
* INCLUDE FILES |
| 16 |
*/ |
| 17 |
#include "general.h" /* must always come first */ |
| 18 |
|
| 19 |
#if defined(HAVE_STDLIB_H) |
| 20 |
# include <stdlib.h> /* to define size_t */ |
| 21 |
#endif |
| 22 |
|
| 23 |
/* |
| 24 |
* MACROS |
| 25 |
*/ |
| 26 |
#ifndef DEBUG |
| 27 |
# define VSTRING_PUTC_MACRO 1 |
| 28 |
#endif |
| 29 |
#ifdef VSTRING_PUTC_MACRO |
| 30 |
#define vStringPut(s,c) \ |
| 31 |
(void)(((s)->length + 1 == (s)->size ? vStringAutoResize (s) : 0), \ |
| 32 |
((s)->buffer [(s)->length] = (c)), \ |
| 33 |
((c) == '\0' ? 0 : ((s)->buffer [++(s)->length] = '\0'))) |
| 34 |
#endif |
| 35 |
|
| 36 |
#define vStringValue(vs) ((vs)->buffer) |
| 37 |
#define vStringItem(vs,i) ((vs)->buffer[i]) |
| 38 |
#define vStringLast(vs) ((vs)->buffer[(vs)->length - 1]) |
| 39 |
#define vStringLength(vs) ((vs)->length) |
| 40 |
#define vStringSize(vs) ((vs)->size) |
| 41 |
#define vStringCat(vs,s) vStringCatS((vs), vStringValue((s))) |
| 42 |
#define vStringNCat(vs,s,l) vStringNCatS((vs), vStringValue((s)), (l)) |
| 43 |
#define vStringCopy(vs,s) vStringCopyS((vs), vStringValue((s))) |
| 44 |
#define vStringNCopy(vs,s,l) vStringNCopyS((vs), vStringValue((s)), (l)) |
| 45 |
#define vStringChar(vs,i) ((vs)->buffer[i]) |
| 46 |
#define vStringTerminate(vs) vStringPut(vs, '\0') |
| 47 |
#define vStringLower(vs) toLowerString((vs)->buffer) |
| 48 |
#define vStringUpper(vs) toUpperString((vs)->buffer) |
| 49 |
|
| 50 |
/* |
| 51 |
* DATA DECLARATIONS |
| 52 |
*/ |
| 53 |
|
| 54 |
typedef struct sVString { |
| 55 |
size_t length; /* size of buffer used */ |
| 56 |
size_t size; /* allocated size of buffer */ |
| 57 |
char *buffer; /* location of buffer */ |
| 58 |
} vString; |
| 59 |
|
| 60 |
/* |
| 61 |
* FUNCTION PROTOTYPES |
| 62 |
*/ |
| 63 |
extern boolean vStringAutoResize (vString *const string); |
| 64 |
extern void vStringClear (vString *const string); |
| 65 |
extern vString *vStringNew (void); |
| 66 |
extern void vStringDelete (vString *const string); |
| 67 |
#ifndef VSTRING_PUTC_MACRO |
| 68 |
extern void vStringPut (vString *const string, const int c); |
| 69 |
#endif |
| 70 |
extern void vStringStripNewline (vString *const string); |
| 71 |
extern void vStringStripLeading (vString *const string); |
| 72 |
extern void vStringChop (vString *const string); |
| 73 |
extern void vStringStripTrailing (vString *const string); |
| 74 |
extern void vStringCatS (vString *const string, const char *const s); |
| 75 |
extern void vStringNCatS (vString *const string, const char *const s, const size_t length); |
| 76 |
extern vString *vStringNewCopy (const vString *const string); |
| 77 |
extern vString *vStringNewInit (const char *const s); |
| 78 |
extern void vStringCopyS (vString *const string, const char *const s); |
| 79 |
extern void vStringNCopyS (vString *const string, const char *const s, const size_t length); |
| 80 |
extern void vStringCopyToLower (vString *const dest, const vString *const src); |
| 81 |
extern void vStringSetLength (vString *const string); |
| 82 |
|
| 83 |
#endif /* _VSTRING_H */ |
| 84 |
|
| 85 |
/* vi:set tabstop=4 shiftwidth=4: */ |