Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

help removing gotos in a c function

0 views
Skip to first unread message

ss...@tiac.net

unread,
Oct 7, 1995, 3:00:00 AM10/7/95
to
help! I know in my heart that this function can be rewritten without
gotos. I would love to hear any suggestions for removing them, or
seeing
someone else's version without them. Of course, the point is to make
the
function more clear and purposeful, not less, so I'd like to avoid a
lot
of repeated code &| excessive returns.

this function is part of an extremely large program that i am
collaborating
on at work. i do not consider it proprietary because (a) it is not
particularly
useful in and of itself, (b) it uses no unique style or proprietary,
advantageous
art that I am aware of, (c) it seems entirely insane anyway, and (d)
my boss said so.

this is an ansi-c function written for HP-UX 9.0, and uses an XWindows
function.
The functions called can all be assumed to do their jobs, whatever
they are,
as expressed by this code. This function, unless you find otherwise,
can also
be assume to do its job as well.

strike a major blow against the goto-ers of the world!

thanks so much for your help!

Patrick McCuller
Scheduling Systems, Waltham MA
s...@tiac.net s...@tiac.net mccu...@marti.tiac.net
http://www.tiac.net/users/mcculler/

#define WhileSteps(lastStep) \
} while (lastStep >= step); } \

#define SetFuncName(name) \
static const char * const funcName = (const char *) #name

#define DoSteps(firstStep) { \
int step = firstStep; \
do switch (step++) {

/*
-------------------------------------------------------------------------
*/

static void RotationCallback(
DBFetchInstance *fi)
{
SetFuncName(RotationCallCallback);
int respLen;
char *p = NULL, *buf;
const char *partName = NULL;
DBFetchRefreshParam *rp;
char *flights = NULL, *tails = NULL;
int num = 0, numTails = 0, numFlights;
int partLen = 0, flightsLen = 0, tailsLen = 0;

if (!InitialCallbackCheck(fi, &buf, &respLen, funcName)) {
failedOut:
rp = NULL;

callCallback:

XtAppAddTimeOut(XApp_GetContext(), 0,
(XtTimerCallbackProc) RefreshRouting, (XtPointer) rp);
return;
}

DoSteps(1) {
case 1:
partName = "flights";

findSeparator:

if (NULL == (p = memchr(buf, DBFETCH_RESPONSE_SEPARATOR, respLen))) {
ErrorMessage("%s: no end to '%s' part\n", funcName, partName);
goto failedOut;
}

if (0 == (partLen = p - buf)) {
ErrorMessage("%s: empty '%s' part\n", funcName, partName);
goto failedOut;
}

break;

case 2:
flightsLen = partLen;
flights = buf;

partName = "tails";

advanceToNextPart:

respLen -= partLen + 1; /* \ + 1 to bypass separator */
buf = p + 1; /* / */
goto findSeparator;

case 3:
tailsLen = partLen;
tails = buf;
partName = "tails number";
goto advanceToNextPart;

case 4:
decodeNum:
if (-1 == (num = DecodePosNum(buf, partLen))) {
ErrorMessage("%s: invalid %s\n", funcName, partName);
goto failedOut;
}

break;

case 5:
numTails = num;
partName = "flights number";
goto advanceToNextPart;

case 6:
goto decodeNum;
} WhileSteps(6);

numFlights = num;

rp = Allocate(DBFetchRefreshParam, 1);
rp->numTails = numTails;
rp->tails = Allocate(char, tailsLen);
memcpy(rp->tails, tails, tailsLen);
rp->numFlights = numFlights;
rp->flights = Allocate(char, flightsLen);
memcpy(rp->flights, flights, flightsLen);
goto callCallback;
}

Michael Smith

unread,
Oct 10, 1995, 3:00:00 AM10/10/95
to
It would be a major job to fix up this code. The main problem is that
the logic flow goes all over the place. You would really need to
re-think the whole flow of the function.

The code has no comments which makes it hard to understand exactly what
you are trying to do in places. If you really need to fix the code, it
might help to annotate the source liberally with comments and re-post
it. Remember that we always find it easier to read our own code than
somebody elses code as we (usually) remember what we were thinking when
we wrote it. The logic may be less obvious to someone else. It may also
help to provide structure definitions so that the code can be compiled.

Good luck ;-)

--
#####################################################################
Michael Smith msm...@mpx.com.au

Emmenjay Consulting
Making computers work for you, not the other way around.

PO Box 909 Ph 018 240 704
Kensington 2033
AUSTRALIA
#####################################################################

0 new messages