CCL Home Page
Up Directory CCL utility.c
#include "utility.h"

char *new_extension(filename,extension)
/*============================================================================*/
/* PURPOSE: REMOVE THE CURRENT EXTENSION (IF ANY) ON FILENAME, AND APPEND THE
/*	SPECIFIED EXTENSION.  AN "EXTENSION" IS DEFINED TO BE THE LAST PERIOD
/*	IN THE FILENAME AND ALL CHARACTERS FOLLOWING IT.  A LEADING PERIOD IS
/*	OPTIONAL IN THE NEW EXTENSION - IF NOT PRESENT, IT WILL BE ADDED.
/* INPUTS:
/*	FILENAME	ORIGINAL FILENAME WITH OPTIONAL EXTENSION.
/*	EXTENSION	NEW EXTENSION TO APPEND TO FILENAME AFTER PREVIOUS
/*			EXTENSION IS REMOVED.
/* OUTPUTS:
/*	FILENAME	THE ORIGINAL FILENAME WITH THE NEW EXTENSION.
/*	RETURN VALUE	POINTER TO MODIFIED FILENAME.
/* WRITTEN: M.V.GRIESHABER
*/
   char filename[];
   char extension[];
   {
   char *last_period;	/* POINTER TO LAST PERIOD IN FILENAME. */

   last_period=strrchr(filename,'.');		/* FIND THE LAST PERIOD. */
   if (last_period!=NULL) *last_period=0;	/* TRIM OFF OLD EXTENSION. */

   if (extension[0]!='.') strcat(filename,".");	/* GUARANTEE A PERIOD. */
   strcat(filename,extension);			/* APPEND NEW EXTENSION. */

   return(filename);
   }

int errmsg(message,return_value)
/*============================================================================*/
/* PURPOSE: PRINT AN ERROR MESSAGE ON STANDARD ERROR, WAIT FOR USER TO
/*	ACKNOWLEDGE IT, AND RETURN THE SPECIFIED RETURN_VALUE.  THIS ROUTINE
/*	IS TYPICALLY USED IN AN ERROR CONDITION LIKE SO:
/*		IF (ERROR) RETURN(ERRMSG("XYZ FAILED",FALSE));
/* INPUTS:
/*	MESSAGE		MESSAGE TO DISPLAY FOR USER ("Error: " WILL BE PREFIXED
/*			TO THE MESSAGE, AND ".\n" WILL BE APPENDED.).
/*	RETURN_VALUE	VALUE TO BE RETURNED AT END OF THIS ROUTINE.
/* OUTPUTS:
/*	FUNCTION VALUE	THE RETURN_VALUE SPECIFIED BY THE USER.
/* WRITTEN: M.V.GRIESHABER
*/
   char message[];
   int return_value;
   {
   char junk[100];	/* JUNK INPUT FROM USER. */

   fprintf(stderr,"Error: %s.\n",message);

   printf("Press  to continue: ");
   gets(junk);

   return(return_value);
   }

char *find_string(line,string)
/*=========================================================================*/
/* PURPOSE: DETERMINE WHETHER STRING IS CONTAINED IN LINE.
/* INPUTS:
/*	LINE		RAW LINE POSSIBLY CONTAINING STRING.
/*	STRING		STRING TO BE MATCHED.
/* OUTPUTS:
/*	RETURN VALUE	POINTER TO START OF MATCHED STRING, ELSE NULL.
/* WRITTEN: M.V.GRIESHABER
*/
   char line[];
   char string[];
   {
   char *i;			/* LOOP INDEX TO CHARACTER POSITION. */
   size_t string_length;	/* LENGTH OF STRING. */

   string_length=(size_t)strlen(string);

   /* CHECK EACH I-TH SUBSTRING FOR A MATCH (THIS IS VERY CRUDE). */
   for (i=line; *i!=(char)0; i++)
      {
      /* IF THE STRING MATCHES THE BEGINNING OF THE I-TH SUBSTRING, RETURN */
      /* POINTER TO START OF MATCH. */
      if (strncmp(string,i,string_length)==0) return(i);
      }

   /* HIT END OF LINE WITHOUT FINDING STRING. */
   return(NULL);
   }

