GRASS Programmer's Manual  6.4.1(2011)
span.c
Go to the documentation of this file.
00001 /* LIBDGL -- a Directed Graph Library implementation
00002  * Copyright (C) 2002 Roberto Micarelli
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 /*
00020  * Source best viewed with tabstop=4
00021  */
00022 
00023 #include <stdio.h>
00024 #include <sys/types.h>
00025 #include <sys/stat.h>
00026 #include <unistd.h>
00027 #include <stdlib.h>
00028 #include <fcntl.h>
00029 #include <time.h>
00030 #include <errno.h>
00031 #include <math.h>
00032 
00033 #include "../type.h"
00034 #include "../graph.h"
00035 
00036 #include "opt.h"
00037 
00038 static int _clipper(dglGraph_s * pgraphIn,
00039                     dglGraph_s * pgraphOut,
00040                     dglSpanClipInput_s * pArgIn,
00041                     dglSpanClipOutput_s * pArgOut, void *pvArg)
00042 {
00043     return 0;
00044 }
00045 
00046 int main(int argc, char **argv)
00047 {
00048     dglGraph_s graph, graphOut;
00049     dglInt32_t nVertex;
00050     int nret, fd;
00051 
00052     /* program options
00053      */
00054     char *pszGraph;
00055     char *pszGraphOut;
00056     char *pszVertex;
00057 
00058     GNO_BEGIN                   /* short   long                default     variable        help */
00059         GNO_OPTION("g", "graph", NULL, &pszGraph, "Input Graph file")
00060         GNO_OPTION("o", "graphout", NULL, &pszGraphOut, "Output Graph file")
00061         GNO_OPTION("v", "vertex", NULL, &pszVertex, "Vertex Node Id")
00062     GNO_END if (GNO_PARSE(argc, argv) < 0)
00063     {
00064         return 1;
00065     }
00066     /*
00067      * options parsed
00068      */
00069 
00070     if (pszVertex == NULL) {
00071         GNO_HELP("span usage");
00072         return 1;
00073     }
00074     nVertex = atol(pszVertex);
00075 
00076     printf("Graph read:\n");
00077     if ((fd = open(pszGraph, O_RDONLY)) < 0) {
00078         perror("open");
00079         return 1;
00080     }
00081     nret = dglRead(&graph, fd);
00082     if (nret < 0) {
00083         fprintf(stderr, "dglRead error: %s\n", dglStrerror(&graph));
00084         return 1;
00085     }
00086     close(fd);
00087     printf("Done.\n");
00088 
00089     printf("Graph depth spanning:\n");
00090     nret = dglDepthSpanning(&graph, &graphOut, nVertex, _clipper, NULL);
00091     if (nret < 0) {
00092         fprintf(stderr, "dglDepthSpanning error: %s\n", dglStrerror(&graph));
00093         return 1;
00094     }
00095     printf("Done.\n");
00096 
00097 
00098     printf("Graph flatten:\n");
00099     nret = dglFlatten(&graphOut);
00100     printf("Done.\n");
00101 
00102     if (dglGet_EdgeCount(&graphOut) > 0) {
00103 
00104 
00105         if (pszGraphOut) {
00106             printf("Graph write:\n");
00107             if ((fd =
00108                  open(pszGraphOut, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
00109                 perror("open");
00110                 return 1;
00111             }
00112             dglWrite(&graphOut, fd);
00113             if (nret < 0) {
00114                 fprintf(stderr, "dglWrite error: %s\n",
00115                         dglStrerror(&graphOut));
00116                 return 1;
00117             }
00118             close(fd);
00119             printf("Done.\n");
00120         }
00121     }
00122     else {
00123         printf("Empty span. No output produced.\n");
00124     }
00125 
00126     dglRelease(&graph);
00127     dglRelease(&graphOut);
00128     return 0;
00129 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines