From: cuz Date: Sun, 29 Sep 2002 19:49:55 +0000 (+0000) Subject: Add a user index to class StrBuf X-Git-Tag: V2.12.0~2219 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=0e4493f0750540f369a1e3134576b4c81ad0e148;p=cc65 Add a user index to class StrBuf git-svn-id: svn://svn.cc65.org/cc65/trunk@1411 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- diff --git a/src/common/strbuf.c b/src/common/strbuf.c index 4d13c8645..2d37f1605 100644 --- a/src/common/strbuf.c +++ b/src/common/strbuf.c @@ -1,12 +1,12 @@ /*****************************************************************************/ /* */ -/* strbuf.c */ +/* strbuf.c */ /* */ /* Variable sized string buffers */ /* */ /* */ /* */ -/* (C) 2001 Ullrich von Bassewitz */ +/* (C) 2001-2002 Ullrich von Bassewitz */ /* Wacholderweg 14 */ /* D-70597 Stuttgart */ /* EMail: uz@musoftware.de */ @@ -42,7 +42,7 @@ /*****************************************************************************/ -/* Data */ +/* Data */ /*****************************************************************************/ @@ -63,6 +63,7 @@ StrBuf* InitStrBuf (StrBuf* B) { B->Allocated = 0; B->Len = 0; + B->Index = 0; B->Buf = 0; return B; } diff --git a/src/common/strbuf.h b/src/common/strbuf.h index fa9b74111..36120c527 100644 --- a/src/common/strbuf.h +++ b/src/common/strbuf.h @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 2001 Ullrich von Bassewitz */ +/* (C) 2001-2002 Ullrich von Bassewitz */ /* Wacholderweg 14 */ /* D-70597 Stuttgart */ /* EMail: uz@musoftware.de */ @@ -55,16 +55,17 @@ typedef struct StrBuf StrBuf; struct StrBuf { - unsigned Allocated; - unsigned Len; - char* Buf; + unsigned Allocated; /* Size of allocated memory */ + unsigned Len; /* Length of the string */ + unsigned Index; /* Used for reading (Get and friends) */ + char* Buf; /* Pointer to buffer */ }; /* An empty string buf */ extern const StrBuf EmptyStrBuf; /* Initializer for static string bufs */ -#define STATIC_STRBUF_INITIALIZER { 0, 0, 0 } +#define STATIC_STRBUF_INITIALIZER { 0, 0, 0, 0 } /* Initializer for auto string bufs */ #define AUTO_STRBUF_INITIALIZER EmptyStrBuf @@ -104,6 +105,16 @@ INLINE unsigned SB_GetLen (StrBuf* B) # define SB_GetLen(B) (B)->Len #endif +#if defined(HAVE_INLINE) +INLINE unsigned SB_GetIndex (StrBuf* B) +/* Return the user index of the string buffer */ +{ + return B->Index; +} +#else +# define SB_GetIndex(B) (B)->Index +#endif + #if defined(HAVE_INLINE) INLINE const char* SB_GetConstBuf (const StrBuf* B) /* Return a buffer pointer */ @@ -166,6 +177,30 @@ INLINE void SB_Clear (StrBuf* B) # define SB_Clear(B) ((B)->Len = 0) #endif +#if defined(HAVE_INLINE) +INLINE char SB_Get (StrBuf* B) +/* Return the next character from the string incrementing Index. Returns NUL + * if the end of the string is reached. + */ +{ + return (B->Index < B->Len)? B->Buf[B->Index++] : '\0'; +} +#else +# define SB_Get(B) (((B)->Index < (B)->Len)? (B)->Buf[(B)->Index++] : '\0') +#endif + +#if defined(HAVE_INLINE) +INLINE char SB_Peek (StrBuf* B) +/* Look at the next character from the string without incrementing Index. + * Returns NUL if the end of the string is reached. + */ +{ + return (B->Index < B->Len)? B->Buf[B->Index] : '\0'; +} +#else +# define SB_Peek(B) (((B)->Index < (B)->Len)? (B)->Buf[(B)->Index] : '\0') +#endif + void SB_Terminate (StrBuf* B); /* Zero terminate the given string buffer. NOTE: The terminating zero is not * accounted for in B->Len, if you want that, you have to use AppendChar!