rllib  1
rlreport.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rlreport.cpp - description
3  -------------------
4  begin : Mon Jul 04 2011
5  copyright : (C) 2011 pvbrowser
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
16 #include <stdarg.h>
17 #include "rlreport.h"
18 #include "rlspreadsheet.h"
19 #include "rlcutil.h"
20 
22 {
23  fout = NULL;
24 }
25 
27 {
28  close();
29 }
30 
31 int rlReport::open(const char *filename)
32 {
33  char *cptr;
34  file = "";
35  if(filename == NULL) return -1;
36  if(fout != NULL) fclose(fout);
37  file = filename;
38  cptr = strstr(file.text(),".tex");
39  if(cptr != NULL) *cptr = '\0';
40  fout = fopen(filename,"w");
41  if(fout == NULL) return -1;
42  return 0;
43 }
44 
46 {
47  if(fout == NULL) return -1;
48  fclose(fout);
49  fout = NULL;
50  return 0;
51 }
52 
53 int rlReport::printf(const char *format, ...)
54 {
55  int ret;
56  char line[rl_PRINTF_LENGTH]; // should be big enough
57 
58  va_list ap;
59  va_start(ap,format);
60  ret = rlvsnprintf(line, rl_PRINTF_LENGTH - 1, format, ap);
61  va_end(ap);
62  if(ret < 0) return ret;
63  fprintf(fout,"%s",line);
64  return ret;
65 }
66 
68 {
69  return printf("\\begin{document}\n");
70 }
71 
73 {
74  return printf("\\end{document}\n");
75 }
76 
77 int rlReport::include(const char *filename, rlIniFile *ini)
78 {
79  FILE *fin;
80  char line[rl_PRINTF_LENGTH]; // should be big enough
81  char *cptr, *start, *section, *name;
82 
83  if(fout == NULL) return -1;
84  if(filename == NULL) return -1;
85  if(ini == NULL)
86  {
87  fin = fopen(filename,"r");
88  if(fin == NULL)
89  {
90  printf ("rlReport::include() ERROR: Could not find %s\n",filename);
91  ::printf("rlReport::include() ERROR: Could not find %s\n",filename);
92  return -1;
93  }
94  while(fgets(line,sizeof(line)-1,fin) != NULL)
95  {
96  fprintf(fout,"%s",line);
97  }
98  fclose(fin);
99  return 0;
100  }
101  else
102  {
103  fin = fopen(filename,"r");
104  if(fin == NULL)
105  {
106  printf ("rlReport::include() ERROR: Could not find %s\n",filename);
107  ::printf("rlReport::include() ERROR: Could not find %s\n",filename);
108  return -1;
109  }
110  while(fgets(line,sizeof(line)-1,fin) != NULL)
111  {
112  start = &line[0];
113  while(*start != '\0')
114  {
115  cptr = strstr(start,"\\$[");
116  if(cptr == NULL)
117  {
118  fprintf(fout,"%s",start);
119  break;
120  }
121  else
122  {
123  *cptr = '\0';
124  fprintf(fout,"%s", start);
125  cptr += 3;
126  section = cptr;
127  cptr = strchr(cptr,']');
128  if(cptr == NULL)
129  {
130  ::printf("rlReport::include ERROR in line=%s", line);
131  fclose(fin);
132  return -1;
133  }
134  *cptr = '\0';
135  cptr++;
136  cptr++;
137  name = cptr;
138  cptr = strchr(cptr,']');
139  if(cptr == NULL)
140  {
141  ::printf("rlReport::include ERROR in line=%s", line);
142  fclose(fin);
143  return -1;
144  }
145  *cptr = '\0';
146  cptr++;
147  start = cptr;
148  fprintf(fout,"%s", ini->text(section,name));
149  }
150  }
151  }
152  fclose(fin);
153  return 0;
154  }
155 }
156 
157 int rlReport::includeHeader(const char *documentclass, const char *language, const char *inputenc, const char *layout)
158 {
159  if(fout == NULL) return -1;
160  if(documentclass == NULL) documentclass = "\\documentclass[a4paper]{article}";
161  if(language == NULL) language = "\\usepackage[english]{babel}";
162  if(inputenc == NULL) inputenc = "\\usepackage[utf8]{inputenc}";
163  if(layout == NULL) layout = "\\setlength{\\parindent}{0pt} \\setlength{\\topmargin}{-50pt} \\setlength{\\oddsidemargin}{0pt} \\setlength{\\textwidth}{480pt} \\setlength{\\textheight}{700pt}";
164 
165  fprintf(fout,"%s","% --- header begin ------------------------------------------------------------------------------------------------\n");
166  fprintf(fout, " %s\n", documentclass);
167  fprintf(fout, " %s\n", inputenc);
168  fprintf(fout,"%s"," \\usepackage[pdftex]{hyperref}\n");
169  fprintf(fout,"%s"," \\pdfcatalog{/UseThumbs /UseOutlines}\n");
170  fprintf(fout,"%s"," \\usepackage{amssymb}\n");
171  fprintf(fout,"%s"," \\newcommand{\\arrow}{\\begin{math}->\\end{math}} % define arrow symbol\n");
172  fprintf(fout,"%s"," \\newcommand{\\asterix}{\\begin{math}*\\end{math}} % define asterix symbol\n");
173  fprintf(fout, " %s\n", language);
174  fprintf(fout,"%s"," \\usepackage{textcomp} % define euro\n");
175  fprintf(fout,"%s"," \\usepackage{longtable} % use tables over several pages\n");
176  fprintf(fout,"%s"," \\usepackage{colortbl}\n");
177  fprintf(fout,"%s","\n");
178  fprintf(fout,"%s"," % % --- documentation support for sourcecode -------------\n");
179  fprintf(fout,"%s"," \\usepackage{listings}\n");
180  fprintf(fout,"%s"," \\usepackage{color}\n");
181  fprintf(fout,"%s"," \\definecolor{white}{rgb}{1.0,1.0,1.0}\n");
182  fprintf(fout,"%s"," \\definecolor{gray}{rgb}{0.5,0.5,0.5}\n");
183  fprintf(fout,"%s"," \\definecolor{darkred}{rgb}{0.5,0.0,0.0}\n");
184  fprintf(fout,"%s"," \\definecolor{lightgray}{rgb}{0.95,0.95,0.95}\n");
185  fprintf(fout,"%s"," \\definecolor{mygray}{gray}{0.55}\n");
186  fprintf(fout,"%s"," \\newcommand{\\simplecodestyle}{\\lstset{basicstyle=\\ttfamily\\small\\mdseries,backgroundcolor=\\color{lightgray},breaklines=true}}\n");
187  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");
188  fprintf(fout,"%s"," \\newcommand{\\simplecodebegin}[1]{\\simplecodestyle\\begin{lstlisting}[frame=single,flexiblecolumns=true,tabsize={8},inputencoding={utf8},title={#1}]}\n");
189  fprintf(fout,"%s"," \\newcommand{\\bashbegin}[1]{\\codestyle\\begin{lstlisting}[language=bash,frame=single,flexiblecolumns=true,tabsize={8},inputencoding={utf8},title={#1}]}\n");
190  fprintf(fout,"%s"," \\newcommand{\\bashsimple}[0]{\\codestyle\\begin{lstlisting}[language=bash,frame=single,flexiblecolumns=true,tabsize={8},inputencoding={utf8}]}\n");
191  fprintf(fout,"%s"," \\newcommand{\\cppbegin}[1]{\\codestyle\\begin{lstlisting}[language=C++,frame=single,flexiblecolumns=true,tabsize={8},inputencoding={utf8},title={#1}]}\n");
192  fprintf(fout,"%s"," \\newcommand{\\cppsimple}[0]{\\codestyle\\begin{lstlisting}[language=C++,frame=single,flexiblecolumns=true],tabsize={8},inputencoding={utf8}}\n");
193  fprintf(fout,"%s"," \\newcommand{\\cppend}{\\end{lstlisting}}\n");
194  fprintf(fout,"%s"," \\newcommand{\\pybegin}[1]{\\codestyle\\begin{lstlisting}[language=Python,frame=single,flexiblecolumns=true,tabsize={8},inputencoding={utf8},title={#1}]}\n");
195  fprintf(fout,"%s"," \\newcommand{\\xmlbegin}[1]{\\codestyle\\begin{lstlisting}[language=XML,frame=single,flexiblecolumns=true,tabsize={8},inputencoding={utf8},title={#1}]}\n");
196  fprintf(fout,"%s"," \\newcommand{\\tclbegin}[1]{\\codestyle\\begin{lstlisting}[language=tcl,frame=single,flexiblecolumns=true,tabsize={8},inputencoding={utf8},title={#1}]}\n");
197  fprintf(fout,"%s"," \\lstdefinelanguage{mylua}\n");
198  fprintf(fout,"%s"," {keywords={while,for,elseif,if,else,then,do,end,return,function,and,or,not,nil,true,false},\n");
199  fprintf(fout,"%s"," sensitive=true,\n");
200  fprintf(fout,"%s"," morecomment=[l][\\color{mygray}]{--},\n");
201  fprintf(fout,"%s"," string=[b],\n");
202  fprintf(fout,"%s"," }\n");
203  fprintf(fout,"%s"," \\newcommand{\\luabegin}[1]{\\codestyle\\begin{lstlisting}[language=mylua,frame=single,flexiblecolumns=true,tabsize={8},inputencoding={utf8},title={#1}]}\n");
204  fprintf(fout,"%s"," % % --- usage example begin -----------------------------\n");
205  fprintf(fout,"%s"," % \\cppbegin{Titel of our C++ code snippet}\n");
206  fprintf(fout,"%s"," % printf(\"hello world\\n\");\n");
207  fprintf(fout,"%s"," % \\end{lstlisting}\n");
208  fprintf(fout,"%s"," % % --- usage example end -------------------------------\n");
209  fprintf(fout,"%s","\n");
210  fprintf(fout,"%s","\n");
211  fprintf(fout,"%s"," % % --- graphics support --------------------------------\n");
212  fprintf(fout,"%s"," \\usepackage{graphicx}\n");
213  fprintf(fout,"%s"," \\usepackage{float}\n");
214  fprintf(fout,"%s"," \\newcommand{\\img}[3]{\n");
215  fprintf(fout,"%s"," \\setlength\\fboxsep{0pt}\n");
216  fprintf(fout,"%s"," \\setlength\\fboxrule{1pt}\n");
217  fprintf(fout,"%s"," \\begin{figure}\n");
218  fprintf(fout,"%s"," \\begin{center}\n");
219  fprintf(fout,"%s"," \\fbox{\\includegraphics[scale=#1]{#2}}\n");
220  fprintf(fout,"%s"," \\caption{#3}\n");
221  fprintf(fout,"%s"," \\end{center}\n");
222  fprintf(fout,"%s"," \\end{figure}\n");
223  fprintf(fout,"%s"," }\n");
224  fprintf(fout,"%s"," \\newcommand{\\imgH}[3]{\n");
225  fprintf(fout,"%s"," \\setlength\\fboxsep{0pt}\n");
226  fprintf(fout,"%s"," \\setlength\\fboxrule{1pt}\n");
227  fprintf(fout,"%s"," \\begin{figure}[H]\n");
228  fprintf(fout,"%s"," \\begin{center}\n");
229  fprintf(fout,"%s"," \\fbox{\\includegraphics[scale=#1]{#2}}\n");
230  fprintf(fout,"%s"," \\caption{#3}\n");
231  fprintf(fout,"%s"," \\end{center}\n");
232  fprintf(fout,"%s"," \\end{figure}\n");
233  fprintf(fout,"%s"," }\n");
234  fprintf(fout,"%s","\n");
235  fprintf(fout,"%s"," % % --- definition of the paper layout ------------------- \n");
236  fprintf(fout, " %s\n", layout);
237  fprintf(fout,"%s","% --- header end --------------------------------------------------------------------------------------------------\n");
238  return 0;
239 }
240 
241 int rlReport::includeCSV(const char *filename, int use_first_row_as_title, const char *legend, char delimitor)
242 {
243  int x,y,xmax,ymax;
244  rlSpreadsheetTable t(delimitor);
245  if(fout == NULL) return -1;
246  if(filename == NULL) return -1;
247  if(t.read(filename) < 0)
248  {
249  printf ("rlReport::includeCSV() ERROR: Could not find %s\n",filename);
250  ::printf("rlReport::includeCSV() ERROR: Could not find %s\n",filename);
251  return -1;
252  }
253 
254  x = xmax = 1;
255  while(t.exists(x,1) && xmax < 1024) xmax = x++;
256  y = ymax = 1;
257  while(t.exists(1,y) && ymax < 256*256) ymax = y++;
258 
259  printf("\\begin{center}\n");
260  printf("\\begin{longtable}");
261 
262  x = 1;
263  printf("{");
264  while(x <= xmax) { printf(" | l"); x++; }
265  printf(" | }\n");
266  printf(" \\hline\n");
267  for(y=1; y<=ymax; y++)
268  {
269  if(y==1 && use_first_row_as_title) printf(" \\rowcolor{gray}\n");;
270  printf(" ");
271  for(x=1; x<=xmax; x++)
272  {
273  printf("%s ", t.text(x,y));
274  if(x < xmax) printf("& ");
275  }
276  printf(" \\\\\n");
277  if(y < ymax) printf(" \\hline\n");
278  }
279  printf(" \\hline\n");
280  if(legend != NULL) printf(" \\caption{%s}\n", legend);
281 
282  printf("\\end{longtable}\n");
283  printf("\\end{center}\n");
284  return 0;
285 }
286 
287 int rlReport::includeImage(const char *filename, const char *legend, float scale)
288 {
289  if(fout == NULL) return -1;
290  if(filename == NULL) return -1;
291  if(legend == NULL) legend = "";
292  printf("\\img{%.1f}{%s}{%s}\n", scale, filename, legend);
293  return 0;
294 }
295 
296 int rlReport::spawn(const char *command)
297 {
298  char cmd[rl_PRINTF_LENGTH]; // should be big enough
299 
300  if(fout == NULL) return -1;
301  if(command == NULL) return -1;
302  sprintf(cmd,"%s > %s.temp", command, file.text());
303  system(cmd);
304  sprintf(cmd,"%s.temp", file.text());
305  return include(cmd);
306 }
307 
308 int rlReport::pdflatex(const char *command)
309 {
310  char cmd[rl_PRINTF_LENGTH]; // should be big enough
311  if(command == NULL)
312  {
313  sprintf(cmd,"pdflatex -interaction=nonstopmode %s.tex", file.text());
314  system(cmd); // run pdflatex twice
315  return system(cmd);
316  }
317  else
318  {
319  sprintf(cmd,"%s %s.tex", command, file.text());
320  system(cmd); // run pdflatex twice
321  return system(cmd);
322  }
323 }
324 
const char * text(int column, int row)
const char * text(const char *section, const char *name)
Definition: rlinifile.cpp:208
int read(const char *filename)
int printf(const char *format,...)
Definition: rlreport.cpp:53
rlReport()
Definition: rlreport.cpp:21
int beginDocument()
Definition: rlreport.cpp:67
#define rl_PRINTF_LENGTH
Definition: rldefine.h:71
FILE * fout
Definition: rlreport.h:150
int endDocument()
Definition: rlreport.cpp:72
int includeHeader(const char *documentclass="\ocumentclass[a4paper]{article}", const char *language="\sepackage[english]{babel}", const char *inputenc="\sepackage[utf8]{inputenc}", const char *layout="\etlength{\arindent}{0pt} \etlength{\opmargin}{-50pt} \etlength{\ddsidemargin}{0pt} \etlength{\extwidth}{480pt} \etlength{\extheight}{700pt}")
Definition: rlreport.cpp:157
int spawn(const char *command)
Definition: rlreport.cpp:296
char * text()
Definition: rlstring.cpp:126
virtual ~rlReport()
Definition: rlreport.cpp:26
int open(const char *filename)
Definition: rlreport.cpp:31
int rlvsnprintf(char *text, int len, const char *format, va_list ap)
Definition: rlcutil.cpp:197
rlString file
Definition: rlreport.h:151
int includeImage(const char *filename, const char *legend=NULL, float scale=1.0f)
Definition: rlreport.cpp:287
int include(const char *filename, rlIniFile *ini=NULL)
Definition: rlreport.cpp:77
int pdflatex(const char *command=NULL)
Definition: rlreport.cpp:308
int includeCSV(const char *filename, int use_first_row_as_title=1, const char *legend=NULL, char delimitor='\t')
Definition: rlreport.cpp:241
int exists(int column, int row)
int close()
Definition: rlreport.cpp:45