home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / comm / cyberpager-1.5.lha / CyberPager / source / library / config.c < prev    next >
C/C++ Source or Header  |  1994-02-07  |  4KB  |  202 lines

  1. #include "memory.h"
  2.  
  3. #define TEMPLATE "LABEL/A,VALUE/F"
  4.   enum templateArgs {
  5.       ARG_LABEL,
  6.       ARG_VALUE,
  7.       ARG_sizeof
  8.   };
  9.  
  10. #define INPUT_BUFFER_SIZE (1024)
  11.  
  12. static ConfigEntry_t *__inline AllocConfigEntry(PagerHandle_t * ph, STRPTR label, STRPTR value)
  13. {
  14.     ConfigEntry_t *ce;
  15.  
  16.     if (!value)
  17.         value = "";
  18.  
  19.     if (ce = (ConfigEntry_t *) MyAllocVec(sizeof(ConfigEntry_t) + strlen(label) + strlen(value) + 2)) {
  20.         ObtainSemaphore(&ph->ph_Sema);
  21.  
  22.         ce->ce_Handle = ph;
  23.  
  24.         ce->ce_UseCount = 1;
  25.         AddTail(&ph->ph_ConfigList, &ce->ce_Node);
  26.  
  27.         strcpy(ce->ce_Data, value);
  28.         ce->ce_Node.ln_Name = &ce->ce_Data[strlen(ce->ce_Data) + 1];
  29.         strcpy(ce->ce_Node.ln_Name, label);
  30.         ReleaseSemaphore(&ph->ph_Sema);
  31.     }
  32.  
  33.     return ce;
  34. }
  35.  
  36. STRPTR __saveds __asm FindPagerConfig(register __a0 PagerHandle_t * ph, register __a1 STRPTR configEntryName)
  37. {
  38.     ConfigEntry_t *ce;
  39.     ULONG line = 0;
  40.     STRPTR inputBuffer, ptr;
  41.     struct RDArgs *ArgsPtr, *MyArgs;
  42.     char *ArgArray[ARG_sizeof];
  43.     BOOL leave;
  44.     BPTR fh;
  45.  
  46.     ObtainSemaphore(&ph->ph_Sema);
  47.  
  48.     /* first look to see if we have already read this config entry in */
  49.  
  50.     for (ce = (ConfigEntry_t *) ph->ph_ConfigList.lh_Head;
  51.          ce->ce_Node.ln_Succ;
  52.          ce = (ConfigEntry_t *) ce->ce_Node.ln_Succ)
  53.         if (stricmp(configEntryName, ce->ce_Node.ln_Name) == 0) {
  54.             ce->ce_UseCount++;
  55.             ReleaseSemaphore(&ph->ph_Sema);
  56.             return ce->ce_Data;
  57.         }
  58.  
  59.     /* sigh.  need to read the config file */
  60.  
  61.     ce = NULL;
  62.  
  63.     if (inputBuffer = MyAllocVec(INPUT_BUFFER_SIZE)) {
  64.         /* try and open the config file */
  65.  
  66.         if (fh = Open("pager:config", MODE_OLDFILE)) {
  67.             /* loop reading lines looking for the service desired */
  68.  
  69.             leave = FALSE;
  70.  
  71.             while (leave == FALSE && FGets(fh, inputBuffer, INPUT_BUFFER_SIZE - 1)) {
  72.                 line++;
  73.  
  74.                 /*
  75.                  * check to make sure the line isn't all
  76.                  * blank
  77.                  */
  78.  
  79.                 ptr = inputBuffer;
  80.                 while (isspace(*ptr))
  81.                     ptr++;
  82.  
  83.                 if (!*ptr)
  84.                     continue;
  85.  
  86.                 /*
  87.                  * check for comment character at beginning
  88.                  * of line
  89.                  */
  90.  
  91.                 if (*ptr == ';')
  92.                     continue;
  93.  
  94.                 /* setup to call ReadArgs() to parse the line */
  95.  
  96.                 if (MyArgs = (struct RDArgs *)AllocDosObject(DOS_RDARGS, TAG_DONE)) {
  97.                     MyArgs->RDA_Flags |= RDAF_NOPROMPT;
  98.                     MyArgs->RDA_Source.CS_Buffer = inputBuffer;
  99.                     MyArgs->RDA_Source.CS_Length = strlen(inputBuffer);
  100.                     MyArgs->RDA_Source.CS_CurChr = 0L;
  101.  
  102.                     memset((char *)ArgArray, 0, sizeof(ArgArray));
  103.                     if (ArgsPtr = ReadArgs(TEMPLATE, (LONG *)&ArgArray, MyArgs)) {
  104.                         if (stricmp(configEntryName, ArgArray[ARG_LABEL]) == 0) {
  105.  
  106.                             /*
  107.                              * we've found the
  108.                              * config entry so
  109.                              * allocate and fill
  110.                              * in a config
  111.                              * structure
  112.                              */
  113.                             leave = TRUE;
  114.  
  115.                             ce = AllocConfigEntry(ph, ArgArray[ARG_LABEL], ArgArray[ARG_VALUE]);
  116.                         }
  117.  
  118.                         FreeArgs(ArgsPtr);
  119.                     }
  120.                     else {
  121.                         Fault(IoErr(), NULL, inputBuffer, INPUT_BUFFER_SIZE - 1);
  122.                         ULog(ph, -1, "Error in config file - line %ld: %s", line, inputBuffer);
  123.                         leave = TRUE;
  124.                     }
  125.  
  126.                     FreeDosObject(DOS_RDARGS, MyArgs);
  127.                 }
  128.             }
  129.  
  130.             Close(fh);
  131.         }
  132.         else
  133.             ULog(ph, -1, "Couldn't open config file");
  134.  
  135.         MyFreeVec(inputBuffer);
  136.     }
  137.  
  138.     ReleaseSemaphore(&ph->ph_Sema);
  139.  
  140.     if (ce)
  141.         return ce->ce_Data;
  142.     else
  143.         return NULL;
  144. }
  145.  
  146. STRPTR __saveds __asm FindPagerConfigDefault(register __a0 PagerHandle_t * ph, register __a1 STRPTR configEntryName, register __a2 STRPTR defValue)
  147. {
  148.     ConfigEntry_t *ce;
  149.     STRPTR entry;
  150.  
  151.     if (!defValue)
  152.         defValue = "";
  153.  
  154.     if (entry = FindPagerConfig(ph, configEntryName))
  155.         return entry;
  156.     else {
  157.         ce = AllocConfigEntry(ph, configEntryName, defValue);
  158.  
  159.         if (ce)
  160.             return ce->ce_Data;
  161.         else
  162.             return NULL;
  163.     }
  164. }
  165.  
  166. void __saveds __asm FreePagerConfig(register __a0 STRPTR configEntry)
  167. {
  168.     ConfigEntry_t *ce;
  169.  
  170.     if (configEntry) {
  171.         ce = (ConfigEntry_t *) ((size_t) configEntry - offsetof(ConfigEntry_t, ce_Data));
  172.  
  173.         ObtainSemaphore(&ce->ce_Handle->ph_Sema);
  174.  
  175.         if (--ce->ce_UseCount == 0) {
  176.             Remove(&ce->ce_Node);
  177.             MyFreeVec(ce);
  178.         }
  179.  
  180.         ReleaseSemaphore(&ce->ce_Handle->ph_Sema);
  181.     }
  182. }
  183.  
  184. BOOL __saveds __asm PagerConfigYesNo(register __a0 STRPTR configEntry)
  185. {
  186.     UBYTE value;
  187.  
  188.     if (configEntry) {
  189.         while (isspace(*configEntry))
  190.             configEntry++;
  191.  
  192.         value = tolower(*configEntry);
  193.  
  194.         if (value == 'y' || value == 't' || atol(configEntry) == 1)
  195.             return TRUE;
  196.         else
  197.             return FALSE;
  198.     }
  199.     else
  200.         return FALSE;
  201. }
  202.