]> git.cworth.org Git - ttt/commitdiff
2005-11-05 Carl Worth <cworth@cworth.org>
authorCarl Worth <carl@theworths.org>
Sat, 5 Nov 2005 16:43:21 +0000 (16:43 +0000)
committerCarl Worth <carl@theworths.org>
Sat, 5 Nov 2005 16:43:21 +0000 (16:43 +0000)
        * src/Makefile.am: Add ttt-board.[ch] and x.[ch]

        * src/ttt-board.h:
        * src/ttt-board.c: (ttt_board_init), (ttt_board_init_from_string),
        (ttt_board_to_string), (ttt_board_write): Add some stub functions
        for Richard and Kevin to have some practice
        implementing. Functions to represent a board and to go to/from a
        string.

        * src/x.h:
        * src/x.c: (xasprintf), (xvasprintf), (xpipe), (xfork), (xmalloc),
        (xcalloc), (xrealloc), (xfdopen), (xstrdup), (xfwrite): Several
        utility functions that make system calls, check the result, and
        exit on any error. This will simplify the error checking needed in
        programs using these functions.

ChangeLog
src/Makefile.am
src/ttt-board.c [new file with mode: 0644]
src/ttt-board.h [new file with mode: 0644]
src/x.c [new file with mode: 0644]
src/x.h [new file with mode: 0644]

index 63169bbaa3538a255e95fec84f10fe87f6b91374..eedf81b665fcf146db1ea4a3b9e965e35920b746 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2005-11-05  Carl Worth  <cworth@cworth.org>
+
+       * src/Makefile.am: Add ttt-board.[ch] and x.[ch]
+       
+       * src/ttt-board.h:
+       * src/ttt-board.c: (ttt_board_init), (ttt_board_init_from_string),
+       (ttt_board_to_string), (ttt_board_write): Add some stub functions
+       for Richard and Kevin to have some practice
+       implementing. Functions to represent a board and to go to/from a
+       string.
+       
+       * src/x.h:
+       * src/x.c: (xasprintf), (xvasprintf), (xpipe), (xfork), (xmalloc),
+       (xcalloc), (xrealloc), (xfdopen), (xstrdup), (xfwrite): Several
+       utility functions that make system calls, check the result, and
+       exit on any error. This will simplify the error checking needed in
+       programs using these functions.
+       
 2005-11-05  Carl Worth  <cworth@cworth.org>
 
        * src/Makefile.am: Break things up for separate ttt-client and
 2005-11-05  Carl Worth  <cworth@cworth.org>
 
        * src/Makefile.am: Break things up for separate ttt-client and
index 9b52b44f86ae6b819e7ff40462dc8a941f5656fe..2e7ae737fdbe35e2fc2e64c19bca5d1883358b1b 100644 (file)
@@ -8,7 +8,9 @@ ttt_common_sources =            \
        ttt-command.c           \
        ttt-command.h           \
        ttt-error.c             \
        ttt-command.c           \
        ttt-command.h           \
        ttt-error.c             \
-       ttt-error.h
+       ttt-error.h             \
+       x.c                     \
+       x.h
 
 ttt_client_SOURCES =           \
        $(ttt_common_sources)   \
 
 ttt_client_SOURCES =           \
        $(ttt_common_sources)   \
