libnode/demo/nddemo0.c
Arnaud G. GIBERT 70b7e5a49e - Complete API function header documentation,
- ND_Value_Alloc() argument normalisation: API Break!
2023-08-17 17:53:27 +02:00

726 lines
21 KiB
C

/*---------------------------------------------------------------------------------*/
/* $RCSfile: nddemo0.c,v $ */
/*---------------------------------------------------------------------------------*/
/* $Revision: 2.9 $ */
/* $Name: $ */
/* $Date: 2010/06/06 21:27:12 $ */
/* $Author: agibert $ */
/*---------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------*/
/* 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 LibNode; 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>
#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 NDD_CMD_INDEX0_PRINT ( NDT_Command) 64
#define NDD_CMD_INDEX1_PRINT ( NDT_Command) 65
#define NDD_CMD_INDEX2_PRINT ( NDT_Command) 66
#define NDD_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 Demo_DS_Manager( 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 Demo_DS_Manager( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, NDT_Node *Node_Ptr, NDT_Command Command, va_list *Args_Ptr)
{
NDT_Command_Name Command_Name;
switch( Command)
{
case NDD_CMD_MANAGER_VERSION:
{
ND_VA_ARG_GET( Version_Name_Ptr, *Args_Ptr, NDT_Version_Name *);
Command_Name = "NDD_CMD_MANAGER_VERSION";
*Version_Name_Ptr = "$Revision: 2.9 $ $Name: $ $Date: 2010/06/06 21:27:12 $ $Author: agibert $";
return( NDS_OK);
}
case NDD_CMD_INDEX_GET:
{
/*
ND_VA_ARG_GET( Reply_Index_Id_Ptr, *Args_Ptr, NDT_Index_Id *);
ND_VA_ARG_GET( Reply_Command_Ptr, *Args_Ptr, NDT_Command *);
ND_VA_ARG_GET( Cmd, *Args_Ptr, NDT_Command);
ND_VA_ARG_GET( Value_Ptr, *Args_Ptr, void *);
*/
ND_VA_ARG_GET( Reply_Index_Id_Ptr, *Args_Ptr, NDT_Index_Id *);
ND_VA_ARG_GET( Reply_Command_Ptr, *Args_Ptr, NDT_Command *);
ND_VA_ARG_GET( Cmd, *Args_Ptr, NDT_Command);
ND_VA_ARG_GET( Value_Ptr, *Args_Ptr, Demo_Value *);
Command_Name = "NDD_CMD_INDEX_GET";
switch(Cmd)
{
case NDD_CMD_INDEX0_PRINT:
{
*Reply_Index_Id_Ptr = 0;
*Reply_Command_Ptr = NDD_CMD_VALUE_PRINT;
break;
}
case NDD_CMD_INDEX1_PRINT:
{
*Reply_Index_Id_Ptr = 1;
*Reply_Command_Ptr = NDD_CMD_VALUE_PRINT;
break;
}
case NDD_CMD_INDEX2_PRINT:
{
*Reply_Index_Id_Ptr = 2;
*Reply_Command_Ptr = NDD_CMD_VALUE_PRINT;
break;
}
case NDD_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:
{
/*
ND_VA_ARG_GET( Value_Ptr_Ptr, *Args_Ptr, void **);
ND_VA_LIST_OPEN( user_args, *Args_Ptr);
ND_VA_ARG_GET( user_data, user_args, user_type);
ND_VA_ARG_GET( ..., user_args, ...);
ND_VA_LIST_CLOSE( user_args);
*/
ND_VA_ARG_GET( Value_Ptr_Ptr, *Args_Ptr, 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:
{
/*
ND_VA_ARG_GET( Value_Ptr, *Args_Ptr, void *);
ND_VA_LIST_OPEN( user_args, *Args_Ptr);
ND_VA_ARG_GET( user_data, user_args, user_type);
ND_VA_ARG_GET( ..., user_args, ...);
ND_VA_LIST_CLOSE( user_args);
*/
ND_VA_ARG_GET( Value_Ptr, *Args_Ptr, Demo_Value *);
Command_Name = "NDD_CMD_VALUE_FREE";
free( Value_Ptr);
return( NDS_OK);
}
case NDD_CMD_VALUE_COMP:
{
/*
ND_VA_ARG_GET( Value1_Ptr, *Args_Ptr, void *);
ND_VA_ARG_GET( Value2_Ptr, *Args_Ptr, void *);
ND_VA_LIST_OPEN( user_args, *Args_Ptr);
ND_VA_ARG_GET( user_data, user_args, user_type);
ND_VA_ARG_GET( ..., user_args, ...);
ND_VA_LIST_CLOSE( user_args);
*/
ND_VA_ARG_GET( Value1_Ptr, *Args_Ptr, Demo_Value *);
ND_VA_ARG_GET( Value2_Ptr, *Args_Ptr, 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:
{
/*
ND_VA_ARG_GET( Value_Ptr, *Args_Ptr, void *);
ND_VA_LIST_OPEN( user_args, *Args_Ptr);
ND_VA_ARG_GET( user_data, user_args, user_type);
ND_VA_ARG_GET( ..., user_args, ...);
ND_VA_LIST_CLOSE( user_args);
*/
Command_Name = "NDD_CMD_VALUE_ADD";
return( NDS_OK);
}
case NDD_CMD_VALUE_REMOVE:
{
/*
ND_VA_ARG_GET( Value_Ptr, *Args_Ptr, void *);
ND_VA_LIST_OPEN( user_args, *Args_Ptr);
ND_VA_ARG_GET( user_data, user_args, user_type);
ND_VA_ARG_GET( ..., user_args, ...);
ND_VA_LIST_CLOSE( user_args);
*/
Command_Name = "NDD_CMD_VALUE_REMOVE";
return( NDS_OK);
}
case NDD_CMD_VALUE_PRINT:
{
/*
ND_VA_ARG_GET( Next_Node_Ptr, *Args_Ptr, NDT_Node *);
ND_VA_LIST_OPEN( lib_args, *Args_Ptr);
ND_VA_ARG_GET( Out, lib_args, FILE *);
ND_VA_ARG_GET( Recursive_Mode, lib_args, NDT_Recursive_Mode);
ND_VA_ARG_GET( Recursive_Depth, lib_args, NDT_Recursive_Depth);
ND_VA_ARG_GET( Recursive_Offset, lib_args, NDT_Recursive_Offset);
ND_VA_LIST_OPEN( user_args, lib_args);
ND_VA_ARG_GET( user_data, user_args, user_type);
ND_VA_ARG_GET( ..., user_args, ...);
ND_VA_LIST_CLOSE( user_args);
ND_VA_LIST_CLOSE( lib_args);
*/
ND_VA_ARG_GET( Next_Node_Ptr, *Args_Ptr, NDT_Node *);
ND_VA_LIST_OPEN( lib_args, *Args_Ptr);
ND_VA_ARG_GET( Out, lib_args, FILE *);
ND_VA_ARG_GET( Recursive_Mode, lib_args, NDT_Recursive_Mode);
ND_VA_ARG_GET( Recursive_Depth, lib_args, NDT_Recursive_Depth);
ND_VA_ARG_GET( Recursive_Offset, lib_args, NDT_Recursive_Offset);
ND_VA_LIST_CLOSE( lib_args);
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:
{
/*
ND_VA_ARG_GET( Next_Node_Ptr, *Args_Ptr, NDT_Node *);
ND_VA_LIST_OPEN( lib_args, *Args_Ptr);
ND_VA_ARG_GET( Out, lib_args, FILE *);
ND_VA_ARG_GET( Recursive_Mode, lib_args, NDT_Recursive_Mode);
ND_VA_ARG_GET( Recursive_Depth, lib_args, NDT_Recursive_Depth);
ND_VA_ARG_GET( Recursive_Offset, lib_args, NDT_Recursive_Offset);
ND_VA_LIST_OPEN( user_args, lib_args);
ND_VA_ARG_GET( user_data, user_args, user_type);
ND_VA_ARG_GET( ..., user_args, ...);
ND_VA_LIST_CLOSE( user_args);
ND_VA_LIST_CLOSE( lib_args);
*/
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( 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( (void **)&value_ptr, ds_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, NDD_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, NDD_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, NDD_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, NDD_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( "Open library: ");
if( ( status = ND_Library_Open( NDD_TRUE)) != NDS_OK)
{
printf( "ND_Library_Open() failed (%d) !\n", status);
}
else
{
printf( "Ok !\n");
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, "Demo_DS_Manager", Demo_DS_Manager, 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");
}
}
printf( "Close library: ");
if( ( status = ND_Library_Close()) != NDS_OK)
{
printf( "ND_Library_Close() failed (%d) !\n", status);
}
else
{
printf( "Ok !\n");
}
}
}
/*---------------------------------------------------------------------------------*/
/* */
/*---------------------------------------------------------------------------------*/
int main( int argc, char **argv)
{
printf( "Non Optimized Demo\n------------------\n\n\n");
Demo( DEMO_FILE_NAME, 0);
printf( "\n\n\n\n\nOptimized Demo\n--------------\n\n\n");
Demo( DEMO_FILE_NAME, 1);
}