[comp.os.os9] DynaCon Utility

jimomura@lsuc.on.ca (Jim Omura) (06/03/89)

     I'm posting a number of data files on BIX that start from
Dynacalc on my CoCo3 but are meant for general consumption.  As
such, I finally got around to writing this Q&D conversion program.
You will notice from the sources that I plan to add many features
to it.  Don't count on it.  It took me over a year to get around
to writing this much.  I write what I need when I need it, and
sometimes not even then.  But if anybody else feels like finishing
it, great!

Cheers! -- Jim O.

#	This is a shell archive.
#	Remove everything above and including the cut line.
#	Then run the rest of the file through sh.
#----cut here-----cut here-----cut here-----cut here----#
#!/bin/sh
# shar:    Shell Archiver
#	Run the following text with /bin/sh to create:
#	dynacon.doc
#	dymake.09
#	dynacon.c
#	sample.dat
#	sample.out
# This archive created: 
# By:	Jim Omura ()
cat << \SHAR_EOF > dynacon.doc
89/06/02

Dynacon
version 0.1

     This is a quick and dirty converter which ONLY handles
one type of conversion.  It will read a Dynacalc Data file
and output a Comma Separated Value file.  With this much
many BIX users will find that they'll be able to read a lot
of datafiles I'll be uploading to BIX in the future and
use the information in their programs and spreadsheets.

     The files in this Sharfile should unpack as follows:

     dymake.09 (Makefile for Dynacalc for OS-9 6809)
     dynacon.doc (this file)
     dynacon.c
     sample.dat
     sample.out

     The 'sample.out' file is the output result that you
should get if your run this program with 'sample.dat' as your
sample input file.

Cheers! -- Jim Omura
BIX 'jimomura'
SHAR_EOF
cat << \SHAR_EOF > dymake.09
* Makefile for Dynacon.C

CFLAGS = -r

dynacon: dynacon.r
  cc1 dynacon.r
  del c.com

dynacon.r: dynacon.c
  cc1 $(CFLAGS) dynacon.c
SHAR_EOF
cat << \SHAR_EOF > dynacon.c
/* Dynacon.C */

/* Converts DynaCalc data format
 * to standard Comma Separated Value format.
 */

/* Version 0.1  Public Domain
 * 1989/06/02 By Jim Omura
 *
 * *** ONLY DEFAULT CONVERSION CURRENTLY WORKING ***
 *
 * Usage: dynacon [-?] [-i=t] [-o=t] [infile] [outfile]
 * ? Short Help
 * i Input file type
 * o Output file type
 *   For 'i' and 'o', 't' denotes a type as follows:
 *     c = Comma Separated Value
 *     d * Dynacalc
 *     s = Super Data Interchange Format/DIF file
 * Default -i=d -o=c
 */

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

#define ERROR -1
#define FALSE  0
#define STDIN  0
#define STDOUT 1
#define STDERR 2
#define TRUE   1

#define BUFLEN 256

/* Constants: */

char comma[]={',',0};
char dquote[]={'"',0};
char eol[]={EOL,0};

