File: input.c

package info (click to toggle)
xemacs21-packages 2009.02.17.dfsg.1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 116,928 kB
  • ctags: 88,975
  • sloc: lisp: 1,232,060; ansic: 16,570; java: 13,514; xml: 6,477; sh: 4,611; makefile: 4,036; asm: 3,007; perl: 839; cpp: 500; ruby: 257; csh: 96; haskell: 93; awk: 49; python: 47
file content (339 lines) | stat: -rw-r--r-- 7,805 bytes parent folder | download | duplicates (12)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* ----------------------------------------------------------------------------
 * File    : input.c
 * Purpose : input routine to create a Tree from an input file
 * ----------------------------------------------------------------------------
 */

/* SunOS 5.4+ way to ask for all functions in header files. */
#define __EXTENSIONS__ 1

#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "defs.h"
#include "tree.h"
#include "input.h"
#include "dbl.h"
#include "intf.h"

char *EnvNm;                 /* Stores name of current Envir file */
static int tokDepth = 0;     /* Depth in tree of current token */
static int prevTokDepth;     /* Depth in tree of prev token */

static void SaveSubtree(Tree *tree, int level, FILE *fp);

/* ----------------------------------------------------------------------------
 *
 *   GetNextToken() reads the next token from the file indicated by 'fp' and
 *   returns a token. If the token is TOKEN_LABEL, the lexeme is returned
 *   in 'lexeme'. If memory could not be allocated for 'lexeme', it is NULL.
 *
 *   The following tokens are supported:
 *
 *     - TOKEN_LABEL: a string of characters, up to 'TOKEN-MAXSIZ'
 *       characters, delimited by number of leading spaces and newlines.
 *       If a label has more than this number of characters, the rest are
 *       ignored.
 *     - TOKEN_EOF
 *
 * ----------------------------------------------------------------------------
 */

static int
GetNextToken(FILE *fp, char **lexeme)
{
  static   char  lexbuf[INPUT_BUFSIZ];
  register char *curbuf = lexbuf;
  register int   charct = 0;
  register int   c;

  prevTokDepth = tokDepth;
  tokDepth = 0;

  c = getc(fp);

  /* skip over leading whitespace */
  while (c == ' ')
    {
      tokDepth++;
      c = getc(fp);
    }
  tokDepth /= 2;

  while (1)
    {
      switch (c)
	{
	case EOF:
	  return TOKEN_EOF;
	case '\n':
	  *curbuf = '\0';
	  *lexeme = strdup(lexbuf);
	  return TOKEN_LABEL;
	default:
	  *curbuf++ = c;
	  charct++;
	  /* check for buffer overflow */
	  if (charct >= TOKEN_MAXSIZ)
	    {
	      *curbuf = '\0';
	      *lexeme = strdup(lexbuf);
	      /* since buffer is full, skip over remaining chars */
	      c = getc(fp);
	      while (c != '\n' && c != EOF)
		c = getc(fp);
	      if (c == EOF)
		ungetc(c, fp);
	      return TOKEN_LABEL;
	    }
	  else
	    c = getc(fp);
	}
    }
}


/* ----------------------------------------------------------------------------
 *
 *   SetNodeLabelAndValue() sets the label text of the specified node and
 *   stores any string value following the label and preceded by a "^^"
 *   delimiter.
 *
 * ----------------------------------------------------------------------------
 */

void
SetNodeLabelAndValue(Tree *node, char *label_and_value)
{
   char*       val;

   if ((val = strstr(label_and_value, "^^")))
       {
           /* Set node value to string following ^^ delimiter. */
           node->value = val+2;
           /* Erase value from input string, leaving only label. */
           *val = '\0';
       }
   else
       {   node->value = NULL; }
   SetNodeLabel(node, label_and_value);
}


/* ----------------------------------------------------------------------------
 *
 *   ReadTreeFromFile() takes a filename argument and constructs
 *   a Tree from the labels in the file. If a tree could be constructed,
 *   even partially, it is returned by the function. NULL is returned if
 *   the file could not be opened or there was insufficient memory for
 *   creating the tree.
 *
 * ----------------------------------------------------------------------------
 */

