Skip to content
Newer
Older
100644 126 lines (101 sloc) 2.76 KB
4f63888 Add Attrib class for holding NetObj attributes.
steve authored Dec 4, 2000
1 /*
2 * Copyright (c) 2000 Stephen Williams (steve@icarus.com)
3 *
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 */
19 #if !defined(WINNT)
20 #ident "$Id: Attrib.cc,v 1.1 2000/12/04 17:37:03 steve Exp $"
21 #endif
22
23 # include "Attrib.h"
24 # include <assert.h>
25
26 Attrib::Attrib()
27 {
28 nlist_ = 0;
29 list_ = 0;
30 }
31
32 Attrib::~Attrib()
33 {
34 delete[] list_;
35 }
36
37 void Attrib::set_attributes(const map<string,string>&attr)
38 {
39 assert(list_ == 0);
40
41 nlist_ = attr.size();
42 list_ = new cell_[nlist_];
43
44 map<string,string>::const_iterator idx;
45 unsigned jdx;
46 for (idx = attr.begin(), jdx = 0 ; idx != attr.end() ; idx ++, jdx++) {
47 struct cell_*tmp = list_ + jdx;
48 tmp->key = (*idx).first;
49 tmp->val = (*idx).second;
50 }
51 }
52
53 string Attrib::attribute(const string&key) const
54 {
55 for (unsigned idx = 0 ; idx < nlist_ ; idx += 1) {
56
57 if (key == list_[idx].key)
58 return list_[idx].val;
59 }
60
61 return "";
62 }
63
64 void Attrib::attribute(const string&key, const string&value)
65 {
66 unsigned idx;
67
68 for (idx = 0 ; idx < nlist_ ; idx += 1) {
69 if (key == list_[idx].key) {
70 list_[idx].val = value;
71 return;
72 }
73 }
74
75 struct cell_*tmp = new struct cell_[nlist_+1];
76 for (idx = 0 ; idx < nlist_ ; idx += 1)
77 tmp[idx] = list_[idx];
78
79 tmp[nlist_].key = key;
80 tmp[nlist_].val = value;
81
82 nlist_ += 1;
83 delete[]list_;
84 list_ = tmp;
85 }
86
87 bool Attrib::has_compat_attributes(const Attrib&that) const
88 {
89 unsigned idx;
90
91 for (idx = 0 ; idx < that.nlist_ ; idx += 1) {
92
93 string tmp = attribute(that.list_[idx].key);
94 if (tmp != that.list_[idx].val)
95 return false;
96 }
97
98 return true;
99 }
100
101 unsigned Attrib::size() const
102 {
103 return nlist_;
104 }
105
106 string Attrib::key(unsigned idx) const
107 {
108 assert(idx < nlist_);
109 return list_[idx].key;
110 }
111
112 string Attrib::value(unsigned idx) const
113 {
114 assert(idx < nlist_);
115 return list_[idx].val;
116 }
117
118
119 /*
120 * $Log: Attrib.cc,v $
121 * Revision 1.1 2000/12/04 17:37:03 steve
122 * Add Attrib class for holding NetObj attributes.
123 *
124 */
125
Something went wrong with that request. Please try again.