
/* Generated by Interface Builder */

#import "Diagrammer.h"

@implementation Diagrammer

    
    //  Modified for NeXT by J. Rettenmayer   5/16/91
/*
$Header: c:/res/adroff/RCS/adroff.c 1.9 88/09/07 07:56:03 jr Exp $
*/

/* Idea for action diagram formatter originated by John W. Rettenmayer,
   who has guided the design and also contributed some code.

   Spring Quarter '86 --  ACTION DIAGRAMMER first programmed by Joyce Miller,
                          Rhonda Aubrey, and Rob Romsa at
			  Eastern Montana College

   Spring Quarter '87 -- Line wrap and line continuation features added
		         by Don Joppa at Eastern Montana College

   Summer 1988 --------  Bugs fixed and graphics characters added by Darin
                         LeBleu at Northeast Louisiana University
*/

#include <stdio.h>
#include <math.h>
#include <ctype.h>

#define EQ ==

#define DOUHORG 205
#define DOULOWCORG 200
#define DOUUPPCORG 201
#define DOUVERG 186
#define EQUALA 61
#define FALSE 0
#define INDENT 5
#define LNLGTH 512
#define NSIZE 15
#define PLUS 43
#define PRINTLNLGTH 80
#define RIGHT_MARGIN 7
#define SINHORG 196
#define SINLOWCORG 192
#define SINLOWLEFCOR 192
#define SINLOWRIGCOR 217
#define SINUPPCORG 218
#define SINUPPRIGCOR 191
#define SINVERA 124
#define SINVERCONA 124
#define SINVERCONG 195
#define SINVERG 179
#define SLASHA 45
#define ARROW 60
#define TRUE 1

- formatDiagram:sender
{

int     firstcol = 10;
int     indwidth = 5;
int     scrwidth = 80;

    NXStream *inStream;
    NXStream *outStream;

   const char *inFileName = "tmpAdroffSource";
    const char *outFileName = "tmpAdroffDiagram";

//FILE * infile;
//FILE * outfile;
//main (argc, argv)
//int     argc;
//char   *argv[];

    int     i,
	    test,
            numbars = 0,
            g,
            barchar = 0,
	    fl = 'm';
    char    line[LNLGTH];

    //g = openfiles (argc, argv);
    
    inStream = NXMapFile(inFileName, NX_READONLY);       
    NXSeek(inStream,0, NX_FROMSTART);  
    outStream = NXMapFile(outFileName, NX_WRITEONLY);       
    NXSeek(stream,0, NX_FROMSTART);  
    
    while (getstring (line) !=  0) {
	test = FALSE;
	if (line[0] EQ '.') {
	    test = TRUE;
	    execute (line, &numbars, g);
	    if (line[1] != 'b' && line[1] != 'i' && line[1] != 'g') {
		getstring (line);
		if (line[0] EQ '.' && line[1] EQ 'b')
		  drawbox (numbars, g);
		else
		  print_line (line, numbars, test);
	    }
	}
	else {
	    printbars (numbars, barchar, fl);
	    for (i = 0; i < indwidth; i++)
		fprintf (outfile, " ");
	    print_line (line, numbars, test);
	}
    }

    NXFlush(outStream);
    NXSaveToFile(outStream,outFileName);
    NXCloseMemory(outStream,NX_FREEBUFFER);
    NXCloseMemory(inStream,NX_FREEBUFFER);
}


execute (line, numbars, g)
char   *line;
int    *numbars;
int    g;

