From de25edcfc0a5c21c7af59d744918a4a99f309f3a Mon Sep 17 00:00:00 2001 From: Thomas Jensen Date: Sat, 14 Aug 1999 19:11:53 +0000 Subject: [PATCH] Added findshape(), genshape(), freeshape(), and isdeepempty() functions Added on_side() back in with a few changes Changed empty_side() to use isdeepempty() -> much simpler code --- src/shape.c | 181 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 164 insertions(+), 17 deletions(-) diff --git a/src/shape.c b/src/shape.c index efc528a..062ae59 100644 --- a/src/shape.c +++ b/src/shape.c @@ -4,7 +4,7 @@ * Date created: June 23, 1999 (Wednesday, 13:39h) * Author: Copyright (C) 1999 Thomas Jensen * tsjensen@stud.informatik.uni-erlangen.de - * Version: $Id: shape.c,v 1.3 1999/07/22 12:28:25 tsjensen Exp tsjensen $ + * Version: $Id: shape.c,v 1.4 1999/07/23 16:38:02 tsjensen Exp tsjensen $ * Language: ANSI C * World Wide Web: http://home.pages.de/~jensen/boxes/ * Purpose: Shape handling and information functions @@ -25,6 +25,10 @@ * Revision History: * * $Log: shape.c,v $ + * Revision 1.4 1999/07/23 16:38:02 tsjensen + * Removed functions iscorner(), on_side(), shapecmp(), both_on_side(), and + * shape_distance() - nobody was using them anyway. + * * Revision 1.3 1999/07/22 12:28:25 tsjensen * Added GNU GPL disclaimer * Added include config.h @@ -43,11 +47,13 @@ #include #include #include +#include #include "shape.h" #include "boxes.h" +#include "tools.h" static const char rcsid_shape_c[] = - "$Id: shape.c,v 1.3 1999/07/22 12:28:25 tsjensen Exp tsjensen $"; + "$Id: shape.c,v 1.4 1999/07/23 16:38:02 tsjensen Exp tsjensen $"; @@ -66,6 +72,131 @@ shape_t *sides[] = { north_side, east_side, south_side, west_side }; +shape_t findshape (const sentry_t *sarr, const int num) +/* + * Find a non-empty shape and return its name + * + * sarr the shape array to check + * num number of entries in sarr to be checked + * + * RETURNS: a shape_name on success + * num on error (e.g. empty shape array) + * +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ +{ + int i; + + for (i=0; i LINE_MAX) { + fprintf (stderr, "%s: internal error\n", PROJECT); + return 1; + } + + *chars = (char **) calloc (height, sizeof(char *)); + if (*chars == NULL) { + perror (PROJECT); + return 2; + } + + for (j=0; j0; --j) + BFREE ((*chars)[j-1]); + BFREE (*chars); + return 3; + } + memset ((*chars)[j], ' ', width); + } + + return 0; +} + + + +void freeshape (sentry_t *shape) +/* + * Free all memory allocated by the shape and set the struct to + * SENTRY_INITIALIZER. Do not free memory of the struct. + * +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ +{ + size_t j; + + for (j=0; jheight; ++j) + BFREE (shape->chars[j]); + BFREE (shape->chars); + + *shape = SENTRY_INITIALIZER; +} + + + int isempty (const sentry_t *shape) /* * Return true if shape is empty. @@ -85,6 +216,31 @@ int isempty (const sentry_t *shape) +int isdeepempty (const sentry_t *shape) +/* + * Return true if shape is empty, also checking if lines consist of spaces + * only. + * +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ +{ + size_t j; + + if (isempty (shape)) + return 1; + + for (j=0; jheight; ++j) { + if (shape->chars[j]) { + if (strspn (shape->chars[j], " ") != shape->width) + return 0; + } + } + + return 1; +} + + + size_t highest (const sentry_t *sarr, const int n, ...) /* * Return height (vert.) of highest shape in given list. @@ -208,12 +364,12 @@ shape_t leftmost (const int aside, const int cnt) -int empty_side (sentry_t *d, const int aside) +int empty_side (sentry_t *sarr, const int aside) /* * Return true if the shapes on the given side consist entirely out of * spaces - and spaces only, tabs are considered non-empty. * - * d pointer to shape list of design to check + * sarr pointer to shape list of design to check * aside the box side (one of BTOP etc.) * * RETURNS: == 0 side is not empty @@ -222,22 +378,13 @@ int empty_side (sentry_t *d, const int aside) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ { - int i; - size_t j; - sentry_t *cs; /* current shape */ - char *p; + int i; for (i=0; iheight; ++j) { - p = cs->chars[j]; - while (*p && *p == ' ') - ++p; - if (*p) - return 0; /* side is not empty */ - } + else + return 0; /* side is not empty */ } return 1; /* side is empty */