69 lines
1.5 KiB
C
69 lines
1.5 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdarg.h>
|
|
#include <string.h>
|
|
#include "errormsg.h"
|
|
|
|
/* compare two strings for euqality, ignoring text within square brackets */
|
|
int ErrEqual(char *str1, char *str2) {
|
|
char *p;
|
|
|
|
while (*str1 != '\0' || *str2 != '\0') {
|
|
if (*str1 != *str2) {
|
|
return 0;
|
|
}
|
|
if (*str1 == '[') {
|
|
str1 = strchr(str1, ']');
|
|
str2 = strchr(str2, ']');
|
|
if (str1 == NULL || str2 == NULL) {
|
|
return str1 == str2;
|
|
}
|
|
}
|
|
str1++;
|
|
str2++;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
ErrMsg *ErrPutMsg(ErrMsg *dump, char *fmt, ...) {
|
|
ErrMsg *m;
|
|
ErrMsg **last;
|
|
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);
|
|
}
|
|
last = &dump;
|
|
for (m = dump; m != NULL; m = m->next) {
|
|
if (ErrEqual(text, m->text)) {
|
|
*last = m->next; /* remove found item from the list */
|
|
break;
|
|
}
|
|
last = &m->next;
|
|
}
|
|
if (m == NULL) { /* make a fresh list item m */
|
|
if (text == buf) text = strdup(buf);
|
|
m = calloc(1, sizeof(*m));
|
|
m->text = text;
|
|
m->cnt = 1;
|
|
} else { /* take the found item m */
|
|
if (text != buf) free(text);
|
|
m->cnt++;
|
|
}
|
|
m->next = dump; /* add the new item to the head of the list */
|
|
time(&m->last);
|
|
return m;
|
|
}
|