GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dglib/examples/components.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;
49 
50 #define MY_MAX_COMPONENTS 1024
51  dglGraph_s agraphComponents[MY_MAX_COMPONENTS];
52  int nret, fd, i, cComponents;
53  char szGraphOutFilename[1024];
54 
55  /* program options
56  */
57  char *pszGraph;
58  char *pszGraphOut;
59 
60  GNO_BEGIN /* short long default variable help */
61  GNO_OPTION("g", "graph", NULL, &pszGraph, "Input Graph file")
62  GNO_OPTION("o", "graphout", NULL, &pszGraphOut, "Output Graph file")
63  GNO_END if (GNO_PARSE(argc, argv) < 0)
64  {
65  return 1;
66  }
67  /*
68  * options parsed
69  */
70 
71  if (pszGraph == NULL || pszGraphOut == NULL) {
72  GNO_HELP("components usage");
73  return 1;
74  }
75 
76 
77  printf("Graph read:\n");
78  if ((fd = open(pszGraph, O_RDONLY)) < 0) {
79  perror("open");
80  return 1;
81  }
82  nret = dglRead(&graph, fd);
83  if (nret < 0) {
84  fprintf(stderr, "dglRead error: %s\n", dglStrerror(&graph));
85  return 1;
86  }
87  close(fd);
88  printf("Done.\n");
89 
90 
91 
92  printf("Graph depth components spanning:\n");
93  cComponents =
94  dglDepthComponents(&graph, agraphComponents, MY_MAX_COMPONENTS,
95  _clipper, NULL);
96  if (cComponents < 0) {
97  fprintf(stderr, "dglDepthSpanning error: %s\n", dglStrerror(&graph));
98  return 1;
99  }
100  printf("Done.\n");
101 
102  printf("Connected Component(s) Found: %d\n", cComponents);
103 
104  for (i = 0; i < cComponents; i++) {
105  printf("Component %d of %d: ", i + 1, cComponents);
106  fflush(stdout);
107 
108  printf("[flatten...");
109  fflush(stdout);
110  nret = dglFlatten(&agraphComponents[i]);
111  printf("done] ");
112  fflush(stdout);
113 
114  if (dglGet_EdgeCount(&agraphComponents[i]) > 0) {
115  if (pszGraphOut) {
116  snprintf(szGraphOutFilename, sizeof(szGraphOutFilename),
117  "%s-component-%d", pszGraphOut, i);
118  printf("[write <%s>...", szGraphOutFilename);
119  fflush(stdout);
120  if ((fd =
121  open(szGraphOutFilename, O_WRONLY | O_CREAT | O_TRUNC,
122  0666)) < 0) {
123  perror("open");
124  return 1;
125  }
126  dglWrite(&agraphComponents[i], fd);
127  if (nret < 0) {
128  fprintf(stderr, "dglWrite error: %s\n",
129  dglStrerror(&graph));
130  return 1;
131  }
132  close(fd);
133  printf("done] ");
134  fflush(stdout);
135  }
136  }
137  else {
138  printf("component is empty. No output produced.\n");
139  }
140 
141  printf("[release...");
142  dglRelease(&agraphComponents[i]);
143  printf("done]\n");
144  }
145 
146  dglRelease(&graph);
147  return 0;
148 }