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 60 61 62 63 64 65 66 67 68
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "npstringarray.h"
const char *NP_Stringarray::operator[]( int position )
{
if ( mutex )
return NULL;
mutex = 1;
if ( position >= total || position < 0 )
{
snprintf( error_message, sizeof error_message, "NP_Stringarray: "
"operator[](): argument %d out of range.", position );
mutex = 0;
return NULL;
}
mutex = 0;
return ( const char *)item_list[ position ];
}
int NP_Stringarray::operator[]( const char *item )
{
if ( mutex )
return 3;
mutex = 1;
if ( !total )
{
strcpy( error_message, "NP_Stringarray: operator[](): this object is "
"empty." );
mutex = 0;
return -1;
}
if ( item == NULL )
{
strcpy( error_message, "NP_Stringarray: operator[](): NULL item pointer"
" passed as argument." );
mutex = 0;
return -1;
}
int i;
char **pointer = item_list;
for( i = 0; i < total; ++i )
if ( !strcmp( item, *pointer++ ))
break;
if ( i == total )
{
snprintf( error_message, sizeof error_message,
"NP_Stringarray: operator[](): no such item "
"has been added to this object: %s.", item );
mutex = 0;
return -2;
}
mutex = 0;
return i;
}
|