DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - download
Index: ┃ T c

⟦26c8f7131⟧ TextFile

    Length: 6904 (0x1af8)
    Types: TextFile
    Names: »colex.c«

Derivation

└─⟦a0efdde77⟧ Bits:30001252 EUUGD11 Tape, 1987 Spring Conference Helsinki
    └─ ⟦this⟧ »EUUGD11/stat-5.3/eu/stat/src/colex.c« 

TextFile

/*  Copyright 1984 Gary Perlman */

/*
	colex extracts columns from its input and prints them
	there are options to specify the printing format of columns
	which are specified in a form convenient for users:
		i integer      (d)
		n numerical    (f)
		a alphabetic   (s)
		e exponential  (g)
	these are converted to the printf format strings in parens.
*/

/* $link: linkml colex parselin+checkio+number+getopt+specol */

#include "stat.h"
PGM (colex,Column Extraction/Formatting,5.2,8/11/86)
#define	MAXCOLS     100
#define MAXCHARS BUFSIZ           /* maximum number of chars in lines */
char	*NAstring = "NA";

Boole	Forcefill = FALSE;    /* empty fields are filled with NA */
Boole	Quotestrings = FALSE; /* strings are quoted */
Boole	Ignore = FALSE;       /* missing strings are ignored */
Boole	Dotab = TRUE;         /* put tab after every column */
Boole	Debug = FALSE;        /* print debugging information */
Boole	Validate = FALSE;     /* validate data type in columns */
Boole	InfoVersion;          /* print version information */
Boole	InfoLimits;           /* print program limits */
Boole	InfoOptions;          /* print usage information */

int 	Lineno = 0;           /* input line number */

char	*Format[MAXCOLS];     /* printf formats for columns */
char	*Dformat = "a";       /* default output format */
int 	Request[MAXCOLS];     /* requested columns */
int 	Nrequest = 0;         /* number of requested columns */
char 	Type[MAXCOLS];        /* data types = a e i n */

#define	T_ALPHA     'a'
#define	T_INTEGER   'i'
#define	T_NUMBER    'n'
#define	T_EXPONENT  'e'

#define	F_ALPHA     's'
#define	F_INTEGER   'd'
#define	F_NUMBER    'f'
#define	F_EXPONENT  'g'

#define	NTYPES       4

\f


main (argc, argv) char **argv;
	{
	char	line[MAXCHARS];
	char	*input[MAXCOLS];
	int 	ncols;
	int 	reqno;              /* request number */
	int 	optind;             /* local optind returned by initial() */
	char	*cs;                /* current column specification */

	optind = initial (argc, argv);

	while (optind < argc)
		{
		cs = argv[optind++];
		Nrequest = specol (cs, Request, Format, Nrequest, MAXCOLS, MAXCOLS);
		if (Nrequest > MAXCOLS)
			ERRMANY (column Requests, MAXCOLS)
		if (Nrequest < 0)
			ERRMSG0 (illegal column specification)
		}

	if (Nrequest == 0)
		ERRMSG0 (No column numbers were supplied)

	settypes ();

	checkstdin ();

	while (gets (line))
		{
		Lineno++;
		ncols = parselin (line, input, MAXCOLS);
		if (ncols > MAXCOLS)
			ERRMANY (columns in input line, MAXCOLS)
		for (reqno = 0; reqno < Nrequest; reqno++)
			prcol (ncols, input, Request[reqno], reqno);
		putchar ('\n');
		}
	exit (0);
	}

\f

/*FUNCTION initial: returns local optind */
int
initial (argc, argv)
int 	argc;
char	**argv;
	{
	extern	int 	optind;
	extern	char	*optarg;
	Boole	errflg = 0;
	int 	c;

	ARGV0;

	while ((c = getopt (argc, argv, "qiftvF:DLOV")) != EOF)
		switch (c)
			{
			default:  errflg = TRUE;       break;
			case 'i': Ignore = TRUE;       break;
			case 'f': Forcefill = TRUE;    break;
			case 'q': Quotestrings = TRUE; break;
			case 't': Dotab = FALSE;       break;
			case 'v': Validate = TRUE;     break;
			case 'F':
				Dformat = optarg;
				break;
			case 'D': Debug = TRUE;        break;
			case 'O': InfoOptions = TRUE;  break;
			case 'V': InfoVersion = TRUE;  break;
			case 'L': InfoLimits = TRUE;   break;
			}

	if (errflg)
		USAGE ([-fiqt] [-F format] column-numbers)

	usinfo ();

	return (optind);
	}

