mirror of
https://github.com/ascii-boxes/boxes.git
synced 2025-06-15 14:27:01 +02:00
Apply new code format from CLion
This commit is contained in:
parent
dcd2328c77
commit
f536d45f7a
737
src/generate.c
737
src/generate.c
File diff suppressed because it is too large
Load Diff
@ -5,12 +5,12 @@
|
|||||||
* This program is free software; you can redistribute it and/or modify it
|
* This program is free software; you can redistribute it and/or modify it
|
||||||
* under the terms of the GNU General Public License, version 2, as published
|
* under the terms of the GNU General Public License, version 2, as published
|
||||||
* by the Free Software Foundation.
|
* by the Free Software Foundation.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful, but
|
* This program is distributed in the hope that it will be useful, but
|
||||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
* for more details.
|
* for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License along
|
* 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.,
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
@ -26,8 +26,9 @@
|
|||||||
#define GENERATE_H
|
#define GENERATE_H
|
||||||
|
|
||||||
|
|
||||||
int generate_box (sentry_t *thebox);
|
int generate_box(sentry_t *thebox);
|
||||||
int output_box (const sentry_t *thebox);
|
|
||||||
|
int output_box(const sentry_t *thebox);
|
||||||
|
|
||||||
|
|
||||||
#endif /*GENERATE_H*/
|
#endif /*GENERATE_H*/
|
||||||
|
763
src/remove.c
763
src/remove.c
File diff suppressed because it is too large
Load Diff
147
src/shape.c
147
src/shape.c
@ -5,12 +5,12 @@
|
|||||||
* This program is free software; you can redistribute it and/or modify it
|
* This program is free software; you can redistribute it and/or modify it
|
||||||
* under the terms of the GNU General Public License, version 2, as published
|
* under the terms of the GNU General Public License, version 2, as published
|
||||||
* by the Free Software Foundation.
|
* by the Free Software Foundation.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful, but
|
* This program is distributed in the hope that it will be useful, but
|
||||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
* for more details.
|
* for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License along
|
* 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.,
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
@ -34,21 +34,25 @@
|
|||||||
|
|
||||||
|
|
||||||
char *shape_name[] = {
|
char *shape_name[] = {
|
||||||
"NW", "NNW", "N", "NNE", "NE", "ENE", "E", "ESE",
|
"NW", "NNW", "N", "NNE", "NE", "ENE", "E", "ESE",
|
||||||
"SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW"
|
"SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW"
|
||||||
};
|
};
|
||||||
|
|
||||||
shape_t north_side[SHAPES_PER_SIDE] = { NW, NNW, N, NNE, NE }; /* clockwise */
|
shape_t north_side[SHAPES_PER_SIDE] = {NW, NNW, N, NNE, NE}; /* clockwise */
|
||||||
shape_t east_side[SHAPES_PER_SIDE] = { NE, ENE, E, ESE, SE };
|
|
||||||
shape_t south_side[SHAPES_PER_SIDE] = { SE, SSE, S, SSW, SW };
|
shape_t east_side[SHAPES_PER_SIDE] = {NE, ENE, E, ESE, SE};
|
||||||
shape_t west_side[SHAPES_PER_SIDE] = { SW, WSW, W, WNW, NW };
|
|
||||||
shape_t corners[ANZ_CORNERS] = { NW, NE, SE, SW };
|
shape_t south_side[SHAPES_PER_SIDE] = {SE, SSE, S, SSW, SW};
|
||||||
shape_t *sides[] = { north_side, east_side, south_side, west_side };
|
|
||||||
|
shape_t west_side[SHAPES_PER_SIDE] = {SW, WSW, W, WNW, NW};
|
||||||
|
|
||||||
|
shape_t corners[ANZ_CORNERS] = {NW, NE, SE, SW};
|
||||||
|
|
||||||
|
shape_t *sides[] = {north_side, east_side, south_side, west_side};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
shape_t findshape(const sentry_t *sarr, const int num)
|
||||||
shape_t findshape (const sentry_t *sarr, const int num)
|
|
||||||
/*
|
/*
|
||||||
* Find a non-empty shape and return its name
|
* Find a non-empty shape and return its name
|
||||||
*
|
*
|
||||||
@ -63,11 +67,12 @@ shape_t findshape (const sentry_t *sarr, const int num)
|
|||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i=0; i<num; ++i) {
|
for (i = 0; i < num; ++i) {
|
||||||
if (isempty(sarr+i))
|
if (isempty(sarr + i)) {
|
||||||
continue;
|
continue;
|
||||||
else
|
} else {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (shape_t) i;
|
return (shape_t) i;
|
||||||
@ -75,7 +80,7 @@ shape_t findshape (const sentry_t *sarr, const int num)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
int on_side (const shape_t s, const int idx)
|
int on_side(const shape_t s, const int idx)
|
||||||
/*
|
/*
|
||||||
* Compute the side that shape s is on.
|
* Compute the side that shape s is on.
|
||||||
*
|
*
|
||||||
@ -92,13 +97,14 @@ int on_side (const shape_t s, const int idx)
|
|||||||
int i;
|
int i;
|
||||||
int found = 0;
|
int found = 0;
|
||||||
|
|
||||||
for (side=0; side<ANZ_SIDES; ++side) {
|
for (side = 0; side < ANZ_SIDES; ++side) {
|
||||||
for (i=0; i<SHAPES_PER_SIDE; ++i) {
|
for (i = 0; i < SHAPES_PER_SIDE; ++i) {
|
||||||
if (sides[side][i] == s) {
|
if (sides[side][i] == s) {
|
||||||
if (found == idx)
|
if (found == idx) {
|
||||||
return side;
|
return side;
|
||||||
else
|
} else {
|
||||||
++found;
|
++found;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -108,7 +114,7 @@ int on_side (const shape_t s, const int idx)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
int genshape (const size_t width, const size_t height, char ***chars)
|
int genshape(const size_t width, const size_t height, char ***chars)
|
||||||
/*
|
/*
|
||||||
* Generate a shape consisting of spaces only.
|
* Generate a shape consisting of spaces only.
|
||||||
*
|
*
|
||||||
@ -127,26 +133,25 @@ int genshape (const size_t width, const size_t height, char ***chars)
|
|||||||
size_t j;
|
size_t j;
|
||||||
|
|
||||||
if (width <= 0 || height <= 0 || width > LINE_MAX_BYTES) {
|
if (width <= 0 || height <= 0 || width > LINE_MAX_BYTES) {
|
||||||
fprintf (stderr, "%s: internal error\n", PROJECT);
|
fprintf(stderr, "%s: internal error\n", PROJECT);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
*chars = (char **) calloc (height, sizeof(char *));
|
*chars = (char **) calloc(height, sizeof(char *));
|
||||||
if (*chars == NULL) {
|
if (*chars == NULL) {
|
||||||
perror (PROJECT);
|
perror(PROJECT);
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j=0; j<height; ++j) {
|
for (j = 0; j < height; ++j) {
|
||||||
(*chars)[j] = (char *) calloc (width+1, sizeof(char));
|
(*chars)[j] = (char *) calloc(width + 1, sizeof(char));
|
||||||
if ((*chars)[j] == NULL) {
|
if ((*chars)[j] == NULL) {
|
||||||
perror (PROJECT);
|
perror(PROJECT);
|
||||||
for (/*empty*/; j>0; --j)
|
for (/*empty*/; j > 0; --j) BFREE ((*chars)[j - 1]);
|
||||||
BFREE ((*chars)[j-1]);
|
|
||||||
BFREE (*chars);
|
BFREE (*chars);
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
memset ((*chars)[j], ' ', width);
|
memset((*chars)[j], ' ', width);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@ -154,7 +159,7 @@ int genshape (const size_t width, const size_t height, char ***chars)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void freeshape (sentry_t *shape)
|
void freeshape(sentry_t *shape)
|
||||||
/*
|
/*
|
||||||
* Free all memory allocated by the shape and set the struct to
|
* Free all memory allocated by the shape and set the struct to
|
||||||
* SENTRY_INITIALIZER. Do not free memory of the struct.
|
* SENTRY_INITIALIZER. Do not free memory of the struct.
|
||||||
@ -164,8 +169,9 @@ void freeshape (sentry_t *shape)
|
|||||||
{
|
{
|
||||||
size_t j;
|
size_t j;
|
||||||
|
|
||||||
for (j=0; j<shape->height; ++j)
|
for (j = 0; j < shape->height; ++j) {
|
||||||
BFREE (shape->chars[j]);
|
BFREE (shape->chars[j]);
|
||||||
|
}
|
||||||
BFREE (shape->chars);
|
BFREE (shape->chars);
|
||||||
|
|
||||||
*shape = SENTRY_INITIALIZER;
|
*shape = SENTRY_INITIALIZER;
|
||||||
@ -173,26 +179,27 @@ void freeshape (sentry_t *shape)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
int isempty (const sentry_t *shape)
|
int isempty(const sentry_t *shape)
|
||||||
/*
|
/*
|
||||||
* Return true if shape is empty.
|
* Return true if shape is empty.
|
||||||
*
|
*
|
||||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
*/
|
*/
|
||||||
{
|
{
|
||||||
if (shape == NULL)
|
if (shape == NULL) {
|
||||||
return 1;
|
return 1;
|
||||||
else if (shape->chars == NULL)
|
} else if (shape->chars == NULL) {
|
||||||
return 1;
|
return 1;
|
||||||
else if (shape->width == 0 || shape->height == 0)
|
} else if (shape->width == 0 || shape->height == 0) {
|
||||||
return 1;
|
return 1;
|
||||||
else
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int isdeepempty (const sentry_t *shape)
|
int isdeepempty(const sentry_t *shape)
|
||||||
/*
|
/*
|
||||||
* Return true if shape is empty, also checking if lines consist of spaces
|
* Return true if shape is empty, also checking if lines consist of spaces
|
||||||
* only.
|
* only.
|
||||||
@ -202,13 +209,15 @@ int isdeepempty (const sentry_t *shape)
|
|||||||
{
|
{
|
||||||
size_t j;
|
size_t j;
|
||||||
|
|
||||||
if (isempty (shape))
|
if (isempty(shape)) {
|
||||||
return 1;
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
for (j=0; j<shape->height; ++j) {
|
for (j = 0; j < shape->height; ++j) {
|
||||||
if (shape->chars[j]) {
|
if (shape->chars[j]) {
|
||||||
if (strspn (shape->chars[j], " ") != shape->width)
|
if (strspn(shape->chars[j], " ") != shape->width) {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,7 +226,7 @@ int isdeepempty (const sentry_t *shape)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
size_t highest (const sentry_t *sarr, const int n, ...)
|
size_t highest(const sentry_t *sarr, const int n, ...)
|
||||||
/*
|
/*
|
||||||
* Return height (vert.) of highest shape in given list.
|
* Return height (vert.) of highest shape in given list.
|
||||||
*
|
*
|
||||||
@ -235,16 +244,17 @@ size_t highest (const sentry_t *sarr, const int n, ...)
|
|||||||
size_t max = 0; /* current maximum height */
|
size_t max = 0; /* current maximum height */
|
||||||
|
|
||||||
#if defined(DEBUG) && 0
|
#if defined(DEBUG) && 0
|
||||||
fprintf (stderr, "highest (%d, ...)\n", n);
|
fprintf (stderr, "highest (%d, ...)\n", n);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
va_start (ap, n);
|
va_start (ap, n);
|
||||||
|
|
||||||
for (i=0; i<n; ++i) {
|
for (i = 0; i < n; ++i) {
|
||||||
shape_t r = va_arg (ap, shape_t);
|
shape_t r = va_arg (ap, shape_t);
|
||||||
if (!isempty (sarr + r)) {
|
if (!isempty(sarr + r)) {
|
||||||
if (sarr[r].height > max)
|
if (sarr[r].height > max) {
|
||||||
max = sarr[r].height;
|
max = sarr[r].height;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -255,7 +265,7 @@ size_t highest (const sentry_t *sarr, const int n, ...)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
size_t widest (const sentry_t *sarr, const int n, ...)
|
size_t widest(const sentry_t *sarr, const int n, ...)
|
||||||
/*
|
/*
|
||||||
* Return width (horiz.) of widest shape in given list.
|
* Return width (horiz.) of widest shape in given list.
|
||||||
*
|
*
|
||||||
@ -273,19 +283,20 @@ size_t widest (const sentry_t *sarr, const int n, ...)
|
|||||||
size_t max = 0; /* current maximum width */
|
size_t max = 0; /* current maximum width */
|
||||||
|
|
||||||
#if defined(DEBUG) && 0
|
#if defined(DEBUG) && 0
|
||||||
fprintf (stderr, "widest (%d, ...)\n", n);
|
fprintf (stderr, "widest (%d, ...)\n", n);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
va_start (ap, n);
|
va_start (ap, n);
|
||||||
|
|
||||||
for (i=0; i<n; ++i) {
|
for (i = 0; i < n; ++i) {
|
||||||
shape_t r = va_arg (ap, shape_t);
|
shape_t r = va_arg (ap, shape_t);
|
||||||
if (!isempty (sarr + r)) {
|
if (!isempty(sarr + r)) {
|
||||||
if (sarr[r].width > max)
|
if (sarr[r].width > max) {
|
||||||
max = sarr[r].width;
|
max = sarr[r].width;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
va_end (ap);
|
va_end (ap);
|
||||||
|
|
||||||
return max;
|
return max;
|
||||||
@ -293,7 +304,7 @@ size_t widest (const sentry_t *sarr, const int n, ...)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
shape_t leftmost (const int aside, const int cnt)
|
shape_t leftmost(const int aside, const int cnt)
|
||||||
/*
|
/*
|
||||||
* Return leftmost existing shape in specification for side aside
|
* Return leftmost existing shape in specification for side aside
|
||||||
* (BTOP or BBOT), skipping cnt shapes. Corners are not considered.
|
* (BTOP or BBOT), skipping cnt shapes. Corners are not considered.
|
||||||
@ -307,18 +318,21 @@ shape_t leftmost (const int aside, const int cnt)
|
|||||||
int c = 0;
|
int c = 0;
|
||||||
int s;
|
int s;
|
||||||
|
|
||||||
if (cnt < 0)
|
if (cnt < 0) {
|
||||||
return ANZ_SHAPES;
|
return ANZ_SHAPES;
|
||||||
|
}
|
||||||
|
|
||||||
if (aside == BTOP) {
|
if (aside == BTOP) {
|
||||||
s = 0;
|
s = 0;
|
||||||
do {
|
do {
|
||||||
++s;
|
++s;
|
||||||
while (s < SHAPES_PER_SIDE-1 &&
|
while (s < SHAPES_PER_SIDE - 1 &&
|
||||||
isempty(opt.design->shape + north_side[s]))
|
isempty(opt.design->shape + north_side[s])) {
|
||||||
++s;
|
++s;
|
||||||
if (s == SHAPES_PER_SIDE-1)
|
}
|
||||||
|
if (s == SHAPES_PER_SIDE - 1) {
|
||||||
return ANZ_SHAPES;
|
return ANZ_SHAPES;
|
||||||
|
}
|
||||||
} while (c++ < cnt);
|
} while (c++ < cnt);
|
||||||
return north_side[s];
|
return north_side[s];
|
||||||
}
|
}
|
||||||
@ -327,10 +341,12 @@ shape_t leftmost (const int aside, const int cnt)
|
|||||||
s = SHAPES_PER_SIDE - 1;
|
s = SHAPES_PER_SIDE - 1;
|
||||||
do {
|
do {
|
||||||
--s;
|
--s;
|
||||||
while (s && isempty(opt.design->shape + south_side[s]))
|
while (s && isempty(opt.design->shape + south_side[s])) {
|
||||||
--s;
|
--s;
|
||||||
if (!s)
|
}
|
||||||
|
if (!s) {
|
||||||
return ANZ_SHAPES;
|
return ANZ_SHAPES;
|
||||||
|
}
|
||||||
} while (c++ < cnt);
|
} while (c++ < cnt);
|
||||||
return south_side[s];
|
return south_side[s];
|
||||||
}
|
}
|
||||||
@ -340,7 +356,7 @@ shape_t leftmost (const int aside, const int cnt)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
int empty_side (sentry_t *sarr, 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
|
* Return true if the shapes on the given side consist entirely out of
|
||||||
* spaces - and spaces only, tabs are considered non-empty.
|
* spaces - and spaces only, tabs are considered non-empty.
|
||||||
@ -356,11 +372,12 @@ int empty_side (sentry_t *sarr, const int aside)
|
|||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i=0; i<SHAPES_PER_SIDE; ++i) {
|
for (i = 0; i < SHAPES_PER_SIDE; ++i) {
|
||||||
if (isdeepempty (sarr + sides[aside][i]))
|
if (isdeepempty(sarr + sides[aside][i])) {
|
||||||
continue;
|
continue;
|
||||||
else
|
} else {
|
||||||
return 0; /* side is not empty */
|
return 0;
|
||||||
|
} /* side is not empty */
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1; /* side is empty */
|
return 1; /* side is empty */
|
||||||
|
217
src/tools.c
217
src/tools.c
@ -5,12 +5,12 @@
|
|||||||
* This program is free software; you can redistribute it and/or modify it
|
* This program is free software; you can redistribute it and/or modify it
|
||||||
* under the terms of the GNU General Public License, version 2, as published
|
* under the terms of the GNU General Public License, version 2, as published
|
||||||
* by the Free Software Foundation.
|
* by the Free Software Foundation.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful, but
|
* This program is distributed in the hope that it will be useful, but
|
||||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
* for more details.
|
* for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License along
|
* 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.,
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
@ -35,7 +35,7 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
int yyerror (const char *fmt, ...)
|
int yyerror(const char *fmt, ...)
|
||||||
/*
|
/*
|
||||||
* Print configuration file parser errors.
|
* Print configuration file parser errors.
|
||||||
*
|
*
|
||||||
@ -46,10 +46,10 @@ int yyerror (const char *fmt, ...)
|
|||||||
|
|
||||||
va_start (ap, fmt);
|
va_start (ap, fmt);
|
||||||
|
|
||||||
fprintf (stderr, "%s: %s: line %d: ", PROJECT,
|
fprintf(stderr, "%s: %s: line %d: ", PROJECT,
|
||||||
yyfilename? yyfilename: "(null)", tjlineno);
|
yyfilename ? yyfilename : "(null)", tjlineno);
|
||||||
vfprintf (stderr, fmt, ap);
|
vfprintf(stderr, fmt, ap);
|
||||||
fputc ('\n', stderr);
|
fputc('\n', stderr);
|
||||||
|
|
||||||
va_end (ap);
|
va_end (ap);
|
||||||
|
|
||||||
@ -58,23 +58,23 @@ int yyerror (const char *fmt, ...)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void regerror (char *msg)
|
void regerror(char *msg)
|
||||||
/*
|
/*
|
||||||
* Print regular expression andling error messages
|
* Print regular expression andling error messages
|
||||||
*
|
*
|
||||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
*/
|
*/
|
||||||
{
|
{
|
||||||
fprintf (stderr, "%s: %s: line %d: %s\n",
|
fprintf(stderr, "%s: %s: line %d: %s\n",
|
||||||
PROJECT, yyfilename? yyfilename: "(null)",
|
PROJECT, yyfilename ? yyfilename : "(null)",
|
||||||
opt.design->current_rule? opt.design->current_rule->line: 0,
|
opt.design->current_rule ? opt.design->current_rule->line : 0,
|
||||||
msg);
|
msg);
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int strisyes (const char *s)
|
int strisyes(const char *s)
|
||||||
/*
|
/*
|
||||||
* Determine if the string s has a contents indicating "yes".
|
* Determine if the string s has a contents indicating "yes".
|
||||||
*
|
*
|
||||||
@ -86,26 +86,28 @@ int strisyes (const char *s)
|
|||||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
*/
|
*/
|
||||||
{
|
{
|
||||||
if (s == NULL)
|
if (s == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (!strncasecmp ("on", s, 3))
|
if (!strncasecmp("on", s, 3)) {
|
||||||
return 1;
|
return 1;
|
||||||
else if (!strncasecmp ("yes", s, 4))
|
} else if (!strncasecmp("yes", s, 4)) {
|
||||||
return 1;
|
return 1;
|
||||||
else if (!strncasecmp ("true", s, 5))
|
} else if (!strncasecmp("true", s, 5)) {
|
||||||
return 1;
|
return 1;
|
||||||
else if (!strncmp ("1", s, 2))
|
} else if (!strncmp("1", s, 2)) {
|
||||||
return 1;
|
return 1;
|
||||||
else if (!strncasecmp ("t", s, 2))
|
} else if (!strncasecmp("t", s, 2)) {
|
||||||
return 1;
|
return 1;
|
||||||
else
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int strisno (const char *s)
|
int strisno(const char *s)
|
||||||
/*
|
/*
|
||||||
* Determine if the string s has a contents indicating "no".
|
* Determine if the string s has a contents indicating "no".
|
||||||
*
|
*
|
||||||
@ -117,50 +119,53 @@ int strisno (const char *s)
|
|||||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
*/
|
*/
|
||||||
{
|
{
|
||||||
if (s == NULL)
|
if (s == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (!strncasecmp ("off", s, 4))
|
if (!strncasecmp("off", s, 4)) {
|
||||||
return 1;
|
return 1;
|
||||||
else if (!strncasecmp ("no", s, 3))
|
} else if (!strncasecmp("no", s, 3)) {
|
||||||
return 1;
|
return 1;
|
||||||
else if (!strncasecmp ("false", s, 6))
|
} else if (!strncasecmp("false", s, 6)) {
|
||||||
return 1;
|
return 1;
|
||||||
else if (!strncmp ("0", s, 2))
|
} else if (!strncmp("0", s, 2)) {
|
||||||
return 1;
|
return 1;
|
||||||
else if (!strncasecmp ("f", s, 2))
|
} else if (!strncasecmp("f", s, 2)) {
|
||||||
return 1;
|
return 1;
|
||||||
else
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void concat_strings (char *dst, int max_len, int count, ...)
|
void concat_strings(char *dst, int max_len, int count, ...)
|
||||||
/*
|
/*
|
||||||
* Concatenate a variable number of strings into a fixed-length buffer.
|
* Concatenate a variable number of strings into a fixed-length buffer.
|
||||||
*
|
*
|
||||||
* dst Destination array
|
* dst Destination array
|
||||||
* max_len Maximum resulting string length (including terminating NULL).
|
* max_len Maximum resulting string length (including terminating NULL).
|
||||||
* count Number of source strings.
|
* count Number of source strings.
|
||||||
*
|
*
|
||||||
* The concatenation process terminates when either the destination
|
* The concatenation process terminates when either the destination
|
||||||
* buffer is full or all 'count' strings are processed. Null string
|
* buffer is full or all 'count' strings are processed. Null string
|
||||||
* pointers are treated as empty strings.
|
* pointers are treated as empty strings.
|
||||||
*
|
*
|
||||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
*/
|
*/
|
||||||
{
|
{
|
||||||
va_list va;
|
va_list va;
|
||||||
const char *src;
|
const char *src;
|
||||||
|
|
||||||
va_start (va, count);
|
va_start (va, count);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Sanity check.
|
* Sanity check.
|
||||||
*/
|
*/
|
||||||
if (max_len < 1)
|
if (max_len < 1) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (max_len == 1 || count < 1) {
|
if (max_len == 1 || count < 1) {
|
||||||
*dst = '\0';
|
*dst = '\0';
|
||||||
@ -170,7 +175,7 @@ void concat_strings (char *dst, int max_len, int count, ...)
|
|||||||
/*
|
/*
|
||||||
* Loop over all input strings.
|
* Loop over all input strings.
|
||||||
*/
|
*/
|
||||||
while (count-->0 && max_len > 1) {
|
while (count-- > 0 && max_len > 1) {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Grab an input string pointer. If it's NULL, skip it (eg. treat
|
* Grab an input string pointer. If it's NULL, skip it (eg. treat
|
||||||
@ -178,10 +183,11 @@ void concat_strings (char *dst, int max_len, int count, ...)
|
|||||||
*/
|
*/
|
||||||
src = va_arg (va, const char *);
|
src = va_arg (va, const char *);
|
||||||
|
|
||||||
if (src == NULL)
|
if (src == NULL) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Concatenate 'src' onto 'dst', as long as we have room.
|
* Concatenate 'src' onto 'dst', as long as we have room.
|
||||||
*/
|
*/
|
||||||
while (*src && max_len > 1) {
|
while (*src && max_len > 1) {
|
||||||
@ -200,7 +206,7 @@ void concat_strings (char *dst, int max_len, int count, ...)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
int empty_line (const line_t *line)
|
int empty_line(const line_t *line)
|
||||||
/*
|
/*
|
||||||
* Return true if line is empty.
|
* Return true if line is empty.
|
||||||
*
|
*
|
||||||
@ -212,22 +218,25 @@ int empty_line (const line_t *line)
|
|||||||
char *p;
|
char *p;
|
||||||
size_t j;
|
size_t j;
|
||||||
|
|
||||||
if (!line)
|
if (!line) {
|
||||||
return 1;
|
return 1;
|
||||||
if (line->text == NULL || line->len <= 0)
|
}
|
||||||
|
if (line->text == NULL || line->len <= 0) {
|
||||||
return 1;
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
for (p=line->text, j=0; *p && j<line->len; ++j, ++p) {
|
for (p = line->text, j = 0; *p && j < line->len; ++j, ++p) {
|
||||||
if (*p != ' ' && *p != '\t' && *p != '\r')
|
if (*p != ' ' && *p != '\t' && *p != '\r') {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
size_t expand_tabs_into (const char *input_buffer, const size_t in_len,
|
size_t expand_tabs_into(const char *input_buffer, const size_t in_len,
|
||||||
const int tabstop, char **text, size_t **tabpos, size_t *tabpos_len)
|
const int tabstop, char **text, size_t **tabpos, size_t *tabpos_len)
|
||||||
/*
|
/*
|
||||||
* Expand tab chars in input_buffer and store result in text.
|
* Expand tab chars in input_buffer and store result in text.
|
||||||
*
|
*
|
||||||
@ -248,51 +257,56 @@ size_t expand_tabs_into (const char *input_buffer, const size_t in_len,
|
|||||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
*/
|
*/
|
||||||
{
|
{
|
||||||
static char temp [LINE_MAX_BYTES*MAX_TABSTOP+1]; /* work string */
|
static char temp[LINE_MAX_BYTES * MAX_TABSTOP + 1]; /* work string */
|
||||||
size_t ii; /* position in input string */
|
size_t ii; /* position in input string */
|
||||||
size_t io; /* position in work string */
|
size_t io; /* position in work string */
|
||||||
size_t jp; /* tab expansion jump point */
|
size_t jp; /* tab expansion jump point */
|
||||||
size_t tabnum; /* number of tabs in input */
|
size_t tabnum; /* number of tabs in input */
|
||||||
|
|
||||||
*text = NULL;
|
*text = NULL;
|
||||||
|
|
||||||
for (ii=0, *tabpos_len=0; ii<in_len; ++ii) {
|
for (ii = 0, *tabpos_len = 0; ii < in_len; ++ii) {
|
||||||
if (input_buffer[ii] == '\t')
|
if (input_buffer[ii] == '\t') {
|
||||||
(*tabpos_len)++;
|
(*tabpos_len)++;
|
||||||
}
|
}
|
||||||
if (opt.tabexp != 'k')
|
}
|
||||||
*tabpos_len = 0;
|
if (opt.tabexp != 'k') {
|
||||||
if (*tabpos_len > 0) {
|
*tabpos_len = 0;
|
||||||
*tabpos = (size_t *) calloc ((*tabpos_len) + 1, sizeof(size_t));
|
}
|
||||||
if (*tabpos == NULL) {
|
if (*tabpos_len > 0) {
|
||||||
return 0; /* out of memory */
|
*tabpos = (size_t *) calloc((*tabpos_len) + 1, sizeof(size_t));
|
||||||
}
|
if (*tabpos == NULL) {
|
||||||
}
|
return 0; /* out of memory */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (ii=0, io=0, tabnum=0; ii < in_len && ((int) io) < (LINE_MAX_BYTES*tabstop-1); ++ii) {
|
for (ii = 0, io = 0, tabnum = 0; ii < in_len && ((int) io) < (LINE_MAX_BYTES * tabstop - 1); ++ii) {
|
||||||
if (input_buffer[ii] == '\t') {
|
if (input_buffer[ii] == '\t') {
|
||||||
if (*tabpos_len > 0) {
|
if (*tabpos_len > 0) {
|
||||||
(*tabpos)[tabnum++] = io;
|
(*tabpos)[tabnum++] = io;
|
||||||
}
|
}
|
||||||
for (jp=io+tabstop-(io%tabstop); io<jp; ++io)
|
for (jp = io + tabstop - (io % tabstop); io < jp; ++io) {
|
||||||
temp[io] = ' ';
|
temp[io] = ' ';
|
||||||
}
|
}
|
||||||
else {
|
}
|
||||||
temp[io] = input_buffer[ii];
|
else {
|
||||||
++io;
|
temp[io] = input_buffer[ii];
|
||||||
}
|
++io;
|
||||||
}
|
}
|
||||||
temp[io] = '\0';
|
}
|
||||||
|
temp[io] = '\0';
|
||||||
|
|
||||||
*text = (char *) strdup (temp);
|
*text = (char *) strdup(temp);
|
||||||
if (*text == NULL) return 0;
|
if (*text == NULL) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
return io;
|
return io;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void btrim (char *text, size_t *len)
|
void btrim(char *text, size_t *len)
|
||||||
/*
|
/*
|
||||||
* Remove trailing whitespace from line.
|
* Remove trailing whitespace from line.
|
||||||
*
|
*
|
||||||
@ -302,7 +316,7 @@ void btrim (char *text, size_t *len)
|
|||||||
long idx = (long) (*len) - 1;
|
long idx = (long) (*len) - 1;
|
||||||
|
|
||||||
while (idx >= 0 && (text[idx] == '\n' || text[idx] == '\r'
|
while (idx >= 0 && (text[idx] == '\n' || text[idx] == '\r'
|
||||||
|| text[idx] == '\t' || text[idx] == ' '))
|
|| text[idx] == '\t' || text[idx] == ' ')) /**/
|
||||||
{
|
{
|
||||||
text[idx--] = '\0';
|
text[idx--] = '\0';
|
||||||
}
|
}
|
||||||
@ -312,7 +326,7 @@ void btrim (char *text, size_t *len)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
char *my_strnrstr (const char *s1, const char *s2, const size_t s2_len, int skip)
|
char *my_strnrstr(const char *s1, const char *s2, const size_t s2_len, int skip)
|
||||||
/*
|
/*
|
||||||
* Return pointer to last occurrence of string s2 in string s1.
|
* Return pointer to last occurrence of string s2 in string s1.
|
||||||
*
|
*
|
||||||
@ -330,24 +344,29 @@ char *my_strnrstr (const char *s1, const char *s2, const size_t s2_len, int skip
|
|||||||
char *p;
|
char *p;
|
||||||
int comp;
|
int comp;
|
||||||
|
|
||||||
if (!s2 || *s2 == '\0')
|
if (!s2 || *s2 == '\0') {
|
||||||
return (char *) s1;
|
return (char *) s1;
|
||||||
if (!s1 || *s1 == '\0')
|
}
|
||||||
|
if (!s1 || *s1 == '\0') {
|
||||||
return NULL;
|
return NULL;
|
||||||
if (skip < 0)
|
}
|
||||||
|
if (skip < 0) {
|
||||||
skip = 0;
|
skip = 0;
|
||||||
|
}
|
||||||
|
|
||||||
p = strrchr (s1, s2[0]);
|
p = strrchr(s1, s2[0]);
|
||||||
if (!p)
|
if (!p) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
while (p >= s1) {
|
while (p >= s1) {
|
||||||
comp = strncmp (p, s2, s2_len);
|
comp = strncmp(p, s2, s2_len);
|
||||||
if (comp == 0) {
|
if (comp == 0) {
|
||||||
if (skip--)
|
if (skip--) {
|
||||||
--p;
|
--p;
|
||||||
else
|
} else {
|
||||||
return p;
|
return p;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
--p;
|
--p;
|
||||||
@ -359,7 +378,7 @@ char *my_strnrstr (const char *s1, const char *s2, const size_t s2_len, int skip
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
char *tabbify_indent (const size_t lineno, char *indentspc, const size_t indentspc_len)
|
char *tabbify_indent(const size_t lineno, char *indentspc, const size_t indentspc_len)
|
||||||
/*
|
/*
|
||||||
* Checks if tab expansion mode is "keep", and if so, calculates a new
|
* Checks if tab expansion mode is "keep", and if so, calculates a new
|
||||||
* indentation string based on the one given. The new string contains
|
* indentation string based on the one given. The new string contains
|
||||||
@ -378,7 +397,7 @@ char *tabbify_indent (const size_t lineno, char *indentspc, const size_t indents
|
|||||||
*/
|
*/
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
char *result;
|
char *result;
|
||||||
size_t result_len;
|
size_t result_len;
|
||||||
|
|
||||||
if (opt.tabexp != 'k') {
|
if (opt.tabexp != 'k') {
|
||||||
@ -388,20 +407,20 @@ char *tabbify_indent (const size_t lineno, char *indentspc, const size_t indents
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (indentspc_len == 0) {
|
if (indentspc_len == 0) {
|
||||||
return (char *) strdup ("");
|
return (char *) strdup("");
|
||||||
}
|
}
|
||||||
|
|
||||||
result = (char *) malloc (indentspc_len + 1);
|
result = (char *) malloc(indentspc_len + 1);
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
perror (PROJECT);
|
perror(PROJECT);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
memset (result, (int)' ', indentspc_len);
|
memset(result, (int) ' ', indentspc_len);
|
||||||
result[indentspc_len] = '\0';
|
result[indentspc_len] = '\0';
|
||||||
result_len = indentspc_len;
|
result_len = indentspc_len;
|
||||||
|
|
||||||
for (i=0; i < input.lines[lineno].tabpos_len
|
for (i = 0; i < input.lines[lineno].tabpos_len
|
||||||
&& input.lines[lineno].tabpos[i] < indentspc_len; ++i)
|
&& input.lines[lineno].tabpos[i] < indentspc_len; ++i) /**/
|
||||||
{
|
{
|
||||||
size_t tpos = input.lines[lineno].tabpos[i];
|
size_t tpos = input.lines[lineno].tabpos[i];
|
||||||
size_t nspc = opt.tabstop - (tpos % opt.tabstop); /* no of spcs covered by tab */
|
size_t nspc = opt.tabstop - (tpos % opt.tabstop); /* no of spcs covered by tab */
|
||||||
|
46
src/tools.h
46
src/tools.h
@ -5,12 +5,12 @@
|
|||||||
* This program is free software; you can redistribute it and/or modify it
|
* This program is free software; you can redistribute it and/or modify it
|
||||||
* under the terms of the GNU General Public License, version 2, as published
|
* under the terms of the GNU General Public License, version 2, as published
|
||||||
* by the Free Software Foundation.
|
* by the Free Software Foundation.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful, but
|
* This program is distributed in the hope that it will be useful, but
|
||||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
* for more details.
|
* for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License along
|
* 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.,
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
@ -27,30 +27,38 @@
|
|||||||
#define TOOLS_H
|
#define TOOLS_H
|
||||||
|
|
||||||
|
|
||||||
#define BMAX(a,b) ((a)>(b)? (a):(b)) /* return the larger value */
|
#define BMAX(a, b) ((a)>(b)? (a):(b)) /* return the larger value */
|
||||||
|
|
||||||
#define BFREE(p) { /* free memory and clear pointer */ \
|
#define BFREE(p) { /* free memory and clear pointer */ \
|
||||||
if (p) { \
|
if (p) { \
|
||||||
free (p); \
|
free (p); \
|
||||||
(p) = NULL; \
|
(p) = NULL; \
|
||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int yyerror (const char *fmt, ...);
|
int yyerror(const char *fmt, ...);
|
||||||
void regerror (char *msg);
|
|
||||||
int empty_line (const line_t *line);
|
|
||||||
size_t expand_tabs_into (const char *input_buffer, const size_t in_len,
|
|
||||||
const int tabstop, char **text, size_t **tabpos, size_t *tabpos_len);
|
|
||||||
void btrim (char *text, size_t *len);
|
|
||||||
char* my_strnrstr (const char *s1, const char *s2, const size_t s2_len,
|
|
||||||
int skip);
|
|
||||||
int strisyes (const char *s);
|
|
||||||
int strisno (const char *s);
|
|
||||||
|
|
||||||
void concat_strings (char *dst, int max_len, int count, ...);
|
void regerror(char *msg);
|
||||||
|
|
||||||
|
int empty_line(const line_t *line);
|
||||||
|
|
||||||
|
size_t expand_tabs_into(const char *input_buffer, const size_t in_len,
|
||||||
|
const int tabstop, char **text, size_t **tabpos, size_t *tabpos_len);
|
||||||
|
|
||||||
|
void btrim(char *text, size_t *len);
|
||||||
|
|
||||||
|
char *my_strnrstr(const char *s1, const char *s2, const size_t s2_len,
|
||||||
|
int skip);
|
||||||
|
|
||||||
|
int strisyes(const char *s);
|
||||||
|
|
||||||
|
int strisno(const char *s);
|
||||||
|
|
||||||
|
void concat_strings(char *dst, int max_len, int count, ...);
|
||||||
|
|
||||||
|
char *tabbify_indent(const size_t lineno, char *indentspc, const size_t indentspc_len);
|
||||||
|
|
||||||
char *tabbify_indent (const size_t lineno, char *indentspc, const size_t indentspc_len);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user