diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/aplwc.h | 16 | ||||
| -rw-r--r-- | src/aplwc_internal.h | 32 | ||||
| -rw-r--r-- | src/errors.c | 18 | ||||
| -rw-r--r-- | src/libmain.c | 45 | ||||
| -rw-r--r-- | src/main.c | 23 | ||||
| -rw-r--r-- | src/malloc.c | 11 |
6 files changed, 145 insertions, 0 deletions
diff --git a/src/aplwc.h b/src/aplwc.h new file mode 100644 index 0000000..7a90147 --- /dev/null +++ b/src/aplwc.h @@ -0,0 +1,16 @@ +#ifndef APLWC_H +#define APLWC_H + +typedef enum { + APLWC_OK, + APLWC_NOT_IMPLEMENTED, + APLWC_MALLOC_FAILED, +} AplwcRc; + +typedef struct Aplwc Aplwc; + +AplwcRc aplwc_new(Aplwc **); +AplwcRc aplwc_destroy(Aplwc *); +const char *aplwc_errstr(Aplwc *); + +#endif /* APLWC_H */ diff --git a/src/aplwc_internal.h b/src/aplwc_internal.h new file mode 100644 index 0000000..f80c8ab --- /dev/null +++ b/src/aplwc_internal.h @@ -0,0 +1,32 @@ +#ifndef APLWC_INTERNAL_H +#define APLWC_INTERNAL_H + +#define UNUSED_PARAMETER(arg) ((void)arg) + +#define RETURN_ERROR(aplwc, rc, msg) do{ \ + Aplwc *_p = (aplwc); \ + AplwcRc _r = APLWC_##rc; \ + if(_p) _p->last_error = _r; \ + return _r; \ + }while(0) + +#define NELEM(x) (sizeof(x)/sizeof(x[0])) + +#include <stddef.h> +#include <stdint.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +struct Aplwc { + AplwcRc last_error; + char last_error_msg[64]; +}; + +/* errors.c */ +const char *rc_to_string(AplwcRc); + +/* malloc.c */ +void *malloc_zero(size_t); + +#endif /* APLWC_INTERNAL_H */ diff --git a/src/errors.c b/src/errors.c new file mode 100644 index 0000000..ab76082 --- /dev/null +++ b/src/errors.c @@ -0,0 +1,18 @@ +#include <aplwc.h> +#include <aplwc_internal.h> + +static const char *errors[] = { + [APLWC_OK] = + "", + [APLWC_NOT_IMPLEMENTED] = + "Not yet implemented", +}; + +const char * +rc_to_string(AplwcRc rc) +{ + const char *str = NULL; + if((rc > 0) && (rc < NELEM(errors))) + str = errors[rc]; + return str; +} diff --git a/src/libmain.c b/src/libmain.c new file mode 100644 index 0000000..1458bf8 --- /dev/null +++ b/src/libmain.c @@ -0,0 +1,45 @@ +#include <aplwc.h> +#include <aplwc_internal.h> + +AplwcRc +aplwc_new(Aplwc **p) +{ + *p = malloc_zero(sizeof(Aplwc)); + if(*p == NULL) + RETURN_ERROR(*p, MALLOC_FAILED, ""); + + return APLWC_OK; +} + +AplwcRc +aplwc_destroy(Aplwc *p) +{ + if(p) + free(p); + + return APLWC_OK; +} + +const char * +aplwc_errstr(Aplwc *aplwc) +{ + static char buf[256]; + buf[0] = 0; + + const char *err = NULL; + const char *msg = NULL; + if(aplwc){ + err = rc_to_string(aplwc->last_error); + msg = aplwc->last_error_msg; + } + + if(!err) + err = "(unable to produce error string)"; + + if(msg && (strlen(msg) > 0)) + snprintf(buf, sizeof(buf), "%s: %s", err, msg); + else + snprintf(buf, sizeof(buf), "%s", err); + + return buf; +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..209c88a --- /dev/null +++ b/src/main.c @@ -0,0 +1,23 @@ +#include <stdio.h> +#include <aplwc.h> + +int +main(void) +{ + Aplwc *aplwc; + + int ret = aplwc_new(&aplwc); + if(ret){ + fprintf(stderr, "Couldn't create new aplwc instance: %s\n", aplwc_errstr(aplwc)); + return 1; + } + + + ret = aplwc_destroy(aplwc); + if(ret){ + fprintf(stderr, "Couldn't destroy the aplwc instance: %s\n", aplwc_errstr(aplwc)); + return 1; + } + + return 0; +} diff --git a/src/malloc.c b/src/malloc.c new file mode 100644 index 0000000..abf0884 --- /dev/null +++ b/src/malloc.c @@ -0,0 +1,11 @@ +#include <aplwc.h> +#include <aplwc_internal.h> + +void * +malloc_zero(size_t size) +{ + void *p = malloc(size); + if(p) + memset(p, 0, size); + return p; +} |