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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "npstringarray.h"
int NP_Stringarray::replace_item( int i, const char *replacement )
{
if ( mutex )
return 3;
mutex = 1;
if ( i < 0 || i >= total )
{
snprintf( error_message, sizeof error_message,
"NP_Stringarray: replace_item(): index out of range: %d.", i );
mutex = 0;
return 2;
}
if ( replacement == NULL )
{
strcpy( error_message, "NP_Stringarray: replace_item(): NULL "
"replacement item passed as argument." );
mutex = 0;
return 1;
}
if ( item_list[ i ] != NULL )
{
if ( !duplicates_allowed )
{
int j;
char **pointer = item_list;
for( j = 0; j < total; ++j )
{
if ( i == j )
{
++pointer;
continue;
}
if ( !strcmp( *pointer++, replacement ))
break;
}
if ( j != total )
{
snprintf( error_message, sizeof error_message,
"NP_Stringarray: replace_item(): replacing %s with %s"
" would create a duplicate entry.", item_list[ i ],
replacement );
mutex = 0;
return 2;
}
}
free( item_list[ i ] );
if (( item_list[ i ] = strdup( replacement )) == NULL )
{
perror( "strdup" );
exit( 1 );
}
}
else
{
snprintf( error_message, sizeof error_message,
"NP_Stringarray: replace_item(): item %d is NULL. "
"This should never happen!", i );
mutex = 0;
return 1;
}
mutex = 0;
return 0;
}
int NP_Stringarray::replace_item( const char *old, const char *replacement )
{
if ( mutex )
return 3;
mutex = 1;
if ( old == NULL )
{
strcpy( error_message, "NP_Stringarray: replace_item(): NULL old item"
" name passed as argument." );
mutex = 0;
return 1;
}
if ( replacement == NULL )
{
strcpy( error_message, "NP_Stringarray: replace_item() NULL "
"replacement item passed as argument." );
mutex = 0;
return 1;
}
char **pointer = item_list;
int i;
for( i = 0; i < total; ++i )
{
if ( *pointer == NULL )
{
strcpy( error_message, "NP_Stringarray: replace_item(): "
"there is a NULL item in this object."
"This should never happen!" );
mutex = 0;
return 1;
}
if ( !strcmp( old, *pointer ))
break;
++pointer;
}
if ( i == total )
{
snprintf( error_message, sizeof error_message,
"NP_Stringarray: replace_item(): "
"no such item has been added to this object: %s.",
replacement );
mutex = 0;
return 1;
}
if ( !duplicates_allowed )
{
int j;
char **pointer = item_list;
for( j = 0; j < total; ++j )
{
if ( i == j )
{
++pointer;
continue;
}
if ( !strcmp( *pointer++, replacement ))
break;
}
if ( j != total )
{
snprintf( error_message, sizeof error_message,
"NP_Stringarray: replace_item(): replacing %s with %s"
" would create a duplicate entry.", item_list[ i ],
replacement );
mutex = 0;
return 2;
}
}
free( *pointer );
if (( *pointer = strdup( replacement )) == NULL )
{
perror( "strdup" );
exit( 1 );
}
mutex = 0;
return 0;
}
|