1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
/*
* ion/ioncore/strings.c
*
* Copyright (c) Tuomo Valkonen 1999-2007.
*
* See the included file LICENSE for details.
*/
#include <libtu/output.h>
#include <libtu/misc.h>
#include <string.h>
#include <regex.h>
#include "log.h"
#include "common.h"
#include "global.h"
#include "strings.h"
/*{{{ String scanning */
wchar_t str_wchar_at(char *p, int max)
{
wchar_t wc;
if(mbtowc(&wc, p, max)>0)
return wc;
return 0;
}
char *str_stripws(char *p)
{
mbstate_t ps;
wchar_t wc;
int first=-1, pos=0;
int n=strlen(p);
int ret;
memset(&ps, 0, sizeof(ps));
while(1){
ret=mbrtowc(&wc, p+pos, n-pos, &ps);
if(ret<=0)
break;
if(!iswspace(wc))
break;
pos+=ret;
}
if(pos!=0)
memmove(p, p+pos, n-pos+1);
if(ret<=0)
return p;
pos=ret;
while(1){
ret=mbrtowc(&wc, p+pos, n-pos, &ps);
if(ret<=0)
break;
if(iswspace(wc)){
if(first==-1)
first=pos;
}else{
first=-1;
}
pos+=ret;
}
if(first!=-1)
p[first]='\0';
return p;
}
int str_prevoff(const char *p, int pos)
{
if(ioncore_g.enc_sb)
return (pos>0 ? 1 : 0);
if(ioncore_g.enc_utf8){
int opos=pos;
while(pos>0){
pos--;
if((p[pos]&0xC0)!=0x80)
break;
}
return opos-pos;
}
assert(ioncore_g.use_mb);
{
/* *sigh* */
int l, prev=0;
mbstate_t ps;
memset(&ps, 0, sizeof(ps));
while(1){
l=mbrlen(p+prev, pos-prev, &ps);
if(l<0){
warn(TR("Invalid multibyte string."));
return 0;
}
if(prev+l>=pos)
return pos-prev;
prev+=l;
}
}
}
int str_nextoff(const char *p, int opos)
{
if(ioncore_g.enc_sb)
return (*(p+opos)=='\0' ? 0 : 1);
if(ioncore_g.enc_utf8){
int pos=opos;
while(p[pos]){
pos++;
if((p[pos]&0xC0)!=0x80)
break;
}
return pos-opos;
}
assert(ioncore_g.use_mb);
{
mbstate_t ps;
int l;
memset(&ps, 0, sizeof(ps));
l=mbrlen(p+opos, strlen(p+opos), &ps);
if(l<0){
warn(TR("Invalid multibyte string."));
return 0;
}
return l;
}
}
int str_len(const char *p)
{
if(ioncore_g.enc_sb)
return strlen(p);
if(ioncore_g.enc_utf8){
int len=0;
while(*p){
if(((*p)&0xC0)!=0x80)
len++;
p++;
}
return len;
}
assert(ioncore_g.use_mb);
{
mbstate_t ps;
int len=0, bytes=strlen(p), l;
memset(&ps, 0, sizeof(ps));
while(bytes>0){
l=mbrlen(p, bytes, &ps);
if(l<=0){
warn(TR("Invalid multibyte string."));
break;
}
len++;
bytes-=l;
p += l;
}
return len;
}
}
/*}}}*/
/*{{{ Title shortening */
INTRSTRUCT(SR);
DECLSTRUCT(SR){
regex_t re;
char *rule;
SR *next, *prev;
bool always;
};
static SR *shortenrules=NULL;
/*EXTL_DOC
* Add a rule describing how too long titles should be shortened to fit in tabs.
* The regular expression \var{rx} (POSIX, not Lua!) is used to match titles
* and when \var{rx} matches, \var{rule} is attempted to use as a replacement
* for title. If \var{always} is set, the rule is used even if no shortening
* is necessary.
*
* Similarly to sed's 's' command, \var{rule} may contain characters that are
* inserted in the resulting string and specials as follows:
*
* \begin{tabularx}{\linewidth}{lX}
* \tabhead{Special & Description}
* \$0 & Place the original string here. \\
* \$1 to \$9 & Insert n:th capture here (as usual,captures are surrounded
* by parentheses in the regex). \\
* \$| & Alternative shortening separator. The shortening described
* before the first this kind of separator is tried first and
* if it fails to make the string short enough, the next is
* tried, and so on. \\
* \$< & Remove characters on the left of this marker to shorten the
* string. \\
* \$> & Remove characters on the right of this marker to shorten the
* string. Only the first \$< or \$> within an alternative
* shortening is used. \\
* \end{tabularx}
*/
EXTL_EXPORT
bool ioncore_defshortening(const char *rx, const char *rule, bool always)
{
SR *si;
int ret;
#define ERRBUF_SIZE 256
static char errbuf[ERRBUF_SIZE];
if(rx==NULL || rule==NULL)
return FALSE;
si=ALLOC(SR);
if(si==NULL)
return FALSE;
ret=regcomp(&(si->re), rx, REG_EXTENDED);
if(ret!=0){
errbuf[0]='\0';
regerror(ret, &(si->re), errbuf, ERRBUF_SIZE);
warn(TR("Error compiling regular expression: %s"), errbuf);
goto fail2;
}
si->rule=scopy(rule);
si->always=always;
if(si->rule==NULL)
goto fail;
LINK_ITEM(shortenrules, si, next, prev);
return TRUE;
fail:
regfree(&(si->re));
fail2:
free(si);
return FALSE;
}
static char *shorten(GrBrush *brush, const char *str, uint maxw,
const char *rule, int nmatch, regmatch_t *pmatch)
{
char *s;
int rulelen, slen, i, j, k, ll;
int strippt=0;
int stripdir=-1;
bool more=FALSE;
/* Ensure matches are at character boundaries */
if(!ioncore_g.enc_sb){
int pos=0, len, strl;
mbstate_t ps;
memset(&ps, 0, sizeof(ps));
strl=strlen(str);
while(pos<strl){
len=mbrtowc(NULL, str+pos, strl-pos, &ps);
if(len<0){
/* Invalid multibyte string */
return scopy("???");
}
if(len==0)
break;
for(i=0; i<nmatch; i++){
if(pmatch[i].rm_so>pos && pmatch[i].rm_so<pos+len)
pmatch[i].rm_so=pos+len;
if(pmatch[i].rm_eo>pos && pmatch[i].rm_eo<pos+len)
pmatch[i].rm_eo=pos;
}
pos+=len;
}
}
/* Stupid alloc rule that wastes space */
rulelen=strlen(rule);
slen=rulelen;
for(i=0; i<nmatch; i++){
if(pmatch[i].rm_so==-1)
continue;
slen+=(pmatch[i].rm_eo-pmatch[i].rm_so);
}
s=ALLOC_N(char, slen);
if(s==NULL)
return NULL;
do{
more=FALSE;
j=0;
strippt=0;
stripdir=-1;
for(i=0; i<rulelen; i++){
if(rule[i]!='$'){
s[j++]=rule[i];
continue;
}
i++;
if(rule[i]=='|'){
rule=rule+i+1;
rulelen=rulelen-i-1;
more=TRUE;
break;
}
if(rule[i]=='$'){
s[j++]='$';
continue;
}
if(rule[i]=='<'){
strippt=j;
stripdir=-1;
continue;
}
if(rule[i]=='>'){
strippt=j;
stripdir=1;
continue;
}
if(rule[i]>='0' && rule[i]<='9'){
k=(int)(rule[i]-'0');
if(k>=nmatch)
continue;
if(pmatch[k].rm_so==-1)
continue;
ll=(pmatch[k].rm_eo-pmatch[k].rm_so);
strncpy(s+j, str+pmatch[k].rm_so, ll);
j+=ll;
}
}
slen=j;
s[slen]='\0';
i=strippt;
j=strippt;
/* shorten */
{
uint bl=grbrush_get_text_width(brush, s, i);
uint el=grbrush_get_text_width(brush, s+j, slen-j);
while(1){
/* el+bl may not be the actual length, but close enough. */
if(el+bl<=maxw){
memmove(s+i, s+j, slen-j+1);
return s;
}
if(stripdir==-1){
ll=str_prevoff(s, i);
if(ll==0)
break;
i-=ll;
bl=grbrush_get_text_width(brush, s, i);
}else{
ll=str_nextoff(s, j);
if(ll==0)
break;
j+=ll;
el=grbrush_get_text_width(brush, s+j, slen-j);
}
}
}
}while(more);
free(s);
return NULL;
}
char *grbrush_make_label(GrBrush *brush, const char *str, uint maxw)
{
size_t nmatch=10;
regmatch_t pmatch[10];
SR *rule;
int ret;
char *retstr;
bool fits=FALSE;
if(grbrush_get_text_width(brush, str, strlen(str))<=maxw)
fits=TRUE;
/*return scopy(str);*/
for(rule=shortenrules; rule!=NULL; rule=rule->next){
if(fits && !rule->always)
continue;
ret=regexec(&(rule->re), str, nmatch, pmatch, 0);
if(ret!=0)
continue;
retstr=shorten(brush, str, maxw, rule->rule, nmatch, pmatch);
goto rettest;
}
if(fits){
retstr=scopy(str);
}else{
pmatch[0].rm_so=0;
pmatch[0].rm_eo=strlen(str)-1;
retstr=shorten(brush, str, maxw, "$1$<...", 1, pmatch);
}
rettest:
if(retstr!=NULL)
return retstr;
return scopy("");
}
/*}}}*/
|