[net.lang.c] "handy.h"

richw@ada-uts.UUCP (08/28/85)

I'm interested in coming up with an include file, say "handy.h",
which declares a lot of commonly useful "stuff".  A lot of discussion
(an AWFUL lot) has been going on about Boolean typedefs/defines --
that's the sort of thing I'm looking for:

    typedef enum { false, true } bool;

Other plausible candidates for this "second stdio.h" might be:

    typedef char *string;
    #define FORMFEED '\014'      /*  ASCII form feed  */

Any other suggestions would be appreciated.

-- Rich Wagner

bc@cyb-eng.UUCP (Bill Crews) (08/30/85)

> 
> I'm interested in coming up with an include file, say "handy.h",
>
> -- Rich Wagner

If people, when choosing names for functions, variables, files, etc, would
think at least briefly about what name would be the most meaningful to an
uninitiated reader, we readers would have a much easier time.  I would
suggest you come up with names like types.h or ctlchars.h or whatever,
segregate them functionally and name them appropriately.

-- 
  /  \    Bill Crews
 ( bc )   Cyb Systems, Inc
  \__/    Austin, Texas

[ gatech | ihnp4 | nbires | seismo | ucbvax ] ! ut-sally ! cyb-eng ! bc

meister@linus.UUCP (Phillip W. Servita) (09/02/85)

>
>I'm interested in coming up with an include file, say "handy.h",
>which declares a lot of commonly useful "stuff".  A lot of discussion
> .
> .
> .
>Any other suggestions would be appreciated.
>
>-- Rich Wagner

my personal favorite:

    #define EVER ;;

then you can say:

    for(EVER) {
        stuff;
        }
                                      -the venn buddhist

richw@ada-uts.UUCP (09/03/85)

>> I'm interested in coming up with an include file, say "handy.h",
>> -- Rich Wagner

> If people, when choosing names for functions, variables, files, etc, would
> think at least briefly about what name would be the most meaningful to an
> uninitiated reader, we readers would have a much easier time.  I would
> suggest you come up with names like types.h or ctlchars.h or whatever,
> segregate them functionally and name them appropriately.
> -- Bill Crews

"segregate them functionally" -- I'm impressed  :-)

Now that you mention it, you've got a point.  I originally didn't expect
as many responses as I've gotten, so it will make sense to create several
files (can you say ``modularity''?).

Now, can we get back to the original question?  Thanks to the replies
I've gotten via e-mail -- a (personally edited; sorry) listing of
``good stuff'' will be posted soon...

-- Rich

mike@whuxl.UUCP (BALDWIN) (09/03/85)

>I'm interested in coming up with an include file, say "handy.h",
>which declares a lot of commonly useful "stuff".

These can be "handy":

#define	NELEM(arr)	(sizeof (arr) / sizeof *(arr))
#define	LAST(arr)	((arr) + NELEM(arr))

e.g.,
	struct vegetable potato[] = {
		"burbank", 0.34,
		"new", 3.44,
		"hot", 0.12,
	};
then
	struct vegetable *pp;
	for (pp = potato; pp < LAST(potato); pp++)
or
	int i;
	for (i = 0; i < NELEM(potato); i++)

I also like the usual string stuff:

#define	EQUAL(a,b)	!strcmp(a,b)
#define	EQUALN(a,b,n)	!strncmp(a,b,n)
#define	PREFIX(a,b)	!strncmp(a,b,strlen(a))
#define	COPY(a,b)	strncpy(a,b,sizeof a)

-- 
						Michael Baldwin
						AT&T Bell Labs
						{at&t}!whuxl!mike

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (09/03/85)

>     #define EVER ;;
> 
> then you can say:
> 
>     for(EVER) {
>         stuff;
>         }

I think this illustrates one of the potential problems with
hiding the actual language underneath definitions.  If the
loop REALLY continues "for ever", then it is probably not a
good algorithm.  Possibly
	#define	repeatedly	for ( ; ; )
