/* kimdabaassist.c Dirty code by Denis-Carl Robidoux */ #include #include #include #include // 1 2 3 4 5 6 7 //345678901234567890123456789012345678901234567890123456789012345678901234567890 static char *usage= "\nkimdabaassist will use the database generated by kimdaba to help you create\n" "web pages. This is a feature already included in kimdaba but with an itch:\n" "the resizing made by non-parametrable algorhytms affecting even the\n" "original size picture (which should only have been copied, dot).\n\n" "USAGE: \n" "kimdabaassist [OPTIONS] <outputpath>\n" "Needs to be run under the same directory as kimdaba's index.xml file\n\n" "OPTIONS:\n" " -persons\n" " Will display names contained in the database under section \"Persons\"\n" " -locations\n" " Will display names contained in the database under section \"Locations\"\n" " -keywords\n" " Will display names contained in the database under section \"Keywords\"\n" " -descriptions\n" " Will display the content of the descriptions.\n" " -originals\n" " Will make the originals available for the website\n" " -resize <XXXxYYY>\n" " Default size is 800x533 but you can change it with this option if you like\n\n" "EXAMPLE:\n" "kimdabaassist -resize 640x480 \"My Key\" \"My title\" ~/public_html/newdir\n"; static int showpersons=0; static int showlocations=0; static int showkeywords=0; static int showoriginals=0; static int showdescription=0; char *resizing="800x533"; struct _VAL { char *value; struct _VAL *next; }; typedef struct _VAL VAL; struct _INFO { char *keytype; VAL *val; struct _INFO *next; }; typedef struct _INFO INFO; struct _PIX { char *fname; char *description; unsigned int hit : 1; unsigned int spare : 31; INFO *info; struct _PIX *next; }; typedef struct _PIX PIX; char *keytolookfor; char *titre; char *path; int pixcount=0; FILE *ifh=NULL; PIX *thelasthit; int thelasthitnum=0; PIX *firstpix=NULL; char *ttable[]= { "\303\251","é", "\303\252","ê", "\303\210","Ê", "\303\240","à", "\303\250","è", "\303\211","É", "\303\247","ç", "\303\257","ï", NULL,NULL }; char *utf8_to_html(char *str) { static int numtrad= -1; static int *curs; static int *sizs; int possible; char *incur, *outcur; int i=0,j,dirty; char *c,*res; static char scratch[128000]; if(numtrad == -1) { numtrad=0; while(ttable[numtrad*2] != NULL) { numtrad++; } if((curs=malloc(sizeof(int)*numtrad))==NULL) { printf("mem problems\n"); exit(1); } if((sizs=malloc(sizeof(int)*numtrad))==NULL) { printf("mem problems\n"); exit(1); } memset(curs,0,sizeof(int)*numtrad); memset(sizs,0,sizeof(int)*numtrad); while(ttable[i*2]!=NULL) { sizs[i]=strlen(ttable[i*2]); i++; } } incur=str; outcur=scratch; rehit: while(incur[0]!=0) { possible=0; for(i=0;i<numtrad;i++) { if(incur[0] == ttable[i*2][curs[i]]) { curs[i]++; possible=1; if(curs[i] == sizs[i]) { strcpy(outcur,ttable[(i*2)+1]); memset(curs,0,sizeof(int)*numtrad); outcur += strlen(ttable[(i*2)+1]); incur++; goto rehit; } } else curs[i]=0; } if(!possible) { *outcur= *incur; incur++; outcur++; *outcur=0; } else { incur++; } } res=strdup(scratch); //printf("Translated: %s\n",res); return res; } char **split(char *str) { char **res; char *dup; char *c,*start; int i,count=0; int escape=0; if((dup=utf8_to_html(str))==NULL) { fprintf(stderr,"Out of mem\n"); return NULL; } while((c=strstr(dup,"\t"))!=NULL)c[0]=' '; c=dup; while(c[0]==' ')c++; if(c[0]!='<') { free(dup); return NULL; } c++; while(c[0]==' ')c++; start=c; while((c[0]!=0)&&(c[0]!='>')&&(c[0]!='/')) { if(c[0]=='\"') { c++; while((c[0]!='\"')&&(c[0]!=0)){ c++; if(c[0]=='\\')if(c[1]!=0)c += 2; } } else c++; count++; while((c[0]!=' ')&&(c[0]!='=')&&(c[0]!=0))c++; while((c[0]==' ')||(c[0]=='='))c++; } //printf("count = %d\n",count); if(count==0) { free(dup); return NULL; } if((res=malloc(sizeof (char *) * (count + 1)))==NULL) { free(dup); return NULL; } memset(res,0,(sizeof (char *) * (count + 1))); c=start; for(i=0;i<count;i++) { //printf("loop: %s\n",c); if(c[0]=='\"') { c++; res[i]=c; while((c[0]!='\"')&&(c[0]!=0)) { c++; if((c[0]=='\\')&&(c[1]!=0))c += 2; } } else { res[i]=c; while((c[0]!=0)&&(c[0]!=' ')&&(c[0]!='/')&&(c[0]!='=')) c++; } c[0]=0; c++; while(c[0]==' ')c++; } /* if(res!=NULL) */ /* { */ /* for(i=0;res[i]!=NULL;i++) */ /* { */ /* printf("(%s)",res[i]); */ /* } */ /* printf("\n"); */ /* } */ return res; } static void freeasplit(char **sp) { if(sp == NULL)return; if(sp[0]!=NULL)free(sp[0]); free(sp); } PIX *loadit(char *fname) { FILE *in; static char ligne[128000]; PIX *pix; INFO *info; VAL *val; char **s; int i; pix=NULL; firstpix=NULL; info=NULL; val=NULL; if((in=fopen(fname,"rt"))==NULL) { fprintf(stderr,"Could not open %s for reading\n",fname); return NULL; } while(fgets(ligne,128000,in)!=NULL) { //printf("Ligne: %s\n",ligne); if((s=split(ligne))!=NULL) { if(strcmp(s[0],"image")==0) { if(pix!=NULL) { if((pix->next=malloc(sizeof (PIX)))==NULL) { fprintf(stderr,"mem problems\n"); exit(1); } pix=pix->next; memset(pix,0,sizeof(PIX)); } else { if((pix=firstpix=malloc(sizeof(PIX)))==NULL) { fprintf(stderr,"mem problems\n"); exit(1); } memset(pix,0,sizeof(PIX)); } info=NULL; i=1; while(s[i]!=NULL) { //printf("%s\n",s[i]); if(strcmp(s[i],"file")==0) { i++; //printf("-->%s\n",s[i]); pix->fname=s[i]; } if(strcmp(s[i],"description")==0) { i++; //printf("-->%s\n",s[i]); pix->description=s[i]; } i++; } } if((pix != NULL)&&(strcmp(s[0],"option")==0)&&(strcmp(s[1],"name")==0)) { if (info==NULL) { if((pix->info=info=malloc(sizeof(INFO)))==NULL) { fprintf(stderr,"mem problems\n"); exit(1); } } else { if((info->next=malloc(sizeof(INFO)))==NULL) { fprintf(stderr,"mem problems\n"); exit(1); } info=info->next; } memset(info,0,sizeof(INFO)); info->keytype=s[2]; val=NULL; } if((info!=NULL) && (strcmp(s[0],"value")==0) && (strcmp(s[1],"value")==0)) { if(val==NULL) { if((info->val=val=malloc(sizeof(VAL)))==NULL) { fprintf(stderr,"mem problems\n"); exit(1); } } else { if((val->next=malloc(sizeof(VAL)))==NULL) { fprintf(stderr,"mem problems\n"); exit(1); } val=val->next; } memset(val,0,sizeof(VAL)); val->value=s[2]; if(strcmp(s[2],keytolookfor)==0) { pix->hit=1; thelasthit=pix; thelasthitnum++; } } } } fclose(in); } char commande[1024]; int countcol; void addpixtoindex(PIX *p) { static int count = 0; INFO *info; VAL *val; if((countcol % 3) == 0) { fprintf(ifh,"<tr valign=\"center\" align=\"center\">\n"); } sprintf(commande,"convert -geometry 150x150 \"%s/z%04d_%s.jpg\" \"%s/z%04d_150.jpg\"",path,pixcount,resizing,path,pixcount); system(commande); fprintf(ifh," <td style=\"width: 200px;\"><a href=\"z%04d.html\"><img src=\"z%04d_150.jpg\" alt=\"%s\"></a>",pixcount,pixcount,p->fname); if(showpersons) { info=p->info; while(info!=NULL) { if(strcmp(info->keytype,"Persons")==0) { val=info->val; while(val!=NULL) { fprintf(ifh," <br>%s\n",val->value); val=val->next; } } info=info->next; } } if(showlocations) { info=p->info; while(info!=NULL) { if(strcmp(info->keytype,"Locations")==0) { val=info->val; while(val!=NULL) { fprintf(ifh," <br>%s\n",val->value); val=val->next; } } info=info->next; } } if(showkeywords) { info=p->info; while(info!=NULL) { if(strcmp(info->keytype,"Keywords")==0) { val=info->val; while(val!=NULL) { fprintf(ifh," <br>%s\n",val->value); val=val->next; } } info=info->next; } } fprintf(ifh," </td>\n"); if((countcol % 3) == 2)fprintf(ifh,"</tr>\n"); countcol++; } void addpixpages(PIX *p) { static int count = 0; INFO *info; FILE *out; VAL *val; sprintf(commande,"%s/z%04d.html",path,pixcount); if((out=fopen(commande, "wt"))==NULL) { printf("Could not open %s for writing\n",commande); exit(1); } fprintf(out,"<html><head><title>%s\n",titre); fprintf(out,"
\n"); fprintf(out,"%s
\n",p->fname); fprintf(out,"\n"); fprintf(out,"
\n",atoi(resizing)+20); fprintf(out,"",((p==thelasthit)?0:pixcount+1)); fprintf(out,"
",pixcount,resizing); sprintf(commande,"convert -geometry %s \"%s\" \"%s/z%04d_%s.jpg\"",resizing,p->fname,path,pixcount,resizing); system(commande); if(showoriginals) { sprintf(commande,"cp -f \"%s\" \"%s/y%04d.jpg\"",p->fname,path,pixcount); system(commande); } if(showdescription) { if(p->description!=NULL) { fprintf(out," %s
\n",p->description); } else { fprintf(out,".
\n"); } } if(showpersons) { fprintf(out,"."); info=p->info; while(info!=NULL) { if(strcmp(info->keytype,"Persons")==0) { val=info->val; while(val!=NULL) { fprintf(out," %s ",val->value); val=val->next; if (val !=NULL) fprintf(out," , "); } } info=info->next; } fprintf(out,".
\n"); } if(showlocations) { fprintf(out,"."); info=p->info; while(info!=NULL) { if(strcmp(info->keytype,"Locations")==0) { val=info->val; while(val!=NULL) { fprintf(out," %s ",val->value); val=val->next; if (val !=NULL) fprintf(out," , "); } } info=info->next; } fprintf(out,".
\n"); } if(showkeywords) { fprintf(out,"."); info=p->info; while(info!=NULL) { if(strcmp(info->keytype,"Keywords")==0) { val=info->val; while(val!=NULL) { fprintf(out," %s ",val->value); val=val->next; if (val !=NULL) fprintf(out," , "); } } info=info->next; } fprintf(out,".
\n"); } fprintf(out,"
\n"); fprintf(out,"

Précédente, ",(pixcount==0)?thelasthitnum - 1:pixcount - 1); fprintf(out," Retour à l\'index, "); fprintf(out," Suivante ",(p==thelasthit)?0:pixcount+1); if(showoriginals) { fprintf(out,"

Cliquez ici pour la photo haute-résolution",pixcount,pixcount); } else { /* fprintf(out,"

Si jamais une (ou plus) des photos vous intéresse " */ /* "pour imprimer il vous suffit de m\'écrire un courriel " */ /* "me demandant de vous envoyer le fichier original de " */ /* "haute-résolution, svp mentionner le nom du fichier qui " */ /* "apparait au-dessus de la photo. Mon adresse est la suivante: " */ /* "
"); */ } fprintf(out,"

\n"); fprintf(out,"

Créé avec kimdabaassist (www.deniscarl.com)\n"); fprintf(out,"

\n"); fclose(out); } int main (int argc, char *argv[]) { int i,count; PIX *pix; INFO *info; VAL *val; if(argc<4) { printf(usage); return 0; } count=0; for(i=1;i= argc) { printf("%s\n",usage); exit(0); } resizing=strdup(argv[i]); } else { printf("Unknown option %s\n",argv[i]); printf("%s\n",usage); exit(0); } } } if (count!=3) { printf("%s\n",usage); exit(0); } loadit("index.xml"); pix=firstpix; sprintf(commande,"mkdir -p \"%s\"",path); system(commande); sprintf(commande,"rm -f \"%s/index.html~\"",path); system(commande); sprintf(commande,"mv \"%s/index.html\" \"%s/index.html~\" ",path,path); system(commande); sprintf(commande,"%s/index.html",path); if((ifh=fopen(commande,"wt"))==NULL) { printf("%s could not be created\n"); return 0; } fprintf(ifh,"%s\n",titre); fprintf(ifh,"

%s

",titre); while(pix!=NULL) { if(pix->hit) { addpixpages(pix); addpixtoindex(pix); pixcount++; } pix=pix->next; } if((countcol % 3) !=0)fprintf(ifh,"\n"); fprintf(ifh,"

"); }