rllib  1
rlhtmldir.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rlhtmldir.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 "rlhtmldir.h"
17 #include "rlspawn.h"
18 
20  :rlHtml()
21 {
23  pattern = "*";
24  initialPath = "";
25  currentPath = "";
26  recursive = 0;
28  textHome = "Home:";
29  textPath = "Path:";
30  textrlHtmlDirCSS = "rlhtml.css";
31 }
32 
34 {
35 }
36 
37 int rlHtmlDir::cd(const char *dirIn, int is_initial_dir)
38 {
39  rlString dirCopy;
40  dirCopy = dirIn;
41  const char *dir = dirCopy.text();
42  if(*dir == '.')
43  {
44  if(dir[1] == '.')
45  {
46  if(strlen( initialPath.text() ) < strlen( currentPath.text() ))
47  {
48  char *cptr = strrchr(currentPath.text(),'/');
49  if(cptr != NULL)
50  {
51  if(cptr != currentPath.text()) *cptr = '\0'; // cd ..
52  return 0;
53  }
54  }
55  }
56  return -1;
57  }
58  else if(*dir == '/') // cd /absolute/path
59  {
60  currentPath = dir;
61  if(is_initial_dir) initialPath = dir;
62  }
63  else if(*dir == '~')
64  {
65  currentPath = initialPath; // cd home
66  }
67  else
68  {
69  currentPath += "/";
70  currentPath += dir; // cd into subdirectory
71  }
72  return 0;
73 }
74 
75 const char * rlHtmlDir::getHtml(int generate_html_header_and_trailer)
76 {
77  html = "";
78  if(generate_html_header_and_trailer)
79  {
80  html = htmlHeader();
81  }
82 
83  html += startHtml.text();
84  html += "<pre class=\"rlhtmldirpre\">\n";
85 
86  rlSpawn spawn;
87  rlString cmd;
88  const char *line;
89  rlString file;
90  rlString htmlline;
91  char *cptr;
92 #ifdef RLWIN32
93  if(recursive) cmd.printf("rlfind \"%s\" -name \"%s\"", currentPath.text(), pattern.text());
94  else cmd.printf("rlfind \"%s\"", currentPath.text());
95  //else cmd.printf("rlfind \"%s\" -only \"%s\"", currentPath.text(), pattern.text());
96 #else
97  if(recursive) cmd.printf("sh -c \"rlfind '%s' -name '%s' | sort\"", currentPath.text(), pattern.text());
98  else cmd.printf("sh -c \"rlfind '%s' -only '%s' | sort\"", currentPath.text(), pattern.text());
99 #endif
100  if(1) //recursive == 0)
101  {
102  htmlline.printf("%s <a class=\"rlhtmldirhome\" href=\"ftp://rlhtmldirhome%s\">%s</a>\n", textHome.text(), initialPath.text(), initialPath.text());
103  html += htmlline.text();
104  htmlline.printf("%s %s\n", textPath.text(), currentPath.text());
105  html += htmlline.text();
106  if(strcmp(initialPath.text(),currentPath.text()) != 0)
107  {
108  htmlline.printf("<a class=\"rlhtmldirup\" href=\"ftp://rlhtmldirup/dir/up\">..</a>\n");
109  html += htmlline.text();
110  }
111  }
112  spawn.spawn(cmd.text());
113  while(1)
114  {
115  line = spawn.readLine();
116  // printf("line=%s", line);
117  if(line == NULL) break;
118  if(strncmp(line,"#EOF#",5) == 0) break;
119  file = line;
120  file.removeNewline();
121  cptr = strstr(file.text()," [");
122  if(cptr != NULL)
123  {
124  if (strcmp(cptr," [d]") == 0)
125  {
126  // list = 1 (list files and directories)
127  // list = 2 (list directories only)
128  // list = 3 (list files only)
129  if(list_what != list_files_only)
130  {
131  if(cptr != NULL) *cptr = '\0';
132  if(strcmp(initialPath.text(),file.text()) != 0 && strcmp(currentPath.text(),file.text()) != 0 )
133  {
134  cptr = strrchr(file.text(),'/');
135  if(cptr == NULL) cptr = file.text();
136  cptr++;
137  if(recursive) htmlline.printf("+<a class=\"rlhtmldir\" href=\"ftp://rlhtmldir%s\">%s</a>\n", file.text(), file.text());
138  else htmlline.printf("+<a class=\"rlhtmldir\" href=\"ftp://rlhtmldir%s\">%s</a>\n", file.text(), cptr);
139  html += htmlline.text();
140  }
141  }
142  }
143  else if(strcmp(cptr," [f]") == 0)
144  {
145  // list = 1 (list files and directories)
146  // list = 2 (list directories only)
147  // list = 3 (list files only)
149  {
150  if(cptr != NULL) *cptr = '\0';
151  cptr = strrchr(file.text(),'/');
152  if(cptr == NULL) cptr = file.text();
153  cptr++;
154  if(hide_hidden_files == 0 || (hide_hidden_files == 1 && strncmp(cptr,".",1) != 0))
155  {
156  if(recursive) htmlline.printf(" <a class=\"rlhtmlfile\" href=\"ftp://rlhtmlfile%s\">%s</a>\n", file.text(), file.text());
157  else htmlline.printf(" <a class=\"rlhtmlfile\" href=\"ftp://rlhtmlfile%s\">%s</a>\n", file.text(), cptr);
158  html += htmlline.text();
159  }
160  }
161  }
162  }
163  }
164 
165  html += "</pre>\n";
166  html += endHtml.text();
167 
168  if(generate_html_header_and_trailer)
169  {
170  html += htmlTrailer();
171  }
172 
173  //printf("HTML=\n%s", html.text());
174  return html.text();
175 }
176 
178 {
179  if(row == NULL) return 0;
180  rlSpawn spawn;
181  rlString cmd;
182  const char *line;
183  rlString file;
184 #ifdef RLWIN32
185  if(recursive) cmd.printf("rlfind \"%s\" -name \"%s\"", currentPath.text(), pattern.text());
186  else cmd.printf("rlfind \"%s\" -only \"%s\"", currentPath.text(), pattern.text());
187 #else
188  if(recursive) cmd.printf("sh -c \"rlfind '%s' -name '%s' | sort\"", currentPath.text(), pattern.text());
189  else cmd.printf("sh -c \"rlfind '%s' -only '%s' | sort\"", currentPath.text(), pattern.text());
190 #endif
191  spawn.spawn(cmd.text());
192  row->clear();
193  int i=1;
194  while(1)
195  {
196  line = spawn.readLine();
197  if(line == NULL) break;
198  file = line;
199  if(hide_hidden_files == 0 || (hide_hidden_files == 1 && strncmp(file.text(),".",1) != 0))
200  {
201  file.removeNewline();
202  row->setText(i++,file.text());
203  //printf("file=%s\n", file.text());
204  }
205  }
206  return i-1;
207 }
208 
rlString textHome
Definition: rlhtmldir.h:60
void setText(int column, const char *text)
const char * htmlTrailer()
Definition: rlhtml.cpp:39
int hide_hidden_files
Definition: rlhtmldir.h:59
rlString pattern
Definition: rlhtmldir.h:56
const char * htmlHeader()
Definition: rlhtml.cpp:29
rlString endHtml
Definition: rlhtmldir.h:65
int cd(const char *dir, int is_initial_dir=0)
Definition: rlhtmldir.cpp:37
char * text()
Definition: rlstring.cpp:126
rlString startHtml
Definition: rlhtmldir.h:64
int getFilenames(rlSpreadsheetRow *row)
Definition: rlhtmldir.cpp:177
rlString textPath
Definition: rlhtmldir.h:61
int list_what
Definition: rlhtmldir.h:58
int removeNewline()
Definition: rlstring.cpp:271
const char * getHtml(int generate_html_header_and_trailer=1)
Definition: rlhtmldir.cpp:75
Definition: rlhtml.h:25
const char * readLine()
Definition: rlspawn.cpp:307
rlString initialPath
Definition: rlhtmldir.h:54
int printf(const char *format,...)
Definition: rlstring.cpp:145
int recursive
Definition: rlhtmldir.h:57
virtual ~rlHtmlDir()
Definition: rlhtmldir.cpp:33
rlString currentPath
Definition: rlhtmldir.h:55
rlString html
Definition: rlhtmldir.h:67
int spawn(const char *command)
Definition: rlspawn.cpp:168
rlString textrlHtmlDirCSS
Definition: rlhtmldir.h:62