GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
unflatten.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 int main(int argc, char **argv)
39 {
40  dglGraph_s graph;
41  int nret, fd;
42 
43  /* program options
44  */
45  char *pszGraph;
46  char *pszGraphOut;
47 
48  GNO_BEGIN /* short long default variable help */
49  GNO_OPTION("g", "graph", NULL, &pszGraph, "Input Graph file")
50  GNO_OPTION("o", "graphout", NULL, &pszGraphOut, "Output Graph file")
51  GNO_END if (GNO_PARSE(argc, argv) < 0)
52  {
53  return 1;
54  }
55  /*
56  * options parsed
57  */
58 
59  printf("Graph read:\n");
60  if ((fd = open(pszGraph, O_RDONLY)) < 0) {
61  perror("open");
62  return 1;
63  }
64  nret = dglRead(&graph, fd);
65  if (nret < 0) {
66  fprintf(stderr, "dglRead error: %s\n", dglStrerror(&graph));
67  return 1;
68  }
69  close(fd);
70  printf("Done.\n");
71 
72 
73  printf("Graph unflatten:\n");
74  nret = dglUnflatten(&graph);
75  if (nret < 0) {
76  fprintf(stderr, "dglUnflatten error: %s\n", dglStrerror(&graph));
77  return 1;
78  }
79  printf("Done.\n");
80 
81 
82  printf("Graph flatten:\n");
83  nret = dglFlatten(&graph);
84  printf("Done.\n");
85 
86 
87  if (pszGraphOut) {
88  printf("Graph write:\n");
89  if ((fd = open(pszGraphOut, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
90  perror("open");
91  return 1;
92  }
93  dglWrite(&graph, fd);
94  if (nret < 0) {
95  fprintf(stderr, "dglWrite error: %s\n", dglStrerror(&graph));
96  return 1;
97  }
98  close(fd);
99  printf("Done.\n");
100  }
101 
102  dglRelease(&graph);
103  return 0;
104 }