/* daves.h david cary's headers. (non-project-specific stuff i expect to use for most of my programs) (supposedly kinda-sorta portable) 1997 Sep 25: hacked version *only* for GNU C++. Jun 30: reviewed; organized a little. ?? started. 1995 If you're going to define DEBUG, do it *before* you include all your headers. I usually include all these files in each .c file: //#include (included by daves.h ?) #include #include // Mac types -- NULL, Byte, etc. #include // Windows types -- NULL, BYTE, WORD, etc. #include on the Mac platform, think about including these files: #include // #include // already included in the default Think C environment. // all Symantec C compiler predefine __SC__ . // Think C compilers predefine THINK_C (at least mine does). think about: use 'const' more, and break programs up into modules with lots of 'static' functions (so the function isn't visible outside that module). Last change: DC 28 Sep 97 5:35 pm */ /* typedefs */ typedef unsigned char byte; typedef unsigned char BYTE; /* there's no reason to use these except when setting up *other* typedefs ... typedef unsigned char uchar; typedef signed char schar; */ typedef unsigned int uint; typedef unsigned long int ulong; /* Already defined in C++. enum bool{ false = 0, true = 1 }; Danger: defining true may cause subtle errors, because in C there's no one True value. */ /* #defined constants */ #define SUCCESS 0 //#include should already define INT_MAX in a decent environment) /* #defined better names for operators. */ // these following bitwise and logical operators are from // _The C++ Report_, June 1991, p. 8; // from a proposal by B. Stroustrup // ...logic operators on *BOOLEAN* values False (0) and True (anything but 0) #define and && #define or || // ...*bitwise* logical operators (on bit patterns) #define bitand & #define bitor | //...(exclusive OR) #define bitxor ^ #define bitcomplement ~ #define chokes_on_equals #ifndef chokes_on_equals #define bitand= &= #define bitor= |= #define bitxor= ^= #endif // David Cary just made up the following: // (or plagarized them from other sources) // logic operators on *BOOLEAN* values False (0) and True (anything but 0) #define and_then && #define or_else || /* I use "and_then" to emphasize places where order *does* matter. I use plain "and" to hint that it doesn't matter what order it's done in. */ #define NOT ! // *logical* exclusive OR //#define XOR(A,B) ((!A)^(!B)) #define MOD % /* There's a *big* difference between bitwise and logical operations. if( 1 and 2){ / * always executed * / }; if( 1 bitand 2 ) { / * never executed -- lint should warn that this isn't a Boolean. * / }; */ /* #defined macros */ /* misc fragments by David Cary */ #define nybble2ascii(n) ( ((n)<=9) ? ((n)+'0') : ((n)-0xA+'A') ) #define sign(a) ( (0<(a)) ? 1 :( (0==(a)) ? 0 : -1 ) ) #define adjust_range( min, sample, max) \ { if ( max < sample) \ { max = sample; \ }else if ( sample < min ) \ { min = sample; \ }; \ } #define Odd(n) ((Boolean)((n) bitand 1)) #define Even(n) (0 == ((n) bitand 1) ) /* I forget which magazine I saw this FOR idea in. */ #define FOR(x,iterations) for( x = 0; x < (iterations); x++ ) /* ANSI C is required to have the assert() macro. DAV's code always uses ASSERT(), so that if i feel in the mood for more information about an error, i only have to redefine ASSERT. */ #include "assert.h" //#define ASSERT(f) printf( "(Confirming [" #f "] is true." ); assert(f) #define ASSERT(f) assert(f) /* this should compile down to the appropriate constant. works well with arrays initialized to unknown size, like char message[] = "this is only a test"; */ #define ARRAY_LENGTH(x) ( sizeof(x) / sizeof( x[0] ) /* DAV's name scheme: prefix meaning g global variable p pointer u unsigned s signed str string b boolean suffix meaning _t type (either a struct or a typedef) _MAX maximum value for that type. *always* a #defined constant. for example, variables of type tilt_t should always be in the range +- tilt_MAX; ; variables of type score_t should always be in the range 0 to score_MAX ... etc. _count a count of how *many* there are (in an array thing_t thing[20] items, count can be from 0 to 20 inclusive) _index the *index* of the current point we're looking at. (in an array of thing_t thing[20] items, index can be from 0 to 19 inclusive). example: Boolean gbDone; // global boolean, named Done. I'm thinking about adding these standards, from Mark & Reed's _Mac C Programming_ book: k #defined constant all variables start with lower-case letter. all functions start with a capital letter. */