queue problems

This commit is contained in:
Ellen Arvidsson 2025-04-11 23:16:29 +02:00
parent 2ac5491b44
commit 8dd2fe0e34
10 changed files with 427 additions and 59 deletions

126
src/spho/ctx.c Normal file
View file

@ -0,0 +1,126 @@
#include <stdio.h>
#include <string.h>
#include "spho/ctx.h"
static const char *spho_ctx_sys_strerror(struct spho_ctx *);
static const char *spho_ctx_intern_strerror(struct spho_ctx *);
struct spho_err {
int num;
char *msg;
};
struct spho_err spho_errmsgs[] = {
{ SPHO_ERR_SYSTEM, "spho system error" },
{ SPHO_ERR_INTERNAL, "spho internal error" },
{ -1, NULL }
};
struct spho_ctx *
spho_ctx_create(void)
{
struct spho_ctx *c;
if ((c = malloc(sizeof(struct spho_ctx))) == NULL)
return (NULL);
c->err = 0;
c->err_info = 0;
SLIST_INIT(&c->noms);
SLIST_INIT(&c->cnsts);
return (c);
}
void
spho_ctx_destroy(struct spho_ctx *ctx)
{
struct spho_nom_le *nom;
struct spho_cnst_le *cnst;
SPHO_PRECOND(ctx != NULL);
while (! SLIST_EMPTY(&c->noms)) {
nom = SLIST_FIRST(&c->noms);
SLIST_REMOVE_HEAD(&c->noms, next);
free(nom);
}
while (! SLIST_EMPTY(&c->cnsts)) {
cnst = SLIST_FIRST(&c->cnsts);
SLIST_REMOVE_HEAD(&c->cnsts, next);
free(cnst);
}
}
const char *
spho_ctx_strerror(struct spho_ctx *ctx)
{
char *msg;
int res;
SPHO_PRECOND(ctx != NULL);
msg = NULL;
if (SPHO_ERR_IS_SYSERR(ctx->err))
msg = spho_ctx_sys_strerror(ctx);
else
msg = spho_ctx_intern_strerror(ctx);
SPHO_POSTCOND(msg != NULL);
return (msg);
}
static const char *
spho_ctx_sys_strerror(struct spho_ctx *ctx)
{
#ifdef SPHO_DEBUG
res = snprintf(ctx->errbuf, sizeof(ctx->errbuf),
"%s:%d:spho_syserr:%s (%d)", ctx->filebuf, ctx->errline,
strerror(ctx->err_info), ctx->err_info);
if (res >= sizeof(ctx->errbuf))
ctx->errbuf[sizeof(ctx->errbuf) - 1] = '\0';
#else
res = snprintf(ctx->errbuf, sizeof(ctx->errbuf),
"spho_syserr:%s (%d)", ctx->filebuf, ctx->errline,
strerror(ctx->err_info), ctx->err_info);
if (res >= sizeof(ctx->errbuf))
ctx->errbuf[sizeof(ctx->errbuf) - 1] = '\0';
#endif
return (ctx->errbuf);
}
static const char *
spho_ctx_intern_strerror(struct spho_ctx *ctx)
{
int i;
int res;
SPHO_PRECOND(ctx != NULL);
for (i = 0; spho_errmsgs[i].msg != NULL; i++) {
if (spho_errmsgs[i].err == ctx->err) {
#ifdef SPHO_DEBUG
res = snprintf(ctx->errbuf, sizeof(ctx->errbuf),
"%s:%d:spho_intern_err:%s", ctx->filebuf,
ctx->errline, spho_errmsgs[i].msg);
#else
res = snprintf(ctx->errbuf, sizeof(ctx->errbuf),
"spho_intern_err:%s", ctx->errbuf);
#endif
}
}
SPHO_POSTCOND(res != -1);
return (ctx->errbuf);
}