File: nobjvec.cpp

package info (click to toggle)
crystalspace 0.94-20020412-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 62,276 kB
  • ctags: 52,843
  • sloc: cpp: 274,783; ansic: 6,608; perl: 6,276; objc: 3,952; asm: 2,942; python: 2,354; php: 542; pascal: 530; sh: 430; makefile: 370; awk: 193
file content (116 lines) | stat: -rw-r--r-- 3,297 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
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
/*
    Crystal Space: Named Object Vector class
    Copyright (C) 1998,1999 by Andrew Zabolotny <bit@eltech.ru>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "cssysdef.h"
#include "csutil/nobjvec.h"
#include "csutil/csobject.h"

csObject *csNamedObjVector::FindByName (const char* name) const
{
  csObject* o;
  int i;
  for (i = Length () - 1; i >= 0; i--)
  {
    o = (csObject *)Get (i);
    const char* oname = o->GetName ();
    if (name == oname || (name && oname && !strcmp (oname, name)))
      return o;
  }
  return NULL;
}

int csNamedObjVector::Compare (csSome Item1, csSome Item2, int Mode) const
{
  (void)Mode;
  return (((csObject *)Item1)->GetName () == ((csObject *)Item2)->GetName ()) ? 0
       : strcmp (((csObject *)Item1)->GetName (), ((csObject *)Item2)->GetName ());
}

int csNamedObjVector::CompareKey (csSome Item, csConstSome Key, int Mode) const
{
  (void)Mode;
  return (((csObject *)Item)->GetName () == (char *)Key) ? 0
       : strcmp (((csObject *)Item)->GetName (), (char *)Key);
}

// ---------------------------------------------------------------------------

// @@@ is there a better way than doing QUERY_INTERFACE all the time?

int csNamedObjectVector::GetIndexByName (const char *name) const
{
  int i;
  for (i = 0; i < Length (); i++)
  {
    iObject *o = Get (i);
    if (!o) continue;
    const char* oname = o->GetName ();
    if (name == oname || (name && oname && !strcmp (oname, name)))
      return i;
  }
  return -1;
}

iObject *csNamedObjectVector::FindByName (const char* name) const
{
  int n = GetIndexByName (name);
  return (n == -1) ? NULL : Get (n);
}

int csNamedObjectVector::Find (iObject *obj) const
{
  int i;
  for (i = 0; i < Length (); i++)
  {
    iObject *o = Get (i);
    if (obj == o)
      return i;
  }
  return -1;
}

int csNamedObjectVector::Compare (csSome Item1, csSome Item2, int)
{
  iObject *obj1 = SCF_QUERY_INTERFACE_FAST (((iBase*)Item1), iObject);
  iObject *obj2 = SCF_QUERY_INTERFACE_FAST (((iBase*)Item2), iObject);

  int res = (obj1->GetName () == obj2->GetName ()) ? 0
       : strcmp (obj1->GetName (), obj2->GetName ());
  obj1->DecRef ();
  obj2->DecRef ();
  return res;
}

int csNamedObjectVector::CompareKey (csSome Item, csConstSome Key, int)
{
  iObject *obj = SCF_QUERY_INTERFACE_FAST (((iBase*)Item), iObject);
  const char *name = (const char *)Key;

  int res = (obj->GetName () == name) ? 0
       : strcmp (obj->GetName (), name);
  obj->DecRef ();
  return res;
}

bool csNamedObjectVector::Delete (iObject *obj)
{
  int n = Find (obj);
  if (n == -1) return false;
  else return Delete (n);
}