common.h

changeset 41
47e686c96d8f
parent 37
c349dca807f9
child 42
5cd91fd1526c
equal deleted inserted replaced
40:9e4f785501db 41:47e686c96d8f
116 return (c <= 32 || c == 127 || c == 255); 116 return (c <= 32 || c == 127 || c == 255);
117 } 117 }
118 118
119 // Byte datatype 119 // Byte datatype
120 typedef unsigned long int word; 120 typedef unsigned long int word;
121 typedef unsigned char byte;
121 122
122 // Keywords 123 // Keywords
123 #define NUM_KEYWORDS 20 124 #define NUM_KEYWORDS 20
124 #ifndef __MAIN_CXX__ 125 #ifndef __MAIN_CXX__
125 extern const char** g_Keywords; 126 extern const char** g_Keywords;
126 #endif 127 #endif
127 bool IsKeyword (str s); 128 bool IsKeyword (str s);
128 129
129 // Script mark -- also serves as reference type 130 // Script mark and reference
130 struct ScriptMark { 131 struct ScriptMark {
131 int type; 132 int type;
132 str name; 133 str name;
133 size_t pos; 134 size_t pos;
134 }; 135 };
137 unsigned int num; 138 unsigned int num;
138 size_t pos; 139 size_t pos;
139 }; 140 };
140 141
141 // ==================================================================== 142 // ====================================================================
143 // Generic union
144 template <class T> union union_t {
145 T val;
146 byte b[sizeof (T)];
147 char c[sizeof (T)];
148 double d;
149 float f;
150 int i;
151 word w;
152 };
153
154 // ====================================================================
142 // Finds a byte in the given value. 155 // Finds a byte in the given value.
143 template <class T> unsigned char GetByteIndex (T a, unsigned int b) { 156 template <class T> inline unsigned char GetByteIndex (T a, unsigned int b) {
144 if (b >= sizeof (T)) 157 union_t<T> uni;
145 error ("CharByte: tried to get byte %u out of a %u-byte %s\n", 158 uni.val = a;
146 b, sizeof (T), typeid (T).name()); 159 return uni.b[b];
147
148 unsigned long p1 = pow<unsigned long> (256, b);
149 unsigned long p2 = pow<unsigned long> (256, b+1);
150 unsigned long r = (a % p2) / p1;
151
152 if (r > 256)
153 error ("DataBuffer::CharByte: result %lu too big!", r);
154
155 unsigned char ur = static_cast<unsigned char> (r);
156 return ur;
157 } 160 }
158 161
159 #endif // __COMMON_H__ 162 #endif // __COMMON_H__

mercurial