would be a better fit to English.  But (unlike the Boolean
data type) the extra effort of becoming familiar with this
usage does not seem to be adequately repaid in clearer code.
(Of course that's a judgement call..)

lwall@sdcrdcf.UUCP (Larry Wall) (09/04/85)

Here's a chunk of code straight out of rn:

/* some handy defs */

#define bool char
#define TRUE (1)
#define FALSE (0)
#define Null(t) ((t)0)
#define Nullch Null(char *)
#define Nullfp Null(FILE *)

#define Ctl(ch) (ch & 037)

#define strNE(s1,s2) (strcmp(s1,s2))
#define strEQ(s1,s2) (!strcmp(s1,s2))
#define strnNE(s1,s2,l) (strncmp(s1,s2,l))
#define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))

Larry Wall
{allegra,burdvax,cbosgd,hplabs,ihnp4,sdcsvax}!sdcrdcf!lwall

dave@andromeda.UUCP (Dave Bloom) (09/04/85)

 >
 >I'm interested in coming up with an include file, say "handy.h",
 >which declares a lot of commonly useful "stuff".  A lot of discussion
 > .
 > .
 > .
 >Any other suggestions would be appreciated.
 >
 >-- Rich Wagner

One of the most useful #defines I use religiously is for those times when you
want to loop indefinately. Rather than say 'for(;;)', I say:

#define loop for(;;)

Then I can say things like:

		loop {
			etc. etc. etc.
			etc. etc. etc.
			}

which seems much more to the point- Loop without condition....
-- 
-------------------------------------------------------------------------------
      allegra\					       Dave Bloom
      harvard \ pyramid\
       seismo  \  pyrnj >!andromeda!dave         HOME: (201) 868-1764
     ut-sally   >!topaz/			 WORK: (201) 648-5083
       sri-iu  /
ihnp4!packard /		           "You're never alone with a schizophrenic...."

msb@lsuc.UUCP (Mark Brader) (09/05/85)

Michael Baldwin (whuxl!mike) suggests, among others:

> #define	NELEM(arr)	(sizeof (arr) / sizeof *(arr))
> #define	LAST(arr)	((arr) + NELEM(arr))
> 	for (pp = potato; pp < LAST(potato); pp++)

and

> #define	COPY(a,b)	strncpy(a,b,sizeof a)

These are dangerous.  LAST is misnamed, because it points to the
space after the last element.  If used, it should be called
something like AFTERLAST.

And with COPY, there is a temptation to call it with arguments of the
wrong type.  Can't you just see somebody writing

	fun(s)
	char s[];
	{
		char temp[PLENTY_BIG];
		...
		COPY (s, temp);	/* WRONG */
	}

and wondering why only the first few bytes were copied?

Mark Brader

rbp@investor.UUCP (Bob Peirce) (09/06/85)

> my personal favorite:
> 
>     #define EVER ;;
> 
> then you can say:
> 
>     for(EVER) {
>         stuff;
>         }

Why not just

#define	FOREVER	for(;;)

Then you can say

	FOREVER{
	    stuff;
	}

I actually have that in my "local.h" file, yet I persist in using "for(;;)!

wohletz@unrvax.UUCP (Greg Wohletz) (09/08/85)

In article <29@andromeda.UUCP> dave@andromeda.UUCP (Dave Bloom) writes:
>
> >
> >I'm interested in coming up with an include file, say "handy.h",
> >which declares a lot of commonly useful "stuff".  A lot of discussion
> > .
> > .
> > .
> >Any other suggestions would be appreciated.
> >
> >-- Rich Wagner
>
>One of the most useful #defines I use religiously is for those times when you
>want to loop indefinately. Rather than say 'for(;;)', I say:
>
>#define loop for(;;)
>
>Then I can say things like:
>
>		loop {
>			etc. etc. etc.
>			etc. etc. etc.
>			}
>
>which seems much more to the point- Loop without condition....
>-- 
>-------------------------------------------------------------------------------
Stuff like this really stinks! Try looking at the code for `sh' and you will
understand...
				--Greg

CSNet:	greg%unlv@CSNet-Relay
UUCP:	{seismo , ucbvax!menlo70}!unr70!unrvax!unlv44!greg

mike@whuxl.UUCP (BALDWIN) (09/09/85)

> > #define	NELEM(arr)	(sizeof (arr) / sizeof *(arr))
> > #define	LAST(arr)	((arr) + NELEM(arr))
> > #define	COPY(a,b)	strncpy(a,b,sizeof a)
> 
> These are dangerous.  LAST is misnamed, because it points to the
> space after the last element.  If used, it should be called
> something like AFTERLAST.

Yeh, you're right.  LAST is too generic sounding.  Reminds me of
curses(3C), with move() and erase(), which conflict with routines of the
same name in plot(3X)!  But I still say the functions they perform
are easier to read (with suitable names) than the corresponding x+y/z...

> And with COPY, there is a temptation to call it with arguments of the
> wrong type.  Can't you just see somebody writing
> 
> 	fun(s)
> 	char s[];
> 	{
> 		char temp[PLENTY_BIG];
> 		...
> 		COPY (s, temp);	/* WRONG */
> 	}

I don't know what to call this one.  But I still think it's useful.
It's not a general-purpose string copy macro; it's just useful when 
you want to copy a string to a safe place without scrogging the safe
place.  Hmm, maybe STRBUFSAVE?  Oh hell, mebbe I'll just use strncpy!

> Mark Brader

Hey!  *My* initials are MSB!  Wild.
-- 
						Michael Scott Baldwin
						AT&T Bell Labs
						{at&t}!whuxl!mike

bet@ecsvax.UUCP (Bennett E. Todd III) (09/10/85)

I like the following:

	#define ISPREFIX(s1, s2) !strncmp(s1, s2, strlen(s1))

It isn't clean, it isn't efficient, but it makes

	if (ISPREFIX("command1", cmdbuff)) {
		...
	} else if (ISPREFIX("command2", cmdbuff)) {
		...

reasonably clean. It refers to s1 twice, and it isn't fully
parenthesized, but it works for this limited application.

Now, somebody want to post a version of the notorious bit-table macros?
God, I wish spell(1) weren't proprietary, those are REALLY cute!

-Bennett
-- 

"Hypocrisy is the vasoline of social intercourse." (Who said that?)

Bennett Todd -- Duke Computation Center, Durham, NC 27706-7756; (919) 684-3695
UUCP: ...{decvax,seismo,philabs,ihnp4,akgua}!mcnc!ecsvax!duccpc!bet

frascado@umn-cs.UUCP (Gregory Frascadore) (09/12/85)

>I'm interested in coming up with an include file, say "handy.h",
>which declares a lot of commonly useful "stuff".  A lot of discussion
>Any other suggestions would be appreciated.
>-- Rich Wagner
 
One of my favorites is:

#define repeat do
#define until( expr) while( !(expr))

I prefer repeat-until loops over do-while's 
(my pascal heritage is showing :-)


-- 
-------
				Gregory Frascadore
				Computer Science Department
				University of Minnesota
				...ihnp4!stolaf!umn-cs!frascado
 

carl@bdaemon.UUCP (carl) (09/14/85)

> 
> >> I'm interested in coming up with an include file, say "handy.h",
> >> -- Rich Wagner
> 

Without question one of the best "handy.h" files can be found in Tom Plum's
book "Learning to Program in C" where it is called "local.h".  While one
may argue about some of the definitions used by Plum, it does illustrate
what a "universally" useful include file might contain.

Carl Brandauer
daemon associates, Inc.
1760 Sunset Blvd.
Boulder, CO 80302
303-442-1731
{allegra|amd|attunix|cbosgd|ucbvax}!nbires!bdaemon!carl