Tree*
ReadTreeFromFile(char *fname, ErrCode *error)
{
  FILE *infile;
  int   inside_list = 0;	/* for semantic checking */
  int   first_child = TRUE;

  int   token;
  char *label;

  Tree *tree = NULL;		/* the return value of this function  */
  Tree *parent = NULL;		/* parent of 'node'                   */
  Tree *node;			/* current node                       */
  Tree *new_node;		/* new node to add after current node */

  *error = ERR_NONE;

  infile = fopen(fname, "r");
  if (infile == NULL)
    {
      *error = ERR_OPENFAIL;
      return NULL;
    }

  /* first line of file is Envir file name, save */
  token = GetNextToken(infile, &label);
  if (token == TOKEN_EOF)
    {
      *error = ERR_EMPTYFILE;
      fclose(infile);
      return NULL;
    }
  else if (token == TOKEN_LABEL)
    {
      if (label == NULL)
	{
	  *error = ERR_MEMALLOC;
	  fclose(infile);
	  return NULL;
	}
      EnvNm = strdup(label);
    }

  /* set up root node */
  token = GetNextToken(infile, &label);
  if (token == TOKEN_EOF)
    {
      *error = ERR_EMPTYFILE;
      fclose(infile);
      return NULL;
    }
  else if (token == TOKEN_LABEL)
    {
      if (label == NULL)
	{
	  *error = ERR_MEMALLOC;
	  fclose(infile);
	  return NULL;
	}
      tree = MakeNode();
      if (tree == NULL)
	{
	  *error = ERR_MEMALLOC;
	  fclose(infile);
	  free(label);
	  return NULL;
	}
      SetNodeLabelAndValue(tree, label);
      tree->parent = NULL;
      node = tree;
    }
  else
    {
      *error = ERR_NOROOT;
      fclose(infile);
      return NULL;
    }

  /* add children and siblings */
  while (1)
    {
      token = GetNextToken(infile, &label);
      if (token == TOKEN_EOF)
	break;

      if (tokDepth > prevTokDepth)  /* then new subtree */
	{
	  inside_list++;
	  first_child = TRUE;
	  parent = node;
	}
      else if (tokDepth < prevTokDepth)  /* then end of subtree */
	if (!inside_list)
	  {
	    *error = ERR_NOBEGIN;
	    fclose(infile);
	    return tree;
	  }
	else
	  while (tokDepth < inside_list)
	    {
	      inside_list--;
	      node = node->parent;
	      parent = node->parent;
	    }

      if (label == NULL)
	{
	  *error = ERR_MEMALLOC;
	  fclose(infile);
	  return tree;
	}
      if (parent == NULL)
	{
	  *error = ERR_MANYROOT;
	  fclose(infile);
	  free(label);
	  return tree;
	}
      else
	{
	  new_node = MakeNode();
	  if (new_node == NULL)
	    {
	      *error = ERR_MEMALLOC;
	      fclose(infile);
	      free(label);
	      return tree;
	    }
	  SetNodeLabelAndValue(new_node, label);
	  new_node->parent = parent;

	  if (first_child)
	    {
	      new_node->parent->child = new_node;
	      first_child = FALSE;
	    }
	  else
	    node->sibling = new_node;

	  node = new_node;
/*
 *	  printf("%3d tok: '%s'; tokDepth: %d; prevTokDepth: %d; inside_list: %d\n",
 *	     NumNodes, node->label.text, tokDepth, prevTokDepth, inside_list);
 */
	}
    }
  fclose(infile);
  return tree;
}


/* ----------------------------------------------------------------------------
 *
 *   SaveTreeToFile() takes a tree and saves it to a file specified by 'fname.'
 *   If the file could not be opened for writing, False is returned. Otherwise,
 *   True is returned.
 *
 * ----------------------------------------------------------------------------
 */

int
SaveTreeToFile(Tree *tree, char *fname)
{
  FILE *outfile;

  outfile = fopen(fname, "w");
  if (outfile == NULL)
    return FALSE;

  fprintf(outfile, "%s\n", EnvNm);   /* Save Env File Name */
  fprintf(outfile, "%s\n", tree->label.text);
  if (tree->child)
    SaveSubtree(tree->child, 0, outfile);

  fclose(outfile);
  return TRUE;
}


/* ----------------------------------------------------------------------------
 *
 *   SaveSubtree() is the recursive procedure that supports SaveTreeToFile().
 *
 * ----------------------------------------------------------------------------
 */

static void
SaveSubtree(Tree *tree, int level, FILE *fp)
{
  int i;

  level++;
  for ( ; tree ; tree = tree->sibling)
    {
      for (i = 0 ; i < level ; i++)
	{
	  putc(' ', fp);
	  putc(' ', fp);
	}
      fprintf(fp, "%s\n", tree->label.text);
      if (tree->child)
	SaveSubtree(tree->child, level, fp);
    }
  level--;
}