diff options
Diffstat (limited to 'labb5/lib/StanfordCPPLib/private')
| -rwxr-xr-x | labb5/lib/StanfordCPPLib/private/_main.h_ | 218 | ||||
| -rwxr-xr-x | labb5/lib/StanfordCPPLib/private/main.h | 62 | ||||
| -rwxr-xr-x | labb5/lib/StanfordCPPLib/private/randompatch.h | 63 | ||||
| -rwxr-xr-x | labb5/lib/StanfordCPPLib/private/tokenpatch.h | 11 | ||||
| -rwxr-xr-x | labb5/lib/StanfordCPPLib/private/tplatform.h | 25 |
5 files changed, 379 insertions, 0 deletions
diff --git a/labb5/lib/StanfordCPPLib/private/_main.h_ b/labb5/lib/StanfordCPPLib/private/_main.h_ new file mode 100755 index 0000000..5392d18 --- /dev/null +++ b/labb5/lib/StanfordCPPLib/private/_main.h_ @@ -0,0 +1,218 @@ +/* + * File: main.h + * ------------ + * This file renames the <code>main</code> method in the client's + * program to <code>Main</code>, thereby allowing a custom + * <code>main</code> method in the libraries to take control + * before passing control back to the client program. The main macro + * also defines a function getMainFlags that returns an int whose bits + * indicate which of the various interfaces have been loaded by this + * definition of main. + * + * Note: This file can be loaded more than once and must therefore + * check to see what has already been defined. + */ + +#ifdef main +# undef main +# undef CONSOLE_FLAG +# undef GRAPHICS_FLAG +#else +# define MAIN_USES_CONSOLE (1<<0) +# define MAIN_USES_GRAPHICS (1<<1) +#endif + +#ifdef _console_h +# define CONSOLE_FLAG MAIN_USES_CONSOLE +#else +# define CONSOLE_FLAG 0 +#endif + +#ifdef _gwindow_h +# define GRAPHICS_FLAG MAIN_USES_GRAPHICS +#else +# define GRAPHICS_FLAG 0 +#endif + +#if CONSOLE_FLAG | GRAPHICS_FLAG + +#define main main(int argc, char **argv) { \ + extern int _mainFlags; \ + _mainFlags = GRAPHICS_FLAG + CONSOLE_FLAG; \ + try { \ + return startupMain(argc, argv); \ + } catch (const std::exception& ex) { \ + string msg = "\n ***\n"; \ + msg += " *** STANFORD C++ LIBRARY \n"; \ + msg += " *** An exception occurred during program execution: \n"; \ + msg += " *** "; \ + msg += ex.what(); \ + msg += "\n ***\n\n"; \ + std::cerr << msg; \ + throw ex; \ + } catch (std::string str) { \ + string msg = "\n ***\n"; \ + msg += " *** STANFORD C++ LIBRARY \n"; \ + msg += " *** A string exception occurred during program execution: \n"; \ + msg += " *** \""; \ + msg += str; \ + msg += "\"\n ***\n"; \ + std::cerr << msg; \ + throw str; \ + } catch (char const* str) { \ + string msg = "\n ***\n"; \ + msg += " *** STANFORD C++ LIBRARY \n"; \ + msg += " *** A string exception occurred during program execution: \n"; \ + msg += " *** \""; \ + msg += str; \ + msg += "\"\n ***\n"; \ + std::cerr << msg; \ + throw str; \ + } catch (int n) { \ + char buf[128]; \ + snprintf(buf, 128, "%d", n); \ + string msg = "\n ***\n"; \ + msg += " *** STANFORD C++ LIBRARY \n"; \ + msg += " *** An int exception occurred during program execution: \n"; \ + msg += " *** "; \ + msg += buf; \ + msg += "\n ***\n\n"; \ + std::cerr << msg; \ + throw n; \ + } catch (long l) { \ + char buf[128]; \ + snprintf(buf, 128, "%ld", l); \ + string msg = "\n ***\n"; \ + msg += " *** STANFORD C++ LIBRARY \n"; \ + msg += " *** A long exception occurred during program execution: \n"; \ + msg += " *** "; \ + msg += buf; \ + msg += "\n ***\n\n"; \ + std::cerr << msg; \ + throw l; \ + } catch (char c) { \ + string msg = "\n ***\n"; \ + msg += " *** STANFORD C++ LIBRARY \n"; \ + msg += " *** A char exception occurred during program execution: \n"; \ + msg += " *** '"; \ + msg += c; \ + msg += "'\n ***\n"; \ + std::cerr << msg; \ + throw c; \ + } catch (bool b) { \ + string msg = "\n ***\n"; \ + msg += " *** STANFORD C++ LIBRARY \n"; \ + msg += " *** A bool exception occurred during program execution: \n"; \ + msg += " *** "; \ + msg += (b ? "true" : "false"); \ + msg += "\n ***\n\n"; \ + std::cerr << msg; \ + throw b; \ + } catch (double d) { \ + char buf[128]; \ + snprintf(buf, 128, "%lf", d); \ + string msg = "\n ***\n"; \ + msg += " *** STANFORD C++ LIBRARY \n"; \ + msg += " *** A double exception occurred during program execution: \n"; \ + msg += " *** "; \ + msg += buf; \ + msg += "\n ***\n\n"; \ + std::cerr << msg; \ + throw d; \ + } \ + } \ + int Main + +extern int startupMain(int argc, char **argv); + +#else + +#define main main(int argc, char **argv) { \ + extern int _mainFlags; \ + _mainFlags = GRAPHICS_FLAG + CONSOLE_FLAG; \ + try { \ + return mainWrapper(argc, argv); } \ + } catch (const std::exception& ex) { \ + string msg = "\n ***\n"; \ + msg += " *** STANFORD C++ LIBRARY \n"; \ + msg += " *** An exception occurred during program execution: \n"; \ + msg += " *** "; \ + msg += ex.what(); \ + msg += "\n ***\n\n"; \ + std::cerr << msg; \ + throw ex; \ + } catch (std::string str) { \ + string msg = "\n ***\n"; \ + msg += " *** STANFORD C++ LIBRARY \n"; \ + msg += " *** A string exception occurred during program execution: \n"; \ + msg += " *** \""; \ + msg += str; \ + msg += "\"\n ***\n"; \ + std::cerr << msg; \ + throw str; \ + } catch (char const* str) { \ + string msg = "\n ***\n"; \ + msg += " *** STANFORD C++ LIBRARY \n"; \ + msg += " *** A string exception occurred during program execution: \n"; \ + msg += " *** \""; \ + msg += str; \ + msg += "\"\n ***\n"; \ + std::cerr << msg; \ + throw str; \ + } catch (int n) { \ + char buf[128]; \ + snprintf(buf, 128, "%d", n); \ + string msg = "\n ***\n"; \ + msg += " *** STANFORD C++ LIBRARY \n"; \ + msg += " *** An int exception occurred during program execution: \n"; \ + msg += " *** "; \ + msg += buf; \ + msg += "\n ***\n\n"; \ + std::cerr << msg; \ + throw n; \ + } catch (long l) { \ + char buf[128]; \ + snprintf(buf, 128, "%ld", l); \ + string msg = "\n ***\n"; \ + msg += " *** STANFORD C++ LIBRARY \n"; \ + msg += " *** A long exception occurred during program execution: \n"; \ + msg += " *** "; \ + msg += buf; \ + msg += "\n ***\n\n"; \ + std::cerr << msg; \ + throw l; \ + } catch (char c) { \ + string msg = "\n ***\n"; \ + msg += " *** STANFORD C++ LIBRARY \n"; \ + msg += " *** A char exception occurred during program execution: \n"; \ + msg += " *** '"; \ + msg += c; \ + msg += "'\n ***\n"; \ + std::cerr << msg; \ + throw c; \ + } catch (bool b) { \ + string msg = "\n ***\n"; \ + msg += " *** STANFORD C++ LIBRARY \n"; \ + msg += " *** A bool exception occurred during program execution: \n"; \ + msg += " *** "; \ + msg += (b ? "true" : "false"); \ + msg += "\n ***\n\n"; \ + std::cerr << msg; \ + throw b; \ + } catch (double d) { \ + char buf[128]; \ + snprintf(buf, 128, "%lf", d); \ + string msg = "\n ***\n"; \ + msg += " *** STANFORD C++ LIBRARY \n"; \ + msg += " *** A double exception occurred during program execution: \n"; \ + msg += " *** "; \ + msg += buf; \ + msg += "\n ***\n\n"; \ + std::cerr << msg; \ + throw d; \ + } \ + int Main + +extern int mainWrapper(int argc, char **argv); + +#endif diff --git a/labb5/lib/StanfordCPPLib/private/main.h b/labb5/lib/StanfordCPPLib/private/main.h new file mode 100755 index 0000000..4339915 --- /dev/null +++ b/labb5/lib/StanfordCPPLib/private/main.h @@ -0,0 +1,62 @@ +/* + * File: main.h + * ------------ + * This file renames the <code>main</code> method in the client's + * program to <code>Main</code>, thereby allowing a custom + * <code>main</code> method in the libraries to take control + * before passing control back to the client program. The main macro + * also defines a function getMainFlags that returns an int whose bits + * indicate which of the various interfaces have been loaded by this + * definition of main. + * + * Note: This file can be loaded more than once and must therefore + * check to see what has already been defined. + */ + +#ifdef main +# undef main +# undef CONSOLE_FLAG +# undef GRAPHICS_FLAG +#else +# define MAIN_USES_CONSOLE (1<<0) +# define MAIN_USES_GRAPHICS (1<<1) +#endif + +#ifdef _console_h +# define CONSOLE_FLAG MAIN_USES_CONSOLE +#else +# define CONSOLE_FLAG 0 +#endif + +#ifdef _gwindow_h +# define GRAPHICS_FLAG MAIN_USES_GRAPHICS +#else +# define GRAPHICS_FLAG 0 +#endif + +void __StanfordCPPLib_terminate_handler(); +void __StanfordCPPLib_set_terminate(); + +#if CONSOLE_FLAG | GRAPHICS_FLAG + +#define main main(int argc, char **argv) { \ + extern int _mainFlags; \ + _mainFlags = GRAPHICS_FLAG + CONSOLE_FLAG; \ + return startupMain(argc, argv); \ + } \ + int Main + +extern int startupMain(int argc, char **argv); + +#else + +#define main main(int argc, char **argv) { \ + extern int _mainFlags; \ + _mainFlags = GRAPHICS_FLAG + CONSOLE_FLAG; \ + return mainWrapper(argc, argv); } \ + } \ + int Main + +extern int mainWrapper(int argc, char **argv); + +#endif diff --git a/labb5/lib/StanfordCPPLib/private/randompatch.h b/labb5/lib/StanfordCPPLib/private/randompatch.h new file mode 100755 index 0000000..c7a4b86 --- /dev/null +++ b/labb5/lib/StanfordCPPLib/private/randompatch.h @@ -0,0 +1,63 @@ +/* + * File: private/randompatch.h + * --------------------------- + * This file patches the implementation of the random number library + * to avoid some serious bugs in standard implementations of rand, + * particularly on Mac OS X. It also includes a hack to set the + * seed from the RANDOM_SEED environment variable, which makes it + * possible to produce repeatable figures. + */ + +/* + * Implementation notes: rand, srand + * --------------------------------- + * To ensure that this package works the same way on all platforms, + * this file completely reimplements the rand and srand methods. The + * algorithm is a conventional linear congruential generator with the + * parameters used in GNU's gclib. RAND_MAX for this implementation + * is 2147483647 (2^31 - 1). + */ + +#define MULTIPLIER 1103515245 +#define OFFSET 12345 + +static int _seed = 1; + +#undef rand +#define rand() ((_seed = MULTIPLIER * _seed + OFFSET) & 0x7FFFFFFF) + +#undef srand +#define srand(seed) (_seed = int(seed), _seed = (_seed <= 0) ? 1 : _seed) + +#undef RAND_MAX +#define RAND_MAX 2147483647 + +/* + * Implementation notes: Windows patch + * ----------------------------------- + * On some versions of Windows, the time function is too coarse to use + * as a random seed. On those versions, this definition substitutes the + * GetTickCount function. + */ + +#if defined (_MSC_VER) && (_MSC_VER >= 1200) +# include <windows.h> +# define time(dummy) (GetTickCount()) +#endif + +#ifdef __APPLE__ + +# include <cstdlib> + + static time_t patchedTime(time_t *) { + char *str = getenv("RANDOM_SEED"); + if (str == NULL) { + return time(NULL); + } else { + return atoi(str); + } + } + +# define time(dummy) patchedTime(dummy) + +#endif diff --git a/labb5/lib/StanfordCPPLib/private/tokenpatch.h b/labb5/lib/StanfordCPPLib/private/tokenpatch.h new file mode 100755 index 0000000..d4930e5 --- /dev/null +++ b/labb5/lib/StanfordCPPLib/private/tokenpatch.h @@ -0,0 +1,11 @@ +/* + * File: tokenpatch.h + * ------------------ + * This file renames TokenType and WORD to avoid conflict with the + * <windows.h> header. + */ + +#ifdef _MSC_VER +# define TokenType TokenTypeT +# define WORD WORD_TC +#endif diff --git a/labb5/lib/StanfordCPPLib/private/tplatform.h b/labb5/lib/StanfordCPPLib/private/tplatform.h new file mode 100755 index 0000000..6cf4bae --- /dev/null +++ b/labb5/lib/StanfordCPPLib/private/tplatform.h @@ -0,0 +1,25 @@ +/* + * File: tplatform.h + * ----------------- + * This interface defines the platform-specific methods on threads + * and locks. + */ + +/* Methods for threads */ + +int forkForPlatform(void (*fn)(void *), void *arg); +void incThreadRefCountForPlatform(int id); +void decThreadRefCountForPlatform(int id); +void joinForPlatform(int id); +int getCurrentThreadForPlatform(); +void yieldForPlatform(); + +/* Methods for locks */ + +int initLockForPlatform(); +void incLockRefCountForPlatform(int id); +void decLockRefCountForPlatform(int id); +void lockForPlatform(int id); +void unlockForPlatform(int id); +void waitForPlatform(int id); +void signalForPlatform(int id); |
