GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
parse_mon.c
Go to the documentation of this file.
1 /* parse_mon - parse monitorcap entry */
2 
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <grass/gis.h>
8 #include <grass/glocale.h>
9 #include <grass/raster.h>
10 #include <grass/monitors.h>
11 
12 static FILE *monitors = NULL;
13 static struct MON_CAP cap;
14 
15 static char *substr(char *, char *);
16 static int read_line(FILE *, char *, int);
17 
18 struct MON_CAP *R_parse_monitorcap(int field, char *key)
19 {
20  int rewound;
21  char line[1024];
22  char *p;
23  char file[500];
24  char *gisbase;
25 
26  gisbase = G_gisbase();
27 
28  rewound = 0;
29  if (!(field == MON_NEXT || field == MON_NAME ||
30  field == MON_PATH || field == MON_LINK || field == MON_CLOSE))
31  return (NULL);
32  if (monitors == NULL) {
33  sprintf(file, "%s/etc/monitorcap", gisbase);
34  if ((monitors = fopen(file, "r")) == NULL)
35  G_fatal_error("Unable to open %s", file);
36  }
37  else {
38  if (field == MON_CLOSE) {
39  fclose(monitors);
40  monitors = NULL;
41  return (NULL);
42  }
43  }
44  while (-1) {
45  if (read_line(monitors, line, sizeof line)) {
46  if (field == MON_NEXT)
47  return (NULL);
48  rewind(monitors);
49  if (read_line(monitors, line, sizeof line) || rewound)
50  return (NULL);
51  rewound = -1;
52  }
53  cap.path = cap.comment = cap.link = cap.tty = cap.where = NULL;
54  if ((cap.name = G_malloc(strlen(line) + 1)) == NULL)
55  return (NULL);
56  strcpy(cap.name, line);
57  if ((p = substr(":", cap.name)) != NULL) {
58  *p++ = '\0';
59  cap.path = p;
60  if ((p = substr(":", p)) != NULL) {
61  *p++ = '\0';
62  cap.comment = p;
63  if ((p = substr(":", p)) != NULL) {
64  *p++ = '\0';
65  cap.link = p;
66  if ((p = substr(":", p)) != NULL) {
67  *p++ = '\0';
68  cap.tty = p;
69  if ((p = substr(":", p)) != NULL) {
70  *p++ = 0;
71  cap.where = p;
72  if ((p = substr("\n", p)) != NULL)
73  *p = '\0';
74  }
75  }
76  }
77  }
78  }
79  if (cap.path == NULL || cap.link == NULL || cap.where == NULL ||
80  cap.tty == NULL || cap.comment == NULL)
81  G_free(cap.name);
82  else {
83  sprintf(line, "%s/%s", gisbase, cap.path);
84  cap.path = G_store(line);
85 
86  if (field == MON_NEXT ||
87  (field == MON_NAME && !strcmp(key, cap.name))
88  || (field == MON_PATH && !strcmp(key, cap.path))
89  || (field == MON_LINK && !strcmp(key, cap.link)))
90  return (&cap);
91  else
92  G_free(cap.name);
93  }
94  }
95 }
96 
97 /* read_line - read a line, possibly continued with a "\" into a buffer */
98 
99 static int read_line(FILE * file, /* file from which to read */
100  char *line, /* buffer in which to put it */
101  int size)
102 { /* size of buffer */
103  int length, full_line, eof, done;
104  char c, last_c;
105 
106  *line = '\0';
107  for (length = full_line = eof = 0; !full_line && !eof;) { /* one entire line at a time */
108  while (1) {
109  eof = (fgets(line + length, size - length - 1, file) == NULL);
110  if (eof)
111  break;
112  if (*(line + length) != '#')
113  break;
114  }
115  length = strlen(line) - 1;
116  if (*(line + length) == '\n') {
117  if (*(line + length - 1) == '\\')
118  --length;
119  else
120  full_line = -1;
121  }
122  else {
123  if (length != -1) {
124  fprintf(stderr, "error: input line too long\n");
125  full_line = -1;
126  last_c = c = ' ';
127  for (done = 0; !done;) {
128  if ((c = getc(file)) != EOF) {
129  if (c == '\n' && last_c != '\\')
130  done = -1;
131  else
132  last_c = c;
133  }
134  else
135  eof = done = -1;
136  }
137  }
138  }
139  }
140  return (eof);
141 }
142 
143 /* substr - find substring in a string. returns pointer to start of */
144 /* substring or NULL if not found */
145 
146 static char *substr(char *string, char *buffer)
147 {
148  int start, i, found;
149  char c;
150 
151  start = i = found = 0;
152  while ((c = *(buffer + start + i)) != '\0' && !found) {
153  if (c == *(string + i)) {
154  if (*(string + ++i) == '\0')
155  found = -1;
156  }
157  else {
158  start++;
159  i = 0;
160  }
161  }
162  if (found)
163  return (buffer + start);
164  else
165  return (NULL);
166 }
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
struct MON_CAP * R_parse_monitorcap(int field, char *key)
Definition: parse_mon.c:18
tuple gisbase
Definition: forms.py:59
tuple size
value.Bind(wx.EVT_TEXT, self.OnVolumeIsosurfMap)
Definition: tools.py:2334
return NULL
Definition: dbfopen.c:1394
fclose(fd)
#define file
char * G_gisbase(void)
top level module directory
Definition: gisbase.c:42
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.