Back to index

wims  3.65+svn20090927
Classes | Defines | Functions | Variables
ccsum.c File Reference
#include "../wimsdef.h"
#include "../includes.h"

Go to the source code of this file.

Classes

struct  cls

Defines

#define MAX_SESSIONS   (128*1024)
#define MAX_CLASSES   8192
#define MIN_CONNECT   2
#define MIN_ADD   1
#define MAX_LAPSE   15

Functions

void * xmalloc (size_t n)
char * find_word_end (char *p)
char * find_word_start (char *p)
void accessfile (char *content, char *type, char *s,...)
long int filelength (char *fn,...)
void mkdirs (char *s)
void oneline (char *p)
void onefile (char *fname)
void classaccount (void)
int clscmp (const void *c1, const void *c2)
void output (void)
int main (int argc, char *argv[])

Variables

struct {
char s [4]
char u [32]
int cl
int start
int end
int cnt
ses [MAX_SESSIONS]
int sescnt
char * datestr
struct cls cls [MAX_CLASSES]
int clscnt

Class Documentation

struct cls

Definition at line 42 of file ccsum.c.

Class Members
int cl
int cnt

Define Documentation

#define MAX_CLASSES   8192

Definition at line 26 of file ccsum.c.

#define MAX_LAPSE   15

Definition at line 32 of file ccsum.c.

#define MAX_SESSIONS   (128*1024)

Definition at line 24 of file ccsum.c.

#define MIN_ADD   1

Definition at line 30 of file ccsum.c.

Definition at line 28 of file ccsum.c.


Function Documentation

void accessfile ( char *  content,
char *  type,
char *  s,
  ... 
)

Definition at line 74 of file ccsum.c.

{
    va_list vp;
    char buf[MAX_LINELEN+1];
    FILE *f;
    int l;

    va_start(vp,s);
    vsnprintf(buf,sizeof(buf),s,vp);
    va_end(vp);
    f=fopen(buf,type); if(f==NULL) {
       if(*type=='r') content[0]=0; return;
    }
    switch(*type) {
       case 'a':
       case 'w': {
           l=strlen(content); fwrite(content,1,l,f); break;
       }
       case 'r': {
           l=fread(content,1,MAX_LINELEN-1,f);
           if(l>0 && l<MAX_LINELEN) content[l]=0;
           else content[0]=0;
           break;
       }
       default: {
           content[0]=0; break;
       }
    }
    fclose(f);
}
void classaccount ( void  )

Definition at line 177 of file ccsum.c.

{
    int i,j;
    clscnt=0;
    for(i=0;i<sescnt;i++) {
       ses[i].cnt=ses[i].end-ses[i].start+MIN_ADD;
       if(ses[i].cnt<MIN_CONNECT) ses[i].cnt=MIN_CONNECT;
       for(j=0;j<clscnt && ses[i].cl!=cls[j].cl;j++);
       if(j<clscnt) cls[j].cnt+=ses[i].cnt;
       else if(clscnt<MAX_CLASSES) {
           cls[clscnt].cl=ses[i].cl;
           cls[clscnt].cnt=ses[i].cnt;
           clscnt++;
       }
    }
}

Here is the caller graph for this function:

int clscmp ( const void *  c1,
const void *  c2 
)

Definition at line 194 of file ccsum.c.

{
    struct cls *cl1, *cl2;
    cl1=(struct cls *) c1; cl2=(struct cls *) c2;
    return cl1->cl-cl2->cl;
}

Here is the caller graph for this function:

long int filelength ( char *  fn,
  ... 
)

Definition at line 106 of file ccsum.c.

{
    char buf[4096];
    va_list vp;
    struct stat st;
    int l;
    
    va_start(vp,fn);
    vsnprintf(buf,sizeof(buf),fn,vp); va_end(vp);
    l=stat(buf,&st); if(l) return -1;
    return st.st_size;
}
char* find_word_end ( char *  p)

Definition at line 58 of file ccsum.c.

{
    int i;
    for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++);
    return p;
}
char* find_word_start ( char *  p)

Definition at line 66 of file ccsum.c.

{
    int i;
    for(i=0; isspace(*p) && i<MAX_LINELEN; p++,i++);
    return p;
}
int main ( int  argc,
char *  argv[] 
)

Definition at line 229 of file ccsum.c.

{
    sescnt=0;
    if(argc<2) return 1;
    datestr=getenv("ccsum_date");
    if(datestr==NULL || strlen(datestr)!=8) return 2;
    onefile(argv[1]);
    classaccount();
    output();
    return 0;
}

Here is the call graph for this function:

void mkdirs ( char *  s)

Definition at line 120 of file ccsum.c.

