rllib  1
rlreport.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           rlreport.cpp  -  description
00003                              -------------------
00004     begin                : Mon Jul 04 2011
00005     copyright            : (C) 2011 pvbrowser
00006     email                : lehrig@t-online.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This library is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as        *
00013  *   published by the Free Software Foundation                             *
00014  *                                                                         *
00015  ***************************************************************************/
00016 #include <stdarg.h>
00017 #include "rlreport.h"
00018 #include "rlspreadsheet.h"
00019 #include "rlcutil.h"
00020 
00021 rlReport::rlReport()
00022 {
00023   fout = NULL;
00024 }
00025 
00026 rlReport::~rlReport()
00027 {
00028   close();
00029 }
00030 
00031 int rlReport::open(const char *filename)
00032 {
00033   char *cptr;
00034   file = "";
00035   if(filename == NULL) return -1;
00036   if(fout != NULL) fclose(fout);
00037   file = filename;
00038   cptr = strstr(file.text(),".tex");
00039   if(cptr != NULL) *cptr = '\0';
00040   fout = fopen(filename,"w");
00041   if(fout == NULL) return -1;
00042   return 0;
00043 }
00044 
00045 int rlReport::close()
00046 {
00047   if(fout == NULL) return -1;
00048   fclose(fout);
00049   fout = NULL;
00050   return 0;
00051 }
00052   
00053 int rlReport::printf(const char *format, ...)
00054 {
00055   int ret;
00056   char line[rl_PRINTF_LENGTH]; // should be big enough
00057 
00058   va_list ap;
00059   va_start(ap,format);
00060   ret = rlvsnprintf(line, rl_PRINTF_LENGTH - 1, format, ap);
00061   va_end(ap);
00062   if(ret < 0) return ret;
00063   fprintf(fout,"%s",line);
00064   return ret;
00065 }
00066 
00067 int rlReport::beginDocument()
00068 {
00069   return printf("\\begin{document}\n");
00070 }
00071 
00072 int rlReport::endDocument()
00073 {
00074   return printf("\\end{document}\n");
00075 }
00076 
00077 int rlReport::include(const char *filename, rlIniFile *ini)
00078 {
00079   FILE *fin;
00080   char line[rl_PRINTF_LENGTH]; // should be big enough
00081   char *cptr, *start, *section, *name;
00082 
00083   if(fout == NULL) return -1; 
00084   if(filename == NULL) return -1;
00085   if(ini == NULL)
00086   {
00087     fin = fopen(filename,"r");
00088     if(fin == NULL)
00089     {
00090       printf  ("rlReport::include() ERROR: Could not find %s\n",filename);
00091       ::printf("rlReport::include() ERROR: Could not find %s\n",filename);
00092       return -1;
00093     }  
00094     while(fgets(line,sizeof(line)-1,fin) != NULL)
00095     {
00096       fprintf(fout,"%s",line);
00097     }
00098     fclose(fin);
00099     return 0;
00100   }
00101   else
00102   {
00103     fin = fopen(filename,"r");
00104     if(fin == NULL)
00105     {
00106       printf  ("rlReport::include() ERROR: Could not find %s\n",filename);
00107       ::printf("rlReport::include() ERROR: Could not find %s\n",filename);
00108       return -1;
00109     }  
00110     while(fgets(line,sizeof(line)-1,fin) != NULL)
00111     {
00112       start = &line[0];
00113       while(*start != '\0')
00114       {
00115         cptr = strstr(start,"\\$[");
00116         if(cptr == NULL)
00117         {
00118           fprintf(fout,"%s",start);
00119           break;
00120         }
00121         else
00122         {
00123           *cptr = '\0';
00124           fprintf(fout,"%s", start);
00125           cptr += 3;
00126           section = cptr;
00127           cptr = strchr(cptr,']');
00128           if(cptr == NULL)
00129           {
00130             ::printf("rlReport::include ERROR in line=%s", line);
00131             fclose(fin);
00132             return -1;
00133           }
00134           *cptr = '\0';
00135           cptr++;
00136           cptr++;
00137           name = cptr;
00138           cptr = strchr(cptr,']');
00139           if(cptr == NULL)
00140           {
00141             ::printf("rlReport::include ERROR in line=%s", line);
00142             fclose(fin);
00143             return -1;
00144           }
00145           *cptr = '\0';
00146           cptr++;
00147           start = cptr;
00148           fprintf(fout,"%s", ini->text(section,name));
00149         }
00150       }  
00151     }
00152     fclose(fin);
00153     return 0;
00154   }  
00155 }
00156 
00157 int rlReport::includeHeader(const char *documentclass, const char *language, const char *inputenc, const char *layout)
00158 {
00159   if(fout == NULL) return -1; 
00160   if(documentclass == NULL) documentclass = "\\documentclass[a4paper]{article}"; 
00161   if(language      == NULL) language      = "\\usepackage[english]{babel}"; 
00162   if(inputenc      == NULL) inputenc      = "\\usepackage[utf8]{inputenc}";
00163   if(layout        == NULL) layout        = "\\setlength{\\parindent}{0pt} \\setlength{\\topmargin}{-50pt} \\setlength{\\oddsidemargin}{0pt}  \\setlength{\\textwidth}{480pt} \\setlength{\\textheight}{700pt}";
00164 
00165   fprintf(fout,"%s","% --- header begin ------------------------------------------------------------------------------------------------\n");
00166   fprintf(fout,     "  %s\n", documentclass);
00167   fprintf(fout,     "  %s\n", inputenc);
00168   fprintf(fout,"%s","  \\usepackage[pdftex]{hyperref}\n");
00169   fprintf(fout,"%s","  \\pdfcatalog{/UseThumbs /UseOutlines}\n");
00170   fprintf(fout,"%s","  \\usepackage{amssymb}\n");
00171   fprintf(fout,"%s","  \\newcommand{\\arrow}{\\begin{math}->\\end{math}}    % define arrow symbol\n");
00172   fprintf(fout,"%s","  \\newcommand{\\asterix}{\\begin{math}*\\end{math}}   % define asterix symbol\n");
00173   fprintf(fout,     "  %s\n", language);
00174   fprintf(fout,"%s","  \\usepackage{textcomp}                               % define euro\n");
00175   fprintf(fout,"%s","  \\usepackage{longtable}                              % use tables over several pages\n");
00176   fprintf(fout,"%s","  \\usepackage{colortbl}\n");
00177   fprintf(fout,"%s","\n");
00178   fprintf(fout,"%s","  %  % --- documentation support for sourcecode -------------\n");
00179   fprintf(fout,"%s","  \\usepackage{listings}\n");
00180   fprintf(fout,"%s","  \\usepackage{color}\n");
00181   fprintf(fout,"%s","  \\definecolor{white}{rgb}{1.0,1.0,1.0}\n");
00182   fprintf(fout,"%s","  \\definecolor{gray}{rgb}{0.5,0.5,0.5}\n");
00183   fprintf(fout,"%s","  \\definecolor{darkred}{rgb}{0.5,0.0,0.0}\n");
00184   fprintf(fout,"%s","  \\definecolor{lightgray}{rgb}{0.95,0.95,0.95}\n");
00185   fprintf(fout,"%s","  \\definecolor{mygray}{gray}{0.55}\n");
00186   fprintf(fout,"%s","  \\newcommand{\\simplecodestyle}{\\lstset{basicstyle=\\ttfamily\\small\\mdseries,backgroundcolor=\\color{lightgray},breaklines=true}}\n");
00187   fprintf(fout,"%s","  \\newcommand{\\codestyle}{\\lstset{basicstyle=\\ttfamily\\small\\mdseries,keywordstyle=\\bfseries\\color{blue},commentstyle=\\color{gray},stringstyle=\\color{darkred},backgroundcolor=\\color{lightgray},breaklines=true}}\n");
00188   fprintf(fout,"%s","  \\newcommand{\\simplecodebegin}[1]{\\simplecodestyle\\begin{lstlisting}[frame=single,flexiblecolumns=true,tabsize={8},inputencoding={utf8},title={#1}]}\n");
00189   fprintf(fout,"%s","  \\newcommand{\\bashbegin}[1]{\\codestyle\\begin{lstlisting}[language=bash,frame=single,flexiblecolumns=true,tabsize={8},inputencoding={utf8},title={#1}]}\n");
00190   fprintf(fout,"%s","  \\newcommand{\\bashsimple}[0]{\\codestyle\\begin{lstlisting}[language=bash,frame=single,flexiblecolumns=true,tabsize={8},inputencoding={utf8}]}\n");
00191   fprintf(fout,"%s","  \\newcommand{\\cppbegin}[1]{\\codestyle\\begin{lstlisting}[language=C++,frame=single,flexiblecolumns=true,tabsize={8},inputencoding={utf8},title={#1}]}\n");
00192   fprintf(fout,"%s","  \\newcommand{\\cppsimple}[0]{\\codestyle\\begin{lstlisting}[language=C++,frame=single,flexiblecolumns=true],tabsize={8},inputencoding={utf8}}\n");
00193   fprintf(fout,"%s","  \\newcommand{\\cppend}{\\end{lstlisting}}\n");
00194   fprintf(fout,"%s","  \\newcommand{\\pybegin}[1]{\\codestyle\\begin{lstlisting}[language=Python,frame=single,flexiblecolumns=true,tabsize={8},inputencoding={utf8},title={#1}]}\n");
00195   fprintf(fout,"%s","  \\newcommand{\\xmlbegin}[1]{\\codestyle\\begin{lstlisting}[language=XML,frame=single,flexiblecolumns=true,tabsize={8},inputencoding={utf8},title={#1}]}\n");
00196   fprintf(fout,"%s","  \\newcommand{\\tclbegin}[1]{\\codestyle\\begin{lstlisting}[language=tcl,frame=single,flexiblecolumns=true,tabsize={8},inputencoding={utf8},title={#1}]}\n");
00197   fprintf(fout,"%s","  \\lstdefinelanguage{mylua}\n");
00198   fprintf(fout,"%s","    {keywords={while,for,elseif,if,else,then,do,end,return,function,and,or,not,nil,true,false},\n");
00199   fprintf(fout,"%s","    sensitive=true,\n");
00200   fprintf(fout,"%s","    morecomment=[l][\\color{mygray}]{--},\n");
00201   fprintf(fout,"%s","    string=[b],\n");
00202   fprintf(fout,"%s","    }\n");
00203   fprintf(fout,"%s","  \\newcommand{\\luabegin}[1]{\\codestyle\\begin{lstlisting}[language=mylua,frame=single,flexiblecolumns=true,tabsize={8},inputencoding={utf8},title={#1}]}\n");
00204   fprintf(fout,"%s","  %   % --- usage example begin -----------------------------\n");
00205   fprintf(fout,"%s","  %   \\cppbegin{Titel of our C++ code snippet}\n");
00206   fprintf(fout,"%s","  %     printf(\"hello world\\n\");\n");
00207   fprintf(fout,"%s","  %   \\end{lstlisting}\n");
00208   fprintf(fout,"%s","  %   % --- usage example end -------------------------------\n");
00209   fprintf(fout,"%s","\n");
00210   fprintf(fout,"%s","\n");
00211   fprintf(fout,"%s","  %   % --- graphics support --------------------------------\n");
00212   fprintf(fout,"%s","  \\usepackage{graphicx}\n");
00213   fprintf(fout,"%s","  \\usepackage{float}\n");
00214   fprintf(fout,"%s","  \\newcommand{\\img}[3]{\n");
00215   fprintf(fout,"%s","    \\setlength\\fboxsep{0pt}\n");
00216   fprintf(fout,"%s","    \\setlength\\fboxrule{1pt}\n");
00217   fprintf(fout,"%s","    \\begin{figure}\n");
00218   fprintf(fout,"%s","    \\begin{center}\n");
00219   fprintf(fout,"%s","    \\fbox{\\includegraphics[scale=#1]{#2}}\n");
00220   fprintf(fout,"%s","    \\caption{#3}\n");
00221   fprintf(fout,"%s","    \\end{center}\n");
00222   fprintf(fout,"%s","    \\end{figure}\n");
00223   fprintf(fout,"%s","  }\n");
00224   fprintf(fout,"%s","  \\newcommand{\\imgH}[3]{\n");
00225   fprintf(fout,"%s","    \\setlength\\fboxsep{0pt}\n");
00226   fprintf(fout,"%s","    \\setlength\\fboxrule{1pt}\n");
00227   fprintf(fout,"%s","    \\begin{figure}[H]\n");
00228   fprintf(fout,"%s","    \\begin{center}\n");
00229   fprintf(fout,"%s","    \\fbox{\\includegraphics[scale=#1]{#2}}\n");
00230   fprintf(fout,"%s","    \\caption{#3}\n");
00231   fprintf(fout,"%s","    \\end{center}\n");
00232   fprintf(fout,"%s","    \\end{figure}\n");
00233   fprintf(fout,"%s","  }\n");
00234   fprintf(fout,"%s","\n");
00235   fprintf(fout,"%s","  %   % --- definition of the paper layout ------------------- \n");
00236   fprintf(fout,    "  %s\n", layout);
00237   fprintf(fout,"%s","% --- header end --------------------------------------------------------------------------------------------------\n");
00238   return 0;
00239 }
00240 
00241 int rlReport::includeCSV(const char *filename, int use_first_row_as_title, const char *legend, char delimitor)
00242 {
00243   int x,y,xmax,ymax;
00244   rlSpreadsheetTable t(delimitor);
00245   if(fout == NULL) return -1;
00246   if(filename == NULL) return -1;
00247   if(t.read(filename) < 0) 
00248   {
00249     printf  ("rlReport::includeCSV() ERROR: Could not find %s\n",filename);
00250     ::printf("rlReport::includeCSV() ERROR: Could not find %s\n",filename);
00251     return -1;
00252   }  
00253  
00254   x = xmax = 1;
00255   while(t.exists(x,1) && xmax < 1024) xmax = x++; 
00256   y = ymax = 1;
00257   while(t.exists(1,y) && ymax < 256*256) ymax = y++; 
00258 
00259   printf("\\begin{center}\n");
00260   printf("\\begin{longtable}");
00261   
00262   x = 1;
00263   printf("{");
00264   while(x <= xmax) { printf(" | l"); x++; }
00265   printf(" | }\n");
00266   printf("    \\hline\n");
00267   for(y=1; y<=ymax; y++)
00268   {
00269     if(y==1 && use_first_row_as_title) printf("    \\rowcolor{gray}\n");;
00270     printf("      ");
00271     for(x=1; x<=xmax; x++)
00272     {
00273       printf("%s ", t.text(x,y));
00274       if(x < xmax) printf("& ");
00275     }
00276     printf(" \\\\\n");
00277     if(y < ymax) printf("    \\hline\n");
00278   }
00279   printf("    \\hline\n");
00280   if(legend != NULL) printf("  \\caption{%s}\n", legend);
00281 
00282   printf("\\end{longtable}\n");
00283   printf("\\end{center}\n");
00284   return 0;
00285 }
00286 
00287 int rlReport::includeImage(const char *filename, const char *legend, float scale)
00288 {
00289   if(fout == NULL) return -1;
00290   if(filename == NULL) return -1;
00291   if(legend == NULL) legend = "";
00292   printf("\\img{%.1f}{%s}{%s}\n", scale, filename, legend);
00293   return 0;
00294 }
00295 
00296 int rlReport::spawn(const char *command)
00297 {
00298   char cmd[rl_PRINTF_LENGTH]; // should be big enough
00299  
00300   if(fout == NULL) return -1;
00301   if(command == NULL) return -1;
00302   sprintf(cmd,"%s > %s.temp", command, file.text());
00303   system(cmd);
00304   sprintf(cmd,"%s.temp", file.text());
00305   return include(cmd);
00306 }
00307 
00308 int rlReport::pdflatex(const char *command)
00309 {
00310   char cmd[rl_PRINTF_LENGTH]; // should be big enough
00311   if(command == NULL)
00312   {
00313     sprintf(cmd,"pdflatex -interaction=nonstopmode %s.tex", file.text());
00314     system(cmd); // run pdflatex twice
00315     return system(cmd);
00316   }
00317   else
00318   {
00319     sprintf(cmd,"%s %s.tex", command, file.text());
00320     system(cmd); // run pdflatex twice
00321     return system(cmd);
00322   }
00323 }
00324   
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines