From: Michel Pollet Date: Thu, 2 Mar 2017 17:03:00 +0000 (+0000) Subject: Added sim_utils.c/h X-Git-Tag: v1.6~48^2~9^2~3 X-Git-Url: https://git.htl-mechatronik.at/public/?a=commitdiff_plain;h=e2e31d1ae34fe4cdcc97be98a4bc2f35a9cd2037;p=sx%2Fsimavr.git Added sim_utils.c/h Contains my string stripper utility. Used by the VCD input module Signed-off-by: Michel Pollet --- diff --git a/simavr/sim/sim_utils.c b/simavr/sim/sim_utils.c new file mode 100644 index 0000000..eeb4986 --- /dev/null +++ b/simavr/sim/sim_utils.c @@ -0,0 +1,68 @@ +/* + sim_utils.c + + Implements a Value Change Dump file outout to generate + traces & curves and display them in gtkwave. + + Copyright 2008, 2009 Michel Pollet + + This file is part of simavr. + + simavr is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + simavr is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with simavr. If not, see . + */ + +#include +#include + +#include "sim_utils.h" + +static argv_p +argv_realloc( + argv_p argv, + uint32_t size ) +{ + argv = realloc(argv, + sizeof(argv_t) + (size * sizeof(argv->argv[0]))); + argv->size = size; + return argv; +} + +argv_p +argv_parse( + argv_p argv, + char * line ) +{ + if (!argv) + argv = argv_realloc(argv, 8); + argv->argc = 0; + + /* strip end of lines and trailing spaces */ + char *d = line + strlen(line); + while ((d - line) > 0 && *(--d) <= ' ') + *d = 0; + /* stop spaces + tabs */ + char *s = line; + while (*s && *s <= ' ') + s++; + argv->line = s; + char * a = NULL; + do { + if (argv->argc == argv->size) + argv = argv_realloc(argv, argv->size + 8); + if ((a = strsep(&s, " \t")) != NULL) + argv->argv[argv->argc++] = a; + } while (a); + argv->argv[argv->argc] = NULL; + return argv; +} diff --git a/simavr/sim/sim_utils.h b/simavr/sim/sim_utils.h new file mode 100644 index 0000000..44cb2c5 --- /dev/null +++ b/simavr/sim/sim_utils.h @@ -0,0 +1,51 @@ +/* + sim_utils.h + + Implements a Value Change Dump file outout to generate + traces & curves and display them in gtkwave. + + Copyright 2008, 2009 Michel Pollet + + This file is part of simavr. + + simavr is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + simavr is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with simavr. If not, see . + */ + +#ifndef __SIM_UTILS_H__ +#define __SIM_UTILS_H__ + +#include + +typedef struct argv_t { + uint32_t size, argc; + char * line; + char * argv[]; +} argv_t, *argv_p; + +/* + * Allocate a argv_t structure, split 'line' into words (destructively) + * and fills up argc, and argv fields with pointers to the individual + * words. The line is stripped of any \r\n as well + * You can pass an already allocated argv_t for it to be reused (and + * grown to fit). + * + * You are still responsible, as the caller, to (free) the resulting + * pointer, and the 'line' text, if appropriate, no duplication is made + */ +argv_p +argv_parse( + argv_p argv, + char * line ); + +#endif /* __SIM_UTILS_H__ */