Files
sics/errormsg.c
2008-01-18 07:55:58 +00:00

64 lines
1.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "errormsg.h"
/* ErrMsgList implementation */
#define MC_NAME(T) ErrMsg##T
#define MC_IMPLEMENTATION
#include "mclist.c"
ErrMsg *ErrPutMsg(ErrMsgList *dump, char *data, char *fmt, ...) {
ErrMsg *m, *p;
va_list ap;
char buf[256];
char *text;
int l;
va_start(ap, fmt);
l = vsnprintf(buf, sizeof buf, fmt, ap);
va_end(ap);
if (l < sizeof buf) {
text = buf;
} else {
/* assuming we have a C99 conforming snprintf and need a larger buffer */
text = calloc(l, 1);
va_start(ap, fmt);
vsnprintf(text, l, fmt, ap);
va_end(ap);
}
m = NULL;
for (p = ErrMsgFirst(dump); p!= NULL; p = ErrMsgNext(dump)) {
if (strcmp(text, p->text) == 0) {
m = ErrMsgTake(dump);
break;
}
}
if (m == NULL) {
m = calloc(1, sizeof(*m));
if (text == buf) {
m->text = strdup(text);
} else {
m->text = text;
}
m->data = NULL;
m->cnt = 1;
} else {
if (text != buf) free(text);
m->cnt++;
}
if (m->data) {
free(m->data);
m->data = NULL;
}
if (data) {
m->data = strdup(data);
}
ErrMsgFirst(dump);
ErrMsgInsert(dump, m);
time(&m->last);
return m;
}