~ubuntu-branches/ubuntu/trusty/grfcodec/trusty

« back to all changes in this revision

Viewing changes to src/escapes.h

  • Committer: Bazaar Package Importer
  • Author(s): Matthijs Kooijman
  • Date: 2010-08-23 14:45:52 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100823144552-4qggmf9izixsw8li
Tags: 1.0.0+debian1-1
* [30caa6a] Repackaged upstream source, to remove a duplicate file (which
  the lenny version of tar --keep-old-files doesn't like.
* [331d12b] Update watch file to upstream's new versioning scheme.
* [28b6b51] Mangle the +debian suffix in the watch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Escapes.h
 
2
// The data required for writing the various not-quoted escape sequences.
 
3
//
 
4
// This file is shared between GRFCodec and NFORenum.
 
5
// NFORenum defines NFORENUM. GRFCodec does not.
 
6
 
 
7
 
 
8
#ifdef NFORENUM
 
9
 
 
10
#define START_ESCAPES()\
 
11
        const struct esc{\
 
12
                char byte;\
 
13
                char*str;\
 
14
                char action;\
 
15
                uint pos;\
 
16
        }escapes[]={
 
17
 
 
18
#define ESCAPE(byte,str,action,off)\
 
19
        {(char)0x##byte,(char*)("\\" str),(char)0x##action,off},
 
20
 
 
21
#define ESCAPE_79(byte,str,off)\
 
22
        ESCAPE(byte,str,7,off)
 
23
 
 
24
#define ESCAPE_AD(byte,str,action,off,adtl)\
 
25
        {(char)0x##byte,(char*)("\\" str),(char)0x##action,off},
 
26
 
 
27
#define ESCAPE_OVR(byte,str,action,ovr)\
 
28
        {(char)0x##byte,(char*)("\\" str),(char)0x##action,0},
 
29
 
 
30
#else   //!NFORENUM
 
31
 
 
32
#define START_ESCAPES()\
 
33
        const struct esc{\
 
34
                char byte;\
 
35
                char*str;\
 
36
                char action;\
 
37
                uint pos;\
 
38
                bool (*additional)(const U8*,uint);\
 
39
                bool (*override)(const U8*,uint);\
 
40
        }escapes[]={
 
41
 
 
42
#define ESCAPE(byte,str,action,off)\
 
43
        {(char)0x##byte,(char*)("\\" str),(char)0x##action,off,NULL,NULL},
 
44
 
 
45
#define ESCAPE_79(byte,str,off)\
 
46
        ESCAPE(byte,str,7,off) ESCAPE(byte,str,9,off)
 
47
 
 
48
#define ESCAPE_AD(byte,str,action,off,adtl)\
 
49
        {(char)0x##byte,(char*)("\\" str),(char)0x##action,off,__ESC_AD__##adtl,NULL},
 
50
 
 
51
#define ESCAPE_OVR(byte,str,action,ovr)\
 
52
        {(char)0x##byte,(char*)("\\" str),(char)0x##action,0,NULL,__ESC_OVR__##ovr},
 
53
 
 
54
#define CALLBACK_AD(name)\
 
55
        bool __ESC_AD__##name(const U8*data,uint len)
 
56
#define CALLBACK_OVR(name)\
 
57
        bool __ESC_OVR__##name(const U8*data,uint pos)
 
58
 
 
59
#endif  //NFORENUM
 
60
 
 
61
#define END_ESCAPES() };\
 
62
        static const unsigned int num_esc=sizeof(escapes)/sizeof(escapes[0]);
 
63
 
 
64
#ifndef NFORENUM
 
65
 
 
66
// ***********************************************************************
 
67
// Define callback functions for ESCAPE_* here
 
68
// Use the same value for the argument to the CALLBACK_* macro and the last
 
69
// argument to ESCAPE_*
 
70
// The signature for CALLBACK_AD is:
 
71
// bool func(const U8*data, uint len)
 
72
// The signature for CALLBACK_OVR is:
 
73
// bool func(const U8*data, uint pos)
 
74
// ***********************************************************************
 
75
 
 
76
CALLBACK_AD(IsGRM){
 
77
        return len==9&&data[2]==0&&data[4]==0xFE&&data[5]==0xFF;
 
78
}
 
79
 
 
80
CALLBACK_OVR(Is2Op){
 
81
        if(pos<7||!(data[3]&0x80)||data[3]==0x80||data[3]==0x83)return false;
 
82
        uint w = 1<<((data[3]>>2)&3);
 
83
        uint loc=4;//Start at first <var>
 
84
        while(true){//read <var> [<param>] <varadjust> [<op> ...]. loc reports byte to be checked.
 
85
                if((data[loc++/*<var>*/]&0xE0)==0x60)loc++;//<param>
 
86
                if(loc>=pos)return false;
 
87
                if(!(data[loc]&0x20))return false;//end of advanced
 
88
                if(data[loc++/*<shift>*/]&0xC0)
 
89
                        loc+=2*w;//<add> and <div>/<mod>
 
90
                loc+=w;//<and>
 
91
                if(loc==pos)return true;//This is an operation byte
 
92
                if(loc++/*<op>*/>pos)return false;//past proposed op byte
 
93
        }
 
94
}
 
95
 
 
96
#endif
 
97
 
 
98
START_ESCAPES()
 
99
 
 
100
// ***********************************************************************
 
101
// Define escape sequences here.
 
102
// The arguments to ESCAPE* are:
 
103
//   byte:   the byte value for this escape sequence, in hex.
 
104
//   str:    the string for this escape sequence, excluding the \.
 
105
//   action: the action for this escape sequence, eg 2, 7, D, &c.
 
106
//           Note that 7 and 9 are usually paired, with ESCAPE_79, which does
 
107
//           not take an action argument.
 
108
//   off:    the offset in the action where this escape sequence appears
 
109
// ESCAPE_AD takes an additional argument; the CALLBACK to call if the
 
110
// byte, action, and off checks all pass.
 
111
// EXCAPE_OVR takes an argument in place of off, the CALLBACK to call
 
112
// instead of running the off check.
 
113
// ***********************************************************************
 
114
 
 
115
ESCAPE_OVR(00,"2+",2,Is2Op)
 
116
ESCAPE_OVR(01,"2-",2,Is2Op)
 
117
ESCAPE_OVR(02,"2<",2,Is2Op)
 
118
ESCAPE_OVR(03,"2>",2,Is2Op)
 
119
ESCAPE_OVR(04,"2u<",2,Is2Op)
 
120
ESCAPE_OVR(05,"2u>",2,Is2Op)
 
121
ESCAPE_OVR(06,"2/",2,Is2Op)
 
122
ESCAPE_OVR(07,"2%",2,Is2Op)
 
123
ESCAPE_OVR(08,"2u/",2,Is2Op)
 
124
ESCAPE_OVR(09,"2u%",2,Is2Op)
 
125
ESCAPE_OVR(0A,"2*",2,Is2Op)
 
126
ESCAPE_OVR(0B,"2&",2,Is2Op)
 
127
ESCAPE_OVR(0C,"2|",2,Is2Op)
 
128
ESCAPE_OVR(0D,"2^",2,Is2Op)
 
129
ESCAPE_OVR(0E,"2sto",2,Is2Op)
 
130
ESCAPE_OVR(0E,"2s",2,Is2Op)
 
131
ESCAPE_OVR(0F,"2rst",2,Is2Op)
 
132
ESCAPE_OVR(0F,"2r",2,Is2Op)
 
133
ESCAPE_OVR(10,"2psto",2,Is2Op)
 
134
ESCAPE_OVR(11,"2ror",2,Is2Op)
 
135
ESCAPE_OVR(11,"2rot",2,Is2Op)
 
136
ESCAPE_OVR(12,"2cmp",2,Is2Op)
 
137
ESCAPE_OVR(13,"2ucmp",2,Is2Op)
 
138
ESCAPE_OVR(14,"2<<",2,Is2Op)
 
139
ESCAPE_OVR(15,"2u>>",2,Is2Op)
 
140
ESCAPE_OVR(16,"2>>",2,Is2Op)
 
141
 
 
142
ESCAPE_79(00,"71",3)
 
143
ESCAPE_79(01,"70",3)
 
144
ESCAPE_79(02,"7=",3)
 
145
ESCAPE_79(03,"7!",3)
 
146
ESCAPE_79(04,"7<",3)
 
147
ESCAPE_79(05,"7>",3)
 
148
ESCAPE_79(06,"7G",3)
 
149
ESCAPE_79(07,"7g",3)
 
150
ESCAPE_79(08,"7gG",3)
 
151
ESCAPE_79(09,"7GG",3)
 
152
ESCAPE_79(0A,"7gg",3)
 
153
ESCAPE_79(0B,"7c",3)
 
154
ESCAPE_79(0C,"7C",3)
 
155
 
 
156
ESCAPE(00,"D=",D,2)
 
157
ESCAPE(01,"D+",D,2)
 
158
ESCAPE(02,"D-",D,2)
 
159
ESCAPE(03,"Du*",D,2)
 
160
ESCAPE(04,"D*",D,2)
 
161
ESCAPE(05,"Du<<",D,2)
 
162
ESCAPE(06,"D<<",D,2)
 
163
ESCAPE(07,"D&",D,2)
 
164
ESCAPE(08,"D|",D,2)
 
165
ESCAPE(09,"Du/",D,2)
 
166
ESCAPE(0A,"D/",D,2)
 
167
ESCAPE(0B,"Du%",D,2)
 
168
ESCAPE(0C,"D%",D,2)
 
169
 
 
170
ESCAPE_AD(00,"DR",D,3,IsGRM)
 
171
ESCAPE_AD(01,"DF",D,3,IsGRM)
 
172
ESCAPE_AD(02,"DC",D,3,IsGRM)
 
173
ESCAPE_AD(03,"DM",D,3,IsGRM)
 
174
ESCAPE_AD(04,"DnF",D,3,IsGRM)
 
175
ESCAPE_AD(05,"DnC",D,3,IsGRM)
 
176
ESCAPE_AD(06,"DO",D,3,IsGRM)
 
177
 
 
178
END_ESCAPES()