1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
/*
* DSH / dancer's shell or the distributed shell
* Copyright (C) 2001, 2002 Junichi Uekawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* A library to read dsh config file style data files.
*/
/**
The ugly function to do the config file reading.
It will read one non-commentline and return to the caller.
Return value of NULL indicates termination and/or error.
*/
static dshconfig_internal*
read_oneline (FILE* f, int delimiter)
{
/* read one line and return */
dshconfig_internal * d = malloc (sizeof dshconfig_internal);
char* s = NULL;
int size = 0;
int pos;
int i;
if (!d)
return 0;
while (getline (&s, &size, f) != -1)
{
if (pos = strchr(s, '#')) /* handle comments, # and not \# */
if (*(pos-1) != '\\')
*pos = 0;
if ((pos = strchr(s, delimiter)) == NULL) /* if the line has no delimiter, get another one */
{
continue;
}
*(pos++) = 0;
/* removing the space at end of title */
for (i = pos - 2; i > s; --i)
{
if (!isspace (*i))
break;
else
*i = 0;
}
/* removing the space in the beginning in title */
for (i = s; i < pos; ++i)
if (!isspace (*i))
break;
d->title=strdup(i);
/* removing the space at the end of data*/
for (i = strlen(pos) + pos; i > pos; --i)
{
if (!isspace (*i))
break;
else
*i = 0;
}
/* removing space at the beginning in data */
for (i = pos; i < pos + strlen (pos); ++i)
if (!isspace (*i))
break;
d->data=strdup(i);
free (s);
return d;
}
free (d);
if (s) free (s);
return 0;
}
/** reads a dsh config file, and load it up in memory
returns NULL when error.
*/
dshconfig *
open_dshconfig (FILE* file, char delimiter)
{
dshconfig * d = malloc (sizeof (dshconfig));
dshconfig_internal * t, * i ;
if (!d)
return NULL;
d->config = NULL;
while (t = read_oneline (file, delimiter))
{
if (d->config)
{
for (i=d->config; i->next; i=i->next);
i->next = t;
}
else
{
t->next = NULL;
d->config = t;
}
}
return d;
}
|