1 | /* |
---|
2 | die drei klassen: |
---|
3 | Punch::Card |
---|
4 | Punch::CardColumn |
---|
5 | Punch::Bit |
---|
6 | */ |
---|
7 | |
---|
8 | #include <iostream> |
---|
9 | #include <list> |
---|
10 | |
---|
11 | typedef unsigned char byte_t; |
---|
12 | |
---|
13 | namespace Punch { |
---|
14 | |
---|
15 | class CardBit { |
---|
16 | public: |
---|
17 | // the common meaing of these bits: |
---|
18 | static const bool hole = true; |
---|
19 | static const bool no_hole = false; |
---|
20 | static const bool default_value = false; |
---|
21 | |
---|
22 | // well, this is the real content: :-) |
---|
23 | bool value; |
---|
24 | |
---|
25 | CardBit() : value(default_value) {} |
---|
26 | CardBit(bool value) : value(value) {} |
---|
27 | CardBit(int value) { this->value = value?true:false; } |
---|
28 | |
---|
29 | void set(bool value) { this->value = value; } |
---|
30 | bool get() { return value; } |
---|
31 | |
---|
32 | void invert() { value = !value; } |
---|
33 | |
---|
34 | // wird von bool casting uebernommen |
---|
35 | //CardBit operator!() { return CardBit(!value); } |
---|
36 | CardBit& operator=(const CardBit& a) { |
---|
37 | value=a.value; return *this; } |
---|
38 | operator bool() const { return value; } |
---|
39 | }; |
---|
40 | |
---|
41 | // to make sure: |
---|
42 | static const int hole_1 = 1; |
---|
43 | static const int hole_2 = 2; |
---|
44 | static const int hole_3 = 3; |
---|
45 | static const int hole_4 = 4; |
---|
46 | static const int hole_5 = 5; |
---|
47 | static const int hole_6 = 6; |
---|
48 | static const int hole_7 = 7; |
---|
49 | static const int hole_8 = 8; |
---|
50 | static const int hole_9 = 9; |
---|
51 | static const int hole_0 = 0; |
---|
52 | static const int hole_11 = 10; |
---|
53 | static const int hole_12 = 11; |
---|
54 | |
---|
55 | class CardWord { |
---|
56 | public: |
---|
57 | static const int word_length = 12; |
---|
58 | Punch::CardBit bits[ word_length ]; |
---|
59 | |
---|
60 | // getters and setters |
---|
61 | void set(int pos, const CardBit& value) { bits[pos]=value; } |
---|
62 | void set(const CardBit& bit0, |
---|
63 | const CardBit& bit1, |
---|
64 | const CardBit& bit2, |
---|
65 | const CardBit& bit3, |
---|
66 | const CardBit& bit4, |
---|
67 | const CardBit& bit5, |
---|
68 | const CardBit& bit6, |
---|
69 | const CardBit& bit7, |
---|
70 | const CardBit& bit8, |
---|
71 | const CardBit& bit9, |
---|
72 | const CardBit& bit10, |
---|
73 | const CardBit& bit11) { |
---|
74 | bits[0] = bit0; bits[1] = bit1; bits[2] = bit2; |
---|
75 | bits[3] = bit3; bits[4] = bit4; bits[5] = bit5; |
---|
76 | bits[6] = bit6; bits[7] = bit7; bits[8] = bit8; |
---|
77 | bits[9] = bit9; bits[10] = bit10; bits[11] = bit11; |
---|
78 | } |
---|
79 | CardBit& get(int pos) { |
---|
80 | if(pos<0||pos>word_length) throw "Out of range"; |
---|
81 | return bits[pos]; } |
---|
82 | |
---|
83 | void print(std::ostream& os) const; |
---|
84 | |
---|
85 | CardBit& operator[](int x) { return get(x); } |
---|
86 | |
---|
87 | const CardWord operator&(const CardWord& right) const { |
---|
88 | CardWord result = *this; |
---|
89 | for(int x=0; x<word_length; x++) |
---|
90 | result[x].value &= right.bits[x].value; |
---|
91 | return result; |
---|
92 | } |
---|
93 | |
---|
94 | const CardWord operator|(const CardWord& right) const { |
---|
95 | CardWord result = *this; |
---|
96 | for(int x=0; x<word_length; x++) |
---|
97 | result[x].value |= right.bits[x].value; |
---|
98 | return result; |
---|
99 | } |
---|
100 | |
---|
101 | const CardWord operator^(const CardWord& right) const { |
---|
102 | CardWord result = *this; |
---|
103 | for(int x=0; x<word_length; x++) |
---|
104 | result[x].value ^= right.bits[x].value; |
---|
105 | return result; |
---|
106 | } |
---|
107 | |
---|
108 | const CardWord operator<<(const int& right) const { |
---|
109 | CardWord result; |
---|
110 | // source, target fields |
---|
111 | for(int s=0,t=-right; s<word_length; s++,t++) { |
---|
112 | // make it cyclic |
---|
113 | if(t < 0) t += word_length; |
---|
114 | if(t >= word_length) t -= word_length; |
---|
115 | result[t] = bits[s]; |
---|
116 | } |
---|
117 | return result; |
---|
118 | } |
---|
119 | |
---|
120 | const CardWord operator>>(const int& right) const { |
---|
121 | return operator<<(-right); |
---|
122 | } |
---|
123 | |
---|
124 | }; // class CardWord |
---|
125 | /* |
---|
126 | class CardWordIterator { |
---|
127 | int current_index; |
---|
128 | CardWord* card; |
---|
129 | |
---|
130 | CardWordIterator next(); |
---|
131 | CardWordIterator previous(); |
---|
132 | } |
---|
133 | */ |
---|
134 | |
---|
135 | class Card { |
---|
136 | public: |
---|
137 | static const int col_length = 80; |
---|
138 | Punch::CardWord word[ col_length ]; |
---|
139 | |
---|
140 | // initialize blanco card |
---|
141 | Card() {} |
---|
142 | // read in card from some stream |
---|
143 | Card(CardFormat format, std::istream& is) { |
---|
144 | this->read(format, is); } |
---|
145 | |
---|
146 | // this will copy your word |
---|
147 | void set(int col, const CardWord& word) { |
---|
148 | this->word[col] = word; } |
---|
149 | |
---|
150 | // iteratoren |
---|
151 | //CardWordIterator start(); |
---|
152 | //CardWordIterator end(); |
---|
153 | |
---|
154 | void print(std::ostream& os) const; |
---|
155 | }; |
---|
156 | |
---|
157 | typedef enum { |
---|
158 | JONES_FORMAT, |
---|
159 | ASCII_FORMAT |
---|
160 | } CardFormat; |
---|
161 | |
---|
162 | class CardFile { |
---|
163 | public: |
---|
164 | public list<Punch::Card> cards; |
---|
165 | void write(CardFormat format, std::ostream& os); |
---|
166 | bool read(CardFormat format, std::istream& is); |
---|
167 | } |
---|
168 | |
---|
169 | |
---|
170 | } // namespace |
---|