plot.c File Reference

#include "cgi.h"
#include "math.h"

Include dependency graph for plot.c:

Go to the source code of this file.

Functions

void Instructions (EZSSTREAM *htmlout, CGINameValue *Params, int pw)
int CalcCountSummary (CGINameValue *Params, char *fieldname)
void UpdateStatistics (CGINameValue *count, CGINameValue *sum, CGINameValue *record, CGINameValue *xtab, CGINameValue *xcount, CGINameValue *xsum)
int CGImain (char *argv0, CGINameValue *Params, EZSSTREAM *htmlout)


Function Documentation

int CalcCountSummary CGINameValue Params,
char *  fieldname
 

Definition at line 44 of file plot.c.

References CGIFREE, GetField(), HasToken(), CGINameValue::name, strdup(), and CGINameValue::value.

00045 {
00046      CGINameValue* start;
00047      start = GetField(Params,fieldname);
00048 
00049      /* Q1 or Q1_A */
00050      if (start)
00051       if (start->value)
00052        return *start->value ? 1 : 0;
00053 
00054      if (strchr(fieldname,'_')) /* is that specific answer set? Q1_1 or Q1=1&Q1=2 */
00055       {
00056        char* name = strdup(fieldname);
00057        char* code = strchr(name,'_');
00058        size_t l;
00059        *code=0;
00060        l = strlen(name);
00061        start = GetField(Params,name);
00062        if (!start) {CGIFREE(name); return 0; } /* no answers to this question */
00063        while (start->name) /* related field values are adjacent */
00064         {
00065          if (strncmp(start->name,name,l))
00066           break;
00067 
00068          if (start->value)
00069           if ((start->name[l] == '_' || start->name[l] == 0) && *start->value)
00070            if (HasToken(start->value,code+1,0))
00071             {CGIFREE(name); return 1;}
00072 
00073          start++;
00074         }
00075        CGIFREE(name);
00076       }
00077      else /* is any Q1 or Q1_1 set? */
00078       {
00079        start = GetField(Params,fieldname);
00080        if (!start)
00081        {
00082         size_t l = strlen(fieldname);
00083         start = Params;
00084         while (start->name)
00085         {
00086          if (!strncmp(start->name,fieldname,l))
00087           if (start->value)
00088            if ((start->name[l] == '_' || start->name[l] == 0) && *start->value)
00089             return 1;
00090 
00091          start++;
00092         }
00093        }
00094       }
00095 
00096  return 0;
00097 }

int CGImain char *  argv0,
CGINameValue Params,
EZSSTREAM htmlout
 

  • _PAGE stores the page currently being viewed
  • _PAGENEXT is a FIFO of the pages to go to. New pages are appended
  • _PAGEBACK is a LIFO for the "Previous" button. New pages are prepended

For both of these lists, the "current" value is taken from the beginning of the comma-delimited values.

Definition at line 198 of file plot.c.

References CGI_ALLOW_REPORT, CGIFREE, CopyListJoin(), DatabaseClose(), DatabaseFindNextRecord(), DatabaseOpen(), DatabaseReadRecord(), DebugShowNVP(), DeleteNVP(), ExpandLocalPath(), GetFieldValue, GetSecurityFlags(), GetSetting(), HTMLEscape(), HTMLWrite(), Instructions(), LogError(), MAXPATH, CGINameValue::name, NewNVP(), NULL, NULLSTR, ReadINIFileSection(), ReadPairedString(), RunReport(), SendCGIHeader(), SetValue(), strdup(), strdup3(), UpdateStatistics(), and CGINameValue::value.

00200 {
00201  /* end with a zero */
00202  /*char * databases[] = {"file1.asc","file2.asc",0};
00203  char * crosstabs[] = {"Q1_1=Q1 eq 1","Q1_2=Q1 eq 2","Q1_3=Q1 eq 3",0};
00204  */
00205  /* hard-code the summary file
00206  char * statistics = "plots.sum";
00207  */
00208 
00209 
00210  /* The database name, used for the .ini file */
00211  char * database = GetFieldValue(Params,"DATABASE");
00212  /* The name of the template test.plot file. */
00213  char * plot = GetFieldValue(Params,"PLOT");
00214  /* The password found in the database.ini file */
00215  char * password    = GetFieldValue(Params,"_PASSWORD");
00216  /* The query that will be applied to the database */
00217  char * query = GetFieldValue(Params,"QUERY");
00218 
00219  CGINameValue* config       = 0;//the [DATABASE] section of the .ini file
00220  CGINameValue* databases    = 0;//list of database names
00221  CGINameValue* plots        = 0;//
00222  CGINameValue* count           = 0;//list of the [COUNT] fields in the .sum file
00223  CGINameValue* sum          = 0;//list of the [SUM] fields in the .sum file
00224  CGINameValue* xtabs           = 0;//list of the [CROSSTAB] fields in the .sum file
00225  CGINameValue* xcount       = 0;//list of xcount results
00226  CGINameValue* xsum         = 0;//list of xsum results
00227  CGINameValue* total        = 0;//
00228 
00229  size_t xtabcount = 0;
00230 
00231  char fn[MAXPATH];
00232  int Security = CGI_ALLOW_REPORT;
00233 
00234  size_t i;
00235 
00236  SendCGIHeader(htmlout,NULL);
00237 
00238  if (database[strcspn(database,"/.\\*?$|'\"")] != 0)
00239   {
00240     HTMLWrite(htmlout,"Bad database name: ");
00241     HTMLWrite(htmlout,database);
00242     return 104;
00243   }
00244 
00245  if (!*database || !*plot)
00246    {
00247        Instructions(htmlout,Params,0);
00248        return 0;
00249    }
00250 
00251  databases = ReadPairedString(database,',',0);
00252 
00253  for (i=0; databases[i].name; i++)
00254  {
00255   #ifdef CGI_ALLOW
00256    Security &= GetSecurityFlags(argv0,GetFieldValue(Params,"HOST"),database,password);
00257   #else
00258    Security &= GetSecurityFlags(argv0,0,database,password);
00259   #endif
00260  }
00261 
00262  if (!(Security & CGI_ALLOW_REPORT))
00263    {
00264        Instructions(htmlout,Params,1);
00265        return 0;
00266    }
00267 
00268 
00269  databases = ReadPairedString(database,',',0);
00270  plots = ReadPairedString(plot,',',0);
00271 
00272  if (databases)
00273  {
00274   ExpandLocalPath(argv0,fn,databases[0].name,".ini");
00275   config = ReadINIFileSection(fn,"DATABASE",0);
00276 
00277  if (config == NULL)
00278    {
00279     Instructions(htmlout,Params,1);
00280 
00281     HTMLWrite(htmlout,argv0);
00282     HTMLWrite(htmlout,": A configuration file for the database \"<b>");
00283     HTMLEscape(htmlout,database);
00284     HTMLWrite(htmlout,"</b>\"was not found. Please check your spelling and server setup and try again.<P>\n");
00285 
00286     LogError("\nCould not open config file ");
00287     LogError(fn);
00288 
00289     DeleteNVP(databases);
00290     DeleteNVP(plots);
00291     return 21;
00292   }
00293  }
00294 
00295  if (config == NULL)
00296  {
00297     Instructions(htmlout,Params,1);
00298     return 21;
00299  }
00300  else
00301  {
00302   char statfile[MAXPATH];
00303   char *statistics = GetSetting(config,"SAVESTATNAME",NULLSTR);
00304   ExpandLocalPath(argv0,statfile,statistics,NULLSTR);
00305   count = ReadINIFileSection(statfile, "COUNT",0);
00306   sum = ReadINIFileSection(statfile, "SUM",0);
00307   /* clear the summary lists -- we're only loading the field names */
00308 
00309   if (count)
00310   for (i=0; count[i].name; i++)
00311    SetValue(count+i,"0");
00312 
00313   if (sum)
00314   for (i=0; sum[i].name; i++)
00315    SetValue(sum+i,"0");
00316  }
00317 
00318     xtabs = ReadINIFileSection(fn,"CROSSTAB",0);
00319    if (xtabs == NULL)
00320    {
00321     char* c = GetFieldValue(Params,"CROSSTAB");
00322     if (c)
00323       xtabs = ReadPairedString(c,';',0);
00324    }
00325 
00326     if (xtabs)
00327     {
00328         if (count)
00329         {
00330             size_t max = 0;
00331             size_t j=0;
00332             for (i=0; count[i].name; i++) max++;
00333             xcount = NewNVP(xtabcount * max);
00334             for (i=0; i<max; i++)
00335              for (j=0; j<xtabcount; j++)
00336              {
00337                 xcount[i*xtabcount + j].name = strdup3(xtabs[j].name,".",count[i].name);
00338                 xcount[i*xtabcount + j].value = strdup("0");
00339              }
00340         }
00341 
00342         if (sum)
00343         {
00344             size_t max = 0;
00345             size_t j = 0;
00346             for (i=0; sum[i].name; i++) max++;
00347             xsum = NewNVP(xtabcount * max);
00348             for (i=0; i<max; i++)
00349              for (j=0; j<xtabcount; j++)
00350              {
00351                 xsum[i*xtabcount + j].name = strdup3(xtabs[j].name,".",sum[i].name);
00352                 xsum[i*xtabcount + j].value = strdup("0");
00353              }
00354         }
00355     }
00356 
00357  for (i=0; databases[i].name; i++)
00358  {
00359   FILE * db;
00360   CGINameValue* record;
00361 
00362   ExpandLocalPath(argv0,fn,databases[i].name,NULLSTR);
00363   if (!DatabaseOpen(fn,&db,&record,0,0))
00364   {
00365    continue;
00366   }
00367 
00368   if (*query)
00369   {
00370     /* automatically skips deleted records */
00371     while (DatabaseFindNextRecord(db,record,Params,query))
00372     {
00373      UpdateStatistics(count,sum,record,xtabs,xcount,xsum);
00374     }
00375   }
00376   else
00377   {
00378     int flags=0;
00379     while (DatabaseReadRecord(db,record,&flags))
00380     {
00381      if (flags) continue; /* record marked deleted with a leading # mark */
00382      UpdateStatistics(count,sum,record,xtabs,xcount,xsum);
00383     }
00384   }
00385 
00386   DatabaseClose(db,record); /* closes the file and deletes record */
00387  }
00388 
00389  if (sum)
00390  {
00391   for (i=0;sum[i].name;i++)
00392   {
00393    char * x = strdup3(sum[i].name,".SUM","");
00394    CGIFREE(sum[i].name);
00395    sum[i].name = x;
00396   }
00397  }
00398 
00399  if (xsum)
00400  {
00401   for (i=0;xsum[i].name;i++)
00402   {
00403    char * x = strdup3(xsum[i].name,".SUM","");
00404    CGIFREE(xsum[i].name);
00405    xsum[i].name = x;
00406   }
00407  }
00408 
00409  /* Updated statistics for multiple databases. Now print the report */
00410 
00411 if (plots)
00412 {
00413    if (count) total = CopyListJoin(count,total,0);
00414    if (sum) total = CopyListJoin(sum,total,0);
00415    if (xcount) total = CopyListJoin(xcount,total,0);
00416    if (xsum) total = CopyListJoin(xsum,total,0);
00417 
00418     for (i=0;plots[i].name; i++)
00419     {
00420         FILE* report;
00421         ExpandLocalPath(argv0,fn,plot,".plot");
00422         report = fopen(fn,"rt");
00423         if (!report)
00424         {
00425             ExpandLocalPath(argv0,fn,plot,".rep");
00426            report = fopen(fn,"rt");
00427         }
00428         if (!report)
00429         {
00430             ExpandLocalPath(argv0,fn,plot,".html");
00431            report = fopen(fn,"rt");
00432         }
00433         if (report)
00434         {
00435             RunReport(argv0,htmlout,report,Params,total,0,0,0); /* set the next form in the file */
00436             fclose(report);
00437         }
00438     }
00439 }
00440 else /* print a table of the results */
00441 {  /*
00442         count   crosstab1 crosstab2 crosstab
00443 field   total   value     value     value
00444 field_1
00445 */
00446  HTMLWrite(htmlout,"<pre>");
00447  if (count)  DebugShowNVP(htmlout,count);
00448  if (sum)    DebugShowNVP(htmlout,sum);
00449  if (xcount) DebugShowNVP(htmlout,xcount);
00450  if (xsum)   DebugShowNVP(htmlout,xsum);
00451  HTMLWrite(htmlout,"</pre>");
00452 
00453 }
00454 
00455 DeleteNVP(count);
00456 DeleteNVP(sum);
00457 DeleteNVP(xcount);
00458 DeleteNVP(xsum);
00459 DeleteNVP(xtabs);
00460 DeleteNVP(databases);
00461 DeleteNVP(plots);
00462 DeleteNVP(total);
00463 return 0;
00464 }