main(argc,argv)
int argc;
char **argv;
{

    char  chrbuffer;        /* Character Buffer */
    int   eorflg;           /* End of Row Flag */
    FILE  *infile;
    char  lnbuffer[BUFLEN]; /* Line Buffer */
    int   newlnflg;         /* New Output Line Flag */
    int   numflg;           /* Number flag */
    int   orientflg;        /* Orientation Flag */
    FILE  *outfile;
    int   report;           /* Returned values */
    char  *rdpntr;          /* Read Pointer */
    int   wrtlen;           /* Length of buffer to write */
    char  *wrtpntr;         /* Write Pointer */

/* Set Defaults: */

    eorflg   = FALSE;
    infile   = stdin;
    newlnflg = TRUE;
    outfile  = stdout;

    orientflg = -1; /* -1=unassigned, 0=default, 1=rows, 2=columns */

    for(;argc > 1;)
    {
        ++argv;
        --argc;
        if(**argv== '-')
        {
            for(;;)
            {
                ++*argv;
                switch((int) **argv)
                {
                case NULL:
                    break;
                case '?':
                    fprintf(stdout, "dynacon [-?] [inpath] [outpath]\n");
                    fprintf(stdout, "Switches Not Implimented.\n");
                    exit(0);
                default:
                    continue;
                }   /* Endswitch */
            }   /* Endloop */
            continue;
        }   /* Endif A Dash */

        if(infile == stdin)
        {
            infile = fopen(*argv,"r");
            if (infile == NULL)
            {
                fprintf(stderr,"DynaCon:  No such file %s.\n",*argv);
                exit(-1);
            }
            continue;
        }   /* Endif inpath openner */

        if(outfile == stdout)
        {
            report=access(*argv,S_IWRITE);
            if(report == 0)
            {
                fprintf(stderr,"DynaCon:  File %s exists.\n",*argv);
                exit(-1);
            }

            outfile=fopen(*argv,"w");
            if(outfile == NULL)
            {
                fprintf(stderr,"DynaCon:  Cannot create %s\n",*argv);
                exit(-1);
            }   /* Endif */

        }   /* Endif outpath creator */

    }   /* Endloop Parameter Handler */

    for(;;)
    {
        report = (int) fgets(lnbuffer,BUFLEN,infile);
        if(report == NULL)
        {
            exit(0);
        }
        wrtlen = strlen(lnbuffer);

/* File Header and Comment Handing */

        if(*lnbuffer == '*')
        {
            continue;
        }

/* First non-comment line *must* be orientation if any */
        if(orientflg == -1)
        {

            if(*lnbuffer != '/')
            {
                orientflg = 0; /* Default orientation */
            }
            else
            {
                chrbuffer = lnbuffer[1]; /* 2nd char */

                switch((int)chrbuffer)
                {
                case 'C':
                    orientflg = 1; /* Columns */
                    continue;
                case 'R':
                    orientflg = 2; /* Rows */
                    continue;
                default:
                    orientflg = 0; /* Default */
                    break;
                }   /* Endswitch */

            }   /* Endcondition orientation defline */

        }   /* Endif first non-Comment line */

        chrbuffer = *lnbuffer; /* 1st Char */
        if((chrbuffer == '.') || (isdigit(chrbuffer)) || (chrbuffer == '-'))
        {
            numflg = TRUE;

        }   /* Endcondition Number */
        else
        {
            numflg = FALSE;
            if(wrtlen == 2)    /* NULL terminator is counted */
            {
                switch ((int) *lnbuffer)
                {
                case '>':
                    *lnbuffer = 0; /* Blank Line */
                    break;
                case '@':
                    *lnbuffer = 0;
                    eorflg = TRUE;
                    break;
                default:
                    break;
                }   /* Endswitch */

            }   /* Endif DynaFlags */

        }   /* Endcondition not number */

/* Strip double-quotes and EOL */
            rdpntr  = lnbuffer;
            report  = wrtlen;
            wrtlen  = 0;        /* Inefficient, but easier to understand */
            wrtpntr = lnbuffer;

            for(;report > 0;)
            {
                chrbuffer = *rdpntr;
                if((chrbuffer != '"') && (chrbuffer != EOL))
                {
                    *wrtpntr = chrbuffer;
                    ++wrtlen;
                    ++wrtpntr;
                }
                ++rdpntr;
                --report;
            }   /* Endloop double quote stripper */
            *wrtpntr = NULL; /* Terminate Line */

/* Write output */

        if (eorflg == FALSE)
        {

            if (newlnflg == TRUE)
            {
                newlnflg = FALSE;
            }
            else
            {
                fprintf(outfile,"%s",comma);
            }

            if(numflg == FALSE)
            {
                fprintf(outfile,"%s",dquote);
            }

            fputs(lnbuffer,outfile); /* *** Write Actual Data *** */

            if(numflg == FALSE)
            {
                fprintf(outfile,"%s",dquote);
            }
        }
        else
        {
            fprintf(outfile,"\n");
            eorflg = FALSE;
            newlnflg = TRUE;
        }

    }   /* Endloop Conversion */

}   /* End of Main() */

/* End of dynacon.c */
SHAR_EOF
cat << \SHAR_EOF > sample.dat
*DYNACALC Data File
*     June  2, 1989
/Col
Sample File
Blank Column
>
>
>
>
>
@
>
Text Column
Label 1
"Label 2"
label 3
'4th Label
>
@
>
Numbers
1234
1.234
12.34
-1234
.1234
@
SHAR_EOF
cat << \SHAR_EOF > sample.out
"Sample File","Blank Column","","","","",""
"","Text Column","Label 1","Label 2","label 3","'4th Label",""
"","Numbers",1234,1.234,12.34,-1234,.1234
SHAR_EOF
#	End of shell archive
exit 0
-- 
Jim Omura, 2A King George's Drive, Toronto, (416) 652-3880
lsuc!jimomura
Byte Information eXchange: jimomura