summaryrefslogtreecommitdiff
path: root/content/fetchers
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2020-07-03 18:07:21 +0100
committerVincent Sanders <vince@kyllikki.org>2020-07-03 18:07:21 +0100
commit0908925ca67652e4e335c5f8ef4139ca6f60904f (patch)
tree13dc0f6975b3f33b3f451f3efe6374151d05f156 /content/fetchers
parent1697118e227406764d1cfad78a5fcc9febe152be (diff)
downloadnetsurf-0908925ca67652e4e335c5f8ef4139ca6f60904f.tar.gz
netsurf-0908925ca67652e4e335c5f8ef4139ca6f60904f.tar.bz2
move the file fetcher sources into a single directory
Diffstat (limited to 'content/fetchers')
-rw-r--r--content/fetchers/Makefile9
-rw-r--r--content/fetchers/file/Makefile3
-rw-r--r--content/fetchers/file/dirlist.c406
-rw-r--r--content/fetchers/file/dirlist.h50
-rw-r--r--content/fetchers/file/file.c (renamed from content/fetchers/file.c)4
-rw-r--r--content/fetchers/file/file.h (renamed from content/fetchers/file.h)0
6 files changed, 468 insertions, 4 deletions
diff --git a/content/fetchers/Makefile b/content/fetchers/Makefile
index 9c8479320..e87a4e891 100644
--- a/content/fetchers/Makefile
+++ b/content/fetchers/Makefile
@@ -1,10 +1,15 @@
# Content fetchers sources
-S_FETCHERS_YES := data.c file.c about.c resource.c
+S_FETCHERS_YES := data.c about.c resource.c
S_FETCHERS_NO :=
S_FETCHERS_$(NETSURF_USE_CURL) += curl.c
-S_FETCHERS := $(addprefix content/fetchers/,$(S_FETCHERS_YES))
+S_FETCHERS := $(addprefix fetchers/,$(S_FETCHERS_YES))
+
+# File fetcher
+include content/fetchers/file/Makefile
+
+S_FETCHERS += $(addprefix fetchers/file/,$(S_FETCHER_FILE))
# The following files depend on the testament
content/fetchers/about.c: testament $(OBJROOT)/testament.h
diff --git a/content/fetchers/file/Makefile b/content/fetchers/file/Makefile
new file mode 100644
index 000000000..c22400a56
--- /dev/null
+++ b/content/fetchers/file/Makefile
@@ -0,0 +1,3 @@
+# File fetcher sources
+
+S_FETCHER_FILE := dirlist.c file.c
diff --git a/content/fetchers/file/dirlist.c b/content/fetchers/file/dirlist.c
new file mode 100644
index 000000000..d49dc7fe7
--- /dev/null
+++ b/content/fetchers/file/dirlist.c
@@ -0,0 +1,406 @@
+/*
+ * Copyright 2010 Michael Drake <tlsa@netsurf-browser.org>
+ *
+ * This file is part of NetSurf, http://www.netsurf-browser.org/
+ *
+ * NetSurf is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * NetSurf is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/** \file
+ * Generate HTML content for displaying directory listings (implementation).
+ */
+
+#include <stdbool.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "utils/nsurl.h"
+#include "utils/messages.h"
+#include "utils/nscolour.h"
+
+#include "netsurf/types.h"
+#include "netsurf/plot_style.h"
+
+#include "dirlist.h"
+#include "desktop/system_colour.h"
+
+static int dirlist_filesize_calculate(unsigned long *bytesize);
+static int dirlist_filesize_value(unsigned long bytesize);
+static char* dirlist_filesize_unit(unsigned long bytesize);
+
+
+/**
+ * Generates the top part of an HTML directory listing page
+ *
+ * \return Top of directory listing HTML
+ *
+ * This is part of a series of functions. To generate a complete page,
+ * call the following functions in order:
+ *
+ * dirlist_generate_top()
+ * dirlist_generate_hide_columns() -- optional
+ * dirlist_generate_title()
+ * dirlist_generate_parent_link() -- optional
+ * dirlist_generate_headings()
+ * dirlist_generate_row() -- call 'n' times for 'n' rows
+ * dirlist_generate_bottom()
+ */
+
+bool dirlist_generate_top(char *buffer, int buffer_length)
+{
+ int error = snprintf(buffer, buffer_length,
+ "<html>\n"
+ "<head>\n"
+ "<link rel=\"stylesheet\" title=\"Standard\" "
+ "type=\"text/css\" href=\"resource:internal.css\">\n"
+ "<style>\n");
+ if (error < 0 || error >= buffer_length)
+ /* Error or buffer too small */
+ return false;
+ else
+ /* OK */
+ return true;
+
+}
+
+
+/**
+ * Generates the part of an HTML directory listing page that can suppress
+ * particular columns
+ *
+ * \param flags flags for which cols to suppress. 0 to suppress none
+ * \param buffer buffer to fill with generated HTML
+ * \param buffer_length maximum size of buffer
+ * \return true iff buffer filled without error
+ *
+ * This is part of a series of functions. To generate a complete page,
+ * call the following functions in order:
+ *
+ * dirlist_generate_top()
+ * dirlist_generate_hide_columns() -- optional
+ * dirlist_generate_title()
+ * dirlist_generate_parent_link() -- optional
+ * dirlist_generate_headings()
+ * dirlist_generate_row() -- call 'n' times for 'n' rows
+ * dirlist_generate_bottom()
+ */
+
+bool dirlist_generate_hide_columns(int flags, char *buffer, int buffer_length)
+{
+ int error = snprintf(buffer, buffer_length,
+ "%s\n%s\n%s\n%s\n%s\n",
+ (flags & DIRLIST_NO_NAME_COLUMN) ?
+ "span.name { display: none; }\n" : "",
+ (flags & DIRLIST_NO_TYPE_COLUMN) ?
+ "span.type { display: none; }\n" : "",
+ (flags & DIRLIST_NO_SIZE_COLUMN) ?
+ "span.size { display: none; }\n" : "",
+ (flags & DIRLIST_NO_DATE_COLUMN) ?
+ "span.date { display: none; }\n" : "",
+ (flags & DIRLIST_NO_TIME_COLUMN) ?
+ "span.time { display: none; }\n" : "");
+ if (error < 0 || error >= buffer_length)
+ /* Error or buffer too small */
+ return false;
+ else
+ /* OK */
+ return true;
+}
+
+
+/**
+ * Generates the part of an HTML directory listing page that contains the title
+ *
+ * \param title title to use
+ * \param buffer buffer to fill with generated HTML
+ * \param buffer_length maximum size of buffer
+ * \return true iff buffer filled without error
+ *
+ * This is part of a series of functions. To generate a complete page,
+ * call the following functions in order:
+ *
+ * dirlist_generate_top()
+ * dirlist_generate_hide_columns() -- optional
+ * dirlist_generate_title()
+ * dirlist_generate_parent_link() -- optional
+ * dirlist_generate_headings()
+ * dirlist_generate_row() -- call 'n' times for 'n' rows
+ * dirlist_generate_bottom()
+ */
+
+bool dirlist_generate_title(const char *title, char *buffer, int buffer_length)
+{
+ const char *stylesheet;
+ nserror err;
+ int error;
+
+ if (title == NULL)
+ title = "";
+
+ err = nscolour_get_stylesheet(&stylesheet);
+ if (err != NSERROR_OK) {
+ return false;
+ }
+
+ error = snprintf(buffer, buffer_length,
+ "</style>\n"
+ "<title>%s</title>\n"
+ "<style>\n"
+ "html {\n"
+ "\tbackground-color: #%06x;\n"
+ "}\n"
+ "%s"
+ "</style>\n"
+ "</head>\n"
+ "<body id=\"dirlist\" class=\"ns-even-bg ns-even-fg ns-border\">\n"
+ "<h1 class=\"ns-border\">%s</h1>\n",
+ title,
+ colour_rb_swap(nscolours[NSCOLOUR_WIN_ODD_BG]),
+ stylesheet, title);
+ if (error < 0 || error >= buffer_length)
+ /* Error or buffer too small */
+ return false;
+ else
+ /* OK */
+ return true;
+}
+
+
+/**
+ * Generates the part of an HTML directory listing page that links to the parent
+ * directory
+ *
+ * \param parent url of parent directory
+ * \param buffer buffer to fill with generated HTML
+ * \param buffer_length maximum size of buffer
+ * \return true iff buffer filled without error
+ *
+ * This is part of a series of functions. To generate a complete page,
+ * call the following functions in order:
+ *
+ * dirlist_generate_top()
+ * dirlist_generate_hide_columns() -- optional
+ * dirlist_generate_title()
+ * dirlist_generate_parent_link() -- optional
+ * dirlist_generate_headings()
+ * dirlist_generate_row() -- call 'n' times for 'n' rows
+ * dirlist_generate_bottom()
+ */
+
+bool dirlist_generate_parent_link(const char *parent, char *buffer,
+ int buffer_length)
+{
+ int error = snprintf(buffer, buffer_length,
+ "<p><a href=\"%s\">%s</a></p>",
+ parent, messages_get("FileParent"));
+ if (error < 0 || error >= buffer_length)
+ /* Error or buffer too small */
+ return false;
+ else
+ /* OK */
+ return true;
+}
+
+
+/**
+ * Generates the part of an HTML directory listing page that displays the column
+ * headings
+ *
+ * \param buffer buffer to fill with generated HTML
+ * \param buffer_length maximum size of buffer
+ * \return true iff buffer filled without error
+ *
+ * This is part of a series of functions. To generate a complete page,
+ * call the following functions in order:
+ *
+ * dirlist_generate_top()
+ * dirlist_generate_hide_columns() -- optional
+ * dirlist_generate_title()
+ * dirlist_generate_parent_link() -- optional
+ * dirlist_generate_headings()
+ * dirlist_generate_row() -- call 'n' times for 'n' rows
+ * dirlist_generate_bottom()
+ */
+
+bool dirlist_generate_headings(char *buffer, int buffer_length)
+{
+ int error = snprintf(buffer, buffer_length,
+ "<div>\n"
+ "<strong>\n"
+ "\t<span class=\"name\">%s</span>\n"
+ "\t<span class=\"type\">%s</span>\n"
+ "\t<span class=\"size\">%s</span>"
+ "<span class=\"size\"></span>\n"
+ "\t<span class=\"date\">%s</span>\n"
+ "\t<span class=\"time\">%s</span>\n"
+ "</strong>\n",
+ messages_get("FileName"), messages_get("FileType"),
+ messages_get("FileSize"), messages_get("FileDate"),
+ messages_get("FileTime"));
+ if (error < 0 || error >= buffer_length)
+ /* Error or buffer too small */
+ return false;
+ else
+ /* OK */
+ return true;
+}
+
+
+/**
+ * Generates the part of an HTML directory listing page that displays a row
+ * in the directory contents table
+ *
+ * \param even evenness of row number, for alternate row colouring
+ * \param directory whether this row is for a directory (or a file)
+ * \param url url for row entry
+ * \param name name of row entry
+ * \param mimetype MIME type of row entry
+ * \param size size of row entry. If negative, size is left blank
+ * \param date date row entry was last modified
+ * \param time time row entry was last modified
+ * \param buffer buffer to fill with generated HTML
+ * \param buffer_length maximum size of buffer
+ * \return true iff buffer filled without error
+ *
+ * This is part of a series of functions. To generate a complete page,
+ * call the following functions in order:
+ *
+ * dirlist_generate_top()
+ * dirlist_generate_hide_columns() -- optional
+ * dirlist_generate_title()
+ * dirlist_generate_parent_link() -- optional
+ * dirlist_generate_headings()
+ * dirlist_generate_row() -- call 'n' times for 'n' rows
+ * dirlist_generate_bottom()
+ */
+
+bool dirlist_generate_row(bool even, bool directory, nsurl *url, char *name,
+ const char *mimetype, long long size, char *date, char *time,
+ char *buffer, int buffer_length)
+{
+ const char *unit;
+ char size_string[100];
+ int error;
+
+ if (size < 0) {
+ unit = "";
+ strncpy(size_string, "", sizeof size_string);
+ } else {
+ unit = messages_get(dirlist_filesize_unit((unsigned long)size));
+ snprintf(size_string, sizeof size_string, "%d",
+ dirlist_filesize_value((unsigned long)size));
+ }
+
+ error = snprintf(buffer, buffer_length,
+ "<a href=\"%s\" class=\"%s %s\">\n"
+ "\t<span class=\"name ns-border\">%s</span>\n"
+ "\t<span class=\"type ns-border\">%s</span>\n"
+ "\t<span class=\"size ns-border\">%s</span>"
+ "<span class=\"size ns-border\">%s</span>\n"
+ "\t<span class=\"date ns-border\">%s</span>\n"
+ "\t<span class=\"time ns-border\">%s</span>\n"
+ "</a>\n", nsurl_access(url),
+ even ? "even ns-even-bg" : "odd ns-odd-bg",
+ directory ? "dir" : "file",
+ name, mimetype, size_string, unit, date, time);
+ if (error < 0 || error >= buffer_length)
+ /* Error or buffer too small */
+ return false;
+ else
+ /* OK */
+ return true;
+}
+
+
+/**
+ * Generates the bottom part of an HTML directory listing page
+ *
+ * \return Bottom of directory listing HTML
+ *
+ * This is part of a series of functions. To generate a complete page,
+ * call the following functions in order:
+ *
+ * dirlist_generate_top()
+ * dirlist_generate_hide_columns() -- optional
+ * dirlist_generate_title()
+ * dirlist_generate_parent_link() -- optional
+ * dirlist_generate_headings()
+ * dirlist_generate_row() -- call 'n' times for 'n' rows
+ * dirlist_generate_bottom()
+ */
+
+bool dirlist_generate_bottom(char *buffer, int buffer_length)
+{
+ int error = snprintf(buffer, buffer_length,
+ "</div>\n"
+ "</body>\n"
+ "</html>\n");
+ if (error < 0 || error >= buffer_length)
+ /* Error or buffer too small */
+ return false;
+ else
+ /* OK */
+ return true;
+}
+
+
+/**
+ * Obtain display value and units for filesize after conversion to B/kB/MB/GB,
+ * as appropriate.
+ *
+ * \param bytesize file size in bytes, updated to filesize in output units
+ * \return number of times bytesize has been divided by 1024
+ */
+
+int dirlist_filesize_calculate(unsigned long *bytesize)
+{
+ int i = 0;
+ while (*bytesize > 1024 * 4) {
+ *bytesize /= 1024;
+ i++;
+ if (i == 3)
+ break;
+ }
+ return i;
+}
+
+
+/**
+ * Obtain display value for filesize after conversion to B/kB/MB/GB,
+ * as appropriate
+ *
+ * \param bytesize file size in bytes
+ * \return Value to display for file size, in units given by filesize_unit()
+ */
+
+int dirlist_filesize_value(unsigned long bytesize)
+{
+ dirlist_filesize_calculate(&bytesize);
+ return (int)bytesize;
+}
+
+
+/**
+ * Obtain display units for filesize after conversion to B/kB/MB/GB,
+ * as appropriate
+ *
+ * \param bytesize file size in bytes
+ * \return Units to display for file size, for value given by filesize_value()
+ */
+
+char* dirlist_filesize_unit(unsigned long bytesize)
+{
+ const char* units[] = { "Bytes", "kBytes", "MBytes", "GBytes" };
+ return (char*)units[dirlist_filesize_calculate(&bytesize)];
+}
diff --git a/content/fetchers/file/dirlist.h b/content/fetchers/file/dirlist.h
new file mode 100644
index 000000000..3a0d48c1c
--- /dev/null
+++ b/content/fetchers/file/dirlist.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2010 Michael Drake <tlsa@netsurf-browser.org>
+ *
+ * This file is part of NetSurf, http://www.netsurf-browser.org/
+ *
+ * NetSurf is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * NetSurf is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * \file
+ * interface to generate HTML content for displaying directory listings.
+ *
+ * These functions should in general be called via the content interface.
+ */
+
+#ifndef NETSURF_CONTENT_DIRLIST_H_
+#define NETSURF_CONTENT_DIRLIST_H_
+
+#include <stdbool.h>
+
+#define DIRLIST_NO_NAME_COLUMN 1
+#define DIRLIST_NO_TYPE_COLUMN 1 << 1
+#define DIRLIST_NO_SIZE_COLUMN 1 << 2
+#define DIRLIST_NO_DATE_COLUMN 1 << 3
+#define DIRLIST_NO_TIME_COLUMN 1 << 4
+
+struct nsurl;
+
+bool dirlist_generate_top(char *buffer, int buffer_length);
+bool dirlist_generate_hide_columns(int flags, char *buffer, int buffer_length);
+bool dirlist_generate_title(const char *title, char *buffer, int buffer_length);
+bool dirlist_generate_parent_link(const char *parent, char *buffer,
+ int buffer_length);
+bool dirlist_generate_headings(char *buffer, int buffer_length);
+bool dirlist_generate_row(bool even, bool directory, struct nsurl *url,
+ char *name, const char *mimetype, long long size, char *date,
+ char *time, char *buffer, int buffer_length);
+bool dirlist_generate_bottom(char *buffer, int buffer_length);
+
+#endif
diff --git a/content/fetchers/file.c b/content/fetchers/file/file.c
index bf2cc3282..ff3a1b1f6 100644
--- a/content/fetchers/file.c
+++ b/content/fetchers/file/file.c
@@ -57,10 +57,10 @@
#include "netsurf/fetch.h"
#include "desktop/gui_internal.h"
-#include "content/dirlist.h"
#include "content/fetch.h"
#include "content/fetchers.h"
-#include "content/fetchers/file.h"
+#include "dirlist.h"
+#include "file.h"
/* Maximum size of read buffer */
#define FETCH_FILE_MAX_BUF_SIZE (1024 * 1024)
diff --git a/content/fetchers/file.h b/content/fetchers/file/file.h
index 5a5cfe89b..5a5cfe89b 100644
--- a/content/fetchers/file.h
+++ b/content/fetchers/file/file.h