1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "npstringarray.h"
int NP_Stringarray::add_item( const char *source )
{
if ( mutex )
return 3;
mutex = 1;
if ( source == NULL )
{
strcpy( error_message, "NP_Stringarray: add_item(): NULL source item"
" passed as argument." );
mutex = 0;
return 1;
}
if ( total && !duplicates_allowed )
{
char **pointer = item_list;
int i;
for( i = 0; i < total; ++i )
{
if ( !strcmp( source, *pointer ))
break;
++pointer;
}
if ( i != total )
{
snprintf( error_message, sizeof error_message, "NP_Stringarray: "
" add_item(): %s has already been added to this object.",
source );
mutex = 0;
return 2;
}
}
if (( item_list = ( char **)realloc( item_list,
++total * sizeof *item_list ))
== NULL )
{
perror( "realloc" );
exit( 1 );
}
if (( item_list[ total - 1 ] = strdup( source )) == NULL )
{
perror( "strdup" );
exit( 1 );
}
mutex = 0;
return 0;
}
|