diff --git a/src/ttt-board.c b/src/ttt-board.c
new file mode 100644 (file)
index 0000000..9e9db98
--- /dev/null
@@ -0,0 +1,72 @@
+/* ttt.c - client-server tic-tac-toe game
+ *
+ * Copyright © 2005 Carl Worth
+ *
+ * This program 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 2, or (at your option)
+ * any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Author: Carl Worth <cworth@cworth.org>
+ */
+
+#include "ttt-board.h"
+
+/* Initialize an empty board. */
+void
+ttt_board_init (ttt_board_t *board)
+{
+    /* XXX: NYI */
+}
+
+/* Initialize a board from its string representation.
+ *
+ * Returns: TTT_BOARD_SUCCESS or TTT_BOARD_INVALID_BOARD if the board
+ * string could not be properly parsed.
+ */
+void
+ttt_board_init_from_string (ttt_board_t *board,
+                           const char  *s)
+{
+    /* XXX: NYI */
+}
+
+/* Return the string representation of a board.
+ *
+ * The return value is a malloc()ed string that should be free()ed
+ * when no longer needed.
+ *
+ * Errors: If out-of-memory occurs, this function will not return.
+ */
+char *
+ttt_board_to_string (ttt_board_t *board)
+{
+    /* XXX: NYI */
+}
+
+/* Write a string representation of a board to the provided file.
+ *
+ * Errors: If out-of-memory or a file IO error occurs, this function
+ * will not return.
+ */
+void
+ttt_board_write (ttt_board_t *board, FILE *file)
+{
+    char *s;
+
+    s = ttt_board_to_string (board);
+
+    xfwrite (s, 1, strlen (s) + 1, file);
+
+    free (s);
+}
+
diff --git a/src/ttt-board.h b/src/ttt-board.h
new file mode 100644 (file)
index 0000000..dd2b6eb
--- /dev/null
@@ -0,0 +1,30 @@
+/* ttt.c - client-server tic-tac-toe game
+ *
+ * Copyright © 2005 Carl Worth
+ *
+ * This program 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 2, or (at your option)
+ * any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Author: Carl Worth <cworth@cworth.org>
+ */
+
+#ifndef _TTT_BOARD_H_
+#define _TTT_BOARD_H_
+
+typedef struct _ttt_board {
+    /* XXX: Fill this out with appropriate fields. */
+} ttt_board_t;
+
+#endif
+
diff --git a/src/x.c b/src/x.c
new file mode 100644 (file)
index 0000000..4062294
--- /dev/null
+++ b/src/x.c
@@ -0,0 +1,163 @@
+/* x.c - Some wrappers for various functions to check and exit on out-of-memory
+ *
+ * Copyright © 2005 Carl Worth
+ *
+ * This program 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 2, or (at your option)
+ * any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Author: Carl Worth <carl@theworths.org>
+ */
+
+#include "ttt.h"
+
+#include <stdarg.h>
+
+void
+xasprintf (char **strp, const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start (ap, fmt);
+    xvasprintf (strp, fmt, ap);
+    va_end (ap);
+}
+
+void
+xvasprintf (char **strp, const char *fmt, va_list ap)
+{
+    int ret;
+
+    ret = vasprintf (strp, fmt, ap);
+
+    if (ret == -1) {
+       fprintf (stderr, "Error: vasprintf failed: Out of memory? Aborting.\n");
+       exit (1);
+    }
+}
+
+void
+xpipe (int filedes[2])
+{
+    int ret;
+
+    ret = pipe (filedes);
+
+    if (ret == -1) {
+       fprintf (stderr, "Error: pipe failed: %s. Aborting.\n", strerror (errno));
+       exit (1);
+    }
+}
+
+pid_t
+xfork (void)
+{
+    pid_t pid;
+
+    pid = fork ();
+
+    if (pid == -1) {
+       fprintf (stderr, "Error: fork failed: %s. Aborting.\n", strerror (errno));
+       exit (1);
+    }
+
+    return pid;
+}
+
+void *
+xmalloc (size_t size)
+{
+    void *ret;
+
+    ret = malloc (size);
+
+    if (ret == NULL) {
+       fprintf (stderr, "Error: malloc failed. Out of memory.  Aborting.\n");
+       exit (1);
+    }
+
+    return ret;
+}
+
+void *
+xcalloc (size_t nmemb, size_t size)
+{
+    void *ret;
+
+    ret = calloc (nmemb, size);
+
+    if (ret == NULL) {
+       fprintf (stderr, "Error: calloc failed. Out of memory. Aborting.\n");
+       exit (1);
+    }
+
+    return ret;
+}
+
+void *
+xrealloc (void *ptr, size_t size)
+{
+    void *ret;
+
+    ret = realloc (ptr, size);
+
+    if (ret == NULL) {
+       fprintf (stderr, "Error: realloc failed: Out of memory. Aborting.\n");
+       exit (1);
+    }
+
+    return ret;
+}
+
+FILE *
+xfdopen (int filedes, const char *mode)
+{
+    FILE *ret;
+
+    ret = fdopen (filedes, mode);
+
+    if (ret == NULL) {
+       fprintf (stderr, "Error: fdopen failed: %s. Aborting.\n", strerror (errno));
+       exit (1);
+    }
+
+    return ret;
+}
+
+char *
+xstrdup (const char *s)
+{
+    char *ret;
+
+    ret = strdup (s);
+
+    if (ret == NULL) {
+       fprintf (stderr, "Error: strdup failed: %s. Aborting.\n",
+                strerror (errno));
+       exit (1);
+    }
+
+    return ret;
+}
+
+void
+xfwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream)
+{
+    int ret;
+
+    ret = fwrite (ptr, size, nmemb, stream);
+    if (ret != nmemb) {
+       fprintf (stderr, "Error: error occured during fwrite. Aborting.\n");
+       exit (1);
+    }
+}
diff --git a/src/x.h b/src/x.h
new file mode 100644 (file)
index 0000000..52c3acd
--- /dev/null
+++ b/src/x.h
@@ -0,0 +1,52 @@
+/* x.c - Some wrappers for various functions to check and exit on out-of-memory
+ *
+ * Copyright © 2005 Carl Worth
+ *
+ * This program 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 2, or (at your option)
+ * any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Author: Carl Worth <carl@theworths.org>
+ */
+
+#ifndef _X_H_
+#define _X_H_
+
+void
+xasprintf (char **strp, const char *fmt, ...) WDO_PRINTF_FORMAT(2, 3);
+
+void
+xvasprintf (char **strp, const char *fmt, va_list ap);
+
+void
+xpipe (int filedes[2]);
+
+pid_t
+xfork (void);
+
+void *
+xmalloc (size_t size);
+
+void *
+xcalloc (size_t nmemb, size_t size);
+
+void *
+xrealloc (void *ptr, size_t size);
+
+FILE *
+xfdopen (int filedes, const char *mode);
+
+char *
+xstrdup (const char *s);
+
+#endif /* _X_H_ */