// // ac_assert.c // // Copyright (C) 2006 Nathan (Acorn) Pooley // // 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 by the Free Software Foundation. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License (gpl.txt) for more details. // // 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // // You can contact the author, Nathan (Acorn) Pooley, by writing // to Nathan (Acorn) Pooley, 949 Buckeye Drive, Sunnyvale, CA 94086, USA // or through the form at http://www.rawbw.com/~acorn/wand/feedback.html // /* #@DOC@ AC_ASSERT assert macro */ //########################################################################### //############################### NOTES ##################################### //########################################################################### //########################################################################### //############################### INCLUDES ################################## //########################################################################### #include <stdio.h> #include <stdlib.h> #include "ac_assert.h" //########################################################################### //############################### GLOBALS ################################### //########################################################################### static FILE *acAssertErrorFile = 0; static enum AcAssertModeEnum acAssertExitMode = AC_ASSERT_CRASH; static AcAssertFuncType acAssertFuncUser = 0; //########################################################################### //############################### CODE ###################################### //########################################################################### //=========================================================================== //;, acAssertSetUserFunc() - set func to call when assert occurs //=========================================================================== AcAssertFuncType acAssertSetUserFunc(AcAssertFuncType func) { AcAssertFuncType oldFunc = acAssertFuncUser; acAssertFuncUser = func; return oldFunc; } //=========================================================================== //;, acAssertCrash() - abort by crashing //=========================================================================== enum AcAssertModeEnum acAssertMode(enum AcAssertModeEnum mode) { int old = acAssertExitMode; if ((unsigned int)mode < (unsigned int)AC_ASSERT_NO_CHANGE) { acAssertExitMode = mode; } return old; } //=========================================================================== //;, acAssertCrash() - abort by crashing //=========================================================================== int acAssertCrash(void) { volatile static int a=1,b=0,*c=(int*)1; volatile double d=1,e=0,*f=(double*)1; fflush(stdout); fflush(stderr); if (acAssertExitMode == AC_ASSERT_EXIT) { exit(1); } else if (acAssertExitMode == AC_ASSERT_LOOP) { b = a; while(b); } else { while(1) { *c = *c + 1; *c = *c + a/b; *f = *f + d/e; } } return 0; } //=========================================================================== //;, acAssertFailed() - assert condition was not true //=========================================================================== int acAssertFailed( const char *cond, const char *func, const char *file, int line) { if (!acAssertErrorFile) { acAssertErrorFile = stderr; } fflush(stdout); if (acAssertFuncUser) { acAssertFuncUser(cond,func,file,line); } fprintf(acAssertErrorFile, "\n\nAssert failed: %s\n",cond); fprintf(acAssertErrorFile, " at %s:%d %s\n\n", file,line,func?func:""); fflush(acAssertErrorFile); return acAssertCrash(); } |
This file Copyright (C) 2006 by Nathan (Acorn) Pooley
Go to TOP Wand page
Go to Acorn's personal webpage
Go to Hogwarts website: www.gotohogwarts.com
Snout: www.snout.org/game
Gadgets of Justice Unlimited
Snout GC (Wiki)
Snout Wiki
File created by do_doc at Wed May 30 03:24:44 PDT 2007