2019-05-19 16:55:31 -05:00
|
|
|
// 19 may 2019
|
2019-05-31 10:17:11 -05:00
|
|
|
#include <errno.h>
|
2019-05-19 16:55:31 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "testing.h"
|
|
|
|
#include "testingpriv.h"
|
|
|
|
|
|
|
|
void testingprivInternalError(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
fprintf(stderr, "** testing internal error: ");
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, "; aborting\n");
|
|
|
|
va_end(ap);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2019-05-30 22:11:08 -05:00
|
|
|
#define sharedbitsPrefix testingpriv
|
|
|
|
#include "../../sharedbits/alloc_impl.h"
|
2019-05-30 23:12:08 -05:00
|
|
|
#include "../../sharedbits/array_impl.h"
|
2019-05-30 22:11:08 -05:00
|
|
|
#undef sharedbitsPrefix
|
2019-05-19 16:55:31 -05:00
|
|
|
|
2019-05-31 01:52:51 -05:00
|
|
|
#define sharedbitsPrefix testingprivImpl
|
|
|
|
#define sharedbitsStatic static
|
2019-05-31 10:17:11 -05:00
|
|
|
#define sharedbitsInternalError testingprivInternalError
|
2019-05-31 01:52:51 -05:00
|
|
|
#include "../../sharedbits/strsafe_impl.h"
|
2019-05-31 10:17:11 -05:00
|
|
|
#undef sharedbitsInternalError
|
2019-05-31 01:52:51 -05:00
|
|
|
#undef sharedbitsStatic
|
|
|
|
#undef sharedbitsPrefix
|
|
|
|
|
2019-06-08 17:39:59 -05:00
|
|
|
#define sharedbitsPrefix testingpriv
|
|
|
|
#define testingprivStrncpy testingprivImplStrncpy
|
|
|
|
#include "../../sharedbits/strdup_impl.h"
|
|
|
|
#undef testingprivStrncpy
|
|
|
|
#undef sharedbitsPrefix
|
|
|
|
|
2019-06-01 10:40:16 -05:00
|
|
|
int testingprivVsnprintf(char *s, size_t n, const char *fmt, va_list ap)
|
2019-05-19 16:55:31 -05:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2019-06-01 10:40:16 -05:00
|
|
|
ret = testingprivImplVsnprintf(s, n, fmt, ap);
|
2019-05-19 16:55:31 -05:00
|
|
|
if (ret < 0)
|
|
|
|
testingprivInternalError("encoding error in vsnprintf(); this likely means your call to testingTLogf() and the like is invalid");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-06-01 10:40:16 -05:00
|
|
|
int testingprivSnprintf(char *s, size_t n, const char *fmt, ...)
|
2019-05-19 16:55:31 -05:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int ret;
|
|
|
|
|
2019-06-01 10:40:16 -05:00
|
|
|
va_start(ap, fmt);
|
|
|
|
ret = testingprivVsnprintf(s, n, fmt, ap);
|
2019-05-19 16:55:31 -05:00
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-19 22:15:35 -05:00
|
|
|
char *testingprivVsmprintf(const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
va_list ap2;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
va_copy(ap2, ap);
|
|
|
|
n = testingprivVsnprintf(NULL, 0, fmt, ap2);
|
|
|
|
va_end(ap2);
|
|
|
|
s = (char *) testingprivAlloc((n + 1) * sizeof (char), "char[]");
|
|
|
|
testingprivVsnprintf(s, n + 1, fmt, ap);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *testingprivSmprintf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
s = testingprivVsmprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct testingprivOutbuf {
|
|
|
|
testingprivArray buf;
|
|
|
|
};
|
|
|
|
|
|
|
|
testingprivOutbuf *testingprivNewOutbuf(void)
|
|
|
|
{
|
|
|
|
testingprivOutbuf *o;
|
|
|
|
|
|
|
|
o = testingprivNew(testingprivOutbuf);
|
|
|
|
testingprivArrayInit(o->buf, char, 32, "testing output buffer");
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
void testingprivOutbufFree(testingprivOutbuf *o)
|
|
|
|
{
|
|
|
|
testingprivArrayFree(o->buf);
|
|
|
|
testingprivFree(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
void testingprivOutbufVprintf(testingprivOutbuf *o, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
char *dest;
|
|
|
|
va_list ap2;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (o == NULL) {
|
|
|
|
vprintf(fmt, ap);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
va_copy(ap2, ap);
|
2019-05-20 10:12:16 -05:00
|
|
|
n = testingprivVsnprintf(NULL, 0, fmt, ap2);
|
2019-05-19 22:15:35 -05:00
|
|
|
va_end(ap2);
|
|
|
|
// To conserve memory, we only allocate the terminating NUL once.
|
|
|
|
if (o->buf.len == 0)
|
|
|
|
dest = (char *) testingprivArrayAppend(&(o->buf), n + 1);
|
|
|
|
else {
|
|
|
|
dest = (char *) testingprivArrayAppend(&(o->buf), n);
|
|
|
|
dest--;
|
|
|
|
}
|
|
|
|
testingprivVsnprintf(dest, n + 1, fmt, ap);
|
|
|
|
}
|
|
|
|
|
2019-05-28 23:35:51 -05:00
|
|
|
void testingprivOutbufVprintfIndented(testingprivOutbuf *o, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
char *lineStart, *lineEnd;
|
|
|
|
const char *indent;
|
|
|
|
|
|
|
|
buf = testingprivVsmprintf(fmt, ap);
|
|
|
|
|
|
|
|
lineStart = buf;
|
|
|
|
indent = "";
|
|
|
|
for (;;) {
|
|
|
|
lineEnd = strchr(lineStart, '\n');
|
|
|
|
if (lineEnd == NULL)
|
|
|
|
break;
|
|
|
|
*lineEnd = '\0';
|
|
|
|
testingprivOutbufPrintf(o, "%s%s\n", indent, lineStart);
|
|
|
|
lineStart = lineEnd + 1;
|
|
|
|
indent = " ";
|
|
|
|
}
|
|
|
|
// and print the last line fragment, if any
|
|
|
|
if (*lineStart != '\0')
|
|
|
|
testingprivOutbufPrintf(o, "%s%s", indent, lineStart);
|
|
|
|
testingprivFree(buf);
|
|
|
|
}
|
|
|
|
|
2019-05-19 22:15:35 -05:00
|
|
|
void testingprivOutbufPrintf(testingprivOutbuf *o, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
testingprivOutbufVprintf(o, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO right now this assumes the last character in o before calling this is a newline
|
2019-05-20 09:57:31 -05:00
|
|
|
void testingprivOutbufAppendOutbuf(testingprivOutbuf *o, testingprivOutbuf *src)
|
2019-05-19 22:15:35 -05:00
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
size_t n;
|
2019-05-30 21:09:45 -05:00
|
|
|
bool hasTrailingBlankLine;
|
2019-05-27 10:02:23 -05:00
|
|
|
size_t trailingBlankLinePos = 0; // silence incorrect MSVC warning
|
2019-05-19 22:15:35 -05:00
|
|
|
char *lineStart, *lineEnd;
|
|
|
|
|
|
|
|
buf = src->buf.buf;
|
|
|
|
n = src->buf.len;
|
|
|
|
if (n == 0)
|
|
|
|
// nothing to write
|
|
|
|
return;
|
|
|
|
|
|
|
|
// strip trailing blank lines, if any
|
2019-05-30 21:09:45 -05:00
|
|
|
hasTrailingBlankLine = false;
|
2019-05-19 22:15:35 -05:00
|
|
|
if (buf[n - 1] == '\n') {
|
2019-05-30 21:09:45 -05:00
|
|
|
hasTrailingBlankLine = true;
|
2019-05-19 22:15:35 -05:00
|
|
|
while (n > 0 && buf[n - 1] == '\n')
|
|
|
|
n--;
|
|
|
|
if (n == 0) {
|
|
|
|
// the buffer only has blank lines, so just add a single newline and be done with it
|
|
|
|
// TODO verify that this is the correct behavior
|
|
|
|
testingprivOutbufPrintf(o, "\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
trailingBlankLinePos = n;
|
|
|
|
buf[trailingBlankLinePos] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
lineStart = buf;
|
|
|
|
for (;;) {
|
|
|
|
lineEnd = strchr(lineStart, '\n');
|
|
|
|
if (lineEnd == NULL) // last line
|
|
|
|
break;
|
|
|
|
*lineEnd = '\0';
|
2019-05-20 09:57:31 -05:00
|
|
|
testingprivOutbufPrintf(o, " %s\n", lineStart);
|
2019-05-19 22:15:35 -05:00
|
|
|
// be sure to restore src to its original state
|
|
|
|
*lineEnd = '\n';
|
|
|
|
lineStart = lineEnd + 1;
|
|
|
|
}
|
2019-05-20 19:07:59 -05:00
|
|
|
// print the last line, if any
|
|
|
|
if (*lineStart != '\0')
|
|
|
|
testingprivOutbufPrintf(o, " %s\n", lineStart);
|
2019-05-19 22:15:35 -05:00
|
|
|
|
|
|
|
// restore src to its original state
|
|
|
|
if (hasTrailingBlankLine)
|
|
|
|
buf[trailingBlankLinePos] = '\n';
|
|
|
|
}
|
2019-05-20 21:03:56 -05:00
|
|
|
|
|
|
|
const char *testingprivOutbufString(testingprivOutbuf *o)
|
|
|
|
{
|
|
|
|
if (o->buf.buf == NULL)
|
|
|
|
return "";
|
|
|
|
return o->buf.buf;
|
|
|
|
}
|