File: programming/cocoa/

#include "glk.h"
/* model.c: Model program for Glk API, version 0.5.
    Designed by Andrew Plotkin <>
    This program is in the public domain.
/* This is a simple model of a text adventure which uses the Glk API.
    It shows how to input a line of text, display results, maintain a
    status window, write to a transcript file, and so on. */
/* This is the cleanest possible form of a Glk program. It includes only
    "glk.h", and doesn't call any functions outside Glk at all. We even
    define our own str_eq() and str_len(), rather than relying on the
    standard libraries. */
/* We also define our own TRUE and FALSE and NULL. */
#ifndef TRUE
#define TRUE 1
#ifndef FALSE
#define FALSE 0
#ifndef NULL
#define NULL 0
/* The story, status, and quote windows. *//* A file reference for the transcript file. *//* A stream for the transcript file, when it's open. *//* Your location. This determines what appears in the status line. *//* A flag indicating whether you should look around. *//* Forward declarations *//* The glk_main() function is called by the Glk system; it's the main entry
    point for your program. *//* Open the main window. *//* It's possible that the main window failed to open. There's
            nothing we can do without it, so exit. *//* Set the current output stream to print to it. *//* Open a second window: a text grid, above the main window, three lines
        high. It is possible that this will fail also, but we accept that. *//* The third window, quotewin, isn't opened immediately. We'll do
        that in verb_quote(). */"Model Glk Program\nAn Interactive Model Glk Program\n""By Andrew Plotkin.\nRelease 7.\n""Type \"help\" for a list of commands.\n");
    current_room = 0; /* Set initial location. */"\n""The Room\n""A Different Room\n""You're in a room of some sort.\n""\n>");
        /* We request up to 255 characters. The buffer can hold 256, but we
            are going to stick a null character at the end, so we have to
            leave room for that. Note that the Glk library does *not*
            put on that null character. *//* Grab an event. *//* Really the event can *only* be from mainwin,
                            because we never request line input from the
                            status window. But we do a paranoia test,
                            because commandbuf is only filled if the line
                            event comes from the mainwin request. If the
                            line event comes from anywhere else, we ignore
                            it. *//* Windows have changed size, so we have to redraw the
                        status window. *//* commandbuf now contains a line of input from the main window.
            You would now run your parser and do something with it. */
        /* First, if there's a blockquote window open, let's close it. 
            This ensures that quotes remain visible for exactly one
            command. *//* The line we have received in commandbuf is not null-terminated.
            We handle that first. */
        len = ev.val1; /* Will be between 0 and 255, inclusive. */
        commandbuf[len] = '\0';
        /* Then squash to lower-case. *//* Then trim whitespace before and after. */' '' '; cx--) { };
        *(cx+1) = '\0';
        /* cmd now points to a nice null-terminated string. We'll do the
            simplest possible parsing. */"""Excuse me?\n""help""move""jump""yada""quote""quit""save""restore""script""unscript""I don't understand the command \"""\".\n"/* It is possible that the window was not successfully 
            created. If that's the case, don't try to draw it. */"The Room""A Different Room"/* Print the room name, centered. *//* Draw a decorative compass rose in the upper right. */"\\|/""-*-""/|\\"/* This loop is identical to the main command loop in glk_main(). */'\0'' ''y' || *cx == 'Y''n' || *cx == 'N'"Please enter \"yes\" or \"no\": ""This model only understands the following commands:\n""HELP: Display this list.\n""JUMP: A verb which just prints some text.\n""YADA: A verb which prints a very long stream of text.\n""MOVE: A verb which prints some text, and also changes the status line display.\n""QUOTE: A verb which displays a block quote in a temporary third window.\n""SCRIPT: Turn on transcripting, so that output will be echoed to a text file.\n""UNSCRIPT: Turn off transcripting.\n""SAVE: Write fake data to a save file.\n""RESTORE: Read it back in.\n""QUIT: Quit and exit.\n""You jump on the fruit, spotlessly.\n"/* This is a goofy (and overly ornate) way to print a long paragraph. 
        It just shows off line wrapping in the Glk implementation. */
    #define NUMWORDS (13)
"Ga", "Bo", "Wa", "Mu", "Bi", "Fo", "Za", "Mo", "Ra", "Po",
            "Ha", "Ni", "Na""figgle", "wob", "shim", "fleb", "moobosh", "fonk", "wabble",
            "gazoon", "ting", "floo", "zonk", "loof", "lob"" ""."'\n'"Someone quotes some poetry.\n");
    /* Open a third window, or clear it if it's already open. Actually,
        since quotewin is closed right after line input, we know it
        can't be open. But better safe, etc. *//* A five-line window above the main window, fixed size. *//* It's possible the quotewin couldn't be opened. In that
                case, just give up. *//* Print some quote. */"Tomorrow probably never rose or set\n"
        "Or went out and bought cheese, or anything like that\n"
        "And anyway, what light through yonder quote box breaks\n"
        "Handle to my hand?\n""              -- Fred\n""You walk for a while.\n""Are you sure you want to quit? ""Thanks for playing.\n"/* glk_exit() actually stops the process; it does not return. */"Scripting is already on.\n"/* If we've turned on scripting before, use the same file reference; 
        otherwise, prompt the player for a file. */"Unable to place script file.\n"/* Open the file. */"Unable to write to script file.\n""Scripting on.\n""This is the beginning of a transcript.\n""Scripting is already off.\n"/* Close the file. */"This is the end of a transcript.\n\n""Scripting off.\n""Unable to place save file.\n""Unable to write to save file.\n"/* We're done with the file ref now. */
    /* Write some binary data. */"Game saved.\n""Unable to find save file.\n""Unable to read from save file.\n"/* We're done with the file ref now. */
    /* Read some binary data. */"Unexpected end of file.\n""This does not appear to be a valid saved game.\n""Failed.\n""Game restored.\n");
/* simple string length test *//* simple string comparison test */

This code uses the PclZip Zip File reading code, which is subject to the GNU LGPL. It also uses the GeSHi syntax highlighter, subject to the GPL. Ask if you want this for your own web site, it's free.