void Instructions EZSSTREAM htmlout,
CGINameValue Params,
int  pw
 

Definition at line 21 of file plot.c.

References GetFieldValue, and HTMLWrite().

Referenced by CGImain().

00022 {
00023     HTMLWrite(htmlout,"<form method=post>\n");
00024     HTMLWrite(htmlout,"Database: <input name=DATABASE value=\"");
00025    HTMLWrite(htmlout,GetFieldValue(Params,"DATABASE"));
00026    HTMLWrite(htmlout,"\">\n");
00027     HTMLWrite(htmlout,"<br>Plot names: <input name=PLOT value=\"");
00028    HTMLWrite(htmlout,GetFieldValue(Params,"PLOT"));
00029    HTMLWrite(htmlout,"\">\n");
00030     HTMLWrite(htmlout,"<br>Password: <input name=_PASSWORD value=\"\">\n");
00031     //if (pw) HTMLWrite(htmlout,GetFieldValue(Params,"_PASSWORD"));
00032     HTMLWrite(htmlout,"<br>Query: <input name=QUERY value=\"");
00033    HTMLWrite(htmlout,GetFieldValue(Params,"QUERY"));
00034    HTMLWrite(htmlout,"\">\n");
00035     HTMLWrite(htmlout,"<br>Crosstabs: <input name=CROSSTAB value=\"");
00036    HTMLWrite(htmlout,GetFieldValue(Params,"CROSSTAB"));
00037    HTMLWrite(htmlout,"\">\n");
00038    HTMLWrite(htmlout,"<br><input type=submit>\n");
00039 
00040     HTMLWrite(htmlout,"</form>\n\n");
00041 }

