├── .gitignore ├── CHANGELOG ├── LICENSE ├── Makefile ├── NOTICE ├── README.md ├── c ├── examples │ ├── .gitignore │ ├── Breakout.c │ ├── DrawLines.c │ ├── DrawRectangles.c │ ├── GHelloWorld.c │ ├── Makefile │ ├── PacMan.c │ ├── StopSign.c │ ├── TestGraphics.c │ ├── TestInteractors.c │ ├── USFlag.c │ ├── images │ │ └── Obama.png │ └── sounds │ │ └── Bounce.wav ├── include │ ├── bst.h │ ├── charset.h │ ├── cmdscan.h │ ├── cmpfn.h │ ├── cslib.h │ ├── exception.h │ ├── filelib.h │ ├── foreach.h │ ├── generic.h │ ├── gevents.h │ ├── ginteractors.h │ ├── gmath.h │ ├── gobjects.h │ ├── graph.h │ ├── gtimer.h │ ├── gtypes.h │ ├── gwindow.h │ ├── hashmap.h │ ├── iterator.h │ ├── itertype.h │ ├── loadobj.h │ ├── map.h │ ├── options.h │ ├── platform.h │ ├── pqueue.h │ ├── private │ │ ├── randompatch.h │ │ └── tokenpatch.h │ ├── queue.h │ ├── random.h │ ├── ref.h │ ├── set.h │ ├── simpio.h │ ├── sound.h │ ├── stack.h │ ├── strbuf.h │ ├── strlib.h │ ├── tokenscanner.h │ ├── unittest.h │ └── vector.h ├── src │ ├── bst.c │ ├── charset.c │ ├── cmdscan.c │ ├── cmpfn.c │ ├── cslib.c │ ├── exception.c │ ├── filelib.c │ ├── foreach.c │ ├── generic.c │ ├── gevents.c │ ├── ginteractors.c │ ├── gmath.c │ ├── gobjects.c │ ├── graph.c │ ├── gtimer.c │ ├── gtypes.c │ ├── gwindow.c │ ├── hashmap.c │ ├── iterator.c │ ├── loadobj.c │ ├── map.c │ ├── options.c │ ├── platform.c │ ├── pqueue.c │ ├── queue.c │ ├── random.c │ ├── ref.c │ ├── set.c │ ├── simpio.c │ ├── sound.c │ ├── stack.c │ ├── strbuf.c │ ├── strlib.c │ ├── tokenscanner.c │ ├── unittest.c │ ├── unixfile.c │ ├── vector.c │ └── winfile.c └── tests │ └── TestStanfordCSLib.c ├── docs ├── charset-h.html ├── charset.html ├── cmdscan-h.html ├── cmdscan.html ├── cmpfn-h.html ├── cmpfn.html ├── cppdoc.css ├── cslib-h.html ├── cslib.html ├── exception-h.html ├── exception.html ├── filelib-h.html ├── filelib.html ├── foreach-h.html ├── foreach.html ├── gevents-h.html ├── gevents.html ├── ginteractors-h.html ├── ginteractors.html ├── gmath-h.html ├── gmath.html ├── gobjects-h.html ├── gobjects.html ├── graph-h.html ├── graph.html ├── gtimer-h.html ├── gtimer.html ├── gtypes-h.html ├── gtypes.html ├── gwindow-h.html ├── gwindow.html ├── hashmap-h.html ├── hashmap.html ├── images │ ├── GArcExamples.png │ ├── GArcGeometry.png │ ├── GCheckBox.png │ ├── GChooser.png │ ├── GEventHierarchy.png │ ├── GInteractorHierarchy.png │ ├── GLabelGeometry.png │ ├── GObjectHierarchy.png │ ├── GPolygonExamples.png │ ├── GSlider.png │ ├── HelloWorld.png │ ├── PacMan.png │ ├── StanfordTreeLogo.png │ └── StopSign.png ├── include │ └── package.html ├── index.html ├── loadobj-h.html ├── loadobj.html ├── map-h.html ├── map.html ├── pqueue-h.html ├── pqueue.html ├── queue-h.html ├── queue.html ├── random-h.html ├── random.html ├── ref-h.html ├── ref.html ├── set-h.html ├── set.html ├── simpio-h.html ├── simpio.html ├── sound-h.html ├── sound.html ├── stack-h.html ├── stack.html ├── strbuf-h.html ├── strbuf.html ├── strlib-h.html ├── strlib.html ├── thread-h.html ├── thread.html ├── tokenscanner-h.html ├── tokenscanner.html ├── vector-h.html └── vector.html └── java ├── Makefile ├── include └── JBEManifest.txt ├── lib └── acm.jar └── src └── stanford └── spl ├── GButton.java ├── GCheckBox.java ├── GChooser.java ├── GInteractor.java ├── GSlider.java ├── GTextField.java ├── JBECanvas.java ├── JBECommand.java ├── JBEConsole.java ├── JBEFileFilter.java ├── JBELabel.java ├── JBEWindow.java ├── JavaBackEnd.java └── TopCompound.java /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | *.swp 3 | -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | 2014-07-29 Rob Bowden 2 | 3 | * src/platform.c (initpipe): use CLASSPATH if present to locate spl.jar 4 | 5 | * examples/Makefile: add -lm flag to remove undefined reference errors 6 | 7 | * src/gobjects.c: add "return" to isRaised 8 | 9 | * src/platform.c (initPipe): fix syntax error in Windows version. Still 10 | untested. 11 | 12 | * include/platform.h: s/closeOp/closeGWindowOp 13 | 14 | * include/sound.h: s/play/playSound 15 | 16 | * include/exception.h (throwException): add __attribute__ ((noreturn)) 17 | 18 | * include/cslib.h (error): add __attribute__ ((noreturn)) 19 | 20 | * include/cslib.c (unhandledError): add __attribute__ ((noreturn)) 21 | 22 | * various files: Add "int" return type to main(s) and explicit "return 0"s 23 | 24 | * src/platform.c (isprint): #include for OS X 25 | 26 | * examples/USFlag.c (waitForClick): #include "gevents.h" 27 | 28 | * src/gobjects.c (getType): exit in error in the default case 29 | 30 | * src/tokenscanner.c (getTokenType): added parentheses around boolean 31 | expression 32 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | /*************************************************************************/ 2 | /* Stanford Portable Library */ 3 | /* Copyright (C) 2013 by Eric Roberts */ 4 | /* */ 5 | /* This program is free software: you can redistribute it and/or modify */ 6 | /* it under the terms of the GNU General Public License as published by */ 7 | /* the Free Software Foundation, either version 3 of the License, or */ 8 | /* (at your option) any later version. */ 9 | /* */ 10 | /* This program is distributed in the hope that it will be useful, */ 11 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 12 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 13 | /* GNU General Public License for more details. */ 14 | /* */ 15 | /* You should have received a copy of the GNU General Public License */ 16 | /* along with this program. If not, see . */ 17 | /*************************************************************************/ 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Stanford Portable Library (SPL) 2 | 3 | This is CS50's fork of Eric Roberts' Stanford Portable Library. 4 | 5 | ## Documentation 6 | 7 | https://cs50.github.io/spl/ 8 | 9 | ## Building 10 | 11 | ### Fedora 12 | 13 | sudo yum install -y bash binutils coreutils findutils gcc java-1.?.0-openjdk-devel 14 | git clone git@github.com:cs50/spl.git 15 | cd spl 16 | make 17 | sudo make install 18 | 19 | ### Ubuntu 20 | 21 | apt-get install -y build-essential git openjdk-7-jdk 22 | git clone git@github.com:cs50/spl.git 23 | cd spl 24 | make 25 | sudo make install 26 | -------------------------------------------------------------------------------- /c/examples/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | spl.jar 3 | Breakout 4 | DrawLines 5 | DrawRectangles 6 | GHelloWorld 7 | PacMan 8 | StopSign 9 | TestGraphics 10 | TestInteractors 11 | USFlag 12 | -------------------------------------------------------------------------------- /c/examples/DrawLines.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: DrawLines.c 3 | * ----------------- 4 | * This program allows users to create lines on the graphics 5 | * canvas by clicking and dragging with the mouse. 6 | */ 7 | 8 | /*************************************************************************/ 9 | /* Stanford Portable Library */ 10 | /* Copyright (C) 2013 by Eric Roberts */ 11 | /* */ 12 | /* This program is free software: you can redistribute it and/or modify */ 13 | /* it under the terms of the GNU General Public License as published by */ 14 | /* the Free Software Foundation, either version 3 of the License, or */ 15 | /* (at your option) any later version. */ 16 | /* */ 17 | /* This program is distributed in the hope that it will be useful, */ 18 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 19 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 20 | /* GNU General Public License for more details. */ 21 | /* */ 22 | /* You should have received a copy of the GNU General Public License */ 23 | /* along with this program. If not, see . */ 24 | /*************************************************************************/ 25 | 26 | #include 27 | #include "cslib.h" 28 | #include "gevents.h" 29 | #include "gobjects.h" 30 | #include "gwindow.h" 31 | 32 | int main() { 33 | GWindow gw; 34 | GObject line; 35 | GEvent e; 36 | 37 | gw = newGWindow(600, 400); 38 | while (true) { 39 | e = waitForEvent(MOUSE_EVENT); 40 | if (getEventType(e) == MOUSE_PRESSED) { 41 | line = newGLine(getX(e), getY(e), getX(e), getY(e)); 42 | add(gw, line); 43 | } else if (getEventType(e) == MOUSE_DRAGGED) { 44 | setEndPoint(line, getX(e), getY(e)); 45 | } 46 | freeEvent(e); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /c/examples/DrawRectangles.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: DrawRectangles.c 3 | * ---------------------- 4 | * This program allows users to create rectangles on the canvas 5 | * by clicking and dragging with the mouse. This version of the 6 | * program also allows the user to change the color of a rectangle 7 | * by typing the first letter of the color name or a space for black. 8 | */ 9 | 10 | /*************************************************************************/ 11 | /* Stanford Portable Library */ 12 | /* Copyright (C) 2013 by Eric Roberts */ 13 | /* */ 14 | /* This program is free software: you can redistribute it and/or modify */ 15 | /* it under the terms of the GNU General Public License as published by */ 16 | /* the Free Software Foundation, either version 3 of the License, or */ 17 | /* (at your option) any later version. */ 18 | /* */ 19 | /* This program is distributed in the hope that it will be useful, */ 20 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 21 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 22 | /* GNU General Public License for more details. */ 23 | /* */ 24 | /* You should have received a copy of the GNU General Public License */ 25 | /* along with this program. If not, see . */ 26 | /*************************************************************************/ 27 | 28 | #include 29 | #include 30 | #include "cslib.h" 31 | #include "gevents.h" 32 | #include "gobjects.h" 33 | #include "gwindow.h" 34 | 35 | int main() { 36 | GWindow gw = newGWindow(600, 400); 37 | GRect rect = NULL; 38 | bool dragging = false; 39 | double startX = 0.0; 40 | double startY = 0.0; 41 | while (true) { 42 | GEvent e = waitForEvent(MOUSE_EVENT | KEY_EVENT); 43 | if (getEventType(e) == MOUSE_PRESSED) { 44 | startX = getX(e); 45 | startY = getY(e); 46 | rect = getGObjectAt(gw, startX, startY); 47 | dragging = (rect != NULL); 48 | if (!dragging) { 49 | rect = newGRect(startX, startY, 0, 0); 50 | setFilled(rect, true); 51 | add(gw, rect); 52 | } 53 | } else if (getEventType(e) == MOUSE_DRAGGED) { 54 | double x = getX(e); 55 | double y = getY(e); 56 | if (dragging) { 57 | move(rect, x - startX, y - startY); 58 | startX = x; 59 | startY = y; 60 | } else { 61 | double width = fabs(x - startX); 62 | double height = fabs(y - startY); 63 | x = fmin(x, startX); 64 | y = fmin(y, startY); 65 | setBounds(rect, x, y, width, height); 66 | } 67 | } else if (getEventType(e) == MOUSE_CLICKED) { 68 | if (rect != NULL) sendToFront(rect); 69 | } else if (getEventType(e) == KEY_TYPED) { 70 | if (rect != NULL) { 71 | string color = "BLACK"; 72 | switch (getKeyChar(e)) { 73 | case 'b': color = "BLUE"; break; 74 | case 'c': color = "CYAN"; break; 75 | case 'g': color = "GREEN"; break; 76 | case 'm': color = "MAGENTA"; break; 77 | case 'o': color = "ORANGE"; break; 78 | case 'r': color = "RED"; break; 79 | case 'w': color = "WHITE"; break; 80 | case 'y': color = "YELLOW"; break; 81 | } 82 | setColor(rect, color); 83 | } 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /c/examples/GHelloWorld.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: GHelloWorld.c 3 | * ------------------- 4 | * This program implements Hello World using the graphics window. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | #include 26 | #include "cslib.h" 27 | #include "gobjects.h" 28 | #include "gwindow.h" 29 | 30 | int main() { 31 | GWindow gw; 32 | GLabel label; 33 | double x, y; 34 | 35 | printf("This program draws the 'hello, world' message.\n"); 36 | gw = newGWindow(600, 400); 37 | label = newGLabel("hello, world"); 38 | setFont(label, "SansSerif-36"); 39 | x = (getWidth(gw) - getWidth(label)) / 2; 40 | y = (getHeight(gw) + getFontAscent(label)) / 2; 41 | setLocation(label, x, y); 42 | add(gw, label); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /c/examples/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for spl 2 | #**************************************************************** 3 | 4 | PROGRAMS = \ 5 | Breakout \ 6 | DrawLines \ 7 | DrawRectangles \ 8 | GHelloWorld \ 9 | PacMan \ 10 | StopSign \ 11 | TestGraphics \ 12 | TestInteractors \ 13 | USFlag 14 | 15 | FLAGS = -L../lib -lcs -lm 16 | 17 | 18 | # *************************************************************** 19 | # Entry to bring the package up to date 20 | # The "make all" entry should be the first real entry 21 | 22 | all: $(PROGRAMS) 23 | 24 | Breakout: Breakout.o 25 | gcc -o Breakout Breakout.o $(FLAGS) 26 | 27 | Breakout.o: Breakout.c 28 | gcc -c -I../include Breakout.c 29 | 30 | DrawLines: DrawLines.o 31 | gcc -o DrawLines DrawLines.o $(FLAGS) 32 | 33 | DrawLines.o: DrawLines.c 34 | gcc -c -I../include DrawLines.c 35 | 36 | DrawRectangles: DrawRectangles.o 37 | gcc -o DrawRectangles DrawRectangles.o $(FLAGS) 38 | 39 | DrawRectangles.o: DrawRectangles.c 40 | gcc -c -I../include DrawRectangles.c 41 | 42 | GHelloWorld: GHelloWorld.o 43 | gcc -o GHelloWorld GHelloWorld.o $(FLAGS) 44 | 45 | GHelloWorld.o: GHelloWorld.c 46 | gcc -c -I../include GHelloWorld.c 47 | 48 | PacMan: PacMan.o 49 | gcc -o PacMan PacMan.o $(FLAGS) 50 | 51 | PacMan.o: PacMan.c 52 | gcc -c -I../include PacMan.c 53 | 54 | StopSign: StopSign.o 55 | gcc -o StopSign StopSign.o $(FLAGS) 56 | 57 | StopSign.o: StopSign.c 58 | gcc -c -I../include StopSign.c 59 | 60 | TestGraphics: TestGraphics.o 61 | gcc -o TestGraphics TestGraphics.o $(FLAGS) 62 | 63 | TestGraphics.o: TestGraphics.c 64 | gcc -c -I../include TestGraphics.c 65 | 66 | TestInteractors: TestInteractors.o 67 | gcc -o TestInteractors TestInteractors.o $(FLAGS) 68 | 69 | TestInteractors.o: TestInteractors.c 70 | gcc -c -I../include TestInteractors.c 71 | 72 | USFlag: USFlag.o 73 | gcc -o USFlag USFlag.o $(FLAGS) 74 | 75 | USFlag.o: USFlag.c 76 | gcc -c -I../include USFlag.c 77 | 78 | 79 | # *************************************************************** 80 | # Standard entries to remove files from the directories 81 | # tidy -- eliminate unwanted files 82 | # clean -- delete derived files in preparation for rebuild 83 | 84 | tidy: 85 | @rm -f `find . -name ',*' -o -name '.,*' -o -name '*~'` 86 | @rm -f `find . -name '*.tmp' -o -name '*.err'` 87 | @rm -f `find . -name core -o -name a.out` 88 | 89 | clean scratch: tidy 90 | @rm -f *.o *.a $(PROGRAMS) 91 | -------------------------------------------------------------------------------- /c/examples/PacMan.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: PacMan.c 3 | * -------------- 4 | * This program creates a 270-degree filled arc that resembles the 5 | * character in the PacMan game. Clicking the mouse causes the 6 | * PacMan character to move across the screen opening and shutting 7 | * its jaws. 8 | */ 9 | 10 | /*************************************************************************/ 11 | /* Stanford Portable Library */ 12 | /* Copyright (C) 2013 by Eric Roberts */ 13 | /* */ 14 | /* This program is free software: you can redistribute it and/or modify */ 15 | /* it under the terms of the GNU General Public License as published by */ 16 | /* the Free Software Foundation, either version 3 of the License, or */ 17 | /* (at your option) any later version. */ 18 | /* */ 19 | /* This program is distributed in the hope that it will be useful, */ 20 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 21 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 22 | /* GNU General Public License for more details. */ 23 | /* */ 24 | /* You should have received a copy of the GNU General Public License */ 25 | /* along with this program. If not, see . */ 26 | /*************************************************************************/ 27 | 28 | #include 29 | #include "cslib.h" 30 | #include "gevents.h" 31 | #include "gmath.h" 32 | #include "gobjects.h" 33 | #include "gwindow.h" 34 | 35 | /* Constants */ 36 | 37 | const double PACMAN_SIZE = 60; 38 | const double PAUSE_TIME = 20; 39 | const double DELTA_X = 2; 40 | const int DELTA_THETA = 5; 41 | 42 | int main() { 43 | GWindow gw; 44 | GArc pacman; 45 | int angle, sign; 46 | double limit; 47 | 48 | gw = newGWindow(600, 400); 49 | pacman = newGArc(0, (getHeight(gw) - PACMAN_SIZE) / 2, 50 | PACMAN_SIZE, PACMAN_SIZE, 45, 270); 51 | setFilled(pacman, true); 52 | setFillColor(pacman, "YELLOW"); 53 | add(gw, pacman); 54 | waitForClick(); 55 | angle = 45; 56 | sign = -1; 57 | limit = getWidth(gw) - PACMAN_SIZE; 58 | while (getX(pacman) < limit) { 59 | move(pacman, DELTA_X, 0); 60 | angle += sign * DELTA_THETA; 61 | if (angle == 0 || angle == 45) sign = -sign; 62 | setStartAngle(pacman, angle); 63 | setSweepAngle(pacman, 360 - 2 * angle); 64 | pause(PAUSE_TIME); 65 | } 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /c/examples/StopSign.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: StopSign.c 3 | * ---------------- 4 | * This program draws a filled red octagon. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | #include 26 | #include 27 | #include "cslib.h" 28 | #include "gobjects.h" 29 | #include "gwindow.h" 30 | 31 | int main() { 32 | GWindow gw; 33 | GPolygon stopSign; 34 | double edge; 35 | int i; 36 | 37 | printf("This program draws a red octagon.\n"); 38 | gw = newGWindow(600, 400); 39 | edge = 75; 40 | stopSign = newGPolygon(); 41 | addVertex(stopSign, -edge / 2, edge / 2 + edge / sqrt(2.0)); 42 | for (i = 0; i < 8; i++) { 43 | addPolarEdge(stopSign, edge, 45 * i); 44 | } 45 | setFilled(stopSign, true); 46 | setColor(stopSign, "RED"); 47 | addAt(gw, stopSign, getWidth(gw) / 2, getHeight(gw) / 2); 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /c/examples/TestGraphics.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: TestGraphics.c 3 | * -------------------- 4 | * This file tests the JBE graphics package from C. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | #include 26 | #include "cslib.h" 27 | #include "gobjects.h" 28 | #include "gwindow.h" 29 | 30 | int main() { 31 | double width, height; 32 | GWindow gw; 33 | 34 | gw = newGWindow(500, 300); 35 | width = getWidth(gw); 36 | height = getHeight(gw); 37 | drawLine(gw, 0, height / 2, width / 2, 0); 38 | drawLine(gw, width / 2, 0, width, height / 2); 39 | drawLine(gw, width, height / 2, width / 2, height); 40 | drawLine(gw, width / 2, height, 0, height / 2); 41 | setColor(gw, "BLUE"); 42 | fillRect(gw, width / 4, height / 4, width / 2, height / 2); 43 | setColor(gw, "GRAY"); 44 | fillOval(gw, width / 4, height / 4, width / 2, height / 2); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /c/examples/TestInteractors.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: TestInteractors.c 3 | * ----------------------- 4 | * This program tests the various interactor classes in the graphics 5 | * library. 6 | */ 7 | 8 | /*************************************************************************/ 9 | /* Stanford Portable Library */ 10 | /* Copyright (C) 2013 by Eric Roberts */ 11 | /* */ 12 | /* This program is free software: you can redistribute it and/or modify */ 13 | /* it under the terms of the GNU General Public License as published by */ 14 | /* the Free Software Foundation, either version 3 of the License, or */ 15 | /* (at your option) any later version. */ 16 | /* */ 17 | /* This program is distributed in the hope that it will be useful, */ 18 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 19 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 20 | /* GNU General Public License for more details. */ 21 | /* */ 22 | /* You should have received a copy of the GNU General Public License */ 23 | /* along with this program. If not, see . */ 24 | /*************************************************************************/ 25 | 26 | #include 27 | #include "cslib.h" 28 | #include "gevents.h" 29 | #include "ginteractors.h" 30 | #include "gwindow.h" 31 | #include "strlib.h" 32 | 33 | int main() { 34 | GWindow gw = newGWindow(800, 400); 35 | GButton button = newGButton("Start"); 36 | addToRegion(gw, button, "SOUTH"); 37 | GChooser chooser = newGChooser(); 38 | addItem(chooser, "Small"); 39 | addItem(chooser, "Medium"); 40 | addItem(chooser, "Large"); 41 | addItem(chooser, "X-Large"); 42 | addToRegion(gw, chooser, "SOUTH"); 43 | GCheckBox chkbox = newGCheckBox("Slow"); 44 | addToRegion(gw, chkbox, "SOUTH"); 45 | setActionCommand(chkbox, "Check"); 46 | GSlider slider = newGSlider(0, 100, 50); 47 | GDimension size = getSize(slider); 48 | setSize(slider, 100, getHeight(size)); 49 | addToRegion(gw, newGLabel(" Slow"), "SOUTH"); 50 | addToRegion(gw, slider, "SOUTH"); 51 | GTextField field = newGTextField(10); 52 | addToRegion(gw, newGLabel("Fast "), "SOUTH"); 53 | setActionCommand(field, "Start"); 54 | addToRegion(gw, newGLabel("Text:"), "SOUTH"); 55 | addToRegion(gw, field, "SOUTH"); 56 | while (true) { 57 | GActionEvent e = waitForEvent(ACTION_EVENT); 58 | string cmd = getActionCommand(e); 59 | if (stringEqual(cmd, "Start")) { 60 | printf("Start: text = \"%s\", size = %s, speed = %d\n", 61 | getText(field), getSelectedItem(chooser), getValue(slider)); 62 | } else if (stringEqual(cmd, "Check")) { 63 | printf("Check = %s\n", isSelected(chkbox) ? "true" : "false"); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /c/examples/USFlag.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: USFlag.c 3 | * -------------- 4 | * This program draws a US flag that fills the window. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | #include 26 | #include 27 | #include "cslib.h" 28 | #include "gmath.h" 29 | #include "gobjects.h" 30 | #include "gevents.h" 31 | #include "gwindow.h" 32 | 33 | /* Constants */ 34 | 35 | #define FLAG_WIDTH 740 36 | #define FLAG_HEIGHT 400 37 | #define FIELD_FRACTION 0.40 38 | #define STAR_FRACTION 0.40 39 | 40 | /* Prototypes */ 41 | 42 | static void drawStripes(GWindow gw); 43 | static void drawField(GWindow gw); 44 | static void drawObama(GWindow gw); 45 | static GPolygon newGStar(double size); 46 | 47 | int main() { 48 | GWindow gw = newGWindow(FLAG_WIDTH, FLAG_HEIGHT); 49 | drawStripes(gw); 50 | drawField(gw); 51 | waitForClick(); 52 | drawObama(gw); 53 | return 0; 54 | } 55 | 56 | /* Draw the stripes */ 57 | 58 | static void drawStripes(GWindow gw) { 59 | double width = getWidth(gw); 60 | double height = getHeight(gw); 61 | double stripeHeight = height / 13; 62 | int i; 63 | for (i = 12; i >= 0; i--) { 64 | GRect stripe = newGRect(0, i * stripeHeight, width, stripeHeight); 65 | setFilled(stripe, true); 66 | setColor(stripe, (i % 2 == 0) ? "RED" : "WHITE"); 67 | add(gw, stripe); 68 | } 69 | } 70 | 71 | /* Draw the star field */ 72 | 73 | static void drawField(GWindow gw) { 74 | double width = getWidth(gw); 75 | double height = getHeight(gw); 76 | double fieldWidth = FIELD_FRACTION * width; 77 | double fieldHeight = height * 7 / 13; 78 | GRect field = newGRect(0, 0, fieldWidth, fieldHeight); 79 | setColor(field, "BLUE"); 80 | setFilled(field, true); 81 | add(gw, field); 82 | double dx = fieldWidth / 6; 83 | double dy = fieldHeight / 5; 84 | double starSize = STAR_FRACTION * fmin(dx, dy); 85 | int row, col; 86 | for (row = 0; row < 5; row++) { 87 | double y = (row + 0.5) * dy; 88 | for (col = 0; col < 6; col++) { 89 | GPolygon star = newGStar(starSize); 90 | setColor(star, "WHITE"); 91 | setFilled(star, true); 92 | double x = (col + 0.5) * dx; 93 | addAt(gw, star, x, y); 94 | } 95 | } 96 | for (row = 0; row < 4; row++) { 97 | double y = (row + 1) * dy; 98 | for (col = 0; col < 5; col++) { 99 | GPolygon star = newGStar(starSize); 100 | setColor(star, "WHITE"); 101 | setFilled(star, true); 102 | double x = (col + 1) * dx; 103 | addAt(gw, star, x, y); 104 | } 105 | } 106 | } 107 | 108 | /* Draw a picture of President Obama */ 109 | 110 | static void drawObama(GWindow gw) { 111 | double width = getWidth(gw); 112 | double height = getHeight(gw); 113 | double fieldWidth = FIELD_FRACTION * width; 114 | GImage obama = newGImage("Obama.png"); 115 | double x = fieldWidth + (width - fieldWidth - getWidth(obama)) / 2; 116 | double y = (height - getHeight(obama)) / 2; 117 | addAt(gw, obama, x, y); 118 | } 119 | 120 | /* Create a star polygon */ 121 | 122 | static GPolygon newGStar(double size) { 123 | GPolygon star = newGPolygon(); 124 | double dx = size / 2; 125 | double dy = dx * tanDegrees(18); 126 | double edge = size / 2 - dy * tanDegrees(36); 127 | addVertex(star, -dx, -dy); 128 | int angle = 0; 129 | int i; 130 | for (i = 0; i < 5; i++) { 131 | addPolarEdge(star, edge, angle); 132 | addPolarEdge(star, edge, angle + 72); 133 | angle -= 72; 134 | } 135 | return star; 136 | } 137 | -------------------------------------------------------------------------------- /c/examples/images/Obama.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs50/spl/24221174d52e601140640f64629cae490b7c295d/c/examples/images/Obama.png -------------------------------------------------------------------------------- /c/examples/sounds/Bounce.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs50/spl/24221174d52e601140640f64629cae490b7c295d/c/examples/sounds/Bounce.wav -------------------------------------------------------------------------------- /c/include/foreach.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: foreach.h 3 | * --------------- 4 | * This interface provides a simple syntactic extension for iterators 5 | * that makes them much easier to read. The general form of the 6 | * foreach statement looks like this: 7 | * 8 | *
 9 |  *    foreach (var in collection) {
10 |  *       statements
11 |  *    }
12 |  *
13 | * 14 | * In C, the variable var must be declared outside the loop. 15 | */ 16 | 17 | /*************************************************************************/ 18 | /* Stanford Portable Library */ 19 | /* Copyright (C) 2013 by Eric Roberts */ 20 | /* */ 21 | /* This program is free software: you can redistribute it and/or modify */ 22 | /* it under the terms of the GNU General Public License as published by */ 23 | /* the Free Software Foundation, either version 3 of the License, or */ 24 | /* (at your option) any later version. */ 25 | /* */ 26 | /* This program is distributed in the hope that it will be useful, */ 27 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 28 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 29 | /* GNU General Public License for more details. */ 30 | /* */ 31 | /* You should have received a copy of the GNU General Public License */ 32 | /* along with this program. If not, see . */ 33 | /*************************************************************************/ 34 | 35 | #ifndef _foreach_h 36 | #define _foreach_h 37 | 38 | #include "cslib.h" 39 | 40 | /* 41 | * Macro: foreach 42 | * Usage: foreach (element in collection) { . . . } 43 | * ------------------------------------------------ 44 | * This macro definition creates a new statement form that simplifies 45 | * the use of iterators. The variable element must be 46 | * declared in the current scope and must be compatible with the base 47 | * type of the collection. 48 | */ 49 | 50 | #define in : (void *) ( 51 | #define foreach(arg) \ 52 | for (initForEach(1 ? (void *) &arg), 0 ? &arg)); \ 53 | stepForEach(1 ? (void *) &arg)); ) 54 | 55 | void initForEach(void *dst, void *collection); 56 | bool stepForEach(void *dst); 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /c/include/gmath.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: gmath.h 3 | * ------------- 4 | * This interface exports several functions for working with graphical 5 | * geometry along with the constants PI and E. 6 | */ 7 | 8 | /*************************************************************************/ 9 | /* Stanford Portable Library */ 10 | /* Copyright (C) 2013 by Eric Roberts */ 11 | /* */ 12 | /* This program is free software: you can redistribute it and/or modify */ 13 | /* it under the terms of the GNU General Public License as published by */ 14 | /* the Free Software Foundation, either version 3 of the License, or */ 15 | /* (at your option) any later version. */ 16 | /* */ 17 | /* This program is distributed in the hope that it will be useful, */ 18 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 19 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 20 | /* GNU General Public License for more details. */ 21 | /* */ 22 | /* You should have received a copy of the GNU General Public License */ 23 | /* along with this program. If not, see . */ 24 | /*************************************************************************/ 25 | 26 | #ifndef _gmath_h 27 | #define _gmath_h 28 | 29 | /* 30 | * Constant: PI 31 | * ------------ 32 | * The mathematical constant pi, which is the ratio of the circumference 33 | * of a circle to its diameter. 34 | */ 35 | 36 | #define PI 3.14159265358979323846 37 | 38 | /* 39 | * Constant: E 40 | * ----------- 41 | * The mathematical constant e, which is the base of natural logarithms. 42 | */ 43 | 44 | #define E 2.71828182845904523536 45 | 46 | /* 47 | * Function: sinDegrees 48 | * Usage: sine = sinDegrees(angle); 49 | * -------------------------------- 50 | * Returns the trigonometric sine of angle, which is 51 | * expressed in degrees. 52 | */ 53 | 54 | double sinDegrees(double angle); 55 | 56 | /* 57 | * Function: cosDegrees 58 | * Usage: cosine = cosDegrees(angle); 59 | * ---------------------------------- 60 | * Returns the trigonometric cosine of angle, which is 61 | * expressed in degrees. 62 | */ 63 | 64 | double cosDegrees(double angle); 65 | 66 | /* 67 | * Function: tanDegrees 68 | * Usage: tangent = tanDegrees(angle); 69 | * ----------------------------------- 70 | * Returns the trigonometric tangent of angle, which is 71 | * expressed in degrees. 72 | */ 73 | 74 | double tanDegrees(double angle); 75 | 76 | /* 77 | * Function: toDegrees 78 | * Usage: degrees = toDegrees(radians); 79 | * ------------------------------------ 80 | * Converts an angle from radians to degrees. 81 | */ 82 | 83 | double toDegrees(double radians); 84 | 85 | /* 86 | * Function: toRadians 87 | * Usage: radians = toRadians(degrees); 88 | * ------------------------------------ 89 | * Converts an angle from degrees to radians. 90 | */ 91 | 92 | double toRadians(double degrees); 93 | 94 | /* 95 | * Function: vectorDistance 96 | * Usage: r = vectorDistance(x, y); 97 | * -------------------------------- 98 | * Computes the distance between the origin and the specified point. 99 | */ 100 | 101 | double vectorDistance(double x, double y); 102 | 103 | /* 104 | * Function: vectorAngle 105 | * Usage: angle = vectorAngle(x, y); 106 | * --------------------------------- 107 | * Returns the angle in degrees from the origin to the specified point. 108 | * This function takes account of the fact that the graphics coordinate 109 | * system is flipped in the y direction from the traditional 110 | * Cartesian plane. 111 | */ 112 | 113 | double vectorAngle(double x, double y); 114 | 115 | #endif 116 | -------------------------------------------------------------------------------- /c/include/gtimer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: gtimer.h 3 | * -------------- 4 | * This interface exports a general interval timer. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | #ifndef _gtimer_h 26 | #define _gtimer_h 27 | 28 | #include "cslib.h" 29 | 30 | /* 31 | * Type: GTimer 32 | * ------------ 33 | * This type implements a simple interval timer that generates a timer 34 | * event with a specified frequency. 35 | */ 36 | 37 | typedef struct GTimerCDT *GTimer; 38 | 39 | /* 40 | * Function: newGTimer 41 | * Usage: timer = newGTimer(milliseconds); 42 | * --------------------------------------- 43 | * Creates a timer that generates a timer event each time the specified 44 | * number of milliseconds has elapsed. No events are generated until 45 | * the client calls startTimer. 46 | */ 47 | 48 | GTimer newGTimer(double milliseconds); 49 | 50 | /* 51 | * Function: freeGTimer 52 | * Usage: freeGTimer(timer); 53 | * ------------------------- 54 | * Frees the resources associated with the timer. 55 | */ 56 | 57 | void freeGTimer(GTimer timer); 58 | 59 | /* 60 | * Function: startTimer 61 | * Usage: startTimer(timer); 62 | * ------------------------- 63 | * Starts the timer. A timer continues to generate timer events until it 64 | * is stopped; to achieve the effect of a one-shot timer, the simplest 65 | * approach is to call the stop function inside the event 66 | * handler. 67 | */ 68 | 69 | void startTimer(GTimer timer); 70 | 71 | /* 72 | * Function: stopTimer 73 | * Usage: stopTimer(timer); 74 | * ------------------------ 75 | * Stops the timer so that it stops generating events. 76 | */ 77 | 78 | void stopTimer(GTimer timer); 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /c/include/hashmap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: hashmap.h 3 | * --------------- 4 | * This interface defines a map abstraction that associates string 5 | * keys with values. The implementation uses a hash table, which 6 | * offers constant-time performance but does not support iterating 7 | * through the keys in order. 8 | */ 9 | 10 | /*************************************************************************/ 11 | /* Stanford Portable Library */ 12 | /* Copyright (C) 2013 by Eric Roberts */ 13 | /* */ 14 | /* This program is free software: you can redistribute it and/or modify */ 15 | /* it under the terms of the GNU General Public License as published by */ 16 | /* the Free Software Foundation, either version 3 of the License, or */ 17 | /* (at your option) any later version. */ 18 | /* */ 19 | /* This program is distributed in the hope that it will be useful, */ 20 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 21 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 22 | /* GNU General Public License for more details. */ 23 | /* */ 24 | /* You should have received a copy of the GNU General Public License */ 25 | /* along with this program. If not, see . */ 26 | /*************************************************************************/ 27 | 28 | #ifndef _hashmap_h 29 | #define _hashmap_h 30 | 31 | #include "cslib.h" 32 | #include "generic.h" 33 | #include "iterator.h" 34 | 35 | /* 36 | * Function: HashMap 37 | * ----------------- 38 | * This type is the ADT used to represent a map from strings to values. 39 | */ 40 | 41 | typedef struct HashMapCDT *HashMap; 42 | 43 | /* Exported entries */ 44 | 45 | /* 46 | * Function: newHashMap 47 | * Usage: map = newHashMap(); 48 | * -------------------------- 49 | * Allocates a new map with no entries. 50 | */ 51 | 52 | HashMap newHashMap(void); 53 | 54 | /* 55 | * Function: freeHashMap 56 | * Usage: freeHashMap(map); 57 | * ------------------------ 58 | * Frees the storage associated with the map. 59 | */ 60 | 61 | void freeHashMap(HashMap map); 62 | 63 | /* 64 | * Function: size 65 | * Usage: n = size(map); 66 | * --------------------- 67 | * Returns the number of elements in the map. 68 | */ 69 | 70 | int sizeHashMap(HashMap map); 71 | 72 | /* 73 | * Function: isEmpty 74 | * Usage: if (isEmpty(map)) . . . 75 | * ------------------------------ 76 | * Returns true if the map has no entries. 77 | */ 78 | 79 | bool isEmptyHashMap(HashMap map); 80 | 81 | /* 82 | * Function: clear 83 | * Usage: clear(map); 84 | * ------------------ 85 | * Removes all entries from the map. 86 | */ 87 | 88 | void clearHashMap(HashMap map); 89 | 90 | /* 91 | * Function: clone 92 | * Usage: newmap = clone(map); 93 | * --------------------------- 94 | * Creates a copy of the map. The clone function copies 95 | * only the first level of the structure and does not copy the individual 96 | * elements. 97 | */ 98 | 99 | HashMap cloneHashMap(HashMap map); 100 | 101 | /* 102 | * Function: put 103 | * Usage: put(map, key, value); 104 | * ---------------------------- 105 | * Associates key with value in the map. 106 | * Each call to put supersedes any previous definition 107 | * for key. 108 | */ 109 | 110 | void putHashMap(HashMap map, string key, void *value); 111 | 112 | /* 113 | * Function: get 114 | * Usage: void *value = get(map, key); 115 | * ----------------------------------- 116 | * Returns the value associated with key in the map, 117 | * or NULL, if no such value exists. 118 | */ 119 | 120 | void *getHashMap(HashMap map, string key); 121 | 122 | /* 123 | * Function: containsKey 124 | * Usage: if (containsKey(map, key)) . . . 125 | * --------------------------------------- 126 | * Checks to see if the map contains the specified key. 127 | */ 128 | 129 | bool containsKeyHashMap(HashMap map, string key); 130 | 131 | /* 132 | * Function: remove 133 | * Usage: remove(map, key); 134 | * ------------------------ 135 | * Removes the key and its value from the map. 136 | */ 137 | 138 | void removeHashMap(HashMap map, string key); 139 | 140 | /* 141 | * Function: map 142 | * Usage: map(map, fn, data); 143 | * -------------------------- 144 | * Iterates through the map and calls the function fn on 145 | * each entry. The callback function takes the following arguments: 146 | * 147 | *
    148 | *
  • The key string 149 | *
  • The associated value 150 | *
  • The data pointer 151 | *
