home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource4
/
290_01
/
misc.c
< prev
next >
Wrap
Text File
|
1990-05-14
|
11KB
|
587 lines
/* misc - miscellaneous flex routines */
/*
* Copyright (c) 1987, the University of California
*
* The United States Government has rights in this work pursuant to
* contract no. DE-AC03-76SF00098 between the United States Department of
* Energy and the University of California.
*
* This program may be redistributed. Enhancements and derivative works
* may be created provided the new works, if made available to the general
* public, are made available for use by anyone.
*/
#include <time.h>
#include "flexdef.h"
#include "main.h"
#include "misc.h"
#include "main.h"
#include "parse.h"
/* action_out - write the actions from the temporary file to lex.yy.c
*
* synopsis
* action_out();
*
* Copies the action file up to %% (or end-of-file) to lex.yy.c
*/
void action_out()
{
char buf[MAXLINE];
while ( fgets( buf, MAXLINE, temp_action_file ) != NULL )
if ( buf[0] == '%' && buf[1] == '%' )
break;
else
fputs( buf, stdout );
}
/* allocate_array - allocate memory for an integer array of the given size */
char *allocate_array( size, element_size )
int size, element_size;
{
register char *mem = malloc( (unsigned) (element_size * size) );
if ( mem == NULL )
flexfatal( "memory allocation failed in allocate_array()" );
return ( mem );
}
/* bubble - bubble sort an integer array in increasing order
*
* synopsis
* int v[n], n;
* bubble( v, n );
*
* description
* sorts the first n elements of array v and replaces them in
* increasing order.
*
* passed
* v - the array to be sorted
* n - the number of elements of 'v' to be sorted */
void bubble( v, n )
int v[], n;
{
register int i, j, k;
for ( i = n; i > 1; --i )
for ( j = 1; j < i; ++j )
if ( v[j] > v[j + 1] ) /* compare */
{
k = v[j]; /* exchange */
v[j] = v[j + 1];
v[j + 1] = k;
}
}
/* clower - replace upper-case letter to lower-case
*
* synopsis:
* char clower(), c;
* c = clower( c );
*/
char clower( c )
register char c;
{
return ( isupper(c) ? tolower(c) : c );
}
/* copy_string - returns a dynamically allocated copy of a string
*
* synopsis
* char *str, *copy, *copy_string();
* copy = copy_string( str );
*/
char *copy_string( str )
register char *str;
{
register char *c;
char *copy;
/* find length */
for ( c = str; *c; ++c )
;
copy = malloc( (unsigned) ((c - str + 1) * sizeof( char )) );
if ( copy == NULL )
flexfatal( "dynamic memory failure in copy_string()" );
for ( c = copy; (*c++ = *str++); )
;
return ( copy );
}
/* cshell - shell sort a character array in increasing order
*
* synopsis
*
* char v[n];
* int n;
* cshell( v, n );
*
* description
* does a shell sort of the first n elements of array v.
*
* passed
* v - array to be sorted
* n - number of elements of v to be sorted
*/
void cshell( v, n )
char v[];
int n;
{
int gap, i, j, jg;
char k;
for ( gap = n / 2; gap > 0; gap = gap / 2 )
for ( i = gap; i < n; ++i )
for ( j = i - gap; j >= 0; j = j - gap )
{
jg = j + gap;
if ( v[j] <= v[jg] )
break;
k = v[j];
v[j] = v[jg];
v[jg] = k;
}
}
/* dataend - finish up a block of data declarations
*
* synopsis
* dataend();
*/
void dataend()
{
if ( datapos > 0 )
dataflush();
/* add terminator for initialization */
puts( " } ;\n" );
dataline = 0;
}
/* dataflush - flush generated data statements
*
* synopsis
* dataflush();
*/
void dataflush()
{
putchar( '\n' );
if ( ++dataline >= NUMDATALINES )
{
/* put out a blank line so that the table is grouped into
* large blocks that enable the user to find elements easily
*/
putchar( '\n' );
dataline = 0;
}
/* reset the number of characters written on the current line */
datapos = 0;
}
/* gettimestr - return current time
*
* synopsis
* char *gettimestr(), *time_str;
* time_str = gettimestr();
*/
char *gettimestr()
{
time_t t ;
char *result, *s ;
t = time( (time_t *) 0 );
result = strdup( ctime( &t ) );
s = result + strlen( result) - 1 ; /* find last char in time string */
while (*s && isspace(*s)) /* trim trailing whitespace */
*s-- = '\0' ;
return ( result );
}
/* lerrif - report an error message formatted with one integer argument
*
* synopsis
* char msg[];
* int arg;
* lerrif( msg, arg );
*/
void lerrif( msg, arg )
char msg[];
int arg;
{
char errmsg[MAXLINE];
(void) sprintf( errmsg, msg, arg );
flexerror( errmsg );
}
/* lerrsf - report an error message formatted with one string argument
*
* synopsis
* char msg[], arg[];
* lerrsf( msg, arg );
*/
void lerrsf( msg, arg )
char msg[], arg[];
{
char errmsg[MAXLINE];
(void) sprintf( errmsg, msg, arg );
flexerror( errmsg );
}
/* flexerror - report an error message and terminate
*
* synopsis
* char msg[];
* flexerror( msg );
*/
void flexerror( msg )
char msg[];
{
fprintf( stderr, "flex: %s\n", msg );
flexend( 1 );
}
/* flexfatal - report a fatal error message and terminate
*
* synopsis
* char msg[];
* flexfatal( msg );
*/
void flexfatal( msg )
char msg[];
{
fprintf( stderr, "flex: fatal internal error %s\n", msg );
flexend( 1 );
}
/* line_directive_out - spit out a "# line" statement */
void line_directive_out( output_file_name )
FILE *output_file_name;
{
if ( infilename && gen_line_dirs )
fprintf( output_file_name, "# line %d \"%s\"\n", linenum, infilename );
}
/* mk2data - generate a data statement for a two-dimensional array
*
* synopsis
* int value;
* mk2data( value );
*
* generates a data statement initializing the current 2-D array to "value"
*/
void mk2data( value )
int value;
{
if ( datapos >= NUMDATAITEMS )
{
putchar( ',' );
dataflush();
}
if ( datapos == 0 )
/* indent */
fputs( " ", stdout );
else
putchar( ',' );
++datapos;
printf( "%5d", value );
}
/* mkdata - generate a data statement
*
* synopsis
* int value;
* mkdata( value );
*
* generates a data statement initializing the current array element to
* "value"
*/
void mkdata( value )
int value;
{
if ( datapos >= NUMDATAITEMS )
{
putchar( ',' );
dataflush();
}
if ( datapos == 0 )
/* indent */
fputs( " ", stdout );
else
putchar( ',' );
++datapos;
printf( "%5d", value );
}
/* myctoi - return the integer represented by a string of digits
*
* synopsis
* char array[];
* int val, myctoi();
* val = myctoi( array );
*
*/
int myctoi( array )
char array[];
{
int val = 0;
(void) sscanf( array, "%d", &val );
return ( val );
}
/* myesc - return character corresponding to escape sequence
*
* synopsis
* char array[], c, myesc();
* c = myesc( array );
*
*/
char myesc( array )
char array[];
{
char c, esc_char;
register int sptr = 2;
switch ( array[1] )
{
case 'n':
return ( '\n' );
case 't':
return ( '\t' );
case 'f':
return ( '\f' );
case 'r':
return ( '\r' );
case 'b':
return ( '\b' );
case '0':
if ( isdigit(array[2]) )
{ /* \0<octal> */
while ( isdigit(array[sptr]) )
/*