void UpdateStatistics CGINameValue count,
CGINameValue sum,
CGINameValue record,
CGINameValue xtab,
CGINameValue xcount,
CGINameValue xsum
 

Definition at line 99 of file plot.c.

References CalcCountSummary(), CGIFREE, EvaluateLogic(), GetField(), CGINameValue::name, SetValue(), strdup(), strdup3(), and CGINameValue::value.

Referenced by CGImain().

00101 {
00102  size_t j;
00103  if (count)
00104  for(j=0; count[j].name; j++)
00105     {/* count */
00106      int x = atoi(count[j].value);
00107      int d = CalcCountSummary(record,count[j].name);
00108      x += d;
00109 
00110      if (d)
00111      {
00112       char temp[32];
00113       sprintf(temp,"%d",x);
00114       SetValue(count+j,temp);
00115 
00116       if (xtab) /* evaluate crosstab entries */
00117       {
00118        size_t i;
00119        char* name;
00120        CGINameValue* v;
00121        for(i=0; xtab[i].name; i++)
00122        {
00123         name = strdup(xtab[i].value);
00124         if (EvaluateLogic(name,record,0))
00125         {
00126          CGIFREE(name);
00127          name = strdup3(xtab[i].name,".",count[j].name);
00128          v = GetField(xcount,name);
00129          sprintf(temp,"%d",atoi(v->value) + d);
00130          SetValue(v,temp);
00131         }
00132         CGIFREE(name);
00133        }
00134       }
00135      }
00136     }
00137 
00138  if (sum)
00139  for(j=0; sum[j].name; j++)
00140     {/* count */
00141      double x = strtod(sum[j].value,0);
00142      double d = strtod(GetFieldValue(record,sum[j].name),0);
00143      if (x < 10E16 && x > -10E16 && d < 10E16 && d > -10E16)
00144      {
00145       if (d < 10E16 && d > -10E16)
00146        x += d;
00147      }
00148 
00149      if (d != 0.0)
00150      {
00151       char temp[64];
00152       sprintf(temp,"%f",x);
00153       SetValue(sum+j,temp);
00154 
00155       if (xtab) /* evaluate crosstab entries */
00156       {
00157        size_t i;
00158        char* name;
00159        CGINameValue* v;
00160        for(i=0; xtab[i].name; i++)
00161        {
00162         name = strdup(xtab[i].value);
00163         if (EvaluateLogic(name,record,0))
00164         {
00165          CGIFREE(name);
00166          name = strdup3(xtab[i].name,".",sum[j].name);
00167          v = GetField(xsum,name);
00168          sprintf(temp,"%f",strtod(v->value,0) + d);
00169          SetValue(v,temp);
00170         }
00171         CGIFREE(name);
00172        }
00173 
00174       }
00175      }
00176     }
00177 }



Raosoft, Inc.
Raosoft EZReport, EZSurvey, InterForm, RapidReport, Raosoft, and SurveyWin are registered trademarks of Raosoft, Inc. Page contents © 1996-2007 by Raosoft, Inc. You may use and modify this file for your own use, but may not distribute it or derivative works without the prior written consent of Raosoft, Inc. This software is provided "as is," and Raosoft makes no warranty, express or implied, of fitness for a particular application. Every measure has been taken to anticipate risks inherent to computer networks, but we cannot guarantee safety or reliability of this program in every situation.
Tel: 206-525-4025 (US) Email: raosoft@raosoft.com
http://www.raosoft.com/