6.3.3 exampletable_access.c/*exampletable_access.c*/
/*
* note: this file originally auto-generated by mib2c using
* :mib2c.access_functions.conf,v 1.9 2004/10/14 12:57:33 dts12 exp $
*/
#include
#include
#include
#includeexampletable_access.h
#includeexampletable_enums.h
#include
struct exampletable_entry {
long machinenumber;
char machinestatus[10];
u_long checktime;
long monset;
struct exampletable_entry *next;
};
struct exampletable_entry *exampletable_head=null;
/* create a new row in the (unsorted)table */
struct exampletable_entry *
exampletable_createentry(long machinenumber,
char *machinestatus,
u_long checktime,
long monset
)
{
struct exampletable_entry *entry;
entry = snmp_malloc_typedef(structexampletable_entry);
if (!entry)
return null;
entry->machinenumber = machinenumber;
strcpy(entry->machinestatus ,machinestatus);
entry->checktime = checktime;
entry->monset =monset;
entry->next =exampletable_head;
exampletable_head = entry;
return entry;
}
/* remove a row from the table */
void
exampletable_removeentry( structexampletable_entry *entry )
{
struct exampletable_entry *ptr, *prev;
if (!entry)
return; /* nothing to remove */
for ( ptr = exampletable_head, prev = null;
ptr != null;
prev = ptr,ptr = ptr->next ) {
if ( ptr == entry )
break;
}
if ( !ptr )
return; /* can't find it */
if ( prev == null )
exampletable_head =ptr->next;
else
prev->next =ptr->next;
snmp_free( entry ); /* xxx - release any otherinternal resources */
}
void data_read(void)
{
file*fp;
int i;
struct exampletable_entry*temp;
fp=fopen(data.txt,r);
while(exampletable_head)/*clean link list begin*/
{
temp=exampletable_head->next;
exampletable_removeentry(exampletable_head);
exampletable_head=temp;
}/*cleanlink list end*/
temp=snmp_malloc_typedef(structexampletable_entry);
temp->next=null;
/*set up a link list begin*/
if(fp)
{
i=fscanf(fp,%d %s %d%d,&temp->machinenumber, temp->machinestatus,&temp->checktime,&temp->monset);
/*fscanf return reading var numbers .if eof return -1.feof()dosen't work well*/
while(i>0)
{
exampletable_createentry(temp->machinenumber,temp->machinestatus,temp->checktime,temp->monset);
i=fscanf(fp,%d%s %d %d,&temp->machinenumber, temp->machinestatus,&temp->checktime,&temp->monset);
}
snmp_free(temp);
fclose(fp);/*set up a link list end*/
}
else
{
printf(error:can'topen data.txt!\n);
/*exit(1);*/
}
}
static u_long long_ret;
/** returns the first data pointwithin the exampletable table data.
set the my_loop_context variable to the firstdata point structure
of your choice (from which you can find the nextone). this could
be anything from the first node in a linkedlist, to an integer
pointer containing the beginning of an arrayvariable.
set the my_data_context variable to something tobe returned to
you later that will provide you with the data toreturn in a given
row. this could be the same pointer as what my_loop_context is
set to, or something different.
the put_index_data variable contains a list ofsnmp variable
bindings, one for each index in your table. set the values of
each appropriately according to the datamatching the first row
and return the put_index_data variable at theend of the function.
*/
netsnmp_variable_list *
exampletable_get_first_data_point(void**my_loop_context, void **my_data_context,
netsnmp_variable_list *put_index_data,
netsnmp_iterator_info *mydata)
{
data_read();
*my_loop_context = exampletable_head;
*my_data_context = exampletable_head;
returnexampletable_get_next_data_point(my_loop_context, my_data_context,
put_index_data, mydata );
}
/** functionally the same asexampletable_get_first_data_point, but
my_loop_context has already been set to a previousvalue and should
be updated to the next in the list. for example, if it was a
linked list, you might want to cast it to your localdata type and
then return my_loop_context->next. the my_data_context pointer
should be set to something you need later and theindexes in
put_index_data updated again. */
netsnmp_variable_list *
exampletable_get_next_data_point(void**my_loop_context, void **my_data_context,
netsnmp_variable_list *put_index_data,
netsnmp_iterator_info *mydata)
{
structexampletable_entry *entry = (struct exampletable_entry *)*my_loop_context;
netsnmp_variable_list *idx = put_index_data;
if ( entry )
{
snmp_set_var_value( idx,(u_char *)&entry->machinenumber, sizeof(entry->machinenumber) );
idx =idx->next_variable;
*my_data_context = (void*)entry;
*my_loop_context =(struct exampletable_entry *)entry->next;
}
else
{
return null;
}
return put_index_data;
}
/** create a data_context fornon-existent rows that sets are performed on.
* return a void* pointer which will be passed to subsequent get_xxx
* and set_xxxfunctions for data retrival and modification during
* this setrequest.
*
* the indexesare encoded (in order) into the index_data pointer,
* and the columnobject which triggered the row creation is available
* via the columnparameter, if it would be helpful to use that information.
*/
/*
void *
exampletable_create_data_context(netsnmp_variable_list*index_data) {
return entry; /* xxx: you likely want to returna real pointer */
/*
}
*/
/** if the implemented set_*functions don't operate directly on the
real-live data (which is actually recommended), thenthis function
can be used to take a given my_data_context pointerand commit it
to whereever the modified data needs to be put backto. for
example, if this was a routing table you could publishthe modified
routes back into the kernel at this point.
new_or_del will be set to 1 if new, or -1 if it shouldbe deleted
or 0 if it is just a modification of an existing row.
if you free the data yourself, make sure to*my_data_context = null */
int
exampletable_commit_row(void**my_data_context, int new_or_del)
{
/** add any necessary commit code here */
/* */
/* return no errors. and there shouldn't be any!!! ever!!! you
should have checked the values long before this.*/
return snmp_err_noerror;
}
/* user-defined data access functions(per column) for data in table exampletable */
/*
* note:
* - these get_ routines must return data that will not befreed (ie,
* usestatic variables or persistent data). it will be copied, if
* needed,immediately after the get_ routine has been called.
* - these set routines must copy the incoming data and cannot take
* ownership of the memory passed in by the val pointer.
*/
/** xxx: return a data pointer to thedata for the machinenumber column and set
ret_len to itsproper size in bytes. */
long *get_machinenumber(void*data_context, size_t *ret_len) {
structexampletable_entry *entry=(struct exampletable_entry *)data_context;
long_ret=entry->machinenumber;
*ret_len=sizeof(long_ret);
return &long_ret;/** xxx: replace this with a pointer to a real value */
}
/** xxx: return a data pointer to thedata for the machinestatus column and set
ret_len to itsproper size in bytes. */
char *get_machinestatus(void*data_context, size_t *ret_len) {
structexampletable_entry *entry=(struct exampletable_entry *)data_context;
*ret_len=strlen(entry->machinestatus);
returnentry->machinestatus; /** xxx: replace this with a pointer to a real value*/
}
/** xxx: return a data pointer to thedata for the checktime column and set
ret_len to itsproper size in bytes. */
u_long *get_checktime(void*data_context, size_t *ret_len) {
structexampletable_entry *entry=(struct exampletable_entry *)data_context;
long_ret=entry->checktime;
*ret_len=sizeof(long_ret);
return &long_ret;/** xxx: replace this with a pointer to a real value */
}
/** xxx: return a data pointer to thedata for the monset column and set
ret_len to itsproper size in bytes. */
long *get_monset(void *data_context,size_t *ret_len) {
structexampletable_entry *entry=(struct exampletable_entry *)data_context;
long_ret=entry->monset;
*ret_len=sizeof(long_ret);
return &long_ret;/** xxx: replace this with a pointer to a real value */
}
/** xxx: set the value of the monsetcolumn and return
snmp_err_noerroron success
snmp_err_xxx for snmpdeterministic error codes
snmp_err_generr on genericfailures (a last result response). */
int set_monset(void *data_context,long *val, size_t val_len) {
file *fp;
struct exampletable_entry*temp=exampletable_head,*entry=data_context;
memcpy(&entry->monset,val, val_len);
fp=fopen(data.txt,w+);
if(!fp)
{debugmsgtl((set_monset,openfile failure\n));
returnsnmp_err_noaccess;
}
else
while(temp)
{
fprintf(fp,\n%d %s %d%d,temp->machinenumber,
temp->machinestatus,
temp->checktime,
temp->monset);
temp=temp->next;
}
fclose(fp);
returnsnmp_err_noerror; /** xxx: changeif an error occurs */
}