char *find_line(file,string,line,line_size)
/*=========================================================================*/
/* PURPOSE: FIND THE NEXT LINE IN THE FILE CONTAINING THE STRING.
/* INPUTS:
/*	FILE		OPEN FILE DESCRIPTOR OF TEXT FILE TO READ.
/*	STRING		STRING TO BE MATCHED.
/*	LINE_SIZE	SIZEOF LINE.
/* OUTPUTS:
/*	LINE		RAW FILE LINE CONTAINING STRING IF FUNCTION IS NON NULL.
/*	RETURN VALUE	POINTER TO START OF MATCHED STRING IN LINE, ELSE NULL.
/* WRITTEN: M.V.GRIESHABER
*/
   FILE *file;
   char string[];
   char line[];
   int line_size;
   {
   char *str_ptr;	/* POINTER TO START OF MATCHED STRING. */

   /* CHECK EACH LINE OF THE FILE. */
   while (fgets(line,line_size,file)!=NULL)
      {
      /* IF THIS LINE CONTAINS STRING, RETURN POINTER TO START OF STRING. */
      if ((str_ptr=find_string(line,string))!=NULL) return(str_ptr);
      }

   /* HIT END OF FILE WITHOUT FINDING LINE CONTAINING STRING. */
   return(NULL);
   }

int gstring(prompt,null_ok,response)
/*============================================================================*/
/* PURPOSE: GET A STRING FROM THE USER.
/* INPUTS:
/*	PROMPT		PROMPT STRING TO BE DISPLAYED FOR USER.
/*	NULL_OK		TRUE/FALSE IF NULL ENTRY IS VALID.
/* OUTPUTS:
/*	RESPONSE	CHARACTER STRING TYPED IN BY USER.
/*	FUNCTION VALUE	STATUS OF USER RESPONSE (AS IN UTILITY.H).
/* WRITTEN: M.V.GRIESHABER
*/
   char prompt[];
   int null_ok;
   char response[];
   {
   char candidate[100];

   while(TRUE)
      {
      if (strlen(prompt)>0) printf("%s: ",prompt);
      gets(candidate);

      if (strlen(candidate)==0)
         {
         if (null_ok) return(NULL_ENTRY);
         else fprintf(stderr,"Error: Invalid response.\n\n");
         }
      else if (candidate[0]==ABORT_CHARACTER) return(ABORT_ENTRY);
      else
         {
         strcpy(response,candidate);
         return(VALID_ENTRY);
         }
      }
   }

int valchars(string,valid_characters)
/*============================================================================*/
/* PURPOSE: DETERMINE IF A STRING IS COMPOSED OF VALID CHARACTERS.
/* INPUTS:
/*	STRING			STRING CONTAINING QUESTIONABLE CHARACTERS.
/*	VALID_CHARACTERS	LIST OF VALID CHARACTERS.
/* OUTPUTS:
/*	FUNCTION VALUE		TRUE IF STRING IS COMPOSED OF VALID CHARACTERS,
/*				FALSE OTHERWISE.
/* WRITTEN: M.V.GRIESHABER
*/
   char string[];
   char valid_characters[];
   {
   int i;			/* LOOP INDEX: WHICH CHAR IN STRING. */
   int j;			/* LOOP INDEX: WHICH CHAR IN VALID CHARS. */
   int good_char;		/* FLAG: WHETHER CHAR IS VALID OR NOT. */

   for (i=0; imaximum))
            fprintf(stderr,"Error: Response out of range.\n\n");
         else
            {
            *number=candidate;
            return(VALID_ENTRY);
            }
         }
      }
   }
Modified: Tue Apr 14 16:00:00 1992 GMT
Page accessed 7206 times since Sat Apr 17 21:32:57 1999 GMT