#include "function.h"
+#define MAXNBBLOCKS 15
+
int strmaxlen (char *str, char ch)
{
int len = 0;
return (x < 10) ? '0' + x : 'A' + x - 10;
}
+int _ztoi (char x)
+{
+ return (x > '9') ? x - 'A' + 10 : x - '0';
+}
+
int _sprinttab (char *buffer, char *name, int *tab, int nb)
{
int i;
int _sprintblock (char *buffer, int id, block_t *block)
{
- int i;
int l = sprintf (buffer, "width[%d]: %d\n", id, block->width);
l += sprintf (buffer + l, "height[%d]: %d\n", id, block->height);
l += sprintf (buffer + l, "color[%d]: %d\n", id, block->color);
- l += sprintf (buffer + l, "block[%d]: \"", id);
- for (i = 0; i < block->width * block->height; i++) {
- l += sprintf (buffer + l, "%c", block->tab[i]);
- }
- l += sprintf (buffer + l, "\"\n");
+ l += sprintf (buffer + l, "block[%d]: \"%s\"\n", id, block->tab);
return l;
}
return ret;
}
-board_t *loadboard (char *str)
+int _checkblockref (char *str, char *ref)
+{
+ return ((strncmp (str, ref, strlen (ref)) == 0) &&
+ (strlen (str) > strlen (ref) + 2) &&
+ (str[strlen (str) - 1] == ']'));
+}
+
+void loadboard (char *str, board_t **pboard, blocks_t **pblocks)
{
+ int i;
int width = 0;
int height = 0;
char *tab = NULL;
-
+ int nb = 0;
+ char *x = NULL;
+ char *y = NULL;
+ char *settle = NULL;
+ block_t block[MAXNBBLOCKS];
char *saveptr1, *saveptr2;
+ memset (block, 0, sizeof (block_t) * MAXNBBLOCKS);
+
char *line = strtok_r (str, "\n", &saveptr1);
while (line) {
height = atoi (value);
} else if (strcmp (keyword, "tab") == 0) {
tab = atos (value);
+ } else if (strcmp (keyword, "nb") == 0) {
+ nb = atoi (value);
+ } else if (strcmp (keyword, "x") == 0) {
+ x = atos (value);
+ } else if (strcmp (keyword, "y") == 0) {
+ y = atos (value);
+ } else if (strcmp (keyword, "settle") == 0) {
+ settle = atos (value);
+ } else if (_checkblockref (keyword, "width")) {
+ i = atoi (keyword + 6);
+ if (i < MAXNBBLOCKS) {
+ (block + i)->width = atoi (value);
+ }
+ } else if (_checkblockref (keyword, "height")) {
+ i = atoi (keyword + 7);
+ if (i < MAXNBBLOCKS) {
+ (block + i)->height = atoi (value);
+ }
+ } else if (_checkblockref (keyword, "color")) {
+ i = atoi (keyword + 6);
+ if (i < MAXNBBLOCKS) {
+ (block + i)->color = atoi (value);
+ }
+ } else if (_checkblockref (keyword, "block")) {
+ i = atoi (keyword + 6);
+ if (i < MAXNBBLOCKS) {
+ (block + i)->tab = atos (value);
+ }
} else if (strcmp (keyword, "rem") == 0) {
/* nothing to do with remark */
} else {
memcpy (board->tab, tab, width * height);
}
- return board;
+ blocks_t *blocks = NULL;
+ int error = 1;
+ if ((nb > 0) && (nb <= MAXNBBLOCKS) &&
+ (x) && (strlen (x) == (size_t)(nb)) &&
+ (y) && (strlen (y) == (size_t)(nb)) &&
+ (settle) && (strlen (settle) == (size_t)(nb))) {
+ error = 0;
+ blocks = initblocks (nb);
+ for (i = 0; i < nb; i++) {
+ blocks->x[i] = _ztoi (x[i]);
+ blocks->y[i] = _ztoi (y[i]);
+ blocks->settle[i] = _ztoi (settle[i]);
+ block_t *b = block + i;
+ if ((b->color <= 0) ||
+ (b->tab == NULL) || ((size_t)(b->width * b->height) != strlen (b->tab))) {
+ error = 1;
+ break;
+ }
+ blocks->block[i] = initblock (b->width, b->height);
+ blocks->block[i]->color = b->color;
+ memcpy (blocks->block[i]->tab, b->tab, b->width * b->height);
+ }
+ }
+
+ if ((error) || (board == NULL) || (blocks == NULL)) {
+ freeboard (board);
+ freeblocks (blocks);
+ return;
+ }
+ *pboard = board;
+ *pblocks = blocks;
}
block_t *initblock (int width, int height)