GRASS Programmer's Manual
6.4.1(2011)
|
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; 00049 00050 #define MY_MAX_COMPONENTS 1024 00051 dglGraph_s agraphComponents[MY_MAX_COMPONENTS]; 00052 int nret, fd, i, cComponents; 00053 char szGraphOutFilename[1024]; 00054 00055 /* program options 00056 */ 00057 char *pszGraph; 00058 char *pszGraphOut; 00059 00060 GNO_BEGIN /* short long default variable help */ 00061 GNO_OPTION("g", "graph", NULL, &pszGraph, "Input Graph file") 00062 GNO_OPTION("o", "graphout", NULL, &pszGraphOut, "Output Graph file") 00063 GNO_END if (GNO_PARSE(argc, argv) < 0) 00064 { 00065 return 1; 00066 } 00067 /* 00068 * options parsed 00069 */ 00070 00071 if (pszGraph == NULL || pszGraphOut == NULL) { 00072 GNO_HELP("components usage"); 00073 return 1; 00074 } 00075 00076 00077 printf("Graph read:\n"); 00078 if ((fd = open(pszGraph, O_RDONLY)) < 0) { 00079 perror("open"); 00080 return 1; 00081 } 00082 nret = dglRead(&graph, fd); 00083 if (nret < 0) { 00084 fprintf(stderr, "dglRead error: %s\n", dglStrerror(&graph)); 00085 return 1; 00086 } 00087 close(fd); 00088 printf("Done.\n"); 00089 00090 00091 00092 printf("Graph depth components spanning:\n"); 00093 cComponents = 00094 dglDepthComponents(&graph, agraphComponents, MY_MAX_COMPONENTS, 00095 _clipper, NULL); 00096 if (cComponents < 0) { 00097 fprintf(stderr, "dglDepthSpanning error: %s\n", dglStrerror(&graph)); 00098 return 1; 00099 } 00100 printf("Done.\n"); 00101 00102 printf("Connected Component(s) Found: %d\n", cComponents); 00103 00104 for (i = 0; i < cComponents; i++) { 00105 printf("Component %d of %d: ", i + 1, cComponents); 00106 fflush(stdout); 00107 00108 printf("[flatten..."); 00109 fflush(stdout); 00110 nret = dglFlatten(&agraphComponents[i]); 00111 printf("done] "); 00112 fflush(stdout); 00113 00114 if (dglGet_EdgeCount(&agraphComponents[i]) > 0) { 00115 if (pszGraphOut) { 00116 snprintf(szGraphOutFilename, sizeof(szGraphOutFilename), 00117 "%s-component-%d", pszGraphOut, i); 00118 printf("[write <%s>...", szGraphOutFilename); 00119 fflush(stdout); 00120 if ((fd = 00121 open(szGraphOutFilename, O_WRONLY | O_CREAT | O_TRUNC, 00122 0666)) < 0) { 00123 perror("open"); 00124 return 1; 00125 } 00126 dglWrite(&agraphComponents[i], fd); 00127 if (nret < 0) { 00128 fprintf(stderr, "dglWrite error: %s\n", 00129 dglStrerror(&graph)); 00130 return 1; 00131 } 00132 close(fd); 00133 printf("done] "); 00134 fflush(stdout); 00135 } 00136 } 00137 else { 00138 printf("component is empty. No output produced.\n"); 00139 } 00140 00141 printf("[release..."); 00142 dglRelease(&agraphComponents[i]); 00143 printf("done]\n"); 00144 } 00145 00146 dglRelease(&graph); 00147 return 0; 00148 }