GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
span.c
Go to the documentation of this file.
1 /* LIBDGL -- a Directed Graph Library implementation
2  * Copyright (C) 2002 Roberto Micarelli
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * Source best viewed with tabstop=4
21  */
22 
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <fcntl.h>
29 #include <time.h>
30 #include <errno.h>
31 #include <math.h>
32 
33 #include "../type.h"
34 #include "../graph.h"
35 
36 #include "opt.h"
37 
38 static int _clipper(dglGraph_s * pgraphIn,
39  dglGraph_s * pgraphOut,
40  dglSpanClipInput_s * pArgIn,
41  dglSpanClipOutput_s * pArgOut, void *pvArg)
42 {
43  return 0;
44 }
45 
46 int main(int argc, char **argv)
47 {
48  dglGraph_s graph, graphOut;
49  dglInt32_t nVertex;
50  int nret, fd;
51 
52  /* program options
53  */
54  char *pszGraph;
55  char *pszGraphOut;
56  char *pszVertex;
57 
58  GNO_BEGIN /* short long default variable help */
59  GNO_OPTION("g", "graph", NULL, &pszGraph, "Input Graph file")
60  GNO_OPTION("o", "graphout", NULL, &pszGraphOut, "Output Graph file")
61  GNO_OPTION("v", "vertex", NULL, &pszVertex, "Vertex Node Id")
62  GNO_END if (GNO_PARSE(argc, argv) < 0)
63  {
64  return 1;
65  }
66  /*
67  * options parsed
68  */
69 
70  if (pszVertex == NULL) {
71  GNO_HELP("span usage");
72  return 1;
73  }
74  nVertex = atol(pszVertex);
75 
76  printf("Graph read:\n");
77  if ((fd = open(pszGraph, O_RDONLY)) < 0) {
78  perror("open");
79  return 1;
80  }
81  nret = dglRead(&graph, fd);
82  if (nret < 0) {
83  fprintf(stderr, "dglRead error: %s\n", dglStrerror(&graph));
84  return 1;
85  }
86  close(fd);
87  printf("Done.\n");
88 
89  printf("Graph depth spanning:\n");
90  nret = dglDepthSpanning(&graph, &graphOut, nVertex, _clipper, NULL);
91  if (nret < 0) {
92  fprintf(stderr, "dglDepthSpanning error: %s\n", dglStrerror(&graph));
93  return 1;
94  }
95  printf("Done.\n");
96 
97 
98  printf("Graph flatten:\n");
99  nret = dglFlatten(&graphOut);
100  printf("Done.\n");
101 
102  if (dglGet_EdgeCount(&graphOut) > 0) {
103 
104 
105  if (pszGraphOut) {
106  printf("Graph write:\n");
107  if ((fd =
108  open(pszGraphOut, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
109  perror("open");
110  return 1;
111  }
112  dglWrite(&graphOut, fd);
113  if (nret < 0) {
114  fprintf(stderr, "dglWrite error: %s\n",
115  dglStrerror(&graphOut));
116  return 1;
117  }
118  close(fd);
119  printf("Done.\n");
120  }
121  }
122  else {
123  printf("Empty span. No output produced.\n");
124  }
125 
126  dglRelease(&graph);
127  dglRelease(&graphOut);
128  return 0;
129 }