/* structure_probe.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_structure(), print_element(); static Display *dpy; /* Global variable to hold the structure ids. */ static PEXStructure structs[5]; static int num_structs; void print_structure( struct_id ) PEXStructure struct_id; { PEXStructureInfo struct_info; unsigned long mask; int el, float_format; mask = PEXNumElements | PEXEditMode | PEXElementPtr | PEXHasRefs; float_format = PEXGetProtocolFloatFormat( dpy ); if ( !PEXGetStructureInfo( dpy, struct_id, float_format, mask, &struct_info ) ) { printf( "structure %d does not exist\n", struct_id ); return; } printf( "\nStructure %d", struct_id ); printf( ", %d elements", struct_info.element_count ); printf( ", edit mode: %s", struct_info.edit_mode == PEXStructureInsert ? "Insert" : "Replace" ); printf( ", element pointer: %d", struct_info.element_pointer ); printf( ", %s\n", struct_info.has_refs == True ? "executed" : "not executed" ); /* Loop through all the elements and print their contents. */ for ( el = 1; el <= struct_info.element_count; el++ ) { printf( "element %d: ", el ); print_element( struct_id, el ); } } static void print_polyline(), print_polyline2D(), print_label(), print_nil(); void print_element( struct_id, element ) PEXStructure struct_id; int element; { char *raw_data; PEXOCData *el_data; unsigned long num_elements, raw_length; int float_format, status; /* Get the raw OC data. */ float_format = PEXGetProtocolFloatFormat( dpy ); status = PEXFetchElements( dpy, struct_id, PEXBeginning, element, PEXBeginning, element, float_format, &num_elements, &raw_length, &raw_data ); if ( status == 0 ) return; /* Convert it to PEXlib format. */ el_data = PEXDecodeOCs( float_format, num_elements, raw_length, raw_data ); /* Branch on the element type and print the contents. */ switch ( el_data->oc_type ) { case PEXOCPolyline: print_polyline( el_data ); break; case PEXOCPolyline2D: print_polyline2D( el_data ); break; case PEXOCLabel: print_label( el_data ); break; case PEXOCNil: print_nil(); break; default: fprintf( stderr, "element type not yet supported.\n" ); break; } XFree( raw_data ); PEXFreeOCData( num_elements, el_data ); } static void print_polyline( el_data ) PEXOCData *el_data; { int i; printf( "type = Polyline (3D)\n" ); printf( "\t\t%d points:\n", el_data->data.Polyline.count ); for ( i = 0; i < el_data->data.Polyline.count; i++ ) printf( "\t\t\tpoint %d = ( %g, %g, %g)\n", i+1, el_data->data.Polyline.points[i].x, el_data->data.Polyline.points[i].y, el_data->data.Polyline.points[i].z ); } static void print_polyline2D( el_data ) PEXOCData *el_data; { int i; printf( "type = Polyline (2D)\n" ); printf( "\t\t%d points:\n", el_data->data.Polyline2D.count ); for ( i = 0; i < el_data->data.Polyline2D.count; i++ ) printf( "\t\t\tpoint %d = ( %g, %g)\n", i+1, el_data->data.Polyline2D.points[i].x, el_data->data.Polyline2D.points[i].y ); } static void print_label( el_data ) PEXOCData *el_data; { printf( "type = Label\n" ); printf( "\t\tlabel = %d\n", el_data->data.Label.label ); } static void print_nil() { printf( "type = Nil (element position 0)\n" ); } static PEXCoord points3[] = {{.2,.2,0},{.4,.7,0},{.6,.3,0}}; static PEXCoord2D points[] = {{.2,.2},{.4,.7},{.6,.3},{.8,.8}}; static void create_structures() { num_structs = 3; structs[0] = PEXCreateStructure( dpy ); PEXLabel( dpy, structs[0], PEXOCStore, 2 ); PEXPolyline( dpy, structs[0], PEXOCStore, 3, points3 ); PEXLabel( dpy, structs[0], PEXOCStore, 4 ); PEXLabel( dpy, structs[0], PEXOCStore, 4 ); PEXLabel( dpy, structs[0], PEXOCStore, 6 ); PEXPolyline2D( dpy, structs[0], PEXOCStore, 3, points ); structs[1] = PEXCreateStructure( dpy ); PEXLabel( dpy, structs[1], PEXOCStore, 3 ); PEXLabel( dpy, structs[1], PEXOCStore, 5 ); PEXLabel( dpy, structs[1], PEXOCStore, 9 ); PEXLabel( dpy, structs[1], PEXOCStore, 9 ); PEXPolyline2D( dpy, structs[1], PEXOCStore, 3, points ); PEXPolyline( dpy, structs[1], PEXOCStore, 3, points3 ); structs[2] = PEXCreateStructure( dpy ); /* leave empty. */ } main( argc, argv ) int argc; char *argv[]; { PEXExtensionInfo *pexinfo; int i; /* Open a display and initialize PEX. */ dpy = ora_init_pex( argv, &pexinfo ); if ( !dpy ) exit(1); if ( STRUCTURE_SPT( pexinfo ) ) { create_structures( dpy ); } else { fprintf( stderr, "Structure not supported.\n" ); exit(1); } /* Loop through the list and print the structure contents. */ for ( i = 0; i < num_structs; i++ ) print_structure( structs[i] ); XCloseDisplay( dpy ); return 0; }