From 0d9fa036c4fbe4b1f7f53d38659cc678eb8b5c74 Mon Sep 17 00:00:00 2001 From: Etienne Brateau <etienne.brateau@ensiie.fr> Date: Sun, 8 Jul 2018 21:03:41 +0200 Subject: [PATCH] strlist: change struct to use char* instead of char[256] --- log/src/log.c | 4 +- log/src/loged.c | 6 +-- log/src/logntk.c | 6 +-- log/src/logspc.c | 6 +-- psys/include/p2c/strlist.h | 10 ++--- psys/src/strlist.c | 89 +++++++++++++++++++++++++------------- 6 files changed, 72 insertions(+), 49 deletions(-) diff --git a/log/src/log.c b/log/src/log.c index 2c9fef8..0a1344c 100644 --- a/log/src/log.c +++ b/log/src/log.c @@ -7372,7 +7372,7 @@ static void doimmedfunction() if (*(char *)l1->value != '\0') { tp = findtool((char *)l1->value); if (tp->ready) - strlist_delete(&commandlist, l1); + strlist_delete(&commandlist, &l1); } } doingcnffunction = false; @@ -21790,7 +21790,7 @@ int main(int argc, char * argv[]) vmessage(messages->s); break; } - strlist_delete(&messages, messages); + strlist_delete(&messages, &messages); } if (gg.startpoint) crosshair(gg.posx, gg.posy); diff --git a/log/src/loged.c b/log/src/loged.c index 0903e10..c741326 100644 --- a/log/src/loged.c +++ b/log/src/loged.c @@ -3259,7 +3259,7 @@ long num; while (l1 != NULL && (long)l1->value != num) l1 = l1->next; if (l1 != NULL) - strlist_delete(&kind.pinnames, l1); + strlist_delete(&kind.pinnames, &l1); } while (l1 != NULL); kind.pinsim[num - 1] = nopin; fixnumpins(num); @@ -3268,10 +3268,6 @@ long num; } - - - - static krec *findkind(n) char *n; { diff --git a/log/src/logntk.c b/log/src/logntk.c index 31610c7..eb30c6e 100644 --- a/log/src/logntk.c +++ b/log/src/logntk.c @@ -1422,7 +1422,7 @@ struct LOC_Log_logntk_proc *LINK; } epilog(LINK); } - strlist_delete(&defndir, defndir); + strlist_delete(&defndir, &defndir); } l1 = argslist; while (l1 != NULL) { @@ -1644,14 +1644,14 @@ struct LOC_Log_logntk_proc *LINK; } epilog(LINK); first = false; - strlist_delete(&defndir, defndir); + strlist_delete(&defndir, &defndir); } while (arglist != NULL) { if ((long)arglist->value == 0) { sprintf(STR1, "No nodes or gates called %s", arglist->s); message(STR1, LINK); } - strlist_delete(&arglist, arglist); + strlist_delete(&arglist, &arglist); } } diff --git a/log/src/logspc.c b/log/src/logspc.c index e13bbee..801da77 100644 --- a/log/src/logspc.c +++ b/log/src/logspc.c @@ -2116,7 +2116,7 @@ struct LOC_Log_logspc_proc *LINK; } epilog(LINK); } - strlist_delete(&defndir, defndir); + strlist_delete(&defndir, &defndir); } l1 = argslist; while (l1 != NULL) { @@ -2334,14 +2334,14 @@ struct LOC_Log_logspc_proc *LINK; } epilog(LINK); first = false; - strlist_delete(&defndir, defndir); + strlist_delete(&defndir, &defndir); } while (arglist != NULL) { if ((long)arglist->value == 0) { sprintf(STR1, "No nodes or gates called %s", arglist->s); message(STR1, LINK); } - strlist_delete(&arglist, arglist); + strlist_delete(&arglist, &arglist); } } diff --git a/psys/include/p2c/strlist.h b/psys/include/p2c/strlist.h index b1d18a6..1799858 100644 --- a/psys/include/p2c/strlist.h +++ b/psys/include/p2c/strlist.h @@ -4,10 +4,10 @@ typedef struct na_strlist_t { struct na_strlist_t *next; - void * value; /* user-defined */ - char kind; /* user-defined */ - char len; /* for internal use */ - char s[256]; /* not entirely allocated */ + void *value; /* user-defined */ + char kind; /* user-defined */ + size_t len; /* for internal use */ + char *s; } na_strlist_t; @@ -16,7 +16,7 @@ extern na_strlist_t *strlist_find (na_strlist_t *base, char *s); extern na_strlist_t *strlist_cifind (na_strlist_t *base, char *s); extern void strlist_change (na_strlist_t **base, na_strlist_t **ptr, char *s); extern void strlist_remove (na_strlist_t **base, char *s); -extern void strlist_delete (na_strlist_t **base, na_strlist_t *ptr); +extern void strlist_delete (na_strlist_t **base, na_strlist_t **ptr); extern void strlist_dispose (na_strlist_t **ptr); extern void strlist_empty (na_strlist_t **base); extern void strlist_copy (na_strlist_t **dest, na_strlist_t *src); diff --git a/psys/src/strlist.c b/psys/src/strlist.c index 91db1c9..2d5b9eb 100644 --- a/psys/src/strlist.c +++ b/psys/src/strlist.c @@ -9,16 +9,22 @@ void strlist_init(na_strlist_t **base) na_strlist_t *strlist_add(na_strlist_t **base, char *s) { na_strlist_t *lp; - int len = strlen(s); + size_t len = strlen(s); + /* go to good position */ while ((lp = *base) && strcmp(lp->s, s) < 0) base = &lp->next; - if (!lp || strcmp(lp->s, s)) + if (lp == NULL || strcmp(lp->s, s)) { - lp = (na_strlist_t *)malloc(sizeof(na_strlist_t) - 255 + len); - if (!lp) + lp = (na_strlist_t *)malloc(sizeof(na_strlist_t)); + if (lp == NULL) _OutMem(); + + lp->s = (char *)malloc(sizeof(char) * (len + 1)); + if (lp->s == NULL) + _OutMem(); + lp->next = *base; *base = lp; strcpy(lp->s, s); @@ -33,15 +39,20 @@ na_strlist_t *strlist_add(na_strlist_t **base, char *s) na_strlist_t *strlist_append(na_strlist_t **base, char *s) { na_strlist_t *lp; - int len = strlen(s); + size_t len = strlen(s); + /* go to end of list */ while ((lp = *base)) base = &lp->next; - lp = (na_strlist_t *)malloc(sizeof(na_strlist_t) - 255 + len); + lp = (na_strlist_t *)malloc(sizeof(na_strlist_t)); if (!lp) _OutMem(); + lp->s = (char *)malloc(sizeof(char) * (len + 1)); + if (lp->s == NULL) + _OutMem(); + lp->next = NULL; *base = lp; strcpy(lp->s, s); @@ -54,11 +65,14 @@ na_strlist_t *strlist_append(na_strlist_t **base, char *s) na_strlist_t *strlist_insert(na_strlist_t **base, char *s) { na_strlist_t *lp; - int len = strlen(s); + size_t len = strlen(s); - lp = (na_strlist_t *)malloc(sizeof(na_strlist_t) - 255 + len); + lp = (na_strlist_t *)malloc(sizeof(na_strlist_t)); if (!lp) _OutMem(); + lp->s = (char *)malloc(sizeof(char) * (len + 1)); + if (lp->s == NULL) + _OutMem(); lp->next = *base; *base = lp; @@ -85,8 +99,9 @@ na_strlist_t *strlist_cifind(na_strlist_t *base, char *s) void strlist_change(na_strlist_t **base, na_strlist_t **lp, char *s) { - na_strlist_t *lp2 = *lp, *lp3; - int len = strlen(s); + na_strlist_t *lp2 = *lp; + na_strlist_t *lp3; + size_t len = strlen(s); while ((lp3 = *base) != lp2) { @@ -99,8 +114,8 @@ void strlist_change(na_strlist_t **base, na_strlist_t **lp, char *s) /* must reallocate */ if (len > lp3->len) { - lp3 = (na_strlist_t *)realloc(lp3, sizeof(na_strlist_t) - 255 + len); - if (!lp3) + lp3->s = (char *)realloc(lp3->s, (len + 1) * sizeof(char)); + if (lp3->s == NULL) _OutMem(); lp3->len = len; @@ -116,28 +131,28 @@ void strlist_remove(na_strlist_t **base, char *s) { na_strlist_t *lp; - while ((lp = *base)) + while ((lp = *base) != NULL) { if (!strcmp(lp->s, s)) { *base = lp->next; - free(lp); + strlist_dispose(&lp); return; } base = &lp->next; } } -void strlist_delete(na_strlist_t **base, na_strlist_t *lp) +void strlist_delete(na_strlist_t **base, na_strlist_t **lp) { na_strlist_t *lp2; - while ((lp2 = *base)) + while ((lp2 = *base) != NULL) { - if (lp2 == lp) + if (lp2 == *lp) { - *base = lp->next; - free(lp); + *base = (*lp)->next; + strlist_dispose(lp); return; } base = &lp2->next; @@ -146,6 +161,7 @@ void strlist_delete(na_strlist_t **base, na_strlist_t *lp) void strlist_dispose(na_strlist_t **ptr) { + free((*ptr)->s); free(*ptr); *ptr = NULL; } @@ -154,34 +170,45 @@ void strlist_empty(na_strlist_t **base) { na_strlist_t *lp; - while ((lp = *base)) + while ((lp = *base) != NULL) { *base = lp->next; - free(lp); + strlist_dispose(&lp); } + *base = NULL; } void strlist_copy(na_strlist_t **dest, na_strlist_t *src) { - na_strlist_t *lp; + na_strlist_t *lcopy; + + strlist_empty(dest); while (src != NULL) { - lp = (na_strlist_t *)malloc(sizeof(na_strlist_t) - 255 + src->len); - lp->len = src->len; - lp->kind = src->kind; - lp->value = src->value; - strcpy(lp->s, src->s); - *dest = lp; - dest = &lp->next; - src = src->next; /* Change made by rhkoshi without much thought */ + lcopy = (na_strlist_t *)malloc(sizeof(na_strlist_t)); + if(lcopy == NULL) + _OutMem(); + lcopy->s = (char *)malloc(sizeof(char) * (src->len + 1)); + + lcopy->len = src->len; + lcopy->kind = src->kind; + lcopy->value = src->value; + strcpy(lcopy->s, src->s); + + *dest = lcopy; + + /* move to next node */ + dest = &lcopy->next; + src = src->next; } + *dest = NULL; } void strlist_dump(FILE *f, na_strlist_t *base) { - while (base) + while (base != NULL) { fprintf(f, "\"%s\" %d, %ld\n", base->s, base->kind, (long)base->value); base = base->next; -- GitLab