📄 cslib.shar
字号:
X/*X * Function: SetEraseModeX * Usage: SetEraseMode(TRUE);X * SetEraseMode(FALSE);X * ---------------------------X * The SetEraseMode function sets the value of the internalX * erasing flag. Setting this flag is similar to setting theX * color to "White" in its effect but does not affect theX * current color setting. When erase mode is set to FALSE,X * normal drawing is restored, using the current color.X */XXvoid SetEraseMode(bool mode);XX/*X * Function: GetEraseModeX * Usage: mode = GetEraseMode();X * -----------------------------X * This function returns the current state of the erase mode flag.X */XXbool GetEraseMode(void);XX/*X * Function: SetWindowTitleX * Usage: SetWindowTitle(title);X * -----------------------------X * This function sets the title of the graphics window, if suchX * an operation is possible on the display. If it is not possibleX * for a particular implementation, the call is simply ignored.X * This function may be called prior to the InitGraphics call toX * set the initial name of the window.X */XXvoid SetWindowTitle(string title);XX/*X * Function: GetWindowTitleX * Usage: title = GetWindowTitle();X * --------------------------------X * This function returns the title of the graphics window. If theX * implementation does not support titles, this call returns theX * empty string.X */XXstring GetWindowTitle(void);XX/*X * Function: UpdateDisplayX * Usage: UpdateDisplay();X * -----------------------X * This function initiates an immediate update of the graphicsX * window and is necessary for animation. Ordinarily, theX * graphics window is updated only when the program waits forX * user input.X */XXvoid UpdateDisplay(void);XX/*X * Function: PauseX * Usage: Pause(seconds);X * ----------------------X * The Pause function updates the graphics window and thenX * pauses for the indicated number of seconds. This functionX * is useful for animation where the motion would otherwiseX * be too fast.X */XXvoid Pause(double seconds);XX/*X * Function: ExitGraphicsX * Usage: ExitGraphics();X * ----------------------X * The ExitGraphics function closes the graphics window andX * exits from the application without waiting for any additionalX * user interaction.X */XXvoid ExitGraphics(void);XX/*X * Functions: SaveGraphicsState, RestoreGraphicsStateX * Usage: SaveGraphicsState();X * . . . graphical operations . . .X * RestoreGraphicsState();X * ---------------------------------------------------X * The SaveGraphicsState function saves the current graphicsX * state (the current pen position, the font, the point size,X * and the erase mode flag) internally, so that they can beX * restored by the next RestoreGraphicsState call. These twoX * functions must be used in pairs but may be nested to any depth.X */XXvoid SaveGraphicsState(void);Xvoid RestoreGraphicsState(void);XX/*X * Functions: GetFullScreenWidth, GetFullScreenHeightX * Usage: width = GetFullScreenWidth();X * height = GetFullScreenHeight();X * --------------------------------------X * These functions return the height and width of the entireX * display screen, not the graphics window. Their onlyX * significant use is for applications that need to adjustX * the size of the graphics window based on available screenX * space. These functions may be called before InitGraphicsX * has been called.X */XXdouble GetFullScreenWidth(void);Xdouble GetFullScreenHeight(void);XX/*X * Functions: SetWindowSizeX * Usage: SetWindowSize(width, height);X * ------------------------------------X * This function sets the window size to the indicated dimensions,X * if possible. This function should be called before the graphicsX * window is created by InitGraphics. Attempts to change the sizeX * of an existing window are ignored by most implementations. ThisX * function should be used sparingly because it reduces theX * portability of applications, particularly if the clientX * requests more space than is available on the screen.X */XXvoid SetWindowSize(double width, double height);XX/*X * Functions: GetXResolution, GetYResolutionX * Usage: xres = GetXResolution();X * yres = GetYResolution();X * -----------------------------------------X * These functions return the number of pixels per inch alongX * each of the coordinate directions and are useful for applicationsX * in which it is important for short distances to be representedX * uniformly in terms of dot spacing. Even though the x and yX * resolutions are the same for most displays, clients shouldX * not rely on this property.X *X * Note: Lines in the graphics library are one pixel unit wide andX * have a length that is always one pixel longer than you mightX * expect. For example, the function callX *X * DrawLine(2 / GetXResolution(), 0);X *X * draws a line from the current point to the point two pixelsX * further right, which results in a line of three pixels.X */XXdouble GetXResolution(void);Xdouble GetYResolution(void);XX#endifEND_OF_FILEif test 14869 -ne `wc -c <'cslib/extgraph.h'`; then echo shar: \"'cslib/extgraph.h'\" unpacked with wrong size!fi# end of 'cslib/extgraph.h'fiif test -f 'cslib/gcalloc.h' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'cslib/gcalloc.h'\"elseecho shar: Extracting \"'cslib/gcalloc.h'\" \(2269 characters\)sed "s/^X//" >'cslib/gcalloc.h' <<'END_OF_FILE'X/*X * File: gcalloc.hX * Version: 1.0X * Last modified on Wed Sep 21 16:21:37 1994 by erobertsX * -----------------------------------------------------X * This file is a stub version of the interface for aX * garbage-collecting allocator that will be part ofX * a future library release. When the garbage-collectingX * allocator is in use, the memory returned by the GetBlockX * and FreeBlock functions in genlib.h can be traced andX * collected automatically when it is no longer accessible.X *X * The garbage-collecting allocator is not part of theX * current cslib distribution. Even so, functions in theX * other libraries call the ProtectVariable and ProtectBlockX * functions, so that they will continue to work when theX * full library is released. Those functions are implementedX * in genlib.c.X */XX#ifndef _gcalloc_hX#define _gcalloc_hXX/*X * Macro: ProtectVariableX * Usage: ProtectVariable(v);X * --------------------------X * This macro registers a global variable with the allocationX * system, so that the variable is traced when the garbageX * collector is used. This operation needs is implementedX * in genlib.c so that code can be written to function correctlyX * whether or not the garbage-collecting allocator is loaded.X */XX#define ProtectVariable(v) ProtectBlock(&v, sizeof v)XX/*X * Function: ProtectBlockX * Usage: ProtectBlock(ptr, nbytes);X * ---------------------------------X * This function is not usually called by clients (who willX * ordinarily use ProtectVariable instead), but has theX * effect of protecting the block of memory beginning atX * ptr and extending for nbytes from the garbage collector.X */XXvoid ProtectBlock(void *ptr, size_t nbytes);XX/*X * Global linkage variable: _acbX * -----------------------------X * This variable is used to hold the allocation control blockX * that provides the linkage between this package and theX * dynamic allocator. The reason for using the structureX * as a linkage is so that the garbage-collecting allocatorX * need not even be loaded if it is not explicitly called.X */XXtypedef struct {X void *(*allocMethod)(size_t nbytes);X void (*freeMethod)(void *ptr);X void (*protectMethod)(void *ptr, size_t nbytes);X} *_GCControlBlock;XXextern _GCControlBlock _acb;XX#endifEND_OF_FILEif test 2269 -ne `wc -c <'cslib/gcalloc.h'`; then echo shar: \"'cslib/gcalloc.h'\" unpacked with wrong size!fi# end of 'cslib/gcalloc.h'fiif test -f 'cslib/genlib.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'cslib/genlib.c'\"elseecho shar: Extracting \"'cslib/genlib.c'\" \(3748 characters\)sed "s/^X//" >'cslib/genlib.c' <<'END_OF_FILE'X/*X * File: genlib.cX * Version: 1.0X * Last modified on Sun Jul 24 10:29:46 1994 by erobertsX * -----------------------------------------------------X * This file implements the general C library package. See theX * interface description in genlib.h for details.X */XX#include <stdio.h>X#include <stddef.h>X#include <string.h>X#include <stdarg.h>XX#include "genlib.h"X#include "gcalloc.h"X#include "exception.h"XX/*X * Constants:X * ----------X * ErrorExitStatus -- Status value used in exit callX * MaxErrorMessage -- Longest error message allowedX */XX#define ErrorExitStatus 1X#define MaxErrorMessage 500XX/* Section 1 -- Define new "primitive" types */XX/*X * Constant: UNDEFINEDX * -------------------X * This entry defines the target of the UNDEFINED constant.X */XXchar undefined_object[] = "UNDEFINED";XX/* Section 2 -- Memory allocation */XX/*X * Implementation notes:X * ---------------------X * The code for the memory allocator is divided betweenX * genlib.c and gcalloc.c, and the division strategy may atX * first seem unnatural, since the function ProtectBlock isX * declared in gcalloc.h but defined here in genlib.c. TheX * intention is to minimize the size of object filesX * produced by linkers that search a library for modulesX * that are actually referenced. The libraries themselvesX * need to call ProtectBlock (usually through the macroX * ProtectVariable), but will not require the actual codeX * for the allocator unless InitGCAllocator is explicitlyX * called.X */XX/*X * Global variable: _acbX * ---------------------X * This variable is used to hold a method suite that makes itX * easy to substitute a garbage-collecting allocator for theX * ANSI allocator.X */XX_GCControlBlock _acb = NULL;XX/* Memory allocation implementation */XXvoid *GetBlock(size_t nbytes)X{X void *result;XX if (_acb == NULL) {X result = malloc(nbytes);X } else {X result = _acb->allocMethod(nbytes);X }X if (result == NULL) Error("No memory available");X return (result);X}XXvoid FreeBlock(void *ptr)X{X if (_acb == NULL) {X free(ptr);X } else {X _acb->freeMethod(ptr);X }X}XXvoid ProtectBlock(void *ptr, size_t nbytes)X{X if (_acb != NULL) _acb->protectMethod(ptr, nbytes);X}XX/* Section 3 -- Basic error handling */XX/*X * Implementation notes: ErrorX * ---------------------------X * Writing the Error function requires some care, since it isX * called in circumstances in which parts of the system may beX * broken. In particular, it is not acceptable for Error toX * call GetBlock, since the error condition may be that theX * system is out of memory, in which case calling GetBlock wouldX * fail. The error string should be allocated dynamically,X * so that this function can be used in reentrant code.X * Note that it is critical to exit if the length bound forX * an error message is exceeded, since this error almostX * certainly corrupts the stack.X */XXvoid Error(string msg, ...)X{X va_list args;X char errbuf[MaxErrorMessage + 1];X string errmsg;X int errlen;XX va_start(args, msg);X vsprintf(errbuf, msg, args);X va_end(args);X errlen = strlen(errbuf);X if (errlen > MaxErrorMessage) {X fprintf(stderr, "Error: Error Message too long\n");X exit(ErrorExitStatus);X }X if (_acb == NULL) {X errmsg = malloc(errlen + 1);X } else {X errmsg = _acb->allocMethod(errlen + 1);X }X if (errmsg == NULL) {X errmsg = "No memory available";X } else {X strcpy(errmsg, errbuf);X }X if (HandlerExists(&ErrorException)) {X RaiseException(&ErrorException, "ErrorException", errmsg);X } else {X fprintf(stderr, "Error: %s\n", errmsg);X exit(ErrorExitStatus);X }X}END_OF_FILEif test 3748 -ne `wc -c <'cslib/genlib.c'`; then echo shar: \"'cslib/genlib.c'\" unpacked with wrong size!fi# end of 'cslib/genlib.c'fiif test -f 'cslib/genlib.h' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'cslib/genlib.h'\"elseecho shar: Extracting \"'cslib/genlib.h'\" \(6232 characters\)sed "s/^X//" >'cslib/genlib.h' <<'END_OF_FILE'X/*X * File: genlib.hX * Version: 1.0X * Last modified on Sun Jul 24 10:32:49 1994 by erobertsX * -----------------------------------------------------X * This file contains several definitions that form theX * core of a general-purpose ANSI C library developed by EricX * Roberts. The goal of this library is to provide a basicX * set of tools and conventions that increase the readabilityX * of C programs, particularly as they are used in a teachingX * environment.X *X * The basic definitions provided by genlib.h are:X *X * 1. Declarations for several new "primitive" typesX * (most importantly bool and string) that areX * used throughout the other libraries andX * applications as fundamental types.X *X * 2. A new set of functions for memory allocation.X *X * 3. A function for error handling.X *X * 4. A repeat statement for loops with interior exits.X */XX#ifndef _genlib_hX#define _genlib_hXX#include <stdio.h>X#include <stdlib.h>X#include <stddef.h>XX/* Section 1 -- Define new "primitive" types */XX/*X * Type: boolX * ----------X * This type has two values, FALSE and TRUE, which are equal to 0X * and 1, respectively. Most of the advantage of defining this typeX * comes from readability because it allows the programmer toX * provide documentation that a variable will take on only one ofX * these two values. Designing a portable representation, however,X * is surprisingly hard, because many libraries and some compilersX * define these names. The definitions are usually compatible butX * may still be flagged as errors.X */XX#ifdef THINK_CX typedef int bool;X#elseX# ifdef TRUE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -