rllib  1
rlhtml.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rlhtml.cpp - description
3  -------------------
4  begin : Thu Jul 30 2015
5  copyright : (C) Lehrig Software Enigineering
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 "rlhtml.h"
17 #include "rlspawn.h"
18 #include "rlfileload.h"
19 #include "rlcutil.h"
20 
22 {
23 }
24 
26 {
27 }
28 
29 const char * rlHtml::htmlHeader()
30 {
31  return "<html>\n"
32  "<head>\n"
33  " <meta charset=\"utf-8\"> <title>rlHtmlDir</title>\n"
34  " <link rel=\"stylesheet\" type=\"text/css\" href=\"rlhtml.css\">\n"
35  "</head>\n"
36  "<body id=\"rlhtmlbody\">\n";
37 }
38 
39 const char * rlHtml::htmlTrailer()
40 {
41  return "</body>\n"
42  "</html>\n";
43 }
44 
45 int rlHtml::textFile(const char *filename, rlString &text)
46 {
47  struct stat statbuf;
48  rlStat(filename, &statbuf);
49 
50  char tmp[4096];
51  int maxlen = sizeof(tmp);
52  maxlen -= 8;
53  rlFileLoad fl;
54  if(fl.load(filename) < 0) return -1;
55  text = htmlHeader();
56  sprintf(tmp,"<p>File=%s Size=%ldByte, %ldkByte</p><hr>\n", filename, statbuf.st_size, statbuf.st_size/(1024));
57  text += tmp;
58  text += "<pre id=\"rlTextFile\">\n";
59  const char *line = fl.firstLine();
60  while(line != NULL)
61  {
62  const char *source = line;
63  char *dest = &tmp[0];
64  int i = 1;
65  while(*source != '\0' && i<maxlen)
66  {
67  i++;
68  if(*source == '<')
69  {
70  source++;
71  *dest++ = '&';
72  *dest++ = 'l';
73  *dest++ = 't';
74  *dest++ = ';';
75  }
76  else if(*source == '>')
77  {
78  source++;
79  *dest++ = '&';
80  *dest++ = 'g';
81  *dest++ = 't';
82  *dest++ = ';';
83  }
84  else
85  {
86  *dest++ = *source++;
87  }
88  }
89  *dest = '\0';
90  strcat(dest,"\n");
91  text += tmp;
92  line = fl.nextLine();
93  }
94  text += "</pre>\n";
95  text += htmlTrailer();
96  return 0;
97 }
98 
99 
100 int rlHtml::hexdumpFile(const char *filename, rlString &text)
101 {
102  char tmp[4096];
103  int maxlen = sizeof(tmp);
104  maxlen -= 8;
105  rlSpawn sp;
106  rlString cmd;
107  cmd.printf("od -A x -t x1z -v \"%s\"", filename);
108  sp.spawn(cmd.text());
109 
110  text = htmlHeader();
111  text += "<pre id=\"rlHexdumpFile\">\n";
112  const char *line;
113  while((line = sp.readLine()) != NULL)
114  {
115  const char *source = line;
116  char *dest = &tmp[0];
117  int i = 1;
118  while(*source != '\0' && i<maxlen)
119  {
120  i++;
121  if(*source == '<')
122  {
123  source++;
124  *dest++ = '&';
125  *dest++ = 'l';
126  *dest++ = 't';
127  *dest++ = ';';
128  }
129  else if(*source == '>')
130  {
131  source++;
132  *dest++ = '&';
133  *dest++ = 'g';
134  *dest++ = 't';
135  *dest++ = ';';
136  }
137  else
138  {
139  *dest++ = *source++;
140  }
141  }
142  *dest = '\0';
143  text += tmp;
144  }
145  text += "</pre>\n";
146  text += htmlTrailer();
147  return 0;
148 }
149 
const char * htmlTrailer()
Definition: rlhtml.cpp:39
int load(const char *filename)
Definition: rlfileload.cpp:33
const char * htmlHeader()
Definition: rlhtml.cpp:29
char * text()
Definition: rlstring.cpp:126
int hexdumpFile(const char *filename, rlString &text)
Definition: rlhtml.cpp:100
int textFile(const char *filename, rlString &text)
Definition: rlhtml.cpp:45
const char * nextLine()
Definition: rlfileload.cpp:91
const char * firstLine()
Definition: rlfileload.cpp:81
const char * readLine()
Definition: rlspawn.cpp:307
int printf(const char *format,...)
Definition: rlstring.cpp:145
int rlStat(const char *filepath, struct stat *buf)
Definition: rlcutil.cpp:804
virtual ~rlHtml()
Definition: rlhtml.cpp:25
int spawn(const char *command)
Definition: rlspawn.cpp:168
rlHtml()
Definition: rlhtml.cpp:21