home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 25 / AACD 25.iso / AACD / Magazine / Backups / FBackNG / config.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-08-30  |  6.0 KB  |  305 lines

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