152 | * 153 | * The data pointer allows the client to pass state 154 | * information to the function fn, if necessary. If no such 155 | * information is required, this argument should be NULL. 156 | */ 157 | 158 | void mapHashMap(HashMap map, proc fn, void *data); 159 | 160 | #endif 161 | -------------------------------------------------------------------------------- /c/include/iterator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: iterator.h 3 | * ---------------- 4 | * This interface exports a polymorphic iterator abstraction for C. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | #ifndef _iterator_h 26 | #define _iterator_h 27 | 28 | #include "cslib.h" 29 | 30 | /* 31 | * Type: Iterator 32 | * -------------- 33 | * An abstract type used to iterate over the elements of any collection. 34 | */ 35 | 36 | typedef struct IteratorCDT *Iterator; 37 | 38 | /* Exported entries */ 39 | 40 | /* 41 | * General overview 42 | * ---------------- 43 | * The newIterator, stepIterator, and 44 | * freeIterator functions make it possible to iterate 45 | * over the elements in any collection that supports iteration. 46 | * In most cases, clients will simply use the foreach 47 | * macro, which automatically invokes these methods. They can 48 | * also be used as shown in the following standalone paradigm: 49 | * 50 | *
51 |  *    iterator = newIterator(collection);
52 |  *    while (stepIterator(iterator, &element)) {
53 |  *       . . . body of loop . . .
54 |  *    }
55 |  *    freeIterator(iterator);
56 |  *
57 | * 58 | *

The call to stepIterator advances the iterator 59 | * and returns the next element using the reference parameter. The 60 | * stepIterator function returns true until 61 | * the elements are exhausted, after which it returns false. 62 | * The freeIterator function releases any storage 63 | * associated with the iterator. 64 | */ 65 | 66 | /* 67 | * Function: newIterator 68 | * Usage: iterator = newIterator(collection); 69 | * ------------------------------------------ 70 | * Creates a new iterator for the specified collection. 71 | */ 72 | 73 | Iterator newIterator(void *collection); 74 | 75 | /* 76 | * Function: stepIterator 77 | * Usage: hasMoreElements = stepIterator(iterator, &element); 78 | * ---------------------------------------------------------- 79 | * Advances the iterator and stores the next element through the 80 | * element pointer provided as a reference parameter. The 81 | * stepIterator function returns true until 82 | * the elements are exhausted, after which it returns false. 83 | */ 84 | 85 | bool stepIterator(Iterator iterator, void *dst); 86 | 87 | /* 88 | * Function: freeIterator 89 | * Usage: freeIterator(iterator); 90 | * ------------------------------ 91 | * Frees any storage associated with the iterator. 92 | */ 93 | 94 | void freeIterator(Iterator iterator); 95 | 96 | #endif 97 | -------------------------------------------------------------------------------- /c/include/loadobj.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: loadobj.h 3 | * --------------- 4 | * This interface supports dynamic loading of functions from object files. 5 | * Any references in the object file to previously defined objects are 6 | * allowed, but no additional searching or unresolved references are 7 | * supported. 8 | */ 9 | 10 | /*************************************************************************/ 11 | /* Stanford Portable Library */ 12 | /* Copyright (C) 2013 by Eric Roberts */ 13 | /* */ 14 | /* This program is free software: you can redistribute it and/or modify */ 15 | /* it under the terms of the GNU General Public License as published by */ 16 | /* the Free Software Foundation, either version 3 of the License, or */ 17 | /* (at your option) any later version. */ 18 | /* */ 19 | /* This program is distributed in the hope that it will be useful, */ 20 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 21 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 22 | /* GNU General Public License for more details. */ 23 | /* */ 24 | /* You should have received a copy of the GNU General Public License */ 25 | /* along with this program. If not, see . */ 26 | /*************************************************************************/ 27 | 28 | #ifndef _loadobj_h 29 | #define _loadobj_h 30 | 31 | #include "cslib.h" 32 | 33 | /* 34 | * Function: loadObject 35 | * Usage: loadObject(pathname); 36 | * ---------------------------- 37 | * Loads the object file into the current executable. 38 | */ 39 | 40 | void loadObject(string pathname); 41 | 42 | /* 43 | * Function: loadSymbols 44 | * Usage: loadSymbols(progname); 45 | * ----------------------------- 46 | * Loads the symbols from the executable program. 47 | */ 48 | 49 | void loadSymbols(string progname); 50 | 51 | /* 52 | * Function: findFunction 53 | * Usage: fn = findFunction(fnname); 54 | * --------------------------------- 55 | * Looks up the function in the symbol table and returns a pointer to the 56 | * code. 57 | */ 58 | 59 | proc findFunction(string fnname); 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /c/include/pqueue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: pqueue.h 3 | * -------------- 4 | * This interface defines a queue abstraction that dequeues elements 5 | * in priority order. In keeping with traditional English usage, 6 | * lower priority numbers have higher priority, so that priority 1 7 | * items appear before priority 2 items. 8 | */ 9 | 10 | /*************************************************************************/ 11 | /* Stanford Portable Library */ 12 | /* Copyright (C) 2013 by Eric Roberts */ 13 | /* */ 14 | /* This program is free software: you can redistribute it and/or modify */ 15 | /* it under the terms of the GNU General Public License as published by */ 16 | /* the Free Software Foundation, either version 3 of the License, or */ 17 | /* (at your option) any later version. */ 18 | /* */ 19 | /* This program is distributed in the hope that it will be useful, */ 20 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 21 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 22 | /* GNU General Public License for more details. */ 23 | /* */ 24 | /* You should have received a copy of the GNU General Public License */ 25 | /* along with this program. If not, see . */ 26 | /*************************************************************************/ 27 | 28 | #ifndef _pqueue_h 29 | #define _pqueue_h 30 | 31 | #include "cslib.h" 32 | #include "generic.h" 33 | 34 | /* 35 | * Type: PriorityQueue 36 | * ------------------- 37 | * This type defines the abstract type for a queue. 38 | */ 39 | 40 | typedef struct PriorityQueueCDT *PriorityQueue; 41 | 42 | /* 43 | * Function: newPriorityQueue 44 | * Usage: pq = newPriorityQueue(); 45 | * ------------------------------- 46 | * Allocates and returns an empty priority queue. 47 | */ 48 | 49 | PriorityQueue newPriorityQueue(void); 50 | 51 | /* 52 | * Function: freePriorityQueue 53 | * Usage: freePriorityQueue(pq); 54 | * ----------------------------- 55 | * Frees the storage associated with the specified priority queue. 56 | */ 57 | 58 | void freePriorityQueue(PriorityQueue pq); 59 | 60 | /* 61 | * Function: enqueue 62 | * Usage: enqueue(pq, value, priority); 63 | * ------------------------------------ 64 | * Adds a value to the queue in the order specified by priority. 65 | * In C, the priority argument must have type double, 66 | * which means that constants used to express priorities in the call to the 67 | * generic enqueue function must include a decimal point. 68 | */ 69 | 70 | void enqueuePriorityQueue(PriorityQueue pq, void *value, double priority); 71 | 72 | /* 73 | * Function: dequeue 74 | * Usage: value = dequeue(pq); 75 | * --------------------------- 76 | * Removes the data value at the head of the queue and returns it 77 | * to the client. If the queue is empty, dequeue calls 78 | * error with an appropriate message. 79 | */ 80 | 81 | void *dequeuePriorityQueue(PriorityQueue pq); 82 | 83 | /* 84 | * Function: peek 85 | * Usage: value = peek(pq); 86 | * ------------------------ 87 | * Returns the data value at the head of the queue without removing it. 88 | * If the queue is empty, peek calls error with 89 | * an appropriate message. 90 | */ 91 | 92 | void *peekPriorityQueue(PriorityQueue pq); 93 | 94 | /* 95 | * Function: peekPriority 96 | * Usage: priority = peekPriority(pq); 97 | * ----------------------------------- 98 | * Returns the priority of the first entry in the queue, without 99 | * removing it. 100 | */ 101 | 102 | double peekPriority(PriorityQueue pq); 103 | 104 | /* 105 | * Function: isEmpty 106 | * Usage: if (isEmpty(pq)) . . . 107 | * ----------------------------- 108 | * Tests whether the queue is empty. 109 | */ 110 | 111 | bool isEmptyPriorityQueue(PriorityQueue pq); 112 | 113 | /* 114 | * Function: size 115 | * Usage: n = size(pq); 116 | * -------------------- 117 | * Returns the number of values in the queue. 118 | */ 119 | 120 | int sizePriorityQueue(PriorityQueue pq); 121 | 122 | /* 123 | * Function: clear 124 | * Usage: clear(pq); 125 | * ----------------- 126 | * Removes all values from the queue. 127 | */ 128 | 129 | void clearPriorityQueue(PriorityQueue pq); 130 | 131 | /* 132 | * Function: clone 133 | * Usage: newpq = clone(pq); 134 | * ------------------------- 135 | * Creates a copy of the priority queue. The clone function 136 | * copies only the first level of the structure and does not copy the 137 | * individual elements. 138 | */ 139 | 140 | PriorityQueue clonePriorityQueue(PriorityQueue pq); 141 | 142 | #endif 143 | -------------------------------------------------------------------------------- /c/include/private/randompatch.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: private/randompatch.h 3 | * --------------------------- 4 | * This file patches the implementation of the random number library 5 | * to avoid some serious bugs in standard implementations of rand, 6 | * particularly on Mac OS X. It also includes a hack to set the 7 | * seed from the RANDOM_SEED environment variable, which makes it 8 | * possible to produce repeatable figures. 9 | */ 10 | 11 | /*************************************************************************/ 12 | /* Stanford Portable Library */ 13 | /* Copyright (C) 2013 by Eric Roberts */ 14 | /* */ 15 | /* This program is free software: you can redistribute it and/or modify */ 16 | /* it under the terms of the GNU General Public License as published by */ 17 | /* the Free Software Foundation, either version 3 of the License, or */ 18 | /* (at your option) any later version. */ 19 | /* */ 20 | /* This program is distributed in the hope that it will be useful, */ 21 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 22 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 23 | /* GNU General Public License for more details. */ 24 | /* */ 25 | /* You should have received a copy of the GNU General Public License */ 26 | /* along with this program. If not, see . */ 27 | /*************************************************************************/ 28 | 29 | /* 30 | * Implementation notes: rand, srand 31 | * --------------------------------- 32 | * To ensure that this package works the same way on all platforms, 33 | * this file completely reimplements the rand and srand methods. The 34 | * algorithm is a conventional linear congruential generator with the 35 | * parameters used in GNU's gclib. RAND_MAX for this implementation 36 | * is 2147483647 (2^31 - 1). 37 | */ 38 | 39 | #define MULTIPLIER 1103515245 40 | #define OFFSET 12345 41 | 42 | static int _seed = 1; 43 | 44 | #undef rand 45 | #define rand() ((_seed = MULTIPLIER * _seed + OFFSET) & 0x7FFFFFFF) 46 | 47 | #undef srand 48 | #define srand(seed) (_seed = (int) seed, _seed = (_seed <= 0) ? 1 : _seed) 49 | 50 | #undef RAND_MAX 51 | #define RAND_MAX 2147483647 52 | 53 | /* 54 | * Implementation notes: Windows patch 55 | * ----------------------------------- 56 | * On some versions of Windows, the time function is too coarse to use 57 | * as a random seed. On those versions, this definition substitutes the 58 | * GetTickCount function. 59 | */ 60 | 61 | #if defined (_MSC_VER) && (_MSC_VER >= 1200) 62 | # include 63 | # define time(dummy) (GetTickCount()) 64 | #endif 65 | 66 | #ifdef __APPLE__ 67 | 68 | static time_t patchedTime(time_t *dummy) { 69 | char *str = getenv("RANDOM_SEED"); 70 | if (str == NULL) { 71 | return time(NULL); 72 | } else { 73 | return atoi(str); 74 | } 75 | } 76 | 77 | # define time(dummy) patchedTime(dummy) 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /c/include/private/tokenpatch.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: tokenpatch.h 3 | * ------------------ 4 | * This file renames TokenType and WORD to avoid conflict with the 5 | * header. 6 | */ 7 | 8 | /*************************************************************************/ 9 | /* Stanford Portable Library */ 10 | /* Copyright (C) 2013 by Eric Roberts */ 11 | /* */ 12 | /* This program is free software: you can redistribute it and/or modify */ 13 | /* it under the terms of the GNU General Public License as published by */ 14 | /* the Free Software Foundation, either version 3 of the License, or */ 15 | /* (at your option) any later version. */ 16 | /* */ 17 | /* This program is distributed in the hope that it will be useful, */ 18 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 19 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 20 | /* GNU General Public License for more details. */ 21 | /* */ 22 | /* You should have received a copy of the GNU General Public License */ 23 | /* along with this program. If not, see . */ 24 | /*************************************************************************/ 25 | 26 | #ifdef _MSC_VER 27 | # define TokenType TokenTypeT 28 | # define WORD WORD_TC 29 | #endif 30 | -------------------------------------------------------------------------------- /c/include/queue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: queue.h 3 | * ------------- 4 | * This interface defines a queue abstraction with first-in/first-out 5 | * semantics. 6 | */ 7 | 8 | /*************************************************************************/ 9 | /* Stanford Portable Library */ 10 | /* Copyright (C) 2013 by Eric Roberts */ 11 | /* */ 12 | /* This program is free software: you can redistribute it and/or modify */ 13 | /* it under the terms of the GNU General Public License as published by */ 14 | /* the Free Software Foundation, either version 3 of the License, or */ 15 | /* (at your option) any later version. */ 16 | /* */ 17 | /* This program is distributed in the hope that it will be useful, */ 18 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 19 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 20 | /* GNU General Public License for more details. */ 21 | /* */ 22 | /* You should have received a copy of the GNU General Public License */ 23 | /* along with this program. If not, see . */ 24 | /*************************************************************************/ 25 | 26 | #ifndef _queue_h 27 | #define _queue_h 28 | 29 | #include "cslib.h" 30 | #include "generic.h" 31 | 32 | /* 33 | * Type: Queue 34 | * ----------- 35 | * This type defines the abstract type for a queue. 36 | */ 37 | 38 | typedef struct QueueCDT *Queue; 39 | 40 | /* 41 | * Function: newQueue 42 | * Usage: queue = newQueue(); 43 | * -------------------------- 44 | * Allocates and returns an empty queue. 45 | */ 46 | 47 | Queue newQueue(void); 48 | 49 | /* 50 | * Function: freeQueue 51 | * Usage: freeQueue(queue); 52 | * ------------------------ 53 | * Frees the storage associated with the specified queue. 54 | */ 55 | 56 | void freeQueue(Queue queue); 57 | 58 | /* 59 | * Function: enqueue 60 | * Usage: enqueue(queue, element); 61 | * ------------------------------- 62 | * Adds an element to the end of the queue. 63 | */ 64 | 65 | void enqueueQueue(Queue queue, void *element); 66 | 67 | /* 68 | * Function: dequeue 69 | * Usage: element = dequeue(queue); 70 | * -------------------------------- 71 | * Removes the data value at the head of the queue and returns it 72 | * to the client. If the queue is empty, dequeue calls 73 | * error with an appropriate message. 74 | */ 75 | 76 | void *dequeueQueue(Queue queue); 77 | 78 | /* 79 | * Function: peek 80 | * Usage: element = peek(queue); 81 | * ----------------------------- 82 | * Returns the data value at the head of the queue without removing it. 83 | * If the queue is empty, peek calls error with 84 | * an appropriate message. 85 | */ 86 | 87 | void *peekQueue(Queue queue); 88 | 89 | /* 90 | * Function: isEmpty 91 | * Usage: if (isEmpty(queue)) . . . 92 | * -------------------------------- 93 | * Tests whether the queue is empty. 94 | */ 95 | 96 | bool isEmptyQueue(Queue queue); 97 | 98 | /* 99 | * Function: size 100 | * Usage: n = size(queue); 101 | * ----------------------- 102 | * Returns the number of elements in the queue. 103 | */ 104 | 105 | int sizeQueue(Queue queue); 106 | 107 | /* 108 | * Function: clear 109 | * Usage: clear(queue); 110 | * -------------------- 111 | * Removes all elements from the queue. 112 | */ 113 | 114 | void clearQueue(Queue queue); 115 | 116 | /* 117 | * Function: clone 118 | * Usage: newqueue = clone(queue); 119 | * ------------------------------- 120 | * Creates a copy of the queue. The clone function copies 121 | * only the first level of the structure and does not copy the individual 122 | * elements. 123 | */ 124 | 125 | Queue cloneQueue(Queue queue); 126 | 127 | #endif 128 | -------------------------------------------------------------------------------- /c/include/random.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: random.h 3 | * -------------- 4 | * This interface exports functions for generating pseudorandom numbers. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | #ifndef _random_h 26 | #define _random_h 27 | 28 | #include "cslib.h" 29 | #include 30 | 31 | /* 32 | * Function: randomInteger 33 | * Usage: n = randomInteger(low, high); 34 | * ------------------------------------ 35 | * Returns a random integer in the range low to 36 | * high, inclusive. 37 | */ 38 | 39 | int randomInteger(int low, int high); 40 | 41 | /* 42 | * Function: randomReal 43 | * Usage: d = randomReal(low, high); 44 | * --------------------------------- 45 | * Returns a random real number in the half-open interval 46 | * [low .. high). A half-open 47 | * interval includes the first endpoint but not the second, which 48 | * means that the result is always greater than or equal to 49 | * low but strictly less than high. 50 | */ 51 | 52 | double randomReal(double low, double high); 53 | 54 | /* 55 | * Function: randomChance 56 | * Usage: if (randomChance(p)) . . . 57 | * --------------------------------- 58 | * Returns true with the probability indicated by p. 59 | * The argument p must be a floating-point number between 60 | * 0 (never) and 1 (always). For example, calling 61 | * randomChance(.30) returns true 30 percent 62 | * of the time. 63 | */ 64 | 65 | bool randomChance(double p); 66 | 67 | /* 68 | * Function: setRandomSeed 69 | * Usage: setRandomSeed(seed); 70 | * --------------------------- 71 | * Sets the internal random number seed to the specified value. You 72 | * can use this function to set a specific starting point for the 73 | * pseudorandom sequence or to ensure that program behavior is 74 | * repeatable during the debugging phase. 75 | */ 76 | 77 | void setRandomSeed(int seed); 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /c/include/simpio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: simpio.h 3 | * -------------- 4 | * This interface exports several functions that simplify the 5 | * reading of input data. 6 | */ 7 | 8 | /*************************************************************************/ 9 | /* Stanford Portable Library */ 10 | /* Copyright (C) 2013 by Eric Roberts */ 11 | /* */ 12 | /* This program is free software: you can redistribute it and/or modify */ 13 | /* it under the terms of the GNU General Public License as published by */ 14 | /* the Free Software Foundation, either version 3 of the License, or */ 15 | /* (at your option) any later version. */ 16 | /* */ 17 | /* This program is distributed in the hope that it will be useful, */ 18 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 19 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 20 | /* GNU General Public License for more details. */ 21 | /* */ 22 | /* You should have received a copy of the GNU General Public License */ 23 | /* along with this program. If not, see . */ 24 | /*************************************************************************/ 25 | 26 | #ifndef _simpio_h 27 | #define _simpio_h 28 | 29 | #include "cslib.h" 30 | 31 | /* 32 | * Function: getInteger 33 | * Usage: n = getInteger(); 34 | * ------------------------ 35 | * Reads a line of text from standard input and scans it as an integer. 36 | * If an integer cannot be scanned or if extraneous characters follow 37 | * the number, the user is given a chance to retry. 38 | */ 39 | 40 | int getInteger(void); 41 | 42 | /* 43 | * Function: getLong 44 | * Usage: l = getLong(); 45 | * --------------------- 46 | * Reads a line of text from standard input and scans it as a 47 | * long. If an integer cannot be scanned or if 48 | * extraneous characters follow the number, the user is given 49 | * a chance to retry. 50 | */ 51 | 52 | long getLong(void); 53 | 54 | /* 55 | * Function: getReal 56 | * Usage: d = getReal(); 57 | * --------------------- 58 | * Reads a line of text from standard input and scans it as a 59 | * double. If an number cannot be scanned or if 60 | * extraneous characters follow the number, the user is given 61 | * a chance to retry. 62 | */ 63 | 64 | double getReal(void); 65 | 66 | /* 67 | * Function: getLine 68 | * Usage: s = getLine(); 69 | * --------------------- 70 | * Reads a line of text from standard input and returns the line 71 | * as a string. The newline character that terminates the input 72 | * is not stored as part of the result. 73 | */ 74 | 75 | string getLine(void); 76 | 77 | /* 78 | * Function: readLine 79 | * Usage: s = readLine(infile); 80 | * ---------------------------- 81 | * Reads a line of text from the input file and returns the line 82 | * as a string. The newline character that terminates the input 83 | * is not stored as part of the result. The readLine 84 | * function returns NULL if infile is 85 | * at the end-of-file position. 86 | */ 87 | 88 | string readLine(FILE *infile); 89 | 90 | /* 91 | * Function: readLinesFromStream 92 | * Usage: string *array = readLinesFromStream(infile); 93 | * --------------------------------------------------- 94 | * Reads an entire file into a NULL-terminated array of lines. 95 | * Opening and closing the file stream is the responsibility of the caller. 96 | */ 97 | 98 | string *readLinesFromStream(FILE *infile); 99 | 100 | /* 101 | * Function: readLinesFromFile 102 | * Usage: string *array = readLinesFromFile(filename); 103 | * --------------------------------------------------- 104 | * Reads an entire file into a NULL-terminated array of lines. 105 | * This version opens the file, reads it, and closes it at the end. If the 106 | * file name is "-", the function reads from stdin. 107 | */ 108 | 109 | string *readLinesFromFile(string filename); 110 | 111 | #endif 112 | -------------------------------------------------------------------------------- /c/include/sound.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: sound.h 3 | * ------------- 4 | * This interface defines an abstract type that represents a sound. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | #ifndef _sound_h 26 | #define _sound_h 27 | 28 | /* 29 | * Type: Sound 30 | * ----------- 31 | * This type encapsulates a sound file. The sound file is specified in the 32 | * constructor and must be a file in either the current directory or a 33 | * subdirectory named sounds. 34 | * 35 | *

The following code, for example, plays the sound file 36 | * ringtone.wav: 37 | * 38 | *

39 |  *    Sound ringtone = newSound("ringtone.wav");
40 |  *    play(ringtone);
41 |  *
42 | */ 43 | 44 | typedef struct SoundCDT *Sound; 45 | 46 | /* 47 | * Function: newSound 48 | * Usage: sound = newSound(filename); 49 | * ---------------------------------- 50 | * Creates a Sound object from the specified file. 51 | */ 52 | 53 | Sound newSound(string filename); 54 | 55 | /* 56 | * Function: freeSound 57 | * Usage: freeSound(sound); 58 | * ------------------------ 59 | * Frees the memory associated with the sound. 60 | */ 61 | 62 | void freeSound(Sound sound); 63 | 64 | /* 65 | * Function: play 66 | * Usage: play(sound); 67 | * ------------------- 68 | * Starts playing the sound. This call returns immediately without waiting 69 | * for the sound to finish. 70 | */ 71 | 72 | void playSound(Sound sound); 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /c/include/stack.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: stack.h 3 | * ------------- 4 | * This interface exports an abstraction for stacks. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | #ifndef _stack_h 26 | #define _stack_h 27 | 28 | #include "cslib.h" 29 | #include "generic.h" 30 | 31 | /* 32 | * Type: Stack 33 | * ----------- 34 | * The abstract data type for the stack. 35 | */ 36 | 37 | typedef struct StackCDT *Stack; 38 | 39 | /* 40 | * Function: newStack 41 | * Usage: stack = newStack(); 42 | * -------------------------- 43 | * Allocates and returns a new stack, which is initially empty. 44 | */ 45 | 46 | Stack newStack(void); 47 | 48 | /* 49 | * Function: freeStack 50 | * Usage: freeStack(stack); 51 | * ------------------------ 52 | * Frees the storage associated with the stack. 53 | */ 54 | 55 | void freeStack(Stack stack); 56 | 57 | /* 58 | * Function: push 59 | * Usage: push(stack, element); 60 | * ---------------------------- 61 | * Pushes the specified element onto the stack. 62 | */ 63 | 64 | void push(Stack stack, void *element); 65 | 66 | /* 67 | * Function: pop 68 | * Usage: element = pop(stack); 69 | * ---------------------------- 70 | * Pops the top element from the stack and returns that value. 71 | * The first value popped is always the last one that was pushed. 72 | * If the stack is empty when pop is called, the 73 | * implementation calls error with an appropriate message. 74 | */ 75 | 76 | void *pop(Stack stack); 77 | 78 | /* 79 | * Function: peek 80 | * Usage: element = peek(stack); 81 | * ----------------------------- 82 | * Returns the top element from the stack without removing it. 83 | * If the stack is empty when pop is called, the 84 | * implementation calls error with an appropriate message. 85 | */ 86 | 87 | void *peekStack(Stack stack); 88 | 89 | /* 90 | * Function: isEmpty 91 | * Usage: if (isEmpty(stack)) . . . 92 | * -------------------------------- 93 | * Tests whether the stack is empty. 94 | */ 95 | 96 | bool isEmptyStack(Stack stack); 97 | 98 | /* 99 | * Function: size 100 | * Usage: n = size(stack); 101 | * ----------------------- 102 | * Returns the number of elements currently pushed on the stack. 103 | */ 104 | 105 | int sizeStack(Stack stack); 106 | 107 | /* 108 | * Function: clear 109 | * Usage: clear(stack); 110 | * -------------------- 111 | * Removes all elements from the stack. 112 | */ 113 | 114 | void clearStack(Stack stack); 115 | 116 | /* 117 | * Function: clone 118 | * Usage: newstack = clone(stack); 119 | * ------------------------------- 120 | * Creates a copy of the stack. The clone function copies 121 | * only the first level of the structure and does not copy the individual 122 | * elements. 123 | */ 124 | 125 | Stack cloneStack(Stack stack); 126 | 127 | #endif 128 | -------------------------------------------------------------------------------- /c/include/strbuf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: strbuf.h 3 | * -------------- 4 | * This interface defines a class that allows strings to grow by 5 | * concatentation. 6 | */ 7 | 8 | /*************************************************************************/ 9 | /* Stanford Portable Library */ 10 | /* Copyright (C) 2013 by Eric Roberts */ 11 | /* */ 12 | /* This program is free software: you can redistribute it and/or modify */ 13 | /* it under the terms of the GNU General Public License as published by */ 14 | /* the Free Software Foundation, either version 3 of the License, or */ 15 | /* (at your option) any later version. */ 16 | /* */ 17 | /* This program is distributed in the hope that it will be useful, */ 18 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 19 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 20 | /* GNU General Public License for more details. */ 21 | /* */ 22 | /* You should have received a copy of the GNU General Public License */ 23 | /* along with this program. If not, see . */ 24 | /*************************************************************************/ 25 | 26 | #ifndef _strbuf_h 27 | #define _strbuf_h 28 | 29 | #include 30 | #include "cslib.h" 31 | 32 | /* 33 | * Type: StringBuffer 34 | * ------------------ 35 | * This type provides an efficient, memory-safe mechanism for strings 36 | * that grow by the addition of characters. 37 | */ 38 | 39 | typedef struct StringBufferCDT *StringBuffer; 40 | 41 | /* 42 | * Function: newStringBuffer 43 | * Usage: sb = newStringBuffer(); 44 | * ------------------------------ 45 | * Creates a new string buffer that expands dynamically if needed. 46 | * The buffer is initially empty. 47 | */ 48 | 49 | StringBuffer newStringBuffer(); 50 | 51 | /* 52 | * Function: freeStringBuffer 53 | * Usage: freeStringBuffer(sb); 54 | * ---------------------------- 55 | * Frees the storage associated with the string buffer. 56 | */ 57 | 58 | void freeStringBuffer(StringBuffer sb); 59 | 60 | /* 61 | * Function: pushChar 62 | * Usage: pushChar(sb, ch); 63 | * ------------------------ 64 | * Appends (pushes) the character ch onto the end of the 65 | * string buffer. 66 | */ 67 | 68 | void pushChar(StringBuffer sb, char ch); 69 | 70 | /* 71 | * Function: popChar 72 | * Usage: ch = popChar(sb); 73 | * ------------------------ 74 | * Pops and removes the last character from the string buffer. 75 | */ 76 | 77 | char popChar(StringBuffer sb); 78 | 79 | /* 80 | * Function: appendString 81 | * Usage: appendString(sb, str); 82 | * ----------------------------- 83 | * Appends the string str to the end of the string buffer. 84 | */ 85 | 86 | void appendString(StringBuffer sb, string str); 87 | 88 | /* 89 | * Function: sbprintf 90 | * Usage: sbprintf(sb, format, ...); 91 | * --------------------------------- 92 | * Expands a printf-style format string and arguments onto 93 | * the end of the string buffer. 94 | */ 95 | 96 | void sbprintf(StringBuffer sb, string format, ...); 97 | 98 | /* 99 | * Function: isEmpty 100 | * Usage: if (isEmpty(sb)) . . . 101 | * ----------------------------- 102 | * Returns true if the string buffer is empty. 103 | */ 104 | 105 | bool isEmptyStringBuffer(StringBuffer vec); 106 | 107 | /* 108 | * Function: size 109 | * Usage: n = size(sb); 110 | * -------------------- 111 | * Returns the number of characters in the string buffer. 112 | */ 113 | 114 | int sizeStringBuffer(StringBuffer vector); 115 | 116 | /* 117 | * Function: clear 118 | * Usage: clear(sb); 119 | * ----------------- 120 | * Removes all characters from the string buffer. 121 | */ 122 | 123 | void clearStringBuffer(StringBuffer sb); 124 | 125 | /* 126 | * Function: getString 127 | * Usage: str = getString(sb); 128 | * --------------------------- 129 | * Returns the string stored inside the buffer. Clients must copy this 130 | * string if they need to retain it. 131 | */ 132 | 133 | string getString(StringBuffer sb); 134 | 135 | /* 136 | * Friend function: printfCapacity 137 | * Usage: capacity = printfCapacity(format, args); 138 | * ----------------------------------------------- 139 | * Returns a character count that will be sufficient to hold the result 140 | * of printing format with arguments substituted from the 141 | * args list. This bound is guaranteed to be adequate but 142 | * is not necessarily tight. 143 | */ 144 | 145 | int printfCapacity(string format, va_list args); 146 | 147 | /* 148 | * Friend function: sbFormat 149 | * Usage: sbFormat(sb, capacity, format, list); 150 | * -------------------------------------------- 151 | * Works like sbprintf except that the list is an argument 152 | * list as in stdarg.h. The capacity argument 153 | * is the required capacity, as returned by printfCapacity. 154 | */ 155 | 156 | void sbFormat(StringBuffer sb, int capacity, string format, va_list args); 157 | 158 | #endif 159 | -------------------------------------------------------------------------------- /c/src/cmpfn.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: cmpfn.c 3 | * ------------- 4 | * This file implements the comparison functions exported by 5 | * the cmpfn.h interface. The reason to centralize them in 6 | * a single module is to allow the same functions to be 7 | * shared among many different modules. 8 | */ 9 | 10 | /*************************************************************************/ 11 | /* Stanford Portable Library */ 12 | /* Copyright (C) 2013 by Eric Roberts */ 13 | /* */ 14 | /* This program is free software: you can redistribute it and/or modify */ 15 | /* it under the terms of the GNU General Public License as published by */ 16 | /* the Free Software Foundation, either version 3 of the License, or */ 17 | /* (at your option) any later version. */ 18 | /* */ 19 | /* This program is distributed in the hope that it will be useful, */ 20 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 21 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 22 | /* GNU General Public License for more details. */ 23 | /* */ 24 | /* You should have received a copy of the GNU General Public License */ 25 | /* along with this program. If not, see . */ 26 | /*************************************************************************/ 27 | 28 | #include 29 | #include "cmpfn.h" 30 | #include "cslib.h" 31 | #include "strlib.h" 32 | 33 | /* Exported entries */ 34 | 35 | int intCmpFn(const void *p1, const void *p2) { 36 | int v1, v2; 37 | 38 | v1 = *((int *) p1); 39 | v2 = *((int *) p2); 40 | if (v1 == v2) return 0; 41 | return (v1 < v2) ? -1 : +1; 42 | } 43 | 44 | int shortCmpFn(const void *p1, const void *p2) { 45 | short v1, v2; 46 | 47 | v1 = *((short *) p1); 48 | v2 = *((short *) p2); 49 | if (v1 == v2) return 0; 50 | return (v1 < v2) ? -1 : +1; 51 | } 52 | 53 | int longCmpFn(const void *p1, const void *p2) { 54 | long v1, v2; 55 | 56 | v1 = *((long *) p1); 57 | v2 = *((long *) p2); 58 | if (v1 == v2) return 0; 59 | return (v1 < v2) ? -1 : +1; 60 | } 61 | 62 | int charCmpFn(const void *p1, const void *p2) { 63 | unsigned char v1, v2; 64 | 65 | v1 = *((unsigned char *) p1); 66 | v2 = *((unsigned char *) p2); 67 | if (v1 == v2) return 0; 68 | return (v1 < v2) ? -1 : +1; 69 | } 70 | 71 | int floatCmpFn(const void *p1, const void *p2) { 72 | float v1, v2; 73 | 74 | v1 = *((float *) p1); 75 | v2 = *((float *) p2); 76 | if (v1 == v2) return 0; 77 | return (v1 < v2) ? -1 : +1; 78 | } 79 | 80 | int doubleCmpFn(const void *p1, const void *p2) { 81 | double v1, v2; 82 | 83 | v1 = *((double *) p1); 84 | v2 = *((double *) p2); 85 | if (v1 == v2) return 0; 86 | return (v1 < v2) ? -1 : +1; 87 | } 88 | 89 | int unsignedCmpFn(const void *p1, const void *p2) { 90 | unsigned v1, v2; 91 | 92 | v1 = *((unsigned *) p1); 93 | v2 = *((unsigned *) p2); 94 | if (v1 == v2) return 0; 95 | return (v1 < v2) ? -1 : +1; 96 | } 97 | 98 | int unsignedShortCmpFn(const void *p1, const void *p2) { 99 | unsigned short v1, v2; 100 | 101 | v1 = *((unsigned short *) p1); 102 | v2 = *((unsigned short *) p2); 103 | if (v1 == v2) return 0; 104 | return (v1 < v2) ? -1 : +1; 105 | } 106 | 107 | int unsignedLongCmpFn(const void *p1, const void *p2) { 108 | unsigned long v1, v2; 109 | 110 | v1 = *((unsigned long *) p1); 111 | v2 = *((unsigned long *) p2); 112 | if (v1 == v2) return 0; 113 | return (v1 < v2) ? -1 : +1; 114 | } 115 | 116 | int unsignedCharCmpFn(const void *p1, const void *p2) { 117 | unsigned char v1, v2; 118 | 119 | v1 = *((unsigned char *) p1); 120 | v2 = *((unsigned char *) p2); 121 | if (v1 == v2) return 0; 122 | return (v1 < v2) ? -1 : +1; 123 | } 124 | 125 | int stringCmpFn(const void *p1, const void *p2) { 126 | return stringCompare(*((string *) p1), *((string *) p2)); 127 | } 128 | 129 | int pointerCmpFn(const void *p1, const void *p2) { 130 | void *v1, *v2; 131 | 132 | v1 = *((void **) p1); 133 | v2 = *((void **) p2); 134 | if (v1 == v2) return 0; 135 | return (v1 < v2) ? -1 : +1; 136 | } 137 | -------------------------------------------------------------------------------- /c/src/foreach.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: foreach.c 3 | * --------------- 4 | * This file implements the foreach construct. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | #include 26 | #include "foreach.h" 27 | #include "cslib.h" 28 | #include "iterator.h" 29 | 30 | typedef struct Cell { 31 | void *dst; 32 | Iterator iterator; 33 | struct Cell *link; 34 | } Cell; 35 | 36 | static Cell *iteratorList = NULL; 37 | 38 | /* Entry points */ 39 | 40 | void initForEach(void *dst, void *collection) { 41 | Cell *cp; 42 | 43 | for (cp = iteratorList; cp != NULL && cp->dst != dst; cp = cp->link) { 44 | /* Empty */ 45 | } 46 | if (cp == NULL) { 47 | cp = newBlock(Cell *); 48 | cp->dst = dst; 49 | cp->link = iteratorList; 50 | iteratorList = cp; 51 | } else { 52 | if (cp->iterator != NULL) freeIterator(cp->iterator); 53 | } 54 | cp->iterator = newIterator(collection); 55 | } 56 | 57 | bool stepForEach(void *dst) { 58 | Cell *cp; 59 | bool result; 60 | 61 | for (cp = iteratorList; cp != NULL && cp->dst != dst; cp = cp->link) { 62 | /* Empty */ 63 | } 64 | if (cp == NULL) error("foreach iterator undefined"); 65 | result = stepIterator(cp->iterator, dst); 66 | if (!result) { 67 | freeIterator(cp->iterator); 68 | cp->iterator = NULL; 69 | } 70 | return result; 71 | } 72 | -------------------------------------------------------------------------------- /c/src/ginteractors.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: ginteractors.c 3 | * -------------------- 4 | * This file is empty. The ginteractors.h interface is implemented by the 5 | * gobjects.c file. 6 | */ 7 | -------------------------------------------------------------------------------- /c/src/gmath.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: gmath.cpp 3 | * --------------- 4 | * This file implements the gmath.h interface. In all cases, the 5 | * implementation for each function requires only one line of code, 6 | * which makes detailed documentation unnecessary. 7 | */ 8 | 9 | /*************************************************************************/ 10 | /* Stanford Portable Library */ 11 | /* Copyright (C) 2013 by Eric Roberts */ 12 | /* */ 13 | /* This program is free software: you can redistribute it and/or modify */ 14 | /* it under the terms of the GNU General Public License as published by */ 15 | /* the Free Software Foundation, either version 3 of the License, or */ 16 | /* (at your option) any later version. */ 17 | /* */ 18 | /* This program is distributed in the hope that it will be useful, */ 19 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 20 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 21 | /* GNU General Public License for more details. */ 22 | /* */ 23 | /* You should have received a copy of the GNU General Public License */ 24 | /* along with this program. If not, see . */ 25 | /*************************************************************************/ 26 | 27 | #include 28 | #include "gmath.h" 29 | 30 | double sinDegrees(double angle) { 31 | return sin(toRadians(angle)); 32 | } 33 | 34 | double cosDegrees(double angle) { 35 | return cos(toRadians(angle)); 36 | } 37 | 38 | double tanDegrees(double angle) { 39 | return tan(toRadians(angle)); 40 | } 41 | 42 | double toDegrees(double radians) { 43 | return radians * 180 / PI; 44 | } 45 | 46 | double toRadians(double degrees) { 47 | return degrees * PI / 180; 48 | } 49 | 50 | double vectorDistance(double x, double y) { 51 | return sqrt(x * x + y * y); 52 | } 53 | 54 | double vectorAngle(double x, double y) { 55 | return (x == 0 && y == 0) ? 0 : toDegrees(atan2(-y, x)); 56 | } 57 | -------------------------------------------------------------------------------- /c/src/gtimer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: gtimer.c 3 | * -------------- 4 | * This file implements the gtimer.h interface. The functions that 5 | * implement receiving timer from the environment are implemented in 6 | * the platform package. 7 | */ 8 | 9 | /*************************************************************************/ 10 | /* Stanford Portable Library */ 11 | /* Copyright (C) 2013 by Eric Roberts */ 12 | /* */ 13 | /* This program is free software: you can redistribute it and/or modify */ 14 | /* it under the terms of the GNU General Public License as published by */ 15 | /* the Free Software Foundation, either version 3 of the License, or */ 16 | /* (at your option) any later version. */ 17 | /* */ 18 | /* This program is distributed in the hope that it will be useful, */ 19 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 20 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 21 | /* GNU General Public License for more details. */ 22 | /* */ 23 | /* You should have received a copy of the GNU General Public License */ 24 | /* along with this program. If not, see . */ 25 | /*************************************************************************/ 26 | 27 | #include 28 | #include "cslib.h" 29 | #include "gtimer.h" 30 | #include "platform.h" 31 | 32 | /* 33 | * Type GTimerCDT 34 | * -------------- 35 | * This type is the concrete type for the timer. 36 | */ 37 | 38 | struct GTimerCDT { 39 | double duration; 40 | }; 41 | 42 | GTimer newGTimer(double milliseconds) { 43 | GTimer timer; 44 | 45 | timer = newBlock(GTimer); 46 | timer->duration = milliseconds; 47 | createTimerOp(timer, milliseconds); 48 | return timer; 49 | } 50 | 51 | void freeGTimer(GTimer timer) { 52 | deleteTimerOp(timer); 53 | } 54 | 55 | void startTimer(GTimer timer) { 56 | startTimerOp(timer); 57 | } 58 | 59 | void stopTimer(GTimer timer) { 60 | stopTimerOp(timer); 61 | } 62 | -------------------------------------------------------------------------------- /c/src/gtypes.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: gtypes.c 3 | * -------------- 4 | * This file implements the gtypes.h interface. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | #include "cslib.h" 26 | #include "generic.h" 27 | #include "gtypes.h" 28 | 29 | GPoint createGPoint(double x, double y) { 30 | GPoint pt; 31 | 32 | pt.x = x; 33 | pt.y = y; 34 | return pt; 35 | } 36 | 37 | double getXGPoint(GPoint pt) { 38 | return pt.x; 39 | } 40 | 41 | double getYGPoint(GPoint pt) { 42 | return pt.y; 43 | } 44 | 45 | GDimension createGDimension(double width, double height) { 46 | GDimension dim; 47 | 48 | dim.width = width; 49 | dim.height = height; 50 | return dim; 51 | } 52 | 53 | double getWidthGDimension(GDimension dim) { 54 | return dim.width; 55 | } 56 | 57 | double getHeightGDimension(GDimension dim) { 58 | return dim.height; 59 | } 60 | 61 | GRectangle createGRectangle(double x, double y, double width, double height) { 62 | GRectangle r; 63 | 64 | r.x = x; 65 | r.y = y; 66 | r.width = width; 67 | r.height = height; 68 | return r; 69 | } 70 | 71 | double getXGRectangle(GRectangle r) { 72 | return r.x; 73 | } 74 | 75 | double getYGRectangle(GRectangle r) { 76 | return r.y; 77 | } 78 | 79 | double getWidthGRectangle(GRectangle r) { 80 | return r.width; 81 | } 82 | 83 | double getHeightGRectangle(GRectangle r) { 84 | return r.height; 85 | } 86 | 87 | bool isEmptyGRectangle(GRectangle r) { 88 | return r.width <= 0 || r.height <= 0; 89 | } 90 | 91 | bool containsGRectangle(GRectangle r, GPoint pt) { 92 | return pt.x >= r.x && pt.y >= r.y && pt.x < r.x + r.width 93 | && pt.y < r.y + r.height; 94 | } 95 | 96 | /**********************************************************************/ 97 | /* Unit test for the gtypes module */ 98 | /**********************************************************************/ 99 | 100 | #ifndef _NOTEST_ 101 | 102 | #include "unittest.h" 103 | 104 | /* Unit test */ 105 | 106 | void testGTypesModule(void) { 107 | GPoint origin, pt; 108 | GDimension dim; 109 | GRectangle r; 110 | 111 | trace(origin = createGPoint(0, 0)); 112 | trace(pt = createGPoint(2, 3)); 113 | trace(dim = createGDimension(3, 1)); 114 | trace(r = createGRectangle(1, 2, 3, 4)); 115 | test(getX(origin), 0.0); 116 | test(getY(origin), 0.0); 117 | test(getX(pt), 2.0); 118 | test(getY(pt), 3.0); 119 | test(getWidth(dim), 3.0); 120 | test(getHeight(dim), 1.0); 121 | test(getX(r), 1.0); 122 | test(getY(r), 2.0); 123 | test(getWidth(r), 3.0); 124 | test(getHeight(r), 4.0); 125 | test(isEmpty(r), false); 126 | test(contains(r, origin), false); 127 | test(contains(r, pt), true); 128 | } 129 | 130 | #endif 131 | -------------------------------------------------------------------------------- /c/src/loadobj.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: loadobj.c 3 | * --------------- 4 | * This file implements the loadobj.h interface. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include "cslib.h" 31 | #include "filelib.h" 32 | #include "loadobj.h" 33 | #include "strlib.h" 34 | 35 | #ifndef MAX_PATH_NAME 36 | # define MAX_PATH_NAME 1000 37 | #endif 38 | 39 | typedef struct Cell { 40 | void *handle; 41 | struct Cell *link; 42 | } *HandleList; 43 | 44 | HandleList handles = NULL; 45 | 46 | void loadObject(string pathname) { 47 | HandleList hl; 48 | static char fullPath[MAX_PATH_NAME]; 49 | 50 | hl = newBlock(HandleList); 51 | switch (pathname[0]) { 52 | case '/': 53 | strcpy(fullPath, pathname); 54 | break; 55 | case '~': 56 | strcpy(fullPath, expandPathname(pathname)); 57 | break; 58 | default: 59 | getcwd(fullPath, MAX_PATH_NAME); 60 | strcat(fullPath, "/"); 61 | strcat(fullPath, pathname); 62 | break; 63 | } 64 | hl->handle = dlopen(fullPath, RTLD_NOW); 65 | if (hl->handle == NULL) { 66 | freeBlock(hl); 67 | error("loadObject: %s", dlerror()); 68 | } 69 | hl->link = handles; 70 | handles = hl; 71 | } 72 | 73 | proc findFunction(string fnname) { 74 | HandleList hl; 75 | void *fn; 76 | 77 | for (hl = handles; hl != NULL; hl = hl->link) { 78 | fn = dlsym(hl->handle, fnname); 79 | if (fn != NULL) return (proc) (long) fn; 80 | } 81 | error("findFunction: %s", dlerror()); 82 | } 83 | 84 | void loadSymbols(string progname) { 85 | /* Empty */ 86 | } 87 | -------------------------------------------------------------------------------- /c/src/sound.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: sound.c 3 | * ------------- 4 | * This file implements the sound.h interface. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | #include 26 | #include "cslib.h" 27 | #include "platform.h" 28 | #include "sound.h" 29 | 30 | /* 31 | * Type SoundCDT 32 | * ------------- 33 | * This type is the concrete type for sounds. 34 | */ 35 | 36 | struct SoundCDT { 37 | string filename; 38 | }; 39 | 40 | Sound newSound(string filename) { 41 | Sound sound; 42 | 43 | sound = newBlock(Sound); 44 | sound->filename = filename; 45 | createSoundOp(sound, filename); 46 | return sound; 47 | } 48 | 49 | void freeSound(Sound sound) { 50 | deleteSoundOp(sound); 51 | } 52 | 53 | void playSound(Sound sound) { 54 | playSoundOp(sound); 55 | } 56 | -------------------------------------------------------------------------------- /c/tests/TestStanfordCSLib.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: TestStanfordCSLib.c 3 | * ------------------------- 4 | * This file creates an application that runs all the unit tests in 5 | * the StanfordCSLib package. 6 | */ 7 | 8 | /*************************************************************************/ 9 | /* Stanford Portable Library */ 10 | /* Copyright (C) 2013 by Eric Roberts */ 11 | /* */ 12 | /* This program is free software: you can redistribute it and/or modify */ 13 | /* it under the terms of the GNU General Public License as published by */ 14 | /* the Free Software Foundation, either version 3 of the License, or */ 15 | /* (at your option) any later version. */ 16 | /* */ 17 | /* This program is distributed in the hope that it will be useful, */ 18 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 19 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 20 | /* GNU General Public License for more details. */ 21 | /* */ 22 | /* You should have received a copy of the GNU General Public License */ 23 | /* along with this program. If not, see . */ 24 | /*************************************************************************/ 25 | 26 | #include 27 | #include "cslib.h" 28 | #include "strlib.h" 29 | #include "unittest.h" 30 | 31 | #define PUBLIC 0 32 | #define PRIVATE 0x1 33 | #define INTERACTIVE 0x2 34 | 35 | typedef struct { 36 | string name; 37 | proc fn; 38 | int flags; 39 | } TestEntry; 40 | 41 | extern void testBSTModule(void); 42 | extern void testCharSetModule(void); 43 | extern void testExceptionModule(void); 44 | extern void testFilelibModule(void); 45 | extern void testGEventsModule(void); 46 | extern void testGraphModule(void); 47 | extern void testGTypesModule(void); 48 | extern void testHashMapModule(void); 49 | extern void testMapModule(void); 50 | extern void testOptionsModule(void); 51 | extern void testPriorityQueueModule(void); 52 | extern void testQueueModule(void); 53 | extern void testRandomModule(void); 54 | extern void testSetModule(void); 55 | extern void testStackModule(void); 56 | extern void testStrbufModule(void); 57 | extern void testStrlibModule(void); 58 | extern void testThreadModule(void); 59 | extern void testTokenScannerModule(void); 60 | extern void testVectorModule(void); 61 | 62 | static TestEntry TEST_MODULES[] = { 63 | { "bst", testBSTModule }, 64 | { "charset", testCharSetModule }, 65 | { "exception", testExceptionModule }, 66 | { "filelib", testFilelibModule }, 67 | { "gevents", testGEventsModule }, 68 | { "graph", testGraphModule }, 69 | { "gtypes", testGTypesModule }, 70 | { "hashmap", testHashMapModule }, 71 | { "map", testMapModule }, 72 | { "options", testOptionsModule }, 73 | { "pqueue", testPriorityQueueModule }, 74 | { "queue", testQueueModule }, 75 | { "random", testRandomModule }, 76 | { "set", testSetModule }, 77 | { "stack", testStackModule }, 78 | { "strbuf", testStrbufModule }, 79 | { "strlib", testStrlibModule }, 80 | { "tokenscanner", testTokenScannerModule }, 81 | { "vector", testVectorModule }, 82 | }; 83 | 84 | static int N_TEST_MODULES = sizeof TEST_MODULES / sizeof TEST_MODULES[0]; 85 | 86 | static int findTestModule(string name); 87 | 88 | int main(int argc, string argv[]) { 89 | int i, index; 90 | string arg; 91 | bool modulesFound; 92 | 93 | modulesFound = false; 94 | for (i = 1; i < argc; i++) { 95 | arg = argv[i]; 96 | if (startsWith(arg, "-")) { 97 | if (stringEqual(arg, "-v") || stringEqual(arg, "-verbose")) { 98 | setVerboseTestingFlag(true); 99 | } else { 100 | error("Unrecognized option %s", arg); 101 | } 102 | } else { 103 | index = findTestModule(arg); 104 | if (index == -1) { 105 | error("No module named %s", arg); 106 | } 107 | testModule(TEST_MODULES[index].name, TEST_MODULES[index].fn); 108 | modulesFound = true; 109 | } 110 | } 111 | if (!modulesFound) { 112 | for (i = 0; i < N_TEST_MODULES; i++) { 113 | if ((TEST_MODULES[i].flags & (INTERACTIVE | PRIVATE)) == 0) { 114 | testModule(TEST_MODULES[i].name, TEST_MODULES[i].fn); 115 | } 116 | } 117 | } 118 | exit(0); 119 | } 120 | 121 | static int findTestModule(string name) { 122 | int i, index; 123 | 124 | index = -1; 125 | for (i = 0; i < N_TEST_MODULES; i++) { 126 | if (startsWith(TEST_MODULES[i].name, name)) { 127 | if (index != -1) return -1; 128 | index = i; 129 | } 130 | } 131 | return index; 132 | } 133 | -------------------------------------------------------------------------------- /docs/charset-h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | ../charset.h 4 | 5 | 6 | 7 |
  8 | /*
  9 |  * File: charset.h
 10 |  * ---------------
 11 |  * This interface exports an abstract type that represents sets of
 12 |  * characters.
 13 |  */
 14 | 
 15 | #ifndef _charset_h
 16 | #define _charset_h
 17 | 
 18 | #include <stdarg.h>
 19 | #include "cslib.h"
 20 | #include "generic.h"
 21 | 
 22 | /*
 23 |  * Type: CharSet
 24 |  * -------------
 25 |  * Defines the abstract type used to represent sets of characters
 26 |  */
 27 | 
 28 | typedef struct CharSetCDT *CharSet;
 29 | 
 30 | /* Exported entries */
 31 | 
 32 | /*
 33 |  * Function: newCharSet
 34 |  * Usage: set = newCharSet();
 35 |  * --------------------------
 36 |  * Creates an empty set capable of holding characters.
 37 |  */
 38 | 
 39 | CharSet newCharSet();
 40 | 
 41 | /*
 42 |  * Function: freeCharSet
 43 |  * Usage: freeCharSet(set);
 44 |  * ------------------------
 45 |  * Frees the storage associated with a character set.
 46 |  */
 47 | 
 48 | void freeCharSet(CharSet set);
 49 | 
 50 | /*
 51 |  * Function: size
 52 |  * Usage: n = size(set);
 53 |  * ---------------------
 54 |  * Returns the number of elements in the set.
 55 |  */
 56 | 
 57 | int sizeCharSet(CharSet set);
 58 | 
 59 | /*
 60 |  * Function: isEmpty
 61 |  * Usage: if (isEmpty(set)) . . .
 62 |  * ------------------------------
 63 |  * Returns true if the set has no elements.
 64 |  */
 65 | 
 66 | bool isEmptyCharSet(CharSet set);
 67 | 
 68 | /*
 69 |  * Function: clear
 70 |  * Usage: clear(set);
 71 |  * ------------------
 72 |  * Removes all characters from the set.
 73 |  */
 74 | 
 75 | void clearCharSet(CharSet set);
 76 | 
 77 | /*
 78 |  * Function: clone
 79 |  * Usage: newset = clone(set);
 80 |  * ---------------------------
 81 |  * Creates a copy of the set.  The clone function copies only the first
 82 |  * level of the structure and does not copy the individual elements.
 83 |  */
 84 | 
 85 | CharSet cloneCharSet(CharSet set);
 86 | 
 87 | /*
 88 |  * Function: contains
 89 |  * Usage: if (contains(set, ch)) . . .
 90 |  * -----------------------------------
 91 |  * Returns true if the character ch is a member of the set.
 92 |  */
 93 | 
 94 | bool containsCharSet(CharSet set, char ch);
 95 | 
 96 | /*
 97 |  * Function: add
 98 |  * Usage: add(set, ch);
 99 |  * --------------------
100 |  * Adds the character ch to the set.
101 |  */
102 | 
103 | void addCharSet(CharSet set, char ch);
104 | 
105 | /*
106 |  * Function: addString
107 |  * Usage: addString(set, str);
108 |  * ---------------------------
109 |  * Adds the characters in str to the set.
110 |  */
111 | 
112 | void addString(CharSet set, string str);
113 | 
114 | /*
115 |  * Function: remove
116 |  * Usage: remove(set, ch);
117 |  * -----------------------
118 |  * Removes an element from the set.
119 |  */
120 | 
121 | void removeCharSet(CharSet set, char ch);
122 | 
123 | /*
124 |  * Function: equals
125 |  * Usage: if (equals(s1, s2)) . . .
126 |  * --------------------------------
127 |  * Returns true if s1 and s2 are equal, which means that they contain the
128 |  * same elements.
129 |  */
130 | 
131 | bool equalsCharSet(CharSet s1, CharSet s2);
132 | 
133 | /*
134 |  * Function: isSubset
135 |  * Usage: if (isSubset(s1, s2)) . . .
136 |  * ----------------------------------
137 |  * Returns true if s1 is a subset of s2.
138 |  */
139 | 
140 | bool isSubsetCharSet(CharSet s1, CharSet s2);
141 | 
142 | /*
143 |  * Function: union
144 |  * Usage: set = union(s1, s2);
145 |  * ---------------------------
146 |  * Returns a new set consisting of all elements in either s1 or s2.
147 |  */
148 | 
149 | CharSet unionCharSet(CharSet s1, CharSet s2);
150 | 
151 | /*
152 |  * Function: intersection
153 |  * Usage: set = intersection(s1, s2);
154 |  * ----------------------------------
155 |  * Returns a new set consisting of all elements in both s1 and s2.
156 |  */
157 | 
158 | CharSet intersectionCharSet(CharSet s1, CharSet s2);
159 | 
160 | /*
161 |  * Function: setDifference
162 |  * Usage: set = setDifference(s1, s2);
163 |  * -----------------------------------
164 |  * Returns a new set consisting of all elements in s1 that are not elements
165 |  * of s2.
166 |  */
167 | 
168 | CharSet setDifferenceCharSet(CharSet s1, CharSet s2);
169 | 
170 | #endif
171 | 
172 | 173 | 174 | -------------------------------------------------------------------------------- /docs/cmpfn-h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | ../cmpfn.h 4 | 5 | 6 | 7 |
  8 | /*
  9 |  * File: cmpfn.h
 10 |  * -------------
 11 |  * This interface exports several comparison functions for use with the
 12 |  * other library packages.
 13 |  */
 14 | 
 15 | #ifndef _cmpfn_h
 16 | #define _cmpfn_h
 17 | 
 18 | #include "cslib.h"
 19 | 
 20 | /*
 21 |  * Type: CompareFn
 22 |  * ---------------
 23 |  * This type defines the type space of comparison functions, each of which
 24 |  * take the addresses of their arguments and return an integer from the set
 25 |  * {-1, 0, +1} depending on whether the first argument is less than, equal
 26 |  * to, or greater than the second.
 27 |  */
 28 | 
 29 | typedef int (*CompareFn)(const void *p1, const void *p2);
 30 | 
 31 | /*
 32 |  * Function: intCmpFn
 33 |  * ------------------
 34 |  * Compares two values of type int whose addresses are supplied by the
 35 |  * pointer arguments p1 and p2.
 36 |  */
 37 | 
 38 | int intCmpFn(const void *p1, const void *p2);
 39 | 
 40 | /*
 41 |  * Function: shortCmpFn
 42 |  * --------------------
 43 |  * Compares two values of type short whose addresses are supplied by the
 44 |  * pointer arguments p1 and p2.
 45 |  */
 46 | 
 47 | int shortCmpFn(const void *p1, const void *p2);
 48 | 
 49 | /*
 50 |  * Function: longCmpFn
 51 |  * -------------------
 52 |  * Compares two values of type long whose addresses are supplied by the
 53 |  * pointer arguments p1 and p2.
 54 |  */
 55 | 
 56 | int longCmpFn(const void *p1, const void *p2);
 57 | 
 58 | /*
 59 |  * Function: charCmpFn
 60 |  * -------------------
 61 |  * Compares two values of type char whose addresses are supplied by the
 62 |  * pointer arguments p1 and p2.
 63 |  */
 64 | 
 65 | int charCmpFn(const void *p1, const void *p2);
 66 | 
 67 | /*
 68 |  * Function: floatCmpFn
 69 |  * --------------------
 70 |  * Compares two values of type float whose addresses are supplied by the
 71 |  * pointer arguments p1 and p2.
 72 |  */
 73 | 
 74 | int floatCmpFn(const void *p1, const void *p2);
 75 | 
 76 | /*
 77 |  * Function: doubleCmpFn
 78 |  * ---------------------
 79 |  * Compares two values of type double whose addresses are supplied by the
 80 |  * pointer arguments p1 and p2.
 81 |  */
 82 | 
 83 | int doubleCmpFn(const void *p1, const void *p2);
 84 | 
 85 | /*
 86 |  * Function: unsignedCmpFn
 87 |  * -----------------------
 88 |  * Compares two values of type unsigned whose addresses are supplied by the
 89 |  * pointer arguments p1 and p2.
 90 |  */
 91 | 
 92 | int unsignedCmpFn(const void *p1, const void *p2);
 93 | 
 94 | /*
 95 |  * Function: unsignedShortCmpFn
 96 |  * ----------------------------
 97 |  * Compares two values of type unsignedshort whose addresses are supplied
 98 |  * by the pointer arguments p1 and p2.
 99 |  */
100 | 
101 | int unsignedShortCmpFn(const void *p1, const void *p2);
102 | 
103 | /*
104 |  * Function: unsignedLongCmpFn
105 |  * ---------------------------
106 |  * Compares two values of type unsignedlong whose addresses are supplied by
107 |  * the pointer arguments p1 and p2.
108 |  */
109 | 
110 | int unsignedLongCmpFn(const void *p1, const void *p2);
111 | 
112 | /*
113 |  * Function: unsignedCharCmpFn
114 |  * ---------------------------
115 |  * Compares two values of type unsignedchar whose addresses are supplied by
116 |  * the pointer arguments p1 and p2.
117 |  */
118 | 
119 | int unsignedCharCmpFn(const void *p1, const void *p2);
120 | 
121 | /*
122 |  * Function: stringCmpFn
123 |  * ---------------------
124 |  * Compares two values of type string whose addresses are supplied by the
125 |  * pointer arguments p1 and p2.
126 |  */
127 | 
128 | int stringCmpFn(const void *p1, const void *p2);
129 | 
130 | /*
131 |  * Function: pointerCmpFn
132 |  * ----------------------
133 |  * Compares two values of type pointer whose addresses are supplied by the
134 |  * pointer arguments p1 and p2.  Pointer comparison is based on the numeric
135 |  * equivalent of the pointer.
136 |  */
137 | 
138 | int pointerCmpFn(const void *p1, const void *p2);
139 | 
140 | #endif
141 | 
142 | 143 | 144 | -------------------------------------------------------------------------------- /docs/cppdoc.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-size:100%; 3 | } 4 | 5 | h1 { 6 | font-family:"Arial", "Helvetica", "sansserif"; 7 | font-size:1.5em; 8 | font-weight:bold; 9 | } 10 | 11 | h2 { 12 | font-family:"Times New Roman", "Times", "serif"; 13 | font-size:1.4em; 14 | font-weight:bold; 15 | margin-bottom:0px; 16 | } 17 | 18 | .header { 19 | font-size:125%; 20 | } 21 | 22 | pre, code, .code { 23 | font-family:"Courier New", "Courier", "monospace"; 24 | font-weight:bold; 25 | font-size:90%; 26 | } 27 | 28 | .comment { 29 | color:blue; 30 | } 31 | 32 | .it { 33 | font-family:"Times New Roman", "Times", "serif"; 34 | font-weight:normal; 35 | font-style:italic; 36 | font-size:90%; 37 | } 38 | 39 | .banner { 40 | border:0; 41 | } 42 | 43 | .sansserif { 44 | font-family:"Helvetica Neue", "Helvetica", "Arial", "sansserif"; 45 | } 46 | 47 | .bannerText { 48 | font-family:"Helvetica Neue", "Helvetica", "Arial", "sansserif"; 49 | font-size:1.6em; 50 | font-weight:bold; 51 | } 52 | 53 | .index { 54 | border-collapse:collapse; 55 | margin-top:1px; 56 | } 57 | 58 | .indexHead { 59 | font-family:"Times New Roman", "Times", "serif"; 60 | font-size:1.4em; 61 | font-weight:bold; 62 | padding-top:1em; 63 | } 64 | 65 | .indexKey { 66 | border:1px solid black; 67 | padding:3px 5px; 68 | font-family:"Courier New", "Courier", "monospace"; 69 | font-weight:bold; 70 | font-size:90%; 71 | vertical-align: top; 72 | } 73 | 74 | .indexSynopsis { 75 | border:1px solid black; 76 | padding:3px 5px; 77 | vertical-align: top; 78 | } 79 | 80 | .include { 81 | font-family:"Courier New", "Courier", "monospace"; 82 | font-weight:bold; 83 | font-size:100%; 84 | } 85 | 86 | .detailCode { 87 | padding:0px; 88 | font-family:"Courier New", "Courier", "monospace"; 89 | font-weight:bold; 90 | font-size:100%; 91 | } 92 | 93 | .detailHTML { 94 | font-family:"Times New Roman", "Times", "serif"; 95 | font-size:1em; 96 | margin-left:20px; 97 | } 98 | 99 | .inset { 100 | margin-left:36px; 101 | } 102 | 103 | .usageCode { 104 | font-family:"Courier New", "Courier", "monospace"; 105 | font-weight:bold; 106 | font-size:90%; 107 | margin-left:40px; 108 | } 109 | -------------------------------------------------------------------------------- /docs/foreach-h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | ../foreach.h 4 | 5 | 6 | 7 |
 8 | /*
 9 |  * File: foreach.h
10 |  * ---------------
11 |  * This interface provides a simple syntactic extension for iterators that
12 |  * makes them much easier to read.  The general form of the foreach
13 |  * statement looks like this:
14 |  *
15 |  *    foreach (var in collection) {
16 |  *       statements
17 |  *    }
18 |  *
19 |  * In C, the variable var must be declared outside the loop.
20 |  */
21 | 
22 | #ifndef _foreach_h
23 | #define _foreach_h
24 | 
25 | #include "cslib.h"
26 | 
27 | /*
28 |  * Macro: foreach
29 |  * Usage: foreach (element in collection) { . . . }
30 |  * ------------------------------------------------
31 |  * This macro definition creates a new statement form that simplifies the
32 |  * use of iterators.  The variable element must be declared in the current
33 |  * scope and must be compatible with the base type of the collection.
34 |  */
35 | 
36 | #define in : (void *) (
37 | #define foreach(arg) \
38 |     for (initForEach(1 ? (void *) &arg), 0 ? &arg)); \
39 |          stepForEach(1 ? (void *) &arg)); )
40 | 
41 | void initForEach(void *dst, void *collection);
42 | bool stepForEach(void *dst);
43 | 
44 | #endif
45 | 
46 | 47 | 48 | -------------------------------------------------------------------------------- /docs/foreach.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | foreach.h 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 | 14 |
15 |

foreach.h

16 | This interface provides a simple syntactic extension for iterators 17 | that makes them much easier to read. The general form of the 18 | foreach statement looks like this: 19 | 20 |
21 |    foreach (var in collection) {
22 |       statements
23 |    }
24 | 
25 | 26 | In C, the variable var must be declared outside the loop. 27 | 28 |
29 | 30 | 31 | -------------------------------------------------------------------------------- /docs/gmath-h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | ../gmath.h 4 | 5 | 6 | 7 |
  8 | /*
  9 |  * File: gmath.h
 10 |  * -------------
 11 |  * This interface exports several functions for working with graphical
 12 |  * geometry along with the constants PI and E.
 13 |  */
 14 | 
 15 | #ifndef _gmath_h
 16 | #define _gmath_h
 17 | 
 18 | /*
 19 |  * Constant: PI
 20 |  * ------------
 21 |  * The mathematical constant pi, which is the ratio of the circumference of
 22 |  * a circle to its diameter.
 23 |  */
 24 | 
 25 | #define PI 3.14159265358979323846
 26 | 
 27 | /*
 28 |  * Constant: E
 29 |  * -----------
 30 |  * The mathematical constant e, which is the base of natural logarithms.
 31 |  */
 32 | 
 33 | #define E 2.71828182845904523536
 34 | 
 35 | /*
 36 |  * Function: sinDegrees
 37 |  * Usage: sine = sinDegrees(angle);
 38 |  * --------------------------------
 39 |  * Returns the trigonometric sine of angle, which is expressed in degrees.
 40 |  */
 41 | 
 42 | double sinDegrees(double angle);
 43 | 
 44 | /*
 45 |  * Function: cosDegrees
 46 |  * Usage: cosine = cosDegrees(angle);
 47 |  * ----------------------------------
 48 |  * Returns the trigonometric cosine of angle, which is expressed in
 49 |  * degrees.
 50 |  */
 51 | 
 52 | double cosDegrees(double angle);
 53 | 
 54 | /*
 55 |  * Function: tanDegrees
 56 |  * Usage: tangent = tanDegrees(angle);
 57 |  * -----------------------------------
 58 |  * Returns the trigonometric tangent of angle, which is expressed in
 59 |  * degrees.
 60 |  */
 61 | 
 62 | double tanDegrees(double angle);
 63 | 
 64 | /*
 65 |  * Function: toDegrees
 66 |  * Usage: degrees = toDegrees(radians);
 67 |  * ------------------------------------
 68 |  * Converts an angle from radians to degrees.
 69 |  */
 70 | 
 71 | double toDegrees(double radians);
 72 | 
 73 | /*
 74 |  * Function: toRadians
 75 |  * Usage: radians = toRadians(degrees);
 76 |  * ------------------------------------
 77 |  * Converts an angle from degrees to radians.
 78 |  */
 79 | 
 80 | double toRadians(double degrees);
 81 | 
 82 | /*
 83 |  * Function: vectorDistance
 84 |  * Usage: r = vectorDistance(x, y);
 85 |  * --------------------------------
 86 |  * Computes the distance between the origin and the specified point.
 87 |  */
 88 | 
 89 | double vectorDistance(double x, double y);
 90 | 
 91 | /*
 92 |  * Function: vectorAngle
 93 |  * Usage: angle = vectorAngle(x, y);
 94 |  * ---------------------------------
 95 |  * Returns the angle in degrees from the origin to the specified point. 
 96 |  * This function takes account of the fact that the graphics coordinate
 97 |  * system is flipped in the y direction from the traditional Cartesian
 98 |  * plane.
 99 |  */
100 | 
101 | double vectorAngle(double x, double y);
102 | 
103 | #endif
104 | 
105 | 106 | 107 | -------------------------------------------------------------------------------- /docs/gmath.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | gmath.h 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 | 14 |
15 |

gmath.h

16 | This interface exports several functions for working with graphical 17 | geometry along with the constants PI and E. 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
Constants
PI The mathematical constant pi, which is the ratio of the circumference of a circle to its diameter.
The mathematical constant e, which is the base of natural logarithms.
Functions
sinDegrees(angle) Returns the trigonometric sine of angle, which is expressed in degrees.
cosDegrees(angle) Returns the trigonometric cosine of angle, which is expressed in degrees.
tanDegrees(angle) Returns the trigonometric tangent of angle, which is expressed in degrees.
toDegrees(radians) Converts an angle from radians to degrees.
toRadians(degrees) Converts an angle from degrees to radians.
vectorDistance(x, y) Computes the distance between the origin and the specified point.
vectorAngle(x, y) Returns the angle in degrees from the origin to the specified point.
31 |

Function detail

32 |
33 | 34 |
 35 | double sinDegrees(double angle);
 36 | 
37 |
38 | Returns the trigonometric sine of angle, which is 39 | expressed in degrees. 40 |

Usage:
41 |

42 |
 43 | sine = sinDegrees(angle);
 44 | 
45 |
46 | 47 |
 48 | double cosDegrees(double angle);
 49 | 
50 |
51 | Returns the trigonometric cosine of angle, which is 52 | expressed in degrees. 53 |

Usage:
54 |

55 |
 56 | cosine = cosDegrees(angle);
 57 | 
58 |
59 | 60 |
 61 | double tanDegrees(double angle);
 62 | 
63 |
64 | Returns the trigonometric tangent of angle, which is 65 | expressed in degrees. 66 |

Usage:
67 |

68 |
 69 | tangent = tanDegrees(angle);
 70 | 
71 |
72 | 73 |
 74 | double toDegrees(double radians);
 75 | 
76 |
77 | Converts an angle from radians to degrees. 78 |

Usage:
79 |

80 |
 81 | degrees = toDegrees(radians);
 82 | 
83 |
84 | 85 |
 86 | double toRadians(double degrees);
 87 | 
88 |
89 | Converts an angle from degrees to radians. 90 |

Usage:
91 |

92 |
 93 | radians = toRadians(degrees);
 94 | 
95 |
96 | 97 |
 98 | double vectorDistance(double x, double y);
 99 | 
100 |
101 | Computes the distance between the origin and the specified point. 102 |

Usage:
103 |

104 |
105 | r = vectorDistance(x, y);
106 | 
107 |
108 | 109 |
110 | double vectorAngle(double x, double y);
111 | 
112 |
113 | Returns the angle in degrees from the origin to the specified point. 114 | This function takes account of the fact that the graphics coordinate 115 | system is flipped in the y direction from the traditional 116 | Cartesian plane. 117 |

Usage:
118 |

119 |
120 | angle = vectorAngle(x, y);
121 | 
122 |
123 | 124 | 125 | 126 | -------------------------------------------------------------------------------- /docs/gtimer-h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | ../gtimer.h 4 | 5 | 6 | 7 |
 8 | /*
 9 |  * File: gtimer.h
10 |  * --------------
11 |  * This interface exports a general interval timer.
12 |  */
13 | 
14 | #ifndef _gtimer_h
15 | #define _gtimer_h
16 | 
17 | #include "cslib.h"
18 | 
19 | /*
20 |  * Type: GTimer
21 |  * ------------
22 |  * This type implements a simple interval timer that generates a timer
23 |  * event with a specified frequency.
24 |  */
25 | 
26 | typedef struct GTimerCDT *GTimer;
27 | 
28 | /*
29 |  * Function: newGTimer
30 |  * Usage: timer = newGTimer(milliseconds);
31 |  * ---------------------------------------
32 |  * Creates a timer that generates a timer event each time the specified
33 |  * number of milliseconds has elapsed.  No events are generated until the
34 |  * client calls startTimer.
35 |  */
36 | 
37 | GTimer newGTimer(double milliseconds);
38 | 
39 | /*
40 |  * Function: freeGTimer
41 |  * Usage: freeGTimer(timer);
42 |  * -------------------------
43 |  * Frees the resources associated with the timer.
44 |  */
45 | 
46 | void freeGTimer(GTimer timer);
47 | 
48 | /*
49 |  * Function: startTimer
50 |  * Usage: startTimer(timer);
51 |  * -------------------------
52 |  * Starts the timer.  A timer continues to generate timer events until it
53 |  * is stopped; to achieve the effect of a one-shot timer, the simplest
54 |  * approach is to call the stop function inside the event handler.
55 |  */
56 | 
57 | void startTimer(GTimer timer);
58 | 
59 | /*
60 |  * Function: stopTimer
61 |  * Usage: stopTimer(timer);
62 |  * ------------------------
63 |  * Stops the timer so that it stops generating events.
64 |  */
65 | 
66 | void stopTimer(GTimer timer);
67 | 
68 | #endif
69 | 
70 | 71 | 72 | -------------------------------------------------------------------------------- /docs/gtimer.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | gtimer.h 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 | 14 |
15 |

gtimer.h

16 | This interface exports a general interval timer. 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
Type
GTimer This type implements a simple interval timer that generates a timer event with a specified frequency.
Functions
newGTimer(milliseconds) Creates a timer that generates a timer event each time the specified number of milliseconds has elapsed.
freeGTimer(timer) Frees the resources associated with the timer.
startTimer(timer) Starts the timer.
stopTimer(timer) Stops the timer so that it stops generating events.
26 |

Type detail

27 |
28 | 29 |
30 | typedef struct GTimerCDT *GTimer;
31 | 
32 |
33 | This type implements a simple interval timer that generates a timer 34 | event with a specified frequency. 35 |
36 |
37 | 38 |

Function detail

39 |
40 | 41 |
42 | GTimer newGTimer(double milliseconds);
43 | 
44 |
45 | Creates a timer that generates a timer event each time the specified 46 | number of milliseconds has elapsed. No events are generated until 47 | the client calls startTimer. 48 |

Usage:
49 |

50 |
51 | timer = newGTimer(milliseconds);
52 | 
53 |
54 | 55 |
56 | void freeGTimer(GTimer timer);
57 | 
58 |
59 | Frees the resources associated with the timer. 60 |

Usage:
61 |

62 |
63 | freeGTimer(timer);
64 | 
65 |
66 | 67 |
68 | void startTimer(GTimer timer);
69 | 
70 |
71 | Starts the timer. A timer continues to generate timer events until it 72 | is stopped; to achieve the effect of a one-shot timer, the simplest 73 | approach is to call the stop function inside the event 74 | handler. 75 |

Usage:
76 |

77 |
78 | startTimer(timer);
79 | 
80 |
81 | 82 |
83 | void stopTimer(GTimer timer);
84 | 
85 |
86 | Stops the timer so that it stops generating events. 87 |

Usage:
88 |

89 |
90 | stopTimer(timer);
91 | 
92 |
93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /docs/gtypes-h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | ../gtypes.h 4 | 5 | 6 | 7 |
  8 | /*
  9 |  * File: gtypes.h
 10 |  * --------------
 11 |  * This interface defines types for representing points, dimensions, and
 12 |  * rectangles.
 13 |  */
 14 | 
 15 | #ifndef _gtypes_h
 16 | #define _gtypes_h
 17 | 
 18 | #include "generic.h"
 19 | 
 20 | /*
 21 |  * Type: GPoint
 22 |  * ------------
 23 |  * This type contains real-valued x and y fields.  It is used to represent
 24 |  * a location on the graphics plane.
 25 |  */
 26 | 
 27 | typedef struct {
 28 |    double x;
 29 |    double y;
 30 | } GPoint;
 31 | 
 32 | /*
 33 |  * Type: GDimension
 34 |  * ----------------
 35 |  * This type contains real-valued width and height fields.  It is used to
 36 |  * indicate the size of a graphical object.
 37 |  */
 38 | 
 39 | typedef struct {
 40 |    double width;
 41 |    double height;
 42 | } GDimension;
 43 | 
 44 | /*
 45 |  * Type: GRectangle
 46 |  * ----------------
 47 |  * This type contains real-valued x, y, width, and height fields.  It is
 48 |  * used to represent the bounding box of a graphical object.
 49 |  */
 50 | 
 51 | typedef struct {
 52 |    double x;
 53 |    double y;
 54 |    double width;
 55 |    double height;
 56 | } GRectangle;
 57 | 
 58 | /*
 59 |  * Function: createGPoint
 60 |  * Usage: pt = createGPoint(x, y);
 61 |  * -------------------------------
 62 |  * Creates a GPoint structure with the specified x and y coordinates.
 63 |  */
 64 | 
 65 | GPoint createGPoint(double x, double y);
 66 | 
 67 | /*
 68 |  * Function: getX
 69 |  * Usage: x = getX(pt);
 70 |  * --------------------
 71 |  * Returns the x component of the GPoint.
 72 |  */
 73 | 
 74 | double getXGPoint(GPoint pt);
 75 | 
 76 | /*
 77 |  * Function: getY
 78 |  * Usage: y = getY(pt);
 79 |  * --------------------
 80 |  * Returns the y component of the GPoint.
 81 |  */
 82 | 
 83 | double getYGPoint(GPoint pt);
 84 | 
 85 | /*
 86 |  * Function: createGDimension
 87 |  * Usage: dim = createGDimension(width, height);
 88 |  * ---------------------------------------------
 89 |  * Creates a GDimension value with the specified width and height
 90 |  * coordinates.
 91 |  */
 92 | 
 93 | GDimension createGDimension(double width, double height);
 94 | 
 95 | /*
 96 |  * Function: getWidth
 97 |  * Usage: width = getWidth(dim);
 98 |  * -----------------------------
 99 |  * Returns the width component of the GDimension.
100 |  */
101 | 
102 | double getWidthGDimension(GDimension dim);
103 | 
104 | /*
105 |  * Function: getHeight
106 |  * Usage: width = getHeight(dim);
107 |  * ------------------------------
108 |  * Returns the width component of the GDimension.
109 |  */
110 | 
111 | double getHeightGDimension(GDimension dim);
112 | 
113 | /*
114 |  * Function: createGRectangle
115 |  * Usage: r = createGRectangle(x, y, width, height);
116 |  * -------------------------------------------------
117 |  * Creates a GRectangle value with the specified components.
118 |  */
119 | 
120 | GRectangle createGRectangle(double x, double y, double width, double height);
121 | 
122 | /*
123 |  * Function: getX
124 |  * Usage: double x = getX(r);
125 |  * --------------------------
126 |  * Returns the x component of the rectangle.
127 |  */
128 | 
129 | double getXGRectangle(GRectangle r);
130 | 
131 | /*
132 |  * Function: getY
133 |  * Usage: double y = getY(r);
134 |  * --------------------------
135 |  * Returns the y component of the rectangle.
136 |  */
137 | 
138 | double getYGRectangle(GRectangle r);
139 | 
140 | /*
141 |  * Function: getWidth
142 |  * Usage: width = getWidth(r);
143 |  * ---------------------------
144 |  * Returns the width component of the GRectangle.
145 |  */
146 | 
147 | double getWidthGRectangle(GRectangle r);
148 | 
149 | /*
150 |  * Function: getHeight
151 |  * Usage: width = getHeight(r);
152 |  * ----------------------------
153 |  * Returns the width component of the GRectangle.
154 |  */
155 | 
156 | double getHeightGRectangle(GRectangle r);
157 | 
158 | /*
159 |  * Function: isEmpty
160 |  * Usage: if (isEmpty(r)) . . .
161 |  * ----------------------------
162 |  * Returns true if the rectangle is empty.
163 |  */
164 | 
165 | bool isEmptyGRectangle(GRectangle r);
166 | 
167 | /*
168 |  * Function: contains
169 |  * Usage: if (contains(r, pt)) . . .
170 |  * ---------------------------------
171 |  * Returns true if the rectangle contains the given point.
172 |  */
173 | 
174 | bool containsGRectangle(GRectangle r, GPoint pt);
175 | 
176 | #endif
177 | 
178 | 179 | 180 | -------------------------------------------------------------------------------- /docs/hashmap-h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | ../hashmap.h 4 | 5 | 6 | 7 |
  8 | /*
  9 |  * File: hashmap.h
 10 |  * ---------------
 11 |  * This interface defines a map abstraction that associates string keys
 12 |  * with values.  The implementation uses a hash table, which offers
 13 |  * constant-time performance but does not support iterating through the
 14 |  * keys in order.
 15 |  */
 16 | 
 17 | #ifndef _hashmap_h
 18 | #define _hashmap_h
 19 | 
 20 | #include "cslib.h"
 21 | #include "generic.h"
 22 | #include "iterator.h"
 23 | 
 24 | /*
 25 |  * Function: HashMap
 26 |  * -----------------
 27 |  * This type is the ADT used to represent a map from strings to values.
 28 |  */
 29 | 
 30 | typedef struct HashMapCDT *HashMap;
 31 | 
 32 | /* Exported entries */
 33 | 
 34 | /*
 35 |  * Function: newHashMap
 36 |  * Usage: map = newHashMap();
 37 |  * --------------------------
 38 |  * Allocates a new map with no entries.
 39 |  */
 40 | 
 41 | HashMap newHashMap(void);
 42 | 
 43 | /*
 44 |  * Function: freeHashMap
 45 |  * Usage: freeHashMap(map);
 46 |  * ------------------------
 47 |  * Frees the storage associated with the map.
 48 |  */
 49 | 
 50 | void freeHashMap(HashMap map);
 51 | 
 52 | /*
 53 |  * Function: size
 54 |  * Usage: n = size(map);
 55 |  * ---------------------
 56 |  * Returns the number of elements in the map.
 57 |  */
 58 | 
 59 | int sizeHashMap(HashMap map);
 60 | 
 61 | /*
 62 |  * Function: isEmpty
 63 |  * Usage: if (isEmpty(map)) . . .
 64 |  * ------------------------------
 65 |  * Returns true if the map has no entries.
 66 |  */
 67 | 
 68 | bool isEmptyHashMap(HashMap map);
 69 | 
 70 | /*
 71 |  * Function: clear
 72 |  * Usage: clear(map);
 73 |  * ------------------
 74 |  * Removes all entries from the map.
 75 |  */
 76 | 
 77 | void clearHashMap(HashMap map);
 78 | 
 79 | /*
 80 |  * Function: clone
 81 |  * Usage: newmap = clone(map);
 82 |  * ---------------------------
 83 |  * Creates a copy of the map.  The clone function copies only the first
 84 |  * level of the structure and does not copy the individual elements.
 85 |  */
 86 | 
 87 | HashMap cloneHashMap(HashMap map);
 88 | 
 89 | /*
 90 |  * Function: put
 91 |  * Usage: put(map, key, value);
 92 |  * ----------------------------
 93 |  * Associates key with value in the map.  Each call to put supersedes any
 94 |  * previous definition for key.
 95 |  */
 96 | 
 97 | void putHashMap(HashMap map, string key, void *value);
 98 | 
 99 | /*
100 |  * Function: get
101 |  * Usage: void *value = get(map, key);
102 |  * -----------------------------------
103 |  * Returns the value associated with key in the map, or NULL, if no such
104 |  * value exists.
105 |  */
106 | 
107 | void *getHashMap(HashMap map, string key);
108 | 
109 | /*
110 |  * Function: containsKey
111 |  * Usage: if (containsKey(map, key)) . . .
112 |  * ---------------------------------------
113 |  * Checks to see if the map contains the specified key.
114 |  */
115 | 
116 | bool containsKeyHashMap(HashMap map, string key);
117 | 
118 | /*
119 |  * Function: remove
120 |  * Usage: remove(map, key);
121 |  * ------------------------
122 |  * Removes the key and its value from the map.
123 |  */
124 | 
125 | void removeHashMap(HashMap map, string key);
126 | 
127 | /*
128 |  * Function: map
129 |  * Usage: map(map, fn, data);
130 |  * --------------------------
131 |  * Iterates through the map and calls the function fn on each entry.  The
132 |  * callback function takes the following arguments:
133 |  *
134 |  *
135 |  *  The key string
136 |  *  The associated value
137 |  *  The data pointer
138 |  *
139 |  *
140 |  * The data pointer allows the client to pass state information to the
141 |  * function fn, if necessary.  If no such information is required, this
142 |  * argument should be NULL.
143 |  */
144 | 
145 | void mapHashMap(HashMap map, proc fn, void *data);
146 | 
147 | #endif
148 | 
149 | 150 | 151 | -------------------------------------------------------------------------------- /docs/images/GArcExamples.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs50/spl/24221174d52e601140640f64629cae490b7c295d/docs/images/GArcExamples.png -------------------------------------------------------------------------------- /docs/images/GArcGeometry.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs50/spl/24221174d52e601140640f64629cae490b7c295d/docs/images/GArcGeometry.png -------------------------------------------------------------------------------- /docs/images/GCheckBox.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs50/spl/24221174d52e601140640f64629cae490b7c295d/docs/images/GCheckBox.png -------------------------------------------------------------------------------- /docs/images/GChooser.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs50/spl/24221174d52e601140640f64629cae490b7c295d/docs/images/GChooser.png -------------------------------------------------------------------------------- /docs/images/GEventHierarchy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs50/spl/24221174d52e601140640f64629cae490b7c295d/docs/images/GEventHierarchy.png -------------------------------------------------------------------------------- /docs/images/GInteractorHierarchy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs50/spl/24221174d52e601140640f64629cae490b7c295d/docs/images/GInteractorHierarchy.png -------------------------------------------------------------------------------- /docs/images/GLabelGeometry.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs50/spl/24221174d52e601140640f64629cae490b7c295d/docs/images/GLabelGeometry.png -------------------------------------------------------------------------------- /docs/images/GObjectHierarchy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs50/spl/24221174d52e601140640f64629cae490b7c295d/docs/images/GObjectHierarchy.png -------------------------------------------------------------------------------- /docs/images/GPolygonExamples.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs50/spl/24221174d52e601140640f64629cae490b7c295d/docs/images/GPolygonExamples.png -------------------------------------------------------------------------------- /docs/images/GSlider.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs50/spl/24221174d52e601140640f64629cae490b7c295d/docs/images/GSlider.png -------------------------------------------------------------------------------- /docs/images/HelloWorld.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs50/spl/24221174d52e601140640f64629cae490b7c295d/docs/images/HelloWorld.png -------------------------------------------------------------------------------- /docs/images/PacMan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs50/spl/24221174d52e601140640f64629cae490b7c295d/docs/images/PacMan.png -------------------------------------------------------------------------------- /docs/images/StanfordTreeLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs50/spl/24221174d52e601140640f64629cae490b7c295d/docs/images/StanfordTreeLogo.png -------------------------------------------------------------------------------- /docs/images/StopSign.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs50/spl/24221174d52e601140640f64629cae490b7c295d/docs/images/StopSign.png -------------------------------------------------------------------------------- /docs/include/package.html: -------------------------------------------------------------------------------- 1 | The cslib package defines a set of interface files that 2 | simplify writing C programs, particularly at the novice level. 3 | -------------------------------------------------------------------------------- /docs/loadobj-h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | ../loadobj.h 4 | 5 | 6 | 7 |
 8 | /*
 9 |  * File: loadobj.h
10 |  * ---------------
11 |  * This interface supports dynamic loading of functions from object files. 
12 |  * Any references in the object file to previously defined objects are
13 |  * allowed, but no additional searching or unresolved references are
14 |  * supported.
15 |  */
16 | 
17 | #ifndef _loadobj_h
18 | #define _loadobj_h
19 | 
20 | #include "cslib.h"
21 | 
22 | /*
23 |  * Function: loadObject
24 |  * Usage: loadObject(pathname);
25 |  * ----------------------------
26 |  * Loads the object file into the current executable.
27 |  */
28 | 
29 | void loadObject(string pathname);
30 | 
31 | /*
32 |  * Function: loadSymbols
33 |  * Usage: loadSymbols(progname);
34 |  * -----------------------------
35 |  * Loads the symbols from the executable program.
36 |  */
37 | 
38 | void loadSymbols(string progname);
39 | 
40 | /*
41 |  * Function: findFunction
42 |  * Usage: fn = findFunction(fnname);
43 |  * ---------------------------------
44 |  * Looks up the function in the symbol table and returns a pointer to the
45 |  * code.
46 |  */
47 | 
48 | proc findFunction(string fnname);
49 | 
50 | #endif
51 | 
52 | 53 | 54 | -------------------------------------------------------------------------------- /docs/loadobj.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | loadobj.h 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 | 14 |
15 |

loadobj.h

16 | This interface supports dynamic loading of functions from object files. 17 | Any references in the object file to previously defined objects are 18 | allowed, but no additional searching or unresolved references are 19 | supported. 20 | 21 | 22 | 23 | 24 | 25 |
Functions
loadObject(pathname) Loads the object file into the current executable.
loadSymbols(progname) Loads the symbols from the executable program.
findFunction(fnname) Looks up the function in the symbol table and returns a pointer to the code.
26 |

Function detail

27 |
28 | 29 |
30 | void loadObject(string pathname);
31 | 
32 |
33 | Loads the object file into the current executable. 34 |

Usage:
35 |

36 |
37 | loadObject(pathname);
38 | 
39 |
40 | 41 |
42 | void loadSymbols(string progname);
43 | 
44 |
45 | Loads the symbols from the executable program. 46 |

Usage:
47 |

48 |
49 | loadSymbols(progname);
50 | 
51 |
52 | 53 |
54 | proc findFunction(string fnname);
55 | 
56 |
57 | Looks up the function in the symbol table and returns a pointer to the 58 | code. 59 |

Usage:
60 |

61 |
62 | fn = findFunction(fnname);
63 | 
64 |
65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /docs/map-h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | ../map.h 4 | 5 | 6 | 7 |
  8 | /*
  9 |  * File: map.h
 10 |  * -----------
 11 |  * This interface defines a map abstraction that associates string-valued
 12 |  * keys with values.  In contrast to the HashMap type defined in the
 13 |  * hashmap.h interface, the implementation of the Map type uses a balanced
 14 |  * binary tree, which offers logarithmic performance and sorted iteration.
 15 |  *
 16 |  * In most applications, the restriction of keys to strings is easy to
 17 |  * circumvent.  The simplest strategy is to convert key values to strings
 18 |  * before inserting them into the map.  A more general strategy is to use
 19 |  * the bst.h interface instead.
 20 |  */
 21 | 
 22 | #ifndef _map_h
 23 | #define _map_h
 24 | 
 25 | #include "cslib.h"
 26 | #include "generic.h"
 27 | #include "iterator.h"
 28 | 
 29 | /*
 30 |  * Type: Map
 31 |  * ---------
 32 |  * This type is the ADT used to represent the map.
 33 |  */
 34 | 
 35 | typedef struct MapCDT *Map;
 36 | 
 37 | /* Exported entries */
 38 | 
 39 | /*
 40 |  * Function: newMap
 41 |  * Usage: map = newMap();
 42 |  * ----------------------
 43 |  * Allocates a new map with no entries.
 44 |  */
 45 | 
 46 | Map newMap();
 47 | 
 48 | /*
 49 |  * Function: freeMap
 50 |  * Usage: freeMap(map);
 51 |  * --------------------
 52 |  * Frees the storage associated with the map.
 53 |  */
 54 | 
 55 | void freeMap(Map map);
 56 | 
 57 | /*
 58 |  * Function: size
 59 |  * Usage: n = size(map);
 60 |  * ---------------------
 61 |  * Returns the number of elements in the map.
 62 |  */
 63 | 
 64 | int sizeMap(Map map);
 65 | 
 66 | /*
 67 |  * Function: isEmpty
 68 |  * Usage: if (isEmpty(map)) . . .
 69 |  * ------------------------------
 70 |  * Returns true if the map has no entries.
 71 |  */
 72 | 
 73 | bool isEmptyMap(Map map);
 74 | 
 75 | /*
 76 |  * Function: clear
 77 |  * Usage: clear(map);
 78 |  * ------------------
 79 |  * Removes all entries from the map.
 80 |  */
 81 | 
 82 | void clearMap(Map map);
 83 | 
 84 | /*
 85 |  * Function: clone
 86 |  * Usage: newmap = clone(map);
 87 |  * ---------------------------
 88 |  * Creates a copy of the map.  The clone function copies only the first
 89 |  * level of the structure and does not copy the individual elements.
 90 |  */
 91 | 
 92 | Map cloneMap(Map map);
 93 | 
 94 | /*
 95 |  * Function: put
 96 |  * Usage: put(map, key, value);
 97 |  * ----------------------------
 98 |  * Associates key with value in the map.  Each call to put supersedes any
 99 |  * previous definition for key.
100 |  */
101 | 
102 | void putMap(Map map, string key, void *value);
103 | 
104 | /*
105 |  * Function: get
106 |  * Usage: void *value = get(map, key);
107 |  * -----------------------------------
108 |  * Returns the value associated with key in the map, or NULL, if no such
109 |  * value exists.
110 |  */
111 | 
112 | void *getMap(Map map, string key);
113 | 
114 | /*
115 |  * Function: containsKey
116 |  * Usage: if (containsKey(map, key)) . . .
117 |  * ---------------------------------------
118 |  * Checks to see if the map contains the specified key.
119 |  */
120 | 
121 | bool containsKeyMap(Map map, string key);
122 | 
123 | /*
124 |  * Function: remove
125 |  * Usage: remove(map, key);
126 |  * ------------------------
127 |  * Removes the key and its value from the map.
128 |  */
129 | 
130 | void removeMap(Map map, string key);
131 | 
132 | /*
133 |  * Function: map
134 |  * Usage: map(map, fn, data);
135 |  * --------------------------
136 |  * Iterates through the map and calls the function fn on each entry.  The
137 |  * callback function takes the following arguments:
138 |  *
139 |  *
140 |  *  The key
141 |  *  The associated value
142 |  *  The data pointer
143 |  *
144 |  *
145 |  * The data pointer allows the client to pass state information to the
146 |  * function fn, if necessary.  If no such information is required, this
147 |  * argument should be NULL.
148 |  */
149 | 
150 | void mapMap(Map map, proc fn, void *data);
151 | 
152 | #endif
153 | 
154 | 155 | 156 | -------------------------------------------------------------------------------- /docs/pqueue-h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | ../pqueue.h 4 | 5 | 6 | 7 |
  8 | /*
  9 |  * File: pqueue.h
 10 |  * --------------
 11 |  * This interface defines a queue abstraction that dequeues elements in
 12 |  * priority order.  In keeping with traditional English usage, lower
 13 |  * priority numbers have higher priority, so that priority 1 items appear
 14 |  * before priority 2 items.
 15 |  */
 16 | 
 17 | #ifndef _pqueue_h
 18 | #define _pqueue_h
 19 | 
 20 | #include "cslib.h"
 21 | #include "generic.h"
 22 | 
 23 | /*
 24 |  * Type: PriorityQueue
 25 |  * -------------------
 26 |  * This type defines the abstract type for a queue.
 27 |  */
 28 | 
 29 | typedef struct PriorityQueueCDT *PriorityQueue;
 30 | 
 31 | /*
 32 |  * Function: newPriorityQueue
 33 |  * Usage: pq = newPriorityQueue();
 34 |  * -------------------------------
 35 |  * Allocates and returns an empty priority queue.
 36 |  */
 37 | 
 38 | PriorityQueue newPriorityQueue(void);
 39 | 
 40 | /*
 41 |  * Function: freePriorityQueue
 42 |  * Usage: freePriorityQueue(pq);
 43 |  * -----------------------------
 44 |  * Frees the storage associated with the specified priority queue.
 45 |  */
 46 | 
 47 | void freePriorityQueue(PriorityQueue pq);
 48 | 
 49 | /*
 50 |  * Function: enqueue
 51 |  * Usage: enqueue(pq, value, priority);
 52 |  * ------------------------------------
 53 |  * Adds a value to the queue in the order specified by priority.  In C, the
 54 |  * priority argument must have type double, which means that constants used
 55 |  * to express priorities in the call to the generic enqueue function must
 56 |  * include a decimal point.
 57 |  */
 58 | 
 59 | void enqueuePriorityQueue(PriorityQueue pq, void *value, double priority);
 60 | 
 61 | /*
 62 |  * Function: dequeue
 63 |  * Usage: value = dequeue(pq);
 64 |  * ---------------------------
 65 |  * Removes the data value at the head of the queue and returns it to the
 66 |  * client.  If the queue is empty, dequeue calls error with an appropriate
 67 |  * message.
 68 |  */
 69 | 
 70 | void *dequeuePriorityQueue(PriorityQueue pq);
 71 | 
 72 | /*
 73 |  * Function: peek
 74 |  * Usage: value = peek(pq);
 75 |  * ------------------------
 76 |  * Returns the data value at the head of the queue without removing it.  If
 77 |  * the queue is empty, peek calls error with an appropriate message.
 78 |  */
 79 | 
 80 | void *peekPriorityQueue(PriorityQueue pq);
 81 | 
 82 | /*
 83 |  * Function: peekPriority
 84 |  * Usage: priority = peekPriority(pq);
 85 |  * -----------------------------------
 86 |  * Returns the priority of the first entry in the queue, without removing
 87 |  * it.
 88 |  */
 89 | 
 90 | double peekPriority(PriorityQueue pq);
 91 | 
 92 | /*
 93 |  * Function: isEmpty
 94 |  * Usage: if (isEmpty(pq)) . . .
 95 |  * -----------------------------
 96 |  * Tests whether the queue is empty.
 97 |  */
 98 | 
 99 | bool isEmptyPriorityQueue(PriorityQueue pq);
100 | 
101 | /*
102 |  * Function: size
103 |  * Usage: n = size(pq);
104 |  * --------------------
105 |  * Returns the number of values in the queue.
106 |  */
107 | 
108 | int sizePriorityQueue(PriorityQueue pq);
109 | 
110 | /*
111 |  * Function: clear
112 |  * Usage: clear(pq);
113 |  * -----------------
114 |  * Removes all values from the queue.
115 |  */
116 | 
117 | void clearPriorityQueue(PriorityQueue pq);
118 | 
119 | /*
120 |  * Function: clone
121 |  * Usage: newpq = clone(pq);
122 |  * -------------------------
123 |  * Creates a copy of the priority queue.  The clone function copies only
124 |  * the first level of the structure and does not copy the individual
125 |  * elements.
126 |  */
127 | 
128 | PriorityQueue clonePriorityQueue(PriorityQueue pq);
129 | 
130 | #endif
131 | 
132 | 133 | 134 | -------------------------------------------------------------------------------- /docs/queue-h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | ../queue.h 4 | 5 | 6 | 7 |
  8 | /*
  9 |  * File: queue.h
 10 |  * -------------
 11 |  * This interface defines a queue abstraction with first-in/first-out
 12 |  * semantics.
 13 |  */
 14 | 
 15 | #ifndef _queue_h
 16 | #define _queue_h
 17 | 
 18 | #include "cslib.h"
 19 | #include "generic.h"
 20 | 
 21 | /*
 22 |  * Type: Queue
 23 |  * -----------
 24 |  * This type defines the abstract type for a queue.
 25 |  */
 26 | 
 27 | typedef struct QueueCDT *Queue;
 28 | 
 29 | /*
 30 |  * Function: newQueue
 31 |  * Usage: queue = newQueue();
 32 |  * --------------------------
 33 |  * Allocates and returns an empty queue.
 34 |  */
 35 | 
 36 | Queue newQueue(void);
 37 | 
 38 | /*
 39 |  * Function: freeQueue
 40 |  * Usage: freeQueue(queue);
 41 |  * ------------------------
 42 |  * Frees the storage associated with the specified queue.
 43 |  */
 44 | 
 45 | void freeQueue(Queue queue);
 46 | 
 47 | /*
 48 |  * Function: enqueue
 49 |  * Usage: enqueue(queue, element);
 50 |  * -------------------------------
 51 |  * Adds an element to the end of the queue.
 52 |  */
 53 | 
 54 | void enqueueQueue(Queue queue, void *element);
 55 | 
 56 | /*
 57 |  * Function: dequeue
 58 |  * Usage: element = dequeue(queue);
 59 |  * --------------------------------
 60 |  * Removes the data value at the head of the queue and returns it to the
 61 |  * client.  If the queue is empty, dequeue calls error with an appropriate
 62 |  * message.
 63 |  */
 64 | 
 65 | void *dequeueQueue(Queue queue);
 66 | 
 67 | /*
 68 |  * Function: peek
 69 |  * Usage: element = peek(queue);
 70 |  * -----------------------------
 71 |  * Returns the data value at the head of the queue without removing it.  If
 72 |  * the queue is empty, peek calls error with an appropriate message.
 73 |  */
 74 | 
 75 | void *peekQueue(Queue queue);
 76 | 
 77 | /*
 78 |  * Function: isEmpty
 79 |  * Usage: if (isEmpty(queue)) . . .
 80 |  * --------------------------------
 81 |  * Tests whether the queue is empty.
 82 |  */
 83 | 
 84 | bool isEmptyQueue(Queue queue);
 85 | 
 86 | /*
 87 |  * Function: size
 88 |  * Usage: n = size(queue);
 89 |  * -----------------------
 90 |  * Returns the number of elements in the queue.
 91 |  */
 92 | 
 93 | int sizeQueue(Queue queue);
 94 | 
 95 | /*
 96 |  * Function: clear
 97 |  * Usage: clear(queue);
 98 |  * --------------------
 99 |  * Removes all elements from the queue.
100 |  */
101 | 
102 | void clearQueue(Queue queue);
103 | 
104 | /*
105 |  * Function: clone
106 |  * Usage: newqueue = clone(queue);
107 |  * -------------------------------
108 |  * Creates a copy of the queue.  The clone function copies only the first
109 |  * level of the structure and does not copy the individual elements.
110 |  */
111 | 
112 | Queue cloneQueue(Queue queue);
113 | 
114 | #endif
115 | 
116 | 117 | 118 | -------------------------------------------------------------------------------- /docs/random-h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | ../random.h 4 | 5 | 6 | 7 |
 8 | /*
 9 |  * File: random.h
10 |  * --------------
11 |  * This interface exports functions for generating pseudorandom numbers.
12 |  */
13 | 
14 | #ifndef _random_h
15 | #define _random_h
16 | 
17 | #include "cslib.h"
18 | #include <stdlib.h>
19 | 
20 | /*
21 |  * Function: randomInteger
22 |  * Usage: n = randomInteger(low, high);
23 |  * ------------------------------------
24 |  * Returns a random integer in the range low to high, inclusive.
25 |  */
26 | 
27 | int randomInteger(int low, int high);
28 | 
29 | /*
30 |  * Function: randomReal
31 |  * Usage: d = randomReal(low, high);
32 |  * ---------------------------------
33 |  * Returns a random real number in the half-open interval [low .. high).  A
34 |  * half-open interval includes the first endpoint but not the second, which
35 |  * means that the result is always greater than or equal to low but
36 |  * strictly less than high.
37 |  */
38 | 
39 | double randomReal(double low, double high);
40 | 
41 | /*
42 |  * Function: randomChance
43 |  * Usage: if (randomChance(p)) . . .
44 |  * ---------------------------------
45 |  * Returns true with the probability indicated by p.  The argument p must
46 |  * be a floating-point number between 0 (never) and 1 (always).  For
47 |  * example, calling randomChance(.30) returns true 30 percent of the time.
48 |  */
49 | 
50 | bool randomChance(double p);
51 | 
52 | /*
53 |  * Function: setRandomSeed
54 |  * Usage: setRandomSeed(seed);
55 |  * ---------------------------
56 |  * Sets the internal random number seed to the specified value.  You can
57 |  * use this function to set a specific starting point for the pseudorandom
58 |  * sequence or to ensure that program behavior is repeatable during the
59 |  * debugging phase.
60 |  */
61 | 
62 | void setRandomSeed(int seed);
63 | 
64 | #endif
65 | 
66 | 67 | 68 | -------------------------------------------------------------------------------- /docs/random.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | random.h 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 | 14 |
15 |

random.h

16 | This interface exports functions for generating pseudorandom numbers. 17 | 18 | 19 | 20 | 21 | 22 | 23 |
Functions
randomInteger(low, high) Returns a random integer in the range low to high, inclusive.
randomReal(low, high) Returns a random real number in the half-open interval [low .. high).
randomChance(p) Returns true with the probability indicated by p.
setRandomSeed(seed) Sets the internal random number seed to the specified value.
24 |

Function detail

25 |
26 | 27 |
28 | int randomInteger(int low, int high);
29 | 
30 |
31 | Returns a random integer in the range low to 32 | high, inclusive. 33 |

Usage:
34 |

35 |
36 | n = randomInteger(low, high);
37 | 
38 |
39 | 40 |
41 | double randomReal(double low, double high);
42 | 
43 |
44 | Returns a random real number in the half-open interval 45 | [low .. high). A half-open 46 | interval includes the first endpoint but not the second, which 47 | means that the result is always greater than or equal to 48 | low but strictly less than high. 49 |

Usage:
50 |

51 |
52 | d = randomReal(low, high);
53 | 
54 |
55 | 56 |
57 | bool randomChance(double p);
58 | 
59 |
60 | Returns true with the probability indicated by p. 61 | The argument p must be a floating-point number between 62 | 0 (never) and 1 (always). For example, calling 63 | randomChance(.30) returns true 30 percent 64 | of the time. 65 |

Usage:
66 |

67 |
68 | if (randomChance(p)) . . .
69 | 
70 |
71 | 72 |
73 | void setRandomSeed(int seed);
74 | 
75 |
76 | Sets the internal random number seed to the specified value. You 77 | can use this function to set a specific starting point for the 78 | pseudorandom sequence or to ensure that program behavior is 79 | repeatable during the debugging phase. 80 |

Usage:
81 |

82 |
83 | setRandomSeed(seed);
84 | 
85 |
86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /docs/simpio-h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | ../simpio.h 4 | 5 | 6 | 7 |
  8 | /*
  9 |  * File: simpio.h
 10 |  * --------------
 11 |  * This interface exports several functions that simplify the reading of
 12 |  * input data.
 13 |  */
 14 | 
 15 | #ifndef _simpio_h
 16 | #define _simpio_h
 17 | 
 18 | #include "cslib.h"
 19 | 
 20 | /*
 21 |  * Function: getInteger
 22 |  * Usage: n = getInteger();
 23 |  * ------------------------
 24 |  * Reads a line of text from standard input and scans it as an integer.  If
 25 |  * an integer cannot be scanned or if extraneous characters follow the
 26 |  * number, the user is given a chance to retry.
 27 |  */
 28 | 
 29 | int getInteger(void);
 30 | 
 31 | /*
 32 |  * Function: getLong
 33 |  * Usage: l = getLong();
 34 |  * ---------------------
 35 |  * Reads a line of text from standard input and scans it as a long.  If an
 36 |  * integer cannot be scanned or if extraneous characters follow the number,
 37 |  * the user is given a chance to retry.
 38 |  */
 39 | 
 40 | long getLong(void);
 41 | 
 42 | /*
 43 |  * Function: getReal
 44 |  * Usage: d = getReal();
 45 |  * ---------------------
 46 |  * Reads a line of text from standard input and scans it as a double.  If
 47 |  * an number cannot be scanned or if extraneous characters follow the
 48 |  * number, the user is given a chance to retry.
 49 |  */
 50 | 
 51 | double getReal(void);
 52 | 
 53 | /*
 54 |  * Function: getLine
 55 |  * Usage: s = getLine();
 56 |  * ---------------------
 57 |  * Reads a line of text from standard input and returns the line as a
 58 |  * string.  The newline character that terminates the input is not stored
 59 |  * as part of the result.
 60 |  */
 61 | 
 62 | string getLine(void);
 63 | 
 64 | /*
 65 |  * Function: readLine
 66 |  * Usage: s = readLine(infile);
 67 |  * ----------------------------
 68 |  * Reads a line of text from the input file and returns the line as a
 69 |  * string.  The newline character that terminates the input is not stored
 70 |  * as part of the result.  The readLine function returns NULL if infile is
 71 |  * at the end-of-file position.
 72 |  */
 73 | 
 74 | string readLine(FILE *infile);
 75 | 
 76 | /*
 77 |  * Function: readLinesFromStream
 78 |  * Usage: string *array = readLinesFromStream(infile);
 79 |  * ---------------------------------------------------
 80 |  * Reads an entire file into a NULL-terminated array of lines.  Opening and
 81 |  * closing the file stream is the responsibility of the caller.
 82 |  */
 83 | 
 84 | string *readLinesFromStream(FILE *infile);
 85 | 
 86 | /*
 87 |  * Function: readLinesFromFile
 88 |  * Usage: string *array = readLinesFromFile(filename);
 89 |  * ---------------------------------------------------
 90 |  * Reads an entire file into a NULL-terminated array of lines.  This
 91 |  * version opens the file, reads it, and closes it at the end.  If the file
 92 |  * name is "-", the function reads from stdin.
 93 |  */
 94 | 
 95 | string *readLinesFromFile(string filename);
 96 | 
 97 | #endif
 98 | 
99 | 100 | 101 | -------------------------------------------------------------------------------- /docs/sound-h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | ../sound.h 4 | 5 | 6 | 7 |
 8 | /*
 9 |  * File: sound.h
10 |  * -------------
11 |  * This interface defines an abstract type that represents a sound.
12 |  */
13 | 
14 | #ifndef _sound_h
15 | #define _sound_h
16 | 
17 | /*
18 |  * Type: Sound
19 |  * -----------
20 |  * This type encapsulates a sound file.  The sound file is specified in the
21 |  * constructor and must be a file in either the current directory or a
22 |  * subdirectory named sounds.
23 |  *
24 |  * The following code, for example, plays the sound file ringtone.wav:
25 |  *
26 |  *    Sound ringtone = newSound("ringtone.wav");
27 |  *    play(ringtone);
28 |  */
29 | 
30 | typedef struct SoundCDT *Sound;
31 | 
32 | /*
33 |  * Function: newSound
34 |  * Usage: sound = newSound(filename);
35 |  * ----------------------------------
36 |  * Creates a Sound object from the specified file.
37 |  */
38 | 
39 | Sound newSound(string filename);
40 | 
41 | /*
42 |  * Function: freeSound
43 |  * Usage: freeSound(sound);
44 |  * ------------------------
45 |  * Frees the memory associated with the sound.
46 |  */
47 | 
48 | void freeSound(Sound sound);
49 | 
50 | /*
51 |  * Function: play
52 |  * Usage: play(sound);
53 |  * -------------------
54 |  * Starts playing the sound.  This call returns immediately without waiting
55 |  * for the sound to finish.
56 |  */
57 | 
58 | void play(Sound sound);
59 | 
60 | #endif
61 | 
62 | 63 | 64 | -------------------------------------------------------------------------------- /docs/sound.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | sound.h 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 | 14 |
15 |

sound.h

16 | This interface defines an abstract type that represents a sound. 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 |
Type
Sound This type encapsulates a sound file.
Functions
newSound(filename) Creates a Sound object from the specified file.
freeSound(sound) Frees the memory associated with the sound.
play(sound) Starts playing the sound.
25 |

Type detail

26 |
27 | 28 |
29 | typedef struct SoundCDT *Sound;
30 | 
31 |
32 | This type encapsulates a sound file. The sound file is specified in the 33 | constructor and must be a file in either the current directory or a 34 | subdirectory named sounds. 35 | 36 |

The following code, for example, plays the sound file 37 | ringtone.wav: 38 | 39 |

40 |    Sound ringtone = newSound("ringtone.wav");
41 |    play(ringtone);
42 | 
43 |
44 |
45 | 46 |

Function detail

47 |
48 | 49 |
50 | Sound newSound(string filename);
51 | 
52 |
53 | Creates a Sound object from the specified file. 54 |

Usage:
55 |

56 |
57 | sound = newSound(filename);
58 | 
59 |
60 | 61 |
62 | void freeSound(Sound sound);
63 | 
64 |
65 | Frees the memory associated with the sound. 66 |

Usage:
67 |

68 |
69 | freeSound(sound);
70 | 
71 |
72 | 73 |
74 | void play(Sound sound);
75 | 
76 |
77 | Starts playing the sound. This call returns immediately without waiting 78 | for the sound to finish. 79 |

Usage:
80 |

81 |
82 | play(sound);
83 | 
84 |
85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /docs/stack-h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | ../stack.h 4 | 5 | 6 | 7 |
  8 | /*
  9 |  * File: stack.h
 10 |  * -------------
 11 |  * This interface exports an abstraction for stacks.
 12 |  */
 13 | 
 14 | #ifndef _stack_h
 15 | #define _stack_h
 16 | 
 17 | #include "cslib.h"
 18 | #include "generic.h"
 19 | 
 20 | /*
 21 |  * Type: Stack
 22 |  * -----------
 23 |  * The abstract data type for the stack.
 24 |  */
 25 | 
 26 | typedef struct StackCDT *Stack;
 27 | 
 28 | /*
 29 |  * Function: newStack
 30 |  * Usage: stack = newStack();
 31 |  * --------------------------
 32 |  * Allocates and returns a new stack, which is initially empty.
 33 |  */
 34 | 
 35 | Stack newStack(void);
 36 | 
 37 | /*
 38 |  * Function: freeStack
 39 |  * Usage: freeStack(stack);
 40 |  * ------------------------
 41 |  * Frees the storage associated with the stack.
 42 |  */
 43 | 
 44 | void freeStack(Stack stack);
 45 | 
 46 | /*
 47 |  * Function: push
 48 |  * Usage: push(stack, element);
 49 |  * ----------------------------
 50 |  * Pushes the specified element onto the stack.
 51 |  */
 52 | 
 53 | void push(Stack stack, void *element);
 54 | 
 55 | /*
 56 |  * Function: pop
 57 |  * Usage: element = pop(stack);
 58 |  * ----------------------------
 59 |  * Pops the top element from the stack and returns that value.  The first
 60 |  * value popped is always the last one  that was pushed.  If the stack is
 61 |  * empty when pop is called, the implementation calls error with an
 62 |  * appropriate message.
 63 |  */
 64 | 
 65 | void *pop(Stack stack);
 66 | 
 67 | /*
 68 |  * Function: peek
 69 |  * Usage: element = peek(stack);
 70 |  * -----------------------------
 71 |  * Returns the top element from the stack without removing it.  If the
 72 |  * stack is empty when pop is called, the implementation calls error with
 73 |  * an appropriate message.
 74 |  */
 75 | 
 76 | void *peekStack(Stack stack);
 77 | 
 78 | /*
 79 |  * Function: isEmpty
 80 |  * Usage: if (isEmpty(stack)) . . .
 81 |  * --------------------------------
 82 |  * Tests whether the stack is empty.
 83 |  */
 84 | 
 85 | bool isEmptyStack(Stack stack);
 86 | 
 87 | /*
 88 |  * Function: size
 89 |  * Usage: n = size(stack);
 90 |  * -----------------------
 91 |  * Returns the number of elements currently pushed on the stack.
 92 |  */
 93 | 
 94 | int sizeStack(Stack stack);
 95 | 
 96 | /*
 97 |  * Function: clear
 98 |  * Usage: clear(stack);
 99 |  * --------------------
100 |  * Removes all elements from the stack.
101 |  */
102 | 
103 | void clearStack(Stack stack);
104 | 
105 | /*
106 |  * Function: clone
107 |  * Usage: newstack = clone(stack);
108 |  * -------------------------------
109 |  * Creates a copy of the stack.  The clone function copies only the first
110 |  * level of the structure and does not copy the individual elements.
111 |  */
112 | 
113 | Stack cloneStack(Stack stack);
114 | 
115 | #endif
116 | 
117 | 118 | 119 | -------------------------------------------------------------------------------- /docs/strbuf-h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | ../strbuf.h 4 | 5 | 6 | 7 |
  8 | /*
  9 |  * File: strbuf.h
 10 |  * --------------
 11 |  * This interface defines a class that allows strings to grow by
 12 |  * concatentation.
 13 |  */
 14 | 
 15 | #ifndef _strbuf_h
 16 | #define _strbuf_h
 17 | 
 18 | #include "cslib.h"
 19 | 
 20 | /*
 21 |  * Type: StringBuffer
 22 |  * ------------------
 23 |  * This type provides an efficient, memory-safe mechanism for strings that
 24 |  * grow by the addition of characters.
 25 |  */
 26 | 
 27 | typedef struct StringBufferCDT *StringBuffer;
 28 | 
 29 | /*
 30 |  * Function: newStringBuffer
 31 |  * Usage: sb = newStringBuffer();
 32 |  * ------------------------------
 33 |  * Creates a new string buffer that expands dynamically if needed.  The
 34 |  * buffer is initially empty.
 35 |  */
 36 | 
 37 | StringBuffer newStringBuffer();
 38 | 
 39 | /*
 40 |  * Function: freeStringBuffer
 41 |  * Usage: freeStringBuffer(sb);
 42 |  * ----------------------------
 43 |  * Frees the storage associated with the string buffer.
 44 |  */
 45 | 
 46 | void freeStringBuffer(StringBuffer sb);
 47 | 
 48 | /*
 49 |  * Function: pushChar
 50 |  * Usage: pushChar(sb, ch);
 51 |  * ------------------------
 52 |  * Appends (pushes) the character ch onto the end of the string buffer.
 53 |  */
 54 | 
 55 | void pushChar(StringBuffer sb, char ch);
 56 | 
 57 | /*
 58 |  * Function: popChar
 59 |  * Usage: ch = popChar(sb);
 60 |  * ------------------------
 61 |  * Pops and removes the last character from the string buffer.
 62 |  */
 63 | 
 64 | char popChar(StringBuffer sb);
 65 | 
 66 | /*
 67 |  * Function: appendString
 68 |  * Usage: appendString(sb, str);
 69 |  * -----------------------------
 70 |  * Appends the string str to the end of the string buffer.
 71 |  */
 72 | 
 73 | void appendString(StringBuffer sb, string str);
 74 | 
 75 | /*
 76 |  * Function: sbprintf
 77 |  * Usage: sbprintf(sb, format, ...);
 78 |  * ---------------------------------
 79 |  * Expands a printf-style format string and arguments onto the end of the
 80 |  * string buffer.
 81 |  */
 82 | 
 83 | void sbprintf(StringBuffer sb, string format, ...);
 84 | 
 85 | /*
 86 |  * Function: isEmpty
 87 |  * Usage: if (isEmpty(sb)) . . .
 88 |  * -----------------------------
 89 |  * Returns true if the string buffer is empty.
 90 |  */
 91 | 
 92 | bool isEmptyStringBuffer(StringBuffer vec);
 93 | 
 94 | /*
 95 |  * Function: size
 96 |  * Usage: n = size(sb);
 97 |  * --------------------
 98 |  * Returns the number of characters in the string buffer.
 99 |  */
