Corrected a long-standing error in which ending text with a literal
[xcircuit.git] / spiceparser / bitlist.c
blobd4e660a8754ec2a061528507fdb76a05bad1d929
1 /********************
2 This file is part of the software library CADLIB written by Conrad Ziesler
3 Copyright 2003, Conrad Ziesler, all rights reserved.
5 *************************
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 ******************/
21 /* bitlist.c, routines for bit arrays
23 Conrad Ziesler
27 #include "debug.h"
28 #include "bitlist.h"
33 bitlist_t * bitlist_new(int qty)
35 bitlist_t *bl;
36 int qbytes;
37 qbytes=(qty>>3)+1;
39 bl= malloc(sizeof(bitlist_t)+qbytes);
40 assert(bl!=NULL);
41 memset(bl,0,sizeof(bitlist_t)+qbytes);
42 bl->qty=qty;
43 bl->qbytes=qbytes;
45 return bl;
48 void bitlist_free(bitlist_t *bl)
50 if(bl!=NULL)
51 free(bl);
54 void bitlist_setall(bitlist_t *bl)
56 memset(bl->data,0xff,bl->qbytes);
59 void bitlist_clearall(bitlist_t *bl)
61 memset(bl->data,0,bl->qbytes);
65 int bitlist_set(bitlist_t *bl, int index)
67 int byte,bit,r=0;
68 if(index<0) return 0;
69 byte=index>>3;
70 bit=index-(byte<<3);
71 assert(byte<bl->qbytes);
72 if((bl->data[byte]&(1<<bit))!=0)r=1;
73 bl->data[byte]|=(1<<bit);
74 return r;
77 int bitlist_test(bitlist_t *bl, int index)
79 int byte,bit;
80 if(index<0) return 0;
81 byte=index>>3;
82 bit=index-(byte<<3);
83 assert(byte<bl->qbytes);
84 if((bl->data[byte]&(1<<bit))!=0)return 1;
85 return 0;
89 int bitlist_clear(bitlist_t *bl, int index)
91 int byte,bit,r=0;
92 if(index<0) return 0;
93 byte=index>>3;
94 bit=index-(byte<<3);
95 assert(byte<bl->qbytes);
96 if((bl->data[byte]&(1<<bit))!=0)r=1;
97 bl->data[byte]&=~(1<<bit);
98 return 0;
101 /* realloc bitlist */
102 bitlist_t *bitlist_resize(bitlist_t *bl, int qty)
104 int oldqty;
105 int qbytes;
106 qbytes=(qty>>3)+1;
107 oldqty=bl->qty;
109 bl=realloc(bl,sizeof(bitlist_t)+qbytes);
110 assert(bl!=NULL);
111 if( (oldqty>>3) < (qbytes-(oldqty>>3)) )
112 memset(bl->data+(oldqty>>3),0,qbytes-(oldqty>>3));
114 bl->qty=qty;
115 bl->qbytes=qbytes;
116 return bl;
120 /* returns the index of the first bit set or cleared
121 from dir
124 int bitlist_scan(bitlist_t *bl, int flags)
126 int i,j;
127 assert(bl!=NULL);
128 switch(flags)
130 case (SCAN_FORWARD|SCAN_SET):
131 for(i=0;i<(bl->qbytes-1);i++)
133 if(bl->data[i]!=0)
135 for(j=(i<<3);j<bl->qty;j++)
136 if(bitlist_test(bl,j))return j;
139 break;
140 case (SCAN_FORWARD|SCAN_CLEAR):
141 for(i=0;i<(bl->qbytes-1);i++)
143 if( (bl->data[i])< ((unsigned char)0x00ff))
146 for(j=(i<<3);j<bl->qty;j++)
147 if(!bitlist_test(bl,j))return j;
150 break;
152 default:
153 assert(0);
155 return -1;
160 int bitlist_copyhead(bitlist_t *dest, bitlist_t *src, int length)
162 int i;
163 int finalbyte;
164 int finalbitoffset;
165 unsigned char a,b;
166 if(length>src->qty)length=src->qty;
167 if(length>dest->qty)length=dest->qty;
169 finalbyte=length>>3;
170 finalbitoffset=length-(finalbyte<<3);
172 for(i=0;i<finalbyte;i++)
173 dest->data[i]=src->data[i];
175 a=dest->data[finalbyte];
176 b=src->data[finalbyte];
178 for(i=0;i<finalbitoffset;i++)
179 a= (a & (~(1<<i)) ) | ((1<<i)&b) ;
181 dest->data[finalbyte]=a;
182 return length;