{

    char    command,
            ext,
            fl = 'e';
    int	    barchar,
            n;
    static  int     sv = SINVERA,
		    svc = SINVERCONA,
		    dvr = SINVERA,
		    shr = SINVERA,
		    dhl = SINVERA,
		    shl = SINVERA,
		    dhr = SINVERA,
         	    sh = SLASHA,
		    dh = SLASHA,
         	    dhh = EQUALA;
    if (g == 1) sv = SINVERG,
		svc = SINVERCONG,
		dvr = DOUVERG,
		shr = SINUPPCORG,
		dhl = DOULOWCORG,
		shl = SINLOWCORG,
		dhr = DOUUPPCORG,
		sh = SINHORG,
		dh = DOUHORG,
		dhh = DOUHORG;


    ext = (strlen (line) > 2) ? line[2] : '1';
    command = line[1];
    barchar = 0;

   switch (command) {
	case 'i':
	    barchar = sv;
	    inoutput (*numbars, barchar, fl, ext);
	    break;
	case 'b':
	    barchar = sv;
	    printbars (*numbars, barchar, fl);
	    drawbox (*numbars, g);
	    break;
	case 't':
	    printarrows (*numbars, ext, g);
	    break;
	case 'p':
	    fprintf (outfile, "\f\n");
	    barchar = sv;
	    printbars (*numbars, barchar, fl);
	    break;
	case 's':                   /* selection command         */
	case 'c':                   /* original "choice" command */
	    barchar = sv;
	    printbars (*numbars, barchar, fl);
	    fprintf (outfile, "%c%c%c%c%c", shr, sh, sh, sh, ' ');
	    (*numbars)++;
	    break;

	case '-':
 	    barchar = sv;
	    printbars (*numbars - 1, barchar, fl);
            fprintf (outfile, "%c%c%c%c%c", svc, sh, sh, sh, ' ');
	    break;

	case 'e':
	    (*numbars)--;
	    n = printbars (*numbars);
	    if (n == 186)
	    fprintf (outfile, "%c%c%c%c%c", dhl, dh, dh, dh, ' ');
	    else
	    fprintf (outfile, "%c%c%c%c%c", shl, sh, sh, sh, ' ');
	    break;

	case 'r':
	    barchar = dvr;
	    printbars (*numbars, barchar, fl);
	    fprintf (outfile, "%c%c%c%c%c", dhr, dhh, dhh, dhh, ' ');
	    (*numbars)++;
	    break;
	default:
	    fprintf (stderr, "UNRECOGNIZABLE DOT COMMAND -- %c --\n\n", command)
;
	    fprintf (stderr, "PLEASE CORRECT DOT COMMAND\n");
	    exit (1);
	    break;
    }
}


drawbox (numbars, g)
int    *numbars,
       g;
{

    char    line[LNLGTH];
    int     i,
	    fl = 'b',
	    barchar = 0,
            dc1 = PLUS,
	    dc2 = PLUS,
	    dc3 = PLUS,
	    dc4 = PLUS,
	    s = SLASHA,
	    l = SINVERA;

    if (g == 1)
	    dc1 = SINUPPCORG,
	    dc2 = SINUPPRIGCOR,
	    dc3 = SINLOWLEFCOR,
	    dc4 = SINLOWRIGCOR,
	    s =   SINHORG,
	    l =   SINVERG;
    getstring (line);
    if (line[strlen (line) - 1] EQ '\n')
	line[strlen (line) - 1] = '\0';
    fprintf (outfile, "     %c", dc1);
    for (i = 0; i < (strlen (line) + 2); i++)
	fprintf (outfile, "%c", s);
    fprintf (outfile, "%c\n", dc2);
    printbars (numbars, barchar, fl);
    fprintf (outfile, "     %c ", l);
    fprintf (outfile, "%s", line);
    fprintf (outfile, " %c\n", l);
    printbars (numbars);
    fprintf (outfile, "     %c", dc3);
    for (i = 0; i < (strlen (line) + 2); i++)
	fprintf (outfile, "%c", s);
    fprintf (outfile, "%c\n", dc4);
}


inoutput (numbars, barchar, fl, ext)
int     numbars;
int     barchar;
char    fl;
char    ext;
{
    char    line[LNLGTH];
    int     i, j,
            fill;
    int     extension;

    extension = ext - '0';

    for (i = 0; i < extension; i++) {
	printbars (numbars, barchar, fl);
	getstring (line);
	fill = scrwidth - ((2 * (indwidth * numbars)) + strlen (line) + 5);
	for (j = 0; j < fill; j++) {
	    fprintf (outfile, " ");
	}
	fprintf (outfile, "*%s", line);
    }
}


