"Fossies" - the Fresh Open Source Software Archive

Member "srg-1.3.6/src/output.cc" (5 Aug 2009, 6025 Bytes) of package /linux/privat/old/srg-1.3.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2     SRG - Squid Report Generator
    3     Copyright 2005 University of Waikato
    4 
    5     This file is part of SRG.
    6 
    7     SRG is free software; you can redistribute it and/or modify
    8     it under the terms of the GNU General Public License as published by
    9     the Free Software Foundation; either version 2 of the License, or
   10     (at your option) any later version.
   11 
   12     SRG is distributed in the hope that it will be useful,
   13     but WITHOUT ANY WARRANTY; without even the implied warranty of
   14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15     GNU General Public License for more details.
   16 
   17     You should have received a copy of the GNU General Public License
   18     along with SRG; if not, write to the Free Software
   19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   20 
   21 */
   22 #include "srg.h"
   23 #include "prototypes.h"
   24 
   25 /* Check the environment for outputting reports */
   26 void check_environment(void)
   27 {
   28     
   29     /* Ensure Base Directory Exists */
   30     if (mkdir(srg.outputDir, 0755) == -1) {
   31         if (errno != EEXIST) {
   32             fprintf(stderr, "%s: Error creating base output "
   33                     "directory: %s\n", progname, srg.outputDir);
   34             exit(1);
   35         }
   36     }
   37 
   38     /* Check for resources directory */
   39     if (access(srg.resourceDir, R_OK)!=0) {
   40         fprintf(stderr, "%s: Unable to access resource directory (%s)!\n",
   41                 progname, srg.resourceDir);
   42         exit(1);
   43     }
   44 
   45     /* Check for stylesheet file */
   46     if (!file_present(SRG_CSS_FILE, "SRG Default Stylesheet - ")) {
   47         init_file(SRG_CSS_FILE);
   48     }
   49     
   50     /* Check for javascript file */
   51     if (srg.usejs && !file_present(SRG_JS_FILE, "SRG Default Javascript - ")) {
   52         init_file(SRG_JS_FILE);
   53     }
   54     
   55     /* Check for PHP header / footer if required */
   56     if (srg.outputMode == OUTPUT_PHP) {
   57         if (!file_present(SRG_HEADER_FILE, "SRG Default PHP Header - ")) {
   58             init_file(SRG_HEADER_FILE);
   59         }
   60         if (!file_present(SRG_FOOTER_FILE, "SRG Default PHP Footer - ")) {
   61             init_file(SRG_FOOTER_FILE);
   62         }
   63     }
   64 
   65     /* Setup Paths */
   66     asprintf(&srg.phpheader, "%s/%s", srg.outputDir, srg.phpheader);
   67     asprintf(&srg.phpfooter, "%s/%s", srg.outputDir, srg.phpfooter);
   68     asprintf(&srg.cssfile, "%s/%s", srg.outputURL, srg.cssfile);
   69     asprintf(&srg.jsfile, "%s/%s", srg.outputURL, srg.jsfile);
   70     
   71 }
   72 
   73 /* Copy a file from the resource directory to the base directory */
   74 void init_file(const char *filename)
   75 {
   76 
   77     char sfilename[1024] = {'\0'};
   78     char dfilename[1024] = {'\0'};
   79     FILE *source = NULL;
   80     FILE *dest = NULL;
   81     
   82     /* Create filenames */
   83     snprintf(&sfilename[0], 1023, "%s/%s", srg.resourceDir, filename);
   84     snprintf(&dfilename[0], 1023, "%s/%s", srg.outputDir, filename);
   85     
   86     /* Open files */
   87     source = fopen(sfilename, "r");
   88     if (!source) {
   89         fprintf(stderr, "ERROR: Could not open file: %s\n", sfilename);
   90         exit(1);
   91     }
   92     dest = fopen(dfilename, "w");
   93     if (!dest) {
   94         fprintf(stderr, "ERROR: Could not open file for writing: %s\n",
   95                 dfilename);
   96         exit(1);
   97     }
   98     
   99     /* Copy data */
  100     while (!feof(source)) {
  101         char *buf[4096] = {'\0'};
  102         int c = fread((void *)&buf[0], 1, 4096, source);
  103         fwrite((void *)&buf[0], 1, c, dest);
  104     }
  105 
  106     /* Close files */
  107     fclose(source);
  108     fclose(dest);
  109     
  110 }
  111 
  112 /* Write the HTML header */
  113 void html_header(FILE *outfile, const char *rootpath) {
  114 
  115     /* rootpath is used when the user is generating a stock standard
  116      * report with no outputURL set. Each report sends through a string
  117      * like ../../ that provides a path from the directory that this
  118      * file will reside in to the top level output directory. This string
  119      * is only used if srg.outputURL is not set
  120      */
  121     char *base="";
  122 
  123     if (strlen(srg.outputURL)==0 && strlen(rootpath)>0) {
  124         base = strdup(rootpath);
  125     }
  126 
  127     /* Header & Title */
  128     if (srg.outputMode == OUTPUT_PHP) {
  129         /* Include the specified PHP header */
  130         fprintf(outfile, "<?php\ninclude_once('%s');\n?>\n", 
  131                 srg.phpheader);
  132         if (srg.authenticate) {
  133             fprintf(outfile, "<?php if (can_view(\"-1\")) { "
  134                     "?>\n\t");
  135         }
  136     } else {
  137         /* Output a static HTML Header */
  138         fprintf(outfile, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML "
  139                 "4.01 Transitional//EN\" \"http://www.w3."
  140                 "org/TR/html4/loose.dtd\">\n");
  141         fprintf(outfile, "<html>\n<head>\n<title>%s</title>\n", 
  142                 srg.title);
  143         fprintf(outfile, "<meta name=\"generator\" content=\""
  144                 "SRG %s (%s)\">\n", version, HOME_URL); 
  145         fprintf(outfile, "<meta name=\"robots\" content=\"noindex,"
  146                 "nofollow\">\n");
  147         fprintf(outfile, "<link rel=\"stylesheet\" href=\"%s%s\" type=\""
  148                 "text/css\">\n", base, srg.cssfile);
  149         if (srg.usejs == 1) {
  150             fprintf(outfile, "<script language=\"javascript\" src=\"%s%s\" "
  151                     "type=\"text/javascript\"></script></head>\n", 
  152                     base, srg.jsfile);
  153             fprintf(outfile, "<body onload=\"setupSort();\">\n");
  154         } else {
  155             fprintf(outfile, "</head>\n\n<body>\n");
  156         }
  157         /* Output some basic layout and headers */
  158         fprintf(outfile, "<div align=\"center\"><h1>%s</h1></div>\n\n", 
  159                 srg.title);
  160     }
  161 
  162     if (strlen(base)>0)
  163         free(base);
  164 
  165 }
  166 
  167 /* Write the HTML footer */
  168 void html_footer(FILE *outfile, const char *rootpath) {
  169 
  170     /* rootpath is used when the user is generating a stock standard
  171      * report with no outputURL set. Each report sends through a string
  172      * like ../../ that provides a path from the directory that this
  173      * file will reside in to the top level output directory. This string
  174      * is only used if srg.outputURL is not set
  175      */
  176     char *base="";
  177 
  178     if (strlen(srg.outputURL)==0 && strlen(rootpath)>0)
  179         base = strdup(rootpath);
  180 
  181     fprintf(outfile, "<div align=\"center\" class=\"srgfooter\"><a href=\""
  182             "%s\">Generated by SRG %s</a></div>\n\n", HOME_URL, version);
  183 
  184     if (srg.outputMode == OUTPUT_PHP) {
  185         /* Include the specified PHP footer */
  186         fprintf(outfile, "<?php\ninclude_once('%s');\n\n",
  187                 srg.phpfooter);
  188     } else {
  189         fprintf(outfile, "\n</body>\n");
  190         if (srg.usejs) {
  191             fprintf(outfile, "<script src=\"%s%s\" type=\""
  192                     "text/javascript\"></script>\n", base, srg.jsfile);
  193         }                
  194         fprintf(outfile, "</html>\n");
  195     }
  196 
  197     if (strlen(base)>0)
  198         free(base);
  199 
  200 }