Debian Codegraph @

common.h

raw symbols SCIP
1 /*
2 Qxw is a program to help construct and publish crosswords.
3
4 Copyright 2011-2020 Mark Owen; Windows port by Peter Flippant
5 http://www.quinapalus.com
6 E-mail: [email protected]
7
8 This file is part of Qxw.
9
10 Qxw is free software: you can redistribute it and/or modify
11 it under the terms of version 2 of the GNU General Public License
12 as published by the Free Software Foundation.
13
14 Qxw is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Qxw. If not, see <http://www.gnu.org/licenses/> or
21 write to the Free Software Foundation, Inc., 51 Franklin Street,
22 Fifth Floor, Boston, MA 02110-1301, USA.
23 */
24
25
26 #ifndef __COMMON_H__
27 #define __COMMON_H__
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <assert.h>
34 #include <ctype.h>
35 #include <wctype.h>
36 #include <time.h>
37 #include <math.h>
38
39 // OS-SPECIFIC
40
41 #ifdef _WIN32
42 #include <direct.h>
43 #include <shlobj.h>
44 #include <windows.h>
45 #define _USE_MATH_DEFINES
46 #include <math.h>
47 #define DIR_SEP_STR "\\"
48 #define DIR_SEP_CHAR '\\'
49 #define STRCASECMP _stricmp
50 #else
51 #define DIR_SEP_STR "/"
52 #define DIR_SEP_CHAR '/'
53 #define STRCASECMP strcasecmp
54 #endif
55
56 #define RELEASE "20200708"
57
58 // GENERAL
59
60 #define NGTYPE 13 // number of different grid types
61 #define MAXNDIR 3 // maximum number of directions
62 #define MXSZ 63 // max grid size X and Y
63 #define MXCL 250 // max number of cells in light; must be >=MXSZ*2; allow more for VL:s
64 #define MXCT 31 // max contents in one cell per direction
65 #define MXMUX MXCT // maximum degree of multiplexing
66 #define NMSG 2 // number of messages passed to treatment code
67 #define MXLE 250 // max entries in a light before tags added
68 #define MXFL 255 // max entries in a word that filler can cope with: light plus tags, >=MXLE+NMSG; <256 to allow poscnt[] not to overflow
69 #define MXLT (MAXNDIR*MXSZ*MXSZ+NVL) // max number of lights (conservative estimate)
70 #define NVL (MXSZ*4) // max number of virtual lights
71 #define MAXNMK (MAXNDIR*2) // max number of corner marks
72 #define MXMK 30 // max length of mark string
73
74 // note that a light (in a Mobius grid with all squares full) can contain up to MXSZ*2*MXCT entries, which is >MXLE; and that
75 // if filler discretion is enabled, the number of treated lights must be <=MXFL, otherwise only limited by MXLT
76 // MXFL<=255 to prevent unsigned char histograms overflowing
77
78 #define NLEM 5 // number of light entry methods
79 #define NATREAT 12 // number of treatments
80 #define TREAT_PLUGIN 9
81
82 #define UNDOS 50
83
84 // MISC
85
86 #define STRSTARTS(s,t) (!strncmp((s),(t),strlen(t)))
87
88 #define MEMBLK 100000 // unit of memory allocation
89
90 struct memblk {
91 struct memblk*next;
92 int ct;
93 char s[MEMBLK];
94 };
95
96 #define PI (M_PI)
97 #define ODD(x) ((x)&1)
98 #define EVEN(x) (!((x)&1))
99 #define FREEX(p) if(p) {free(p);p=0;}
100 #define SLEN 1000 // maximum length for filenames, strings etc.; Windows has MAX_PATH around 260; should be >>MXFL
101 #define LEMDESCLEN 20 // maximum length of entry method description
102
103 #define MX(a,b) ((a)>(b)?(a):(b))
104
105 extern int debug;
106 #define DEB_F0 if(debug&0x00000001) // filler: start and stop
107 #define DEB_F1 if(debug&0x00000002) // filler: basics
108 #define DEB_F2 if(debug&0x00000004) // more detail
109 #define DEB_F3 if(debug&0x00000008) // spreads and jumbles
110 #define DEB_GU if(debug&0x00000010) // general GUI
111 #define DEB_RF if(debug&0x00000020) // refresh, events
112 #define DEB_DI if(debug&0x00000040) // dictionaries
113 #define DEB_DV if(debug&0x00000080) // dictionaries, verbose
114 #define DEB_AL if(debug&0x00000100) // alphabets
115 #define DEB_FL if(debug&0x00000200) // feasible list building
116 #define DEB_TR if(debug&0x00000400) // treatments and plug-ins
117 #define DEB_GR if(debug&0x00000800) // grid operations, save, load
118 #define DEB_BS if(debug&0x00001000) // buildstructs
119 #define DEB_DE if(debug&0x00002000) // decks
120 #define DEB_EX if(debug&0x00004000) // export
121 // bit 31 is used to force printing of version information etc. without other output
122
123 // ALPHABETS
124
125 typedef unsigned int uchar; // to hold UTF-32 character; must be unsigned
126 #define MAXUCHAR 0x30000 // maximum Unicode character accepted: first three planes only
127 #define MAXEQUIV 100 // maximum equivalent unicode charaacters mapping to given internal code
128 #define MAXICC 61 // internal character codes from 1..MAXICC-1 are allowed for normal characters; 0 reserved for 0-termination; code MAXICC used for "dash" in treatments (to mean "no treatment")
129 #define ICC_DASH MAXICC // internal character code for dash
130
131 #define TOUUPPER(c) g_unichar_toupper(c)
132 #define TOULOWER(c) g_unichar_tolower(c)
133 #define ISUUPPER(c) g_unichar_isupper(c)
134 #define ISULOWER(c) g_unichar_islower(c)
135 #define ISUDIGIT(c) g_unichar_isdigit(c)
136 #define ISUALPHA(c) g_unichar_isalpha(c)
137 #define ISUPRINT(c) g_unichar_isprint(c)
138 #define ISUGRAPH(c) g_unichar_isgraph(c)
139
140 struct alphaentry {
141 char*rep; // representative as UTF-8
142 char*eqv; // UTF-8 string of equivalents (need not include rep)
143 int vow; // is it a vowel?
144 int con; // is it a consonant?
145 int seq; // forms sequence with next?
146 };
147
148 #define ICCTOABM(x) (1ULL<<((x)-1))
149 typedef unsigned long long int ABM; // alphabet bitmap type
150 #define ABM_NRM ((1ULL<<(MAXICC-1))-1) // normal characters
151 #define ABM_DASH (1ULL<<(ICC_DASH-1))
152 #define ABM_ALL (ABM_NRM|ABM_DASH) // normal characters plus dash
153
154 // Terminology:
155 // A `square' is the quantum of area in the grid; one or more squares (a `merge group') form
156 // an `entry', which is a single enclosed white area in the grid where a letter (or group of letters) will appear.
157 // A `word' is a sequence of entries to be filled.
158 // bldstructs() constructs the words and entries from the square data produced
159 // by the editor.
160
161 struct sprop { // square properties
162 unsigned int bgcol; // background colour
163 unsigned int fgcol; // foreground colour
164 unsigned int mkcol; // mark colour
165 unsigned char ten; // treatment enable: flag to plug-in
166 unsigned char spor; // global square property override (not used in dsp)
167 unsigned char fstyle; // font style: b0=bold, b1=italic
168 unsigned char dech; // dechecked: 0=normal, 1=contributions stacked atop one another, 2=contributions side-by-side
169 char mk[MAXNMK][MXMK+1]; // square corner mark strings in each direction
170 };
171
172 // entry methods
173 #define EM_FWD 1
174 #define EM_REV 2
175 #define EM_CYC 4
176 #define EM_RCY 8
177 #define EM_JUM 16
178 #define EM_ALL 31
179 #define EM_SPR 32 // for internal use only
180
181 struct lprop { // light properties
182 unsigned int dmask; // mask of allowed dictionaries; special values 1<<MAXNDICTS and above for "implicit" words
183 unsigned int emask; // mask of allowed entry methods
184 unsigned char ten; // treatment enable
185 unsigned char lpor; // global light property override (not used in dlp)
186 unsigned char dnran; // does not receive a number
187 unsigned char mux; // is multiplexed
188 };
189
190 struct jdata { // per-feasible-dictionary-entry information relating to words with jumbled entry mode
191 int nuf; // total number of unforced letters
192 unsigned char ufhist[MAXICC+1]; // histogram of unforced letters
193 unsigned char poscnt[MAXICC+1]; // number of positions where each unforced letter can go
194 };
195
196 struct sdata { // per-feasible-dictionary-entry information relating to words with spread entry mode
197 ABM flbm[MXFL]; // feasible letter bitmaps // could economise on storage here as done for jdata, but spreading only used with one-word dictionaries for now
198 double ct[MXFL][MXFL]; // [i][j] is the number of ways char i in string goes through entry slot j in word // must economise on storage if normal dictionaries are ever used
199 double ctd[MXFL]; // [j] is number of ways '-' goes through entry slot j in word
200 };
201
202 struct word {
203 int nent; // number of entries in word, <=MXFL
204 int wlen; // length of strings in feasible list (<=nent in spread entry case)
205 int jlen; // number of entries subject to jumble (<nent if tagged)
206 int gx0,gy0; // start position (not necessarily mergerep)
207 int ldir;
208 int*flist; // start of feasible list
209 int flistlen; // length of feasible list
210 struct jdata*jdata;
211 ABM*jflbm;
212 struct sdata*sdata;
213 int commitdep; // depth at which this word committed during fill, or -1 if not committed
214 struct entry*e[MXFL]; // list of nent entries making up this word
215 struct lprop*lp; // applicable properties for this word
216 unsigned char upd; // updated flag
217 int fe; // fully-entered flag
218 int goi[MXLE]; // grid order indices for each entry
219 };
220 struct entry {
221 ABM flbm; // feasible letter bitmap
222 ABM flbmh; // copy of flbm provided by solver to running display
223 int gx,gy; // corresponding grid position (indices to gsq) of representative
224 int checking; // count of intersecting words
225 double score[MAXICC+1]; // includes dash character
226 double crux; // priority
227 unsigned char sel; // selected flag
228 unsigned char upd; // updated flag
229 };
230 struct square {
231 // source grid information (saved in undo buffers)
232 unsigned int bars; // bar presence in each direction
233 unsigned int merge; // merge flags in each direction
234 unsigned char fl; // flags: b0=blocked, b3=not part of grid (for irregular shapes); b4=cell selected
235 unsigned char dsel; // one light-selected flag for each direction
236 int ctlen[MAXNDIR]; // length of contents in each direction
237 ABM ctbm[MAXNDIR][MXCT]; // square contents bitmap strings in each direction
238 struct sprop sp;
239 struct lprop lp[MAXNDIR];
240 // derived information from here on
241 struct entry*ents[MAXNDIR][MXCT]; // entries corresponding 1:1 to ctbm:s
242 struct word*w[MAXNDIR][MXMUX];
243 unsigned int vflags[MAXNDIR]; // violation flags: b0=double unch, b1=triple+ unch, b2=underchecked, b3=overchecked
244 int number; // number in square (-1 for no number)
245 int goi; // grid order index of treated squares (-1 in untreated squares)
246 };
247 struct vl { // virtual light
248 int l; // number of constituent entries, <=MXCL
249 int x[MXCL],y[MXCL]; // constituent entry coords
250 struct lprop lp; // properties
251 unsigned char sel; // is selected?
252 struct word*w;
253 };
254
255 extern struct word*words;
256 extern struct entry*entries;
257 extern struct entry*treatmsge0[NMSG][MXFL]; // to help passing info from filler to constraints
258 extern struct square gsq[MXSZ][MXSZ];
259 extern struct vl vls[NVL];
260 extern int nc,nw,nw0,ntw,ne,ne0,ns,nvl;
261
262 extern int cwperm[MAXICC]; // "codeword" permutation
263
264 // DICTIONARY
265
266 struct answer { // a word found in one or more dictionaries
267 int ahlink; // hash table link for isword()
268 unsigned int dmask; // mask of dictionaries where word found
269 unsigned int cfdmask; // mask of dictionaries where word found with this citation form
270 // int light[NLEM]; // light indices of treated versions
271 double score;
272 char*cf; // citation form of the word in dstrings
273 struct answer*acf; // alternative citation form (linked list)
274 char*ul; // untreated light in dstrings: ansp is uniquified by this; in internal character code
275 char banned; // 0:not banned; 1:banned
276 };
277
278 struct light { // a string that can appear in the grid, the result of treating an answer; not uniquified
279 int hashslink; // hash table (s only) linked list
280 int hashaeslink; // hash table (a,e,s) linked list
281 int ans; // answer giving rise to this light; negative to represent message words
282 int em; // mode of entry giving rise to this light
283 char*s; // the light in dstrings, containing only chars in alphabet
284 int uniq; // uniquifying number (by string s only), used as index into lused[]
285 int tagged; // does s include NMSG tag characters?
286 ABM lbm; // bitmap of letters used
287 unsigned char hist[MAXICC+1]; // letter histogram, indexed by internal character code
288 };
289
290 // FILLER
291
292 extern int*llist; // buffer for word index list
293 extern int*llistp; // ptr to matching word indices
294 extern int llistn; // number of matching words
295 extern int llistwlen; // word length applicable to matching lights
296 extern unsigned int llistdm; // dictionary mask applicable to matching lights
297 extern int llistem; // entry method mask applicable to matching lights
298
299 extern int ifamode; // interactive fill assist mode: 0=none; 1=current light only; 2=full grid
300 extern volatile int abort_flag; // abort word list building?
301
302 extern struct answer**ansp;
303 extern struct light*lts;
304
305 extern int atotal; // total answers in dict
306 extern int ultotal; // total unique lights in dict
307
308 extern char tpifname[SLEN];
309 extern int treatmode; // 0=none, TREAT_PLUGIN=custom plug-in
310 extern int treatorder[NMSG]; // 0=first come first served, 1=spread, 2=jumble
311 extern char treatmsg[NMSG][MXLT*8+1];
312 extern ABM treatcstr[NMSG][MXFL];
313 extern int tambaw; // treated answer must be a word
314
315 extern uchar treatmsgU [NMSG][MXLT+1]; // converted to uchars
316 extern char treatmsgICC [NMSG][MXLT+1]; // converted to ICCs
317 extern char treatmsgICCAZ [NMSG][MXLT+1]; // ICCs, alphabetic only
318 extern char treatmsgICCAZ09[NMSG][MXLT+1]; // ... plus digits
319 extern uchar treatmsgUAZ [NMSG][MXLT+1]; // converted to uchars, alphabetic, mapped to entries
320 extern uchar treatmsgUAZ09 [NMSG][MXLT+1]; // ... plus digits
321
322 extern char treatmsgAZ [NMSG][MXLT+1]; // 7-bit clean *no transliteration*
323 extern char treatmsgAZ09 [NMSG][MXLT+1];
324
325 extern char msgword[NMSG][MXFL+1]; // for "one-word dictionary" created for tagging; in internal character code
326
327 extern int clueorderindex;
328 extern int gridorderindex[MXLE];
329 extern int checking[MXLE];
330 extern int lightx;
331 extern int lighty;
332 extern int lightdir;
333
334 // PREFERENCES
335
336 #define NLOOKUP 6
337 #define NPREFS (14+NLOOKUP*2)
338 extern int prefdata[NPREFS];
339 extern char prefstring[NPREFS][SLEN+1];
340 #define clickblock (prefdata[0])
341 #define clickbar (prefdata[1])
342 #define shownums (prefdata[2])
343 #define mincheck (prefdata[3])
344 #define maxcheck (prefdata[4])
345 #define afunique (prefdata[5])
346 #define afrandom (prefdata[6])
347 #define eptsq (prefdata[7])
348 #define hpxsq (prefdata[8])
349 #define lnis (prefdata[9])
350 #define lookupname(i) (prefstring[10+(i)])
351 #define lookup(i) (prefstring[10+NLOOKUP+(i)])
352 #define startup_dict1 (prefstring[10+NLOOKUP*2])
353 #define startup_ff1 (prefstring[10+NLOOKUP*2+1])
354 #define startup_af1 (prefstring[10+NLOOKUP*2+2])
355 #define startup_al (prefdata[10+NLOOKUP*2+3])
356
357 #endif