printarrows (numbars, ext, g)
int     numbars;
char    ext;
int g;
{
    int     i, num, extension;
    int numext,
	fl = 'a',
        barchar = 0,
        s = SLASHA,
	ar = ARROW;

    if (g == 1)
	    s = SINHORG,
	    ar = ARROW;

    extension = ext - '0';

    if (extension == numbars) {
	for (i = 1; i < (firstcol); i++)
	    fprintf (outfile, " ");
    }
    else {
	numext = (numbars - extension);
	printbars (numext, barchar, fl);
    }
    fprintf (outfile, "\b%c", ar);
    for (i = 0; i < extension; i++)
	fprintf (outfile, "%c%c%c%c%c", s, s, s, s, s);
    fprintf (outfile, "%c%c%c%c ", s, s, s, s);
}


printbars (numbars, barchar, fl)
int     numbars;
int     barchar;
char    fl;
{
static    int    bartable[NSIZE];
    int     i,
            j,
	    x;
    bartable[0] = 179;
	if (fl == 'e') bartable[numbars + 1] = barchar;
    for (i = 1; i < firstcol; i++)
	fprintf (outfile, " ");
    for (i = 1; i <= numbars; i++) {
	fprintf (outfile, "%c", bartable[i]);
	for (j = 1; j < indwidth; j++)
	    fprintf (outfile, " ");
    }
    return(bartable[i]);
}


printerrmsg (filename)
char   *filename;
{
    fprintf (stderr, "Can not open %s\n", filename);
    exit (1);
}


print_line (line, numbars, test)
char   *line;
int     numbars, test;
{
    char    fl = 'p';
    int     barchar = 0,
	    counter = 0,
            i,
            length_of_line = strlen (line),
            k = 0;

    break_line_up (numbars, line, test);
    while (counter <= length_of_line - 1) {
	k++;
	if (k != 1) {
	    fputc ('\n', outfile);
	    printbars (numbars, barchar, fl);
	    if (test == TRUE)
		for (i = 1; i <= INDENT; i++)
		    fputc (' ', outfile);
            else
	        for (i = 1; i <= (indwidth + INDENT); i++)
	            fputc (' ', outfile);
	    counter++;
	}
	while ((line[counter] != '\0') && (counter <= length_of_line - 1))
	    fputc (line[counter++], outfile);
    }
}

break_line_up (numbars, line, test)
int     numbars,
	test;
char   *line;

{
    int     k = 1,
            out_line_length,
            spaces_used,
            pointer,
            length_of_str = strlen (line),
            counter;

    spaces_used = firstcol + numbars * indwidth - 1;
    out_line_length = PRINTLNLGTH - spaces_used - RIGHT_MARGIN;
    pointer = out_line_length;
    if (test == TRUE)
	out_line_length = out_line_length + 5;
    if (out_line_length < length_of_str) {
	while (pointer <= length_of_str - 3) {
	    counter = 0;
	    if (k == 2)
	       	out_line_length -= INDENT;
	    while (!isspace (line[pointer - counter]))
		counter++;
	    line[pointer - counter] = '\0';
	    pointer += (out_line_length - counter);
	    k++;
	}
    }
}


int getstring (char * line)

{
    char    line2[LNLGTH];
    int       place_of_alpha = 2;

    if (fgets (line, LNLGTH, infile) != NULL) {
	place_of_alpha = find_alpha(line, place_of_alpha);
	while (line[strlen (line) - place_of_alpha] == '\\') {
	    if (isspace(line[strlen (line) - (place_of_alpha + 1)])) {
		place_of_alpha = find_alpha(line,
			place_of_alpha + 1);
		place_of_alpha--;
		}
	    fgets (line2, LNLGTH, infile);
	    line[strlen (line) - (place_of_alpha)] = ' ';
	    line[strlen (line) - (place_of_alpha - 1)] = '\0';
	    strcat (line, line2);
	    place_of_alpha = 2;
	    place_of_alpha = find_alpha(line,
		    place_of_alpha);
	}
    }
    else
	return (0);
}

find_alpha (char * line, int counter)
{
    while (isspace (line[strlen (line) - counter]))
	counter++;
    return (counter);
}

    
    return self;
}


@end
