"Fossies" - the Fresh Open Source Software Archive 
Member "npadmin-0.14/oidseq.C" (22 Nov 2002, 5421 Bytes) of package /linux/misc/old/npadmin-0.14.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /* Copyright (c) 2000 Ben Woodard
2 * All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public Licence
6 * as published by the Free Software Foundation; either version 2
7 * of the Licence, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRENTY; without even the implied warrenty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public Licence in the COPYING file for more
13 * details.
14 *
15 * You should have received a copy of the GNU Library General
16 * Public License along with the GNU C Library; see the file
17 * COPYING.LIB. If not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include <stdarg.h>
22 #include <string.h>
23 #include <assert.h>
24 #include <stdio.h>
25
26 #include "oidseq.h"
27 #include "structfill.h"
28
29 Assoc::Assoc(const char *oidstr,Assoc *nxt):next(nxt){
30 oid=new BerOid(NOCONSTCAST oidstr,strlen(oidstr));
31 str=strdup(oidstr);
32 assert(str);
33 }
34
35 Assoc::Assoc(const char *oidstr,BerOid *newone,Assoc *nxt):
36 oid(newone),next(nxt){
37 str=strdup(oidstr);
38 assert(str);
39 assert(oid);
40 }
41
42 OidSeq::OidSeq(TableEntry *table):seq(NULL),index(NULL){
43 for(;table;table=table->next)
44 append(table->oidstr);
45 }
46
47 void OidSeq::remove(const char *oidstr){
48 assert(index); /* trying to delete from an empty list */
49 if(!strcmp(oidstr,index->str)){
50 Assoc *nextone=index->next;
51 delete index;
52 index=nextone;
53 } else {
54 char test=0;
55 for(Assoc *cur=index;cur->next;cur=cur->next){
56 if(*cur->next==oidstr){
57 Assoc *doomed=cur->next;
58 cur->next=doomed->next;
59 delete doomed;
60 test=1;
61 break;
62 }
63 }
64 assert(test); /* item being deleted doesn't exist on list */
65 }
66 assert(seq); /* should never happen -- problem with seq and
67 index being out of sync */
68
69 // char buf[10240];
70 // print(buf,10240);
71 // fprintf(stderr,"%s\n",buf);
72
73 BerOid standard(NOCONSTCAST oidstr,strlen(oidstr));
74 BerBase *testcase;
75 unsigned int i=0;
76 while((testcase=seq->peek(i))){
77 testcase=((BerSequence*)testcase)->peek(0); //move into the oid
78 if(*(BerOid*)testcase==standard){
79 seq->extract(i);
80 // memset(buf,0,10240);
81 // print(buf,10240);
82 // fprintf(stderr,"i=%d\n%s\n",i,buf);
83 return;
84 }
85 i++;
86 }
87 assert(0); /* oid wasn't found */
88 }
89
90 void OidSeq::append(const char *oidstr){
91 index=new Assoc(oidstr,index);
92 if(seq==NULL)
93 seq=new BerSequence(SEQUENCE_TAG,1,
94 new BerSequence(SEQUENCE_TAG,2,index->oid,
95 new BerNull()));
96 else
97 seq->append(1,new BerSequence(SEQUENCE_TAG,2,index->oid,new BerNull()));
98 }
99
100 void OidSeq::append(CONST char *oidstr, Tags type, void *data,
101 unsigned int len){
102 index=new Assoc(oidstr,index);
103
104 BerBase *newone;
105 switch(type){
106 case STRING_TAG:
107 newone=new BerString((char*)data,len);
108 break;
109 case OID_TAG:
110 newone=new BerOid((char*)data,len);
111 break;
112 case IPADDR_TAG:
113 newone=new BerIPAddr((char*)data,len);
114 break;
115 default:
116 assert(0); //bad type passed into append
117 }
118
119 if(seq==NULL)
120 seq=new BerSequence(SEQUENCE_TAG,1,
121 new BerSequence(SEQUENCE_TAG,2,index->oid,newone));
122 else
123 seq->append(1,new BerSequence(SEQUENCE_TAG,2,index->oid,newone));
124
125 }
126
127
128 void OidSeq::append(const char *oidstr, Tags type, long data){
129 index=new Assoc(oidstr,index);
130
131 BerBase *newone;
132 assert(type==INT_TAG || type==COUNTER_TAG); /* used the wrong version
133 of append */
134 newone=new BerInt(data);
135
136 // printf("val=%ld\n",((BerInt*)newone)->value());
137
138 if(seq==NULL)
139 seq=new BerSequence(SEQUENCE_TAG,1,
140 new BerSequence(SEQUENCE_TAG,2,index->oid,newone));
141 else
142 seq->append(1,new BerSequence(SEQUENCE_TAG,2,index->oid,newone));
143
144 }
145
146 OidSeq::OidSeq(unsigned int num...):seq(NULL),index(NULL){
147 va_list oidstrs;
148 va_start(oidstrs,num);
149
150 for(;num>0;num--)
151 append(va_arg(oidstrs,char*));
152
153 va_end(oidstrs);
154 }
155
156 OidSeq::~OidSeq(){
157 delete seq;
158 while(index){
159 Assoc *tmp=index->next;
160 delete index;
161 index=tmp;
162 }
163 }
164
165 /* the fact that these are implemented using serial searches
166 should not be a problem due to the fact that the list of oids
167 should be rather small. */
168 BerBase *OidSeq::value(const char *oid){
169 for(Assoc *cur=index;cur;cur=cur->next)
170 if(!strcmp(oid,cur->str))
171 return cur->oid->next;
172 return NULL;
173 }
174
175 BerBase *OidSeq::value(BerOid &oid){
176 for(Assoc *cur=index;cur;cur=cur->next)
177 if(oid==*(cur->oid))
178 return cur->oid->next;
179 return NULL;
180 }
181
182 BerBase *OidSeq::child(const char *oid){
183 for(Assoc *cur=index;cur;cur=cur->next)
184 if(!strncmp(oid,cur->str,strlen(oid)))
185 return cur->oid->next;
186 return NULL;
187 }
188
189 BerOid *OidSeq::key(const char *oid){
190 for(Assoc *cur=index;cur;cur=cur->next)
191 if(!strcmp(oid,cur->str))
192 return cur->oid;
193 return NULL;
194 }
195
196 OidSeq::OidSeq(BerSequence *valseq):seq(valseq),index(NULL){
197 for(BerBase *cur=valseq->peek(0);cur!=NULL;cur=cur->next){
198 char buf[256]; //max oid length
199 assert(cur->type()==SEQUENCE_TAG);
200 BerSequence *curseq=(BerSequence*)cur;
201 int len;
202 assert(curseq->peek(0)->type()==OID_TAG);
203 assert((len=curseq->peek(0)->print(buf,256))!=-1);
204 buf[len]=0;
205 index=new Assoc(buf,(BerOid*)curseq->peek(0),index);
206 }
207 }