libnode/demo/nddemo0.c

704 lines
23 KiB
C
Raw Normal View History

/*---------------------------------------------------------------------------------*/
/* $RCSfile: nddemo0.c,v $ */
/*---------------------------------------------------------------------------------*/
/* $Revision: 2.5 $ */
/* $Name: $ */
/* $Date: 2003/07/16 00:13:20 $ */
/* $Author: agibert $ */
/*---------------------------------------------------------------------------------*/
2002-02-28 23:00:00 +01:00
/*---------------------------------------------------------------------------------*/
/* This file is part of LibNode */
/* */
/* LibNode is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public Licence as published by */
/* the Free Software Foundation; either version 2 of the License, or */
/* (at your option) any later version. */
/* */
/* LibNode is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU Lesser General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with Foobar; if not, write to the Free Software */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/*---------------------------------------------------------------------------------*/
#include <stdio.h>
#include <errno.h>
#include <node.h>
//#include <windows.h>
#define DEMO_FILE_NAME "nddemo0.dat"
#define STRING_LEN (short)1024
#define STRING_SIZE (short)(STRING_LEN + 1)
#define VAL_LEN (short)64
#define VAL_SIZE (short)(VAL_LEN + 1)
#define INDEX_NB (short)4
#define NDT_CMD_INDEX0_PRINT (NDT_Command)64
#define NDT_CMD_INDEX1_PRINT (NDT_Command)65
#define NDT_CMD_INDEX2_PRINT (NDT_Command)66
#define NDT_CMD_INDEX3_PRINT (NDT_Command)67
typedef struct Demo_Value
{
char Val0[VAL_SIZE];
char Val1[VAL_SIZE];
char Val2[VAL_SIZE];
char Val3[VAL_SIZE];
} Demo_Value;
/*---------------------------------------------------------------------------------*/
/* Prototypes */
/*---------------------------------------------------------------------------------*/
NDT_Status Manager_Demo_DS( NDT_Root *, NDT_Index_Id, NDT_Node *, NDT_Command, va_list);
void DataStruct_Load( NDT_Root *, FILE *);
void DataStruct_Load( NDT_Root *, FILE *);
void Demo( char *, short);
int main( int, char **);
/*---------------------------------------------------------------------------------*/
/* */
/*---------------------------------------------------------------------------------*/
NDT_Status Manager_Demo_DS( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, NDT_Node *Node_Ptr, NDT_Command Command, va_list Args)
{
NDT_Command_Name Command_Name;
switch( Command)
{
case NDD_CMD_MANAGER_VERSION:
{
NDT_Version_Name *Version_Name_Ptr = (NDT_Version_Name *)va_arg( Args, NDT_Version_Name *);
Command_Name = "NDD_CMD_MANAGER_VERSION";
*Version_Name_Ptr = "$Revision: 2.5 $ $Name: $ $Date: 2003/07/16 00:13:20 $ $Author: agibert $";
return( NDS_OK);
}
case NDD_CMD_INDEX_GET:
{
/*
NDT_Index_Id *Reply_Index_Id_Ptr = (NDT_Index_Id *)va_arg( Args, NDT_Index_Id *);
NDT_Command *Reply_Command_Ptr = (NDT_Command *)va_arg( Args, NDT_Command *);
NDT_Command Cmd = (NDT_Command)va_arg( Args, NDT_Command);
void *Value_ptr = (void *)va_arg( Args, void *);
*/
NDT_Index_Id *Reply_Index_Id_Ptr = (NDT_Index_Id *)va_arg( Args, NDT_Index_Id *);
NDT_Command *Reply_Command_Ptr = (NDT_Command *)va_arg( Args, NDT_Command *);
NDT_Command Cmd = (NDT_Command)va_arg( Args, NDT_Command);
Demo_Value *Value_ptr = (Demo_Value *)va_arg( Args, Demo_Value *);
Command_Name = "NDD_CMD_INDEX_GET";
switch(Cmd)
{
case NDT_CMD_INDEX0_PRINT:
{
*Reply_Index_Id_Ptr = 0;
*Reply_Command_Ptr = NDD_CMD_VALUE_PRINT;
break;
}
case NDT_CMD_INDEX1_PRINT:
{
*Reply_Index_Id_Ptr = 1;
*Reply_Command_Ptr = NDD_CMD_VALUE_PRINT;
break;
}
case NDT_CMD_INDEX2_PRINT:
{
*Reply_Index_Id_Ptr = 2;
*Reply_Command_Ptr = NDD_CMD_VALUE_PRINT;
break;
}
case NDT_CMD_INDEX3_PRINT:
{
*Reply_Index_Id_Ptr = 3;
*Reply_Command_Ptr = NDD_CMD_VALUE_PRINT;
break;
}
default:
{
*Reply_Index_Id_Ptr = Index_Id;
*Reply_Command_Ptr = Cmd;
break;
}
}
return( NDS_OK);
}
case NDD_CMD_VALUE_ALLOC:
{
/*
void **Value_Ptr_Ptr = (void **)va_arg( Args, void **);
va_list user_args = (va_list)va_arg( Args, va_list);
user_type user_data = (user_type)va_arg( user_args, user_type);
... = (...)va_arg( user_args, ...);
*/
Demo_Value **Value_Ptr_Ptr = va_arg( Args, Demo_Value **);
Command_Name = "NDD_CMD_VALUE_ALLOC";
if( ( *Value_Ptr_Ptr = (Demo_Value *)malloc( sizeof(Demo_Value))) == NULL)
{
return(NDS_ERRMEM);
}
else
{
(*Value_Ptr_Ptr)->Val0[0] = '\0';
(*Value_Ptr_Ptr)->Val1[0] = '\0';
(*Value_Ptr_Ptr)->Val2[0] = '\0';
(*Value_Ptr_Ptr)->Val3[0] = '\0';
return( NDS_OK);
}
}
case NDD_CMD_VALUE_FREE:
{
/*
void *Value_Ptr = (void *)va_arg( Args, void *);
va_list user_args = (va_list)va_arg( Args, va_list);
user_type user_data = (user_type)va_arg( user_args, user_type);
... = (...)va_arg( user_args, ...);
*/
Demo_Value *Value_Ptr = (Demo_Value *)va_arg( Args, Demo_Value *);
Command_Name = "NDD_CMD_VALUE_FREE";
free( Value_Ptr);
return( NDS_OK);
}
case NDD_CMD_VALUE_COMP:
{
/*
void *Value1_Ptr = (void *)va_arg( Args, void *);
void *Value2_Ptr = (void *)va_arg( Args, void *);
va_list user_args = (va_list)va_arg( Args, va_list);
user_type user_data = (user_type)va_arg( user_args, user_type);
... = (...)va_arg( user_args, ...);
*/
Demo_Value *Value1_Ptr = va_arg( Args, Demo_Value *);
Demo_Value *Value2_Ptr = va_arg( Args, Demo_Value *);
Command_Name = "NDD_CMD_VALUE_COMP";
switch( Index_Id)
{
case 0:
{
int rc;
rc = strcmp( Value1_Ptr->Val0, Value2_Ptr->Val0);
if( rc < 0)
{
return(NDS_LOWER);
}
else
{
if( rc > 0)
{
return(NDS_GREATER);
}
else
{
return(NDS_EQUAL);
}
}
}
case 1:
{
int val1 = atoi(Value1_Ptr->Val1);
int val2 = atoi(Value2_Ptr->Val1);
if( val1 < val2)
{
return(NDS_LOWER);
}
else
{
if( val1 > val2)
{
return(NDS_GREATER);
}
else
{
return(NDS_EQUAL);
}
}
}
case 2:
{
int val1 = strlen(Value1_Ptr->Val2);
int val2 = strlen(Value2_Ptr->Val2);
if( val1 < val2)
{
return(NDS_LOWER);
}
else
{
if( val1 > val2)
{
return(NDS_GREATER);
}
else
{
return(NDS_EQUAL);
}
}
}
default:
{
printf( "Unknown COMP idx (%d) !\n", Index_Id);
return( NDS_KO);
}
}
return( NDS_OK);
}
case NDD_CMD_VALUE_ADD:
{
/*
void *Value_Ptr = (void *)va_arg( Args, void *);
va_list user_args = (va_list)va_arg( Args, va_list);
user_type user_data = (user_type)va_arg( user_args, user_type);
... = (...)va_arg( user_args, ...);
*/
Command_Name = "NDD_CMD_VALUE_ADD";
return( NDS_OK);
}
case NDD_CMD_VALUE_REMOVE:
{
/*
void *Value_Ptr = (void *)va_arg( Args, void *);
va_list user_args = (va_list)va_arg( Args, va_list);
user_type user_data = (user_type)va_arg( user_args, user_type);
... = (...)va_arg( user_args, ...);
*/
Command_Name = "NDD_CMD_VALUE_REMOVE";
return( NDS_OK);
}
case NDD_CMD_VALUE_PRINT:
{
/*
NDT_Node *Next_Node_Ptr = (NDT_Node *)va_arg( Args, NDT_Node *);
va_list lib_args = (va_list)va_arg( Args, va_list);
FILE *Out = (FILE *)va_arg( lib_args, FILE *);
NDT_Recursive_Mode Recursive_Mode = (NDT_Recursive_Mode)va_arg( lib_args, NDT_Recursive_Mode);
NDT_Recursive_Depth Recursive_Depth = (NDT_Recursive_Depth)va_arg( lib_args, NDT_Recursive_Depth);
va_list user_args = (va_list)va_arg( lib_args, va_list);
user_type user_data = (user_type)va_arg( user_args, user_type);
... = (...)va_arg( user_args, ...);
void *Value_Ptr = Node_Ptr->Value;
*/
NDT_Node *Next_Node_Ptr = (NDT_Node *)va_arg( Args, NDT_Node *);
va_list lib_args = (va_list)va_arg( Args, va_list);
FILE *Out = (FILE *)va_arg( lib_args, FILE *);
NDT_Recursive_Mode Recursive_Mode = (NDT_Recursive_Mode)va_arg( lib_args, NDT_Recursive_Mode);
NDT_Recursive_Depth Recursive_Depth = (NDT_Recursive_Depth)va_arg( lib_args, NDT_Recursive_Depth);
Demo_Value *Value_Ptr = Node_Ptr->Value;
Command_Name = "NDD_CMD_VALUE_PRINT";
fprintf( Out, "Val0: (%s) Val1: (%s) Val2: (%s) Val3: (%s)\n",
Value_Ptr->Val0, Value_Ptr->Val1, Value_Ptr->Val2, Value_Ptr->Val3);
return( NDS_OK);
}
case NDD_CMD_INFO_PRINT:
{
/*
NDT_Node *Next_Node_Ptr = (NDT_Node *)va_arg( Args, NDT_Node *);
va_list lib_args = (va_list)va_arg( Args, va_list);
FILE *Out = (FILE *)va_arg( lib_args, FILE *);
NDT_Recursive_Mode Recursive_Mode = (NDT_Recursive_Mode)va_arg( lib_args, NDT_Recursive_Mode);
NDT_Recursive_Depth Recursive_Depth = (NDT_Recursive_Depth)va_arg( lib_args, NDT_Recursive_Depth);
va_list user_args = (va_list)va_arg( lib_args, va_list);
user_type user_data = (user_type)va_arg( user_args, user_type);
... = (...)va_arg( user_args, ...);
void *Value_Ptr = Node_Ptr->Value;
*/
Command_Name = "NDD_CMD_INFO_PRINT";
return( NDS_OK);
}
default:
{
printf( "Demo_DS_Manager() called with an undefined command %d\n", Command);
return NDS_ERRAPI;
}
}
printf( "Demo_DS_Manager() called with command %d (%s)\n", Command, Command_Name);
return NDS_OK;
}
/*---------------------------------------------------------------------------------*/
/* */
/*---------------------------------------------------------------------------------*/
void DataStruct_Load( NDT_Root *ds_ptr, FILE *demo_file)
{
NDT_Status status;
char *str;
char input_string[STRING_SIZE];
char val0[VAL_SIZE], val1[VAL_SIZE], val2[VAL_SIZE], val3[VAL_SIZE];
Demo_Value *value_ptr;
while( !feof( demo_file))
{
str = fgets( input_string, STRING_SIZE, demo_file);
if( ( (const int)str != EOF) && ( str != NULL))
{
if( strlen( input_string) >= STRING_LEN)
{
printf( "Input line too long ! Skipping ...");
while( !feof( demo_file) && strlen( input_string) >= STRING_LEN)
{
printf( ".");
str = fgets( input_string, STRING_SIZE, demo_file);
}
printf("\n");
}
else
{
if( input_string[0] != '#')
{
if( sscanf( input_string, "%s %s %s %s\n", val0, val1, val2, val3) != 4)
{
printf( "Input line scaning error... Skipping !\n");
}
else
{
printf( "Input line read: \t(%s)\t(%s)\t(%s)\t(%s)\n", val0, val1, val2, val3);
printf( "Allocate Vallue: ");
if( ( status = ND_Value_Alloc( ds_ptr, (void **)&value_ptr)) != NDS_OK)
{
printf( "ND_Value_Alloc() failed (%d) !\n", status);
}
else
{
printf( "Ok !\n");
strcpy( value_ptr->Val0, val0);
strcpy( value_ptr->Val1, val1);
strcpy( value_ptr->Val2, val2);
strcpy( value_ptr->Val3, val3);
printf( "Add Vallue: ");
if( ( status = ND_DataStruct_Value_Add( ds_ptr, (void **)value_ptr)) != NDS_OK)
{
printf( "ND_Value_Add() failed (%d) !\n", status);
}
else
{
printf( "Ok !\n");
}
}
}
}
}
}
}
}
/*---------------------------------------------------------------------------------*/
/* */
/*---------------------------------------------------------------------------------*/
void DataStruct_Dump( NDT_Root *ds_ptr)
{
NDT_Status status;
printf( "Print DataStructure Info:\n");
if( ( status = ND_DataStruct_Info_Print( stdout, ds_ptr, NDD_RECURSIVE_MODE_PARENT_CHILD, 0, 0)) != NDS_OK)
{
printf( "ND_DataStruct_Info_Print() failed (%d) !\n", status);
}
printf( "\n");
printf( "Traverse DataStructure Index 0:\n");
if( ( status = ND_DataStruct_Traverse( ds_ptr, NDT_CMD_INDEX0_PRINT, stdout, NDD_RECURSIVE_MODE_PARENT, 0)) != NDS_OK)
{
printf( "ND_DataStruct_Traverse() failed (%d) !\n", status);
}
printf( "\n");
printf( "Traverse DataStructure Index 1:\n");
if( ( status = ND_DataStruct_Traverse( ds_ptr, NDT_CMD_INDEX1_PRINT, stdout, NDD_RECURSIVE_MODE_PARENT, 0)) != NDS_OK)
{
printf( "ND_DataStruct_Traverse() failed (%d) !\n", status);
}
printf( "\n");
printf( "Traverse DataStructure Index 2:\n");
if( ( status = ND_DataStruct_Traverse( ds_ptr, NDT_CMD_INDEX2_PRINT, stdout, NDD_RECURSIVE_MODE_PARENT, 0)) != NDS_OK)
{
printf( "ND_DataStruct_Traverse() failed (%d) !\n", status);
}
printf( "\n");
printf( "Traverse DataStructure Index 3:\n");
if( ( status = ND_DataStruct_Traverse( ds_ptr, NDT_CMD_INDEX3_PRINT, stdout, NDD_RECURSIVE_MODE_PARENT, 0)) != NDS_OK)
{
printf( "ND_DataStruct_Traverse() failed (%d) !\n", status);
}
printf( "\n");
}
/*---------------------------------------------------------------------------------*/
/* */
/*---------------------------------------------------------------------------------*/
void Demo( char *Demo_File_Name, short Optimized_Mode)
{
NDT_Status status;
NDT_Root *demo_ds_ptr;
NDT_Index_Type index_type_initial_tab[INDEX_NB] =
{
(NDD_INDEX_STATUS_OPENED | NDD_INDEX_TYPE_LIST | NDD_INDEX_SUBTYPE_FIFO),
(NDD_INDEX_STATUS_OPENED | NDD_INDEX_TYPE_LIST | NDD_INDEX_SUBTYPE_FIFO),
(NDD_INDEX_STATUS_OPENED | NDD_INDEX_TYPE_LIST | NDD_INDEX_SUBTYPE_FIFO),
(NDD_INDEX_STATUS_OPENED | NDD_INDEX_TYPE_LIST | NDD_INDEX_SUBTYPE_FIFO)
};
NDT_Index_Type index_type_final_tab[INDEX_NB] =
{
(NDD_INDEX_STATUS_OPENED | NDD_INDEX_TYPE_TREE | NDD_INDEX_SUBTYPE_BALANCED),
(NDD_INDEX_STATUS_OPENED | NDD_INDEX_TYPE_LIST | NDD_INDEX_SUBTYPE_SORTED),
(NDD_INDEX_STATUS_OPENED | NDD_INDEX_TYPE_TREE | NDD_INDEX_SUBTYPE_BALANCED),
(NDD_INDEX_STATUS_OPENED | NDD_INDEX_TYPE_LIST | NDD_INDEX_SUBTYPE_FIFO)
};
NDT_Index_Type *index_type_ptr;
FILE *demo_file;
printf( "Create demo DataStructure: ");
if( Optimized_Mode)
{
index_type_ptr = index_type_initial_tab;
}
else
{
index_type_ptr = index_type_final_tab;
}
if( ( status = ND_DataStruct_Open( &demo_ds_ptr, INDEX_NB, index_type_ptr, "Manager_Demo_DS", Manager_Demo_DS, NULL, NULL, NULL, NULL,0, NULL)) != NDS_OK)
{
printf( "ND_DataStruct_Open() failed (%d) !\n", status);
}
else
{
printf( "Ok !\n");
printf( "Open demo datafile: ");
if( ( demo_file = fopen( Demo_File_Name, "r")) == NULL)
{
printf( "fopen() failed (%d/%s) !\n", errno, strerror(errno));
}
else
{
printf( "Ok !\n");
printf( "\n\n\nLaod Data File:\n\n");
DataStruct_Load( demo_ds_ptr, demo_file);
if( Optimized_Mode)
{
printf( "\n\n\nInitial Structure Dump:\n\n");
DataStruct_Dump( demo_ds_ptr);
printf( "Convert data structure: ");
if( ( status = ND_DataStruct_Convert( demo_ds_ptr, index_type_final_tab)) != NDS_OK)
{
printf( "ND_DataStruct_Convert() failed (%d) !\n", status);
}
else
{
printf( "Ok !\n");
}
}
printf( "\n\n\nFinal Structure Dump:\n\n");
DataStruct_Dump( demo_ds_ptr);
printf( "Close demo datafile: ");
if( fclose( demo_file) != 0)
{
printf( "fclose() failed (%d/%s) !\n", errno, strerror(errno));
}
else
{
printf( "Ok !\n");
}
}
printf( "Close demo DataStructure: ");
if( ( status = ND_DataStruct_Close( demo_ds_ptr)) != NDS_OK)
{
printf( "ND_DataStruct_Close() failed (%d) !\n", status);
}
else
{
printf( "Ok !\n");
}
}
}
/*---------------------------------------------------------------------------------*/
/* */
/*---------------------------------------------------------------------------------*/
int main( int argc, char **argv)
{
2003-01-17 09:06:57 +01:00
printf( "Non Optimized Demo\n------------------\n\n\n");
Demo( DEMO_FILE_NAME, 0);
2003-01-17 09:06:57 +01:00
printf( "\n\n\n\n\nOptimized Demo\n--------------\n\n\n");
Demo( DEMO_FILE_NAME, 1);
{
/*
HMODULE my_module;
char buf[256];
FARPROC my_function;
my_module = GetModuleHandle( NULL);
printf( "Module_Handle: (%p)\n", my_module);
buf[0] = '\0';
GetModuleFileName( my_module, buf, 255);
printf( "Module_Name: (%s)\n", buf);
my_function = GetProcAddress( my_module, "zob");
printf( "Function_Addr: (%p)\n", my_function);
my_function();
*/
}
}
/*
__declspec(dllexport) void zob(void)
{
printf( "Zoby la mouche qui p<>te...!!!\n");
}
*/