{
    struct stat st;
    char *buf;
    if(stat(s,&st)==-1) {
       if(strrchr(s,'/')!=NULL) {
           buf=xmalloc(strlen(s)+1);
           strcpy(buf,s); *strrchr(buf,'/')=0;
           mkdirs(buf); free(buf);
       }
       mkdir(s,-1);
    }
}

Here is the call graph for this function:

void onefile ( char *  fname)

Definition at line 161 of file ccsum.c.

{
    FILE *f;
    long l;
    char *fbuf, *p1, *p2, *p3;
    l=filelength(fname); if(l<=0) return;
    f=fopen(fname,"r"); if(f==NULL) return;
    fbuf=xmalloc(l+16); fread(fbuf,1,l,f); fclose(f); fbuf[l]=0;
    for(p1=fbuf; *p1; p1=p2) {
       p2=strchr(p1,'\n'); if(p2==NULL) p2=p1+strlen(p1); else *p2++=0;
       p3=strchr(p1,','); if(p3==NULL) continue;
       if(strncmp(p1,datestr,8)!=0) continue;
       oneline(p1);
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void oneline ( char *  p)

Definition at line 134 of file ccsum.c.

{
    char tbuf[8], sbuf[8], ubuf[256], cbuf[64];
    char *p1, *p2;
    int i,t,cl;
    memmove(tbuf,p+9,6); tbuf[2]=tbuf[5]=0;
    t=atoi(tbuf)*60+atoi(tbuf+3);
    memmove(sbuf,p+18,4); sbuf[4]=0;
    p1=strchr(p,','); if(p1==NULL) return;
    if(!isdigit(*(p1+1))) return;
    snprintf(cbuf,sizeof(cbuf),"%s",p1+1);
    for(p2=cbuf;isdigit(*p2); p2++); *p2=0; cl=atoi(cbuf);
    *p1=0; for(p1--;p1>p && !isspace(*(p1-1)); p1--);
    snprintf(ubuf,sizeof(ubuf),"%s",p1);
    for(i=0;i<sescnt;i++) {
       if(cl==ses[i].cl && memcmp(sbuf,ses[i].s,4)==0 &&
          ses[i].end>=t-MAX_LAPSE) {
           ses[i].end=t; return;
       }
    }
    if(sescnt>=MAX_SESSIONS) return;
    memmove(ses[sescnt].s,sbuf,4); ses[sescnt].cl=cl;
    ses[sescnt].start=ses[sescnt].end=t;
    snprintf(ses[sescnt].u,sizeof(ses[sescnt].u),"%s",ubuf);
    sescnt++;
}

Here is the caller graph for this function:

void output ( void  )

Definition at line 201 of file ccsum.c.

{
    char *p, buf[1024], dbuf[1024];
    int i,t;
    p=getenv("ccsum_outdir"); if(p==NULL || *p==0) return;
    for(i=0;i<sescnt;i++) {
       snprintf(dbuf,sizeof(dbuf),"%s/%d",p,ses[i].cl);
       mkdirs(dbuf);
       snprintf(buf,sizeof(buf),"%s.%02d:%02d %d\n",
               datestr,ses[i].start/60,ses[i].start%60,ses[i].cnt);
       accessfile(buf,"a","%s/%s",dbuf,ses[i].u);
    }
    snprintf(dbuf,sizeof(dbuf),"%s/bydate/%.4s",p,datestr);
    mkdirs(dbuf);
    snprintf(dbuf+strlen(dbuf),sizeof(dbuf)-strlen(dbuf),"/%.2s",datestr+4);
    t=0;
    qsort(cls,clscnt,sizeof(cls[0]),clscmp);
    for(i=0;i<clscnt;i++) {
       snprintf(buf,sizeof(buf),"%s %d\n",datestr,cls[i].cnt);
       accessfile(buf,"a","%s/%d/.total",p,cls[i].cl);
       snprintf(buf,sizeof(buf),"%s %d %d\n",datestr+4,cls[i].cl,cls[i].cnt);
       accessfile(buf,"a","%s",dbuf);
       t+=cls[i].cnt;
    }
    snprintf(buf,sizeof(buf),"%s %d %d\n",datestr,t,(t+30)/60);
    accessfile(buf,"a","%s/done",p);
}

Here is the call graph for this function:

void* xmalloc ( size_t  n)

Definition at line 47 of file ccsum.c.

{
    void *p;
    p=malloc(n);
    if(p==NULL) {
       fprintf(stderr,"Malloc failure."); exit(1);
    }
    return p;
}

Variable Documentation

struct cls cls[MAX_CLASSES]
int clscnt

Definition at line 45 of file ccsum.c.

char* datestr

Definition at line 40 of file ccsum.c.

struct { ... } ses[MAX_SESSIONS]
int sescnt

Definition at line 38 of file ccsum.c.