"Fossies" - the Fresh Open Source Software Archive

Member "epstool-3.08/src/cprofile.c" (10 Jun 2005, 9756 Bytes) of package /linux/misc/old/ghost/ghostgum/epstool-3.08-os2.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* Copyright (C) 1993-2001, Ghostgum Software Pty Ltd.  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 /* $Id: cprofile.c,v 1.3 2002/05/29 11:39:43 ghostgum Exp $ */
   19 /* Common profile (INI file) routines */
   20 
   21 /* Windows provides good Profile functions.
   22  * OS/2 provides binary profile functions which don't allow 
   23  * easy editing of the INI file.
   24  * Unix doesn't have generic profile functions.
   25  * 
   26  * To keep things consistent, these profile routines write
   27  * text INI files compatible with Windows.
   28  * Hand editing is easy.
   29  *
   30  * profile_open() reads the entire profile into memory.
   31  * For efficiency, try to avoid repeated opening and closing the profile.
   32  */
   33 
   34 #include <stdio.h>
   35 #include <stdlib.h>
   36 #include <string.h>
   37 #include "cplat.h"
   38 #define DEFINE_CPROFILE
   39 #include "cprofile.h"
   40 
   41 #ifndef min
   42 #define min(a,b) ((a) < (b) ? (a) : (b))
   43 #endif
   44 
   45 /* free keys in the section, but not the section itself */
   46 static void
   47 profile_free_section(prfsection *section)
   48 {
   49 prfentry *pe, *ne;
   50     pe = section->entry;
   51     while (pe) { /* free this entry */
   52         if (pe->name)
   53         free(pe->name);
   54         if (pe->value)
   55         free(pe->value);
   56         ne = pe->next;
   57         free(pe);
   58         pe = ne;
   59     }
   60     if (section->name)
   61         free(section->name);
   62 }
   63 
   64 static PROFILE *
   65 profile_cleanup(PROFILE *prf)
   66 {
   67 prfsection *ps, *ns;
   68     if (prf == (PROFILE *)NULL)
   69         return NULL;
   70     if (prf->file)
   71         fclose(prf->file);
   72     ps = prf->section;
   73     while (ps) {  /* free this section */
   74         profile_free_section(ps);
   75         ns = ps->next;
   76         free(ps);
   77         ps = ns;
   78     }
   79     if (prf->name)
   80         free(prf->name);
   81     free(prf);
   82     return NULL;
   83 }
   84 
   85 PROFILE *
   86 profile_open(LPCTSTR filename)
   87 {
   88 char line[256];
   89 PROFILE *prf;
   90 prfsection *ps, *ns;
   91 prfentry *pe, *ne;
   92 char *p;
   93 int len;
   94 
   95     if ( (prf = (PROFILE *)malloc(sizeof(PROFILE))) == (PROFILE *)NULL )
   96         return (PROFILE *)NULL;
   97     prf->changed = FALSE;
   98     prf->section = NULL;
   99         len = (int)cslen(filename)+1;
  100     if ( (prf->name = (TCHAR *)malloc(len*sizeof(TCHAR))) == (TCHAR *)NULL )
  101         return profile_cleanup(prf);
  102     csncpy(prf->name, filename, len);
  103     if ( (prf->file = csfopen(filename, TEXT("r"))) == (FILE *)NULL )
  104         return prf; /* file doesn't exist - we may be creating it */
  105     ps = ns = NULL;
  106     pe = ne = NULL;
  107     while (fgets(line, sizeof(line), prf->file)) {
  108         if (line[0] == '[') { /* new section */
  109             if ( (ns = (prfsection *)malloc(sizeof(prfsection))) 
  110                 == (prfsection *)NULL )
  111                 return profile_cleanup(prf);
  112             ns->name = NULL;
  113             ns->entry = NULL;
  114             ns->next = NULL;
  115             if (ps)
  116                 ps->next = ns;
  117             else
  118                 prf->section = ns;
  119         ps = ns;
  120         pe = NULL;
  121             if ( (p = strchr(line+1, ']')) != (char *)NULL )
  122                 *p = '\0';
  123         len = (int)strlen(line);
  124             if ( (ns->name = (char *)malloc(len)) == (char *)NULL )
  125                 return profile_cleanup(prf);
  126             strncpy(ns->name, line+1, len);
  127         }
  128         else {  /* new entry */
  129             if (ns == (prfsection *)NULL)
  130                 continue;
  131             if ( (p = strchr(line, '\n')) != (char *)NULL )
  132                 *p = '\0';
  133             if (line[0] == '\0')
  134             continue;
  135             if ( (ne = (prfentry *)malloc(sizeof(prfentry))) 
  136                 == (prfentry *)NULL )
  137                 return profile_cleanup(prf);
  138             ne->name = NULL;
  139             ne->value = NULL;
  140             ne->next = NULL;
  141             if (pe)
  142                 pe->next = ne;
  143             else
  144                 ns->entry = ne;
  145         pe = ne;
  146             if (line[0] == ';') { /* comment line */
  147             len = (int)strlen(line)+1;
  148                 if ( (ne->value = (char *)malloc(len)) == (char *)NULL )
  149                     return profile_cleanup(prf);
  150                 strncpy(ne->value, line, len);
  151             }
  152             else {  /* a real entry */
  153                 strtok(line, "=");
  154             len = (int)strlen(line)+1;
  155                 if ( (ne->name = (char *)malloc(len)) == (char *)NULL )
  156                     return profile_cleanup(prf);
  157                 strncpy(ne->name, line, len);
  158             p = line + strlen(line) + 1;
  159 /*
  160                 if ( (p = strtok(NULL, "=")) == (char *)NULL )
  161                     continue;
  162 */
  163             len = (int)strlen(p)+1;
  164                 if ( (ne->value = (char *)malloc(len)) == (char *)NULL )
  165                     return profile_cleanup(prf);
  166                 strncpy(ne->value, p, len);
  167             }
  168         }
  169     }
  170     fclose(prf->file);
  171     prf->file = NULL;
  172     return prf;
  173 }
  174 
  175 int 
  176 profile_read_string(PROFILE *prf, const char *section, const char *entry, 
  177     const char *def, char *buffer, int len)
  178 {
  179 prfsection *ps;
  180 prfentry *pe;
  181 int count;
  182 int slen;
  183 char *p;
  184     if (prf == (PROFILE *)NULL)
  185         return 0;
  186     if (buffer == (char *)NULL)
  187         return 0;
  188     ps = prf->section;
  189     if (section == NULL) {
  190         /* return all section names */
  191         count = 0;
  192         p = buffer;
  193         *p = '\0';
  194         while (ps) {
  195         slen = (int)strlen(ps->name)+ 1;
  196         if ( ps->name && ((slen + 2 + count) < len) ) {
  197             strncpy(p, ps->name, slen);
  198             count += (int)strlen(ps->name) + 1;
  199             p = buffer + count;
  200         }
  201             ps = ps->next;
  202         }
  203         *p = '\0';
  204         return count;
  205     }
  206     while (ps) {
  207         if (strcmp(ps->name, section) == 0)
  208             break;
  209         ps = ps->next;
  210     }
  211     if (ps == (prfsection *)NULL) {
  212         strncpy(buffer, def, len); 
  213         return min((int)strlen(buffer), len);
  214         }
  215     /* found section */
  216     pe = ps->entry;
  217     if (entry == NULL) {
  218         /* return all entry names */
  219         count = 0;
  220         p = buffer;
  221         *p = '\0';
  222         while (pe) {
  223         slen = (int)strlen(pe->name)+1;
  224         if ( pe->name && ((int)(count + slen + 2) < len) ) {
  225             strncpy(p, pe->name, slen);
  226             count += (int)strlen(pe->name) + 1;
  227             p = buffer + count;
  228         }
  229             pe = pe->next;
  230         }
  231         *p = '\0';
  232         return count;
  233     }
  234     while (pe) {
  235         if (pe->name && (strcmp(pe->name, entry) == 0))
  236             break;
  237         pe = pe->next;
  238     }
  239     if ( (pe == (prfentry *)NULL) ||
  240          (pe->value == (char *)NULL) ) {
  241         strncpy(buffer, def, len); 
  242         return min((int)strlen(buffer), len);
  243     }
  244     /* return value */
  245         strncpy(buffer, pe->value, len);    /* got it! */
  246     return min((int)strlen(buffer), len);
  247 }
  248 
  249 BOOL
  250 profile_write_string(PROFILE *prf, const char *section, const char *entry, 
  251     const char *value)
  252 {
  253 prfsection *ps, *ns;
  254 prfentry *pe, *ne;
  255 int slen;
  256     if (prf == (PROFILE *)NULL)
  257         return FALSE;
  258     ns = prf->section;
  259     ps = NULL;
  260     while (ns) {
  261         if (strcmp(ns->name, section) == 0)
  262             break;
  263         ps = ns;
  264         ns = ns->next;
  265     }
  266     if (entry == (char *)NULL) {
  267         /* delete section */
  268         if (ns == (prfsection *)NULL)
  269         return TRUE;
  270         profile_free_section(ns);
  271         if (ps)
  272         ps->next = ns->next;
  273         else
  274         prf->section = ns->next;
  275         free(ns);
  276         prf->changed = TRUE;
  277         return TRUE;
  278     }
  279     if (ns == (prfsection *)NULL) {
  280         /* add section */
  281         if ( (ns = (prfsection *)malloc(sizeof(prfsection))) 
  282             == (prfsection *)NULL )
  283             return FALSE;
  284         ns->name = NULL;
  285         ns->entry = NULL;
  286         ns->next = NULL;
  287         if (ps)
  288             ps->next = ns;
  289         else
  290             prf->section = ns;
  291         ps = ns;
  292         pe = NULL;
  293         slen = (int)strlen(section)+1;
  294         if ( (ns->name = (char *)malloc(slen)) == (char *)NULL )
  295             return FALSE;
  296         strncpy(ns->name, section, slen);
  297         }
  298     ne = ns->entry;
  299     pe = NULL;
  300     while (ne) {
  301         if (ne->name && (strcmp(ne->name, entry) == 0))
  302             break;
  303         pe = ne;
  304         ne = ne->next;
  305     }
  306     if (ne == (prfentry *)NULL) {
  307         /* add new entry */
  308         if ( (ne = (prfentry *)malloc(sizeof(prfentry))) 
  309             == (prfentry *)NULL )
  310             return FALSE;
  311         ne->name = NULL;
  312         ne->value = NULL;
  313         ne->next = NULL;
  314         if (pe)
  315             pe->next = ne;
  316         else
  317             ns->entry = ne;
  318         pe = ne;
  319         slen = (int)strlen(entry)+1;
  320         if ( (ne->name = (char *)malloc(slen)) == (char *)NULL )
  321             return FALSE;
  322         strncpy(ne->name, entry, slen);
  323     }
  324     if (ne->value != (char *)NULL)
  325         free(ne->value);    /* release old value */
  326     if (value) { /* change value */
  327         slen = (int)strlen(value)+1;
  328         if ( (ne->value = (char *)malloc(slen)) == (char *)NULL )
  329         return FALSE;
  330         strncpy(ne->value, value, slen);
  331     }
  332     else { /* delete entry */
  333         free(ne->name);
  334         if (pe)
  335             pe->next = ne->next;
  336         else
  337         ns->entry = ne->next;
  338         free(ne);
  339     }
  340     prf->changed = TRUE;
  341     return TRUE;
  342 }
  343 
  344 BOOL
  345 profile_close(PROFILE *prf)
  346 {
  347 prfsection *ps;
  348 prfentry *pe;
  349     if (prf == (PROFILE *)NULL)
  350         return FALSE;
  351     if (prf->changed) {
  352         if ( (prf->file = csfopen(prf->name, TEXT("w"))) == (FILE *)NULL ) {
  353         profile_cleanup(prf);
  354             return FALSE;
  355         }
  356         ps = prf->section;
  357         while (ps) {
  358             if (ps->name)
  359             fprintf(prf->file, "[%s]\n", ps->name);
  360             pe = ps->entry;
  361             while (pe) {
  362             if (pe->name) {
  363                 if (pe->value)
  364                     fprintf(prf->file, "%s=%s\n", pe->name, pe->value);
  365                 else
  366                     fprintf(prf->file, "%s=\n", pe->name);
  367             }
  368             else {
  369                 if (pe->value)
  370                     fprintf(prf->file, "%s\n", pe->value);
  371             }
  372             pe = pe->next;
  373             }
  374         fprintf(prf->file, "\n");
  375             ps = ps->next;
  376         }
  377     }
  378     profile_cleanup(prf);
  379     return TRUE;
  380 }
  381