/*****************************************************************************/
/* */
-/* 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 */
/*****************************************************************************/
-/* Data */
+/* Data */
/*****************************************************************************/
{
B->Allocated = 0;
B->Len = 0;
+ B->Index = 0;
B->Buf = 0;
return B;
}
/* */
/* */
/* */
-/* (C) 2001 Ullrich von Bassewitz */
+/* (C) 2001-2002 Ullrich von Bassewitz */
/* Wacholderweg 14 */
/* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */
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
# 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 */
# 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!