From 175bb0344ceccf7f6779c4bc957b86dd895faa07 Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Thu, 26 Aug 2010 17:29:02 +0000 Subject: Move directory listing HTML generation functions into separate module. svn path=/trunk/netsurf/; revision=10715 --- Makefile.sources | 2 +- content/dirlist.c | 378 +++++++++++++++++++++++++++++++++++++++++++++++++++++ content/dirlist.h | 47 +++++++ render/directory.c | 371 +--------------------------------------------------- 4 files changed, 427 insertions(+), 371 deletions(-) create mode 100644 content/dirlist.c create mode 100644 content/dirlist.h diff --git a/Makefile.sources b/Makefile.sources index 57a2d370b..29898f535 100644 --- a/Makefile.sources +++ b/Makefile.sources @@ -5,7 +5,7 @@ # for each build. # -S_CONTENT := content.c fetch.c hlcache.c llcache.c urldb.c \ +S_CONTENT := content.c dirlist.c fetch.c hlcache.c llcache.c urldb.c \ fetchers/fetch_curl.c fetchers/fetch_data.c S_CSS := css.c dump.c internal.c select.c utils.c S_RENDER := box.c box_construct.c box_normalise.c directory.c favicon.c \ diff --git a/content/dirlist.c b/content/dirlist.c new file mode 100644 index 000000000..bbe120296 --- /dev/null +++ b/content/dirlist.c @@ -0,0 +1,378 @@ +/* + * Copyright 2010 Michael Drake + * + * 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 . + */ + +/** \file + * Generate HTML content for displaying directory listings (implementation). + */ + +#include +#include +#include +#include +#include "content/dirlist.h" +#include "utils/messages.h" + +static const char footer[] = "\n\n\n"; + +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() + */ + +const char* dirlist_generate_top(void) +{ + return "\n" + "\n" + "\n" + "%s\n" + "\n" + "\n" + "

%s

\n", + title, 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(char *parent, char *buffer, int buffer_length) +{ + int error = snprintf(buffer, buffer_length, + "

%s

", + 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, + "
\n" + "%s " + "%s " + "%s" + " " + "%s " + "%s\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 type 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, char *url, char *name, + char *type, 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, + "" + "%s " + "%s " + "%s" + "%s " + "%s " + "%s\n", + url, even ? "even" : "odd", + directory ? "dir" : "file", + name, type, 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() + */ + +const char* dirlist_generate_bottom(void) +{ + return "
\n" + "\n" + "\n"; +} + + +/** + * 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/dirlist.h b/content/dirlist.h new file mode 100644 index 000000000..9b369144e --- /dev/null +++ b/content/dirlist.h @@ -0,0 +1,47 @@ +/* + * Copyright 2010 Michael Drake + * + * 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 . + */ + +/** \file + * Generate HTML content for displaying directory listings (interface). + * + * These functions should in general be called via the content interface. + */ + +#ifndef _NETSURF_CONTENT_DIRLIST_H_ +#define _NETSURF_CONTENT_DIRLIST_H_ + +#include + +#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 + +const char* dirlist_generate_top(void); +bool dirlist_generate_hide_columns(int flags, char *buffer, int buffer_length); +bool dirlist_generate_title(char *title, char *buffer, int buffer_length); +bool dirlist_generate_parent_link(char *parent, char *buffer, + int buffer_length); +bool dirlist_generate_headings(char *buffer, int buffer_length); +bool dirlist_generate_row(bool even, bool directory, char *url, char *name, + char *type, long long size, char *date, char *time, + char *buffer, int buffer_length); +const char* dirlist_generate_bottom(void); + +#endif diff --git a/render/directory.c b/render/directory.c index ac27d9cbb..cdffdd0f1 100644 --- a/render/directory.c +++ b/render/directory.c @@ -31,6 +31,7 @@ #include #include #include "content/content_protected.h" +#include "content/dirlist.h" #include "content/fetch.h" #include "render/directory.h" #include "render/html.h" @@ -40,376 +41,6 @@ #define MAX_LENGTH 2048 -#define NO_NAME_COLUMN 1 -#define NO_TYPE_COLUMN 1 << 1 -#define NO_SIZE_COLUMN 1 << 2 -#define NO_DATE_COLUMN 1 << 3 -#define NO_TIME_COLUMN 1 << 4 - -static const char footer[] = "\n\n\n"; - -static const char* dirlist_generate_top(void); -static bool dirlist_generate_hide_columns(int flags, char *buffer, - int buffer_length); -static bool dirlist_generate_title(char *title, char *buffer, - int buffer_length); -static bool dirlist_generate_parent_link(char *parent, char *buffer, - int buffer_length); -static bool dirlist_generate_headings(char *buffer, int buffer_length); -static bool dirlist_generate_row(bool even, bool directory, char *url, - char *name, char *type, long long size, char *date, - char *time, char *buffer, int buffer_length); -static const char* dirlist_generate_bottom(void); - -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 directroy 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() - */ - -const char* dirlist_generate_top(void) -{ - return "\n" - "\n" - "\n" - "%s\n" - "\n" - "\n" - "

%s

\n", - title, 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(char *parent, char *buffer, int buffer_length) -{ - int error = snprintf(buffer, buffer_length, - "

%s

", - 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, - "
\n" - "%s " - "%s " - "%s" - " " - "%s " - "%s\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 type 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, char *url, char *name, - char *type, 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, - "" - "%s " - "%s " - "%s" - "%s " - "%s " - "%s\n", - url, even ? "even" : "odd", - directory ? "dir" : "file", - name, type, 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 directroy 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() - */ - -const char* dirlist_generate_bottom(void) -{ - return "
\n" - "\n" - "\n"; -} - - -/** - * 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)]; -} - - bool directory_create(struct content *c, const struct http_parameter *params) { if (!html_create(c, params)) /* html_create() must have broadcast MSG_ERROR already, so we -- cgit v1.2.3