File: operator%5B%5D.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 (67 lines) | stat: -rw-r--r-- 1,342 bytes parent folder | download
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
#include <stdlib.h>
#include <stdio.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;
}