home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / LharcUUCP0_22.lha / LharcUUCP / source / ConfigFiles.c next >
Encoding:
C/C++ Source or Header  |  1993-12-15  |  8.2 KB  |  249 lines

  1. /*
  2.  *      $Filename: ConfigFiles.c $
  3.  *      $Revision: 0.4 $
  4.  *      $Date: 1993/12/15 20:08:37 $
  5.  *
  6.  *      Copyright (C) 1993 by Peter Simons <simons@peti.GUN.de>
  7.  *
  8.  *      This program is free software; you can redistribute it and/or
  9.  *      modify it under the terms of the GNU General Public License as
  10.  *      published by the Free Software Foundation; either version 2 of
  11.  *      the License, or (at your option) any later version.
  12.  *
  13.  *      This program is distributed in the hope that it will be useful,
  14.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  *      General Public License for more details.
  17.  *
  18.  *      You should have received a copy of the GNU General Public License
  19.  *      along with this program; if not, write to the Free Software
  20.  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  *
  23.  *      $Id: ConfigFiles.c,v 0.4 1993/12/15 20:08:37 simons Exp $
  24.  *
  25.  * ------------------------------- log history ----------------------------
  26.  * $Log: ConfigFiles.c,v $
  27.  * Revision 0.4  1993/12/15  20:08:37  simons
  28.  * The buffer GetConfigEntry() returns now isn't static anymore!!
  29.  *
  30.  * Revision 0.3  1993/12/11  14:22:18  simons
  31.  * Added file-locking.
  32.  *
  33.  * Revision 0.2  1993/12/11  14:16:44  simons
  34.  * Removed the netsupport.library specific stuff and added LoadFile().
  35.  *
  36.  * Revision 0.1  1993/12/11  14:00:27  simons
  37.  * Taken from the netsupport.library. Several changes will be necessary
  38.  * to make it work with LharcUUCP.
  39.  */
  40.  
  41.  
  42. /**************************************************************************
  43.  *                                                                        *
  44.  * SEKTION: Labels, Macros, Switches, Structures                          *
  45.  *                                                                        *
  46.  **************************************************************************/
  47.  
  48. /************************************* Includes ***********/
  49. #include <string.h>
  50. #include <stdlib.h>
  51.  
  52. #include <dos/dos.h>
  53. #include <proto/dos.h>
  54.  
  55. /************************************* Defines ************/
  56.  
  57. /************************************* Prototypes *********/
  58. char * GetConfigEntry(char * , char * , char * );
  59. char * LoadUUConfig(void);
  60. char * GetKeyword(char * , char * );
  61. char * GetKeywordLine(char * , char * );
  62. char * InterpretKeywordLine(char * );
  63. char * LoadFile(char * );
  64.  
  65. void LockFile(char *);
  66. int FileIsLocked(char *);
  67. void UnLockFile(char *);
  68. void UnLockFiles(void);
  69.  
  70. /************************************* global variables ***/
  71.  
  72. static const char __RCSId[] = "$Id: ConfigFiles.c,v 0.4 1993/12/15 20:08:37 simons Exp $";
  73.  
  74. /**************************************************************************
  75.  *                                                                        *
  76.  * SEKTION: main functions                                                *
  77.  *                                                                        *
  78.  **************************************************************************/
  79.  
  80. char *GetConfigEntry(char *filename, char *keyword, char *def)
  81. {
  82.         char *parameter, *filebuffer;
  83.  
  84.         /* If no filename is given, we assume the UUCP main config
  85.          * file.
  86.          */
  87.  
  88.         if (filename) {
  89.                 LockFile(filename);
  90.                 filebuffer = LoadFile(filename);
  91.                 UnLockFile(filename);
  92.         }
  93.         else
  94.                 filebuffer = LoadUUConfig();
  95.  
  96.  
  97.         /* If the file has been loaded successfully, find the keyword
  98.          * and returen the parameter.
  99.          */
  100.  
  101.         if (filebuffer) {
  102.                 if (parameter = GetKeywordLine(keyword, filebuffer))
  103.                         parameter = InterpretKeywordLine(parameter);
  104.  
  105.                 free(filebuffer);   /* free filebuffer */
  106.         }
  107.  
  108.         return (parameter) ? parameter : def;
  109. }
  110.  
  111.  
  112.  
  113. /**************************************************************************
  114.  *                                                                        *
  115.  * SEKTION: internal functions                                            *
  116.  *                                                                        *
  117.  **************************************************************************/
  118.  
  119. char *LoadUUConfig(void)
  120. {
  121.         char *filebuffer;
  122.  
  123.  
  124.         LockFile("S:UUConfig");
  125.         filebuffer = LoadFile("S:UUConfig");
  126.         UnLockFile("S:UUConfig");
  127.  
  128.         if (!filebuffer) {
  129.                 LockFile("UULIB:Config");
  130.                 filebuffer = LoadFile("UULIB:Config");
  131.                 UnLockFile("UULIB:Config");
  132.         }
  133.  
  134.         return filebuffer;
  135. }
  136.  
  137.  
  138. char * GetKeyword(char *keyword, char *buffer)
  139. {
  140.         char *parameter;
  141.  
  142.         if (parameter = GetKeywordLine(keyword, buffer))
  143.                 parameter = InterpretKeywordLine(parameter);
  144.  
  145.  
  146.         /* If GetKeywordLine() didn't find the keyword, return a pointer
  147.          * to the provided default. */
  148.  
  149.         if (!(*parameter))
  150.                 parameter = &keyword[strlen(keyword)+1];
  151.  
  152.         return parameter;
  153. }
  154.  
  155.  
  156. char * GetKeywordLine(char *keyword, char *buffer)
  157. {
  158.         int keywdlen;
  159.  
  160.         if (!(keywdlen = strlen(keyword)))
  161.                 return NULL;            /* no keyword provided */
  162.  
  163.         while (strnicmp(keyword, buffer, keywdlen)) {
  164.                 while(*buffer != '\0' && *buffer != '\n')
  165.                         buffer++;       /* search for '\0' or '\n' */
  166.  
  167.                 if (!(*buffer))         /* if '\0', break loop */
  168.                         break;
  169.  
  170.                 buffer++;       /* skip '\n' and compare next line */
  171.         }
  172.  
  173.         return (*buffer) ? buffer : NULL;
  174. }
  175.  
  176. char * InterpretKeywordLine(char *keywordline)
  177. {
  178.         char *parameter;
  179.         int i;
  180.  
  181.         while (*keywordline != ' ' && *keywordline != '\t')
  182.                 keywordline++;  /* skip keyword */
  183.  
  184.         while (*keywordline == ' ' || *keywordline == '\t')
  185.                 keywordline++;  /* skip leading blanks or tabs */
  186.  
  187.         for (i = 0; keywordline[i] != '\n' && keywordline[i] != '\0'; i++)
  188.                 ;               /* find end of line */
  189.  
  190.         while (keywordline[i-1] == ' ' || keywordline[i-1] == '\t')
  191.                 i--;            /* remove trailing blanks or tabs */
  192.  
  193.         if (parameter = malloc(i+1)) {
  194.                 parameter[i] = '\0';    /* send end of string */
  195.                 while (i--)             /* copy string to keywordline */
  196.                         parameter[i] = keywordline[i];
  197.         }
  198.  
  199.         return parameter;
  200. }
  201.  
  202.  
  203.  /*
  204.   * LoadFile() -- determines the length of a given file, allocates the
  205.   * appropiate buffer and actually loads the file. If everything works,
  206.   * the address of the buffer is returned and the length of the file is
  207.   * available via IoErr().
  208.   *
  209.   * ATTENTION: A zero-byte is appended to the loaded file, to enable the
  210.   * usage of standard C string routines on the contents.
  211.   *
  212.   * The buffer should be freed after usage using free().
  213.   */
  214.  
  215. char *LoadFile(char *filename)
  216. {
  217.         BPTR fh, fh2;
  218.         struct FileInfoBlock *fib;
  219.         char *memblock, *filebuffer = NULL;
  220.         BOOL isfile = FALSE;
  221.         int readlen;
  222.  
  223.         if (fh = Lock(filename, ACCESS_READ)) {
  224.                 if (fib = malloc(sizeof(struct FileInfoBlock))) {
  225.                         if (Examine(fh, fib)) {
  226.                                 if (fh2 = OpenFromLock(fh)) {
  227.                                         isfile = TRUE;
  228.                                         if (memblock = malloc(fib->fib_Size + 1)) {
  229.                                                 if ((readlen = Read(fh2, memblock, fib->fib_Size)) != -1L) {
  230.                                                         memblock[readlen] = '\0';
  231.                                                         filebuffer = memblock;
  232.                                                         SetIoErr(readlen);
  233.                                                 }
  234.                                                 else
  235.                                                         free(memblock);
  236.                                         }
  237.                                         Close(fh2);
  238.                                 }
  239.                         }
  240.                         free(fib);
  241.                 }
  242.                 if (!(isfile))
  243.                         UnLock(fh);
  244.         }
  245.  
  246.         return filebuffer;
  247. }
  248.  
  249.