~ubuntu-branches/ubuntu/lucid/graphviz/lucid-security

« back to all changes in this revision

Viewing changes to tools/src/ingraphs.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen M Moraco
  • Date: 2002-02-05 18:52:12 UTC
  • Revision ID: james.westby@ubuntu.com-20020205185212-8i04c70te00rc40y
Tags: upstream-1.7.16
ImportĀ upstreamĀ versionĀ 1.7.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    This software may only be used by you under license from AT&T Corp.
 
3
    ("AT&T").  A copy of AT&T's Source Code Agreement is available at
 
4
    AT&T's Internet website having the URL:
 
5
    <http://www.research.att.com/sw/tools/graphviz/license/source.html>
 
6
    If you received this software without first entering into a license
 
7
    with AT&T, you have an infringing copy of this software and cannot use
 
8
    it without violating AT&T's intellectual property rights.
 
9
*/
 
10
 
 
11
/*
 
12
 * Written by Emden Gansner
 
13
 */
 
14
 
 
15
#include <stdio.h>
 
16
#include <stdlib.h>
 
17
 
 
18
typedef struct { char* dummy; } Agraph_t;
 
19
 
 
20
#include <ingraphs.h>
 
21
 
 
22
/* nextFile:
 
23
 * Set next available file.
 
24
 * If Files is NULL, we just read from stdin.
 
25
 */
 
26
static void
 
27
nextFile(ingraph_state* sp)
 
28
{
 
29
    FILE    *rv = NULL;
 
30
 
 
31
    if (sp->Files == NULL) {
 
32
        if (sp->ctr++ == 0) {
 
33
          rv = stdin;
 
34
        }
 
35
    }
 
36
    else {
 
37
        while (sp->Files[sp->ctr]) {
 
38
            if ((rv = fopen(sp->Files[sp->ctr++],"r")) != 0) break;
 
39
            else fprintf(stderr,"Can't open %s\n",sp->Files[sp->ctr-1]);
 
40
        }
 
41
    }
 
42
    sp->fp = rv;
 
43
}
 
44
 
 
45
/* nextGraph:
 
46
 * Read and return next graph; return NULL if done.
 
47
 * Read graph from currently open file. If none, open next file.
 
48
 */
 
49
Agraph_t* 
 
50
nextGraph (ingraph_state* sp)
 
51
{
 
52
    Agraph_t*       g;
 
53
 
 
54
    if (sp->fp == NULL) nextFile(sp);
 
55
    g = NULL;
 
56
 
 
57
    while (sp->fp != NULL) {
 
58
        if (sp->openf) {
 
59
          if ((g = sp->openf(sp->fp)) != 0) break;
 
60
        }
 
61
        else {
 
62
          fprintf (stderr, "ingraphs: NULL graph reader\n");
 
63
          return 0;
 
64
        }
 
65
        if (sp->Files)    /* Only close if not using stdin */
 
66
          fclose (sp->fp);
 
67
        nextFile(sp);
 
68
    }
 
69
    return g;
 
70
}
 
71
 
 
72
/* newIngraph:
 
73
 * Create new ingraph state. If sp is non-NULL, we
 
74
 * assume user is supplying memory.
 
75
 * At present, we require opf to be non-NULL. In
 
76
 * theory, we could assume a function agread(FILE*,void*)
 
77
 */
 
78
ingraph_state* 
 
79
newIngraph (ingraph_state* sp, char** files, opengfn opf)
 
80
{
 
81
    if (!sp) {
 
82
      sp = (ingraph_state*)malloc(sizeof(ingraph_state));
 
83
      if (!sp) {
 
84
        fprintf (stderr, "ingraphs: out of memory\n");
 
85
        return 0;
 
86
      }
 
87
    }
 
88
    sp->Files = files;
 
89
    sp->ctr = 0;
 
90
    sp->fp = NULL;
 
91
    if (opf) sp->openf = opf;
 
92
    else {
 
93
      fprintf (stderr, "ingraphs: NULL graph reader\n");
 
94
      return 0;
 
95
    }
 
96
    return sp;
 
97
}
 
98
 
 
99
/* closeIngraph:
 
100
 * Close any open files. Use must handle freeing of memory.
 
101
 */
 
102
void
 
103
closeIngraph (ingraph_state* sp)
 
104
{
 
105
  if (sp->Files && sp->fp) fclose (sp->fp);
 
106
}
 
107
 
 
108
/* fileName:
 
109
 * Return name of current file being processed.
 
110
 */
 
111
char* 
 
112
fileName (ingraph_state* sp)
 
113
{
 
114
  if (sp->Files) {
 
115
    if (sp->ctr) return sp->Files[sp->ctr - 1];
 
116
    else return "<>";
 
117
  }
 
118
  else return "<stdin>";
 
119
}