home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / config / config.c next >
C/C++ Source or Header  |  1996-05-25  |  6KB  |  293 lines

  1. /*
  2. ** Config.c
  3. **
  4. ** $VER: Config.c 1.0 (25.5.96)
  5. **
  6. ** Copyright (C) 1996, Adam Dawes
  7. **
  8. ** Refer to accompanying documentation for further details
  9. */
  10.  
  11.  
  12. #include <exec/exec.h>
  13. #include <clib/exec_protos.h>
  14.  
  15.  
  16. #include <stdio.h>
  17. #include <string.h>
  18.  
  19. #include "Config.h"
  20.  
  21.  
  22.  
  23. int WriteConfig(char *filename, char *section, char *item, char *data)
  24. {
  25.     struct FILE *cfgfile;
  26.     struct List cfglist;
  27.     char *cfgdata;
  28.  
  29.     char readbuffer[BUFFERSIZE];
  30.  
  31.     struct Node *worknode;
  32.  
  33.     int linecount = 0;
  34.     int insection = FALSE, sectionfound = FALSE, itemfound = FALSE;
  35.  
  36.  
  37.     init_list(&cfglist);
  38.  
  39.  
  40.     /* read existing cfg file (if it exists) */
  41.     cfgfile = fopen(filename,"r");
  42.     if (cfgfile != NULL)
  43.     {
  44.         while (fgets(readbuffer,BUFFERSIZE,cfgfile) != NULL)
  45.         {
  46.             readbuffer[strlen(readbuffer)-1] = '\0';        /* kill the trailing CR */
  47.             if (strlen(readbuffer) > 0)
  48.             {
  49.                 cfgdata = AllocVec(strlen(readbuffer),MEMF_PUBLIC);
  50.                 if (cfgdata == NULL)        /* allocation error */
  51.                 {
  52.                     fclose(cfgfile);            /* close the file */
  53.                     free_list(&cfglist);        /* free everything we've allocated so far */
  54.                     return(CFG_WRITE_FAIL);    /* return an error */
  55.                 }
  56.                 strcpy(cfgdata,readbuffer);
  57.                 if (add_list_item(&cfglist,cfgdata) == FALSE)    /* error allocating node? */
  58.                 {
  59.                     fclose(cfgfile);            /* close the file */
  60.                     FreeVec(cfgdata);            /* free this line of data */
  61.                     free_list(&cfglist);        /* free everything we've allocated so far */
  62.                     return(CFG_WRITE_FAIL);    /* return an error */
  63.                 }
  64.             }
  65.         }
  66.     }
  67.     fclose(cfgfile);
  68.  
  69.  
  70.  
  71.     /* rewrite config file from list, adding new entry at required position */
  72.     cfgfile = fopen(filename,"w");
  73.     if (cfgfile == NULL)
  74.     {
  75.         free_list(&cfglist);
  76.         return(CFG_WRITE_FAIL);
  77.     }
  78.  
  79.     worknode = cfglist.lh_Head;
  80.  
  81.     while (worknode != (struct Node*)&cfglist.lh_Tail)
  82.     {
  83.         if (*worknode->ln_Name == '[' && *(worknode->ln_Name + strlen(worknode->ln_Name) - 1) == ']')
  84.         {
  85.             if (insection == TRUE && itemfound == FALSE)
  86.             {
  87.                 /* we were in the section, but now we've found another. Therefore the data needs to be added to the previous section */
  88.                 fprintf(cfgfile,"%s = %s\n",item,data);
  89.             }
  90.  
  91.             fprintf(cfgfile,"%s%s\n",linecount==0 ? "" : "\n", worknode->ln_Name);
  92.             if (strnicmp(worknode->ln_Name+1,section,strlen(section)) == 0)
  93.             {
  94.                 insection = TRUE;
  95.                 sectionfound = TRUE;
  96.             }
  97.             else
  98.             {
  99.                 insection = FALSE;
  100.             }
  101.         }
  102.         else
  103.         {
  104.             if (insection == TRUE && strnicmp(worknode->ln_Name,item,strlen(item)) == 0 && strnicmp(worknode->ln_Name + strlen(item)," = ",3) == 0)
  105.             {
  106.                 fprintf(cfgfile,"%s = %s\n",item,data);
  107.                 itemfound = TRUE;
  108.             }
  109.             else
  110.             {
  111.                 fprintf(cfgfile,"%s\n",worknode->ln_Name);
  112.             }
  113.         }
  114.  
  115.         worknode = worknode->ln_Succ;
  116.         linecount += 1;
  117.     }
  118.  
  119.     if (insection == TRUE && itemfound == FALSE)
  120.     {
  121.         /* we were in the section, but now we've reached the end. Therefore the data needs to be added to the previous section */
  122.         fprintf(cfgfile,"%s = %s\n",item,data);
  123.     }
  124.  
  125.     if (sectionfound == FALSE)
  126.     {
  127.         /* couldn't find the section, so create it now */
  128.         fprintf(cfgfile,"%s[%s]\n",linecount==0 ? "" : "\n", section);
  129.         fprintf(cfgfile,"%s = %s\n",item,data);
  130.     }
  131.  
  132.     fclose(cfgfile);
  133.  
  134.  
  135.     /* clean up */
  136.     free_list(&cfglist);
  137.  
  138.     return(CFG_WRITE_SUCCESS);
  139. }
  140.  
  141.  
  142.  
  143. int WriteConfigNumber(char *filename, char *section, char *item, long data)
  144. {
  145.     char numbuf[20];
  146.  
  147.     sprintf(numbuf,"%d",data);
  148.  
  149.     return(WriteConfig(filename,section,item,numbuf));
  150. }
  151.  
  152.  
  153.  
  154.  
  155. int ReadConfig(char *filename, char *section, char *item, char *buffer, int bufsize, char *def)
  156. {
  157.     FILE *cfgfile;
  158.     char readbuffer[BUFFERSIZE];
  159.  
  160.     int insection = FALSE;
  161.  
  162.     /* First open the config file. If it can't be opened, return the default value. */
  163.     cfgfile = fopen(filename,"r");
  164.     if (cfgfile == NULL)
  165.     {
  166.         if (strlen(def) >= bufsize)
  167.         {
  168.             return(CFG_READ_FAIL);
  169.         }
  170.         else
  171.         {
  172.             strcpy(buffer,def);
  173.             return(CFG_READ_SUCCESS);
  174.         }
  175.     }
  176.  
  177.  
  178.     /* Now scan the file looking the the desired section and item */
  179.     while (fgets(readbuffer,BUFFERSIZE,cfgfile) != NULL)
  180.     {
  181.         readbuffer[strlen(readbuffer)-1] = '\0';        /* kill the trailing CR */
  182.         if (strlen(readbuffer) > 0)
  183.         {
  184.             if (readbuffer[0] == '[' && readbuffer[strlen(readbuffer)-1] == ']')
  185.             {
  186.                 if (strnicmp(&readbuffer[1],section,strlen(section)) == 0)
  187.                 {
  188.                     insection = TRUE;
  189.                 }
  190.                 else
  191.                 {
  192.                     insection = FALSE;
  193.                 }
  194.             }
  195.             else
  196.             {
  197.                 if (insection == TRUE && strnicmp(readbuffer,item,strlen(item)) == 0 && strnicmp(&readbuffer[strlen(item)]," = ",3) == 0)
  198.                 {
  199.                     if (strlen(&readbuffer[strlen(item)+3]) >= bufsize)
  200.                     {
  201.                         fclose(cfgfile);
  202.                         return(CFG_READ_FAIL);
  203.                     }
  204.                     else
  205.                     {
  206.                         fclose(cfgfile);
  207.                         strcpy(buffer,&readbuffer[strlen(item)+3]);
  208.                         return(CFG_READ_SUCCESS);
  209.                     }
  210.                 }
  211.             }
  212.         }
  213.     }
  214.  
  215.     fclose(cfgfile);
  216.  
  217.  
  218.     /* we've reached the end of the file without finding the requested item in */
  219.     /* the requested section, so we'll return the default value instead. */
  220.  
  221.     if (strlen(def) >= bufsize)
  222.     {
  223.         return(CFG_READ_FAIL);
  224.     }
  225.     else
  226.     {
  227.         strcpy(buffer,def);
  228.         return(CFG_READ_SUCCESS);
  229.     }
  230. }
  231.  
  232.  
  233.  
  234. extern long ReadConfigNumber(char *filename, char *section, char *item, long def)
  235. {
  236.     char numbuf[20], retbuf[20];
  237.     char *tail;
  238.  
  239.     sprintf(numbuf,"%d",def);
  240.  
  241.     ReadConfig(filename,section,item,retbuf,20,numbuf);
  242.  
  243.     return(strtol(retbuf,&tail,10));
  244. }
  245.  
  246.  
  247.  
  248.  
  249. void init_list(struct List *lst)
  250. {
  251.     lst->lh_Head = (struct Node*)&lst->lh_Tail;
  252.     lst->lh_Tail = 0;
  253.     lst->lh_TailPred = &lst->lh_Head;
  254.     lst->lh_Type = NT_TASK;
  255. }
  256.  
  257.  
  258.  
  259. int add_list_item(struct List *lst, char *item)
  260. {
  261.  
  262.     struct Node *worknode;
  263.  
  264.     worknode = AllocVec(sizeof(struct Node),MEMF_PUBLIC);
  265.     if (worknode == NULL)        /* allocation error */
  266.     {
  267.         return(FALSE);
  268.     }
  269.     worknode->ln_Name = item;
  270.     worknode->ln_Pri = 0;
  271.     worknode->ln_Type = NT_TASK;
  272.  
  273.     AddTail(lst,worknode);
  274.  
  275.     return(TRUE);
  276. }
  277.  
  278.  
  279. void free_list(struct List *lst)
  280. {
  281.     struct Node *worknode;
  282.  
  283.     while (lst->lh_Head != (struct Node*)&lst->lh_Tail)
  284.     {
  285.         worknode = lst->lh_Head;                /* get a pointer to this node */
  286.  
  287.         FreeVec(worknode->ln_Name);            /* free the name memory */
  288.  
  289.         RemHead(lst);                                /* remove from the list */
  290.         FreeVec(worknode);                        /* and free the node */
  291.     }
  292. }
  293.