first code for library management
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Tue, 25 Jun 2024 16:32:06 +0000 (18:32 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Tue, 25 Jun 2024 16:32:06 +0000 (18:32 +0200)
function.c
function.h
gameoflife.c
type.h

index 2957bb7ffcbaeee3230a66005942df00e50b3634..2ce081676945b8ea611b3c06758269f257d0f733 100644 (file)
@@ -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;
 }
index cfca3ff53bd5375c41ff63f9ef74f5bc66cb9c92..5438c17394f63f4105737d81ff76a7440da46cbd 100644 (file)
@@ -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);
 
index 4c59510be0594b73e0e1c7c6d3f1943ed9c61872..9882aa98417ad48362677e9389be6df6ec2e3311 100644 (file)
@@ -7,6 +7,7 @@
 #include <curses.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <time.h>
 
 #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 b32dd5651d3d5712f9c5ed99b99d776448176095..be31e3bf62321a702bc38bf29119c60971a442cd 100644 (file)
--- 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: */