#include "alloc.h"
#include "message.h"
\f
-/* sprintf() wrapper functions for convenience. */
-
-#if !__GNUC__
-char *
-spprintf (char *buf, const char *format,...)
-{
-#if HAVE_GOOD_SPRINTF
- int count;
-#endif
- va_list args;
-
- va_start (args, format);
-#if HAVE_GOOD_SPRINTF
- count =
-#endif
- vsprintf (buf, format, args);
- va_end (args);
-
-#if HAVE_GOOD_SPRINTF
- return &buf[count];
-#else
- return strchr (buf, 0);
-#endif
-}
-#endif /* !__GNUC__ */
-
-#if !__GNUC__ && !HAVE_GOOD_SPRINTF
-int
-nsprintf (char *buf, const char *format,...)
-{
- va_list args;
-
- va_start (args, format);
- vsprintf (buf, format, args);
- va_end (args);
-
- return strlen (buf);
-}
-
-int
-nvsprintf (char *buf, const char *format, va_list args)
-{
- vsprintf (buf, format, args);
- return strlen (buf);
-}
-#endif /* Not GNU C and not good sprintf(). */
-\f
/* Reverses the order of NBYTES bytes at address P, thus converting
between little- and big-endian byte orders. */
void
strcpy (st->string, s);
}
+/* Initializes DST with the contents of SRC between characters FIRST and LAST
+ inclusive */
+void
+ds_create_substr(struct string *dst, const struct string *src,
+ int first, int last)
+{
+ assert(last >= first);
+ dst->length = last - first + 1;
+ dst->capacity = 8 + dst->length * 2;
+ dst->string = xmalloc (dst->capacity + 1);
+
+ memcpy (dst->string, &src->string[first], dst->length);
+}
+
+
/* Initializes ST, making room for at least CAPACITY characters. */
void
ds_init (struct string *st, size_t capacity)
return cnt;
}
+/* Removes leading spaces from ST.
+ Returns number of spaces removed. */
+int
+ds_ltrim_spaces (struct string *st)
+{
+ int idx = ds_n_find(st, "\t ");
+ if (0 == idx)
+ return 0;
+
+ if (idx < 0 )
+ {
+ int len = ds_length(st);
+ ds_clear(st);
+ return len;
+ }
+
+ ds_replace(st, &ds_c_str(st)[idx]);
+
+ return idx;
+}
+
+
/* If the last character in ST is C, removes it and returns true.
Otherwise, returns false without modifying ST. */
bool
return st->capacity;
}
+
+/* Returns the index of the first character in ST which
+ is an element of the set CS.
+ Returns -1 if no characters are found.
+*/
+int
+ds_find(const struct string *st, const char cs[])
+{
+ int i;
+ int j;
+ for(i = 0; i < st->length ; ++i)
+ {
+ if ('\0' == st->string[i])
+ break;
+ for (j = 0 ; j < strlen(cs) ; ++j)
+ {
+ if ( st->string[i] == cs[j])
+ return i;
+ }
+ }
+ return -1;
+}
+
+/* Returns the index of the first character in ST which
+ is NOT an element of the set CS.
+ Returns -1 if no such character is found.
+*/
+int
+ds_n_find(const struct string *st, const char cs[])
+{
+ int i;
+ int j;
+ for(i = 0; i < st->length ; ++i)
+ {
+ bool found = false;
+ if ('\0' == st->string[i])
+ break;
+ for (j = 0 ; j < strlen(cs) ; ++j)
+ {
+ if ( st->string[i] == cs[j])
+ {
+ found = true;
+ break;
+ }
+ }
+ if ( !found )
+ return i;
+ }
+ return -1;
+}
+
+
+
/* Returns the first character in ST as a value in the range of
unsigned char. Returns EOF if ST is the empty string. */
int