[Commit] librr/src Makefile.am,1.5,1.6 rr.c,1.5,1.6 rr.h,1.9,1.10
rr_board.c,1.9,1.10 rr_client.c,1.6,1.7 rr_string.c,1.2,1.3
rrint.h,1.5,1.6
Carl Worth
commit at keithp.com
Thu Jun 19 05:39:18 PDT 2003
Committed by: cworth
Update of /local/src/CVS/librr/src
In directory home.keithp.com:/tmp/cvs-serv21660/src
Modified Files:
Makefile.am rr.c rr.h rr_board.c rr_client.c rr_string.c
rrint.h
Log Message:
Unified style of parsing code.
rr_client now delivers rr_notice_t rather than char **
Index: Makefile.am
===================================================================
RCS file: /local/src/CVS/librr/src/Makefile.am,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- Makefile.am 14 Jun 2003 07:32:26 -0000 1.5
+++ Makefile.am 19 Jun 2003 11:39:15 -0000 1.6
@@ -8,6 +8,7 @@
rr_board.c \
rr_client.c \
rr_history.c \
+ rr_notice.c \
rr_string.c \
rr_strbuf.c
Index: rr.c
===================================================================
RCS file: /local/src/CVS/librr/src/rr.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- rr.c 14 Jun 2003 07:32:26 -0000 1.5
+++ rr.c 19 Jun 2003 11:39:15 -0000 1.6
@@ -28,26 +28,22 @@
#include "rrint.h"
-#define RR_ROBOT_CHAR_BLUE 'b'
-#define RR_ROBOT_CHAR_GREEN 'g'
-#define RR_ROBOT_CHAR_RED 'r'
-#define RR_ROBOT_CHAR_YELLOW 'y'
-#define RR_ROBOT_CHAR_NONE '.'
-
-#define RR_TARGET_COLOR_CHAR_BLUE 'b'
-#define RR_TARGET_COLOR_CHAR_GREEN 'g'
-#define RR_TARGET_COLOR_CHAR_RED 'r'
-#define RR_TARGET_COLOR_CHAR_YELLOW 'y'
-#define RR_TARGET_COLOR_CHAR_MULTI 'w'
-#define RR_TARGET_COLOR_CHAR_NONE '.'
-
-#define RR_TARGET_SHAPE_CHAR_CIRCLE 'c'
-#define RR_TARGET_SHAPE_CHAR_OCTAGON 'o'
-#define RR_TARGET_SHAPE_CHAR_SQUARE 's'
-#define RR_TARGET_SHAPE_CHAR_TRIANGLE 't'
-#define RR_TARGET_SHAPE_CHAR_WHIRL 'w'
-#define RR_TARGET_SHAPE_CHAR_NONE '.'
-
+const char *
+rr_gamestate_str (rr_gamestate_t state)
+{
+ switch (state) {
+ case RR_GAMESTATE_NEW:
+ return "new";
+ case RR_GAMESTATE_BID:
+ return "bid";
+ case RR_GAMESTATE_SHOW:
+ return "show";
+ case RR_GAMESTATE_DONE:
+ return "done";
+ default:
+ return "unknown gamestate";
+ }
+}
const char *
rr_status_str (rr_status_t status)
{
@@ -121,131 +117,235 @@
}
}
-char
-_rr_robot_to_char (rr_robot_t robot)
+static struct {
+ char *str;
+ rr_robot_t robot;
+} robots[] = {
+ { "red", RR_ROBOT_RED },
+ { "yellow", RR_ROBOT_YELLOW },
+ { "green", RR_ROBOT_GREEN },
+ { "blue", RR_ROBOT_BLUE },
+};
+#define RR_NUM_ROBOTS (sizeof (robots) / sizeof (robots[0]))
+
+rr_robot_t
+_rr_robot_parse (char *str)
{
- switch (robot) {
- case RR_ROBOT_BLUE:
- return RR_ROBOT_CHAR_BLUE;
- case RR_ROBOT_GREEN:
- return RR_ROBOT_CHAR_GREEN;
- case RR_ROBOT_RED:
- return RR_ROBOT_CHAR_RED;
- case RR_ROBOT_YELLOW:
- return RR_ROBOT_CHAR_YELLOW;
- case RR_ROBOT_NONE:
- default:
- return RR_ROBOT_CHAR_NONE;
- }
+ int i;
+
+ for (i = 0; i < RR_NUM_ROBOTS; i++)
+ if (strncasecmp (robots[i].str, str, strlen (str)) == 0)
+ return robots[i].robot;
+
+ return RR_ROBOT_NONE;
}
rr_robot_t
-_rr_char_to_robot (char c)
+_rr_robot_parse_symbol (char c)
{
- switch (tolower (c)) {
- case RR_ROBOT_CHAR_BLUE:
- return RR_ROBOT_BLUE;
- case RR_ROBOT_CHAR_GREEN:
- return RR_ROBOT_GREEN;
- case RR_ROBOT_CHAR_RED:
- return RR_ROBOT_RED;
- case RR_ROBOT_CHAR_YELLOW:
- return RR_ROBOT_YELLOW;
- case RR_ROBOT_CHAR_NONE:
- default:
- return RR_ROBOT_NONE;
- }
+ int i;
+
+ for (i = 0; i < RR_NUM_ROBOTS; i++)
+ if (robots[i].str[0] == tolower (c))
+ return robots[i].robot;
+
+ return RR_ROBOT_NONE;
}
-rr_target_t
-_rr_chars_to_target (char color, char shape)
+char *
+rr_robot_str (rr_robot_t robot)
{
- rr_target_color_t target_color;
- rr_target_shape_t target_shape;
+ int i;
- switch (tolower (color)) {
- case RR_TARGET_COLOR_CHAR_BLUE:
- target_color = RR_TARGET_BLUE;
- break;
- case RR_TARGET_COLOR_CHAR_GREEN:
- target_color = RR_TARGET_GREEN;
- break;
- case RR_TARGET_COLOR_CHAR_RED:
- target_color = RR_TARGET_RED;
- break;
- case RR_TARGET_COLOR_CHAR_YELLOW:
- target_color = RR_TARGET_YELLOW;
- break;
- case RR_TARGET_COLOR_CHAR_MULTI:
- target_color = RR_TARGET_MULTI;
- break;
- case RR_TARGET_COLOR_CHAR_NONE:
- default:
- target_color = RR_TARGET_COLOR_NONE;
- break;
- }
+ for (i = 0; i < RR_NUM_ROBOTS; i++)
+ if (robots[i].robot == robot)
+ return robots[i].str;
- switch (tolower (shape)) {
- case RR_TARGET_SHAPE_CHAR_CIRCLE:
- target_shape = RR_TARGET_CIRCLE;
- break;
- case RR_TARGET_SHAPE_CHAR_OCTAGON:
- target_shape = RR_TARGET_OCTAGON;
- break;
- case RR_TARGET_SHAPE_CHAR_SQUARE:
- target_shape = RR_TARGET_SQUARE;
- break;
- case RR_TARGET_SHAPE_CHAR_TRIANGLE:
- target_shape = RR_TARGET_TRIANGLE;
- break;
- case RR_TARGET_SHAPE_CHAR_WHIRL:
- target_shape = RR_TARGET_SHAPE_WHIRL;
- break;
- case RR_TARGET_SHAPE_CHAR_NONE:
- default:
- target_shape = RR_TARGET_SHAPE_NONE;
- break;
- }
+ return "";
+}
- return (target_color | target_shape);
+char
+_rr_robot_symbol (rr_robot_t robot)
+{
+ char *str;
+
+ if (robot == RR_ROBOT_NONE)
+ return '.';
+
+ str = rr_robot_str (robot);
+
+ return str[0];
+}
+
+static struct {
+ char *str;
+ rr_target_color_t color;
+} colors[] = {
+ { "red", RR_TARGET_COLOR_RED },
+ { "yellow", RR_TARGET_COLOR_YELLOW },
+ { "green", RR_TARGET_COLOR_GREEN },
+ { "blue", RR_TARGET_COLOR_BLUE },
+ { "whirl", RR_TARGET_COLOR_WHIRL },
+};
+#define RR_NUM_COLORS (sizeof (colors) / sizeof (colors[0]))
+
+rr_target_color_t
+_rr_target_color_parse (char *str)
+{
+ int i;
+
+ for (i = 0; i < RR_NUM_COLORS; i++)
+ if (strncasecmp (colors[i].str, str, strlen (str)) == 0)
+ return colors[i].color;
+
+ return RR_TARGET_COLOR_NONE;
+}
+
+rr_target_color_t
+_rr_target_color_parse_symbol (char c)
+{
+ int i;
+
+ for (i = 0; i < RR_NUM_COLORS; i++)
+ if (colors[i].str[0] == tolower (c))
+ return colors[i].color;
+
+ return RR_TARGET_COLOR_NONE;
+}
+
+char *
+rr_target_color_str (rr_target_color_t color)
+{
+ int i;
+
+ for (i = 0; i < RR_NUM_COLORS; i++)
+ if (colors[i].color == color)
+ return colors[i].str;
+
+ return "";
}
char
-_rr_target_color_to_char (rr_target_color_t color)
+_rr_target_color_symbol (rr_target_color_t color)
{
- switch (color) {
- case RR_TARGET_BLUE:
- return RR_TARGET_COLOR_CHAR_BLUE;
- case RR_TARGET_GREEN:
- return RR_TARGET_COLOR_CHAR_GREEN;
- case RR_TARGET_RED:
- return RR_TARGET_COLOR_CHAR_RED;
- case RR_TARGET_YELLOW:
- return RR_TARGET_COLOR_CHAR_YELLOW;
- case RR_TARGET_MULTI:
- return RR_TARGET_COLOR_CHAR_MULTI;
- case RR_TARGET_NONE:
- default:
- return RR_TARGET_COLOR_CHAR_NONE;
- }
+ char *str;
+
+ if (color == RR_TARGET_COLOR_NONE)
+ return '.';
+
+ str = rr_target_color_str (color);
+
+ return str[0];
+}
+
+static struct {
+ char *str;
+ rr_target_shape_t shape;
+} shapes[] = {
+ { "circle", RR_TARGET_SHAPE_CIRCLE },
+ { "octagon", RR_TARGET_SHAPE_OCTAGON },
+ { "square", RR_TARGET_SHAPE_SQUARE },
+ { "triangle", RR_TARGET_SHAPE_TRIANGLE },
+ { "whirl", RR_TARGET_SHAPE_WHIRL },
+};
+#define RR_NUM_SHAPES (sizeof (shapes) / sizeof (shapes[0]))
+
+rr_target_shape_t
+_rr_target_shape_parse (char *str)
+{
+ int i;
+
+ for (i = 0; i < RR_NUM_SHAPES; i++)
+ if (strncasecmp (shapes[i].str, str, strlen (str)) == 0)
+ return shapes[i].shape;
+
+ return RR_TARGET_SHAPE_NONE;
+}
+
+rr_target_shape_t
+_rr_target_shape_parse_symbol (char c)
+{
+ int i;
+
+ for (i = 0; i < RR_NUM_SHAPES; i++)
+ if (shapes[i].str[0] == tolower (c))
+ return shapes[i].shape;
+
+ return RR_TARGET_SHAPE_NONE;
+}
+
+char *
+rr_target_shape_str (rr_target_shape_t shape)
+{
+ int i;
+
+ for (i = 0; i < RR_NUM_SHAPES; i++)
+ if (shapes[i].shape == shape)
+ return shapes[i].str;
+
+ return "";
}
char
-_rr_target_shape_to_char (rr_target_shape_t shape)
+_rr_target_shape_symbol (rr_target_shape_t shape)
{
- switch (shape) {
- case RR_TARGET_CIRCLE:
- return RR_TARGET_SHAPE_CHAR_CIRCLE;
- case RR_TARGET_OCTAGON:
- return RR_TARGET_SHAPE_CHAR_OCTAGON;
- case RR_TARGET_SQUARE:
- return RR_TARGET_SHAPE_CHAR_SQUARE;
- case RR_TARGET_TRIANGLE:
- return RR_TARGET_SHAPE_CHAR_TRIANGLE;
- case RR_TARGET_SHAPE_WHIRL:
- return RR_TARGET_SHAPE_CHAR_WHIRL;
- case RR_TARGET_NONE:
- default:
- return RR_TARGET_SHAPE_CHAR_NONE;
- }
+ char *str;
+
+ if (shape == RR_TARGET_SHAPE_NONE)
+ return '.';
+
+ str = rr_target_shape_str (shape);
+
+ return str[0];
+}
+
+rr_target_t
+_rr_target_parse (char *color, char *shape)
+{
+ return _rr_target_color_parse (color)
+ | _rr_target_shape_parse (shape);
}
+
+rr_target_t
+_rr_target_parse_symbols (char color, char shape)
+{
+ return _rr_target_color_parse_symbol (color)
+ | _rr_target_shape_parse_symbol (shape);
+}
+
+static struct {
+ char *str;
+ rr_direction_t direction;
+} directions[] = {
+ { "north", RR_DIRECTION_NORTH },
+ { "east", RR_DIRECTION_EAST },
+ { "south", RR_DIRECTION_SOUTH },
+ { "west", RR_DIRECTION_WEST },
+};
+#define RR_NUM_DIRECTIONS (sizeof (directions) / sizeof (directions[0]))
+
+rr_direction_t
+_rr_direction_parse (char *str)
+{
+ int i;
+
+ for (i = 0; i < RR_NUM_DIRECTIONS; i++)
+ if (strncasecmp (directions[i].str, str, strlen (str)) == 0)
+ return directions[i].direction;
+
+ return RR_DIRECTION_NONE;
+}
+
+char *
+rr_direction_str (rr_direction_t direction)
+{
+ int i;
+
+ for (i = 0; i < RR_NUM_DIRECTIONS; i++)
+ if (directions[i].direction == direction)
+ return directions[i].str;
+
+ return "";
+}
+
+
Index: rr.h
===================================================================
RCS file: /local/src/CVS/librr/src/rr.h,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- rr.h 14 Jun 2003 07:32:26 -0000 1.9
+++ rr.h 19 Jun 2003 11:39:15 -0000 1.10
@@ -87,49 +87,57 @@
typedef enum {
RR_TARGET_SHAPE_NONE = 0,
- RR_TARGET_CIRCLE = 0x0100,
- RR_TARGET_OCTAGON = 0x0200,
- RR_TARGET_SQUARE = 0x0400,
- RR_TARGET_TRIANGLE = 0x0800,
+ RR_TARGET_SHAPE_CIRCLE = 0x0100,
+ RR_TARGET_SHAPE_OCTAGON = 0x0200,
+ RR_TARGET_SHAPE_SQUARE = 0x0400,
+ RR_TARGET_SHAPE_TRIANGLE = 0x0800,
RR_TARGET_SHAPE_WHIRL = 0x1000,
} rr_target_shape_t;
-#define RR_TARGET_SHAPE_ANY (RR_TARGET_CIRCLE | RR_TARGET_OCTAGON \
- | RR_TARGET_SQUARE | RR_TARGET_TRIANGLE | RR_TARGET_SHAPE_WHIRL)
+#define RR_TARGET_SHAPE_ANY (RR_TARGET_SHAPE_CIRCLE \
+ | RR_TARGET_SHAPE_OCTAGON \
+ | RR_TARGET_SHAPE_SQUARE \
+ | RR_TARGET_SHAPE_TRIANGLE \
+ | RR_TARGET_SHAPE_WHIRL)
typedef enum {
- RR_TARGET_COLOR_NONE = 0,
- RR_TARGET_BLUE = 0x02000,
- RR_TARGET_GREEN = 0x04000,
- RR_TARGET_RED = 0x08000,
- RR_TARGET_YELLOW = 0x10000,
- RR_TARGET_MULTI = 0x20000
+ RR_TARGET_COLOR_NONE = 0,
+ RR_TARGET_COLOR_BLUE = 0x02000,
+ RR_TARGET_COLOR_GREEN = 0x04000,
+ RR_TARGET_COLOR_RED = 0x08000,
+ RR_TARGET_COLOR_YELLOW = 0x10000,
+ RR_TARGET_COLOR_WHIRL = 0x20000
} rr_target_color_t;
-#define RR_TARGET_COLOR_ANY (RR_TARGET_BLUE | RR_TARGET_GREEN \
- | RR_TARGET_RED | RR_TARGET_YELLOW | RR_TARGET_MULTI)
+#define RR_TARGET_COLOR_ANY (RR_TARGET_COLOR_BLUE \
+ | RR_TARGET_COLOR_GREEN \
+ | RR_TARGET_COLOR_RED \
+ | RR_TARGET_COLOR_YELLOW \
+ | RR_TARGET_COLOR_WHIRL)
typedef enum {
RR_TARGET_NONE = 0,
- RR_TARGET_BLUE_CIRCLE = RR_TARGET_BLUE | RR_TARGET_CIRCLE,
- RR_TARGET_BLUE_OCTAGON = RR_TARGET_BLUE | RR_TARGET_OCTAGON,
- RR_TARGET_BLUE_SQUARE = RR_TARGET_BLUE | RR_TARGET_SQUARE,
- RR_TARGET_BLUE_TRIANGLE = RR_TARGET_BLUE | RR_TARGET_TRIANGLE,
- RR_TARGET_GREEN_CIRCLE = RR_TARGET_GREEN | RR_TARGET_CIRCLE,
- RR_TARGET_GREEN_OCTAGON = RR_TARGET_GREEN | RR_TARGET_OCTAGON,
- RR_TARGET_GREEN_SQUARE = RR_TARGET_GREEN | RR_TARGET_SQUARE,
- RR_TARGET_GREEN_TRIANGLE = RR_TARGET_GREEN | RR_TARGET_TRIANGLE,
- RR_TARGET_RED_CIRCLE = RR_TARGET_RED | RR_TARGET_CIRCLE,
- RR_TARGET_RED_OCTAGON = RR_TARGET_RED | RR_TARGET_OCTAGON,
- RR_TARGET_RED_SQUARE = RR_TARGET_RED | RR_TARGET_SQUARE,
- RR_TARGET_RED_TRIANGLE = RR_TARGET_RED | RR_TARGET_TRIANGLE,
- RR_TARGET_YELLOW_CIRCLE = RR_TARGET_YELLOW | RR_TARGET_CIRCLE,
- RR_TARGET_YELLOW_OCTAGON = RR_TARGET_YELLOW | RR_TARGET_OCTAGON,
- RR_TARGET_YELLOW_SQUARE = RR_TARGET_YELLOW | RR_TARGET_SQUARE,
- RR_TARGET_YELLOW_TRIANGLE = RR_TARGET_YELLOW | RR_TARGET_TRIANGLE,
- RR_TARGET_WHIRL = RR_TARGET_MULTI | RR_TARGET_SHAPE_WHIRL
+ RR_TARGET_BLUE_CIRCLE = RR_TARGET_COLOR_BLUE | RR_TARGET_SHAPE_CIRCLE,
+ RR_TARGET_BLUE_OCTAGON = RR_TARGET_COLOR_BLUE | RR_TARGET_SHAPE_OCTAGON,
+ RR_TARGET_BLUE_SQUARE = RR_TARGET_COLOR_BLUE | RR_TARGET_SHAPE_SQUARE,
+ RR_TARGET_BLUE_TRIANGLE = RR_TARGET_COLOR_BLUE | RR_TARGET_SHAPE_TRIANGLE,
+ RR_TARGET_GREEN_CIRCLE = RR_TARGET_COLOR_GREEN | RR_TARGET_SHAPE_CIRCLE,
+ RR_TARGET_GREEN_OCTAGON = RR_TARGET_COLOR_GREEN | RR_TARGET_SHAPE_OCTAGON,
+ RR_TARGET_GREEN_SQUARE = RR_TARGET_COLOR_GREEN | RR_TARGET_SHAPE_SQUARE,
+ RR_TARGET_GREEN_TRIANGLE = RR_TARGET_COLOR_GREEN | RR_TARGET_SHAPE_TRIANGLE,
+ RR_TARGET_RED_CIRCLE = RR_TARGET_COLOR_RED | RR_TARGET_SHAPE_CIRCLE,
+ RR_TARGET_RED_OCTAGON = RR_TARGET_COLOR_RED | RR_TARGET_SHAPE_OCTAGON,
+ RR_TARGET_RED_SQUARE = RR_TARGET_COLOR_RED | RR_TARGET_SHAPE_SQUARE,
+ RR_TARGET_RED_TRIANGLE = RR_TARGET_COLOR_RED | RR_TARGET_SHAPE_TRIANGLE,
+ RR_TARGET_YELLOW_CIRCLE = RR_TARGET_COLOR_YELLOW | RR_TARGET_SHAPE_CIRCLE,
+ RR_TARGET_YELLOW_OCTAGON = RR_TARGET_COLOR_YELLOW | RR_TARGET_SHAPE_OCTAGON,
+ RR_TARGET_YELLOW_SQUARE = RR_TARGET_COLOR_YELLOW | RR_TARGET_SHAPE_SQUARE,
+ RR_TARGET_YELLOW_TRIANGLE = RR_TARGET_COLOR_YELLOW | RR_TARGET_SHAPE_TRIANGLE,
+ RR_TARGET_WHIRL = RR_TARGET_COLOR_WHIRL | RR_TARGET_SHAPE_WHIRL
} rr_target_t;
#define RR_TARGET_ANY (RR_TARGET_SHAPE_ANY | RR_TARGET_COLOR_ANY)
typedef enum {
+ RR_DIRECTION_NONE,
+
RR_DIRECTION_UP,
RR_DIRECTION_NORTH = RR_DIRECTION_UP,
@@ -434,13 +442,34 @@
rr_client_notice_pending (rr_client_t *client);
rr_status_t
-rr_client_next_notice (rr_client_t *client, char ***notice);
+rr_client_next_notice (rr_client_t *client, rr_notice_t **notice);
+
+/* rr_notice.c */
rr_notice_t *
-rr_client_parse_notice (rr_client_t *client, char **notice);
+rr_notice_create (char **words);
+
+void
+rr_notice_destroy (rr_notice_t *notice);
+
/* rr.c */
const char *
+rr_gamestate_str (rr_gamestate_t state);
+
+const char *
rr_status_str (rr_status_t status);
+
+char *
+rr_direction_str (rr_direction_t direction);
+
+char *
+rr_robot_str (rr_robot_t robot);
+
+char *
+rr_target_color_str (rr_target_color_t color);
+
+char *
+rr_target_shape_str (rr_target_shape_t shape);
#endif
Index: rr_board.c
===================================================================
RCS file: /local/src/CVS/librr/src/rr_board.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- rr_board.c 14 Jun 2003 07:32:26 -0000 1.9
+++ rr_board.c 19 Jun 2003 11:39:15 -0000 1.10
@@ -407,7 +407,7 @@
rr_robot_t robot;
robot = RR_CELL_GET_ROBOT (cell);
- *r = _rr_robot_to_char (robot);
+ *r = _rr_robot_symbol (robot);
if (robot == board->goal_robot)
*r = toupper (*r);
@@ -422,8 +422,8 @@
rr_target_t target;
target = RR_CELL_GET_TARGET (cell);
- *color = _rr_target_color_to_char (target & RR_TARGET_COLOR_ANY);
- *shape = _rr_target_shape_to_char (target & RR_TARGET_SHAPE_ANY);
+ *color = _rr_target_color_symbol (target & RR_TARGET_COLOR_ANY);
+ *shape = _rr_target_shape_symbol (target & RR_TARGET_SHAPE_ANY);
if (target == board->goal_target) {
*color = toupper (*color);
@@ -535,11 +535,11 @@
else if (c != ' ')
return RR_STATUS_PARSE_ERROR;
c = NEXT;
- robot = _rr_char_to_robot (c);
+ robot = _rr_robot_parse_symbol (c);
rr_board_add_robot (board, i, j, robot, isupper (c));
c = NEXT;
d = NEXT;
- target = _rr_chars_to_target (c, d);
+ target = _rr_target_parse_symbols (c, d);
rr_board_add_target (board, i, j, target, isupper (c));
}
EXPECT ('|');
@@ -560,3 +560,5 @@
return RR_STATUS_SUCCESS;
}
+
+
Index: rr_client.c
===================================================================
RCS file: /local/src/CVS/librr/src/rr_client.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- rr_client.c 14 Jun 2003 09:09:13 -0000 1.6
+++ rr_client.c 19 Jun 2003 11:39:15 -0000 1.7
@@ -400,7 +400,7 @@
return RR_STATUS_NO_MEMORY;
}
for (p = &client->notice_queue; *p; p = &(*p)->next);
- e->notice = arg;
+ e->notice = rr_notice_create (arg);
e->next = 0;
*p = e;
@@ -508,7 +508,7 @@
if (status)
return status;
- *diagram = _rr_string_dup (args[0]);
+ *diagram = strdup (args[0]);
free (args);
return RR_STATUS_SUCCESS;
}
@@ -738,289 +738,28 @@
}
rr_status_t
-rr_client_next_notice (rr_client_t *client, char ***notice)
+rr_client_next_notice (rr_client_t *client, rr_notice_t **notice)
{
rr_notice_elt_t *elt = client->notice_queue;
rr_status_t status;
- char **arg;
- if (elt)
- {
+ char **words;
+
+ if (elt) {
client->notice_queue = elt->next;
*notice = elt->notice;
free (elt);
return RR_STATUS_SUCCESS;
}
- status = _rr_client_read_line (client, &arg);
+
+ status = _rr_client_read_line (client, &words);
if (status != RR_STATUS_SUCCESS)
return status;
- _rr_strings_cdr (arg);
- *notice = arg;
- return RR_STATUS_SUCCESS;
-}
-
-static rr_notice_t *
-_rr_notice_create (rr_notice_type_t type, int extra)
-{
- rr_notice_t *n = malloc (sizeof (rr_notice_t) + extra);
- if (!n)
- return 0;
- memset (n, '\0', sizeof (rr_notice_t) + extra);
- n->type = type;
- return n;
-}
-
-static rr_robot_t
-_rr_parse_robot (char *name)
-{
- static struct {
- char *name;
- rr_robot_t robot;
- } robots[] = {
- { "red", RR_ROBOT_RED },
- { "yellow", RR_ROBOT_YELLOW },
- { "green", RR_ROBOT_GREEN },
- { "blue", RR_ROBOT_BLUE },
- };
-#define RR_NUM_ROBOTS (sizeof (robots) / sizeof (robots[0]))
- int i;
-
- for (i = 0; i < RR_NUM_ROBOTS; i++)
- if (!strcmp (robots[i].name, name))
- return robots[i].robot;
- return robots[0].robot;
-}
-
-static rr_target_color_t
-_rr_parse_color (char *name)
-{
- static struct {
- char *name;
- rr_target_color_t color;
- } colors[] = {
- { "red", RR_TARGET_RED },
- { "yellow", RR_TARGET_YELLOW },
- { "green", RR_TARGET_GREEN },
- { "blue", RR_TARGET_BLUE },
- { "whirl", RR_TARGET_MULTI },
- };
-#define RR_NUM_COLORS (sizeof (colors) / sizeof (colors[0]))
- int i;
-
- for (i = 0; i < RR_NUM_COLORS; i++)
- if (!strcmp (colors[i].name, name))
- return colors[i].color;
- return colors[0].color;
-}
-
-static rr_target_shape_t
-_rr_parse_shape (char *name)
-{
- static struct {
- char *name;
- rr_target_shape_t shape;
- } shapes[] = {
- { "circle", RR_TARGET_CIRCLE },
- { "octagon", RR_TARGET_OCTAGON },
- { "square", RR_TARGET_SQUARE },
- { "triangle", RR_TARGET_TRIANGLE },
- { "whirl", RR_TARGET_SHAPE_WHIRL },
- };
-#define RR_NUM_SHAPES (sizeof (shapes) / sizeof (shapes[0]))
- int i;
-
- for (i = 0; i < RR_NUM_SHAPES; i++)
- if (!strcmp (shapes[i].name, name))
- return shapes[i].shape;
- return shapes[0].shape;
-}
-
-static rr_direction_t
-_rr_parse_direction (char *name)
-{
- static struct {
- char *name;
- rr_direction_t direction;
- } directions[] = {
- { "north", RR_DIRECTION_NORTH },
- { "east", RR_DIRECTION_EAST },
- { "south", RR_DIRECTION_SOUTH },
- { "west", RR_DIRECTION_WEST },
- };
-#define RR_NUM_DIRECTIONS (sizeof (directions) / sizeof (directions[0]))
- int i;
-
- for (i = 0; i < RR_NUM_DIRECTIONS; i++)
- if (!strcmp (directions[i].name, name))
- return directions[i].direction;
- return directions[0].direction;
-}
-
-static int
-_rr_parse_int (char *n)
-{
- return atoi (n);
-}
-
-static rr_notice_t *
-_rr_notice_void (rr_notice_type_t type, char **notice)
-{
- return _rr_notice_create (type, 0);
-}
-
-static rr_notice_t *
-_rr_notice_string (rr_notice_type_t type, char **notice)
-{
- rr_notice_t *n = _rr_notice_create (type, strlen (notice[1]) + 1);
- if (!n)
- return 0;
- n->u.string = (char *) (n + 1);
- strcpy (n->u.string, notice[1]);
- return n;
-}
-static rr_notice_t *
-_rr_notice_message (rr_notice_type_t type, char **notice)
-{
- rr_notice_t *n = _rr_notice_create (type,
- strlen (notice[1]) + 1 +
- strlen (notice[2]) + 1);
- if (!n)
- return 0;
- n->u.message.username = (char *) (n + 1);
- n->u.message.text = n->u.message.username + strlen (notice[1]) + 1;
- strcpy (n->u.message.username, notice[1]);
- strcpy (n->u.message.text, notice[2]);
- return n;
-}
-
-static rr_notice_t *
-_rr_notice_gamestate (rr_notice_type_t type, char **notice)
-{
- rr_notice_t *n = _rr_notice_create (type, 0);
- static struct {
- char *name;
- rr_gamestate_t gamestate;
- } gamestates[] = {
- {"NEW", RR_GAMESTATE_NEW},
- {"BID", RR_GAMESTATE_BID},
- {"SHOW",RR_GAMESTATE_SHOW},
- {"DONE",RR_GAMESTATE_DONE}
- };
-
-#define RR_NUM_GAMESTATES (sizeof (gamestates) / sizeof (gamestates[0]))
- int i;
-
- if (!n)
- return 0;
- for (i = 0; i < RR_NUM_GAMESTATES; i++)
- if (!strcmp (notice[1], gamestates[i].name))
- {
- n->u.gamestate = gamestates[i].gamestate;
- return n;
- }
- free (n);
- return 0;
-}
-
-static rr_notice_t *
-_rr_notice_target (rr_notice_type_t type, char **notice)
-{
- rr_notice_t *n = _rr_notice_create (type, 0);
- if (!n)
- return 0;
- n->u.target = _rr_parse_color (notice[1]) | _rr_parse_shape (notice[2]);
- return n;
-}
-
-static rr_notice_t *
-_rr_notice_bid (rr_notice_type_t type, char **notice)
-{
- rr_notice_t *n = _rr_notice_create (type, strlen (notice[1]) + 1);
- if (!n)
- return 0;
- n->u.bid.username = (char *) (n + 1);
- strcpy (n->u.bid.username, notice[1]);
- n->u.bid.number = _rr_parse_int(notice[2]);
- return n;
-}
-
-static rr_notice_t *
-_rr_notice_number (rr_notice_type_t type, char **notice)
-{
- rr_notice_t *n = _rr_notice_create (type, 0);
- if (!n)
- return 0;
- n->u.number = _rr_parse_int(notice[1]);
- return n;
-}
-
-static rr_notice_t *
-_rr_notice_move (rr_notice_type_t type, char **notice)
-{
- rr_notice_t *n = _rr_notice_create (type, 0);
- if (!n)
- return 0;
- n->u.move.count = _rr_parse_int(notice[1]);
- n->u.move.robot = _rr_parse_robot(notice[2]);
- n->u.move.direction = _rr_parse_direction (notice[3]);
- return n;
-}
-
-static rr_notice_t *
-_rr_notice_position (rr_notice_type_t type, char **notice)
-{
- rr_notice_t *n = _rr_notice_create (type, 0);
- if (!n)
- return 0;
- n->u.position.robot = _rr_parse_robot (notice[1]);
- n->u.position.x = _rr_parse_int (notice[2]);
- n->u.position.y = _rr_parse_int (notice[3]);
- return n;
-}
-
-static struct {
- char *name;
- rr_notice_type_t type;
- rr_notice_t *(*func) (rr_notice_type_t, char **);
-} notices[] = {
- /* global game notices */
- { "USER", RR_NOTICE_USER, _rr_notice_string },
- { "QUIT", RR_NOTICE_QUIT, _rr_notice_void },
- { "GAME", RR_NOTICE_GAME, _rr_notice_string },
- { "DISPOSE", RR_NOTICE_DISPOSE, _rr_notice_void},
- { "MESSAGE", RR_NOTICE_MESSAGE, _rr_notice_message },
- { "GAMESTATE", RR_NOTICE_GAMESTATE, _rr_notice_gamestate },
- { "TURN", RR_NOTICE_TURN, _rr_notice_target },
- { "GAME", RR_NOTICE_GAME, _rr_notice_string },
- { "JOIN", RR_NOTICE_JOIN, _rr_notice_string },
- { "WATCH", RR_NOTICE_WATCH, _rr_notice_string },
- { "PART", RR_NOTICE_PART, _rr_notice_string },
- /* bid notices */
- { "BID", RR_NOTICE_BID, _rr_notice_bid },
- { "REVOKE", RR_NOTICE_REVOKE, _rr_notice_string },
- { "TIMER", RR_NOTICE_TIMER, _rr_notice_number },
- { "ABANDON", RR_NOTICE_ABANDON, _rr_notice_string },
- { "NOBID", RR_NOTICE_NOBID, _rr_notice_string },
- /* solving notices */
- { "ACTIVE", RR_NOTICE_ACTIVE, _rr_notice_bid },
- { "MOVE", RR_NOTICE_MOVE, _rr_notice_move },
- { "UNDO", RR_NOTICE_UNDO, _rr_notice_void },
- { "RESET", RR_NOTICE_RESET, _rr_notice_void },
- { "POSITION", RR_NOTICE_POSITION, _rr_notice_position },
- { "SCORE", RR_NOTICE_SCORE, _rr_notice_bid },
- /* user notices */
- { "ACTIVATE", RR_NOTICE_ACTIVATE, _rr_notice_number }
-};
-
-#define NUM_NOTICES (sizeof (notices) / sizeof (notices[0]))
+ _rr_strings_cdr (words);
-rr_notice_t *
-rr_client_parse_notice (rr_client_t *client, char **notice)
-{
- int i;
+ *notice = rr_notice_create (words);
+ if (*notice == NULL)
+ return RR_STATUS_NO_MEMORY;
- for (i = 0; i < NUM_NOTICES; i++)
- if (!strcmp (notice[0], notices[i].name))
- return (*notices[i].func) (notices[i].type, notice);
- return 0;
+ return RR_STATUS_SUCCESS;
}
Index: rr_string.c
===================================================================
RCS file: /local/src/CVS/librr/src/rr_string.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- rr_string.c 12 Jun 2003 07:09:09 -0000 1.2
+++ rr_string.c 19 Jun 2003 11:39:15 -0000 1.3
@@ -79,12 +79,3 @@
memmove (args, args + 1, l * sizeof (char *));
}
-char *
-_rr_string_dup (char *s)
-{
- char *r = malloc (strlen (s) + 1);
- if (!r)
- return 0;
- strcpy (r, s);
- return r;
-}
Index: rrint.h
===================================================================
RCS file: /local/src/CVS/librr/src/rrint.h,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- rrint.h 12 Jun 2003 07:09:09 -0000 1.5
+++ rrint.h 19 Jun 2003 11:39:15 -0000 1.6
@@ -98,11 +98,11 @@
typedef struct rr_command {
rr_cmd_t cmd;
char *arg;
-} rr_command_t, rr_request_t, rr_response_t;
+} rr_command_t;
typedef struct rr_notice_elt {
struct rr_notice_elt *next;
- char **notice;
+ rr_notice_t *notice;
} rr_notice_elt_t;
#define RR_BUF_SIZE 1024
@@ -127,20 +127,42 @@
} rr_strstrbuf_t;
/* rr.c */
-char
-_rr_robot_to_char (rr_robot_t robot);
rr_robot_t
-_rr_char_to_robot (char c);
+_rr_robot_parse (char *str);
-rr_target_t
-_rr_chars_to_target (char color, char shape);
+rr_robot_t
+_rr_robot_parse_symbol (char c);
char
-_rr_target_color_to_char (rr_target_color_t color);
+_rr_robot_symbol (rr_robot_t robot);
+
+rr_target_color_t
+_rr_target_color_parse (char *str);
+
+rr_target_color_t
+_rr_target_color_parse_symbol (char c);
char
-_rr_target_shape_to_char (rr_target_shape_t shape);
+_rr_target_color_symbol (rr_target_color_t color);
+
+rr_target_shape_t
+_rr_target_shape_parse (char *str);
+
+rr_target_shape_t
+_rr_target_shape_parse_symbol (char c);
+
+char
+_rr_target_shape_symbol (rr_target_shape_t shape);
+
+rr_target_t
+_rr_target_parse (char *color, char *shape);
+
+rr_target_t
+_rr_target_parse_symbols (char color, char shape);
+
+rr_direction_t
+_rr_direction_parse (char *str);
/* rr_board.c */
@@ -209,8 +231,5 @@
void
_rr_strings_cdr (char **args);
-
-char *
-_rr_string_dup (char *str);
#endif
More information about the Commit
mailing list