[Commit] librr/src rr.h,1.14,1.15 rr_board.c,1.11,1.12

Carl Worth commit at keithp.com
Thu Jul 3 08:07:16 PDT 2003


Committed by: cworth

Update of /local/src/CVS/librr/src
In directory home.keithp.com:/tmp/cvs-serv14755/src

Modified Files:
	rr.h rr_board.c 
Log Message:
Added rr_board_create_from_file

Index: rr.h
===================================================================
RCS file: /local/src/CVS/librr/src/rr.h,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- rr.h	27 Jun 2003 03:07:25 -0000	1.14
+++ rr.h	3 Jul 2003 14:07:13 -0000	1.15
@@ -247,6 +247,9 @@
 rr_board_t *
 rr_board_create_from_str (const char *str);
 
+rr_board_t *
+rr_board_create_from_file (const char *filename);
+
 void
 rr_board_destroy (rr_board_t *board);
 

Index: rr_board.c
===================================================================
RCS file: /local/src/CVS/librr/src/rr_board.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- rr_board.c	25 Jun 2003 10:48:02 -0000	1.11
+++ rr_board.c	3 Jul 2003 14:07:13 -0000	1.12
@@ -38,6 +38,9 @@
 _rr_board_get_target_chars (rr_board_t *board, rr_cell_t cell,
 			    char *color, char *shape);
 
+static char *
+_rr_board_read_diagram (FILE *file);
+
 rr_board_t *
 rr_board_create (int width, int height)
 {
@@ -76,6 +79,55 @@
     return board;
 }
 
+rr_board_t *
+rr_board_create_from_file (const char *filename)
+{
+    FILE *file;
+    char *diagram;
+    rr_board_t *board;
+
+    file = fopen (filename, "r");
+    if (file == NULL) {
+	fprintf (stderr, "Failed to open %s for reading.\n", filename);
+	return NULL;
+    }
+
+    diagram = _rr_board_read_diagram (file);
+    if (diagram == NULL) {
+	fprintf (stderr, "Did not find a valid board diagram in %s.\n", filename);
+	return NULL;
+    }
+
+    board = rr_board_create_from_str (diagram);
+
+    free (diagram);
+
+    return board;
+}
+
+static char *
+_rr_board_read_diagram (FILE *file)
+{
+    int c;
+    rr_strbuf_t buf;
+    char *diagram;
+
+    _rr_strbuf_init (&buf);
+
+    while (1) {
+	c = fgetc (file);
+	if (c == EOF)
+	    break;
+	_rr_strbuf_append (&buf, c);
+    }
+
+    diagram = _rr_strbuf_str (&buf);
+
+    _rr_strbuf_fini (&buf);
+
+    return diagram;
+}
+
 rr_status_t
 _rr_board_init (rr_board_t *board, int width, int height)
 {
@@ -530,7 +582,13 @@
 
     s = str;
 
-    EXPECT ('\n');
+    if (s == NULL)
+	return RR_STATUS_PARSE_ERROR;
+
+    /* Consume optional initial newline */
+    if (*s == '\n')
+	s++;
+
     n = strchr (s, '\n');
     if (n == NULL)
 	return RR_STATUS_PARSE_ERROR;




More information about the Commit mailing list