From oleg@nrcbsa.bio.nrc.ca Thu May 23 08:52:29 1991 Return-Path: Received: from nrcnet0.nrc.ca by CS.UTK.EDU with SMTP (5.61++/2.5.1s-UTK) id AA26051; Thu, 23 May 91 08:52:24 -0400 Message-Id: <9105231251.AA28702@ nrcbsa.bio.nrc.ca> Date: Thu, 23 May 91 08:51:55 EDT From: Dr. Oleg Keselyov To: dongarra@cs.utk.edu Subject: task_env.shar Status: RO #--------------------------------CUT HERE------------------------------------- #! /bin/sh # # This is a shell archive. Save this into a file, edit it # and delete all lines above this comment. Then give this # file to sh by executing the command 'sh file'. The files # will be extracted into the current directory owned by # you with default permissions. # The archive contains a resource facility, or managing global "private" # parameters that specify "options". It help keep reasonable number of # arguments in function calls. # See comments to the task_env.h for more details. # To compile and link the program, service functions and header files from # the package serv.shar are needed. # The contents of the archive # # Header file # task_env.h Define the package # # Source code for the programs being submitted # task_env.c Package body echo 'x - task_env.h' sed 's/^X//' << '________This_Is_The_END________' > task_env.h X/* X ************************************************************************ X * X * Task environment handling X * X * X * Task environment area is a set of global (common) data. Any procedure X * within the active task can deposit a piece of information to be used X * by other procedure. And any procedure can request to retrieve information X * having been deposited under the given name. X * X * The present service is assumed to be used as task customisition tool. X * Root module of the task reads the configuration files and deposits X * data in the task environment area (e.g. no. of grids, output file name X * etc.). Other modules that need such an parameter request it from X * the present service. X * X * X ************************************************************************ X */ X X X#ifdef TASK_ENV_private X /* Private package data */ X Xtypedef struct _Env_Element { /* Environment area element */ X char * name; /* Name */ X char * value; /* Value */ X struct _Env_Element * next; /* Ptr to the next element */ X } X _ENV_ELEMENT; X X /* Ptr to the list of elements */ Xstatic _ENV_ELEMENT * Task_Environment_Area; X X#endif X X X /* Package procedures */ X X /* Deposit a string at the env area */ X /* Parameter string must be of the form */ Xvoid task_env_deposit( /* "name = value ;other comment" */ X const char * str X ); X X /* Retrive the value of a parameter */ X /* string by its name */ Xchar * task_env_retrieve( X const char * name /* Param. name */ X ); X /* Print the contents of the environment*/ X /* area */ Xvoid task_env_print(void); X X /* Write the environment area in the */ X /* specified file */ X /* Output is terminated by the string */ X /* given */ Xvoid task_env_write( X /*const char * terminator, String terminating the output*/ X /* FILE * filep File where to write */ X ); X X /* Read the environment area from the */ X /* file specified from the current */ X /* record to either specified termina- */ X /* ting string or EOF (whichever comes */ X /* first */ Xvoid task_env_read( X /*const char * terminator, String terminating the output*/ X /* FILE * filep File where to read from */ X ); ________This_Is_The_END________ if test `wc -l < task_env.h` -ne 73; then echo 'shar: task_env.h was damaged during transit (should have had 73 lines)' fi echo 'x - task_env.c' sed 's/^X//' << '________This_Is_The_END________' > task_env.c X/* X ************************************************************************ X * X * Task environment handling X * X * Package body X * X ************************************************************************ X */ X X#define TASK_ENV_private X#include "task_env.h" X X#include "assert.h" X#include X#include X X/* X *----------------------------------------------------------------------- X * Some service functions X */ X Xstatic void convert_to_uppercase(str) Xchar * str; /* Convert str to uppercase letters */ X{ X while ( *str ) X *str++ = toupper(*str); X} X X Xstatic _ENV_ELEMENT * find_element_by_name(name) Xchar * name; /* Must be uppercase */ X{ X register _ENV_ELEMENT * p = Task_Environment_Area; X X while( p != 0 ) X if( strcmp(p->name,name) == 0 ) X return p; /* Name has been found */ X else X p = p->next; /* Else try the next element */ X return 0; /* No element was found */ X} X X X /* Create a new element and append it to the */ X /* environment list */ Xstatic void create_element(name,value) Xconst char * name; Xconst char * value; X{ X register _ENV_ELEMENT * p = Task_Environment_Area; X register _ENV_ELEMENT * new; X X assure( (new = malloc(sizeof(_ENV_ELEMENT))), "No memory"); X new->name = name; X new->value = value; X new->next = 0; X X if( p == 0 ) /* Task environment was empty */ X { /* Put the first elem to it */ X Task_Environment_Area = new; X return; X } X X while( p->next != 0 ) X p = p->next; /* p will point to the last elem*/ X X p->next = new; /* Append new elem to the last */ X} X X X X/* X *----------------------------------------------------------------------- X * Deposit a parameter string in the task environment area X * X * Format X * "name = value ;other comment" X */ X Xvoid task_env_deposit(str) Xconst char * str; /* Param. string */ X{ X char * name; X char * value; X register char *p, *q; X register int i; X X /* Distinct and analyze name */ X if( (p=strchr(str,'=')) == 0 ) X _error("Equal sign was not found where expected in the string '%s'", X str); X X while( --p >= str && *p == ' ' ) /* Delete blanks between name */ X ; /* and equal sign */ X X if( (i= (++p - str)) <= 0 ) /* i is the name length */ X _error("No name found in string '%s'",str); X X assure( (name = calloc(i+1,sizeof(char))), "No memory"); X X strncpy(name,str,i); X/* convert_to_uppercase(name); */ X X /* Distinct and analyze value */ X assert( (q=strchr(str,'=')) ); X while( *++q == ' ' && *q != '\0' ) /* Find where a value starts */ X ; X if( *q == '\0' ) X _error("No value found in string '%s'",str); X X if( (p=strchr(q,';')) == 0 ) /* Locate p at the '\0' or */ X p = q + strlen(q); /* at the beginning of comment */ X X while( --p >= q && *p == ' ' ) /* Delete padding blanks */ X ; X X if( (i= (++p - q)) <= 0 ) /* i is the value length */ X _error("No value found in string '%s'",str); X X assure( (value = calloc(i+1,sizeof(char))), "No memory"); X X strncpy(value,q,i); X X /* Create a new _ENV_ELEMENT */ X if( find_element_by_name(name) ) X _error("Element '%s' is already in the task environment area",name); X X create_element(name,value); X} X X X X/* X *----------------------------------------------------------------------- X * Retrive the value of a parameter string by its name X * X */ X Xchar * task_env_retrieve(name) Xconst char * name; /* Param. name */ X{ X register _ENV_ELEMENT * p; X X if( (p=find_element_by_name(name)) == 0 ) X _error("No env string with the name '%s' was found",name); X X return p->value; X} X X X X/* X *----------------------------------------------------------------------- X * Print/Write/Read the contents of the environment area X * X */ X X#include "stdio.h" X Xvoid task_env_print() X{ X register _ENV_ELEMENT * p = Task_Environment_Area; X X if( p == 0 ) X { X message("\nTask environment area is empty\n\n"); X return; X } X X message("\n\n\t\t\tTask environment area\n\nName\t\t\tValue\n"); X while( p != 0 ) X { X message("%s\t\t%s\n",p->name,p->value); X p = p->next; X } X} X X /* Write the environment area in the */ X /* specified file */ X /* Output is terminated by the string */ X /* given */ Xvoid task_env_write(terminator,filep) Xconst char * terminator; /* String terminating the output*/ XFILE * filep; /* File where to write */ X{ X register _ENV_ELEMENT * p = Task_Environment_Area; X X if( p == 0 ) X { X message("\nTask environment area is empty\n\n"); X return; X } X X while( p != 0 ) X { X fprintf(filep,"%s = %s\n",p->name,p->value); X p = p->next; X } X fprintf(filep,"%s\n",terminator); X} X X /* Read the environment area from the */ X /* file specified from the current */ X /* record to either specified termina- */ X /* ting string or EOF (whichever comes */ X /* first */ Xvoid task_env_read(terminator,filep) Xconst char * terminator; /* String terminating the output*/ XFILE * filep; /* File where to read from */ X{ X char buffer [100]; X register char *p; X X for(;;) X { X fgets(buffer,sizeof(buffer)-1,filep); X if( feof(filep) ) X break; /* Exit on EOF */ X X if( buffer[0] == ';' ) X continue; /* Bypass the comment string */ X if( (p=strchr(buffer,'\n')) == 0 ) X _error("Too long string has been read\n\"%s\" ",buffer); X *p = '\0'; /* Replace \n by \0 */ X X if( strcmp(buffer,terminator) == 0 )/* Exit on terminator */ X break; X task_env_deposit(buffer); X } X X} ________This_Is_The_END________ if test `wc -l < task_env.c` -ne 232; then echo 'shar: task_env.c was damaged during transit (should have had 232 lines)' fi .