100 | 
101 | int sizeStringBuffer(StringBuffer vector);
102 | 
103 | /*
104 |  * Function: clear
105 |  * Usage: clear(sb);
106 |  * -----------------
107 |  * Removes all characters from the string buffer.
108 |  */
109 | 
110 | void clearStringBuffer(StringBuffer sb);
111 | 
112 | /*
113 |  * Function: getString
114 |  * Usage: str = getString(sb);
115 |  * ---------------------------
116 |  * Returns the string stored inside the buffer.  Clients must copy this
117 |  * string if they need to retain it.
118 |  */
119 | 
120 | string getString(StringBuffer sb);
121 | 
122 | /*
123 |  * Friend function: printfCapacity
124 |  * Usage: capacity = printfCapacity(format, args);
125 |  * -----------------------------------------------
126 |  * Returns a character count that will be sufficient to hold the result of
127 |  * printing format with arguments substituted from the args list.  This
128 |  * bound is guaranteed to be adequate but is not necessarily tight.
129 |  */
130 | 
131 | int printfCapacity(string format, va_list args);
132 | 
133 | /*
134 |  * Friend function: sbFormat
135 |  * Usage: sbFormat(sb, capacity, format, list);
136 |  * --------------------------------------------
137 |  * Works like sbprintf except that the list is an argument list as in
138 |  * stdarg.h.  The capacity argument is the required capacity, as returned
139 |  * by printfCapacity.
140 |  */
141 | 
142 | void sbFormat(StringBuffer sb, int capacity, string format, va_list args);
143 | 
144 | #endif
145 | 
146 | 147 | 148 | -------------------------------------------------------------------------------- /docs/vector-h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | ../vector.h 4 | 5 | 6 | 7 |
  8 | /*
  9 |  * File: vector.h
 10 |  * --------------
 11 |  * This interface exports an array-like indexed collection type.
 12 |  */
 13 | 
 14 | #ifndef _vector_h_
 15 | #define _vector_h_
 16 | 
 17 | #include "cslib.h"
 18 | #include "generic.h"
 19 | 
 20 | /*
 21 |  * Type: Vector
 22 |  * ------------
 23 |  * This type defines the abstract vector type.
 24 |  */
 25 | 
 26 | typedef struct VectorCDT *Vector;
 27 | 
 28 | /* Exported entries */
 29 | 
 30 | /*
 31 |  * Function: newVector
 32 |  * Usage: vector = newVector();
 33 |  * ----------------------------
 34 |  * Returns an empty Vector.
 35 |  */
 36 | 
 37 | Vector newVector(void);
 38 | 
 39 | /*
 40 |  * Function: freeVector
 41 |  * Usage: freeVector(vector);
 42 |  * --------------------------
 43 |  * Frees the storage associated with vector.
 44 |  */
 45 | 
 46 | void freeVector(Vector vector);
 47 | 
 48 | /*
 49 |  * Function: arrayToVector
 50 |  * Usage: vector = arrayToVector(array, n);
 51 |  * ----------------------------------------
 52 |  * Creates a vector from an array of void * pointers.  If the array
 53 |  * argument is NULL, this function returns NULL.
 54 |  */
 55 | 
 56 | Vector arrayToVector(void *array[], int n);
 57 | 
 58 | /*
 59 |  * Function: vectorToArray
 60 |  * Usage: array = vectorToArray(vector);
 61 |  * -------------------------------------
 62 |  * Returns a NULL-terminated array with the same elements as vector.  If
 63 |  * vector is NULL, this function returns NULL.
 64 |  */
 65 | 
 66 | void **vectorToArray(Vector vector);
 67 | 
 68 | /*
 69 |  * Function: isEmpty
 70 |  * Usage: if (isEmpty(vector)) . . .
 71 |  * ---------------------------------
 72 |  * Returns true if the vector is empty.
 73 |  */
 74 | 
 75 | bool isEmptyVector(Vector vector);
 76 | 
 77 | /*
 78 |  * Function: size
 79 |  * Usage: n = size(vector);
 80 |  * ------------------------
 81 |  * Returns the number of elements in the vector.
 82 |  */
 83 | 
 84 | int sizeVector(Vector vector);
 85 | 
 86 | /*
 87 |  * Function: clear
 88 |  * Usage: clear(vector);
 89 |  * ---------------------
 90 |  * Removes all elements from the vector.
 91 |  */
 92 | 
 93 | void clearVector(Vector vector);
 94 | 
 95 | /*
 96 |  * Function: clone
 97 |  * Usage: newvec = clone(vector);
 98 |  * ------------------------------
 99 |  * Creates a copy of the vector.  The clone function copies only the first
100 |  * level of the structure and does not copy the individual elements.
101 |  */
102 | 
103 | Vector cloneVector(Vector vector);
104 | 
105 | /*
106 |  * Function: get
107 |  * Usage: value = get(vector, index);
108 |  * ----------------------------------
109 |  * Gets the element at the specified index position, raising an error if
110 |  * the index is out of range.
111 |  */
112 | 
113 | void *getVector(Vector vector, int index);
114 | 
115 | /*
116 |  * Function: set
117 |  * Usage: set(vector, index, value);
118 |  * ---------------------------------
119 |  * Sets the element at the specified index position, raising an error if
120 |  * the index is out of range.
121 |  */
122 | 
123 | void setVector(Vector vector, int index, void *value);
124 | 
125 | /*
126 |  * Function: add
127 |  * Usage: add(vector, value);
128 |  * --------------------------
129 |  * Adds a new value to the end of the vector.
130 |  */
131 | 
132 | void addVector(Vector vector, void *value);
133 | 
134 | /*
135 |  * Function: insert
136 |  * Usage: insert(vector, index, value);
137 |  * ------------------------------------
138 |  * Inserts a new value before the specified index position.
139 |  */
140 | 
141 | void insert(Vector vector, int index, void *value);
142 | 
143 | /*
144 |  * Function: remove
145 |  * Usage: remove(vector, index);
146 |  * -----------------------------
147 |  * Deletes the element at the specified index position.
148 |  */
149 | 
150 | void removeVector(Vector vector, int index);
151 | 
152 | #endif
153 | 
154 | 155 | 156 | -------------------------------------------------------------------------------- /java/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for the Stanford Portable Library java side 2 | # Last modified on Thu Aug 8 08:53:58 2013 by eroberts 3 | #**************************************************************** 4 | 5 | JAR = spl.jar 6 | CP = acm.jar:. 7 | 8 | CLASSES = \ 9 | stanford/spl/JavaBackEnd.class 10 | 11 | PSFI = public static final int 12 | 13 | # *************************************************************** 14 | # Entry to bring the package up to date 15 | # The "make all" entry should be the first real entry 16 | 17 | all: $(JAR) 18 | 19 | spl.jar: stanford/spl/JavaBackEnd.class 20 | @cp acm.jar spl.jar 21 | @(cd classes; jar ufm ../spl.jar ../JBEManifest.txt \ 22 | `find stanford -name '*.class'`) 23 | @echo jar cf spl.jar . . . 24 | 25 | stanford/spl/JavaBackEnd.class: $(DERIVED) src/stanford/spl/*.java 26 | javac -d classes -classpath $(CP) -sourcepath src \ 27 | src/stanford/spl/JavaBackEnd.java 28 | 29 | 30 | # *************************************************************** 31 | # Standard entries to remove files from the directories 32 | # tidy -- eliminate unwanted files 33 | # scratch -- delete derived files in preparation for rebuild 34 | 35 | tidy: 36 | @rm -f `find . -name ',*' -o -name '.,*' -o -name '*~'` 37 | @rm -f `find . -name '*.tmp' -o -name '*.err'` 38 | @rm -f `find . -name core -o -name a.out` 39 | 40 | scratch clean: tidy $(DERIVED) 41 | @rm -fr *.o *.a classes 42 | @mkdir classes -------------------------------------------------------------------------------- /java/include/JBEManifest.txt: -------------------------------------------------------------------------------- 1 | Main-Class: stanford.spl.JavaBackEnd 2 | -------------------------------------------------------------------------------- /java/lib/acm.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs50/spl/24221174d52e601140640f64629cae490b7c295d/java/lib/acm.jar -------------------------------------------------------------------------------- /java/src/stanford/spl/GButton.java: -------------------------------------------------------------------------------- 1 | /* 2 | * File: GButton.java 3 | * ------------------ 4 | * This file implements the GButton interactor class. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | package stanford.spl; 26 | 27 | import java.awt.event.ActionListener; 28 | 29 | import javax.swing.JButton; 30 | 31 | public class GButton extends GInteractor { 32 | public GButton(String label, ActionListener listener) { 33 | super(new JButton(label)); 34 | JButton button = (JButton) getInteractor(); 35 | if (listener != null) button.addActionListener(listener); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /java/src/stanford/spl/GCheckBox.java: -------------------------------------------------------------------------------- 1 | /* 2 | * File: GCheckBox.java 3 | * -------------------- 4 | * This file implements the GCheckBox interactor class. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | package stanford.spl; 26 | 27 | import java.awt.event.ActionListener; 28 | 29 | import javax.swing.JCheckBox; 30 | 31 | public class GCheckBox extends GInteractor { 32 | public GCheckBox(String label, ActionListener listener) { 33 | super(new JCheckBox(label)); 34 | JCheckBox chkbox = (JCheckBox) getInteractor(); 35 | if (listener != null) chkbox.addActionListener(listener); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /java/src/stanford/spl/GChooser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * File: GChooser.java 3 | * ------------------- 4 | * This file implements the GChooser interactor class. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | package stanford.spl; 26 | 27 | import java.awt.event.ActionListener; 28 | 29 | import javax.swing.JComboBox; 30 | 31 | public class GChooser extends GInteractor { 32 | public GChooser(ActionListener listener) { 33 | super(new JComboBox()); 34 | JComboBox chooser = (JComboBox) getInteractor(); 35 | if (listener != null) { 36 | chooser.setEditable(false); 37 | chooser.addActionListener(listener); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /java/src/stanford/spl/GInteractor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * File: GInteractor.java 3 | * ---------------------- 4 | * This file implements the abstract class GInteractor, 5 | * which forms the root of the interactor hierarchy. 6 | */ 7 | 8 | /*************************************************************************/ 9 | /* Stanford Portable Library */ 10 | /* Copyright (C) 2013 by Eric Roberts */ 11 | /* */ 12 | /* This program is free software: you can redistribute it and/or modify */ 13 | /* it under the terms of the GNU General Public License as published by */ 14 | /* the Free Software Foundation, either version 3 of the License, or */ 15 | /* (at your option) any later version. */ 16 | /* */ 17 | /* This program is distributed in the hope that it will be useful, */ 18 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 19 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 20 | /* GNU General Public License for more details. */ 21 | /* */ 22 | /* You should have received a copy of the GNU General Public License */ 23 | /* along with this program. If not, see . */ 24 | /*************************************************************************/ 25 | 26 | package stanford.spl; 27 | 28 | import acm.graphics.GContainer; 29 | import acm.graphics.GDimension; 30 | import acm.graphics.GMath; 31 | import acm.graphics.GObject; 32 | import acm.graphics.GRectangle; 33 | import acm.graphics.GResizable; 34 | 35 | import java.awt.BorderLayout; 36 | import java.awt.Dimension; 37 | import java.awt.Graphics; 38 | import java.awt.Point; 39 | 40 | import javax.swing.JComponent; 41 | import javax.swing.JLabel; 42 | import javax.swing.JPanel; 43 | 44 | public abstract class GInteractor extends GObject implements GResizable { 45 | public GInteractor(JComponent comp) { 46 | interactor = comp; 47 | actionCommand = ""; 48 | Dimension size = comp.getPreferredSize(); 49 | comp.setSize(size.width, size.height); 50 | } 51 | 52 | public JComponent getInteractor() { 53 | return interactor; 54 | } 55 | 56 | public GRectangle getBounds() { 57 | Point pt = interactor.getLocation(); 58 | Dimension size = interactor.getPreferredSize(); 59 | return new GRectangle(pt.x, pt.y, size.width, size.height); 60 | } 61 | 62 | public void setLocation(double x, double y) { 63 | super.setLocation(x, y); 64 | interactor.setLocation(GMath.round(x), GMath.round(y)); 65 | interactor.repaint(); 66 | } 67 | 68 | public void setVisible(boolean visible) { 69 | super.setVisible(visible); 70 | interactor.setVisible(visible); 71 | interactor.repaint(); 72 | } 73 | 74 | public void setParent(GContainer parent) { 75 | if (parent == null) { 76 | interactor.getParent().remove(interactor); 77 | } else if (parent instanceof TopCompound) { 78 | TopCompound top = (TopCompound) parent; 79 | JBECanvas jc = top.getCanvas(); 80 | if (jc != null) { 81 | jc.add(interactor); 82 | jc.validate(); 83 | } 84 | } 85 | } 86 | 87 | public void setSize(double width, double height) { 88 | int iw = GMath.round(width); 89 | int ih = GMath.round(height); 90 | Dimension d = new Dimension(iw, ih); 91 | interactor.setPreferredSize(d); 92 | interactor.setMinimumSize(d); 93 | interactor.setSize(iw, ih); 94 | interactor.repaint(); 95 | } 96 | 97 | public void setSize(GDimension size) { 98 | setSize(size.getWidth(), size.getHeight()); 99 | } 100 | 101 | public void setBounds(double x, double y, double width, double height) { 102 | setLocation(x, y); 103 | setSize(width, height); 104 | } 105 | 106 | public void setBounds(GRectangle bounds) { 107 | setBounds(bounds.getX(), bounds.getY(), bounds.getWidth(), 108 | bounds.getHeight()); 109 | } 110 | 111 | public void setActionCommand(String cmd) { 112 | actionCommand = cmd; 113 | } 114 | 115 | public String getActionCommand() { 116 | return actionCommand; 117 | } 118 | 119 | public void repaint() { 120 | /* Empty */ 121 | } 122 | 123 | public void paint(Graphics g) { 124 | /* Empty */ 125 | } 126 | 127 | protected void paintObject(Graphics g) { 128 | /* Empty */ 129 | } 130 | 131 | private JComponent interactor; 132 | private String actionCommand; 133 | } 134 | -------------------------------------------------------------------------------- /java/src/stanford/spl/GSlider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * File: GSlider.java 3 | * ------------------ 4 | * This file implements the GSlider interactor class. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | package stanford.spl; 26 | 27 | import javax.swing.JSlider; 28 | import javax.swing.event.ChangeListener; 29 | 30 | public class GSlider extends GInteractor { 31 | public GSlider(int min, int max, int value, ChangeListener listener) { 32 | super(new JSlider(min, max, value)); 33 | JSlider slider = (JSlider) getInteractor(); 34 | if (listener != null) slider.addChangeListener(listener); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /java/src/stanford/spl/GTextField.java: -------------------------------------------------------------------------------- 1 | /* 2 | * File: GTextField.java 3 | * --------------------- 4 | * This file implements the GTextField interactor class. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | package stanford.spl; 26 | 27 | import java.awt.event.ActionListener; 28 | 29 | import javax.swing.JTextField; 30 | 31 | public class GTextField extends GInteractor { 32 | public GTextField(int nChars, ActionListener listener) { 33 | super(new JTextField(nChars)); 34 | JTextField field = (JTextField) getInteractor(); 35 | if (listener != null) field.addActionListener(listener); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /java/src/stanford/spl/JBEConsole.java: -------------------------------------------------------------------------------- 1 | /* 2 | * File: JBEConsole.java 3 | * --------------------- 4 | * This file implements the console for the JBE. Most of the code is 5 | * inherited from IOConsole. 6 | */ 7 | 8 | /*************************************************************************/ 9 | /* Stanford Portable Library */ 10 | /* Copyright (C) 2013 by Eric Roberts */ 11 | /* */ 12 | /* This program is free software: you can redistribute it and/or modify */ 13 | /* it under the terms of the GNU General Public License as published by */ 14 | /* the Free Software Foundation, either version 3 of the License, or */ 15 | /* (at your option) any later version. */ 16 | /* */ 17 | /* This program is distributed in the hope that it will be useful, */ 18 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 19 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 20 | /* GNU General Public License for more details. */ 21 | /* */ 22 | /* You should have received a copy of the GNU General Public License */ 23 | /* along with this program. If not, see . */ 24 | /*************************************************************************/ 25 | 26 | package stanford.spl; 27 | 28 | import acm.io.IOConsole; 29 | 30 | import java.awt.Dimension; 31 | 32 | public class JBEConsole extends IOConsole { 33 | public void setPreferredSize(int width, int height) { 34 | preferredWidth = width; 35 | preferredHeight = height; 36 | } 37 | 38 | public Dimension getPreferredSize() { 39 | return new Dimension(preferredWidth, preferredHeight); 40 | } 41 | 42 | private int preferredWidth; 43 | private int preferredHeight; 44 | 45 | } 46 | -------------------------------------------------------------------------------- /java/src/stanford/spl/JBEFileFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * File: JBEFileFilter.java 3 | * ------------------------ 4 | * This file implements the file filter for the file dialogs. 5 | */ 6 | 7 | /*************************************************************************/ 8 | /* Stanford Portable Library */ 9 | /* Copyright (C) 2013 by Eric Roberts */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify */ 12 | /* it under the terms of the GNU General Public License as published by */ 13 | /* the Free Software Foundation, either version 3 of the License, or */ 14 | /* (at your option) any later version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 19 | /* GNU General Public License for more details. */ 20 | /* */ 21 | /* You should have received a copy of the GNU General Public License */ 22 | /* along with this program. If not, see . */ 23 | /*************************************************************************/ 24 | 25 | package stanford.spl; 26 | 27 | import acm.util.FileChooserFilter; 28 | 29 | public class JBEFileFilter extends FileChooserFilter { 30 | 31 | public JBEFileFilter(String path) { 32 | super(getPattern(path)); 33 | int sp = Math.max(path.lastIndexOf("/"), path.lastIndexOf('\\')); 34 | dir = (sp == -1) ? "" : path.substring(0, sp); 35 | String last = path.substring(sp + 1); 36 | if (!isPattern(last)) { 37 | if (dir.isEmpty()) dir += "/"; 38 | dir += last; 39 | } 40 | if (dir.isEmpty()) { 41 | dir = System.getProperty("user.dir"); 42 | } else if (!dir.startsWith("/")) { 43 | dir = System.getProperty("user.dir") + "/" + dir; 44 | } 45 | } 46 | 47 | public String getDirectory() { 48 | return dir; 49 | } 50 | 51 | private static String getPattern(String path) { 52 | int sp = Math.max(path.lastIndexOf("/"), path.lastIndexOf('\\')); 53 | String last = path.substring(sp + 1); 54 | return (isPattern(last)) ? last : null; 55 | } 56 | 57 | private static boolean isPattern(String str) { 58 | for (int i = 0; i < str.length(); i++) { 59 | switch (str.charAt(i)) { 60 | case ';': case '*': case '?': case '[': case ']': return true; 61 | } 62 | } 63 | return false; 64 | } 65 | 66 | private String dir; 67 | 68 | } 69 | -------------------------------------------------------------------------------- /java/src/stanford/spl/JBELabel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * File: JBELabel.java 3 | * ------------------- 4 | * This file implements an extension to the GLabel class that stores 5 | * a shadow JLabel used for the control strips. 6 | */ 7 | 8 | /*************************************************************************/ 9 | /* Stanford Portable Library */ 10 | /* Copyright (C) 2013 by Eric Roberts */ 11 | /* */ 12 | /* This program is free software: you can redistribute it and/or modify */ 13 | /* it under the terms of the GNU General Public License as published by */ 14 | /* the Free Software Foundation, either version 3 of the License, or */ 15 | /* (at your option) any later version. */ 16 | /* */ 17 | /* This program is distributed in the hope that it will be useful, */ 18 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 19 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 20 | /* GNU General Public License for more details. */ 21 | /* */ 22 | /* You should have received a copy of the GNU General Public License */ 23 | /* along with this program. If not, see . */ 24 | /*************************************************************************/ 25 | 26 | package stanford.spl; 27 | 28 | import acm.graphics.GLabel; 29 | 30 | import java.awt.Color; 31 | import java.awt.Font; 32 | 33 | import javax.swing.JComponent; 34 | import javax.swing.JLabel; 35 | 36 | public class JBELabel extends GLabel { 37 | public JBELabel(String label) { 38 | super(label); 39 | jlabel = null; 40 | } 41 | 42 | public JComponent getInteractor() { 43 | if (jlabel == null) { 44 | jlabel = new JLabel(getLabel()); 45 | jlabel.setFont(getFont()); 46 | jlabel.setForeground(getColor()); 47 | } 48 | return jlabel; 49 | } 50 | 51 | public void setFont(Font font) { 52 | super.setFont(font); 53 | if (jlabel != null) jlabel.setFont(font); 54 | } 55 | 56 | public void setColor(Color color) { 57 | super.setColor(color); 58 | if (jlabel != null) jlabel.setForeground(color); 59 | } 60 | 61 | public void setLabel(String s) { 62 | if (jlabel == null) super.setLabel(s); 63 | else jlabel.setText(s); 64 | } 65 | private JLabel jlabel; 66 | } 67 | -------------------------------------------------------------------------------- /java/src/stanford/spl/TopCompound.java: -------------------------------------------------------------------------------- 1 | /* 2 | * File: TopCompound.java 3 | * ---------------------- 4 | * This file extends GCompound so that it can represent the 5 | * the top-level compound of a GWindow. 6 | */ 7 | 8 | /*************************************************************************/ 9 | /* Stanford Portable Library */ 10 | /* Copyright (C) 2013 by Eric Roberts */ 11 | /* */ 12 | /* This program is free software: you can redistribute it and/or modify */ 13 | /* it under the terms of the GNU General Public License as published by */ 14 | /* the Free Software Foundation, either version 3 of the License, or */ 15 | /* (at your option) any later version. */ 16 | /* */ 17 | /* This program is distributed in the hope that it will be useful, */ 18 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 19 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 20 | /* GNU General Public License for more details. */ 21 | /* */ 22 | /* You should have received a copy of the GNU General Public License */ 23 | /* along with this program. If not, see . */ 24 | /*************************************************************************/ 25 | 26 | package stanford.spl; 27 | 28 | import acm.graphics.GCompound; 29 | 30 | public class TopCompound extends GCompound { 31 | public void setCanvas(JBECanvas jc) { 32 | owner = jc; 33 | } 34 | 35 | public JBECanvas getCanvas() { 36 | return owner; 37 | } 38 | 39 | private JBECanvas owner; 40 | } 41 | --------------------------------------------------------------------------------