#### Examples Using Pointers

The examples in this topic contain the following files, listed here with the function of each file:

Example | Function
STRING_TREE.IDL | Defines data types and interfaces
CLIENT.C | The user of the interface
MANAGER.C | The server code that implements the procedure
SERVER.C | Declares the server; enables the client code to find the interface it needs
STRING_TREE.OUTPUT | Shows the output

The STRING_TREE.IDL Example

[uuid(0144d600-2d28-11c9-a812-08002b0ecef1), version(0)]
interface string_tree
{
/*
* Maximum length of a string in the tree
*/
const long int st_c_name_len = 32;

/*
* Definition of a node in the tree.
*/
typedef struct node
{
[string] char name[0..st_c_name_len];
[ptr] struct node *left;
[ptr] struct node *right;
} st_node_t;

/*
* Operation that prunes the left subtree of the specified
* tree and returns it as the value.
*/
st_node_t *st_prune_left (
[in, out] st_node_t *tree /* root of tree by ref */
);
}

The CLIENT.C Example

#include <stdio.h>
#include "string_tree.h"

#include <stdlib.h>

/*
** Routine to print a depiction of the tree
*/
void st_print_tree (tree, indent)
st_node_t *tree;
int indent;
{
int i;
if (tree == NULL) return;
for (i = 0; i < indent; i++) printf(" ");
printf("%s\n",tree->name);
st_print_tree(tree->left, indent + 1);
st_print_tree(tree->right, indent + 1);
}

/*
** Create a tree with a few nodes
*/
st_node_t *st_make_tree( )
{
st_node_t *root = (st_node_t *)malloc(sizeof(st_node_t));
strcpy(root->name,"Root Node");

/* left subtree node */
root->left = (st_node_t *)malloc(sizeof(st_node_t));
strcpy(root->left->name,"Left subtree");
/* left subtree children */
root->left->right = NULL;
root->left->left = (st_node_t *)malloc(sizeof(st_node_t));
strcpy(root->left->left->name,"Child of left subtree");
root->left->left->left = NULL;
root->left->left->right = NULL;

/* right subtree node */
root->right = (st_node_t *)malloc(sizeof(st_node_t));
strcpy(root->right->name,"Right subtree");
root->right->left = NULL;
root->right->right = NULL;

return root;
}

main( )
{
st_node_t *tree;
st_node_t *subtree;

/* setup and print original tree */
tree = st_make_tree( );
printf("Original Tree:\n");
st_print_tree(tree, 1);

/* call the prune routine */
subtree = st_prune_left (tree);

/* print the resulting trees */
printf("\nPruned Tree:\n");
st_print_tree(tree, 1);

printf("\nPruned subtree:\n");
st_print_tree(subtree, 1);
}

The MANAGER.C Example

#include <stdio.h>
#include "string_tree.h"

/*
** Prune the left subtree of the specified tree and return
** it as the function value.
*/
st_node_t *st_prune_left (tree)
/* [in,out] */ st_node_t *tree;
{
st_node_t *left_sub_tree = tree->left;
tree->left = (st_node_t *)NULL;
return left_sub_tree;
}

The SERVER.C Example

#include <stdio.h>
#include "string_tree.h" /* header created by idl compiler */
#define check_error(s, msg) if(s != rpc_s_ok) \
{fprintf(stderr, "%s", msg); exit(1);}

main ( )
{
unsigned32 status; /* error status (nbase.h) */
rpc_binding_vector_p_t binding_vector; /* set of binding handles (rpc.h) */

rpc_server_register_if( /* register interface with RPC runtime */
string_tree_v0_0_s_ifspec, /* interface specification (string_tree.h) */
NULL,
NULL,
&status /* error status */
);
check_error(status, "Can't register interface\n");

rpc_server_use_all_protseqs( /* establish protocol sequences */
rpc_c_protseq_max_calls_default, /* concurrent calls server takes (rpc.h) */
&status
);
check_error(status, "Can't establish protocol sequences\n");

rpc_server_inq_bindings( /* get set of this server's binding handles */
&binding_vector,
&status
);
check_error(status, "Can't get binding handles\n");

rpc_ep_register( /* register addresses in endpoint map database */
string_tree_v0_0_s_ifspec, /* interface specification (string_tree.h) */
binding_vector, /* the set of binding handles */
NULL,
"",
&status
);
check_error(status, "Can't add address to the endpoint database\n");

rpc_ns_binding_export( /* establish namespace entry */
rpc_c_ns_syntax_dce, /* syntax of the entry name (rpc.h) */
"string_tree", /* entry name in directory service */
&string_tree_v0_0_s_ifspec, /* interface specification (string_tree.h) */
binding_vector, /* the set of binding handles */
NULL,
&status
);
check_error(status, "Can't export to directory service\n");

rpc_binding_vector_free( /* free set of binding handles */
&binding_vector,
status
);
check_error(status, "Can't free binding handles and vector\n");

rpc_server_listen( /* listen for remote calls */
rpc_c_listen_max_calls_default, /* concurrent calls server executes (rpc.h) */
&status
);
check_error(status, "rpc listen failed\n");
}

The STRING_TREE.OUTPUT Example

Original Tree:
Root Node
Left subtree
Child of left subtree
Right subtree
Pruned Tree:
Root Node
Right subtree
Pruned subtree:
Left subtree
Child of left subtree

###