示例程序

完整的示例程序如下图所示。

图 2:使用 Hcon 包的示例程序

/*  hcon_sample.c: A sample program showing how to use Hcon package */
#include "prg.h"
#include "vos_env.h"
#include "op_std_funcs.h"
#include "prg_prototypes.h"
#include "prg_list_funcs.h"
#include "hcon.h"
int
m3_main(int argc, char * argv[])
	{
	char*					card_fname;
	char*					router_fname;
	HconT_Library*					lib;
	HconT_Demand *					dmd;
	PrgT_List*					routers;
	HconT_Device*					dev, card;
	PrgT_List*					levels;
	int					i, j;
	double					cost;
	FIN (m3_main (argc, argv));
	/* initialize PRG package */
	prg_init (argc, argv, PRGC_INIT_OPT_NONE);
	/* Usage:  hcon_sample -card <card_DCR_file> */
	/* -router <router_DCR_file> 	*/
	card_fname = argv[2];
	router_fname = argv[4];
	/* Create level names list. */
	levels = prg_list_create ();
	prg_list_insert (levels, "Port", PRGC_LISTPOS_TAIL);
	prg_list_insert (levels, "Card", PRGC_LISTPOS_TAIL);
	prg_list_insert (levels, "Router", PRGC_LISTPOS_TAIL);
	/* create a device library with three levels of hierarchy */
	lib = Hcon_Library_Create ("My Devices", 3, levels);
	/* Destroy level names list. Don’t destroy the elements; */
	/* they are string constants. */
	prg_list_destroy (levels, PRGC_FALSE);
	/* load the card and router DCR files */
	Hcon_Library_Nth_Level_DCR_File_Load (lib, card_fname, 1);
	Hcon_Library_Nth_Level_DCR_File_Load (lib, router_fname, 2);
	/* create a demand for 4 DS3 ports and 3 OC48 ports */
	demand = Hcon_Demand_Create (); 
	Hcon_Demand_Devices_Needed_Add (demand, lib, 0, "Port", "DS3", 4);
	Hcon_Demand_Devices_Needed_Add (demand, lib, 0, "Port", "OC48", 3);
	routers = Hcon_Device_Build_From_Demand (demand, lib);
	if (routers == NULL)
		printf ("Failed \n");
	else 
		{
		for (i = 0 ; i < prg_list_size (routers); i++)
			{
			dev = (HconT_Device*) prg_list_access (routers, i);
			cost = Hcon_Device_Cost_Get_Inclusive (dev);
			Hcon_Device_Model_Name_Get (dev, vendor);
			Hcon_Device_Model_Name_Get (dev, model);
			printf ("Router %d chosen : %s - %s, total cost %lf\n", i,
				vendor, model, cost); 		
			for (j = 0 ; j < Hcon_Device_Components_Count_Get (dev); j++)
				{
				card = Hcon_Device_Nth_Component_Get (dev, j);
				cost = Hcon_Device_Cost_Get_Inclusive (card);
				Hcon_Device_Model_Name_Get (card, vendor);
				Hcon_Device_Model_Name_Get (card, model);
				printf ("\t card %d installed: %s - %s, total cost %lf\n",
					j, vendor, model, cost);
				}
			/* this  will destroy the router as well as the cards */
			/* in it*/				
			Hcon_Device_Destroy (dev);
			}
	/* shallow-free the list only because the routers in the list have */
	/* already been freed by the  Hcon_Device_Destroy() call above.    */
		prg_list_destroy (routers, 0);
		}
	Hcon_Demand_Destroy (demand); 
	Hcon_Library_Destroy (lib);
	FRET (0);
	}