Subversion Repositories wimsdev

Rev

Rev 8147 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18.         /* This is an internal program,
  19.          * used to show statistics of frequentation, module by module. */
  20.  
  21. #include "../wims.h"
  22.  
  23. #define MAX_FLEN 102400
  24. #define MAX_LANGS   16
  25. char mbuf[MAX_LINELEN+1];
  26. char mbase[MAX_LINELEN+1];
  27. char indexfile[MAX_LINELEN+1];
  28. char modify_time[1024];
  29. char vbuf[MAX_LINELEN+1];
  30. char *tlist, *mlist, *slist; /* fields, modules, sqled modules */
  31. char *langs; /* site languages */
  32. char language[MAX_LANGS][4];
  33.  
  34. int start,end,mstart,mend,modtype,reqs,sites;
  35. int languagecnt=0;
  36. int count[MAX_LANGS],tcount;
  37.  
  38. void *xmalloc(size_t n)
  39. {
  40.     void *p;
  41.     p=malloc(n);
  42.     if(p==NULL) {
  43.         printf("Malloc failure.\n");
  44.         exit(1);
  45.     }
  46.     return p;
  47. }
  48.  
  49.         /* Points to the end of the word */
  50. char *find_word_end(char *p)
  51. {
  52.     int i;
  53.     for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++);
  54.     return p;
  55. }
  56.  
  57.         /* Strips leading spaces */
  58. char *find_word_start(char *p)
  59. {
  60.     int i;
  61.     for(i=0; isspace(*p) && i<MAX_LINELEN; p++,i++);
  62.     return p;
  63. }
  64.  
  65.         /* Find first occurrence of word */
  66. char *wordchr(char *p, char *w)
  67. {
  68.     char *r;
  69.  
  70.     for(r=strstr(p,w);r!=NULL &&
  71.         ( (r>p && !isspace(*(r-1))) || (!isspace(*(r+strlen(w))) && *(r+strlen(w))!=0) );
  72.         r=strstr(r+1,w));
  73.     return r;
  74. }
  75.  
  76.         /* find a variable in a string (math expression).
  77.          * Returns the pointer or NULL. */
  78. char *varchr(char *p, char *v)
  79. {
  80.     char *pp; int n=strlen(v);
  81.     for(pp=strstr(p,v); pp!=NULL; pp=strstr(pp+1,v)) {
  82.         if((pp==p || !isalnum(*(pp-1))) &&
  83.            (!isalnum(*(pp+n)) || *(pp+n)==0)) break;
  84.     }
  85.     return pp;
  86. }
  87.  
  88.         /* strip trailing spaces; return string end. */
  89. char *strip_trailing_spaces(char *p)
  90. {
  91.     char *pp;
  92.     if(*p==0) return p;
  93.     for(pp=p+strlen(p)-1; pp>=p && isspace(*pp); *(pp--)=0);
  94.     return pp;
  95. }
  96.  
  97. void getlangs(void)
  98. {
  99.     char *p;
  100.    
  101.     langs=getenv("w_wims_site_languages"); if(langs==NULL) langs="";
  102.     for(p=langs;strlen(p)>=2 && languagecnt<MAX_LANGS;
  103.         p=find_word_start(find_word_end(p))) {
  104.         if(!isalpha(*p) || !isalpha(*(p+1))) continue;
  105.         memmove(language[languagecnt],p,2);
  106.         language[languagecnt][2]=0;
  107.         languagecnt++;
  108.     }
  109. }
  110.  
  111. int onefile(char *name)
  112. {
  113.     FILE *f;
  114.     char *buf, *p, *pe, *p2, *p3;
  115.     long int len,len2;
  116.     int s,i,j,k,t,u;
  117.    
  118.     f=fopen(name,"r"); if(f==NULL) return 0;
  119.     fseek(f,0,SEEK_END);len=ftell(f);
  120.     if(len<=0 || len>MAX_FLEN) return 0;
  121.     fseek(f,0,SEEK_SET);buf=xmalloc(len+1);
  122.     len2=fread(buf,1,len,f);
  123.     if(len2<=0 || len2>len) {
  124.         free(buf); return 0;
  125.     }
  126.     buf[len2]=0; fclose(f); s=t=u=0;
  127.     for(p=buf;p!=NULL && p<buf+len2;p=pe) {
  128.         p=find_word_start(p); pe=strchr(p,'\n');
  129.         if(pe!=NULL) *pe++=0;
  130.         p2=find_word_end(p);
  131.         if(*p2!=0) *p2++=0;
  132.         p2=find_word_start(p2);
  133.         p3=find_word_start(find_word_end(p2));
  134.         *find_word_end(p3)=0;
  135.         if(*p!=0) i=atoi(p); else i=0;
  136.         if(*p2!=0) j=atoi(p2); else j=0;
  137.         if(*p3!=0) k=atoi(p3); else k=0;
  138.         if(t==0 && i<mstart) mstart=i;
  139.         if(i>=start && i<=end) {s+=j; u+=k;}
  140.         t=1;
  141.     }
  142.     reqs+=s; sites+=u;
  143.     if(modtype==3) return u;
  144.     else return s;
  145. }
  146.  
  147. void onemodule(char *mod)
  148. {
  149.     char ibuf[MAX_LINELEN+5];
  150.     int i,k,sum;
  151.    
  152.     sum=reqs=sites=0;mstart=end;
  153.     k=onefile(mod);sum+=k;
  154.     for(i=0;i<languagecnt;i++) {
  155.         snprintf(ibuf,sizeof(ibuf),"%s.%s",mod,language[i]);
  156.         k=onefile(ibuf);sum+=k;
  157.         count[i]=k;
  158.     }
  159.     if(modtype!=2) tcount=sum;
  160.     else {
  161.         if(sites>0) tcount=(double) (100*reqs/sites+0.5);
  162.         else tcount=0;
  163.     }
  164. }
  165.        
  166. int main()
  167. {
  168.     char mmbuf[MAX_LINELEN+1];
  169.     char *p1, *p2, *pp;
  170.     int i;
  171.    
  172.     mlist=getenv("ll");
  173.     if(mlist==NULL || *mlist==0) return 1;
  174.     pp=getenv("start"); if(pp==NULL || *pp==0) return 1;
  175.     start=atoi(pp);
  176.     pp=getenv("end"); if(pp==NULL || *pp==0) return 1;
  177.     end=atoi(pp);
  178.     pp=getenv("w_module_type");
  179.     if(pp==NULL || (*pp!='2' && *pp!='3')) modtype=1; else modtype=*pp-'0';
  180.     getlangs();
  181.     for(p1=find_word_start(mlist);*p1!=0 && !isspace(*p1); p1=find_word_start(p2)) {
  182.         p2=find_word_end(p1);
  183.         if(p2-p1>MAX_LINELEN) continue;
  184.         memmove(mmbuf,p1,p2-p1);mmbuf[p2-p1]=0;
  185.         onemodule(mmbuf);
  186.         printf("%d %d %s",tcount,mstart,mmbuf);
  187.         for(i=0;i<languagecnt;i++) printf(" %d",count[i]);
  188.         printf("\n");
  189.     }
  190.     return 0;
  191. }
  192.  
  193.