struct/dstr.[ch3] (dstr_putc): Accept an `int' argument.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 22 Jun 2013 13:47:31 +0000 (14:47 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 28 Jun 2013 22:24:18 +0000 (23:24 +0100)
This is a rather annoying problem.  It's useful to store binary data in
`dstr' objects, and, indeed, characters encoded (as <stdio.h> does) as
`unsigned char' values in an `int' object; but the `char' type of the
argument requires an implicit conversion, which may cause undefined
behaviour, and raises warnings if called with an out-of-range constant
value.

struct/dstr.3
struct/dstr.c
struct/dstr.h

index 28acb04..1cc866d 100644 (file)
@@ -60,7 +60,7 @@ dstr \- a simple dynamic string type
 .BI "void dstr_ensure(dstr *" d ", size_t " sz );
 .BI "void dstr_tidy(dstr *" d );
 
-.BI "void dstr_putc(dstr *" d ", char " ch );
+.BI "void dstr_putc(dstr *" d ", int " ch );
 .BI "void dstr_putz(dstr *" d );
 .BI "void dstr_puts(dstr *" d ", const char *" s );
 .BI "int dstr_vputf(dstr *" d ", va_list *" ap );
index 3072c57..7e154ae 100644 (file)
@@ -118,14 +118,14 @@ void dstr_ensure(dstr *d, size_t sz)
 /* --- @dstr_putc@ --- *
  *
  * Arguments:  @dstr *d@ = pointer to a dynamic string block
- *             @char ch@ = character to append
+ *             @int ch@ = character to append
  *
  * Returns:    ---
  *
  * Use:                Appends a character to a string.
  */
 
-void dstr_putc(dstr *d, char ch) { DPUTC(d, ch); }
+void dstr_putc(dstr *d, int ch) { DPUTC(d, ch); }
 
 /* --- @dstr_putz@ --- *
  *
index 3ef3051..e1851a2 100644 (file)
@@ -141,19 +141,19 @@ extern void dstr_ensure(dstr */*d*/, size_t /*sz*/);
 /* --- @dstr_putc@ --- *
  *
  * Arguments:  @dstr *d@ = pointer to a dynamic string block
- *             @char ch@ = character to append
+ *             @int ch@ = character to append
  *
  * Returns:    ---
  *
  * Use:                Appends a character to a string.
  */
 
-extern void dstr_putc(dstr */*d*/, char /*ch*/);
+extern void dstr_putc(dstr */*d*/, int /*ch*/);
 
 #define DPUTC(d, ch) do {                                              \
   dstr *_d = (d);                                                      \
   DENSURE(_d, 1);                                                      \
-  _d->buf[_d->len++] = (ch);                                           \
+  *((unsigned char *)_d->buf + _d->len++) = (ch);                      \
 } while (0)
 
 /* --- @dstr_putz@ --- *