Debian Codegraph @

qxw.c

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 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <time.h>
30 #include <math.h>
31 #ifdef _WIN32
32 #include <Windows.h>
33 #include <stringapiset.h>
34 #include <io.h>
35 #include "pfgetopt.h"
36 #else
37 #include <unistd.h>
38 #include <pwd.h>
39 #endif
40 #include <wchar.h>
41 #include <gtk/gtk.h>
42 #include <gdk/gdkkeysyms.h>
43 #include <cairo.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 #include <glib.h>
47 #include <glib/gstdio.h>
48
49 #include "common.h"
50 #include "qxw.h"
51 #include "filler.h"
52 #include "dicts.h"
53 #include "treatment.h"
54 #include "gui.h"
55 #include "draw.h"
56 #include "deck.h"
57 #include "alphabets.h"
58
59 // GLOBAL PARAMETERS
60
61 // these are saved:
62
63 int width=12,height=12; // grid size
64 int gtype=0; // grid type: 0=square, 1=hexH, 2=hexV, 3=circleA (edge at 12 o'clock), 4=circleB (cell at 12 o'clock)
65 // 5=cylinder L-R, 6=cylinder T-B, 7=Moebius L-R, 8=Moebius T-B, 9=torus
66 int symmr=2,symmm=0,symmd=0; // symmetry mode flags (rotation, mirror, up-and-down/left-and-right)
67 int nvl=0; // number of virtual lights
68 struct sprop dsp; // default square properties
69 struct lprop dlp; // default light properties
70
71 char gtitle[SLEN]="";
72 char gauthor[SLEN]="";
73
74 // these are not saved:
75 int debug=0; // debug level
76 int usegui=1; // 0=use text console; 1=use GUI
77 unsigned int fseed; // force filler srand() argument if non-zero
78
79 char filename[SLEN+50]; // result from filedia()
80 char filenamebase[SLEN]; // base to use for constructing default filenames
81 int havesavefn; // do we have a validated filename to save to?
82
83 int curx=0,cury=0,curdir=0,curmux=0; // cursor position, direction and multiplex
84 int curent=-1; // entry pointed to by cursor (set by bldstructs(); -1 if not set)
85 int curword=-1; // word pointed to by cursor (set by bldstructs(); -1 if not set)
86 int unsaved=0; // edited-since-save flag
87 int cwperm[MAXICC];
88
89 int ndir[NGTYPE] ={2,3,3,2,2, 2,2,2,2,2,2,2,2}; // number of directions per grid type
90 int gshape[NGTYPE]={0,1,2,3,4, 0,0,0,0,0,0,0,0}; // basic shape of grid
91 int dhflip[NGTYPE][MAXNDIR*2]={
92 {2,1,0,3},
93 {4,3,2,1,0,5},
94 {3,2,1,0,5,4},
95 {2,1,0,3},
96 {2,1,0,3},
97 {2,1,0,3},
98 {2,1,0,3},
99 {2,1,0,3},
100 {2,1,0,3},
101 {2,1,0,3},
102 {2,1,0,3},
103 {2,1,0,3},
104 {2,1,0,3}
105 };
106 int dvflip[NGTYPE][MAXNDIR*2]={
107 {0,3,2,1},
108 {1,0,5,4,3,2},
109 {0,5,4,3,2,1},
110 {2,1,0,3},
111 {2,1,0,3},
112 {0,3,2,1},
113 {0,3,2,1},
114 {0,3,2,1},
115 {0,3,2,1},
116 {0,3,2,1},
117 {0,3,2,1},
118 {0,3,2,1},
119 {0,3,2,1}
120 };
121
122 static unsigned char log2lut[65536];
123
124 int cbits(ABM x) {ABM i; int j; for(i=1,j=0;i<ABM_ALL;i+=i) if(x&i) j++; return j;} // count set bits
125 int onebit(ABM x) {return x!=0&&(x&(x-1))==0;}
126 int logbase2(ABM x) {int u; // find lowest set bit
127 u= x &0xffff; if(u) return log2lut[u];
128 u=(x>>16)&0xffff; if(u) return log2lut[u]+16;
129 u=(x>>32)&0xffff; if(u) return log2lut[u]+32;
130 u=(x>>48)&0xffff; if(u) return log2lut[u]+48;
131 return -1;
132 }
133
134 int*llistp=0; // ptr to matching lights
135 int llistn=0; // number of matching lights
136 unsigned int llistdm=0; // dictionary mask applicable to matching lights
137 int llistwlen=0; // word length (less tags if any) applicable to matching lights
138 int llistem=0; // entry method mask applicable to matching lights
139 int*llist=0; // buffer for light index list
140
141 int ifamode=2; // interactive fill assist mode
142
143 // GRID
144
145 struct square gsq[MXSZ][MXSZ];
146 struct vl vls[NVL];
147
148 int getnumber(int x,int y) {
149 return gsq[x][y].number;
150 }
151
152 int getflags(int x,int y) {int xr,yr;
153 getmergerep(&xr,&yr,x,y);
154 return gsq[xr][yr].fl;
155 }
156
157 int getbgcol(int x,int y) {int xr,yr;
158 getmergerep(&xr,&yr,x,y);
159 return (gsq[xr][yr].sp.spor?gsq[xr][yr].sp.bgcol:dsp.bgcol)&0xffffff;
160 }
161
162 int getfgcol(int x,int y) {int xr,yr;
163 getmergerep(&xr,&yr,x,y);
164 return (gsq[xr][yr].sp.spor?gsq[xr][yr].sp.fgcol:dsp.fgcol)&0xffffff;
165 }
166
167 int getmkcol(int x,int y) {int xr,yr;
168 getmergerep(&xr,&yr,x,y);
169 return (gsq[xr][yr].sp.spor?gsq[xr][yr].sp.mkcol:dsp.mkcol)&0xffffff;
170 }
171
172 int getfstyle(int x,int y) {int xr,yr;
173 getmergerep(&xr,&yr,x,y);
174 return gsq[xr][yr].sp.spor?gsq[xr][yr].sp.fstyle:dsp.fstyle;
175 }
176
177 int getdech(int x,int y) {int xr,yr;
178 getmergerep(&xr,&yr,x,y);
179 return gsq[xr][yr].sp.spor?gsq[xr][yr].sp.dech:dsp.dech;
180 }
181
182 int getctlen(int x,int y,int d) {int xr,yr;
183 getmergerep(&xr,&yr,x,y);
184 if((gsq[xr][yr].sp.spor?gsq[xr][yr].sp.dech:dsp.dech)==0) d=0;
185 return gsq[xr][yr].ctlen[d];
186 }
187
188 struct lprop*getvlprop(int d) { return vls[d].lp.lpor?&vls[d].lp:&dlp; }
189 struct lprop*getlprop(int x,int y,int d) {
190 if(d>=100) return getvlprop(d-100);
191 return gsq[x][y].lp[d].lpor?&gsq[x][y].lp[d]:&dlp;
192 }
193
194 // returns 0 for error
195 int getmaxmux(int x,int y,int d) {
196 int tx[MXCL],ty[MXCL],i,l,m,u;
197 if(!ismux(x,y,d)) return 1;
198 m=1;
199 l=getlight(tx,ty,x,y,d);
200 if(l<1) return 0;
201 for(i=0;i<l;i++) {
202 u=getctlen(tx[i],ty[i],d);
203 if(u>m) m=u;
204 }
205 return m;
206 }
207
208 void getmk(char*s,int x,int y,int c) {int d,nd,nd2,xr,yr; char*t;
209 strcpy(s,"");
210 getmergerep(&xr,&yr,x,y);
211 t=gsq[xr][yr].sp.spor?gsq[xr][yr].sp.mk[c]:dsp.mk[c];
212 if(!strcmp(t,"\\#")) goto ew0; // if number, put it wherever necessary
213 if(!strcmp(t,"\\o")) { // if circle, only do in first cell of group
214 if(x!=xr||y!=yr) return;
215 goto ew0;
216 }
217 d=getmergedir(x,y); // otherwise only in extreme corners of merge group
218 nd=ndir[gtype];
219 nd2=nd+nd;
220 if(d>=0) {
221 if(!ismerge(x,y,d )&&(c-1-d+nd2)%nd2< nd) goto ew0; // end of group d=0:1,2/d=1:2,3
222 if(!ismerge(x,y,d+nd)&&(c-1-d+nd2)%nd2>=nd) goto ew0;
223 else return;
224 }
225 ew0:
226 strcpy(s,t);
227 }
228
229 // move backwards one cell in direction d
230 void stepback(int*x,int*y,int d) {
231 // printf("stepback(%d,%d,%d)\n",*x,*y,d);
232 if(d>=ndir[gtype]) {stepforw(x,y,d-ndir[gtype]); return;}
233 if(gtype==1) {
234 switch(d) {
235 case 0: if(((*x)&1)==1) (*y)++; (*x)--; break;
236 case 1: if(((*x)&1)==0) (*y)--; (*x)--; break;
237 case 2: (*y)--;break;
238 }
239 return;
240 }
241 if(gtype==2) {
242 switch(d) {
243 case 2: if(((*y)&1)==1) (*x)++; (*y)--; break;
244 case 1: if(((*y)&1)==0) (*x)--; (*y)--; break;
245 case 0: (*x)--;break;
246 }
247 return;
248 }
249 // rectangular grid cases
250 if(d) (*y)--; else (*x)--;
251 if(gtype==3||gtype==4||gtype==5||gtype==7||gtype==9||gtype==10||gtype==11||gtype==12) { // loop in x direction
252 if(*x<0) {
253 *x+=width;
254 if(gtype==7||gtype==10||gtype==12) *y=height-1-*y;
255 }
256 }
257 if(gtype==6||gtype==8||gtype==9||gtype==10||gtype==11||gtype==12) { // loop in y direction
258 if(*y<0) {
259 *y+=height;
260 if(gtype==8||gtype==11||gtype==12) *x=width-1-*x;
261 }
262 }
263 // printf("=(%d,%d)\n",*x,*y);
264 }
265
266 // move forwards one cell in direction d
267 void stepforw(int*x,int*y,int d) {
268 // printf("stepforw(%d,%d,%d)\n",*x,*y,d);
269 if(d>=ndir[gtype]) {stepback(x,y,d-ndir[gtype]); return;}
270 if(gtype==1) {
271 switch(d) {
272 case 0: (*x)++; if(((*x)&1)==1) (*y)--; break;
273 case 1: (*x)++; if(((*x)&1)==0) (*y)++; break;
274 case 2: (*y)++;break;
275 }
276 return;
277 }
278 if(gtype==2) {
279 switch(d) {
280 case 2: (*y)++; if(((*y)&1)==1) (*x)--; break;
281 case 1: (*y)++; if(((*y)&1)==0) (*x)++; break;
282 case 0: (*x)++;break;
283 }
284 return;
285 }
286 // rectangular grid cases
287 if(d) (*y)++; else (*x)++;
288 if(gtype==3||gtype==4||gtype==5||gtype==7||gtype==9||gtype==10||gtype==11||gtype==12) { // loop in x direction
289 if(*x>=width) {
290 *x-=width;
291 if(gtype==7||gtype==10||gtype==12) *y=height-1-*y;
292 }
293 }
294 if(gtype==6||gtype==8||gtype==9||gtype==10||gtype==11||gtype==12) { // loop in y direction
295 if(*y>=height) {
296 *y-=height;
297 if(gtype==8||gtype==11||gtype==12) *x=width-1-*x;
298 }
299 }
300 // printf("=(%d,%d)\n",*x,*y);
301 }
302
303 int isingrid(int x,int y) {
304 if(x<0||y<0||x>=width||y>=height) return 0;
305 if(gshape[gtype]==1&&ODD(width )&&ODD(x)&&y==height-1) return 0;
306 if(gshape[gtype]==2&&ODD(height)&&ODD(y)&&x==width -1) return 0;
307 return 1;
308 }
309
310 int isclear(int x,int y) {
311 if(!isingrid(x,y)) return 0;
312 if(gsq[x][y].fl&0x09) return 0;
313 return 1;
314 }
315
316 int isbar(int x,int y,int d) {int u;
317 if(d>=ndir[gtype]) {d-=ndir[gtype];stepback(&x,&y,d);}
318 if(!isingrid(x,y)) return 0;
319 u=(gsq[x][y].bars>>d)&1;
320 // DEB_GR printf(" x=%d y=%d d=%d u=%d g[x,y].bars=%08x\n",x,y,d,u,gsq[x][y].bars);
321 stepforw(&x,&y,d);
322 if(!isingrid(x,y)) return 0;
323 return u;
324 }
325
326 int ismerge(int x,int y,int d) {int u;
327 if(d>=ndir[gtype]) {d-=ndir[gtype];stepback(&x,&y,d);}
328 if(!isingrid(x,y)) return 0;
329 u=(gsq[x][y].merge>>d)&1;
330 stepforw(&x,&y,d);
331 if(!isingrid(x,y)) return 0;
332 return u;
333 }
334
335 int sqexists(int i,int j) { // is a square within the grid (and not cut out)?
336 if(!isingrid(i,j)) return 0;
337 return !(gsq[i][j].fl&8);
338 }
339
340 // is a step backwards clear? (not obstructed by a bar, block, cutout or edge of grid)
341 int clearbefore(int x,int y,int d) {int tx,ty;
342 tx=x;ty=y;stepback(&tx,&ty,d);
343 if(!isingrid(tx,ty)) return 0;
344 if(!isclear(tx,ty)) return 0;
345 if(isbar(tx,ty,d)) return 0;
346 return 1;
347 }
348
349 // is a step forwards clear?
350 int clearafter(int x,int y,int d) {int tx,ty;
351 tx=x;ty=y;stepforw(&tx,&ty,d);
352 if(!isingrid(tx,ty)) return 0;
353 if(!isclear(tx,ty)) return 0;
354 if(isbar(x,y,d)) return 0;
355 return 1;
356 }
357
358 // move forwards one (merged) cell in direction d
359 static void stepforwm(int*x,int*y,int d) {int x0,y0;
360 x0=*x;y0=*y;
361 while(ismerge(*x,*y,d)) {stepforw(x,y,d);if(*x==x0&&*y==y0) return;} // can loop e.g. in circular grids
362 stepforw(x,y,d);
363 }
364
365 // move backwards one (merged) cell in direction d
366 static void stepbackm(int*x,int*y,int d) {int x0,y0,od;
367 x0=*x;y0=*y;
368 od=(d+ndir[gtype])%(ndir[gtype]*2); // opposite direction
369 stepback(x,y,d); if(*x==x0&&*y==y0) return;
370 while(ismerge(*x,*y,od)) {stepback(x,y,d);if(*x==x0&&*y==y0) return;}
371 }
372
373 // is a step forwards clear?
374 static int clearafterm(int x,int y,int d) {int tx,ty;
375 tx=x;ty=y;stepforwm(&tx,&ty,d);
376 if(x==tx&&y==ty) return 0;
377 if(!isingrid(tx,ty)) return 0;
378 if(!isclear(tx,ty)) return 0;
379 stepback(&tx,&ty,d);
380 if(isbar(tx,ty,d)) return 0;
381 return 1;
382 }
383
384 int stepbackifingrid (int*x,int*y,int d) {int tx,ty; tx=*x;ty=*y; stepback (&tx,&ty,d);if(!isingrid(tx,ty)) return 0; *x=tx;*y=ty; return 1;}
385 //static int stepforwifingrid (int*x,int*y,int d) {int tx,ty; tx=*x;ty=*y; stepforw (&tx,&ty,d);if(!isingrid(tx,ty)) return 0; *x=tx;*y=ty; return 1;} // not currently used
386 int stepforwmifingrid(int*x,int*y,int d) {int tx,ty; tx=*x;ty=*y; stepforwm(&tx,&ty,d);if(!isingrid(tx,ty)) return 0; *x=tx;*y=ty; return 1;}
387 int stepbackmifingrid(int*x,int*y,int d) {int tx,ty; tx=*x;ty=*y; stepbackm(&tx,&ty,d);if(!isingrid(tx,ty)) return 0; *x=tx;*y=ty; return 1;}
388
389
390 void getmergerepd(int*mx,int*my,int x,int y,int d) {int x0,y0; // find merge representative, direction d (0<=d<ndir[gtype]) only
391 assert(0<=d);
392 assert(d<ndir[gtype]);
393 *mx=x;*my=y;
394 if(!isclear(x,y)) return;
395 if(!ismerge(x,y,d+ndir[gtype])) return;
396 x0=x;y0=y;
397 do {
398 stepback(&x,&y,d);
399 if(!isclear(x,y)) goto ew1;
400 if(x+y*MXSZ<*mx+*my*MXSZ) *mx=x,*my=y; // first lexicographically if loop
401 if(x==x0&&y==y0) goto ew1; // loop detected
402 } while(ismerge(x,y,d+ndir[gtype]));
403 *mx=x;*my=y;
404 ew1: ;
405 }
406
407 int getmergedir(int x,int y) {int d; // get merge direction
408 if(!isclear(x,y)) return -1;
409 for(d=0;d<ndir[gtype];d++) if(ismerge(x,y,d)||ismerge(x,y,d+ndir[gtype])) return d;
410 return 0;
411 }
412
413 void getmergerep(int*mx,int*my,int x,int y) {int d; // find merge representative, any direction
414 *mx=x;*my=y;
415 d=getmergedir(x,y); if(d<0) return;
416 getmergerepd(mx,my,x,y,d);
417 }
418
419 int isownmergerep(int x,int y) {int x0,y0;
420 getmergerep(&x0,&y0,x,y);
421 return x==x0&&y==y0;
422 }
423
424 // get coordinates of cells (max MXCL) in merge group with (x,y) in direction d
425 int getmergegroupd(int*gx,int*gy,int x,int y,int d) {int l,x0,y0;
426 if(!isclear(x,y)) {if(gx) *gx=x;if(gy) *gy=y;return 1;}
427 getmergerepd(&x,&y,x,y,d);
428 x0=x;y0=y;l=0;
429 for(;;) {
430 assert(l<MXCL);
431 if(gx) gx[l]=x;
432 if(gy) gy[l]=y;
433 l++;
434 if(!ismerge(x,y,d)) break;
435 stepforw(&x,&y,d);
436 if(!isclear(x,y)) break;
437 if(x==x0&&y==y0) break;
438 }
439 return l;
440 }
441
442 // get coordinates of cells (max MXCL) in merge group with (x,y) in merge direction
443 int getmergegroup(int*gx,int*gy,int x,int y) {int d;
444 if(!isclear(x,y)) {if(gx) *gx=x;if(gy) *gy=y;return 1;}
445 d=getmergedir(x,y); assert(d>=0);
446 return getmergegroupd(gx,gy,x,y,d);
447 }
448
449 int isstartoflight(int x,int y,int d) {
450 if(!isclear(x,y)) return 0;
451 if(clearbefore(x,y,d)) return 0;
452 if(clearafterm(x,y,d)) return 1;
453 return 0;
454 }
455
456 // returns:
457 // -1: looped, no light found
458 // 0: blocked, no light found
459 // 1: light found, start (not mergerep'ed) stored in *lx,*ly
460 static int getstartoflight(int*lx,int*ly,int x,int y,int d) {int x0,y0;
461 if(!isclear(x,y)) return 0;
462 x0=x;y0=y;
463 while(clearbefore(x,y,d)) {
464 stepback(&x,&y,d); // to start of light
465 if(x==x0&&y==y0) return -1; // loop found
466 }
467 *lx=x;*ly=y;
468 return 1;
469 }
470
471 int ismux(int x,int y,int d) {
472 int l,lx,ly;
473 if(d>=100) return 0; // virtual light?
474 l=getstartoflight(&lx,&ly,x,y,d);
475 if(l<1) return 0;
476 if(!isstartoflight(lx,ly,d)) return 0; // not actually a light
477 return getlprop(lx,ly,d)->mux;
478 }
479
480 // is light selected?
481 int issellight(int x,int y,int d) {int l,lx,ly;
482 l=getstartoflight(&lx,&ly,x,y,d);
483 if(l<1) return 0;
484 if(!isstartoflight(lx,ly,d)) return 0; // not actually a light
485 return (gsq[lx][ly].dsel>>d)&1;
486 }
487
488 // set selected flag on a light to k
489 void sellight(int x,int y,int d,int k) {int l,lx,ly;
490 DEB_GR printf("sellight(%d,%d,%d,%d)\n",x,y,d,k);
491 l=getstartoflight(&lx,&ly,x,y,d);
492 if(l<1) return;
493 if(!isstartoflight(lx,ly,d)) return; // not actually a light
494 gsq[lx][ly].dsel=(gsq[lx][ly].dsel&(~(1<<d)))|(k<<d);
495 }
496
497 // extract mergerepd'ed grid squares (max MXCL) forming light running through (x,y) in direction d
498 // returns:
499 // -1: loop detected
500 // 0: (x,y) blocked
501 // 1: no light in this direction (lx[0], ly[0] still set)
502 // n>=2: length of light (lx[0..n-1], ly[0..n-1] set), can be up to MXCL for VL, up to MXSZ*2 in Mobius case
503 // if d>=100 returns data for VL #d-100
504 // result is independent of multiplex property of light
505 int getlightd(int*lx,int*ly,int x,int y,int d) {int i,j;
506 if(d>=100) {
507 d-=100;
508 for(i=0,j=0;i<vls[d].l;i++) if(isclear(vls[d].x[i],vls[d].y[i])) lx[j]=vls[d].x[i],ly[j]=vls[d].y[i],j++; // skip invalid squares
509 return j;
510 }
511 i=getstartoflight(&x,&y,x,y,d);
512 if(i<1) return i;
513 i=0;
514 for(;;) {
515 assert(i<MXSZ*2);
516 lx[i]=x;ly[i]=y;i++;
517 if(!clearafterm(x,y,d)) break;
518 stepforwm(&x,&y,d);
519 }
520 return i;
521 }
522
523 // extract merge-representative grid squares (up to MXCL) forming light running through (x,y) in direction d
524 // if d>=100 returns data for VL #d-100
525 // errors as for getlightd()
526 // result is independent of multiplex property of light
527 int getlight(int*lx,int*ly,int x,int y,int d) {int i,l;
528 l=getlightd(lx,ly,x,y,d);
529 for(i=0;i<l;i++) getmergerep(lx+i,ly+i,lx[i],ly[i]);
530 return l;
531 }
532
533 // extract data for light running through (x,y) in direction d
534 // if d>=100 returns data for VL #d-100
535 // for non-mux light mx=0; for multiplexed light mx=desired multiplex number
536 // returns -2 if light overflows plus errors as for getlightd()
537 // lp: bitmap ptrs (<=MXLE)
538 // lx: mergerep square x (<=MXLE)
539 // ly: mergerep square y (<=MXLE)
540 // ls: index of contributing string (<=MXLE)
541 // lo: offset in contributing string (<=MXLE)
542 // le: entry number (<=MXLE)
543 int getlightdat(ABM**lp,int*lx,int*ly,int*ls,int*lo,struct entry**le,int x0,int y0,int d,int mx) {
544 int c,i,j,k,k0,k1,l,m,tx[MXCL],ty[MXCL],x,y;
545 l=getlight(tx,ty,x0,y0,d);
546 if(l<1) return l;
547
548 if(!ismux(x0,y0,d)) mx=-1;
549 for(i=0,m=0;i<l;i++) { // for each square in the light...
550 x=tx[i]; y=ty[i];
551 // compute contribution mask
552 if(getdech(x,y)==0) c=1; // normal checking
553 else if(d<100) c=1<<d; // de-checked: not VL
554 else c=(1<<ndir[gtype])-1; // all directions contribute to VL:s
555 for(j=0;j<ndir[gtype];j++) {
556 if(c&(1<<j)) { // does this direction contribute to light?
557 if(gsq[x][y].ctlen[j]==0) continue; // empty contribution? skip
558 if(mx<0) {k0=0; k1=gsq[x][y].ctlen[j]; } // non-multiplexed light? use entire contents
559 else {k0=mx%gsq[x][y].ctlen[j]; k1=k0+1; } // multiplexed light: extract single character from contents
560 for(k=k0;k<k1;k++) {
561 if(m==MXLE) return -2;
562 if(lp) lp[m]=gsq[x][y].ctbm[j]+k;
563 if(lx) lx[m]=x;
564 if(ly) ly[m]=y;
565 if(ls) ls[m]=j;
566 if(lo) lo[m]=k;
567 if(le) le[m]=gsq[x][y].ents[j][k];
568 m++;
569 }
570 }
571 }
572 }
573 return m;
574 }
575
576 // extract pointers to bitmaps forming light running through (x,y) in direction d
577 // for non-mux light mx=0; for multiplexed light mx=desired multiplex number
578 // returns errors as getlightdat
579 int getlightbmp(ABM**p,int x,int y,int d,int mx) {return getlightdat(p,0,0,0,0,0,x,y,d,mx);}
580
581 // extract word running through x,y in direction d from grid in UTF-8
582 // s must have space for MXLE*16+1 chars
583 // for non-mux light mx=0; for multiplexed light mx=desired multiplex number
584 // returns length of light or errors as getlightdat
585 int getwordutf8(int x,int y,int d,char*s,int mx) {
586 int i,l,u;
587 ABM*chp[MXLE];
588 l=getlightbmp(chp,x,y,d,mx);
589 s[0]='\0';
590 for(i=0;i<l;i++) {
591 u=abmtoicc(chp[i][0]);
592 if(u==-2) strcat(s,".");
593 else if(u==-1) strcat(s,"?");
594 else strcat(s,icctoutf8[u]);
595 }
596 return l;
597 }
598
599 // if normally-checked, single-character square, get character in mergerep square
600 // return 0..MAXICC-1
601 // -1: bitmap is zero
602 // -2: more than one bit set
603 // else return -3
604 int geteicc(int x,int y) {
605 if(getdech(x,y)) return -3;
606 getmergerep(&x,&y,x,y);
607 if(gsq[x][y].ctlen[0]!=1) return -3;
608 return abmtoicc(gsq[x][y].ctbm[0][0]);
609 }
610
611 // if normally-checked, single-character square or
612 // dechecked but only single character in direction d,
613 // set character in mergrep square and return 0; else return 1
614 // c is either in internal character code or -1 to clear cell
615 int seteicc(int x,int y,int d,int c) {
616 if(!getdech(x,y)) d=0;
617 getmergerep(&x,&y,x,y);
618 if(gsq[x][y].ctlen[d]!=1) return 1;
619 gsq[x][y].ctbm[d][0]=(c==-1)?ABM_NRM:ICCTOABM(c);
620 return 0;
621 }
622
623 // clear contents of cell
624 void clrcont(int x,int y) {int i,j;
625 getmergerep(&x,&y,x,y);
626 for(i=0;i<MAXNDIR;i++) for(j=0;j<gsq[x][y].ctlen[i];j++) gsq[x][y].ctbm[i][j]=ABM_NRM;
627 }
628
629
630 static void setmerge(int x,int y,int d,int k) { // set merge state in direction d to k, make bar state consistent
631 if(d>=ndir[gtype]) {d-=ndir[gtype];stepback(&x,&y,d);}
632 if(!isingrid(x,y)) return;
633 if(k) k=1;
634 gsq[x][y].merge&=~(1<<d);
635 gsq[x][y].merge|= k<<d;
636 gsq[x][y].bars &=~gsq[x][y].merge;
637 }
638
639 static void setbars(int x,int y,int d,int k) { // set bar state in direction d to k, make merge state consistent
640 if(d>=ndir[gtype]) {d-=ndir[gtype];stepback(&x,&y,d);}
641 if(!isingrid(x,y)) return;
642 if(k) k=1;
643 gsq[x][y].bars &=~(1<<d);
644 gsq[x][y].bars |= k<<d;
645 gsq[x][y].merge&=~gsq[x][y].bars;
646 }
647
648 static void demerge(int x,int y) {int i; // demerge from all neighbours
649 for(i=0;i<ndir[gtype]*2;i++) setmerge(x,y,i,0);
650 }
651
652 // calculate mask of feasible symmetries
653 int symmrmask(void) {int i,m;
654 switch(gshape[gtype]) {
655 case 0: return 0x16;break;
656 case 1:
657 if(EVEN(width)) return 0x06;
658 if(EVEN(width/2+height)) return 0x06;
659 else return 0x5e;
660 break;
661 case 2:
662 if(EVEN(height)) return 0x06;
663 if(EVEN(height/2+width)) return 0x06;
664 else return 0x5e;
665 break;
666 case 3: case 4:
667 m=0;
668 for(i=1;i<=12;i++) if(width%i==0) m|=1<<i;
669 return m;
670 break;
671 }
672 return 0x02;
673 }
674
675 int symmmmask(void) {
676 switch(gshape[gtype]) {
677 case 0: case 1: case 2: return 0x0f;break;
678 case 3: case 4:
679 if(width%2) return 0x03;
680 else return 0x0f;
681 break;
682 }
683 return 0x01;
684 }
685
686 int symmdmask(void) {
687 switch(gshape[gtype]) {
688 case 0: case 1: case 2:return 0x0f;break;
689 case 3: case 4:return 0x01;break;
690 }
691 return 0x01;
692 }
693
694 int nw,nw0,ntw,nc,ne,ne0; // count of words, treated words, cells, entries
695 struct word*words=0;
696 struct entry*entries=0;
697 struct entry*treatmsge0[NMSG][MXFL]={{0}};
698
699 struct lprop msglprop[NMSG];
700 char msgword[NMSG][MXFL+1]; // in internal character code
701
702 // statistics calculated en passant by bldstructs()
703 int st_lc[MXLE+1]; // total lights, by length
704 int st_lucc[MXLE+1]; // underchecked lights, by length
705 int st_locc[MXLE+1]; // overchecked lights, by length
706 int st_lsc[MXLE+1]; // total of checked entries in lights, by length
707 int st_lmnc[MXLE+1]; // minimum checked entries in lights, by length
708 int st_lmxc[MXLE+1]; // maximum checked entries in lights, by length
709 int st_hist[MAXICC+2]; // entry letter histogram: [MAXICC]=uncommitted, [MAXICC+1]=partially committed
710 int st_sc; // total checked cells
711 int st_ce; // total checked entries (i.e., squares)
712 int st_2u,st_3u; // count of double+ and triple+ unches
713 int st_tlf,st_vltlf,st_tmtlf; // count of (free) lights too long for filler
714
715
716
717 // calculate numbers for squares and grid-order-indices of treated squares
718 void donumbers(void) {
719 int d,i,i0,j,num=1,goi=0;
720
721 for(j=0;j<height;j++) for(i=0;i<width;i++) gsq[i][j].number=-1,gsq[i][j].goi=-1;
722 for(j=0;j<height;j++) for(i0=0;i0<width;i0++) {
723 if(gshape[gtype]==1) {i=i0*2;if(i>=width) i=(i-width)|1;} // special case for hexH grid
724 else i=i0;
725 if(isclear(i,j)) {
726 if(isownmergerep(i,j)&&(gsq[i][j].sp.spor?gsq[i][j].sp.ten:dsp.ten)) gsq[i][j].goi=goi++;
727 for(d=0;d<ndir[gtype];d++)
728 if(isstartoflight(i,j,d)) {
729 if(!getlprop(i,j,d)->dnran) {gsq[i][j].number=num++; break;}
730 }
731 }
732 }
733 }
734
735 // add word to filler data, starting at (i,j) in direction d
736 // if d>=100, add virtual light #d-100
737 // for non-mux light mx=0; for multiplexed light mx=desired multiplex number
738 // return:
739 // 0: OK
740 // 1: too long
741 // 2: ignored as dmask is zero
742 static int addwordfd(int i,int j,int d,int mx) {
743 int k,l,lx[MXLE],ly[MXLE];
744 struct entry*le[MXLE];
745 ABM*lcp[MXLE];
746 struct lprop*lp;
747
748 lp=getlprop(i,j,d);
749 if(lp->dmask==0) return 2; // skip if dictionary mask empty
750 l=getlightdat(lcp,lx,ly,0,0,le,i,j,d,mx);
751 if(l<1) return 1; // too long: ignore
752 DEB_BS printf("new word %d: l=%d mx=%d\n",nw,l,mx);
753 words[nw].gx0=i;
754 words[nw].gy0=j;
755 if(d<100) {
756 gsq[i][j].w[d][mx]=words+nw;
757 words[nw].ldir=d;
758 }
759 else {
760 vls[d-100].w=words+nw;
761 words[nw].ldir=d;
762 }
763 words[nw].lp=lp;
764 for(k=0;k<l;k++) {
765 assert(le[k]!=0);
766 words[nw].e[k]=le[k];
767 if(le[k]==entries+curent&&d==curdir&&mx==curmux) curword=nw;
768 words[nw].goi[k]=gsq[lx[k]][ly[k]].goi;
769 DEB_BS printf(" d=%d x=%d y=%d k=%d e=%d goi=%d\n",d,lx[k],ly[k],k,(int)(le[k]-entries),words[nw].goi[k]);
770 nc++;
771 }
772 words[nw].nent=k;
773 words[nw].wlen=k;
774 words[nw].jlen=k;
775 DEB_BS printf(" nent=%d\n",k);
776 nw++;
777 if(lp->ten&&treatmode>0) ntw++;
778 return 0;
779 }
780
781 void freewords() {
782 int i;
783 if(words)
784 for(i=0;i<nw;i++) {
785 FREEX(words[i].flist);
786 FREEX(words[i].jdata);
787 FREEX(words[i].jflbm);
788 FREEX(words[i].sdata);
789 words[i].flistlen=0;
790 }
791 FREEX(words);
792 }
793
794 void initstructs() {
795 int c,g,i,j,k0,k1,k2,k3,k4,d,m,u;
796 freewords();
797 FREEX(entries);
798 nw=0; ntw=0; nc=0; ne=0; ne0=0;
799 for(i=0;i<NVL;i++) vls[i].w=0;
800 for(j=0;j<MXSZ;j++) for(i=0;i<MXSZ;i++) memset(gsq[i][j].ents,0,sizeof(gsq[i][j].ents)); // make sure all ents[][] are NULL
801 for(j=0;j<height;j++) for(i=0;i<width;i++) {
802 for(d=0;d<MAXNDIR;d++) {
803 for(m=0;m<MXMUX;m++) gsq[i][j].w[d][m]=0;
804 gsq[i][j].vflags[d]=0;
805 }
806 gsq[i][j].number=-1;
807 }
808 for(i=0;i<NMSG;i++) for(j=0;j<MXFL;j++) treatmsge0[i][j]=0;
809 for(i=0;i<MXLE+1;i++) st_lc[i]=st_lucc[i]=st_locc[i]=st_lsc[i]=0,st_lmxc[i]=-1,st_lmnc[i]=99; // initialise stats
810 for(i=0;i<MAXICC+2;i++) st_hist[i]=0;
811 st_tlf=st_vltlf=st_tmtlf=0;
812
813 for(i=0;i<NMSG;i++) {
814 utf8touchars(treatmsgU[i],treatmsg[i],MXLT+1);
815 for(j=0,k0=k1=k2=k3=k4=0;treatmsgU[i][j];j++) {
816 u=treatmsgU[i][j];
817 c=uchartoICC(u);
818 if(c<1) continue;
819 u=icctouchar[c];
820 g=icctogroup[c];
821 treatmsgICC [i][k0++]=c; // everything, ICC
822 if (g==0) treatmsgICCAZ [i][k1 ]=c, // alphabetic only
823 treatmsgUAZ [i][k1++]=u;
824 if (g==0 &&u<128) treatmsgAZ [i][k2++]=u; // alphabetic only, 7-bit clean
825 if (g==0||g==1) treatmsgICCAZ09[i][k3 ]=c, // alphabetic+digits
826 treatmsgUAZ09 [i][k3++]=u;
827 if((g==0||g==1)&&u<128) treatmsgAZ09 [i][k4++]=u; // alphabetic+digits, 7-bit clean
828 }
829 treatmsgICC [i][k0]=0; // zero-termination
830 treatmsgICCAZ [i][k1]=0;
831 treatmsgUAZ [i][k1]=0;
832 treatmsgAZ [i][k2]=0;
833 treatmsgICCAZ09[i][k3]=0;
834 treatmsgUAZ09 [i][k3]=0;
835 treatmsgAZ09 [i][k4]=0;
836 }
837 }
838
839 void addimplicitwords() {
840 int i,j,k,l;
841 ne0=ne; nw0=nw; // number of entries and words before implicit words added: needed for stats and deck dump
842 k=nw;
843 if(ntw) { // if there are any treated words add in "implicit" words as needed
844 for(i=0;i<NMSG;i++) {
845 for(j=0,l=0;j<k;j++) if(words[j].lp->ten) {
846 if(treatorder[i]>0) entries[ne].flbm=treatcstr[i][l];
847 else entries[ne].flbm=ABM_ALL;
848 entries[ne].sel=1; // make sure we fill even if only in "fill selection" mode
849 entries[ne].checking=2;
850 if(l==MXFL) st_tmtlf=1; // too many treated lights for filler to use discretion
851 if(l<MXFL) {
852 treatmsge0[i][l]=entries+ne;
853 words[nw].e[l++]=entries+ne; // build up new implicit word
854 }
855 words[j].e[words[j].nent]=entries+ne; // add tag to end of word in grid
856 words[j].nent++; // note that we do not increment jlen here
857 ne++;
858 }
859 words[nw].nent=l;
860 words[nw].lp=msglprop+i;
861 memset(msglprop+i,0,sizeof(struct lprop));
862 msglprop[i].dmask=1<<(MAXNDICTS+i); // special dictionary mask flag value
863 strncpy(msgword[i],treatmsgICC[i],l); // truncate message if necessary
864 msgword[i][l]=0;
865 if(treatorder[i]==0) { // first come first served mode
866 msglprop[i].emask=EM_FWD;
867 words[nw].wlen=strlen(msgword[i]);
868 } else if(treatorder[i]==1) { // "spread" mode
869 msglprop[i].emask=EM_SPR;
870 words[nw].wlen=strlen(msgword[i]);
871 } else {
872 msglprop[i].emask=EM_ALL;
873 for(j=strlen(msgword[i]);j<l;j++) msgword[i][j]=ICC_DASH; // pad message if necessary with dashes
874 words[nw].wlen=l;
875 words[nw].jlen=l;
876 }
877 if(l&&treatorder[i]>0) nw++; // don't create an empty word, and don't create one for first come first served entry
878 }
879 }
880 }
881
882 // build structures for solver and compile statistics
883 // 0: OK
884 // 1: too complex
885 // Ensure filler is stopped before calling this (reallocates e.g. words[])
886 static int bldstructs(void) {
887 int cnw,cne,d,i,j,k,l,m,nd,v;
888 int ml,mm,curk,curl;
889 unsigned char unch[MXLE];
890 ABM c;
891
892 curent=-1;
893 curword=-1;
894 initstructs();
895 cnw=0; // count words
896 for(d=0;d<ndir[gtype];d++) for(j=0;j<height;j++) for(i=0;i<width;i++)
897 if(isstartoflight(i,j,d)) cnw+=getmaxmux(i,j,d); // look for normal lights
898 cnw+=nvl; // plus virtual lights
899 cne=0; // count entries: entry count is independent of multiplexing
900 for(j=0;j<height;j++) for(i=0;i<width;i++) if(isclear(i,j)&&isownmergerep(i,j)) {
901 if(getdech(i,j)) nd=ndir[gtype];
902 else nd=1;
903 for(k=0;k<nd;k++) cne+=gsq[i][j].ctlen[k];
904 }
905
906 words=calloc(cnw+NMSG,sizeof(struct word)); // one spare word for each message
907 if(!words) return 1;
908 entries=calloc(cne+NMSG*cnw,sizeof(struct entry)); // cnw spares for each message
909 if(!entries) return 1;
910
911 for(j=0;j<height;j++) for(i=0;i<width;i++) if(isclear(i,j)&&isownmergerep(i,j)) { // loop over all mergerep cells and assign entries to them
912 mm=getmaxmux(i,j,curdir);
913 if(mm<1) mm=1;
914 if(getdech(i,j)) nd=ndir[gtype],curk=curdir;
915 else nd=1, curk=0;
916 if(curk>=100) curk=0; // cursor "points" at first character in case of VL
917 for(k=0;k<nd;k++) {
918 ml=gsq[i][j].ctlen[k];
919 if(ml) curl=curmux%ml;
920 else curl=0;
921 for(l=0;l<ml;l++) { // one entry for each potentially different character in this cell
922 if(i==curx&&j==cury&&k==curk&&l==curl) curent=ne;
923 entries[ne].gx=i;
924 entries[ne].gy=j;
925 entries[ne].sel=!!(gsq[i][j].fl&16); // selected?
926 c=gsq[i][j].ctbm[k][l]&ABM_NRM; // letter in square
927 entries[ne].flbm=c; // bitmap of feasible letters
928 if(c==ABM_NRM) st_hist[MAXICC]++; // uncommitted
929 else if(onebit(c)) st_hist[abmtoicc(c)]++;
930 else st_hist[MAXICC+1]++; // partially committed
931 gsq[i][j].ents[k][l]=entries+ne;
932 ne++;
933 }
934 }
935 }
936 DEB_BS printf("total entries=%d\n",ne);
937 assert(ne==cne);
938
939 for(d=0;d<ndir[gtype];d++) for(j=0;j<height;j++) for(i=0;i<width;i++) if(isstartoflight(i,j,d)) { // look for normal lights
940 m=getmaxmux(i,j,d);
941 for(k=0;k<m;k++) if(addwordfd(i,j,d,k)==1) st_tlf++;
942 }
943 for(i=0;i<nvl;i++) if(addwordfd(0,0,i+100,0)==1) st_vltlf++; // add in virtual lights
944
945 for(i=0;i<ne;i++) entries[i].checking=0; // now calculate checking (and other) statistics
946 for(i=0;i<nw;i++) for(j=0;j<words[i].nent;j++) words[i].e[j]->checking++;
947 st_ce=0;
948 for(i=0;i<ne;i++) if(entries[i].checking>1) st_ce++; // number of checked squares
949 st_sc=0;st_2u=0;st_3u=0;
950 for(i=0;i<nw;i++) {
951 l=words[i].nent;
952 st_lc[l]++;
953 v=0;
954 for(j=0,k=0;j<l;j++) {
955 unch[j]=(words[i].e[j]->checking<=1);
956 if(!unch[j]) k++; // count checked cells in word
957 if(j>0&&unch[j]&&unch[j-1] ) v|=1; // double unch?
958 if(j>1&&unch[j]&&unch[j-1]&&unch[j-2]) v|=2; // triple+ unch?
959 }
960 if(v&1) st_2u++; // double+ unch?
961 if(v&2) st_3u++; // triple+ unch?
962 // k is now number of checked cells (out of l total)
963 if( k *100<l*mincheck) st_lucc[l]++,v|=4; // violation flags
964 if((k-1)*100>l*maxcheck) st_locc[l]++,v|=8;
965 st_lsc[l]+=k;
966 if(k<st_lmnc[l]) st_lmnc[l]=k;
967 if(k>st_lmxc[l]) st_lmxc[l]=k;
968 st_sc+=k;
969 if(words[i].ldir<ndir[gtype])
970 for(j=0;j<l;j++) gsq[words[i].gx0][words[i].gy0].vflags[words[i].ldir]|=v; // update violation flags for non VLs
971 }
972
973 addimplicitwords();
974
975 donumbers();
976 stats_upd(); // refresh statistics window if it is in view
977 DEB_BS printf("nw=%d ntw=%d nc=%d ne=%d curent=%d curword=%d\n",nw,ntw,nc,ne,curent,curword);
978 return 0;
979 }
980
981 // symmetry functions
982 // call f on (x,y) if in range
983 static void symmdo5(void f(int,int,int,int),int k,int x,int y,int d) {
984 if(isingrid(x,y)) f(k,x,y,d);
985 }
986
987 // call symmdo5 on (x,y) and other square implied by up-and-down symmetry flag (if any)
988 static void symmdo4(void f(int,int,int,int),int k,int x,int y,int d) {
989 if(symmd&2) switch(gshape[gtype]) {
990 case 0: case 1: case 2:
991 symmdo5(f,k,x,(y+(height+1)/2)%((height+1)&~1),d);break;
992 case 3: case 4: break; // not for circular grids
993 }
994 symmdo5(f,k,x,y,d);
995 }
996
997 // call symmdo4 on (x,y) and other square implied by left-and-right symmetry flag (if any)
998 static void symmdo3(void f(int,int,int,int),int k,int x,int y,int d) {
999 if(symmd&1) switch(gshape[gtype]) {
1000 case 0: case 1: case 2:
1001 symmdo4(f,k,(x+(width+1)/2)%((width+1)&~1),y,d);break;
1002 case 3: case 4: break; // not for circular grids
1003 }
1004 symmdo4(f,k,x,y,d);
1005 }
1006
1007 // call symmdo3 on (x,y) and other square implied by vertical mirror flag (if any)
1008 static void symmdo2(void f(int,int,int,int),int k,int x,int y,int d) {int h;
1009 h=height;
1010 if(gshape[gtype]==1&&ODD(width)&&ODD(x)) h--;
1011 if(symmm&2) switch(gshape[gtype]) {
1012 case 0: case 1: case 2:
1013 symmdo3(f,k,x,h-y-1,dvflip[gtype][d]);break;
1014 case 3:
1015 if(width%2) break; // not for odd-size circular grids
1016 symmdo3(f,k,(width*3/2-x-1)%width,y,dvflip[gtype][d]);
1017 break;
1018 case 4:
1019 if(width%2) break; // not for odd-size circular grids
1020 symmdo3(f,k,(width*3/2-x)%width,y,dvflip[gtype][d]);
1021 break;
1022 }
1023 symmdo3(f,k,x,y,d);
1024 }
1025
1026 // call symmdo2 on (x,y) and other square implied by horizontal mirror flag (if any)
1027 static void symmdo1(void f(int,int,int,int),int k,int x,int y,int d) {int w;
1028 w=width;
1029 if(gshape[gtype]==2&&ODD(height)&&ODD(y)) w--;
1030 if(symmm&1) switch(gshape[gtype]) {
1031 case 0: case 1: case 2: case 3:
1032 symmdo2(f,k,w-x-1,y,dhflip[gtype][d]);break;
1033 case 4:
1034 symmdo2(f,k,(w-x)%w,y,dhflip[gtype][d]);break;
1035 break;
1036 }
1037 symmdo2(f,k,x,y,d);
1038 }
1039
1040 // get centre of rotation in 6x h-units, 4x v-units (gshape[gtype]=1; mutatis mutandis for gshape[gtype]=2)
1041 static void getcrot(int*cx,int*cy) {int w,h;
1042 w=width;h=height;
1043 if(gshape[gtype]==1) {
1044 *cx=(w-1)*3;
1045 if(EVEN(w)) *cy=h*2-1;
1046 else *cy=h*2-2;
1047 } else {
1048 *cy=(h-1)*3;
1049 if(EVEN(h)) *cx=w*2-1;
1050 else *cx=w*2-2;
1051 }
1052 }
1053
1054 // rotate by d/6 of a revolution in 6x h-units, 4x v-units (gshape[gtype]=1; mutatis mutandis for gshape[gtype]=2)
1055 static void rot6(int*x0,int*y0,int x,int y,int d) {int u,v;
1056 u=1;v=1;
1057 switch(d) {
1058 case 0:u= x*2 ;v= 2*y;break;
1059 case 1:u= x -3*y;v= x+ y;break;
1060 case 2:u=-x -3*y;v= x- y;break;
1061 case 3:u=-x*2 ;v= -2*y;break;
1062 case 4:u=-x +3*y;v=-x- y;break;
1063 case 5:u= x +3*y;v=-x+ y;break;
1064 }
1065 assert(EVEN(u));assert(EVEN(v));
1066 *x0=u/2;*y0=v/2;
1067 }
1068
1069 // call f on (x,y) and any other squares implied by symmetry flags by
1070 // calling symmdo1 on (x,y) and any other squares implied by rotational symmetry flags
1071 void symmdo(void f(int,int,int,int),int k,int x,int y,int d) {int i,mx,my,x0,y0;
1072 switch(gshape[gtype]) {
1073 case 0:
1074 switch(symmr) {
1075 case 4:symmdo1(f,k,width-y-1,x, (d+1)%4);
1076 symmdo1(f,k,y, height-x-1,(d+3)%4); // fall through
1077 case 2:symmdo1(f,k,width-x-1,height-y-1,(d+2)%4); // fall through
1078 case 1:symmdo1(f,k,x, y, d);
1079 }
1080 break;
1081 case 1:
1082 getcrot(&mx,&my);
1083 y=y*4+(x&1)*2-my;x=x*6-mx;
1084 for(i=5;i>=0;i--) {
1085 if((i*symmr)%6==0) {
1086 rot6(&x0,&y0,x,y,i);
1087 x0+=mx;y0+=my;
1088 assert(x0%6==0); x0/=6;
1089 y0-=(x0&1)*2;
1090 assert(y0%4==0); y0/=4;
1091 symmdo1(f,k,x0,y0,(d+i)%6);
1092 }
1093 }
1094 break;
1095 case 2:
1096 getcrot(&mx,&my);
1097 x=x*4+(y&1)*2-mx;y=y*6-my;
1098 for(i=5;i>=0;i--) {
1099 if((i*symmr)%6==0) {
1100 rot6(&y0,&x0,y,x,i);
1101 x0+=mx;y0+=my;
1102 assert(y0%6==0); y0/=6;
1103 x0-=(y0&1)*2;
1104 assert(x0%4==0); x0/=4;
1105 symmdo1(f,k,x0,y0,(d-i+6)%6);
1106 }
1107 }
1108 break;
1109 case 3: case 4:
1110 if(symmr==0) break; // assertion
1111 for(i=symmr-1;i>=0;i--) symmdo1(f,k,(x+i*width/symmr)%width,y,d);
1112 break;
1113 }
1114 }
1115
1116 // basic grid editing commands (candidates for f() in symmdo above)
1117 void a_editblock (int k,int x,int y,int d) {int l,gx[MXCL],gy[MXCL];
1118 l=getmergegroup(gx,gy,x,y);
1119 gsq[x][y].fl=(gsq[x][y].fl&0x06)|1;
1120 demerge(x,y);
1121 refreshsqlist(l,gx,gy);
1122 }
1123
1124 void a_editempty (int k,int x,int y,int d) {
1125 gsq[x][y].fl= gsq[x][y].fl&0x16;
1126 refreshsqmg(x,y);
1127 }
1128
1129 void a_editcutout(int k,int x,int y,int d) {int l,gx[MXCL],gy[MXCL];
1130 l=getmergegroup(gx,gy,x,y);
1131 gsq[x][y].fl=(gsq[x][y].fl&0x06)|8;
1132 demerge(x,y);
1133 refreshsqlist(l,gx,gy);
1134 }
1135
1136 // set bar state in direction d to k
1137 void a_editbar(int k,int x,int y,int d) {int tx,ty;
1138 if(!isingrid(x,y)) return;
1139 tx=x;ty=y;
1140 stepforw(&tx,&ty,d);
1141 // printf("<%d,%d %d,%d %d>\n",x,y,tx,ty,d);
1142 if(!isingrid(tx,ty)) return;
1143 setbars(x,y,d,k);
1144 refreshsqmg(x,y);
1145 refreshsqmg(tx,ty);
1146 donumbers();
1147 }
1148
1149 // set merge state in direction d to k, deal with consequences
1150 void a_editmerge(int k,int x,int y,int d) {int f,i,l,tl,tx,ty,gx[MXCL],gy[MXCL],tgx[MXCL],tgy[MXCL];
1151 if(!isclear(x,y)) return;
1152 tx=x;ty=y;
1153 stepforw(&tx,&ty,d);
1154 if(!isclear(tx,ty)) return;
1155 l=getmergegroup(gx,gy,x,y);
1156 tl=getmergegroup(tgx,tgy,tx,ty);
1157 if(k) for(i=0;i<ndir[gtype];i++) if(i!=d&&i+ndir[gtype]!=d) {
1158 setmerge(x,y,i,0);
1159 setmerge(x,y,i+ndir[gtype],0);
1160 setmerge(tx,ty,i,0);
1161 setmerge(tx,ty,i+ndir[gtype],0);
1162 }
1163 setmerge(x,y,d,k);
1164 refreshsqlist(l,gx,gy);
1165 refreshsqlist(tl,tgx,tgy);
1166 f=gsq[x][y].fl&16; // make selection flags consistent
1167 l=getmergegroup(gx,gy,x,y);
1168 for(i=0;i<l;i++) gsq[gx[i]][gy[i]].fl=(gsq[gx[i]][gy[i]].fl&~16)|f;
1169 refreshsqmg(x,y);
1170 donumbers();
1171 }
1172
1173
1174
1175
1176
1177 // PREFERENCES
1178
1179
1180 static int preftype[NPREFS]= {0,0,0, 0, 0,0,0, 0, 0,0, 1,1,1,1,1,1, 1,1,1,1,1,1, 1,1,1,0 }; // 0=int, 1=string
1181 static int prefminv[NPREFS]= {0,0,0, 0, 0,0,0,10,10,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0 }; // legal range
1182 static int prefmaxv[NPREFS]= {1,1,1,100,100,1,2,72,72,1, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,NALPHAINIT-1 };
1183 static int prefdatadefault[NPREFS]={0,0,1, 66, 75,1,0,36,36,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,ALPHABET_AZ09};
1184 static char prefstringdefault[NPREFS][SLEN+1]={
1185 "", "", "", "", "",
1186 "", "", "", "", "",
1187 "Look up at Collins online",
1188 "Look up at Oxford online",
1189 "Search at English Wiktionary",
1190 "Search at English Wikipedia",
1191 "Search at Startpage",
1192 "Look up at Dictionary.com",
1193 "https://www.collinsdictionary.com/search/?dictCode=english&q=%s",
1194 "https://en.oxforddictionaries.com/definition/%s",
1195 "https://en.wiktionary.org/w/index.php?search=%s",
1196 "https://en.wikipedia.org/w/index.php?search=%s",
1197 "https://www.startpage.com/do/search?q=%s",
1198 "https://www.dictionary.com/browse/%s?s=t",
1199 "","","",""
1200 };
1201
1202 int prefdata[NPREFS]={0};
1203 char prefstring[NPREFS][SLEN+1]={{0}};
1204
1205 static char*prefname[NPREFS]={ // names as used in preferences file
1206 "edit_click_for_block", "edit_click_for_bar", "edit_show_numbers",
1207 "stats_min_check_percent", "stats_max_check_percent",
1208 "autofill_no_duplicates", "autofill_random",
1209 "export_EPS_square_points","export_HTML_square_pixels",
1210 "light_numbers_in_solutions",
1211 "lookupname_0", "lookupname_1", "lookupname_2", "lookupname_3", "lookupname_4", "lookupname_5",
1212 "lookupuri_0", "lookupuri_1", "lookupuri_2", "lookupuri_3", "lookupuri_4", "lookupuri_5",
1213 "startup_defdict1", "startup_ff1", "startup_af1", "startup_al"
1214 };
1215
1216 static void loadprefdefaults() {
1217 memcpy(prefdata,prefdatadefault,sizeof(prefdata));
1218 memcpy(prefstring,prefstringdefault,sizeof(prefstring));
1219 }
1220
1221 // open preferences file, mode=0 for writing, mode=1 for reading
1222 FILE*openprefs(int mode) {
1223 #ifdef _WIN32 // Folder for preferences file
1224 wchar_t sw[SLEN];
1225 if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, sw))) return 0;
1226 wcscat_s(sw, SLEN, L"\\Qxw");
1227 if(mode==0) _wmkdir(sw);
1228 wcscat_s(sw, SLEN, L"\\Qxw.ini");
1229 return _wfopen(sw,mode?L"r":L"w");
1230 #else
1231 char s[SLEN];
1232 struct passwd*p;
1233 p=getpwuid(getuid());
1234 if(!p) return 0;
1235 if(strlen(p->pw_dir)>SLEN-20) return 0;
1236 strcpy(s,p->pw_dir);
1237 strcat(s,"/.qxw");
1238 if(mode==0) mkdir(s,0777);
1239 strcat(s,"/preferences");
1240 return g_fopen(s,mode?"r":"w");
1241 #endif
1242 }
1243
1244 // read preferences from file
1245 // fail silently
1246 static void loadprefs() {
1247 FILE*fp;
1248 char s[SLEN],*q;
1249 int i,u;
1250
1251 fp=openprefs(1); if(!fp) return;
1252 while (fgets(s, SLEN, fp)) {
1253 q=strchr(s,' ');
1254 if(!q) continue;
1255 *q++=0;
1256 i=strlen(q);
1257 while(i>0&&iscntrl((unsigned char)q[i-1])) q[--i]=0;
1258 for(i=0;i<NPREFS;i++) {
1259 if(strcmp(s,prefname[i])) continue;
1260 switch(preftype[i]) {
1261 case 0:
1262 u=atoi(q);
1263 if(u<prefminv[i]) u=prefminv[i];
1264 if(u>prefmaxv[i]) u=prefmaxv[i];
1265 prefdata[i]=u;
1266 break;
1267 case 1:
1268 strcpy(prefstring[i],q);
1269 break;
1270 default:
1271 break;
1272 }
1273 }
1274 }
1275 fclose(fp);
1276 }
1277
1278 // write preferences to file
1279 // fail silently
1280 void saveprefs(void) {
1281 FILE*fp;
1282 int i;
1283
1284 fp=openprefs(0); if(!fp) return;
1285 for(i=0;i<NPREFS;i++) {
1286 switch(preftype[i]) {
1287 case 0:
1288 fprintf(fp,"%s %d\n",prefname[i],prefdata[i]);
1289 break;
1290 case 1:
1291 fprintf(fp,"%s %s\n",prefname[i],prefstring[i]);
1292 break;
1293 default:
1294 break;
1295 }
1296 }
1297 fclose(fp);
1298 }
1299
1300
1301
1302
1303 // UNDO
1304
1305 static struct square ugsq[UNDOS][MXSZ][MXSZ]; // circular undo buffers
1306 static int ugtype[UNDOS];
1307 static int uwidth[UNDOS];
1308 static int uheight[UNDOS];
1309 static struct sprop udsp[UNDOS];
1310 static struct lprop udlp[UNDOS];
1311 static int unvl[UNDOS];
1312 static struct vl uvls[UNDOS][NVL];
1313
1314 static char utpifname[UNDOS][SLEN];
1315 static int utreatmode[UNDOS]; // 0=none, TREAT_PLUGIN=custom plug-in
1316 static int utreatorder[UNDOS][NMSG]; // 0=first come first served, 1=spread, 2=jumble
1317 static char utreatmsg[UNDOS][NMSG][MXLT*8+1];
1318 static ABM utreatcstr[UNDOS][NMSG][MXFL];
1319 static int utambaw[UNDOS]; // treated answer must be a word
1320
1321 int uhead=0,utail=0,uhwm=0; // undos can be performed from uhead back to utail, redos from uhead up to uhwm (`high water mark')
1322
1323
1324 void undo_push(void) {
1325 unsaved=1;
1326 #define USAVE(p) memcpy(u##p+uhead,p,sizeof(p))
1327 #define USAVES(p) memcpy(u##p+uhead,&p,sizeof(p))
1328 USAVE(gsq);
1329 USAVES(gtype);
1330 USAVES(width);
1331 USAVES(height);
1332 USAVES(dsp);
1333 USAVES(dlp);
1334 USAVES(nvl);
1335 USAVE(vls);
1336 USAVE(tpifname);
1337 USAVES(treatmode);
1338 USAVE(treatorder);
1339 USAVE(treatmsg);
1340 USAVE(treatcstr);
1341 USAVES(tambaw);
1342 uhead=(uhead+1)%UNDOS; // advance circular buffer pointer
1343 uhwm=uhead; // can no longer redo
1344 if(uhead==utail) utail=(utail+1)%UNDOS; // buffer full? delete one entry at tail
1345 DEB_GR printf("undo_push: head=%d tail=%d hwm=%d\n",uhead,utail,uhwm);
1346 }
1347
1348 void undo_pop(void) {int u;
1349 filler_stop();
1350 uhead=(uhead+UNDOS-1)%UNDOS; // back head pointer up one
1351 u=(uhead+UNDOS-1)%UNDOS; // get previous state index
1352 #define ULOAD(p) memcpy(p,u##p+u,sizeof(p))
1353 #define ULOADS(p) memcpy(&p,u##p+u,sizeof(p))
1354 ULOAD(gsq);
1355 ULOADS(gtype);
1356 ULOADS(width);
1357 ULOADS(height);
1358 ULOADS(dsp);
1359 ULOADS(dlp);
1360 ULOADS(nvl);
1361 ULOAD(vls);
1362 ULOAD(tpifname);
1363 ULOADS(treatmode);
1364 ULOAD(treatorder);
1365 ULOAD(treatmsg);
1366 ULOAD(treatcstr);
1367 ULOADS(tambaw);
1368 if(curdir>=ndir[gtype]) curdir=0;
1369 if(curx>=width) curx=width -1;
1370 if(cury>=height) cury=height-1;
1371 }
1372
1373
1374
1375
1376 static char*defaultmk(int k) {return k?"":"\\#";} // default mark is just number in NW corner
1377
1378 // convert ABM to UTF-8 choice string of the form [a-eghq-z0-39], using inverting notation if inv==1 and
1379 // including "-" if dash==1
1380 // return number of characters in UTF-8 string
1381 static int abmtocs(char*s,ABM b,int inv,int dash) {
1382 int c0,c1,w=0;
1383 *s=0;
1384 strcat(s,"["),w++;
1385 if(inv) strcat(s,"^"),w++,b^=dash?ABM_ALL:ABM_NRM;
1386 b&=abm_use|ABM_DASH;
1387 for(c0=1;;) {
1388 for(;c0<MAXICC;c0++) if(b&ICCTOABM(c0)) break;
1389 if(c0==MAXICC) break;
1390 for(c1=c0+1;c1<MAXICC;c1++) {
1391 if((b&ICCTOABM(c1))==0) break;
1392 if(c1>1&&!iccseq[c1-1]) break;
1393 }
1394 strcat(s,icctoutf8[c0]),w++;
1395 if(c1-c0>1) {
1396 if(c1-c0>2) strcat(s,"-"),w++;
1397 strcat(s,icctoutf8[c1-1]),w++;
1398 }
1399 c0=c1;
1400 }
1401 if(dash&&(b&ABM_DASH)) strcat(s,"-"),w++;
1402 strcat(s,"]"),w++;
1403 return w;
1404 }
1405
1406 // convert ABM to UTF-8 string of the form "s" or "[aeiou]" or whatever
1407 // MAXICC*16+4 is a very safe upper bound on the resulting length
1408 void abmtostr(char*s,ABM b,int dash) {
1409 int w0,w1;
1410 char s0[MAXICC*16+4],s1[MAXICC*16+4];
1411 b&=dash?ABM_ALL:ABM_NRM;
1412 if((b&(abm_use|ABM_DASH))==(abm_use|ABM_DASH)) {strcpy(s,"?"); return;}
1413 if((b& abm_use )== abm_use ) {strcpy(s,"."); return;}
1414 if(b==abm_vow) {strcpy(s,"@"); return;}
1415 if(b==abm_con) {strcpy(s,"#"); return;}
1416 if(onebit(b)) {strcpy(s,icctoutf8[abmtoicc(b)]); return;}
1417 w0=abmtocs(s0,b,0,dash);
1418 w1=abmtocs(s1,b,1,dash);
1419 // printf("abmtostr: %016llx %s %s\n",b,s0,s1);
1420 if(w0<=w1) strcpy(s,s0); // choose shorter of the two representations
1421 else strcpy(s,s1);
1422 }
1423
1424 // convert sequence of ABMs to UTF-8 string in compact format suitable for display
1425 void abmstodispstr(char*s,ABM*b,int l) {int c,i;
1426 s[0]=0;
1427 for(i=0;i<l;i++) {
1428 c=abmtoicc(*b++);
1429 if(c==-2) strcat(s," ");
1430 else if(c==-1) strcat(s,"?");
1431 else strcat(s,icctoutf8[c]);
1432 }
1433 }
1434
1435 // convert sequence of ABMs to string: concantenates results of abmtostr()
1436 void abmstostr(char*s,ABM*b,int l,int dash) {int i;
1437 *s=0;
1438 for(i=0;i<l;i++) abmtostr(s,*b++,dash),s+=strlen(s);
1439 }
1440
1441 // call abmtostr() and print the result
1442 void pabm(ABM b,int dash) {
1443 char s[MAXICC*16+4];
1444 abmtostr(s,b,dash);
1445 printf("%s",s);
1446 }
1447
1448 // call abmstostr() and print the result
1449 void pabms(ABM*b,int l,int dash) {
1450 int i;
1451 for(i=0;i<l;i++) pabm(b[i],dash);
1452 }
1453
1454
1455 // convert string containing choice lists to array of ABMs
1456 // if dash==1, dash is allowed at end of each choice list
1457 // returns number of ABMs, maximum l
1458 int strtoabms(ABM*p,int l,char*s0,int dash) {
1459 int c,c0,c1,f,i;
1460 ABM b;
1461 uchar t[SLEN],*s;
1462
1463 utf8touchars(t,s0,SLEN);
1464 // printf("strtoabms: >%s<",s0);
1465 // for(i=0;t[i];i++) printf(" %08x",t[i]);
1466 // printf("\n");
1467
1468 for(i=0,s=t;*s&&i<l;) {
1469 c=uchartoICC(*s);
1470 if(dash&&*s=='-') b=ABM_DASH,s++;
1471 else if(*s=='?') b=ABM_ALL,s++;
1472 else if(*s=='.') b=ABM_NRM,s++;
1473 else if(*s==' ') b=ABM_NRM,s++;
1474 else if(*s=='@') b=abm_vow,s++;
1475 else if(*s=='#') b=abm_con,s++;
1476 else if(*s=='[') {
1477 s++;
1478 f=0; b=0;
1479 if(*s=='^') f=1,s++;
1480 for(;;) {
1481 if(dash&&*s=='-') {b|=ABM_DASH; s++; continue;}
1482 if(*s==']') break;
1483 if(*s=='@') { b|=abm_vow; s++; continue; }
1484 if(*s=='#') { b|=abm_con; s++; continue; }
1485 c=uchartoICC(*s);
1486 if(c==0) break;
1487 c0=c1=c;
1488 if(s[1]=='-') {
1489 c=uchartoICC(s[2]);
1490 if(c) {
1491 if(c>c0) c1=c; else c0=c; // found a range: sort bounds
1492 s+=2;
1493 }
1494 }
1495 for(c=c0;c<=c1;c++) b|=ICCTOABM(c);
1496 s++;
1497 }
1498 if(*s==']') s++;
1499 if(f) b^=dash?ABM_ALL:ABM_NRM; // negation flag
1500 }
1501 else if(c>0) b=ICCTOABM(c),s++;
1502 else {s++; continue;} // skip unrecognised characters (includes BOM)
1503 b&=dash?ABM_ALL:ABM_NRM;
1504 p[i++]=b;
1505 }
1506 return i;
1507 }
1508
1509 static void resetsp(struct sprop*p) {int k;
1510 p->bgcol=0xffffff;
1511 p->fgcol=0x000000;
1512 p->mkcol=0x000000;
1513 p->fstyle=0;
1514 p->dech=0;
1515 p->ten=0;
1516 // for(k=0;k<MAXNMK;k++) sprintf(p->mk[k],"%c",k+'a'); // for testing
1517 for(k=0;k<MAXNMK;k++) strcpy(p->mk[k],defaultmk(k));
1518 p->spor=0;
1519 }
1520
1521 void resetlp(struct lprop*p) {
1522 p->dmask=1;
1523 p->emask=EM_FWD;
1524 p->ten=0;
1525 p->dnran=0;
1526 p->mux=0;
1527 p->lpor=0;
1528 }
1529
1530 // generate "codeword" permutation cwperm mapping ICC to ICC
1531 void initcodeword() {
1532 int i,j,k,p[MAXICC],u0,u1;
1533 for(i=1,j=0;i<MAXICC;i++) {
1534 cwperm[i]=0; // default
1535 k=icctousedindex[i];
1536 if(k>=0&&iccusedtogroup[k]==0) { // skip non-letters
1537 cwperm[i]=j+1; // count from 1
1538 p[j]=i;
1539 j++;
1540 }
1541 }
1542 // here there are j permutable letters, indices in p[]
1543 if(j>1) // something worth permuting?
1544 for(i=0;i<j*j*10;i++) {u0=p[rand()%j]; u1=p[rand()%j]; k=cwperm[u0]; cwperm[u0]=cwperm[u1]; cwperm[u1]=k;}
1545 }
1546
1547 void initsquare(int x,int y) {
1548 int k;
1549 memset(&gsq[x][y],0,sizeof(gsq[x][y])); // inter alia ensure ents[][] are all NULL
1550 gsq[x][y].ctlen[0]=1;
1551 gsq[x][y].ctbm[0][0]=ABM_NRM;
1552 resetsp(&gsq[x][y].sp);
1553 for(k=0;k<MAXNDIR;k++) resetlp(&gsq[x][y].lp[k]);
1554 }
1555
1556 static void resetstate(void) {
1557 int i,j;
1558 for(i=0;i<MXSZ;i++) for(j=0;j<MXSZ;j++) initsquare(i,j);
1559 resetsp(&dsp);
1560 resetlp(&dlp);
1561 for(i=0;i<NVL;i++) vls[i].l=0,vls[i].sel=0;
1562 for(i=0;i<NVL;i++) resetlp(&vls[i].lp);
1563 for(i=0;i<NVL;i++) for(j=0;j<MXCL;j++) vls[i].x[j]=0,vls[i].y[j]=0;
1564 nvl=0;
1565 nsel=0;
1566 selmode=0;
1567 treatmode=0;
1568 for(i=0;i<NMSG;i++) {
1569 treatorder[i]=0;
1570 treatmsg[i][0]=0;
1571 for(j=0;j<MXFL;j++) treatcstr[i][j]=ABM_ALL;
1572 }
1573 tpifname[0]=0;
1574 tambaw=0;
1575 unsaved=0;
1576 bldstructs();
1577 }
1578
1579 // tidy up square properties structure
1580 static void fixsp(struct sprop*sp) {
1581 sp->bgcol&=0xffffff;
1582 sp->fgcol&=0xffffff;
1583 sp->mkcol&=0xffffff;
1584 // if(sp->fstyle<0) sp->fstyle=0;
1585 if(sp->fstyle>3) sp->fstyle=3;
1586 sp->ten=!!sp->ten;
1587 // if(sp->dech<0) sp->dech=0;
1588 if(sp->dech>2) sp->dech=2;
1589 sp->spor=!!sp->spor;
1590 }
1591
1592 // tidy up light properties structure
1593 static void fixlp(struct lprop*lp) {
1594 lp->dmask&=(1<<MAXNDICTS)-1;
1595 lp->emask&=(1<<NLEM)-1;
1596 if(lp->emask==0) lp->emask=EM_FWD;
1597 lp->ten=!!lp->ten;
1598 lp->lpor=!!lp->lpor;
1599 lp->dnran=!!lp->dnran;
1600 }
1601
1602 // FILE SAVE/LOAD
1603
1604 FILE* q_fopen(char* filename, char* mode) {
1605 #ifdef _WIN32
1606 wchar_t wfilename[SLEN];
1607 wchar_t wmode[4];
1608 MultiByteToWideChar(CP_UTF8, 0, filename, -1, wfilename, SLEN);
1609 MultiByteToWideChar(CP_UTF8, 0, mode, -1, wmode, 4);
1610 return _wfopen(wfilename, wmode);
1611 #else
1612 return g_fopen(filename, mode);
1613 #endif
1614 }
1615
1616 // flags b23..16: grid width
1617 // flags b15..8: grid height
1618 // flags b7..0: grain
1619 void a_filenew(int flags) {
1620 char*p;
1621 int u,v,x,y;
1622
1623 filler_stop();
1624 resetstate();
1625 u=(flags>>16)&0xff;
1626 v=(flags>>8)&0xff;
1627 if(u>0&&u<=MXSZ&&v>0&&v<MXSZ) {
1628 gtype=0,width=u,height=v;
1629 if((flags&0x80)==0) { // pre-fill with blocks?
1630 u=!!(flags&1); v=!!(flags&2);
1631 for(x=0;x<width;x++) for(y=0;y<height;y++) if((x+u)&(y+v)&1) gsq[x][y].fl|=1;
1632 }
1633 }
1634 if(!strcmp(filenamebase,"")) { // brand new file
1635 #ifdef _WIN32
1636 wchar_t sw[SLEN];
1637 size_t l;
1638 if(SHGetFolderPathW(NULL, CSIDL_MYDOCUMENTS, NULL, 0, sw)==S_OK) {
1639 l = WideCharToMultiByte(CP_UTF8, 0, sw, -1, filenamebase, SLEN, NULL, NULL);
1640 if(l<1||l>SLEN-20) strcpy(filenamebase,"");
1641 else strcat(filenamebase,"\\");
1642 }
1643 else strcpy(filenamebase,"");
1644 #else
1645 struct passwd*pw;
1646 pw=getpwuid(getuid());
1647 if(!pw||strlen(pw->pw_dir)>SLEN-20) strcpy(filenamebase,"");
1648 else strcpy(filenamebase,pw->pw_dir),strcat(filenamebase,"/");
1649 #endif
1650 } else { // we have a path to start from
1651 p=strrchr(filenamebase,DIR_SEP_CHAR);
1652 if(p) strcpy(p,DIR_SEP_STR);
1653 else strcpy(filenamebase,"");
1654 }
1655 strcat(filenamebase,"untitled");
1656 havesavefn=0;
1657 donumbers();
1658 undo_push();
1659 unsaved=0;
1660 }
1661
1662 #define SAVE_VERSION 5
1663
1664 // read next line from file, strip terminators so compatible with MS-DOS and Unix
1665 #define NEXTL {if(!fgets(s,SLEN*4-1,fp)) goto ew1; l=strlen(s); while(l>0&&iscntrl((unsigned char)s[l-1])) s[--l]='\0';}
1666 #define NEXTLN {if(!fgets(s,SLEN*4-1,fp)) goto ew4; l=strlen(s); while(l>0&&iscntrl((unsigned char)s[l-1])) s[--l]='\0';} // fail gracefully on EOF
1667
1668 // load state from file
1669 void a_load(void) {
1670 int d,i,j,k,l,n,u,t0,t1,t2,t3,b,m,f,wf,ver;
1671 char *p,s[SLEN*4],s0[SLEN*4],*t,c;
1672 struct sprop sp;
1673 struct lprop lp;
1674 FILE*fp;
1675
1676 filler_stop();
1677 DEB_GR printf("filler stopped: loading\n");
1678 setfilenamebase(filename);
1679 fp=q_fopen(filename,"r");
1680 if(!fp) {fserror();return;}
1681 resetstate();
1682 initalphamap(alphainitdata[ALPHABET_AZ09]); // default for legacy save files and .SYM
1683 *gtitle=0;
1684 *gauthor=0;
1685 NEXTL;
1686 wf=0;
1687
1688 if(STRSTARTS(s,"#QXW2")) {
1689 // #QXW2 load
1690 ver=0;
1691 if(s[5]=='v'&&isdigit((unsigned char)s[6])) ver=atoi(s+6);
1692 if(ver>SAVE_VERSION) wf=1; // check if the file was saved using a newer version
1693 if(ver>=4) clearalphamap(); // ver>=4 will include alphabet information
1694 NEXTL;
1695 if(sscanf(s,"GP %d %d %d %d %d %d\n",&gtype,&width,&height,&symmr,&symmm,&symmd)!=6) goto ew1;
1696 if(gtype<0||gtype>=NGTYPE|| // validate basic parameters
1697 width<1||width>MXSZ||
1698 height<1||height>MXSZ) goto ew1;
1699 if(symmr<1||symmr>12) symmr=1;
1700 if(symmm<0||symmm>3) symmm=0;
1701 if(symmd<0||symmd>3) symmd=0;
1702 if((symmr&symmrmask())==0) symmr=1;
1703 draw_init();
1704 NEXTL;
1705 if(strcmp(s,"TTL")) goto ew1;
1706 NEXTL;
1707 if(s[0]!='+') goto ew1;
1708 strncpy(gtitle,s+1,SLEN-1);
1709 gtitle[SLEN-1]=0;
1710 NEXTL;
1711 if(strcmp(s,"AUT")) goto ew1;
1712 NEXTL;
1713 if(s[0]!='+') goto ew1;
1714 strncpy(gauthor,s+1,SLEN-1);
1715 gauthor[SLEN-1]=0;
1716 DEB_GR printf("L0\n");
1717 NEXTL;
1718 while(STRSTARTS(s,"ALP ")) {
1719 if(sscanf(s,"ALP %d %d %d %d\n",&i,&t0,&t1,&t2)<4) goto ew1;
1720 NEXTL;
1721 if(s[0]!='+') goto ew1;
1722 strcpy(s0,s);
1723 NEXTL;
1724 if(s[0]!='+') goto ew1;
1725 if(s0[1]) addalphamapentry(i,s0+1,s+1,!!t0,!!t1,!!t2); // add entry if non-empty
1726 NEXTL;
1727 }
1728 if(STRSTARTS(s,"GLP ")) {
1729 int ten,lpor,dnran=0,mux=0;
1730 resetlp(&dlp);
1731 if(sscanf(s,"GLP %u %u %d %d %d %d\n",&dlp.dmask,&dlp.emask,&ten,&lpor,&dnran,&mux)<4) goto ew1;
1732 dlp.ten=ten; // using %hhd above fails in Visual C
1733 dlp.lpor=0;
1734 dlp.dnran=dnran;
1735 dlp.mux=mux;
1736 fixlp(&dlp);
1737 NEXTL;
1738 }
1739 while(STRSTARTS(s,"GSP ")) {
1740 int ten,spor,fstyle=0,dech=0;
1741 resetsp(&dsp);
1742 if(sscanf(s,"GSP %x %x %d %d %d %d %x\n",&dsp.bgcol,&dsp.fgcol,&ten,&spor,&fstyle,&dech,&dsp.mkcol)<4) goto ew1;
1743 dsp.ten=ten;
1744 dsp.spor=0;
1745 dsp.fstyle=fstyle;
1746 dsp.dech=dech;
1747 fixsp(&dsp);
1748 NEXTL;
1749 }
1750 while(STRSTARTS(s,"GSPMK ")) {
1751 if(sscanf(s,"GSPMK %d\n",&k)<1) goto ew1;
1752 if(k<0||k>=MAXNMK) continue;
1753 NEXTL;
1754 if(s[0]=='+') {
1755 strncpy(dsp.mk[k],s+1,MXMK);
1756 dsp.mk[k][MXMK]='\0';
1757 }
1758 NEXTL;
1759 }
1760 while(STRSTARTS(s,"TM ")) {
1761 t2=0; t3=0;
1762 if(sscanf(s,"TM %d %d %d %d %d\n",&j,&t0,&t1,&t2,&t3)<3) goto ew1;
1763 NEXTL;
1764 if(j==0&&s[0]=='+') {
1765 if(t0<0||t0>=NATREAT) continue;
1766 treatmode=t0,tambaw=!!t1;
1767 if(t2<0||t2>2) t2=0;
1768 treatorder[0]=t2;
1769 if(t3<0||t3>2) t3=0;
1770 treatorder[1]=t3;
1771 if(treatmode==TREAT_PLUGIN) {
1772 strncpy(tpifname,s+1,SLEN-1);
1773 tpifname[SLEN-1]=0;
1774 }
1775 NEXTL;
1776 }
1777 }
1778 while(STRSTARTS(s,"TMSG ")) {
1779 if(sscanf(s,"TMSG %d %d\n",&j,&t0)!=2) goto ew1;
1780 NEXTL;
1781 if(j==0&&s[0]=='+') {
1782 if(t0<0||t0>=NMSG) continue;
1783 strncpy(treatmsg[t0],s+1,sizeof(treatmsg[0])-1);
1784 treatmsg[t0][sizeof(treatmsg[0])-1]=0;
1785 NEXTL;
1786 }
1787 }
1788 while(STRSTARTS(s,"TCST ")) {
1789 if(sscanf(s,"TCST %d %d %s\n",&i,&j,s0)!=3) goto ew1;
1790 if(i>=0&&i<NMSG&&j>=0&&j<MXFL)
1791 strtoabms(treatcstr[i]+j,1,s0,1);
1792 NEXTL;
1793 }
1794 while(STRSTARTS(s,"DFN ")) {
1795 if(sscanf(s,"DFN %d\n",&j)!=1) goto ew1;
1796 NEXTL;
1797 if(j<0||j>=MAXNDICTS) continue;
1798 if(s[0]=='+') {
1799 strncpy(dfnames[j],s+1,SLEN-1);
1800 dfnames[j][SLEN-1]=0;
1801 NEXTL;
1802 }
1803 }
1804 while(STRSTARTS(s,"DSF ")) {
1805 if(sscanf(s,"DSF %d\n",&j)!=1) goto ew1;
1806 NEXTL;
1807 if(j<0||j>=MAXNDICTS) continue;
1808 if(s[0]=='+') {
1809 strncpy(dsfilters[j],s+1,SLEN-1);
1810 dsfilters[j][SLEN-1]=0;
1811 NEXTL;
1812 }
1813 }
1814 while(STRSTARTS(s,"DAF ")) {
1815 if(sscanf(s,"DAF %d\n",&j)!=1) goto ew1;
1816 NEXTL;
1817 if(j<0||j>=MAXNDICTS) continue;
1818 if(s[0]=='+') {
1819 strncpy(dafilters[j],s+1,SLEN-1);
1820 dafilters[j][SLEN-1]=0;
1821 NEXTL;
1822 }
1823 }
1824 DEB_GR printf("L1: %s\n",s);
1825 while(STRSTARTS(s,"SQ ")) {
1826 c=' ';
1827 // DEB_GR printf("SQ: ");
1828 if(sscanf(s,"SQ %d %d %d %d %d %c\n",&i,&j,&b,&m,&f,&c)<5) goto ew1;
1829 // DEB_GR printf("%d,%d %d\n",i,j,b);
1830 if(i<0||i>=MXSZ||j<0||j>=MXSZ) continue;
1831 gsq[i][j].bars =b&((1<<ndir[gtype])-1);
1832 gsq[i][j].merge =m&((1<<ndir[gtype])-1);
1833 gsq[i][j].fl =f&0x09;
1834 if(c==' ') gsq[i][j].ctbm[0][0]=ABM_NRM;
1835 else gsq[i][j].ctbm[0][0]=ICCTOABM(uchartoICC((int)c));
1836 if(!onebit(gsq[i][j].ctbm[0][0])) gsq[i][j].ctbm[0][0]=ABM_NRM;
1837 NEXTL;
1838 }
1839 DEB_GR printf("L2\n");
1840 while(STRSTARTS(s,"SQSP ")) {
1841 int ten,spor,fstyle=0,dech=0;
1842 resetsp(&sp);
1843 if(sscanf(s,"SQSP %d %d %x %x %d %d %d %d %x\n",&i,&j,&sp.bgcol,&sp.fgcol,&ten,&spor,&fstyle,&dech,&sp.mkcol)<6) goto ew1;
1844 if(i<0||i>=MXSZ||j<0||j>=MXSZ) continue;
1845 sp.ten=ten;
1846 sp.spor=spor;
1847 sp.fstyle=fstyle;
1848 sp.dech=dech;
1849 fixsp(&sp);
1850 gsq[i][j].sp=sp;
1851 NEXTL;
1852 }
1853 while(STRSTARTS(s,"SQSPMK ")) {
1854 if(sscanf(s,"SQSPMK %d %d %d\n",&i,&j,&k)<3) goto ew1;
1855 if(i<0||i>=MXSZ||j<0||j>=MXSZ||k<0||k>=MAXNMK) continue;
1856 NEXTL;
1857 if(s[0]=='+') {
1858 strncpy(gsq[i][j].sp.mk[k],s+1,MXMK);
1859 gsq[i][j].sp.mk[k][MXMK]='\0';
1860 }
1861 NEXTL;
1862 }
1863 while(STRSTARTS(s,"SQLP ")) {
1864 int ten,lpor,dnran=0,mux=0;
1865 resetlp(&lp);
1866 if(sscanf(s,"SQLP %d %d %d %u %u %d %d %d %d\n",&i,&j,&d,&lp.dmask,&lp.emask,&ten,&lpor,&dnran,&mux)<7) goto ew1;
1867 if(i<0||i>=MXSZ||j<0||j>=MXSZ||d<0||d>=ndir[gtype]) continue;
1868 lp.ten=ten;
1869 lp.lpor=lpor;
1870 lp.dnran=dnran;
1871 lp.mux=mux;
1872 fixlp(&lp);
1873 gsq[i][j].lp[d]=lp;
1874 NEXTL;
1875 }
1876 while(STRSTARTS(s,"VL ")) {
1877 if(sscanf(s,"VL %d %d %d %d\n",&d,&n,&i,&j)!=4) goto ew1;
1878 if(i<0||i>=MXSZ||j<0||j>=MXSZ||n<0||n>=MXCL||d<0||d>=NVL) continue;
1879 vls[d].x[n]=i;
1880 vls[d].y[n]=j;
1881 if(n>=vls[d].l) vls[d].l=n+1;
1882 if(d>=nvl) nvl=d+1;
1883 NEXTL;
1884 }
1885 while(STRSTARTS(s,"VLP ")) {
1886 int ten,lpor,dnran=0,mux=0;
1887 resetlp(&lp);
1888 if(sscanf(s,"VLP %d %u %u %d %d %d %d\n",&d,&lp.dmask,&lp.emask,&ten,&lpor,&dnran,&mux)<5) goto ew1;
1889 if(d<0||d>=NVL) continue;
1890 lp.ten=ten;
1891 lp.lpor=lpor;
1892 lp.dnran=dnran;
1893 lp.mux=mux;
1894 fixlp(&lp);
1895 vls[d].lp=lp;
1896 NEXTL;
1897 }
1898 while(STRSTARTS(s,"SQCT ")) {
1899 if(sscanf(s,"SQCT %d %d %d %s\n",&i,&j,&d,s0)!=4) goto ew1;
1900 if(i<0||i>=MXSZ||j<0||j>=MXSZ||d<0||d>=MAXNDIR) continue;
1901 p=s0;
1902 l=strlen(p);
1903 if(l>1&&*p=='\"'&&p[l-1]=='\"') p[l-1]=0,p++; // strip double quote marks if any
1904 gsq[i][j].ctlen[d]=strtoabms(gsq[i][j].ctbm[d],MXCT,p,0);
1905 NEXTL;
1906 }
1907 if(ver>=4) initcodeword(); // ver>=4 may have updated alphabet information
1908 }
1909 else if(strlen(s)>3&&STRSTARTS(s+2,"ympath")) { // necessarily incomplete as we don't have documentation
1910 int chkt=0,symt=0,blkh=0,x0,x1,y0,y1,dir;
1911 NEXTL;
1912 if(sscanf(s,"[Grid %d %d]",&width,&height)!=2) goto ew1;
1913 if(width<1||width>MXSZ|| // validate basic parameters
1914 height<1||height>MXSZ) goto ew1;
1915 draw_init();
1916 for(;;) {
1917 NEXTL;
1918 if(STRSTARTS(s,"[Light ")) break;
1919 if(STRSTARTS(s,"#Title=" )) { strncpy(gtitle ,s+7,SLEN-1); gtitle [SLEN-1]=0; continue; }
1920 if(STRSTARTS(s,"#Author=")) { strncpy(gauthor,s+8,SLEN-1); gauthor[SLEN-1]=0; continue; }
1921 if(STRSTARTS(s,"BlockHole=" )) { blkh=atoi(s+10); continue; }
1922 if(STRSTARTS(s,"CheckingType=")) { chkt=atoi(s+13); continue; }
1923 if(STRSTARTS(s,"SymmetryType=")) { symt=atoi(s+13); continue; }
1924 DEB_GR printf("Unprocessed: <%s>\n",s);
1925 }
1926 DEB_GR printf("chkt=%d symt=%d; now processing lights\n",chkt,symt);
1927 if(chkt==0) { // barred? add bars everywhere
1928 for(j=0;j<height ;j++) for(i=0;i<width-1;i++) gsq[i][j].bars =1;
1929 for(j=0;j<height-1;j++) for(i=0;i<width ;i++) gsq[i][j].bars|=2;
1930 }
1931 for(j=0;j<height;j++) for(i=0;i<width;i++) gsq[i][j].fl=blkh?8:1;
1932 for(;;) { // process the lights
1933 if(sscanf(s,"[Light %d %d %d %d]",&x0,&y0,&x1,&y1)!=4) goto ew1;
1934 DEB_GR printf("(%d,%d - %d,%d)\n",x0,y0,x1,y1);
1935 if(x0<0||x0>width||y0<0||y0>height) goto ew1;
1936 if(x1<0||x1>width||y1<0||y1>height) goto ew1;
1937 if(y0==y1) { // across
1938 for(i=x0;i<x1 ;i++) gsq[i][y0].fl=0;
1939 if(chkt==0) for(i=x0;i<x1-1;i++) gsq[i][y0].bars&=~1;
1940 dir=0;
1941 }
1942 else if(x0==x1) { // down
1943 for(j=y0;j<y1 ;j++) gsq[x0][j].fl=0;
1944 if(chkt==0) for(j=y0;j<y1-1;j++) gsq[x0][j].bars&=~2;
1945 dir=1;
1946 }
1947 else {
1948 DEB_GR printf("Light %d %d %d %d is not straight\n",x0,y0,x1,y1);
1949 goto ew1;
1950 }
1951 for(;;) {
1952 NEXTLN;
1953 if(STRSTARTS(s,"[Light ")) break; // process next light
1954 if(STRSTARTS(s,"Text=")) {
1955 i=x0; j=y0; p=s+5;
1956 while(*p) {
1957 gsq[i][j].ctbm[0][0]=ICCTOABM(uchartoICC((int)*p++));
1958 if(dir==0) i++;
1959 else j++;
1960 if(i==x1&&j==y1) break;
1961 }
1962 continue;
1963 }
1964 if(STRSTARTS(s,"Number=")) { // set dnran
1965 if(!strcmp(s,"Number=")) { // only for exact match: presumably there are other possibilities than "empty"
1966 gsq[x0][y0].lp[dir].lpor =1;
1967 gsq[x0][y0].lp[dir].dnran=1;
1968 continue;
1969 }
1970 }
1971 if(STRSTARTS(s,"BackgroundColor=")) { // sic
1972 int r,g,b;
1973 if(sscanf(s,"BackgroundColor=%d,%d,%d",&r,&g,&b)==3) {
1974 i=x0; j=y0;
1975 for(;;) {
1976 gsq[i][j].sp.spor=1;
1977 gsq[i][j].sp.bgcol=((r&0xff)<<16) | ((g&0xff)<< 8) | (b&0xff);
1978 if(dir==0) i++;
1979 else j++;
1980 if(i==x1&&j==y1) break;
1981 }
1982 continue;
1983 }
1984 }
1985 DEB_GR printf("Unprocessed: <%s>\n",s);
1986 }
1987 }
1988 ew4:
1989 for(j=0;j<height;j++) for(i=0;i<width;i++) if(gsq[i][j].fl==8) {
1990 gsq[i ][j ].bars=0;
1991 if(i>0) gsq[i-1][j ].bars&=~1;
1992 if(j>0) gsq[i ][j-1].bars&=~2;
1993 }
1994 }
1995 else {
1996 // LEGACY LOAD
1997 gtype=0;
1998 if(sscanf(s,"%d %d %d %d %d\n",&width,&height,&symmr,&symmm,&symmd)!=5) goto ew1;
1999 if(width<1||width>MXSZ|| // validate basic parameters
2000 height<1||height>MXSZ||
2001 symmr<0||symmr>2||
2002 symmm<0||symmm>3||
2003 symmd<0||symmd>3) goto ew1;
2004 if (symmr==1) symmr=2;
2005 else if(symmr==2) symmr=4;
2006 else symmr=1;
2007 draw_init();
2008 for(j=0;j<height;j++) { // read flags
2009 NEXTL;
2010 t=s;
2011 for(i=0;i<width;i++) {
2012 u=strtol(t,&t,10);
2013 if(u<0||u>31) goto ew1;
2014 gsq[i][j].fl=u&0x09;
2015 gsq[i][j].bars=(u>>1)&3;
2016 gsq[i][j].merge=0;
2017 }
2018 }
2019 for(j=0;j<height;j++) { // read grid
2020 for(i=0;i<width;i++) {
2021 c=fgetc(fp);
2022 if((c<'A'||c>'Z')&&(c<'0'||c>'9')&&c!=' ') goto ew1;
2023 gsq[i][j].ctbm[0][0]=ICCTOABM(uchartoICC((int)c));
2024 }
2025 if(fgetc(fp)!='\n') goto ew1;
2026 }
2027 }
2028
2029 if(fclose(fp)) goto ew3;
2030 reloadtpi();
2031 donumbers();
2032 loaddicts(0);
2033 undo_push();unsaved=0;
2034 if(wf) repwarn("File was saved using\na newer version of Qxw.\nSome features may be lost.");
2035 havesavefn=1;
2036 bldstructs();
2037 return; // no errors
2038
2039 ew1:fclose(fp);syncgui();reperr("File format error");goto ew2;
2040 ew3:fserror();
2041 ew2:
2042 a_filenew(0);
2043 initalphamap(alphainitdata[startup_al]); // use preferences value of default alphabet
2044 loaddefdicts();
2045 bldstructs();
2046 }
2047
2048 // write state to file
2049 void a_save(void) {
2050 int d,i,j,k,l;
2051 char s0[MXCT*(MAXICC*16+4)];
2052 FILE*fp;
2053
2054 setfilenamebase(filename);
2055 fp=q_fopen(filename,"w");
2056 if(!fp) {fserror();return;}
2057 if(fprintf(fp,"#QXW2v%d http://www.quinapalus.com\n",SAVE_VERSION)<0) goto ew0;
2058 if(fprintf(fp,"GP %d %d %d %d %d %d\n",gtype,width,height,symmr,symmm,symmd)<0) goto ew0;
2059 if(fprintf(fp,"TTL\n+%s\n",gtitle)<0) goto ew0;
2060 if(fprintf(fp,"AUT\n+%s\n",gauthor)<0) goto ew0;
2061 for(i=1;i<MAXICC;i++) {
2062 if(icctoutf8[i][0]==0) continue;
2063 if(!strcmp(icctoutf8[i],"�")) continue;
2064 if(fprintf(fp,"ALP %d %d %d %d\n+%s\n+%s\n",i,!!(abm_vow&ICCTOABM(i)),(int)!!(abm_con&ICCTOABM(i)),iccseq[i],icctoutf8[i],iccequivs[i])<0) goto ew0;
2065 }
2066 if(fprintf(fp,"GLP %d %d %d %d %d %d\n",dlp.dmask,dlp.emask,dlp.ten,dlp.lpor,dlp.dnran,dlp.mux)<0) goto ew0;
2067 if(fprintf(fp,"GSP %06x %06x %d %d %d %d %06x\n",dsp.bgcol,dsp.fgcol,dsp.ten,dsp.spor,dsp.fstyle,dsp.dech,dsp.mkcol)<0) goto ew0;
2068 for(k=0;k<MAXNMK;k++) if(fprintf(fp,"GSPMK %d\n+%s\n",k,dsp.mk[k])<0) goto ew0;
2069 if(fprintf(fp,"TM 0 %d %d %d %d\n+%s\n",treatmode,tambaw,treatorder[0],treatorder[1],tpifname)<0) goto ew0;
2070 for(i=0;i<NMSG;i++) if(fprintf(fp,"TMSG 0 %d\n+%s\n",i,treatmsg[i])<0) goto ew0;
2071 for(i=0;i<NMSG;i++) for(j=0;j<MXFL;j++) if(treatcstr[i][j]!=ABM_ALL) {
2072 abmtostr(s0,treatcstr[i][j],1);
2073 if(fprintf(fp,"TCST %d %d %s\n",i,j,s0)<0) goto ew0;
2074 }
2075 for(i=0;i<MAXNDICTS;i++) if(fprintf(fp,"DFN %d\n+%s\n",i,dfnames[i])<0) goto ew0;
2076 for(i=0;i<MAXNDICTS;i++) if(fprintf(fp,"DSF %d\n+%s\n",i,dsfilters[i])<0) goto ew0;
2077 for(i=0;i<MAXNDICTS;i++) if(fprintf(fp,"DAF %d\n+%s\n",i,dafilters[i])<0) goto ew0;
2078 for(j=0;j<height;j++) for(i=0;i<width;i++) if(fprintf(fp,"SQ %d %d %d %d %d .\n",i,j,gsq[i][j].bars,gsq[i][j].merge,gsq[i][j].fl)<0) goto ew0;
2079 for(j=0;j<height;j++) for(i=0;i<width;i++)
2080 if(fprintf(fp,"SQSP %d %d %06x %06x %d %d %d %d %06x\n",i,j,
2081 gsq[i][j].sp.bgcol,gsq[i][j].sp.fgcol,gsq[i][j].sp.ten,gsq[i][j].sp.spor,gsq[i][j].sp.fstyle,gsq[i][j].sp.dech,gsq[i][j].sp.mkcol)<0) goto ew0;
2082 for(j=0;j<height;j++) for(i=0;i<width;i++)
2083 for(k=0;k<MAXNMK;k++) if(fprintf(fp,"SQSPMK %d %d %d\n+%s\n",i,j,k,gsq[i][j].sp.mk[k])<0) goto ew0;
2084 for(j=0;j<height;j++) for(i=0;i<width;i++) for(d=0;d<ndir[gtype];d++)
2085 if(fprintf(fp,"SQLP %d %d %d %d %d %d %d %d %d\n",i,j,d,gsq[i][j].lp[d].dmask,gsq[i][j].lp[d].emask,gsq[i][j].lp[d].ten,gsq[i][j].lp[d].lpor,gsq[i][j].lp[d].dnran,gsq[i][j].lp[d].mux)<0) goto ew0;
2086 for(d=0;d<nvl;d++) for(i=0;i<vls[d].l;i++)
2087 if(fprintf(fp,"VL %d %d %d %d\n",d,i,vls[d].x[i],vls[d].y[i])<0) goto ew0;
2088 for(d=0;d<nvl;d++)
2089 if(fprintf(fp,"VLP %d %d %d %d %d %d %d\n",d,vls[d].lp.dmask,vls[d].lp.emask,vls[d].lp.ten,vls[d].lp.lpor,vls[d].lp.dnran,vls[d].lp.mux)<0) goto ew0;
2090 for(j=0;j<height;j++) for(i=0;i<width;i++) for(d=0;d<ndir[gtype];d++) {
2091 l=gsq[i][j].ctlen[d];
2092 abmstostr(s0,gsq[i][j].ctbm[d],l,0);
2093 if(fprintf(fp,"SQCT %d %d %d \"%s\"\n",i,j,d,s0)<0) goto ew0;
2094 }
2095 if(fprintf(fp,"END\n")<0) goto ew0;
2096 if(ferror(fp)) goto ew0;
2097 if(fclose(fp)) {fserror(); return;}
2098 havesavefn=1;
2099 unsaved=0;return; // saved successfully
2100 ew0:
2101 fserror();
2102 fclose(fp);
2103 }
2104
2105 void a_importvls(char*fn) {
2106 FILE*fp;
2107 struct vl tvls[NVL];
2108 char s[SLEN*4];
2109 int d,i,l;
2110
2111 filler_stop();
2112 DEB_GR printf("filler stopped: importing VL:s\n");
2113 fp=q_fopen(fn,"r");
2114 if(!fp) goto ew3;
2115 for(d=0;d<NVL;) {
2116 tvls[d].l=0;
2117 resetlp(&tvls[d].lp);
2118 tvls[d].sel=0;
2119 tvls[d].w=0;
2120 for(i=0;;) {
2121 if(!fgets(s,SLEN*4-1,fp)) {
2122 if(feof(fp)) goto ew0;
2123 else goto ew3;
2124 }
2125 l=strlen(s); while(l>0&&!isprint((unsigned char)s[l-1])) s[--l]='\0';
2126 if(l==0) break; // end of this vl
2127 if(s[0]=='#') continue; // comment line?
2128 if(i>=MXCL) goto ew1;
2129 if(sscanf(s,"%d %d",tvls[d].x+i,tvls[d].y+i)<2) goto ew1;
2130 if(tvls[d].x[i]<0||tvls[d].x[i]>=MXSZ) goto ew1;
2131 if(tvls[d].y[i]<0||tvls[d].y[i]>=MXSZ) goto ew1;
2132 i++;
2133 tvls[d].l=i;
2134 }
2135 if(tvls[d].l>0) d++;
2136 }
2137 ew0:
2138 if(fclose(fp)) goto ew3;
2139 if(tvls[d].l>0) d++;
2140 memcpy(vls,tvls,d*sizeof(struct vl));
2141 nvl=d;
2142 undo_push();unsaved=0;syncgui();compute(0);
2143 return;
2144 ew3:fserror();return;
2145 ew1:fclose(fp);syncgui();reperr("File format error");
2146 }
2147
2148 void a_exportvls(char*fn) {
2149 int d,i;
2150 FILE*fp;
2151
2152 fp=q_fopen(fn,"w");
2153 if(!fp) {fserror();return;}
2154 if(fprintf(fp,"# Free light export file created by Qxw %s http://www.quinapalus.com\n",RELEASE)<0) goto ew0;
2155 for(d=0;d<nvl;d++) {
2156 if(fprintf(fp,"# Free light %d\n",d)<0) goto ew0;
2157 for(i=0;i<vls[d].l;i++) if(fprintf(fp,"%d %d\n",vls[d].x[i],vls[d].y[i])<0) goto ew0;
2158 if(fprintf(fp,"\n")<0) goto ew0;
2159 }
2160 if(fprintf(fp,"# END\n")<0) goto ew0;
2161 if(ferror(fp)) goto ew0;
2162 if(fclose(fp)) goto ew0;
2163 return; // saved successfully
2164 ew0:
2165 fserror();
2166 fclose(fp);
2167 }
2168
2169
2170 char*titlebyauthor(void) {static char t[SLEN*2+100];
2171 if(gtitle[0]) strcpy(t,gtitle);
2172 else if(filenamebase[0]) strcpy(t,filenamebase);
2173 else strcpy(t,"(untitled)");
2174 if(gauthor[0]) strcat(t," by "),strcat(t,gauthor);
2175 return t;
2176 }
2177
2178 // MAIN
2179
2180 #ifdef _WIN32
2181 extern wchar_t* optarg;
2182 #else
2183 extern char* optarg;
2184 #endif
2185 extern int optind,opterr,optopt;
2186
2187 int main(int argc,char*argv[]) {
2188 int i,j,nd;
2189 char alphabet[SLEN+1]="";
2190 int deckmode=0;
2191 int rc=0; // return code
2192 unsigned int rseed;
2193
2194 rseed=(unsigned int)time(0);
2195 fseed=0;
2196 log2lut[0]=log2lut[1]=0; for(i=2;i<65536;i+=2) { log2lut[i]=log2lut[i/2]+1; log2lut[i+1]=0; }
2197 resetstate();
2198 for(i=0;i<MAXNDICTS;i++) dfnames[i][0]='\0';
2199 for(i=0;i<MAXNDICTS;i++) strcpy(dsfilters[i],"");
2200 for(i=0;i<MAXNDICTS;i++) strcpy(dafilters[i],"");
2201 freedicts();
2202
2203 nd=0;
2204 i=0;
2205 #ifdef _WIN32
2206 int wArgc;
2207 LPWSTR* wArgv = CommandLineToArgvW(GetCommandLineW(), &wArgc);
2208 for (;;) switch (getoptw(wArgc, wArgv, L"a:bd:?D:R:F:")) {
2209 case -1: goto ew0;
2210 case L'a':
2211 if (wcslen(optarg) < SLEN) WideCharToMultiByte(CP_UTF8, 0, optarg, -1, alphabet, SLEN, NULL, NULL);
2212 break;
2213 case L'b':deckmode = 1; break;
2214 case L'd':
2215 if (wcslen(optarg) < SLEN && nd < MAXNDICTS) WideCharToMultiByte(CP_UTF8, 0, optarg, -1, dfnames[nd++], SLEN, NULL, NULL);
2216 break;
2217 case L'D':debug = wcstol(optarg, 0, 0) | 0x80000000; break;
2218 case L'R':rseed = (unsigned int)wcstol(optarg, 0, 0); break;
2219 case L'F':fseed = (unsigned int)wcstol(optarg, 0, 0); break;
2220 case L'?':
2221 default:i = 1; break;
2222 }
2223 #else
2224 for (;;) switch (getopt(argc, argv, "a:bd:?D:R:F:")) {
2225 case -1: goto ew0;
2226 case 'a':
2227 if (strlen(optarg) < SLEN) strcpy(alphabet, optarg);
2228 break;
2229 case 'b':deckmode = 1; break;
2230 case 'd':
2231 if (strlen(optarg) < SLEN && nd < MAXNDICTS) strcpy(dfnames[nd++], optarg);
2232 break;
2233 case 'D':debug = strtol(optarg, 0, 0) | 0x80000000; break;
2234 case 'R':rseed = (unsigned int)strtol(optarg, 0, 0); break;
2235 case 'F':fseed = (unsigned int)strtol(optarg, 0, 0); break;
2236 case '?':
2237 default:i = 1; break;
2238 }
2239 #endif
2240 ew0:
2241 if(i) {
2242 printf("\nThis is Qxw, release %s.\n\n"
2243 "Copyright 2011-2020 Mark Owen; Windows port by Peter Flippant\n"
2244 "\n"
2245 "This program is free software; you can redistribute it and/or modify\n"
2246 "it under the terms of version 2 of the GNU General Public License as\n"
2247 "published by the Free Software Foundation.\n"
2248 "\n"
2249 "This program is distributed in the hope that it will be useful,\n"
2250 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
2251 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
2252 "GNU General Public License for more details.\n"
2253 "\n"
2254 "You should have received a copy of the GNU General Public License along\n"
2255 "with this program; if not, write to the Free Software Foundation, Inc.,\n"
2256 "51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\n"
2257 "\n"
2258 "For more information visit http://www.quinapalus.com or\n"
2259 "e-mail [email protected]\n\n",RELEASE);
2260 printf("Usage: %s [-a <initial alphabet code>] [-d <dictionary_file>]* [<qxw_file>]\n",argv[0]);
2261 printf(" OR: %s -b [-a <initial alphabet code>] [-d <dictionary_file>]* <qxw_deck>\n",argv[0]);
2262 printf("\n"
2263 "-b enables batch mode: GUI is disabled and a Qxw deck is read from the\n"
2264 " specified file\n\n");
2265 printf("Available alphabets and corresponding names and codes:\n");
2266 for(i=0;i<NALPHAINIT;i++) {
2267 printf("%30s: ",alphaname[i][0]);
2268 for(j=1;alphaname[i][j][0];j++) {
2269 if(j>1) printf(", ");
2270 printf("%s",alphaname[i][j]);
2271 }
2272 printf("\n");
2273 }
2274 return 0;
2275 }
2276
2277 #ifdef _WIN32
2278 if (optind < wArgc && wcslen(wArgv[optind]) < SLEN) WideCharToMultiByte(CP_UTF8, 0, wArgv[optind], -1, filename, SLEN, NULL, NULL);
2279 else strcpy(filename, "");
2280 #else
2281 if (optind < argc && strlen(argv[optind]) < SLEN) strcpy(filename, argv[optind]);
2282 else strcpy(filename, "");
2283 #endif
2284 if(deckmode) usegui=0;
2285
2286 if(debug) {
2287 printf("Qxw release %s\n",RELEASE);
2288 printf("debug=0x%08x\n",debug);
2289 printf("rseed=0x%08x (set this using -R)\n",rseed);
2290 printf("glib version %d.%d.%d\n",glib_major_version,glib_minor_version,glib_micro_version);
2291 printf("gtk version %d.%d.%d\n",gtk_major_version,gtk_minor_version,gtk_micro_version);
2292 draw_printversions();
2293 }
2294 srand(rseed);
2295 g_thread_init(0);
2296 gdk_threads_init();
2297 gdk_threads_enter();
2298 filler_init();
2299 if(usegui) {
2300 gtk_init(&argc,&argv);
2301 startgtk();
2302 }
2303 a_filenew(0); // reset grid
2304 loadprefdefaults();
2305 if(usegui) loadprefs(); // load preferences file (silently failing to defaults)
2306 draw_init();
2307 if(alphabet[0]==0) { // no alphabet specified on command line
2308 initalphamap(alphainitdata[startup_al]); // so use preferences value
2309 }
2310 else if(initalphamapbycode(alphabet)) {
2311 rc=16;
2312 reperr("Unrecognised alphabet");
2313 if(deckmode) goto ew1;
2314 }
2315
2316 if(deckmode) {
2317 rc=loaddeck(nd>0);
2318 if(rc==0) {
2319 if(filler_start(1)) {
2320 fprintf(stderr,"Failed to initialise filler\n");
2321 rc=16;
2322 } else {
2323 filler_wait();
2324 rc=dumpdeck();
2325 }
2326 }
2327 } else { // GUI mode
2328 if(filename[0]) { // we have a filename from the command line
2329 a_load();
2330 } else { // otherwise attempt to load dictionaries specified on command line
2331 if(nd) loaddicts(0);
2332 else // or, if not, try the preferences defaults and then final fallbacks
2333 if(loaddefdicts()) repwarn("No dictionaries loaded");
2334 strcpy(filenamebase,"");
2335 }
2336 syncgui();
2337 compute(0);
2338 if(debug==0&&!strcmp(RELEASE+strlen(RELEASE)-4,"beta")) {
2339 while(gtk_events_pending()) gtk_main_iteration_do(0);
2340 reperr(" This is a beta release of Qxw. \n Please do not distribute. ");
2341 }
2342 gtk_main();
2343 filler_stop();
2344 }
2345 ew1:
2346 draw_finit();
2347 if(usegui) stopgtk();
2348 filler_finit();
2349 gdk_threads_leave();
2350 freedicts();
2351 FREEX(llist);
2352 freewords();
2353 FREEX(entries);
2354 return rc;
2355 }
2356
2357
2358
2359
2360 // INTERFACE TO FILLER
2361
2362 // (re-)start the filler when an edit has been made
2363 // Return non-zero if cannot start filler
2364 int compute(int mode) {
2365 filler_stop(); // stop if already running
2366 setposslabel("");
2367 if(bldstructs()) return 1; // failed?
2368 if(filler_start(mode)) return 1;
2369 if(ifamode>0) setposslabel(" Working...");
2370 return 0;
2371 }
2372
2373 // get all word lists up-to-date prior to exporting answers
2374 // return 1 if something goes wrong and word lists are not valid
2375 int preexport(void) {
2376 DEB_EX printf("preexport()\n");
2377 filler_stop(); // stop if already running
2378 if(bldstructs()) return 1; // failed?
2379 if(filler_start(3)) return 1; // need to run as far as init+settle done
2380 filler_wait();
2381 return 0;
2382 }
2383
2384 void postexport(void) {
2385 DEB_EX printf("postexport()\n");
2386 filler_stop();
2387 compute(0); // restore everything
2388 }
2389
2390 // comparison function for sorting feasible word list by score
2391 static int cmpscores(const void*p,const void*q) {double f,g;
2392 if(lts[*(int*)p].ans<0) return 0;
2393 if(lts[*(int*)q].ans<0) return 0;
2394 f=ansp[lts[*(int*)p].ans]->score; // negative ans values should not occur here
2395 g=ansp[lts[*(int*)q].ans]->score;
2396 if(f<g) return 1;
2397 if(f>g) return -1;
2398 return (char*)p-(char*)q; // stabilise sort
2399 }
2400
2401 // called by filler when a list of feasible words through the cursor has been found
2402 void mkfeas(void) {int l; int*p; struct word*w=0;
2403 llistp=NULL;llistn=0; // default answer: no list
2404 if(curword==-1||words[curword].flist==0||ifamode==0) {llistp=NULL;llistn=0;return;} // no list
2405 w=words+curword;
2406 p=w->flist;
2407 l=w->flistlen;
2408 FREEX(llist);
2409 llist=(int*)malloc(l*sizeof(int)); // space for feasible word list
2410 if(llist==NULL) return;
2411 memcpy(llist,p,l*sizeof(int));
2412 llistp=llist;
2413 llistn=l;
2414 llistwlen=w->wlen;
2415 llistdm=w->lp->dmask; // copy list across
2416 llistem=w->lp->emask;
2417 DEB_FL printf("llist=%p p=%p l=%d dm=%08x llistwlen=%d llistdm=%08x\n",(void*)llist,(void*)p,l,w->lp->dmask,llistwlen,llistdm);
2418 qsort(llistp,llistn,sizeof(int),&cmpscores);
2419 // DEB_FL printf("mkfeas: %d matches; dm=%08x\n",llistn,llistdm);
2420 }
2421
2422 // provide progress info to display
2423 void updategrid(void) {int i;
2424 for(i=0;i<ne;i++) entries[i].flbmh=entries[i].flbm; // make back-up copy of hints
2425 if(usegui) refreshhin();
2426 }
2427
2428
2429 #ifdef _WIN32 // For a Win32 app the entry point is WinMain
2430 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
2431 return main (__argc, __argv);
2432 }
2433 #endif