Implement src/error.c

Implement src/error.c to provide the patty_error type, to allow easy
error string formatting, error state checking, and error clearing
This commit is contained in:
XANTRONIX Development 2020-09-13 21:45:30 -04:00 committed by XANTRONIX Industrial
parent 2c3a461ee9
commit 356b25fd94
3 changed files with 71 additions and 6 deletions

24
include/patty/error.h Normal file
View file

@ -0,0 +1,24 @@
#ifndef _PATTY_ERROR_H
#define _PATTY_ERROR_H
#define PATTY_ERROR_STRLEN 256
enum patty_error_state {
PATTY_ERROR_OK,
PATTY_ERROR_SET
};
typedef struct _patty_error {
enum patty_error_state state;
char err[PATTY_ERROR_STRLEN];
} patty_error;
int patty_error_fmt(patty_error *e, const char *message, ...);
void patty_error_clear(patty_error *e);
int patty_error_set(patty_error *e);
char *patty_error_string(patty_error *e);
#endif /* _PATTY_ERROR_H */

View file

@ -7,13 +7,13 @@ CC = $(CROSS)cc
CFLAGS = $(CGFLAGS) -fPIC -Wall -O2 -I$(INCLUDE_PATH)
LDFLAGS = -lutil
HEADERS = kiss.h ax25.h client.h ax25/if.h ax25/frame.h \
ax25/sock.h ax25/route.h ax25/server.h daemon.h list.h \
hash.h dict.h timer.h print.h conf.h
HEADERS = kiss.h kiss/tnc.h ax25.h client.h ax25/if.h ax25/frame.h \
ax25/sock.h ax25/route.h ax25/server.h daemon.h \
error.h list.h hash.h dict.h timer.h print.h conf.h
OBJS = kiss.o ax25.o client.o if.o frame.o \
sock.o route.o server.o daemon.o list.o \
hash.o dict.o timer.o print.o conf.o
OBJS = kiss.o tnc.o ax25.o client.o if.o frame.o \
sock.o route.o server.o daemon.o \
error.o list.o hash.o dict.o timer.o print.o conf.o
VERSION_MAJOR = 0
VERSION_MINOR = 0.1

41
src/error.c Normal file
View file

@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <patty/error.h>
int patty_error_fmt(patty_error *e, const char *message, ...) {
va_list args;
va_start(args, message);
if (vsnprintf(e->err, sizeof(e->err)-1, message, args) < 0) {
goto error_vsnprintf;
}
va_end(args);
e->state = PATTY_ERROR_SET;
return 0;
error_vsnprintf:
va_end(args);
return -1;
}
void patty_error_clear(patty_error *e) {
memset(e->err, '\0', sizeof(e->err));
e->state = PATTY_ERROR_OK;
}
int patty_error_isset(patty_error *e) {
return e->state == PATTY_ERROR_SET? 1: 0;
}
char *patty_error_string(patty_error *e) {
return e->err;
}