Hogwarts Wand Docs: ../server/gg.c

Wand Sourcecode: ../server/gg.c

//
// gg.c - graph a wpic*.txt file
//
// Copyright (C) 2006  Nathan (Acorn) Pooley
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 2 as published by the Free Software Foundation.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License (gpl.txt) for more details. 
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
// 
// You can contact the author, Nathan (Acorn) Pooley, by writing
// to Nathan (Acorn) Pooley, 949 Buckeye Drive, Sunnyvale, CA  94086, USA
// or through the form at http://www.rawbw.com/~acorn/wand/feedback.html
//

//#@DOC@ draw a visual graph of a wpic*.txt file

//###########################################################################
//############################### INCLUDES ##################################
//###########################################################################

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <math.h>
#include "graph.h"
#include <stdio.h>

//###########################################################################
//############################### GLOBALS ###################################
//###########################################################################

const char *g_filename="";
float g_mag_max = 0;
float OPT_avgx = -1.0;
float OPT_avgy = -1.0;

//###########################################################################
//############################### GLOBALS ###################################
//###########################################################################

//===========================================================================
// usage()
//===========================================================================
void usage(void)
{
    printf(
        "Usage: gg [OPTIONS] <filename>\n"
        "  Display file as a graph.  File should contain lines with N space\n"
        "  separated numbers per line.  # marks comment lines.\n"
        "OPTIONS:\n"
        "  -s<n>   skip first <n> points\n"
        "  -q      exit as soon as calculations are done\n"
    );
    exit(1);
}

//===========================================================================
// getInt()
//===========================================================================
int getInt(FILE *fp, int *val)
{
    int c = getc(fp);
    while(isspace(c) && c!='\n') {
        c = getc(fp);
    }
    ungetc(c,fp);
    if (isdigit(c)) {
        fscanf(fp,"%i",val);
        return 1;
    } else {
        return 0;
    }
}

//===========================================================================
// main()
//===========================================================================
int main(int argc, char *argv[])
{
    const char *filename = argv[1]?argv[1]:"wpic.txt";
    GWindow *gw;
    FILE *fp;
    int i;
    int line[100];
    int breakline[100];
    float  t;
    float  t0;
    float  last_t;
    float  scale_time = 1.0/(16.0 * 256);
    int OPT_skip = 0;
    int OPT_calc_exit = 0;
    int lidx = -1;
    int cnt = -1;
    int lcnt = 0;
    int linenum = 1;

    static int colors[] = {
        0xff0000,   // red
        0x0000ff,   // blue
        0x00ffff,   // cyan
        0xffff00,   // yellow
        0xff00ff,   // magenta
        0x00ff00,   // green
    };

    for (i=1; i<argc; i++) {
        if (argv[i][0] == '-') {
            switch(argv[i][1]) {
                case 's':
                    OPT_skip = strtol(argv[i]+2,0,10);
                    break;
                case 'q':
                    OPT_calc_exit = 1;
                    break;
                default:
                    printf("Unknown option: '%s'\n",argv[i]);
                    usage();
            }
        } else {
            filename = argv[i];
        }
    }

    if (OPT_skip < 0) {
        printf("Bad OPT_skip = %d < 0\n",OPT_skip);
        usage();
    }

    gw = gwinCreate();
    gwinSetArgs(gw, argc,argv);
    gwinSetName(gw, filename);
    gwinOpen(gw);


    g_filename = filename;
    fp = fopen(filename,"r");
    while(1) {
        int skipline = 0;
        int c = getc(fp);
        float val = 0;
        while(isspace(c)) {
            if (c == '\n') {
                linenum++;
                lidx = -1;
            }
            c = getc(fp);
        }
        if (c==EOF) break;

        if (isdigit(c) || c=='-'||c=='.'||c=='+') {
            ungetc(c,fp);
            fscanf(fp, "%f", &val);
            if (lidx < 0) {
                if (cnt == -1) {
                    t0 = val;
                    cnt = 0;
                } else {
                    while(1) {
                        t = val - t0;
                        if (t>= last_t) break;
                        t0 -= 65536;
                    }
                    last_t = t;
                }
                lidx = 0;
            } else {
                if (lidx>=lcnt || breakline[lidx]) {
                    char name[100];
                    line[lidx] = gwinLineCreate(gw);
                    breakline[lidx] = 0;
                    snprintf(name,sizeof(name),"%d",lidx);
                    gwinLineName(gw,line[lidx],name);
                    gwinLineColor(gw,line[lidx],
                        colors[lidx % (sizeof(colors)/sizeof(colors[0]))]);
                    printf("Line %d is color 0x%06x\n",
                        lidx,
                        colors[lidx % (sizeof(colors)/sizeof(colors[0]))]);
                    lcnt++;
                }
                gwinLinePointFlags(
                            gw,
                            line[lidx],
                            scale_time*t,
                            val,
                            0);
                lidx++;
            }
        } else if (c=='#') {
            skipline = 1;

        } else if (lidx < 0 && c=='B') {
            int lnum;
            if (getInt(fp,&lnum)) {
                breakline[lnum] = 1;
            } else {
                fprintf(stderr,"Error in B command at line %d\n",linenum);
            }
            skipline = 1;

        } else {
            fprintf(stderr,"Error at line %d\n",linenum);
        }
        if (skipline) {
            while(c!='\n' && c!=EOF) {
                c = getc(fp);
            }
            linenum++;
            lidx = -1;
        }
    }

    printf("Found %d points\n",i);
    gwinDraw(gw);

    if (OPT_calc_exit) {
        exit(0);
    }

    gwinEventLoop(gw);

    gwinDestroy(gw);
    return 0;
}


This file Copyright (C) 2006 by Nathan (Acorn) Pooley
Go to TOP Wand page
Go to Acorn's personal webpage
Go to Hogwarts website: www.gotohogwarts.com
Snout: www.snout.org/game
Gadgets of Justice Unlimited
Snout GC (Wiki)
Snout Wiki
File created by do_doc at Wed May 30 03:27:49 PDT 2007