From: Laurent MAZET Date: Tue, 25 Jun 2024 16:32:06 +0000 (+0200) Subject: first code for library management X-Git-Tag: v1.0~10 X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=1f6c5e7fd03521f537a5a8aec7c90e598434ef48;p=gameoflife.git first code for library management --- diff --git a/function.c b/function.c index 2957bb7..2ce0816 100644 --- a/function.c +++ b/function.c @@ -159,7 +159,7 @@ char *atos (char *str) return ret; } -board_t *loadboard (char *str) +board_t *loadboard (char *str, char **pname) { char *name = NULL; int xsize = 0; @@ -200,6 +200,9 @@ board_t *loadboard (char *str) board = initboard (xsize, ysize); memcpy (board->tab, tab, xsize * ysize); } + if ((board != NULL) && (*pname != NULL)) { + *pname = strdup (name); + } return board; } diff --git a/function.h b/function.h index cfca3ff..5438c17 100644 --- a/function.h +++ b/function.h @@ -27,7 +27,7 @@ int writedata (char *name, char *data); char *readdata (char *filename); -board_t *loadboard (char *str); +board_t *loadboard (char *str, char **pname); char *getcell (board_t *board, int x, int y); diff --git a/gameoflife.c b/gameoflife.c index 4c59510..9882aa9 100644 --- a/gameoflife.c +++ b/gameoflife.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "debug.h" @@ -23,7 +24,7 @@ char *progname = NULL; char *version = "0.1"; -char *filename = NULL; +char *listoffilename = NULL; char *playname = NULL; char mode = 'e'; int speed = 5; @@ -53,7 +54,7 @@ int usage (int ret) { FILE *fd = ret ? stderr : stdout; fprintf (fd, "usage: %s [-e file] [-h] [-p file] [-s tens] [-v level] [-x int] [-y int]\n", progname); - fprintf (fd, " -e: edit file (%s)\n", (filename) ? filename : "none"); + fprintf (fd, " -e: list of file for edit (%s)\n", (listoffilename) ? listoffilename : "none"); fprintf (fd, " -h: help message\n"); fprintf (fd, " -p: play file (%s)\n", (playname) ? playname : "none"); fprintf (fd, " -s: speed Ä­n tens/sec (%d)\n", speed); @@ -65,7 +66,7 @@ int usage (int ret) return ret; } -board_t *processfile (char *filename, char **name) +board_t *processfile (char *filename, char **pname) { char *buffer = readdata (filename); if (buffer == NULL) { @@ -73,7 +74,7 @@ board_t *processfile (char *filename, char **name) return NULL; } - board_t *board = loadboard (buffer); + board_t *board = loadboard (buffer, pname); if (board == NULL) { VERBOSE (ERROR, fprintf (stderr, "incorrect file (%s)\n", filename)); } @@ -83,6 +84,39 @@ board_t *processfile (char *filename, char **name) return board; } +library_t *processlibrary (char *libname) +{ + int n = 0; + library_t *lib = (library_t *) malloc ((n + 2) * sizeof (library_t)); + CHECKALLOC (lib); + + (lib + n)->board = initboard (1, 1); + (lib + n)->board->tab[0] = 'X'; + (lib + n)->name = strdup ("Cell"); + n++; + + if (libname) { + char *saveptr; + char *filename = strtok_r (libname, ":", &saveptr); + while (filename) { + char *name; + n++; + lib = (library_t *) realloc (lib, (n + 2) * sizeof (library_t)); + CHECKALLOC (lib); + + (lib + n)->board = processfile (filename, &name); + if ((lib + n)->board != NULL) { + (lib + n)->name = name; + n++; + } + + filename = strtok_r (NULL, "\n", &saveptr); + } + } + + return lib; +} + /* main function */ int main (int argc, char *argv[]) { @@ -112,7 +146,7 @@ int main (int argc, char *argv[]) return usage (1); } mode = 'e'; - filename = arg; + listoffilename = arg; break; case 'p': arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; @@ -166,19 +200,9 @@ int main (int argc, char *argv[]) return 1; } - /* load element */ - board_t *element = NULL; - int xelement = 0; - int yelement = 0; - if (filename) { - element = processfile (filename, NULL); - if (element == NULL) { - return 1; - } - } else { /* mono cell */ - element = initboard (1, 1); - element->tab[0] = 'X'; - } + /* load library */ + library_t *lib = processlibrary (listoffilename); + board_t *element = (lib + 0)->board; /* load playground */ board_t *playground = NULL; @@ -219,8 +243,8 @@ int main (int argc, char *argv[]) int yboard = xoffset + 1; int xhelp = xboard + xoffset + 1 + board->xsize; int yhelp = yboard; - xelement = (xsize - element->xsize) / 2; - yelement = (ysize - element->ysize) / 2; + int xelement = (xsize - element->xsize) / 2; + int yelement = (ysize - element->ysize) / 2; /* init windows */ helpwindow (help, xhelp, yhelp); @@ -330,7 +354,12 @@ int main (int argc, char *argv[]) endwin (); - freeboard (element); + int n = 0; + while ((lib + n)->board) { + freeboard ((lib + n)->board); + free ((lib + n)->name); + } + free (lib); freeboard (board); return 0; diff --git a/type.h b/type.h index b32dd56..be31e3b 100644 --- a/type.h +++ b/type.h @@ -7,6 +7,11 @@ typedef struct { char *tab; } board_t; +typedef struct { + board_t *board; + char *name; +} library_t; + #endif /* __TYPE_H__ */ /* vim: set ts=4 sw=4 et: */