/** * \file * source file of ProtoInterface */ #include #include "protointerface.h" #include "generic.h" #include "proto.h" #define PRINTBUFFERP (&protoInterfacep->printBuffer) static void putString(ProtoInterface *protoInterfacep) { char *newlinePos = NULL; char *beginp = NULL; BufferVector *bufferp = &protoInterfacep->printBuffer; size_t actSize = bufferVector_getActSize(bufferp); if (actSize != 0) { do { beginp = bufferVector_getElementsRef(bufferp, 0, actSize); newlinePos = memchr(beginp, '\n', actSize); if (newlinePos) { *newlinePos = '\0'; PROTO_Puts(protoInterfacep->context, beginp); bufferVector_skip(bufferp, newlinePos - beginp + 1); actSize -= newlinePos - beginp + 1; } } while (newlinePos); } } int protoInterface_init(ProtoInterface *protoInterfacep, protoContext context) { int okf = !0; protoInterfacep->context = context; okf = (okf && bufferVector_init(&protoInterfacep->printBuffer, sizeof(char), PROTOINTERFACE_MESSAGE_BUFFER_INIT_SIZE, protoInterfacep)); return okf; } void protoInterface_destroy(ProtoInterface *protoInterfacep) { protoInterfacep->context = NULL; bufferVector_destroy(&protoInterfacep->printBuffer); } int protoInterface_printf(ProtoInterface *protoInterfacep, const char *formatString, ...) { BufferVector *bufferp = &protoInterfacep->printBuffer; char *beginp = NULL; size_t spaceNeeded; int ret; va_list ap; va_start(ap, formatString); spaceNeeded = PROTOINTERFACE_MESSAGE_BUFFER_INIT_SIZE / 2; do { beginp = bufferVector_ensureSpaceForElemements(bufferp, spaceNeeded); if (beginp == NULL) return 0; ret = wrapper_vsnprintf(beginp, spaceNeeded, formatString, ap); spaceNeeded += PROTOINTERFACE_MESSAGE_BUFFER_INIT_SIZE; } while (ret == -1); bufferVector_appendEmptyElements(bufferp, ret); va_end(ap); putString(protoInterfacep); return ret; } int protoInterface_prints(ProtoInterface *protoInterfacep, const char *outputStringp) { BufferVector *bufferp = &protoInterfacep->printBuffer; int len = strlen(outputStringp); if (len) { if (0 == bufferVector_appendElements(bufferp, outputStringp, len)) return 0; putString(protoInterfacep); } return len; } void protoInterface_flush(ProtoInterface *protoInterfacep) { char *beginp = NULL; putString(protoInterfacep); beginp = bufferVector_getElementRef(PRINTBUFFERP, 0); if (beginp) { PROTO_Puts(protoInterfacep->context, beginp); bufferVector_clear(PRINTBUFFERP); } }