/* paths.c 1.1 */ /* Copyright 1992, 1993 O'Reilly and Associates, Inc. Permission to use, copy, and modify this program is hereby granted, as long as this copyright notice appears in each copy of the program source code. */ #include "book_utils.h" void print_ancestors(), print_descendants(); static Display *dpy; void print_ancestors( struct_id ) PEXStructure struct_id; { unsigned long num_paths, i, j; PEXStructurePath *paths; paths = PEXGetAncestors( dpy, struct_id, PEXTopPart, 0, &num_paths ); printf( "%d ancestor paths for structure %d:\n", num_paths, struct_id ); for ( i = 0; i < num_paths; i++ ) { printf( "\tpath %d:", i+1 ); for( j = 0; j < paths[i].count; j++ ) printf( " (%d, %d)", paths[i].elements[j].structure, paths[i].elements[j].offset ); printf( "\n" ); } PEXFreeStructurePaths( num_paths, paths ); } void print_descendants( struct_id ) PEXStructure struct_id; { unsigned long num_paths, i, j; PEXStructurePath *paths; paths = PEXGetDescendants( dpy, struct_id, PEXTopPart, 0, &num_paths ); printf( "%d descendant paths for structure %d:\n", num_paths, struct_id ); for ( i = 0; i < num_paths; i++ ) { printf( "\tpath %d:", i+1 ); for( j = 0; j < paths[i].count; j++ ) printf( " (%d, %d)", paths[i].elements[j].structure, paths[i].elements[j].offset ); printf( "\n" ); } PEXFreeStructurePaths( num_paths, paths ); } PEXStructure* networks_containing( struct_id, num_nets ) PEXStructure struct_id; unsigned long *num_nets; { int i; PEXStructurePath *paths; PEXStructure *roots = (PEXStructure *)NULL; /* Inquire ancestors, specifying "top part" and a depth of 1. */ paths = PEXGetAncestors( dpy, struct_id, PEXTopPart, 1, num_nets ); if ( *num_nets > 0 ) { roots = (PEXStructure *) malloc( *num_nets * sizeof(PEXStructure)); for ( i = 0; i < *num_nets; i++ ) roots[i] = paths[i].elements[0].structure; } PEXFreeStructurePaths( *num_nets, paths ); return( roots ); } PEXStructure struct_A, struct_B, struct_C, struct_D; main( argc, argv ) int argc; char *argv[]; { PEXExtensionInfo *pexinfo; PEXStructure *roots; unsigned long num_nets; int i; /* Open a display and initialize PEX. */ dpy = ora_init_pex( argv, &pexinfo ); if ( !dpy ) exit(1); if ( !STRUCTURE_SPT( pexinfo ) ) { fprintf( stderr, "Structures not supported.\n" ); exit(1); } /** Inquire Ancestors example. **/ struct_A = PEXCreateStructure( dpy ); struct_B = PEXCreateStructure( dpy ); struct_C = PEXCreateStructure( dpy ); struct_D = PEXCreateStructure( dpy ); /* Build structure A. */ PEXLabel( dpy, struct_A, PEXOCStore, 0 ); PEXLabel( dpy, struct_A, PEXOCStore, 0 ); PEXExecuteStructure( dpy, struct_A, PEXOCStore, struct_D ); PEXLabel( dpy, struct_A, PEXOCStore, 0 ); /* Build structure B. */ PEXExecuteStructure( dpy, struct_B, PEXOCStore, struct_C ); PEXLabel( dpy, struct_B, PEXOCStore, 0 ); PEXLabel( dpy, struct_B, PEXOCStore, 0 ); /* Build structure C. */ PEXLabel( dpy, struct_C, PEXOCStore, 0 ); PEXLabel( dpy, struct_C, PEXOCStore, 0 ); PEXLabel( dpy, struct_C, PEXOCStore, 0 ); PEXExecuteStructure( dpy, struct_C, PEXOCStore, struct_D ); print_ancestors( struct_D ); roots = networks_containing( struct_D, &num_nets ); printf( "Structure %d is part of %d networks:", struct_D, num_nets ); for ( i = 0; i < num_nets; i++ ) printf( " %d ", roots[i] ); printf( "\n" ); /** Inquire Descendants example. **/ PEXDeleteElements( dpy, struct_A, PEXBeginning, 0, PEXEnd, 0 ); PEXDeleteElements( dpy, struct_B, PEXBeginning, 0, PEXEnd, 0 ); PEXDeleteElements( dpy, struct_C, PEXBeginning, 0, PEXEnd, 0 ); PEXDeleteElements( dpy, struct_D, PEXBeginning, 0, PEXEnd, 0 ); /* Build structure A. */ PEXLabel( dpy, struct_A, PEXOCStore, 0 ); PEXExecuteStructure( dpy, struct_A, PEXOCStore, struct_B ); PEXLabel( dpy, struct_A, PEXOCStore, 0 ); PEXExecuteStructure( dpy, struct_A, PEXOCStore, struct_C ); /* Build structure B. */ PEXLabel( dpy, struct_B, PEXOCStore, 0 ); PEXLabel( dpy, struct_B, PEXOCStore, 0 ); PEXExecuteStructure( dpy, struct_B, PEXOCStore, struct_D ); PEXLabel( dpy, struct_B, PEXOCStore, 0 ); print_descendants( struct_A ); XCloseDisplay( dpy ); return 0; }