GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
cr_from_a.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 <string.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <fcntl.h>
30 #include <time.h>
31 #include <errno.h>
32 
33 #include "../type.h"
34 #include "../graph.h"
35 
36 #include "opt.h"
37 
38 extern int errno;
39 
40 
41 
42 int main(int argc, char **argv)
43 {
44  FILE *fp;
45  dglGraph_s graph;
46  char sz[1024];
47  char c;
48  int nret, fd;
49  int version, attrsize;
50  dglInt32_t nodeid, from, to, cost, user, xyz[3];
51  dglInt32_t opaqueset[16] = {
52  360000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
53  };
54 
55  /* program options
56  */
57  char *pszFilein;
58  char *pszFileout;
59 
60  GNO_BEGIN /* short long default variable help */
61  GNO_OPTION("f", "file", NULL, &pszFilein,
62  "Input Graph definition file")
63  GNO_OPTION("g", "graph", NULL, &pszFileout, "Output Graph file")
64  GNO_END if (GNO_PARSE(argc, argv) < 0)
65  {
66  return 1;
67  }
68  /*
69  * options parsed
70  */
71 
72  if (pszFilein == NULL) {
73  GNO_HELP("Incomplete parameters");
74  return 1;
75  }
76 
77  if ((fp = fopen(pszFilein, "r")) == NULL) {
78  perror("fopen");
79  return 1;
80  }
81 
82  reread_first_line:
83  if (fgets(sz, sizeof(sz), fp) == NULL) {
84  fprintf(stderr, "unexpected EOF\n");
85  return 1;
86  }
87 
88  if (sz[0] == '#' || strlen(sz) == 0)
89  goto reread_first_line;
90 
91  sscanf(sz, "%d %d", &version, &attrsize);
92 
93  /*
94  * initialize the graph
95  */
96  dglInitialize(&graph, /* graph context to initialize */
97  version, /* version */
98  sizeof(xyz), /* node attributes size */
99  0, /* edge attributes size */
100  opaqueset /* opaque graph parameters */
101  );
102 
103  /*
104  * generate edge cost prioritizing
105  */
107 
108 
109  /* add arcs and X/Y/Z node attributes
110  */
111  while (fgets(sz, sizeof(sz), fp) != NULL) {
112  if (sz[0] == '#')
113  continue;
114 
115  if (strlen(sz) == 0)
116  continue;
117 
118  if (sz[0] == 'A') { /* add a edge */
119  sscanf(sz, "%c %ld %ld %ld %ld", &c, &from, &to, &cost, &user);
120 
121  nret = dglAddEdge(&graph, from, to, cost, user);
122 
123 
124  if (nret < 0) {
125  fprintf(stderr, "dglAddArc error: %s\n", dglStrerror(&graph));
126  return 1;
127  }
128  }
129  else if (sz[0] == 'V') { /* add a node */
130  sscanf(sz, "%c %ld", &c, &nodeid);
131 
132  printf("add node: %ld\n", nodeid);
133 
134  nret = dglAddNode(&graph, nodeid, NULL, 0);
135 
136  if (nret < 0) {
137  fprintf(stderr, "dglAddNode error: %s\n",
138  dglStrerror(&graph));
139  return 1;
140  }
141  }
142  else if (sz[0] == 'N') { /* set attributes for a (already inserted) node */
143  sscanf(sz, "%c %ld %ld %ld %ld", &c, &nodeid, &xyz[0], &xyz[1],
144  &xyz[2]);
145 
146  dglNodeSet_Attr(&graph, dglGetNode(&graph, nodeid), xyz);
147  }
148  }
149  fclose(fp);
150 
151 
152 #if 0 /* show edges */
153  {
155  dglInt32_t *pEdge;
156 
157  nret =
158  dglEdge_T_Initialize(&t, &graph, dglGet_EdgePrioritizer(&graph));
159  if (nret < 0) {
160  fprintf(stderr, "\ndglEdge_T_Initialize error: %s\n",
161  dglStrerror(&graph));
162  return 1;
163  }
164  for (pEdge = dglEdge_T_First(&t); pEdge; pEdge = dglEdge_T_Next(&t)) {
165  printf("edge: id=%ld cost=%ld\n",
166  dglEdgeGet_Id(&graph, pEdge),
167  dglEdgeGet_Cost(&graph, pEdge));
168  }
169  dglEdge_T_Release(&t);
170  }
171 #endif
172 
173  /*
174  * flatten the graph (make it serializable)
175  */
176  nret = dglFlatten(&graph);
177 
178  if (nret < 0) {
179  fprintf(stderr, "dglFlatten error: %s\n", dglStrerror(&graph));
180  return 1;
181  }
182 
183  /*
184  * store the graph
185  */
186  if ((fd = open(pszFileout, O_WRONLY | O_CREAT, 0666)) < 0) {
187  perror("open");
188  return 1;
189  }
190 
191  nret = dglWrite(&graph, fd);
192 
193  if (nret < 0) {
194  fprintf(stderr, "dglWrite error: %s\n", dglStrerror(&graph));
195  return 1;
196  }
197 
198  close(fd);
199 
200  /*
201  * finish
202  */
203  dglRelease(&graph);
204 
205  return 0;
206 }