/* Qxw is a program to help construct and publish crosswords. Copyright 2011-2020 Mark Owen; Windows port by Peter Flippant http://www.quinapalus.com E-mail: qxw@quinapalus.com This file is part of Qxw. Qxw is free software: you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. Qxw is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Qxw. If not, see or write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include #include #include #include #ifdef _WIN32 #include #include #include #include "pfgetopt.h" #else #include #include #endif #include #include #include #include #include #include #include #include #include "common.h" #include "qxw.h" #include "filler.h" #include "dicts.h" #include "treatment.h" #include "gui.h" #include "draw.h" #include "deck.h" #include "alphabets.h" // GLOBAL PARAMETERS // these are saved: int width=12,height=12; // grid size 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) // 5=cylinder L-R, 6=cylinder T-B, 7=Moebius L-R, 8=Moebius T-B, 9=torus int symmr=2,symmm=0,symmd=0; // symmetry mode flags (rotation, mirror, up-and-down/left-and-right) int nvl=0; // number of virtual lights struct sprop dsp; // default square properties struct lprop dlp; // default light properties char gtitle[SLEN]=""; char gauthor[SLEN]=""; // these are not saved: int debug=0; // debug level int usegui=1; // 0=use text console; 1=use GUI unsigned int fseed; // force filler srand() argument if non-zero char filename[SLEN+50]; // result from filedia() char filenamebase[SLEN]; // base to use for constructing default filenames int havesavefn; // do we have a validated filename to save to? int curx=0,cury=0,curdir=0,curmux=0; // cursor position, direction and multiplex int curent=-1; // entry pointed to by cursor (set by bldstructs(); -1 if not set) int curword=-1; // word pointed to by cursor (set by bldstructs(); -1 if not set) int unsaved=0; // edited-since-save flag int cwperm[MAXICC]; int ndir[NGTYPE] ={2,3,3,2,2, 2,2,2,2,2,2,2,2}; // number of directions per grid type int gshape[NGTYPE]={0,1,2,3,4, 0,0,0,0,0,0,0,0}; // basic shape of grid int dhflip[NGTYPE][MAXNDIR*2]={ {2,1,0,3}, {4,3,2,1,0,5}, {3,2,1,0,5,4}, {2,1,0,3}, {2,1,0,3}, {2,1,0,3}, {2,1,0,3}, {2,1,0,3}, {2,1,0,3}, {2,1,0,3}, {2,1,0,3}, {2,1,0,3}, {2,1,0,3} }; int dvflip[NGTYPE][MAXNDIR*2]={ {0,3,2,1}, {1,0,5,4,3,2}, {0,5,4,3,2,1}, {2,1,0,3}, {2,1,0,3}, {0,3,2,1}, {0,3,2,1}, {0,3,2,1}, {0,3,2,1}, {0,3,2,1}, {0,3,2,1}, {0,3,2,1}, {0,3,2,1} }; static unsigned char log2lut[65536]; int cbits(ABM x) {ABM i; int j; for(i=1,j=0;i>16)&0xffff; if(u) return log2lut[u]+16; u=(x>>32)&0xffff; if(u) return log2lut[u]+32; u=(x>>48)&0xffff; if(u) return log2lut[u]+48; return -1; } int*llistp=0; // ptr to matching lights int llistn=0; // number of matching lights unsigned int llistdm=0; // dictionary mask applicable to matching lights int llistwlen=0; // word length (less tags if any) applicable to matching lights int llistem=0; // entry method mask applicable to matching lights int*llist=0; // buffer for light index list int ifamode=2; // interactive fill assist mode // GRID struct square gsq[MXSZ][MXSZ]; struct vl vls[NVL]; int getnumber(int x,int y) { return gsq[x][y].number; } int getflags(int x,int y) {int xr,yr; getmergerep(&xr,&yr,x,y); return gsq[xr][yr].fl; } int getbgcol(int x,int y) {int xr,yr; getmergerep(&xr,&yr,x,y); return (gsq[xr][yr].sp.spor?gsq[xr][yr].sp.bgcol:dsp.bgcol)&0xffffff; } int getfgcol(int x,int y) {int xr,yr; getmergerep(&xr,&yr,x,y); return (gsq[xr][yr].sp.spor?gsq[xr][yr].sp.fgcol:dsp.fgcol)&0xffffff; } int getmkcol(int x,int y) {int xr,yr; getmergerep(&xr,&yr,x,y); return (gsq[xr][yr].sp.spor?gsq[xr][yr].sp.mkcol:dsp.mkcol)&0xffffff; } int getfstyle(int x,int y) {int xr,yr; getmergerep(&xr,&yr,x,y); return gsq[xr][yr].sp.spor?gsq[xr][yr].sp.fstyle:dsp.fstyle; } int getdech(int x,int y) {int xr,yr; getmergerep(&xr,&yr,x,y); return gsq[xr][yr].sp.spor?gsq[xr][yr].sp.dech:dsp.dech; } int getctlen(int x,int y,int d) {int xr,yr; getmergerep(&xr,&yr,x,y); if((gsq[xr][yr].sp.spor?gsq[xr][yr].sp.dech:dsp.dech)==0) d=0; return gsq[xr][yr].ctlen[d]; } struct lprop*getvlprop(int d) { return vls[d].lp.lpor?&vls[d].lp:&dlp; } struct lprop*getlprop(int x,int y,int d) { if(d>=100) return getvlprop(d-100); return gsq[x][y].lp[d].lpor?&gsq[x][y].lp[d]:&dlp; } // returns 0 for error int getmaxmux(int x,int y,int d) { int tx[MXCL],ty[MXCL],i,l,m,u; if(!ismux(x,y,d)) return 1; m=1; l=getlight(tx,ty,x,y,d); if(l<1) return 0; for(i=0;im) m=u; } return m; } void getmk(char*s,int x,int y,int c) {int d,nd,nd2,xr,yr; char*t; strcpy(s,""); getmergerep(&xr,&yr,x,y); t=gsq[xr][yr].sp.spor?gsq[xr][yr].sp.mk[c]:dsp.mk[c]; if(!strcmp(t,"\\#")) goto ew0; // if number, put it wherever necessary if(!strcmp(t,"\\o")) { // if circle, only do in first cell of group if(x!=xr||y!=yr) return; goto ew0; } d=getmergedir(x,y); // otherwise only in extreme corners of merge group nd=ndir[gtype]; nd2=nd+nd; if(d>=0) { if(!ismerge(x,y,d )&&(c-1-d+nd2)%nd2< nd) goto ew0; // end of group d=0:1,2/d=1:2,3 if(!ismerge(x,y,d+nd)&&(c-1-d+nd2)%nd2>=nd) goto ew0; else return; } ew0: strcpy(s,t); } // move backwards one cell in direction d void stepback(int*x,int*y,int d) { // printf("stepback(%d,%d,%d)\n",*x,*y,d); if(d>=ndir[gtype]) {stepforw(x,y,d-ndir[gtype]); return;} if(gtype==1) { switch(d) { case 0: if(((*x)&1)==1) (*y)++; (*x)--; break; case 1: if(((*x)&1)==0) (*y)--; (*x)--; break; case 2: (*y)--;break; } return; } if(gtype==2) { switch(d) { case 2: if(((*y)&1)==1) (*x)++; (*y)--; break; case 1: if(((*y)&1)==0) (*x)--; (*y)--; break; case 0: (*x)--;break; } return; } // rectangular grid cases if(d) (*y)--; else (*x)--; if(gtype==3||gtype==4||gtype==5||gtype==7||gtype==9||gtype==10||gtype==11||gtype==12) { // loop in x direction if(*x<0) { *x+=width; if(gtype==7||gtype==10||gtype==12) *y=height-1-*y; } } if(gtype==6||gtype==8||gtype==9||gtype==10||gtype==11||gtype==12) { // loop in y direction if(*y<0) { *y+=height; if(gtype==8||gtype==11||gtype==12) *x=width-1-*x; } } // printf("=(%d,%d)\n",*x,*y); } // move forwards one cell in direction d void stepforw(int*x,int*y,int d) { // printf("stepforw(%d,%d,%d)\n",*x,*y,d); if(d>=ndir[gtype]) {stepback(x,y,d-ndir[gtype]); return;} if(gtype==1) { switch(d) { case 0: (*x)++; if(((*x)&1)==1) (*y)--; break; case 1: (*x)++; if(((*x)&1)==0) (*y)++; break; case 2: (*y)++;break; } return; } if(gtype==2) { switch(d) { case 2: (*y)++; if(((*y)&1)==1) (*x)--; break; case 1: (*y)++; if(((*y)&1)==0) (*x)++; break; case 0: (*x)++;break; } return; } // rectangular grid cases if(d) (*y)++; else (*x)++; if(gtype==3||gtype==4||gtype==5||gtype==7||gtype==9||gtype==10||gtype==11||gtype==12) { // loop in x direction if(*x>=width) { *x-=width; if(gtype==7||gtype==10||gtype==12) *y=height-1-*y; } } if(gtype==6||gtype==8||gtype==9||gtype==10||gtype==11||gtype==12) { // loop in y direction if(*y>=height) { *y-=height; if(gtype==8||gtype==11||gtype==12) *x=width-1-*x; } } // printf("=(%d,%d)\n",*x,*y); } int isingrid(int x,int y) { if(x<0||y<0||x>=width||y>=height) return 0; if(gshape[gtype]==1&&ODD(width )&&ODD(x)&&y==height-1) return 0; if(gshape[gtype]==2&&ODD(height)&&ODD(y)&&x==width -1) return 0; return 1; } int isclear(int x,int y) { if(!isingrid(x,y)) return 0; if(gsq[x][y].fl&0x09) return 0; return 1; } int isbar(int x,int y,int d) {int u; if(d>=ndir[gtype]) {d-=ndir[gtype];stepback(&x,&y,d);} if(!isingrid(x,y)) return 0; u=(gsq[x][y].bars>>d)&1; // 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); stepforw(&x,&y,d); if(!isingrid(x,y)) return 0; return u; } int ismerge(int x,int y,int d) {int u; if(d>=ndir[gtype]) {d-=ndir[gtype];stepback(&x,&y,d);} if(!isingrid(x,y)) return 0; u=(gsq[x][y].merge>>d)&1; stepforw(&x,&y,d); if(!isingrid(x,y)) return 0; return u; } int sqexists(int i,int j) { // is a square within the grid (and not cut out)? if(!isingrid(i,j)) return 0; return !(gsq[i][j].fl&8); } // is a step backwards clear? (not obstructed by a bar, block, cutout or edge of grid) int clearbefore(int x,int y,int d) {int tx,ty; tx=x;ty=y;stepback(&tx,&ty,d); if(!isingrid(tx,ty)) return 0; if(!isclear(tx,ty)) return 0; if(isbar(tx,ty,d)) return 0; return 1; } // is a step forwards clear? int clearafter(int x,int y,int d) {int tx,ty; tx=x;ty=y;stepforw(&tx,&ty,d); if(!isingrid(tx,ty)) return 0; if(!isclear(tx,ty)) return 0; if(isbar(x,y,d)) return 0; return 1; } // move forwards one (merged) cell in direction d static void stepforwm(int*x,int*y,int d) {int x0,y0; x0=*x;y0=*y; while(ismerge(*x,*y,d)) {stepforw(x,y,d);if(*x==x0&&*y==y0) return;} // can loop e.g. in circular grids stepforw(x,y,d); } // move backwards one (merged) cell in direction d static void stepbackm(int*x,int*y,int d) {int x0,y0,od; x0=*x;y0=*y; od=(d+ndir[gtype])%(ndir[gtype]*2); // opposite direction stepback(x,y,d); if(*x==x0&&*y==y0) return; while(ismerge(*x,*y,od)) {stepback(x,y,d);if(*x==x0&&*y==y0) return;} } // is a step forwards clear? static int clearafterm(int x,int y,int d) {int tx,ty; tx=x;ty=y;stepforwm(&tx,&ty,d); if(x==tx&&y==ty) return 0; if(!isingrid(tx,ty)) return 0; if(!isclear(tx,ty)) return 0; stepback(&tx,&ty,d); if(isbar(tx,ty,d)) return 0; return 1; } 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;} //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 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;} 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;} void getmergerepd(int*mx,int*my,int x,int y,int d) {int x0,y0; // find merge representative, direction d (0<=d=0); return getmergegroupd(gx,gy,x,y,d); } int isstartoflight(int x,int y,int d) { if(!isclear(x,y)) return 0; if(clearbefore(x,y,d)) return 0; if(clearafterm(x,y,d)) return 1; return 0; } // returns: // -1: looped, no light found // 0: blocked, no light found // 1: light found, start (not mergerep'ed) stored in *lx,*ly static int getstartoflight(int*lx,int*ly,int x,int y,int d) {int x0,y0; if(!isclear(x,y)) return 0; x0=x;y0=y; while(clearbefore(x,y,d)) { stepback(&x,&y,d); // to start of light if(x==x0&&y==y0) return -1; // loop found } *lx=x;*ly=y; return 1; } int ismux(int x,int y,int d) { int l,lx,ly; if(d>=100) return 0; // virtual light? l=getstartoflight(&lx,&ly,x,y,d); if(l<1) return 0; if(!isstartoflight(lx,ly,d)) return 0; // not actually a light return getlprop(lx,ly,d)->mux; } // is light selected? int issellight(int x,int y,int d) {int l,lx,ly; l=getstartoflight(&lx,&ly,x,y,d); if(l<1) return 0; if(!isstartoflight(lx,ly,d)) return 0; // not actually a light return (gsq[lx][ly].dsel>>d)&1; } // set selected flag on a light to k void sellight(int x,int y,int d,int k) {int l,lx,ly; DEB_GR printf("sellight(%d,%d,%d,%d)\n",x,y,d,k); l=getstartoflight(&lx,&ly,x,y,d); if(l<1) return; if(!isstartoflight(lx,ly,d)) return; // not actually a light gsq[lx][ly].dsel=(gsq[lx][ly].dsel&(~(1<=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 // if d>=100 returns data for VL #d-100 // result is independent of multiplex property of light int getlightd(int*lx,int*ly,int x,int y,int d) {int i,j; if(d>=100) { d-=100; for(i=0,j=0;i=100 returns data for VL #d-100 // errors as for getlightd() // result is independent of multiplex property of light int getlight(int*lx,int*ly,int x,int y,int d) {int i,l; l=getlightd(lx,ly,x,y,d); for(i=0;i=100 returns data for VL #d-100 // for non-mux light mx=0; for multiplexed light mx=desired multiplex number // returns -2 if light overflows plus errors as for getlightd() // lp: bitmap ptrs (<=MXLE) // lx: mergerep square x (<=MXLE) // ly: mergerep square y (<=MXLE) // ls: index of contributing string (<=MXLE) // lo: offset in contributing string (<=MXLE) // le: entry number (<=MXLE) int getlightdat(ABM**lp,int*lx,int*ly,int*ls,int*lo,struct entry**le,int x0,int y0,int d,int mx) { int c,i,j,k,k0,k1,l,m,tx[MXCL],ty[MXCL],x,y; l=getlight(tx,ty,x0,y0,d); if(l<1) return l; if(!ismux(x0,y0,d)) mx=-1; for(i=0,m=0;i=ndir[gtype]) {d-=ndir[gtype];stepback(&x,&y,d);} if(!isingrid(x,y)) return; if(k) k=1; gsq[x][y].merge&=~(1<=ndir[gtype]) {d-=ndir[gtype];stepback(&x,&y,d);} if(!isingrid(x,y)) return; if(k) k=1; gsq[x][y].bars &=~(1<=width) i=(i-width)|1;} // special case for hexH grid else i=i0; if(isclear(i,j)) { if(isownmergerep(i,j)&&(gsq[i][j].sp.spor?gsq[i][j].sp.ten:dsp.ten)) gsq[i][j].goi=goi++; for(d=0;ddnran) {gsq[i][j].number=num++; break;} } } } } // add word to filler data, starting at (i,j) in direction d // if d>=100, add virtual light #d-100 // for non-mux light mx=0; for multiplexed light mx=desired multiplex number // return: // 0: OK // 1: too long // 2: ignored as dmask is zero static int addwordfd(int i,int j,int d,int mx) { int k,l,lx[MXLE],ly[MXLE]; struct entry*le[MXLE]; ABM*lcp[MXLE]; struct lprop*lp; lp=getlprop(i,j,d); if(lp->dmask==0) return 2; // skip if dictionary mask empty l=getlightdat(lcp,lx,ly,0,0,le,i,j,d,mx); if(l<1) return 1; // too long: ignore DEB_BS printf("new word %d: l=%d mx=%d\n",nw,l,mx); words[nw].gx0=i; words[nw].gy0=j; if(d<100) { gsq[i][j].w[d][mx]=words+nw; words[nw].ldir=d; } else { vls[d-100].w=words+nw; words[nw].ldir=d; } words[nw].lp=lp; for(k=0;kten&&treatmode>0) ntw++; return 0; } void freewords() { int i; if(words) for(i=0;iten) { if(treatorder[i]>0) entries[ne].flbm=treatcstr[i][l]; else entries[ne].flbm=ABM_ALL; entries[ne].sel=1; // make sure we fill even if only in "fill selection" mode entries[ne].checking=2; if(l==MXFL) st_tmtlf=1; // too many treated lights for filler to use discretion if(l0) nw++; // don't create an empty word, and don't create one for first come first served entry } } } // build structures for solver and compile statistics // 0: OK // 1: too complex // Ensure filler is stopped before calling this (reallocates e.g. words[]) static int bldstructs(void) { int cnw,cne,d,i,j,k,l,m,nd,v; int ml,mm,curk,curl; unsigned char unch[MXLE]; ABM c; curent=-1; curword=-1; initstructs(); cnw=0; // count words for(d=0;d=100) curk=0; // cursor "points" at first character in case of VL for(k=0;kchecking++; st_ce=0; for(i=0;i1) st_ce++; // number of checked squares st_sc=0;st_2u=0;st_3u=0; for(i=0;ichecking<=1); if(!unch[j]) k++; // count checked cells in word if(j>0&&unch[j]&&unch[j-1] ) v|=1; // double unch? if(j>1&&unch[j]&&unch[j-1]&&unch[j-2]) v|=2; // triple+ unch? } if(v&1) st_2u++; // double+ unch? if(v&2) st_3u++; // triple+ unch? // k is now number of checked cells (out of l total) if( k *100l*maxcheck) st_locc[l]++,v|=8; st_lsc[l]+=k; if(kst_lmxc[l]) st_lmxc[l]=k; st_sc+=k; if(words[i].ldir=0;i--) { if((i*symmr)%6==0) { rot6(&x0,&y0,x,y,i); x0+=mx;y0+=my; assert(x0%6==0); x0/=6; y0-=(x0&1)*2; assert(y0%4==0); y0/=4; symmdo1(f,k,x0,y0,(d+i)%6); } } break; case 2: getcrot(&mx,&my); x=x*4+(y&1)*2-mx;y=y*6-my; for(i=5;i>=0;i--) { if((i*symmr)%6==0) { rot6(&y0,&x0,y,x,i); x0+=mx;y0+=my; assert(y0%6==0); y0/=6; x0-=(y0&1)*2; assert(x0%4==0); x0/=4; symmdo1(f,k,x0,y0,(d-i+6)%6); } } break; case 3: case 4: if(symmr==0) break; // assertion for(i=symmr-1;i>=0;i--) symmdo1(f,k,(x+i*width/symmr)%width,y,d); break; } } // basic grid editing commands (candidates for f() in symmdo above) void a_editblock (int k,int x,int y,int d) {int l,gx[MXCL],gy[MXCL]; l=getmergegroup(gx,gy,x,y); gsq[x][y].fl=(gsq[x][y].fl&0x06)|1; demerge(x,y); refreshsqlist(l,gx,gy); } void a_editempty (int k,int x,int y,int d) { gsq[x][y].fl= gsq[x][y].fl&0x16; refreshsqmg(x,y); } void a_editcutout(int k,int x,int y,int d) {int l,gx[MXCL],gy[MXCL]; l=getmergegroup(gx,gy,x,y); gsq[x][y].fl=(gsq[x][y].fl&0x06)|8; demerge(x,y); refreshsqlist(l,gx,gy); } // set bar state in direction d to k void a_editbar(int k,int x,int y,int d) {int tx,ty; if(!isingrid(x,y)) return; tx=x;ty=y; stepforw(&tx,&ty,d); // printf("<%d,%d %d,%d %d>\n",x,y,tx,ty,d); if(!isingrid(tx,ty)) return; setbars(x,y,d,k); refreshsqmg(x,y); refreshsqmg(tx,ty); donumbers(); } // set merge state in direction d to k, deal with consequences 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]; if(!isclear(x,y)) return; tx=x;ty=y; stepforw(&tx,&ty,d); if(!isclear(tx,ty)) return; l=getmergegroup(gx,gy,x,y); tl=getmergegroup(tgx,tgy,tx,ty); if(k) for(i=0;ipw_dir)>SLEN-20) return 0; strcpy(s,p->pw_dir); strcat(s,"/.qxw"); if(mode==0) mkdir(s,0777); strcat(s,"/preferences"); return g_fopen(s,mode?"r":"w"); #endif } // read preferences from file // fail silently static void loadprefs() { FILE*fp; char s[SLEN],*q; int i,u; fp=openprefs(1); if(!fp) return; while (fgets(s, SLEN, fp)) { q=strchr(s,' '); if(!q) continue; *q++=0; i=strlen(q); while(i>0&&iscntrl((unsigned char)q[i-1])) q[--i]=0; for(i=0;iprefmaxv[i]) u=prefmaxv[i]; prefdata[i]=u; break; case 1: strcpy(prefstring[i],q); break; default: break; } } } fclose(fp); } // write preferences to file // fail silently void saveprefs(void) { FILE*fp; int i; fp=openprefs(0); if(!fp) return; for(i=0;i=ndir[gtype]) curdir=0; if(curx>=width) curx=width -1; if(cury>=height) cury=height-1; } static char*defaultmk(int k) {return k?"":"\\#";} // default mark is just number in NW corner // convert ABM to UTF-8 choice string of the form [a-eghq-z0-39], using inverting notation if inv==1 and // including "-" if dash==1 // return number of characters in UTF-8 string static int abmtocs(char*s,ABM b,int inv,int dash) { int c0,c1,w=0; *s=0; strcat(s,"["),w++; if(inv) strcat(s,"^"),w++,b^=dash?ABM_ALL:ABM_NRM; b&=abm_use|ABM_DASH; for(c0=1;;) { for(;c01&&!iccseq[c1-1]) break; } strcat(s,icctoutf8[c0]),w++; if(c1-c0>1) { if(c1-c0>2) strcat(s,"-"),w++; strcat(s,icctoutf8[c1-1]),w++; } c0=c1; } if(dash&&(b&ABM_DASH)) strcat(s,"-"),w++; strcat(s,"]"),w++; return w; } // convert ABM to UTF-8 string of the form "s" or "[aeiou]" or whatever // MAXICC*16+4 is a very safe upper bound on the resulting length void abmtostr(char*s,ABM b,int dash) { int w0,w1; char s0[MAXICC*16+4],s1[MAXICC*16+4]; b&=dash?ABM_ALL:ABM_NRM; if((b&(abm_use|ABM_DASH))==(abm_use|ABM_DASH)) {strcpy(s,"?"); return;} if((b& abm_use )== abm_use ) {strcpy(s,"."); return;} if(b==abm_vow) {strcpy(s,"@"); return;} if(b==abm_con) {strcpy(s,"#"); return;} if(onebit(b)) {strcpy(s,icctoutf8[abmtoicc(b)]); return;} w0=abmtocs(s0,b,0,dash); w1=abmtocs(s1,b,1,dash); // printf("abmtostr: %016llx %s %s\n",b,s0,s1); if(w0<=w1) strcpy(s,s0); // choose shorter of the two representations else strcpy(s,s1); } // convert sequence of ABMs to UTF-8 string in compact format suitable for display void abmstodispstr(char*s,ABM*b,int l) {int c,i; s[0]=0; for(i=0;i%s<",s0); // for(i=0;t[i];i++) printf(" %08x",t[i]); // printf("\n"); for(i=0,s=t;*s&&ic0) c1=c; else c0=c; // found a range: sort bounds s+=2; } } for(c=c0;c<=c1;c++) b|=ICCTOABM(c); s++; } if(*s==']') s++; if(f) b^=dash?ABM_ALL:ABM_NRM; // negation flag } else if(c>0) b=ICCTOABM(c),s++; else {s++; continue;} // skip unrecognised characters (includes BOM) b&=dash?ABM_ALL:ABM_NRM; p[i++]=b; } return i; } static void resetsp(struct sprop*p) {int k; p->bgcol=0xffffff; p->fgcol=0x000000; p->mkcol=0x000000; p->fstyle=0; p->dech=0; p->ten=0; // for(k=0;kmk[k],"%c",k+'a'); // for testing for(k=0;kmk[k],defaultmk(k)); p->spor=0; } void resetlp(struct lprop*p) { p->dmask=1; p->emask=EM_FWD; p->ten=0; p->dnran=0; p->mux=0; p->lpor=0; } // generate "codeword" permutation cwperm mapping ICC to ICC void initcodeword() { int i,j,k,p[MAXICC],u0,u1; for(i=1,j=0;i=0&&iccusedtogroup[k]==0) { // skip non-letters cwperm[i]=j+1; // count from 1 p[j]=i; j++; } } // here there are j permutable letters, indices in p[] if(j>1) // something worth permuting? for(i=0;ibgcol&=0xffffff; sp->fgcol&=0xffffff; sp->mkcol&=0xffffff; // if(sp->fstyle<0) sp->fstyle=0; if(sp->fstyle>3) sp->fstyle=3; sp->ten=!!sp->ten; // if(sp->dech<0) sp->dech=0; if(sp->dech>2) sp->dech=2; sp->spor=!!sp->spor; } // tidy up light properties structure static void fixlp(struct lprop*lp) { lp->dmask&=(1<emask&=(1<emask==0) lp->emask=EM_FWD; lp->ten=!!lp->ten; lp->lpor=!!lp->lpor; lp->dnran=!!lp->dnran; } // FILE SAVE/LOAD FILE* q_fopen(char* filename, char* mode) { #ifdef _WIN32 wchar_t wfilename[SLEN]; wchar_t wmode[4]; MultiByteToWideChar(CP_UTF8, 0, filename, -1, wfilename, SLEN); MultiByteToWideChar(CP_UTF8, 0, mode, -1, wmode, 4); return _wfopen(wfilename, wmode); #else return g_fopen(filename, mode); #endif } // flags b23..16: grid width // flags b15..8: grid height // flags b7..0: grain void a_filenew(int flags) { char*p; int u,v,x,y; filler_stop(); resetstate(); u=(flags>>16)&0xff; v=(flags>>8)&0xff; if(u>0&&u<=MXSZ&&v>0&&vSLEN-20) strcpy(filenamebase,""); else strcat(filenamebase,"\\"); } else strcpy(filenamebase,""); #else struct passwd*pw; pw=getpwuid(getuid()); if(!pw||strlen(pw->pw_dir)>SLEN-20) strcpy(filenamebase,""); else strcpy(filenamebase,pw->pw_dir),strcat(filenamebase,"/"); #endif } else { // we have a path to start from p=strrchr(filenamebase,DIR_SEP_CHAR); if(p) strcpy(p,DIR_SEP_STR); else strcpy(filenamebase,""); } strcat(filenamebase,"untitled"); havesavefn=0; donumbers(); undo_push(); unsaved=0; } #define SAVE_VERSION 5 // read next line from file, strip terminators so compatible with MS-DOS and Unix #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';} #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 // load state from file void a_load(void) { int d,i,j,k,l,n,u,t0,t1,t2,t3,b,m,f,wf,ver; char *p,s[SLEN*4],s0[SLEN*4],*t,c; struct sprop sp; struct lprop lp; FILE*fp; filler_stop(); DEB_GR printf("filler stopped: loading\n"); setfilenamebase(filename); fp=q_fopen(filename,"r"); if(!fp) {fserror();return;} resetstate(); initalphamap(alphainitdata[ALPHABET_AZ09]); // default for legacy save files and .SYM *gtitle=0; *gauthor=0; NEXTL; wf=0; if(STRSTARTS(s,"#QXW2")) { // #QXW2 load ver=0; if(s[5]=='v'&&isdigit((unsigned char)s[6])) ver=atoi(s+6); if(ver>SAVE_VERSION) wf=1; // check if the file was saved using a newer version if(ver>=4) clearalphamap(); // ver>=4 will include alphabet information NEXTL; if(sscanf(s,"GP %d %d %d %d %d %d\n",>ype,&width,&height,&symmr,&symmm,&symmd)!=6) goto ew1; if(gtype<0||gtype>=NGTYPE|| // validate basic parameters width<1||width>MXSZ|| height<1||height>MXSZ) goto ew1; if(symmr<1||symmr>12) symmr=1; if(symmm<0||symmm>3) symmm=0; if(symmd<0||symmd>3) symmd=0; if((symmr&symmrmask())==0) symmr=1; draw_init(); NEXTL; if(strcmp(s,"TTL")) goto ew1; NEXTL; if(s[0]!='+') goto ew1; strncpy(gtitle,s+1,SLEN-1); gtitle[SLEN-1]=0; NEXTL; if(strcmp(s,"AUT")) goto ew1; NEXTL; if(s[0]!='+') goto ew1; strncpy(gauthor,s+1,SLEN-1); gauthor[SLEN-1]=0; DEB_GR printf("L0\n"); NEXTL; while(STRSTARTS(s,"ALP ")) { if(sscanf(s,"ALP %d %d %d %d\n",&i,&t0,&t1,&t2)<4) goto ew1; NEXTL; if(s[0]!='+') goto ew1; strcpy(s0,s); NEXTL; if(s[0]!='+') goto ew1; if(s0[1]) addalphamapentry(i,s0+1,s+1,!!t0,!!t1,!!t2); // add entry if non-empty NEXTL; } if(STRSTARTS(s,"GLP ")) { int ten,lpor,dnran=0,mux=0; resetlp(&dlp); if(sscanf(s,"GLP %u %u %d %d %d %d\n",&dlp.dmask,&dlp.emask,&ten,&lpor,&dnran,&mux)<4) goto ew1; dlp.ten=ten; // using %hhd above fails in Visual C dlp.lpor=0; dlp.dnran=dnran; dlp.mux=mux; fixlp(&dlp); NEXTL; } while(STRSTARTS(s,"GSP ")) { int ten,spor,fstyle=0,dech=0; resetsp(&dsp); 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; dsp.ten=ten; dsp.spor=0; dsp.fstyle=fstyle; dsp.dech=dech; fixsp(&dsp); NEXTL; } while(STRSTARTS(s,"GSPMK ")) { if(sscanf(s,"GSPMK %d\n",&k)<1) goto ew1; if(k<0||k>=MAXNMK) continue; NEXTL; if(s[0]=='+') { strncpy(dsp.mk[k],s+1,MXMK); dsp.mk[k][MXMK]='\0'; } NEXTL; } while(STRSTARTS(s,"TM ")) { t2=0; t3=0; if(sscanf(s,"TM %d %d %d %d %d\n",&j,&t0,&t1,&t2,&t3)<3) goto ew1; NEXTL; if(j==0&&s[0]=='+') { if(t0<0||t0>=NATREAT) continue; treatmode=t0,tambaw=!!t1; if(t2<0||t2>2) t2=0; treatorder[0]=t2; if(t3<0||t3>2) t3=0; treatorder[1]=t3; if(treatmode==TREAT_PLUGIN) { strncpy(tpifname,s+1,SLEN-1); tpifname[SLEN-1]=0; } NEXTL; } } while(STRSTARTS(s,"TMSG ")) { if(sscanf(s,"TMSG %d %d\n",&j,&t0)!=2) goto ew1; NEXTL; if(j==0&&s[0]=='+') { if(t0<0||t0>=NMSG) continue; strncpy(treatmsg[t0],s+1,sizeof(treatmsg[0])-1); treatmsg[t0][sizeof(treatmsg[0])-1]=0; NEXTL; } } while(STRSTARTS(s,"TCST ")) { if(sscanf(s,"TCST %d %d %s\n",&i,&j,s0)!=3) goto ew1; if(i>=0&&i=0&&j=MAXNDICTS) continue; if(s[0]=='+') { strncpy(dfnames[j],s+1,SLEN-1); dfnames[j][SLEN-1]=0; NEXTL; } } while(STRSTARTS(s,"DSF ")) { if(sscanf(s,"DSF %d\n",&j)!=1) goto ew1; NEXTL; if(j<0||j>=MAXNDICTS) continue; if(s[0]=='+') { strncpy(dsfilters[j],s+1,SLEN-1); dsfilters[j][SLEN-1]=0; NEXTL; } } while(STRSTARTS(s,"DAF ")) { if(sscanf(s,"DAF %d\n",&j)!=1) goto ew1; NEXTL; if(j<0||j>=MAXNDICTS) continue; if(s[0]=='+') { strncpy(dafilters[j],s+1,SLEN-1); dafilters[j][SLEN-1]=0; NEXTL; } } DEB_GR printf("L1: %s\n",s); while(STRSTARTS(s,"SQ ")) { c=' '; // DEB_GR printf("SQ: "); if(sscanf(s,"SQ %d %d %d %d %d %c\n",&i,&j,&b,&m,&f,&c)<5) goto ew1; // DEB_GR printf("%d,%d %d\n",i,j,b); if(i<0||i>=MXSZ||j<0||j>=MXSZ) continue; gsq[i][j].bars =b&((1<=MXSZ||j<0||j>=MXSZ) continue; sp.ten=ten; sp.spor=spor; sp.fstyle=fstyle; sp.dech=dech; fixsp(&sp); gsq[i][j].sp=sp; NEXTL; } while(STRSTARTS(s,"SQSPMK ")) { if(sscanf(s,"SQSPMK %d %d %d\n",&i,&j,&k)<3) goto ew1; if(i<0||i>=MXSZ||j<0||j>=MXSZ||k<0||k>=MAXNMK) continue; NEXTL; if(s[0]=='+') { strncpy(gsq[i][j].sp.mk[k],s+1,MXMK); gsq[i][j].sp.mk[k][MXMK]='\0'; } NEXTL; } while(STRSTARTS(s,"SQLP ")) { int ten,lpor,dnran=0,mux=0; resetlp(&lp); 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; if(i<0||i>=MXSZ||j<0||j>=MXSZ||d<0||d>=ndir[gtype]) continue; lp.ten=ten; lp.lpor=lpor; lp.dnran=dnran; lp.mux=mux; fixlp(&lp); gsq[i][j].lp[d]=lp; NEXTL; } while(STRSTARTS(s,"VL ")) { if(sscanf(s,"VL %d %d %d %d\n",&d,&n,&i,&j)!=4) goto ew1; if(i<0||i>=MXSZ||j<0||j>=MXSZ||n<0||n>=MXCL||d<0||d>=NVL) continue; vls[d].x[n]=i; vls[d].y[n]=j; if(n>=vls[d].l) vls[d].l=n+1; if(d>=nvl) nvl=d+1; NEXTL; } while(STRSTARTS(s,"VLP ")) { int ten,lpor,dnran=0,mux=0; resetlp(&lp); if(sscanf(s,"VLP %d %u %u %d %d %d %d\n",&d,&lp.dmask,&lp.emask,&ten,&lpor,&dnran,&mux)<5) goto ew1; if(d<0||d>=NVL) continue; lp.ten=ten; lp.lpor=lpor; lp.dnran=dnran; lp.mux=mux; fixlp(&lp); vls[d].lp=lp; NEXTL; } while(STRSTARTS(s,"SQCT ")) { if(sscanf(s,"SQCT %d %d %d %s\n",&i,&j,&d,s0)!=4) goto ew1; if(i<0||i>=MXSZ||j<0||j>=MXSZ||d<0||d>=MAXNDIR) continue; p=s0; l=strlen(p); if(l>1&&*p=='\"'&&p[l-1]=='\"') p[l-1]=0,p++; // strip double quote marks if any gsq[i][j].ctlen[d]=strtoabms(gsq[i][j].ctbm[d],MXCT,p,0); NEXTL; } if(ver>=4) initcodeword(); // ver>=4 may have updated alphabet information } else if(strlen(s)>3&&STRSTARTS(s+2,"ympath")) { // necessarily incomplete as we don't have documentation int chkt=0,symt=0,blkh=0,x0,x1,y0,y1,dir; NEXTL; if(sscanf(s,"[Grid %d %d]",&width,&height)!=2) goto ew1; if(width<1||width>MXSZ|| // validate basic parameters height<1||height>MXSZ) goto ew1; draw_init(); for(;;) { NEXTL; if(STRSTARTS(s,"[Light ")) break; if(STRSTARTS(s,"#Title=" )) { strncpy(gtitle ,s+7,SLEN-1); gtitle [SLEN-1]=0; continue; } if(STRSTARTS(s,"#Author=")) { strncpy(gauthor,s+8,SLEN-1); gauthor[SLEN-1]=0; continue; } if(STRSTARTS(s,"BlockHole=" )) { blkh=atoi(s+10); continue; } if(STRSTARTS(s,"CheckingType=")) { chkt=atoi(s+13); continue; } if(STRSTARTS(s,"SymmetryType=")) { symt=atoi(s+13); continue; } DEB_GR printf("Unprocessed: <%s>\n",s); } DEB_GR printf("chkt=%d symt=%d; now processing lights\n",chkt,symt); if(chkt==0) { // barred? add bars everywhere for(j=0;jwidth||y0<0||y0>height) goto ew1; if(x1<0||x1>width||y1<0||y1>height) goto ew1; if(y0==y1) { // across for(i=x0;i\n",s); } } ew4: for(j=0;j0) gsq[i-1][j ].bars&=~1; if(j>0) gsq[i ][j-1].bars&=~2; } } else { // LEGACY LOAD gtype=0; if(sscanf(s,"%d %d %d %d %d\n",&width,&height,&symmr,&symmm,&symmd)!=5) goto ew1; if(width<1||width>MXSZ|| // validate basic parameters height<1||height>MXSZ|| symmr<0||symmr>2|| symmm<0||symmm>3|| symmd<0||symmd>3) goto ew1; if (symmr==1) symmr=2; else if(symmr==2) symmr=4; else symmr=1; draw_init(); for(j=0;j31) goto ew1; gsq[i][j].fl=u&0x09; gsq[i][j].bars=(u>>1)&3; gsq[i][j].merge=0; } } for(j=0;j'Z')&&(c<'0'||c>'9')&&c!=' ') goto ew1; gsq[i][j].ctbm[0][0]=ICCTOABM(uchartoICC((int)c)); } if(fgetc(fp)!='\n') goto ew1; } } if(fclose(fp)) goto ew3; reloadtpi(); donumbers(); loaddicts(0); undo_push();unsaved=0; if(wf) repwarn("File was saved using\na newer version of Qxw.\nSome features may be lost."); havesavefn=1; bldstructs(); return; // no errors ew1:fclose(fp);syncgui();reperr("File format error");goto ew2; ew3:fserror(); ew2: a_filenew(0); initalphamap(alphainitdata[startup_al]); // use preferences value of default alphabet loaddefdicts(); bldstructs(); } // write state to file void a_save(void) { int d,i,j,k,l; char s0[MXCT*(MAXICC*16+4)]; FILE*fp; setfilenamebase(filename); fp=q_fopen(filename,"w"); if(!fp) {fserror();return;} if(fprintf(fp,"#QXW2v%d http://www.quinapalus.com\n",SAVE_VERSION)<0) goto ew0; if(fprintf(fp,"GP %d %d %d %d %d %d\n",gtype,width,height,symmr,symmm,symmd)<0) goto ew0; if(fprintf(fp,"TTL\n+%s\n",gtitle)<0) goto ew0; if(fprintf(fp,"AUT\n+%s\n",gauthor)<0) goto ew0; for(i=1;i0&&!isprint((unsigned char)s[l-1])) s[--l]='\0'; if(l==0) break; // end of this vl if(s[0]=='#') continue; // comment line? if(i>=MXCL) goto ew1; if(sscanf(s,"%d %d",tvls[d].x+i,tvls[d].y+i)<2) goto ew1; if(tvls[d].x[i]<0||tvls[d].x[i]>=MXSZ) goto ew1; if(tvls[d].y[i]<0||tvls[d].y[i]>=MXSZ) goto ew1; i++; tvls[d].l=i; } if(tvls[d].l>0) d++; } ew0: if(fclose(fp)) goto ew3; if(tvls[d].l>0) d++; memcpy(vls,tvls,d*sizeof(struct vl)); nvl=d; undo_push();unsaved=0;syncgui();compute(0); return; ew3:fserror();return; ew1:fclose(fp);syncgui();reperr("File format error"); } void a_exportvls(char*fn) { int d,i; FILE*fp; fp=q_fopen(fn,"w"); if(!fp) {fserror();return;} if(fprintf(fp,"# Free light export file created by Qxw %s http://www.quinapalus.com\n",RELEASE)<0) goto ew0; for(d=0;d] [-d ]* []\n",argv[0]); printf(" OR: %s -b [-a ] [-d ]* \n",argv[0]); printf("\n" "-b enables batch mode: GUI is disabled and a Qxw deck is read from the\n" " specified file\n\n"); printf("Available alphabets and corresponding names and codes:\n"); for(i=0;i1) printf(", "); printf("%s",alphaname[i][j]); } printf("\n"); } return 0; } #ifdef _WIN32 if (optind < wArgc && wcslen(wArgv[optind]) < SLEN) WideCharToMultiByte(CP_UTF8, 0, wArgv[optind], -1, filename, SLEN, NULL, NULL); else strcpy(filename, ""); #else if (optind < argc && strlen(argv[optind]) < SLEN) strcpy(filename, argv[optind]); else strcpy(filename, ""); #endif if(deckmode) usegui=0; if(debug) { printf("Qxw release %s\n",RELEASE); printf("debug=0x%08x\n",debug); printf("rseed=0x%08x (set this using -R)\n",rseed); printf("glib version %d.%d.%d\n",glib_major_version,glib_minor_version,glib_micro_version); printf("gtk version %d.%d.%d\n",gtk_major_version,gtk_minor_version,gtk_micro_version); draw_printversions(); } srand(rseed); g_thread_init(0); gdk_threads_init(); gdk_threads_enter(); filler_init(); if(usegui) { gtk_init(&argc,&argv); startgtk(); } a_filenew(0); // reset grid loadprefdefaults(); if(usegui) loadprefs(); // load preferences file (silently failing to defaults) draw_init(); if(alphabet[0]==0) { // no alphabet specified on command line initalphamap(alphainitdata[startup_al]); // so use preferences value } else if(initalphamapbycode(alphabet)) { rc=16; reperr("Unrecognised alphabet"); if(deckmode) goto ew1; } if(deckmode) { rc=loaddeck(nd>0); if(rc==0) { if(filler_start(1)) { fprintf(stderr,"Failed to initialise filler\n"); rc=16; } else { filler_wait(); rc=dumpdeck(); } } } else { // GUI mode if(filename[0]) { // we have a filename from the command line a_load(); } else { // otherwise attempt to load dictionaries specified on command line if(nd) loaddicts(0); else // or, if not, try the preferences defaults and then final fallbacks if(loaddefdicts()) repwarn("No dictionaries loaded"); strcpy(filenamebase,""); } syncgui(); compute(0); if(debug==0&&!strcmp(RELEASE+strlen(RELEASE)-4,"beta")) { while(gtk_events_pending()) gtk_main_iteration_do(0); reperr(" This is a beta release of Qxw. \n Please do not distribute. "); } gtk_main(); filler_stop(); } ew1: draw_finit(); if(usegui) stopgtk(); filler_finit(); gdk_threads_leave(); freedicts(); FREEX(llist); freewords(); FREEX(entries); return rc; } // INTERFACE TO FILLER // (re-)start the filler when an edit has been made // Return non-zero if cannot start filler int compute(int mode) { filler_stop(); // stop if already running setposslabel(""); if(bldstructs()) return 1; // failed? if(filler_start(mode)) return 1; if(ifamode>0) setposslabel(" Working..."); return 0; } // get all word lists up-to-date prior to exporting answers // return 1 if something goes wrong and word lists are not valid int preexport(void) { DEB_EX printf("preexport()\n"); filler_stop(); // stop if already running if(bldstructs()) return 1; // failed? if(filler_start(3)) return 1; // need to run as far as init+settle done filler_wait(); return 0; } void postexport(void) { DEB_EX printf("postexport()\n"); filler_stop(); compute(0); // restore everything } // comparison function for sorting feasible word list by score static int cmpscores(const void*p,const void*q) {double f,g; if(lts[*(int*)p].ans<0) return 0; if(lts[*(int*)q].ans<0) return 0; f=ansp[lts[*(int*)p].ans]->score; // negative ans values should not occur here g=ansp[lts[*(int*)q].ans]->score; if(fg) return -1; return (char*)p-(char*)q; // stabilise sort } // called by filler when a list of feasible words through the cursor has been found void mkfeas(void) {int l; int*p; struct word*w=0; llistp=NULL;llistn=0; // default answer: no list if(curword==-1||words[curword].flist==0||ifamode==0) {llistp=NULL;llistn=0;return;} // no list w=words+curword; p=w->flist; l=w->flistlen; FREEX(llist); llist=(int*)malloc(l*sizeof(int)); // space for feasible word list if(llist==NULL) return; memcpy(llist,p,l*sizeof(int)); llistp=llist; llistn=l; llistwlen=w->wlen; llistdm=w->lp->dmask; // copy list across llistem=w->lp->emask; 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); qsort(llistp,llistn,sizeof(int),&cmpscores); // DEB_FL printf("mkfeas: %d matches; dm=%08x\n",llistn,llistdm); } // provide progress info to display void updategrid(void) {int i; for(i=0;i