File: add_item.C

package info (click to toggle)
peruser 4b33-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,952 kB
  • ctags: 1,064
  • sloc: cpp: 22,367; perl: 2,733; makefile: 345; sh: 335
file content (59 lines) | stat: -rw-r--r-- 1,216 bytes parent folder | download | duplicates (2)
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;
}