ARA ROOT v3.10 Software

AraWebPlotter/configLib/configLoad.cxx

00001 /* configLoad.c - Functions for loading configuration data */
00002 
00003 /* G.J.Crone, University College London */
00004 
00005 /*
00006  * Current CVS Tag:
00007  * $Header: /work1/anitaCVS/flightSoft/common/configLib/configLoad.c,v 1.3 2005/06/15 16:22:00 rjn Exp $
00008  */
00009  
00010 /* 
00011  * Modification History : DO NOT EDIT - MAINTAINED BY CVS 
00012  * $Log: configLoad.c,v $
00013  * Revision 1.3  2005/06/15 16:22:00  rjn
00014  * Fixed a lot of silly warnings
00015  *
00016  * Revision 1.2  2004/08/31 17:51:21  rjn
00017  * Stuck a load of syslog messages into the configLib stuff. Previosuly it used some strange wrapper around syslog, due to implementation issues on the operating system used by some parts of the MINOS daq.
00018  *
00019  * Revision 1.1  2004/08/31 15:48:09  rjn
00020  * Um... lots of additions. The biggest ones are switching from shared to static libraries, and the additon of configLib and kvpLib to read formatted config files. These two libraries were created by Gordon Crone (gjc@hep.ucl.ac.uk) and are in spirit released under the GPL license.
00021  * Also the first of the actually programs (Eventd) has been started. At the moment it just opens a socket to fakeAcqd. Other sockets and better handling of multiple sockets in socketLib are the next things on my to do list.
00022  *
00023  * Revision 1.10  2002/10/09 08:47:32  gjc
00024  * Downgrade "block not found" from error to warning.
00025  *
00026  * Revision 1.9  2002/07/17 15:57:44  gjc
00027  * Added missing fclose to configLoad (whoops!)
00028  *
00029  * Revision 1.8  2002/06/12 12:22:43  gjc
00030  * Changed loading message from info to debug on level 1
00031  *
00032  * Revision 1.7  2001/12/03 13:36:07  gjc
00033  * Removed duplicate lines from parseBlockList, presumambly left by wrong
00034  * edit of previous update conflict.
00035  *
00036  * Revision 1.6  2001/11/26 15:13:20  howcroft
00037  * some file format error fixed
00038  *
00039  * Revision 1.5  2001/11/19 15:17:35  howcroft
00040  * Removed comment in commet bug
00041  *
00042  * Revision 1.4  2001/11/08 11:39:55  howcroft
00043  * Fixed small bug in string termination in func:  parseBlockList
00044  *  
00045  *         blockName[nBlocks][length]='\0';
00046  *
00047  * Revision 1.3  2001/08/15 14:39:34  gjc
00048  * Fix silly bug:  forgot to switch off continuation flag!
00049  *
00050  * Revision 1.2  2001/07/27 12:51:07  gjc
00051  * Stricter checking in getTag.  New function configValidate. Extract
00052  * filespec expansion to separate file
00053  *
00054  * Revision 1.1  2001/07/24 13:36:44  gjc
00055  * First check in of new package
00056  *
00057  *
00058  *
00059  */ 
00060 
00061 /*
00062 DESCRIPTION
00063 <Insert a description of the file/module here>
00064 INCLUDE FILES: <place list of any relevant header files here>
00065 */
00066 
00067 /* includes */
00068 #include <stdio.h>
00069 #include <stdlib.h>
00070 #include <syslog.h>
00071 #include <sys/types.h>
00072 #include <string.h>
00073 #include <errno.h>
00074 
00075 #include "configLib/configLib.h"
00076 #include "kvpLib/keyValuePair.h"
00077 
00078 /* defines */
00079 
00080 /* typedefs */
00081 
00082 /* globals */   
00083 
00084 /* locals */
00085 static FILE* config ;
00086 static char blockName[MAX_BLOCKS][BLOCKNAME_MAX] ;
00087 static int nBlocks ;
00088 static int inBlock ;
00089 
00090 /* forward declarations */
00091 
00092 static ConfigErrorCode getTag (char* thisBlock)
00093 {
00094    int gotTag ;
00095    char* tPtr ;
00096    char buffer[RECLEN_MAX] ;
00097    int length ;
00098 
00099    gotTag = 0 ;
00100    while (!gotTag) {
00101       tPtr = fgets (buffer, sizeof (buffer), config) ;
00102       if (tPtr == NULL) {
00103          return (CONFIG_E_EOF) ;
00104       }
00105 
00106       if (buffer[0] == '<') {
00107          if (buffer[1] != '/') {
00108             /* block delimiter */
00109             tPtr = &buffer[1] ;
00110             length = 0 ;
00111             /* copy up to closing '>' */
00112             while ((*tPtr != '>') && (length < BLOCKNAME_MAX)) {
00113                thisBlock[length++] = *tPtr++ ;
00114             }
00115             /* terminate the string */
00116             thisBlock[length] = 0 ;
00117             gotTag = 1 ;
00118             inBlock = 1 ;
00119          }
00120          else {
00121             inBlock = 0 ;
00122          }
00123       } else if ((!inBlock) &&
00124                  !((buffer[0] == '/') && (buffer[1] == '/')) &&
00125                  (buffer[0] != '\n')) {
00126          return (CONFIG_E_UNNAMED) ;
00127       }
00128    }
00129    return (CONFIG_E_OK) ;
00130 }
00131 
00132 static int checkTag (
00133                      char* tag
00134                      )
00135 {
00136    int block ;
00137 
00138    /* Compare with list of blocks we're interested in */
00139    for (block = 0 ; block < nBlocks ; block++) {
00140       if (strcmp (tag, blockName[block]) == 0) {
00141          return (block) ;
00142       }
00143    }
00144    return (-1) ;
00145 }
00146 
00147 static int readBlock (void)
00148 {
00149    char buffer[RECLEN_MAX] ;
00150    char* tPtr ;
00151    int wantBlock ;
00152    int nitems ;
00153    //   char* errMessage ;
00154    int spaceLeft=0 ;
00155    int offset=0 ;
00156    int continuation ;
00157    int retCode ;
00158 
00159    continuation = 0 ;
00160    wantBlock = 1 ;
00161    while (wantBlock) {
00162       if (!continuation) {
00163          offset = 0 ;
00164          spaceLeft = sizeof (buffer) ;
00165       }
00166       tPtr = fgets (&buffer[offset], spaceLeft, config) ;
00167       if (tPtr == NULL)  {
00168          if (feof (config)) {
00169             syslog (LOG_ERR,"readBlock: End of file during read") ;
00170             retCode = CONFIG_E_EOF ;
00171          }
00172          else {
00173             syslog (LOG_ERR,"readBlock: Error %d reading config", errno) ;
00174             retCode = CONFIG_E_SYSTEM ;
00175          }
00176          return (retCode) ;
00177       }
00178 
00179       if (*tPtr == '<') {
00180          if (tPtr[1] == '/') {
00181             wantBlock = 0 ;
00182             inBlock = 0 ;
00183          }
00184          else {
00185             syslog (LOG_ERR,"Syntax error, nested blocks not supported") ;
00186             return (CONFIG_E_NESTING) ;
00187          }
00188       }
00189       else if ((tPtr[0] != 0) && !((tPtr[0] == '/') && (tPtr[1] == '/'))) {
00190          offset = strlen (buffer) - 1 ;
00191          /* Ignore newline if there is one */
00192          if (buffer[offset] == '\n') {
00193             buffer[offset] = 0 ;
00194             offset-- ;
00195          }
00196          /* Check for continuation mark */
00197          if (buffer[offset] == '\\') {
00198             continuation = 1 ;
00199          }
00200          else {
00201             nitems = kvpParse (buffer) ;
00202             if (nitems < 0) {
00203 /*               errMessage = kvpErrorString (kvpError()) ;*/
00204                syslog (LOG_ERR,"configLoad: kvpParse error: %s, parsing <%s>",
00205                          kvpErrorString (kvpError()), buffer) ;
00206                return (CONFIG_E_KVP) ;
00207             }
00208             continuation = 0 ;
00209          }
00210       }
00211    }
00212    return (CONFIG_E_OK) ;
00213 }
00214 
00215 
00216 static int parseBlockList (char* blockList)
00217 {
00218    int length ;
00219    int nBlocks ;
00220    int offset ;
00221 
00222    /* Split blockList into separate strings */
00223    length = 0 ;
00224    nBlocks = 0 ;
00225    for (offset = 0 ; offset < strlen (blockList) + 1 ; offset++) {
00226       if ((blockList[offset] != ',') && (blockList[offset] != 0)) {
00227          blockName[nBlocks][length] = blockList[offset] ;
00228          length++ ;
00229       }
00230       else {
00231          /*hack by clfh*/
00232          blockName[nBlocks][length]='\0';
00233          length = 0 ;
00234          nBlocks++ ;
00235       }
00236    }
00237    return (nBlocks) ;
00238 }
00239 
00240 
00241 /********************************************************************
00242 *
00243 * configLoad - Load configuration parameters from file
00244 *
00245 * <Insert longer description here>
00246 *
00247 * RETURNS: 0 => success, -1 => error
00248 *
00249 */
00250 ConfigErrorCode configLoad (
00251                             const char* fileName,   /* Name of config file */
00252                             const char* blockList   /* Blocks we're interested in */
00253                             )
00254 {
00255    char thisBlock[BLOCKNAME_MAX] ;
00256    int foundBlock[MAX_BLOCKS] ;
00257    int wantBlock ;
00258    int blocksRead ;
00259    int block ;
00260    int status;
00261    //   ConfigErrorCode retVal ;
00262    char* fileSpec ;
00263 
00264    nBlocks = parseBlockList ((char*)blockList) ;
00265 
00266    blocksRead = 0 ;
00267    for (block = 0 ; block < nBlocks ; block++) {
00268       foundBlock[block] = 0 ;
00269    }
00270 
00271    fileSpec = configFileSpec ((char*)fileName) ;
00272    syslog (LOG_DEBUG,"configLoad Reading params from: %s", fileSpec) ;
00273 
00274    config = fopen (fileSpec, "r") ;
00275    if (config != NULL) {
00276       blocksRead = 0 ;
00277       do {
00278          /* Skip to block that we want */
00279          wantBlock = 0 ;
00280          inBlock = 0 ;
00281          status = 0 ;
00282          while (!wantBlock && (status == 0)) {
00283             status = getTag (thisBlock) ;
00284             if (status == CONFIG_E_OK) {
00285                block = checkTag (thisBlock) ;
00286                if (block >= 0) {
00287                   foundBlock[block] = 1 ;
00288                   wantBlock = 1 ;
00289                }
00290             }
00291             else if (status == CONFIG_E_EOF) {
00292                status = CONFIG_E_SECTION ;
00293                for (block = 0 ; block < nBlocks ; block++) {
00294                   if (!foundBlock[block]) {
00295                      syslog (LOG_WARNING,"<%s> block not found", blockName[block]) ;
00296                   }
00297                }
00298             }
00299          }
00300 
00301          if (wantBlock) {
00302             status = readBlock () ;
00303             blocksRead++ ;
00304          }
00305 
00306       } while ((blocksRead < nBlocks) && (status == CONFIG_E_OK));
00307       fclose (config) ;
00308    }
00309    else {
00310       if (errno == ENOENT) {
00311          syslog (LOG_ERR,"configLoad: %s not found", fileSpec) ;
00312          status = CONFIG_E_NOFILE ;
00313       }
00314       else {
00315          syslog (LOG_ERR,"configLoad: error %d opening %s", errno, fileSpec) ;
00316          status = CONFIG_E_SYSTEM ;
00317       }
00318    }
00319 
00320    return ConfigErrorCode(status) ;
00321 }
00322 
00323 
00324 
00325 ConfigErrorCode configLoadFullPath (
00326                             char* fileName,   /* Name of config file */
00327                             char* blockList   /* Blocks we're interested in */
00328                             )
00329 {
00330    char thisBlock[BLOCKNAME_MAX] ;
00331    int foundBlock[MAX_BLOCKS] ;
00332    int wantBlock ;
00333    int blocksRead ;
00334    int block ;
00335    int status ;
00336 //   char* fileSpec ;
00337 
00338    nBlocks = parseBlockList (blockList) ;
00339 
00340    blocksRead = 0 ;
00341    for (block = 0 ; block < nBlocks ; block++) {
00342       foundBlock[block] = 0 ;
00343    }
00344 
00345 //   fileSpec = configFileSpec (fileName) ;
00346    syslog (LOG_DEBUG,"configLoad Reading params from: %s", fileName) ;
00347 
00348    config = fopen (fileName, "r") ;
00349    if (config != NULL) {
00350       blocksRead = 0 ;
00351       do {
00352          /* Skip to block that we want */
00353          wantBlock = 0 ;
00354          inBlock = 0 ;
00355          status = 0 ;
00356          while (!wantBlock && (status == 0)) {
00357             status = getTag (thisBlock) ;
00358             if (status == CONFIG_E_OK) {
00359                block = checkTag (thisBlock) ;
00360                if (block >= 0) {
00361                   foundBlock[block] = 1 ;
00362                   wantBlock = 1 ;
00363                }
00364             }
00365             else if (status == CONFIG_E_EOF) {
00366                status = CONFIG_E_SECTION ;
00367                for (block = 0 ; block < nBlocks ; block++) {
00368                   if (!foundBlock[block]) {
00369                      syslog (LOG_WARNING,"<%s> block not found", blockName[block]) ;
00370                   }
00371                }
00372             }
00373          }
00374 
00375          if (wantBlock) {
00376             status = readBlock () ;
00377             blocksRead++ ;
00378          }
00379 
00380       } while ((blocksRead < nBlocks) && (status == CONFIG_E_OK));
00381       fclose (config) ;
00382    }
00383    else {
00384       if (errno == ENOENT) {
00385          syslog (LOG_ERR,"configLoad: %s not found", fileName) ;
00386          status = CONFIG_E_NOFILE ;
00387       }
00388       else {
00389          syslog (LOG_ERR,"configLoad: error %d opening %s", errno, fileName) ;
00390          status = CONFIG_E_SYSTEM ;
00391       }
00392    }
00393 
00394    return ConfigErrorCode(status) ;
00395 }
00396 
00397 
00398 
00399 ConfigErrorCode configValidate (
00400                                 char* fileName   /* Name of config file */
00401                                 )
00402 {
00403    char thisBlock[BLOCKNAME_MAX] ;
00404    int blocksRead ;
00405    int status ;
00406    char* fileSpec ;
00407    KvpErrorCode errCode ;
00408 
00409 
00410    blocksRead = 0 ;
00411 
00412    fileSpec = configFileSpec (fileName) ;
00413 
00414    config = fopen (fileSpec, "r") ;
00415    if (config != NULL) {
00416       blocksRead = 0 ;
00417       kvpReset () ;
00418       do {
00419         status = getTag (thisBlock) ;
00420         if (status == CONFIG_E_OK) {
00421            printf ("Validating block <%s>\n", thisBlock) ;
00422            status = readBlock () ;
00423            if (status == CONFIG_E_OK) {
00424               printf ("Block <%s> validated OK\n", thisBlock) ;
00425            }
00426            else if (status == CONFIG_E_KVP) {
00427               errCode = kvpError () ;
00428               printf ("Error %d (%s) from kvpParse\n",
00429                       errCode, kvpErrorString (errCode)) ;
00430            }
00431            kvpReset () ;
00432            blocksRead++ ;
00433         }
00434         else if (status == CONFIG_E_EOF) {
00435           printf ("Read %d blocks of configuration from %s\n",
00436                   blocksRead, fileSpec) ;
00437           return (CONFIG_E_OK) ;
00438         }
00439       } while (status == CONFIG_E_OK) ;
00440    }
00441    else {
00442       if (errno == ENOENT) {
00443          printf ("configValidate: %s not found", fileSpec) ;
00444          status = CONFIG_E_NOFILE ;
00445       }
00446       else {
00447          printf ("configValidate: error %d opening %s", errno, fileSpec) ;
00448          status = CONFIG_E_SYSTEM ;
00449       }
00450    }
00451    return ConfigErrorCode(status) ;
00452 }
00453 
00454 
00455 
00456 /********************************************************************
00457 *
00458 * readBlocks - Read block names from file
00459 *
00460 * <Insert longer description here>
00461 *
00462 * RETURNS: 0 => success, -1 => error
00463 *
00464 */
00465 ConfigErrorCode readBlocks(char *fileName,char blockList[MAX_BLOCKS][BLOCKNAME_MAX],int *numBlocks)
00466 {
00467    int status ;
00468    char* fileSpec ;
00469    char thisBlock[BLOCKNAME_MAX] ;
00470    int blockNum=0;
00471    fileSpec = configFileSpec (fileName) ;
00472    config = fopen (fileSpec, "r") ;
00473    
00474    if (config != NULL) {
00475       do {
00476           status=0;
00477           status = getTag (thisBlock) ;
00478           if (status == CONFIG_E_OK) {
00479               strncpy(blockList[blockNum],thisBlock,BLOCKNAME_MAX);
00480               blockNum++;
00481           }
00482       } while ( status == CONFIG_E_OK);
00483       fclose (config) ;
00484    }
00485    else {
00486       if (errno == ENOENT) {
00487          syslog (LOG_ERR,"readBlocks: %s not found", fileSpec) ;
00488          status = CONFIG_E_NOFILE ;
00489       }
00490       else {
00491          syslog (LOG_ERR,"readBlocks: error %d opening %s", errno, fileSpec) ;
00492          status = CONFIG_E_SYSTEM ;
00493       }
00494    }
00495    *numBlocks=blockNum;
00496    return ConfigErrorCode(status) ;
00497 }

Generated on Tue Jul 16 16:58:01 2013 for ARA ROOT v3.10 Software by doxygen 1.4.7