\f

/*
	must be passed the column request number to have format information
*/
prcol (ncols, array, colno, reqno)
int 	ncols;        /* number of columns read in input array */
char	**array;      /* data input array */
int 	colno;        /* requested column number == Request[reqno] */
int 	reqno;        /* request number */
	{
	if (colno > ncols) /* missing column */
		{
		if (Forcefill)
			printstring (NAstring, T_ALPHA, "s");
		else if (Ignore)
			{
			if (Quotestrings)
				printstring ("", T_ALPHA, "s");
			/* else do nothing */
			}
		else
			ERRMSG2 (missing column %d in short input line %d, colno, Lineno)
		}
	else
		printstring (array[colno-1], Type[reqno], Format[reqno]);
	}

printstring (string, type, fmt)
char	*string;
int 	type;
char	*fmt;
	{
	char	qchar = '"';
	double	dtmp, atof ();
	int 	itmp, atoi ();
	char	printbuf[128];

	switch (type)
		{
		case T_ALPHA:
			if (Quotestrings)
				{
				if (index (string, qchar))
					qchar = '\'';
				putchar (qchar);
				}
			sprintf (printbuf, "%c%s", '%', fmt);
			printf (printbuf, string);
			if (Quotestrings)
				putchar (qchar);
			break;
		case T_INTEGER:
			if (Validate && number (string) != 1)
				fprintf (stderr, "%s: %s on line %d is not an integer\n",
					Argv0, string, Lineno);
			itmp = atoi (string);
			sprintf (printbuf, "%c%s", '%', fmt);
			printf (printbuf, itmp);
			break;
		case T_NUMBER:
		case T_EXPONENT:
			if (Validate && !number (string))
				fprintf (stderr, "%s: %s on line %d is not a number\n",
					Argv0, string, Lineno);
			dtmp = atof (string);
			sprintf (printbuf, "%c%s", '%', fmt);
			printf (printbuf, dtmp);
			break;
		}
	if (Dotab)
		putchar ('\t');
	}

\f

/*FUNCTION settypes: set up (user supplied) type and format information */
Status
settypes ()
	{
	int 	colno;
	char	*ptr;
	for (colno = 0; colno < Nrequest; colno++)
		{
		if (*Format[colno] == '\0')
			Format[colno] = Dformat;
		for (ptr = Format[colno]; *ptr && !isalpha (*ptr); ptr++)
			continue;
		switch (*ptr)
			{
			default:
				ERRMSG1 (unknown data type specification: %s, Format[colno])
				/*NOTREACHED*/
				break;
			case T_ALPHA:
			case F_ALPHA:
				Type[colno] = T_ALPHA;
				*ptr++ = F_ALPHA;
				*ptr = '\0';
				break;
			case T_INTEGER:
			case F_INTEGER:
				Type[colno] = T_INTEGER;
				*ptr++ = F_INTEGER;
				*ptr = '\0';
				break;
			case T_NUMBER:
			case F_NUMBER:
				Type[colno] = T_NUMBER;
				*ptr++ = F_NUMBER;
				*ptr = '\0';
				break;
			case T_EXPONENT:
			case F_EXPONENT:
				Type[colno] = T_EXPONENT;
				*ptr++ = F_EXPONENT;
				*ptr = '\0';
				break;
			}

		if (Format[colno][0] == '%')
			Format[colno]++;

		if (Debug)
			fprintf (stderr, "%3d %3d %c %s\n",
				colno+1, Request[colno], Type[colno], Format[colno]);
		}
	return (SUCCESS);
	}

\f


usinfo ()
	{
	if (InfoVersion)
		pver (Version);
	if (InfoLimits)
		{
		plim (Argv0);
		const (MAXCOLS, "maximum number of columns");
		const (MAXCHARS, "maximum number of characters in lines");
		}
	if (InfoOptions)
		{
		ppgm (Argv0, Purpose);
		lopt ('f', "force-fill columns with NA", Forcefill);
		sopt ('F', "format", "default column output format (aine)", Dformat);
		lopt ('i', "ignore missing columns", Ignore);
		lopt ('q', "quote output columns", Quotestrings);
		lopt ('t', "place tab after every column", Dotab);
		lopt ('v', "validate data types in columns", Validate);
		oper ("columns", "columns to extract", "");
		}
	if (InfoVersion || InfoLimits || InfoOptions)
		exit (0);
	}