Mercurial > hg > xemacs-beta
diff pkg-src/tree-x/input.c @ 167:85ec50267440 r20-3b10
Import from CVS: tag r20-3b10
author | cvs |
---|---|
date | Mon, 13 Aug 2007 09:45:46 +0200 |
parents | 0132846995bd |
children | 15872534500d |
line wrap: on
line diff
--- a/pkg-src/tree-x/input.c Mon Aug 13 09:44:44 2007 +0200 +++ b/pkg-src/tree-x/input.c Mon Aug 13 09:45:46 2007 +0200 @@ -6,49 +6,50 @@ #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(); +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. + * ignored. * - TOKEN_EOF - * + * * ---------------------------------------------------------------------------- */ -int - GetNextToken(fp, lexeme) -FILE *fp; -char **lexeme; +static int +GetNextToken(FILE *fp, char **lexeme) { static char lexbuf[INPUT_BUFSIZ]; register char *curbuf = lexbuf; register int charct = 0; register int c; int done = FALSE; - + prevTokDepth = tokDepth; tokDepth = 0; - + c = getc(fp); - + /* skip over leading whitespace */ while (c == ' ') { @@ -56,7 +57,7 @@ c = getc(fp); } tokDepth /= 2; - + while (1) { switch (c) @@ -67,7 +68,6 @@ *curbuf = '\0'; *lexeme = strdup(lexbuf); return (TOKEN_LABEL); - break; default: *curbuf++ = c; charct++; @@ -92,22 +92,20 @@ /* ---------------------------------------------------------------------------- - * + * * SetNodeLabelAndValue() sets the label text of the specified node and * stores any string value following the label and preceded by a "^^" - * delimiter. - * + * delimiter. + * * ---------------------------------------------------------------------------- */ void -SetNodeLabelAndValue(node, label_and_value) - Tree *node; - char *label_and_value; +SetNodeLabelAndValue(Tree *node, char *label_and_value) { char* val; - if (val = strstr(label_and_value, "^^")) + if ((val = strstr(label_and_value, "^^"))) { /* Set node value to string following ^^ delimiter. */ node->value = val+2; @@ -121,42 +119,40 @@ /* ---------------------------------------------------------------------------- - * + * * 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(fname, error) -char *fname; -ErrCode *error; +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) @@ -175,7 +171,7 @@ } EnvNm = strdup(label); } - + /* set up root node */ token = GetNextToken(infile, &label); if (token == TOKEN_EOF) @@ -210,14 +206,14 @@ 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++; @@ -238,7 +234,7 @@ node = node->parent; parent = node->parent; } - + if (label == NULL) { *error = ERR_MEMALLOC; @@ -264,7 +260,7 @@ } SetNodeLabelAndValue(new_node, label); new_node->parent = parent; - + if (first_child) { new_node->parent->child = new_node; @@ -272,7 +268,7 @@ } else node->sibling = new_node; - + node = new_node; /* * printf("%3d tok: '%s'; tokDepth: %d; prevTokDepth: %d; inside_list: %d\n", @@ -286,49 +282,45 @@ /* ---------------------------------------------------------------------------- - * + * * 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. - * + * * ---------------------------------------------------------------------------- */ -SaveTreeToFile(tree, fname) - Tree *tree; - char *fname; +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, level, fp) -Tree *tree; -int level; -FILE *fp; +SaveSubtree(Tree *tree, int level, FILE *fp) { int i; - + level++; for ( ; tree ; tree = tree->sibling) { @@ -343,5 +335,3 @@ } level--; } - -