// Copyright status: this file is in the public domain. #include #include #include #include "lx.h" static regex_t greg; static int greg_set = 0; static int geom_size(const char *spec, regmatch_t *rm, int min, int max, int *vp, LX_GEOMETRY *g, unsigned int bit) { long int v; if (rm->rm_so < 0) return(0); if ((rm->rm_eo - rm->rm_so == 1) && (spec[rm->rm_so] == '.')) return(0); v = strtol(spec+rm->rm_so,0,10); if ((v < min) || (v > max)) return(1); *vp = v; g->flags |= bit; return(0); } static int geom_pos(const char *spec, regmatch_t *rm, int min, int max, int *vp, LX_GEOMETRY *g, unsigned int pbit, unsigned int nbit) { long int v; if (rm->rm_so < 0) return(0); if ((rm->rm_eo - rm->rm_so == 1) && (spec[rm->rm_so] == '.')) return(0); v = strtol(spec+rm->rm_so,0,10); if ((v < min) || (v > max)) return(1.); *vp = v; g->flags |= (spec[rm->rm_so-1] == '+') ? pbit : nbit; return(0); } int lx_parse_geometry(const char *spec, LX_GEOMETRY *gp) { int e; regmatch_t rm[7]; gp->flags = 0; if (!spec || !*spec) return(0); if (! greg_set) { e = regcomp(&greg,"^(([0-9]+|\\.)x([0-9]+|\\.))?([-+](-?[0-9]+|\\.)[-+](-?[0-9]+|\\.))?$",REG_EXTENDED|REG_ICASE); if (e) abort(); greg_set = 1; } e = regexec(&greg,spec,7,&rm[0],0); switch (e) { case 0: break; case REG_NOMATCH: return(LX_GEOM_ERR_SYNTAX); break; default: abort(); break; } // 1: WxH (not useful) // 2: width // 3: height // 4: [-+]X[-+]Y (not useful) // 5: X // 6: Y if ( geom_size(spec,&rm[2],0,65535,&gp->w,gp,LX_GEOM_W) || geom_size(spec,&rm[3],0,65535,&gp->h,gp,LX_GEOM_H) || geom_pos(spec,&rm[5],-32767,32768,&gp->x,gp,LX_GEOM_PX,LX_GEOM_NX) || geom_pos(spec,&rm[6],-32767,32768,&gp->y,gp,LX_GEOM_PY,LX_GEOM_NX) ) return(LX_GEOM_ERR_RANGE); return(0); }