/* Grow the array if necessary */
if (C->Count >= C->Size) {
- /* Must grow */
+ /* Must grow */
void** NewItems;
- if (C->Size > 0) {
- C->Size *= 2;
- } else {
- C->Size = 8;
- }
- NewItems = xmalloc (C->Size * sizeof (void*));
- memcpy (NewItems, C->Items, C->Count * sizeof (void*));
- xfree (C->Items);
- C->Items = NewItems;
+ if (C->Size > 0) {
+ C->Size *= 2;
+ } else {
+ C->Size = 8;
+ }
+ NewItems = xmalloc (C->Size * sizeof (void*));
+ memcpy (NewItems, C->Items, C->Count * sizeof (void*));
+ xfree (C->Items);
+ C->Items = NewItems;
}
/* Move the existing elements if needed */
if (C->Count != Index) {
- memmove (C->Items+Index+1, C->Items+Index, (C->Count-Index) * sizeof (void*));
+ memmove (C->Items+Index+1, C->Items+Index, (C->Count-Index) * sizeof (void*));
}
++C->Count;
+#if !defined(HAVE_INLINE)
+void CollAppend (Collection* C, void* Item)
+/* Append an item to the end of the collection */
+{
+ /* Insert the item at the end of the current list */
+ CollInsert (C, Item, C->Count);
+}
+#endif
+
+
+
+#if !defined(HAVE_INLINE)
+void* CollAt (Collection* C, unsigned Index)
+/* Return the item at the given index */
+{
+ /* Check the index */
+ PRECONDITION (Index < C->Count);
+
+ /* Return the element */
+ return C->Items[Index];
+}
+#endif
+
+
+
+#if !defined(HAVE_INLINE)
+const void* CollConstAt (const Collection* C, unsigned Index)
+/* Return the item at the given index */
+{
+ /* Check the index */
+ PRECONDITION (Index < C->Count);
+
+ /* Return the element */
+ return C->Items[Index];
+}
+#endif
+
+
+
+#if !defined(HAVE_INLINE)
+void* CollLast (Collection* C)
+/* Return the last item in a collection */
+{
+ /* We must have at least one entry */
+ PRECONDITION (C->Count > 0);
+
+ /* Return the element */
+ return C->Items[C->Count-1];
+}
+#endif
+
+
+
+#if !defined(HAVE_INLINE)
+const void* CollConstLast (const Collection* C)
+/* Return the last item in a collection */
+{
+ /* We must have at least one entry */
+ PRECONDITION (C->Count > 0);
+
+ /* Return the element */
+ return C->Items[C->Count-1];
+}
+#endif
+
+
+
+#if !defined(HAVE_INLINE)
+void* CollPop (Collection* C)
+/* Remove the last segment from the stack and return it. Calls FAIL if the
+ * collection is empty.
+ */
+{
+ /* We must have at least one entry */
+ PRECONDITION (C->Count > 0);
+
+ /* Return the element */
+ return C->Items[--C->Count];
+}
+#endif
+
+
+
int CollIndex (Collection* C, const void* Item)
/* Return the index of the given item in the collection. Return -1 if the
* item was not found in the collection.
/* Linear search */
unsigned I;
for (I = 0; I < C->Count; ++I) {
- if (Item == C->Items[I]) {
- /* Found */
- return (int)I;
+ if (Item == C->Items[I]) {
+ /* Found */
+ return (int)I;
}
}
+#if !defined(HAVE_INLINE)
+void CollReplace (Collection* C, void* Item, unsigned Index)
+/* Replace the item at the given position. The old item will not be freed,
+ * just the pointer will get replaced.
+ */
+{
+ /* Check the index */
+ PRECONDITION (Index < C->Count);
+
+ /* Replace the item pointer */
+ C->Items[Index] = Item;
+}
+#endif
+
+
+
void CollMove (Collection* C, unsigned OldIndex, unsigned NewIndex)
/* Move an item from one position in the collection to another. OldIndex
* is the current position of the item, NewIndex is the new index after
CollInsert (C, Item, C->Count);
}
#else
-# define CollAppend(C, Item) CollInsert (C, Item, (C)->Count)
+void CollAppend (Collection* C, void* Item);
+/* Append an item to the end of the collection */
#endif
#if defined(HAVE_INLINE)
return C->Items[Index];
}
#else
-# define CollAt(C, Index) \
- (PRECONDITION ((Index) < (C)->Count), \
- (C)->Items[(Index)])
+void* CollAt (Collection* C, unsigned Index);
+/* Return the item at the given index */
#endif
#if defined(HAVE_INLINE)
return C->Items[Index];
}
#else
-# define CollConstAt(C, Index) \
- (PRECONDITION ((Index) < (C)->Count), \
- (C)->Items[(Index)])
+const void* CollConstAt (const Collection* C, unsigned Index);
+/* Return the item at the given index */
#endif
#if defined(HAVE_INLINE)
return C->Items[C->Count-1];
}
#else
-# define CollLast(C) \
- (PRECONDITION ((C)->Count > 0), \
- (C)->Items[(C)->Count-1])
+void* CollLast (Collection* C);
+/* Return the last item in a collection */
#endif
#if defined(HAVE_INLINE)
return C->Items[C->Count-1];
}
#else
-# define CollConstLast(C) \
- (PRECONDITION ((C)->Count > 0), \
- (C)->Items[(C)->Count-1])
+const void* CollConstLast (const Collection* C);
+/* Return the last item in a collection */
#endif
#if defined(HAVE_INLINE)
return C->Items[--C->Count];
}
#else
-# define CollPop(C) \
- (PRECONDITION ((C)->Count > 0), \
- (C)->Items[--(C)->Count])
+void* CollPop (Collection* C);
+/* Remove the last segment from the stack and return it. Calls FAIL if the
+ * collection is empty.
+ */
#endif
int CollIndex (Collection* C, const void* Item);
#if defined(HAVE_INLINE)
INLINE void CollReplace (Collection* C, void* Item, unsigned Index)
/* Replace the item at the given position. The old item will not be freed,
- * just the pointer will et replaced.
+ * just the pointer will get replaced.
*/
{
/* Check the index */
C->Items[Index] = Item;
}
#else
-# define CollReplace(C, Item, Index) \
- (PRECONDITION ((Index) < (C)->Count), \
- (C)->Items[(Index)] = (Item))
+void CollReplace (Collection* C, void* Item, unsigned Index);
+/* Replace the item at the given position. The old item will not be freed,
+ * just the pointer will get replaced.
+ */
#endif
void CollMove (Collection* C, unsigned OldIndex, unsigned NewIndex);
-/* End of exprlist.h */
+/* End of coll.h */
#endif
-
+
+#if !defined(HAVE_INLINE)
+char SB_At (const StrBuf* B, unsigned Index)
+/* Get a character from the buffer */
+{
+ PRECONDITION (Index < B->Len);
+ return B->Buf[Index];
+}
+#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!
+#if !defined(HAVE_INLINE)
+void SB_CopyStr (StrBuf* Target, const char* S)
+/* Copy S to Target, discarding the old contents of Target */
+{
+ SB_CopyBuf (Target, S, strlen (S));
+}
+#endif
+
+
+
+#if !defined(HAVE_INLINE)
+void SB_Copy (StrBuf* Target, const StrBuf* Source)
+/* Copy Source to Target, discarding the old contents of Target */
+{
+ SB_CopyBuf (Target, Source->Buf, Source->Len);
+}
+#endif
+
+
+
void SB_AppendChar (StrBuf* B, char C)
/* Append a character to a string buffer */
{
+#if !defined(HAVE_INLINE)
+void SB_AppendStr (StrBuf* B, const char* S)
+/* Append a string to the end of the string buffer */
+{
+ SB_AppendBuf (B, S, strlen (S));
+}
+#endif
+
+
+
+#if !defined(HAVE_INLINE)
+void SB_Append (StrBuf* Target, const StrBuf* Source)
+/* Append the contents of Source to Target */
+{
+ SB_AppendBuf (Target, Source->Buf, Source->Len);
+}
+#endif
+
+
+
+#if !defined(HAVE_INLINE)
+void SB_Cut (StrBuf* B, unsigned Len)
+/* Cut the contents of B at the given length. If the current length of the
+ * buffer is smaller than Len, nothing will happen.
+ */
+{
+ if (Len < B->Len) {
+ B->Len = Len;
+ }
+}
+#endif
+
+
+
void SB_Slice (StrBuf* Target, const StrBuf* Source, unsigned Start, unsigned Len)
/* Copy a slice from Source into Target. The current contents of Target are
* destroyed. If Start is greater than the length of Source, or if Len
return B->Buf[Index];
}
#else
-# define SB_At(B, Index) \
- (PRECONDITION ((Index) < (B)->Len), \
- (B)->Buf[Index])
+char SB_At (const StrBuf* B, unsigned Index);
+/* Get a character from the buffer */
#endif
#if defined(HAVE_INLINE)
SB_CopyBuf (Target, S, strlen (S));
}
#else
-# define SB_CopyStr(Target, S) SB_CopyBuf (Target, S, strlen (S))
+void SB_CopyStr (StrBuf* Target, const char* S);
+/* Copy S to Target, discarding the old contents of Target */
#endif
#if defined(HAVE_INLINE)
SB_CopyBuf (Target, Source->Buf, Source->Len);
}
#else
-# define SB_Copy(Target, Source) SB_CopyBuf (Target, (Source)->Buf, (Source)->Len)
+void SB_Copy (StrBuf* Target, const StrBuf* Source);
+/* Copy Source to Target, discarding the old contents of Target */
#endif
void SB_AppendChar (StrBuf* B, char C);
SB_AppendBuf (B, S, strlen (S));
}
#else
-# define SB_AppendStr(B, S) SB_AppendBuf (B, S, strlen (S))
+void SB_AppendStr (StrBuf* B, const char* S);
+/* Append a string to the end of the string buffer */
#endif
#if defined(HAVE_INLINE)
SB_AppendBuf (Target, Source->Buf, Source->Len);
}
#else
-# define SB_Append(Target, Source) SB_AppendBuf (Target, (Source)->Buf, (Source)->Len)
+void SB_Append (StrBuf* Target, const StrBuf* Source);
+/* Append the contents of Source to Target */
#endif
#if defined(HAVE_INLINE)
}
}
#else
-# define SB_Cut(B, L) if ((L) < (B)->Len) { (B)->Len = (L); }
+void SB_Cut (StrBuf* B, unsigned Len);
+/* Cut the contents of B at the given length. If the current length of the
+ * buffer is smaller than Len, nothing will happen.
+ */
#endif
void SB_Slice (StrBuf* Target, const StrBuf* Source, unsigned Start, unsigned Len);