home *** CD-ROM | disk | FTP | other *** search
/ swCHIP 1991 January / swCHIP_95-1.bin / utility / gsview13 / src / gvcprf.c < prev    next >
C/C++ Source or Header  |  1995-12-09  |  9KB  |  324 lines

  1. /* Copyright (C) 1993, 1994, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of GSview.
  4.   
  5.   This program is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the GSview Free Public Licence 
  9.   (the "Licence") for full details.
  10.   
  11.   Every copy of GSview must include a copy of the Licence, normally in a 
  12.   plain ASCII text file named LICENCE.  The Licence grants you the right 
  13.   to copy, modify and redistribute GSview, but only under certain conditions 
  14.   described in the Licence.  Among other things, the Licence requires that 
  15.   the copyright notice and this notice be preserved on all copies.
  16. */
  17.  
  18. /* gvcprf.c */
  19. /* Profile (INI file) routines Common to PM and possibly Windows */
  20.  
  21. /* Windows provides good Profile functions */
  22. /* PM provides binary PM functions which don't allow easy editing */
  23. /* of the INI file.  Also emx08g crashed when calling PrfOpenProfile. */
  24. /* To keep things consistent, these profile routines should write */
  25. /* INI files compatible with Windows. */
  26. /* Since the INI files are plain text, they are easy to edit */
  27.  
  28. #ifdef _Windows
  29. #include "gvwin.h"
  30. #else
  31. #include "gvpm.h"
  32. #endif
  33.  
  34.  
  35. PROFILE *
  36. profile_cleanup(PROFILE *prf)
  37. {
  38. struct prfsection *ps, *ns;
  39. struct prfentry *pe, *ne;
  40.     if (prf == (PROFILE *)NULL)
  41.         return NULL;
  42.     if (prf->file)
  43.         fclose(prf->file);
  44.     ps = prf->section;
  45.     while (ps) {  /* free this section */
  46.         pe = ps->entry;
  47.         while (pe) { /* free this entry */
  48.         if (pe->name)
  49.             free(pe->name);
  50.         if (pe->value)
  51.             free(pe->value);
  52.         ne = pe->next;
  53.         free(pe);
  54.         pe = ne;
  55.         }
  56.         if (ps->name)
  57.             free(ps->name);
  58.         ns = ps->next;
  59.         free(ps);
  60.         ps = ns;
  61.     }
  62.     if (prf->name)
  63.         free(prf->name);
  64.     free(prf);
  65.     return NULL;
  66. }
  67.  
  68. PROFILE *
  69. profile_open(char *filename)
  70. {
  71. char line[256];
  72. PROFILE *prf;
  73. struct prfsection *ps, *ns;
  74. struct prfentry *pe, *ne;
  75. char *p;
  76.  
  77.     if ( (prf = (PROFILE *)malloc(sizeof(PROFILE))) == (PROFILE *)NULL )
  78.         return (PROFILE *)NULL;
  79.     prf->changed = FALSE;
  80.     prf->section = NULL;
  81.     if ( (prf->name = malloc(strlen(filename)+1)) == (char *)NULL )
  82.         return profile_cleanup(prf);
  83.     strcpy(prf->name, filename);
  84.     if ( (prf->file = fopen(filename, "r")) == (FILE *)NULL )
  85.         return prf;    /* file doesn't exist - we may be creating it */
  86.     ps = ns = NULL;
  87.     pe = ne = NULL;
  88.     while (fgets(line, sizeof(line), prf->file)) {
  89.         if (line[0] == '[') { /* new section */
  90.             if ( (ns = (struct prfsection *)malloc(sizeof(struct prfsection))) 
  91.                 == (struct prfsection *)NULL )
  92.                 return profile_cleanup(prf);
  93.             ns->name = NULL;
  94.             ns->entry = NULL;
  95.             ns->next = NULL;
  96.             if (ps)
  97.                 ps->next = ns;
  98.             else
  99.                 prf->section = ns;
  100.         ps = ns;
  101.         pe = NULL;
  102.             if ( (p = strchr(line+1, ']')) != (char *)NULL )
  103.                 *p = '\0';
  104.             if ( (ns->name = (char *)malloc(strlen(line))) == (char *)NULL )
  105.                 return profile_cleanup(prf);
  106.             strcpy(ns->name, line+1);
  107.         }
  108.         else {    /* new entry */
  109.             if (ns == (struct prfsection *)NULL)
  110.                 continue;
  111.             if ( (p = strchr(line, '\n')) != (char *)NULL )
  112.                 *p = '\0';
  113.             if (line[0] == '\0')
  114.             continue;
  115.             if ( (ne = (struct prfentry *)malloc(sizeof(struct prfentry))) 
  116.                 == (struct prfentry *)NULL )
  117.                 return profile_cleanup(prf);
  118.             ne->name = NULL;
  119.             ne->value = NULL;
  120.             ne->next = NULL;
  121.             if (pe)
  122.                 pe->next = ne;
  123.             else
  124.                 ns->entry = ne;
  125.         pe = ne;
  126.             if (line[0] == ';') { /* comment line */
  127.                 if ( (ne->value = (char *)malloc(strlen(line)+1)) == (char *)NULL )
  128.                     return profile_cleanup(prf);
  129.                 strcpy(ne->value, line);
  130.             }
  131.             else {    /* a real entry */
  132.                 strtok(line, "=");
  133.                 if ( (ne->name = (char *)malloc(strlen(line)+1)) == (char *)NULL )
  134.                     return profile_cleanup(prf);
  135.                 strcpy(ne->name, line);
  136.                 if ( (p = strtok(NULL, "=")) == (char *)NULL )
  137.                     continue;
  138.                 if ( (ne->value = (char *)malloc(strlen(p)+1)) == (char *)NULL )
  139.                     return profile_cleanup(prf);
  140.                 strcpy(ne->value, p);
  141.             }
  142.         }
  143.     }
  144.     fclose(prf->file);
  145.     prf->file = NULL;
  146.     return prf;
  147. }
  148.  
  149. int 
  150. profile_read_string(PROFILE *prf, char *section, char *entry, char *def, char *buffer, int len)
  151. {
  152. struct prfsection *ps;
  153. struct prfentry *pe;
  154. int count;
  155. char *p;
  156.     if (prf == (PROFILE *)NULL)
  157.         return 0;
  158.     if (buffer == (char *)NULL)
  159.         return 0;
  160.     ps = prf->section;
  161.     if (section == NULL) {
  162.         /* return all section names */
  163.         count = 0;
  164.         p = buffer;
  165.         *p = '\0';
  166.         while (ps) {
  167.         if ( ps->name && (count + strlen(ps->name) + 2 < len) ) {
  168.             strcpy(p, ps->name);
  169.             count += strlen(ps->name) + 1;
  170.             p = buffer + count;
  171.         }
  172.             ps = ps->next;
  173.         }
  174.         *p = '\0';
  175.         return count;
  176.     }
  177.     while (ps) {
  178.         if (strcmp(ps->name, section) == 0)
  179.             break;
  180.         ps = ps->next;
  181.     }
  182.     if (ps == (struct prfsection *)NULL) {
  183.         strncpy(buffer, def, len); 
  184.         return min(strlen(buffer), len);
  185.         }
  186.     /* found section */
  187.     pe = ps->entry;
  188.     if (entry == NULL) {
  189.         /* return all entry names */
  190.         count = 0;
  191.         p = buffer;
  192.         *p = '\0';
  193.         while (pe) {
  194.         if ( pe->name && (count + strlen(pe->name) + 2 < len) ) {
  195.             strcpy(p, pe->name);
  196.             count += strlen(pe->name) + 1;
  197.             p = buffer + count;
  198.         }
  199.             pe = pe->next;
  200.         }
  201.         *p = '\0';
  202.         return count;
  203.     }
  204.     while (pe) {
  205.         if (pe->name && (strcmp(pe->name, entry) == 0))
  206.             break;
  207.         pe = pe->next;
  208.     }
  209.     if ( (pe == (struct prfentry *)NULL) ||
  210.          (pe->value == (char *)NULL) ) {
  211.         strncpy(buffer, def, len); 
  212.         return min(strlen(buffer), len);
  213.     }
  214.     /* return value */
  215.         strncpy(buffer, pe->value, len);    /* got it! */
  216.     return min(strlen(buffer), len);
  217. }
  218.  
  219. BOOL
  220. profile_write_string(PROFILE *prf, char *section, char *entry, char *value)
  221. {
  222. struct prfsection *ps, *ns;
  223. struct prfentry *pe, *ne;
  224.     if (prf == (PROFILE *)NULL)
  225.         return FALSE;
  226.     ns = prf->section;
  227.     ps = NULL;
  228.     while (ns) {
  229.         if (strcmp(ns->name, section) == 0)
  230.             break;
  231.         ps = ns;
  232.         ns = ns->next;
  233.     }
  234.     if (ns == (struct prfsection *)NULL) {
  235.         /* add section */
  236.         if ( (ns = (struct prfsection *)malloc(sizeof(struct prfsection))) 
  237.             == (struct prfsection *)NULL )
  238.             return FALSE;
  239.         ns->name = NULL;
  240.         ns->entry = NULL;
  241.         ns->next = NULL;
  242.         if (ps)
  243.             ps->next = ns;
  244.         else
  245.             prf->section = ns;
  246.         ps = ns;
  247.         pe = NULL;
  248.         if ( (ns->name = (char *)malloc(strlen(section)+1)) == (char *)NULL )
  249.             return FALSE;
  250.         strcpy(ns->name, section);
  251.         }
  252.     ne = ns->entry;
  253.     pe = NULL;
  254.     while (ne) {
  255.         if (ne->name && (strcmp(ne->name, entry) == 0))
  256.             break;
  257.         pe = ne;
  258.         ne = ne->next;
  259.     }
  260.     if (ne == (struct prfentry *)NULL) {
  261.         /* add new entry */
  262.         if ( (ne = (struct prfentry *)malloc(sizeof(struct prfentry))) 
  263.             == (struct prfentry *)NULL )
  264.             return FALSE;
  265.         ne->name = NULL;
  266.         ne->value = NULL;
  267.         ne->next = NULL;
  268.         if (pe)
  269.             pe->next = ne;
  270.         else
  271.             ns->entry = ne;
  272.         pe = ne;
  273.         if ( (ne->name = (char *)malloc(strlen(entry)+1)) == (char *)NULL )
  274.             return FALSE;
  275.         strcpy(ne->name, entry);
  276.     }
  277.     if (ne->value != (char *)NULL)
  278.         free(ne->value);    /* release old value */
  279.     if ( (ne->value = (char *)malloc(strlen(value)+1)) == (char *)NULL )
  280.         return FALSE;
  281.     strcpy(ne->value, value);
  282.     prf->changed = TRUE;
  283.     return TRUE;
  284. }
  285.  
  286. BOOL
  287. profile_close(PROFILE *prf)
  288. {
  289. struct prfsection *ps;
  290. struct prfentry *pe;
  291.     if (prf == (PROFILE *)NULL)
  292.         return FALSE;
  293.     if (prf->changed) {
  294.         if ( (prf->file = fopen(prf->name, "w")) == (FILE *)NULL ) {
  295.         profile_cleanup(prf);
  296.             return FALSE;
  297.         }
  298.         ps = prf->section;
  299.         while (ps) {
  300.             if (ps->name)
  301.             fprintf(prf->file, "[%s]\n", ps->name);
  302.             pe = ps->entry;
  303.             while (pe) {
  304.             if (pe->name) {
  305.                 if (pe->value)
  306.                     fprintf(prf->file, "%s=%s\n", pe->name, pe->value);
  307.                 else
  308.                     fprintf(prf->file, "%s=\n", pe->name);
  309.             }
  310.             else {
  311.                 if (pe->value)
  312.                     fprintf(prf->file, "%s\n", pe->value);
  313.             }
  314.             pe = pe->next;
  315.             }
  316.         fprintf(prf->file, "\n");
  317.             ps = ps->next;
  318.         }
  319.     }
  320.     profile_cleanup(prf);
  321.     return TRUE;
  322. }
  323.  
  324.