Back to index

wims  3.65+svn20090927
modstat.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018        /* This is an internal program,
00019         * used to show statistics of frequentation, module by module. */
00020 
00021 #include "../wims.h"
00022 
00023 #define MAX_FLEN 102400
00024 #define MAX_LANGS   16
00025 char mbuf[MAX_LINELEN+1];
00026 char mbase[MAX_LINELEN+1];
00027 char indexfile[MAX_LINELEN+1];
00028 char modify_time[1024];
00029 char vbuf[MAX_LINELEN+1];
00030 char *tlist, *mlist, *slist; /* fields, modules, sqled modules */
00031 char *langs; /* site languages */
00032 char language[MAX_LANGS][4];
00033 
00034 int start,end,mstart,mend,modtype,reqs,sites;
00035 int languagecnt=0;
00036 int count[MAX_LANGS],tcount;
00037 
00038 void *xmalloc(size_t n)
00039 {
00040     void *p;
00041     p=malloc(n);
00042     if(p==NULL) {
00043        printf("Malloc failure.\n");
00044        exit(1);
00045     }
00046     return p;
00047 }
00048 
00049        /* Points to the end of the word */
00050 char *find_word_end(char *p)
00051 {
00052     int i;
00053     for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++);
00054     return p;
00055 }
00056 
00057        /* Strips leading spaces */
00058 char *find_word_start(char *p)
00059 {
00060     int i;
00061     for(i=0; isspace(*p) && i<MAX_LINELEN; p++,i++);
00062     return p;
00063 }
00064 
00065        /* Find first occurrence of word */
00066 char *wordchr(char *p, char *w)
00067 {
00068     char *r;
00069 
00070     for(r=strstr(p,w);r!=NULL && 
00071        ( (r>p && !isspace(*(r-1))) || (!isspace(*(r+strlen(w))) && *(r+strlen(w))!=0) );
00072        r=strstr(r+1,w));
00073     return r;
00074 }
00075 
00076        /* find a variable in a string (math expression).
00077         * Returns the pointer or NULL. */
00078 char *varchr(char *p, char *v)
00079 {
00080     char *pp; int n=strlen(v);
00081     for(pp=strstr(p,v); pp!=NULL; pp=strstr(pp+1,v)) {
00082        if((pp==p || !isalnum(*(pp-1))) &&
00083           (!isalnum(*(pp+n)) || *(pp+n)==0)) break;
00084     }
00085     return pp;
00086 }
00087 
00088        /* strip trailing spaces; return string end. */
00089 char *strip_trailing_spaces(char *p)
00090 {
00091     char *pp;
00092     if(*p==0) return p;
00093     for(pp=p+strlen(p)-1; pp>=p && isspace(*pp); *(pp--)=0);
00094     return pp;
00095 }
00096 
00097 void getlangs(void)
00098 {
00099     char *p;
00100     
00101     langs=getenv("w_wims_site_languages"); if(langs==NULL) langs="";
00102     for(p=langs;strlen(p)>=2 && languagecnt<MAX_LANGS;
00103        p=find_word_start(find_word_end(p))) {
00104        if(!isalpha(*p) || !isalpha(*(p+1))) continue;
00105        memmove(language[languagecnt],p,2);
00106        language[languagecnt][2]=0;
00107        languagecnt++;
00108     }
00109 }
00110 
00111 int onefile(char *name)
00112 {
00113     FILE *f;
00114     char *buf, *p, *pe, *p2, *p3;
00115     long int len,len2;
00116     int s,i,j,k,t,u;
00117     
00118     f=fopen(name,"r"); if(f==NULL) return 0;
00119     fseek(f,0,SEEK_END);len=ftell(f);
00120     if(len<=0 || len>MAX_FLEN) return 0;
00121     fseek(f,0,SEEK_SET);buf=xmalloc(len+1);
00122     len2=fread(buf,1,len,f);
00123     if(len2<=0 || len2>len) {
00124        free(buf); return 0;
00125     }
00126     buf[len2]=0; fclose(f); s=t=u=0;
00127     for(p=buf;p!=NULL && p<buf+len2;p=pe) {
00128        p=find_word_start(p); pe=strchr(p,'\n');
00129        if(pe!=NULL) *pe++=0;
00130        p2=find_word_end(p);
00131        if(*p2!=0) *p2++=0;
00132        p2=find_word_start(p2);
00133        p3=find_word_start(find_word_end(p2));
00134        *find_word_end(p3)=0;
00135        if(*p!=0) i=atoi(p); else i=0;
00136        if(*p2!=0) j=atoi(p2); else j=0;
00137        if(*p3!=0) k=atoi(p3); else k=0;
00138        if(t==0 && i<mstart) mstart=i;
00139        if(i>=start && i<=end) {s+=j; u+=k;}
00140        t=1;
00141     }
00142     reqs+=s; sites+=u;
00143     if(modtype==3) return u;
00144     else return s;
00145 }
00146 
00147 void onemodule(char *mod)
00148 {
00149     char ibuf[MAX_LINELEN+5];
00150     int i,k,sum;
00151     
00152     sum=reqs=sites=0;mstart=end;
00153     k=onefile(mod);sum+=k;
00154     for(i=0;i<languagecnt;i++) {
00155        snprintf(ibuf,sizeof(ibuf),"%s.%s",mod,language[i]);
00156        k=onefile(ibuf);sum+=k;
00157        count[i]=k;
00158     }
00159     if(modtype!=2) tcount=sum;
00160     else {
00161        if(sites>0) tcount=(double) (100*reqs/sites+0.5);
00162        else tcount=0;
00163     }
00164 }
00165        
00166 int main()
00167 {
00168     char mmbuf[MAX_LINELEN+1];
00169     char *p1, *p2, *pp;
00170     int i;
00171     
00172     mlist=getenv("ll");
00173     if(mlist==NULL || *mlist==0) return 1;
00174     pp=getenv("start"); if(pp==NULL || *pp==0) return 1;
00175     start=atoi(pp);
00176     pp=getenv("end"); if(pp==NULL || *pp==0) return 1;
00177     end=atoi(pp);
00178     pp=getenv("w_module_type"); 
00179     if(pp==NULL || (*pp!='2' && *pp!='3')) modtype=1; else modtype=*pp-'0';
00180     getlangs();
00181     for(p1=find_word_start(mlist);*p1!=0 && !isspace(*p1); p1=find_word_start(p2)) {
00182        p2=find_word_end(p1);
00183        if(p2-p1>MAX_LINELEN) continue;
00184        memmove(mmbuf,p1,p2-p1);mmbuf[p2-p1]=0;
00185        onemodule(mmbuf);
00186        printf("%d %d %s",tcount,mstart,mmbuf);
00187        for(i=0;i<languagecnt;i++) printf(" %d",count[i]);
00188        printf("\n");
00189     }
00190     return 0;
00191 }
00192