File: parsecfg.c

package info (click to toggle)
cabber 0.4.0-test5-2.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 172 kB
  • ctags: 100
  • sloc: ansic: 1,421; makefile: 82
file content (90 lines) | stat: -rw-r--r-- 1,702 bytes parent folder | download | duplicates (2)
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include "list.h"

List CFG;

int
ReadCFGFile (char *filename)
{
  FILE *fp;
  char *buf;
  char *line;
  char *value;
  Ptrnode p = NULL;
  elementT key;

  CFG = NewList ();
  p = HeaderList (CFG);

  buf = malloc (255);

  if ((fp = fopen (filename, "r")) == NULL)
    {
      /* perror ("fopen (parsecfg.c:15)"); */
      fprintf (stderr, "You must have a $HOME/.cabberrc file!\n");
      exit(0);
      exit (EXIT_FAILURE);
    }

  while (fgets (buf, 255, fp) != NULL)
    {
      line = buf;

      while (isspace ((int) *line))
	line++;

      while ((strlen (line) > 0) && isspace ((int) line[strlen (line) - 1]))
	line[strlen (line) - 1] = '\0';

      if ((*line == '\n') || (*line == '\0') || (*line == '#'))
	continue;

      if ((strchr (line, '=') != NULL))
	{
	  value = strchr (line, '=');
	  *value = '\0';
	  value++;

	  while (isspace ((int) *value))
	    value++;

	  while ((strlen (line) > 0)
		 && isspace ((int) line[strlen (line) - 1]))
	    line[strlen (line) - 1] = '\0';

	  key = malloc (sizeof (element));
	  strcpy (key->jid, "CFG-FILE");
	  strcpy (key->name, line);
	  strcpy (key->resource, value);
	  strcpy (key->group, "CFG-FILE");
	  key->connected = -1;
	  InsertInList (key, p, CFG);
	  p = NextInList (p);

	  continue;
	}
      fprintf (stderr, "CFG: orphaned line \"%s\"\n", line);
    }
  return 1;
}

char *
CFGRead (char *key)
{
  Ptrnode p;
  elementT tkey;

  p = FirstInList (CFG);
  while (p != NULL)
    {
      tkey = RetrieveFromList (p);
      if (!strcmp (tkey->name, key))
	return tkey->resource;
      p = NextInList (p);
    }
  return NULL;
}