File: bitfield.cpp

package info (click to toggle)
libdjconsole 0.1.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,064 kB
  • sloc: sh: 7,088; cpp: 1,130; makefile: 78
file content (136 lines) | stat: -rw-r--r-- 2,465 bytes parent folder | download | duplicates (5)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/****************************************************************************
(c) 2006 Melanie Thielker

This file is part of libdjconsole.

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

libdjconsole 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 General Public License for
more details.

You should have received a copy of the GNU General Public License along with
libdjconsole; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 
****************************************************************************/

#include "bitfield.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

class BitfieldSpec
{
public:
	int byte;
	unsigned char mask;
	int code;
};

Bitfield::Bitfield()
{
	Buffer=0;
	Size=0;
	Dirty=false;
}

Bitfield::Bitfield(int size)
{
	Buffer=new unsigned char[size];
	Size=size;
	memset(Buffer, 0, Size);
}

Bitfield::~Bitfield()
{
	if(Buffer != 0)
		delete[] Buffer;
}

bool Bitfield::load(const char *file)
{
	FILE *fp;

	fp=fopen(file, "r");
	if(!fp)
		return false;
	
	while(Specs.size())
	{
		delete Specs[0];
		Specs.erase(Specs.begin());
	}

	char buf[1024];

	while(fgets(buf, 1024, fp))
	{
		int byte, mask, code;

		if(sscanf(buf, "%d %d %d", &byte, &mask, &code) == 3)
		{
			BitfieldSpec *s=new BitfieldSpec;
			s->byte=byte;
			s->mask=(unsigned char)(mask & 0xff);
			s->code=code;

			Specs.push_back(s);
		}
	}
	fclose(fp);

	return true;
}

void Bitfield::setSize(int size)
{
	if(Size != 0)
		return;
	Buffer=new unsigned char[size];
	Size=size;
	memset(Buffer, 0, Size);
}

int Bitfield::size()
{
	return Size;
}

const unsigned char *Bitfield::bits()
{
	Dirty=false;
	return Buffer;
}

void Bitfield::setBit(int code, bool on)
{
	vector<BitfieldSpec *>::iterator it;

	for(it=Specs.begin();it != Specs.end();++it)
	{
		if((*it)->code == code)
		{
			if((*it)->byte < 0 || (*it)->byte >= Size)
				continue;
			if(!on)
				Buffer[(*it)->byte] &= ~(*it)->mask;
			else
				Buffer[(*it)->byte] |= (*it)->mask;
			Dirty=true;
		}
	}
}

void Bitfield::clearBit(int code)
{
	setBit(code, 0);
}

bool Bitfield::dirty()
{
	return Dirty;
}