int main() { static stralloc sa; stralloc_copys(&sa,"foo\r\n"); assert(sa.len==5); assert(stralloc_chomp(&sa)==2); assert(stralloc_chop(&sa)=='o'); assert(stralloc_chop(&sa)=='o'); assert(stralloc_chop(&sa)=='f'); assert(stralloc_chop(&sa)==-1); return 0; }
int main() { stralloc sa; buffer b; stralloc line; stralloc_init(&sa); stralloc_init(&line); stralloc_copys(&sa,"this is a test\nline 2\n"); buffer_fromsa(&b,&sa); while (buffer_getline_sa(&b,&line)==0) { buffer_puts(buffer_1,"got line: \""); if (stralloc_chop(&line)!='\n') break; buffer_putsa(buffer_1,&line); buffer_putsflush(buffer_1,"\"\n"); stralloc_copys(&line,""); } return 0; }
static int mangleurl(stralloc* tag,const char* baseurl) { char* x; const char* y; static stralloc before,arg,after,tmp; int found; struct stat ss; found=0; if (stralloc_istag(tag,"a") || stralloc_istag(tag,"link")) found=1; else if (stralloc_istag(tag,"img") || stralloc_istag(tag,"frame")) found=2; if (!found) return 0; if (extractparam(tag,found==1?"href":"src",&before,&arg,&after)) { if (stralloc_starts(&arg,"/") || stralloc_starts(&arg,"http://") || stralloc_starts(&arg,"https://")) { canonicalize(&arg,baseurl); } else return 0; /* url was already relative */ if (stralloc_0(&arg)==0) return -1; stralloc_chop(&arg); x=arg.s+7; if (*x=='/') ++x; y=baseurl+7; if (*y=='/') ++y; /* now x is something like * "www.spiegel.de/img/0,1020,525770,00.jpg" * and baseurl is something like * "www.spiegel.de/panorama/0,1518,378421,00.html" * and we want to change x into "../img/0,1020,525770,00.jpg" */ if (stat(x,&ss)!=0) return 0; for (;;) { int i=str_chr(x,'/'); int j=str_chr(y,'/'); if (i>0 && i==j && byte_equal(x,i,y)) { x+=i+1; y+=i+1; while (*x=='/') ++x; while (*y=='/') ++y; } else break; } stralloc_zero(&tmp); for (;;) { int i=str_chr(y,'/'); if (y[i]=='/') { y+=i+1; while (*y=='/') ++y; if (stralloc_cats(&tmp,"../")==0) return -1; } else break; } { int i,needquote; for (i=needquote=0; x[i]; ++i) if (!isalnum(x[i]) && x[i]!='/' && x[i]!='_' && x[i]!='.') needquote=1; if (needquote) { if (stralloc_cats(&before,"\"")==0 || stralloc_cat(&before,&tmp)==0 || stralloc_cats(&before,x)==0 || stralloc_cats(&before,"\"")==0) return -1; } else if (stralloc_cat(&before,&tmp)==0 || stralloc_cats(&before,x)==0) return -1; } if (stralloc_cat(&before,&after)==0) return -1; if (stralloc_copy(tag,&before)==0) return -1; } return 0; }