]> git.sur5r.net Git - cc65/commitdiff
Add a user index to class StrBuf
authorcuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Sun, 29 Sep 2002 19:49:55 +0000 (19:49 +0000)
committercuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Sun, 29 Sep 2002 19:49:55 +0000 (19:49 +0000)
git-svn-id: svn://svn.cc65.org/cc65/trunk@1411 b7a2c559-68d2-44c3-8de9-860c34a00d81

src/common/strbuf.c
src/common/strbuf.h

index 4d13c8645e4b4015a420a5c947c3f44d70ecff69..2d37f1605646a161bd4b2df3829314a0c17c423f 100644 (file)
@@ -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;
 }
index fa9b74111feb2a788631cf20acb904e05bf5f5a9..36120c527cc2fee052f13adb8381eebb1b40dca6 100644 (file)
@@ -6,7 +6,7 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (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
@@ -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!