~ubuntu-branches/ubuntu/intrepid/tcm/intrepid

« back to all changes in this revision

Viewing changes to src/sd/bv/eventset.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2003-07-03 20:08:21 UTC
  • Revision ID: james.westby@ubuntu.com-20030703200821-se4xtqx25e5miczi
Tags: upstream-2.20
ImportĀ upstreamĀ versionĀ 2.20

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "eventset.h"
 
2
#include "string.h"
 
3
#include <stdio.h>
 
4
#include <stdlib.h>
 
5
#include <ctype.h>
 
6
 
 
7
List<string> EventSet::EventNames;
 
8
 
 
9
EventSet::EventSet() {
 
10
        memset(EventFlag, '\0', sizeof(EventFlag));
 
11
}
 
12
 
 
13
 
 
14
unsigned EventSet::add(const string &event) {
 
15
        string e(event);
 
16
        while ( isspace(e[0]) )
 
17
                e.remove(0);
 
18
        while ( isspace(e[e.length() - 1]) )
 
19
                e.remove();
 
20
        int i = EventNames.find(e);
 
21
        if ( i < 0 ) {
 
22
                i = EventNames.count();
 
23
                if ( i >= MAX_EVENTS ) {
 
24
                        printf("Error: Maximum number of events reached.\n");
 
25
                        exit(1);
 
26
                }
 
27
                EventNames.add(e);
 
28
        }
 
29
        EventFlag[i >> 3] |= 0200 >> (i & 07);
 
30
        return i;
 
31
}
 
32
 
 
33
 
 
34
void EventSet::sub(string event) {
 
35
        while ( isspace(event[0]) )
 
36
                event.remove(0);
 
37
        while ( isspace(event[event.length() - 1]) )
 
38
                event.remove();
 
39
        int i = EventNames.find(event);
 
40
        if ( i >= 0 )
 
41
                EventFlag[i >> 3] &= ~(0200 >> (i & 07));
 
42
}
 
43
 
 
44
 
 
45
void EventSet::clear() {
 
46
        for ( int i = (MAX_EVENTS + 7) / 8 ; --i >= 0 ; )
 
47
                EventFlag[i] = 0;
 
48
}
 
49
 
 
50
 
 
51
bool EventSet::HasEvent(string event) const {
 
52
        while ( isspace(event[0]) )
 
53
                event.remove(0);
 
54
        while ( isspace(event[event.length() - 1]) )
 
55
                event.remove();
 
56
        int i = EventNames.find(event);
 
57
        return i >= 0 && 0 != (EventFlag[i >> 3] & 0200 >> (i & 07));
 
58
}
 
59
 
 
60
 
 
61
unsigned EventSet::count() const {
 
62
        unsigned result = 0;
 
63
        for ( int i = (MAX_EVENTS + 7) / 8 ; --i >= 0 ; ) {
 
64
                register int flag = EventFlag[i];
 
65
                if ( flag & 0200 ) ++result;
 
66
                if ( flag & 0100 ) ++result;
 
67
                if ( flag &  040 ) ++result;
 
68
                if ( flag &  020 ) ++result;
 
69
                if ( flag &  010 ) ++result;
 
70
                if ( flag &   04 ) ++result;
 
71
                if ( flag &   02 ) ++result;
 
72
                result += flag & 01;
 
73
        }
 
74
        return result;
 
75
}
 
76
 
 
77
 
 
78
bool EventSet::empty() const {
 
79
        for ( int i = (MAX_EVENTS + 7) / 8 ; --i >= 0 ; )
 
80
                if ( EventFlag[i] )
 
81
                        return False;
 
82
        return True;
 
83
}
 
84
 
 
85
 
 
86
const string &EventSet::operator[](int index) const {
 
87
        if ( index >= 0 )
 
88
                for ( int i = 0 ; i < MAX_EVENTS ; i++ )
 
89
                        if ( EventFlag[i >> 3] & 0200 >> (i & 07)
 
90
                             && --index < 0 )
 
91
                                return EventNames[i];
 
92
        return string::EMPTY;
 
93
}
 
94
 
 
95
bool EventSet::first() {
 
96
        current = 0;
 
97
        while ( 0 == (EventFlag[current >> 3] & 0200 >> (current & 07)) )
 
98
                if ( ++current >= MAX_EVENTS ) {
 
99
                        current = -1;
 
100
                        return False;
 
101
                }
 
102
        return True;
 
103
}
 
104
 
 
105
bool EventSet::last() {
 
106
        current = MAX_EVENTS;
 
107
        return prev();
 
108
}
 
109
 
 
110
bool EventSet::next() {
 
111
        if ( current >= 0 )
 
112
                do
 
113
                        if ( ++current >= MAX_EVENTS ) {
 
114
                                current = -1;
 
115
                                return False;
 
116
                        }
 
117
                while ( 0 == (EventFlag[current >> 3] & 0200 >> (current & 07)) );
 
118
        return True;
 
119
}
 
120
 
 
121
bool EventSet::prev() {
 
122
        if ( current >= 0 )
 
123
                do
 
124
                        if ( --current < 0 )
 
125
                                return False;
 
126
                while ( 0 == (EventFlag[current >> 3] & 0200 >> (current & 07)) );
 
127
        return True;
 
128
}
 
129
 
 
130
void EventSet::print() {
 
131
        for ( int i = EventNames.count() ; i != 0 ; ) {
 
132
                --i;
 
133
                printf("%c%s ",
 
134
                        EventFlag[i >> 3] & 0200 >> (i & 07) ? '+' : '-',
 
135
                        EventNames[i].getstr());
 
136
        }
 
137
}
 
138
 
 
139
 
 
140
string EventSet::GetEvents(const char *prefix /* = NULL */) const {
 
141
        /* calculates a string composed of all the events, without guards */
 
142
        string result;
 
143
        for ( int i = EventNames.count() ; i != 0 ; ) {
 
144
                --i;
 
145
                const string *str = &EventNames[i];
 
146
                if ( EventFlag[i >> 3] & 0200 >> (i & 07) &&
 
147
                     ('[' != (*str)[0] || ']' != (*str)[str->length() - 1]) ) {
 
148
                        if ( result.length() )
 
149
                                result += ' ';
 
150
                        if ( prefix )
 
151
                                result += prefix;
 
152
                        result += *str;
 
153
                }
 
154
        }
 
155
        return result;
 
156
}
 
157
 
 
158
 
 
159
string EventSet::GetGuards() const {
 
160
        /* calculates a string composed of all the guards */
 
161
        string result;
 
162
        for ( int i = EventNames.count() ; i != 0 ; ) {
 
163
                --i;
 
164
                const string *str = &EventNames[i];
 
165
                if ( '[' == (*str)[0] && ']' == (*str)[str->length() - 1] &&
 
166
                    EventFlag[i >> 3] & 0200 >> (i & 07) ) {
 
167
                        if ( result.length() )
 
168
                                result += " and ";
 
169
                        result += '(';
 
170
                        result.add(str->getstr() + 1, str->length() - 2);
 
171
                        result += ')';
 
172
                }
 
173
        }
 
174
        return result;
 
175
}