]> sjero.net Git - wget/blobdiff - src/convert.c
[svn] Remove K&R support.
[wget] / src / convert.c
index ed567124a2c8d4c67936a292033b244046c72bf9..04beea2455a67f2dd33ceaa46c0ef7e98a40b503 100644 (file)
@@ -31,17 +31,12 @@ so, delete this exception statement from your version.  */
 
 #include <stdio.h>
 #include <stdlib.h>
-#ifdef HAVE_STRING_H
-# include <string.h>
-#else
-# include <strings.h>
-#endif /* HAVE_STRING_H */
+#include <string.h>
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif /* HAVE_UNISTD_H */
 #include <errno.h>
 #include <assert.h>
-#include <sys/types.h>
 
 #include "wget.h"
 #include "convert.h"
@@ -49,6 +44,7 @@ so, delete this exception statement from your version.  */
 #include "recur.h"
 #include "utils.h"
 #include "hash.h"
+#include "ptimer.h"
 
 static struct hash_table *dl_file_url_map;
 struct hash_table *dl_url_file_map;
@@ -57,7 +53,7 @@ struct hash_table *dl_url_file_map;
    conversion after Wget is done.  */
 struct hash_table *downloaded_html_set;
 
-static void convert_links PARAMS ((const char *, struct urlpos *));
+static void convert_links (const char *, struct urlpos *);
 
 /* This function is called when the retrieval is done to convert the
    links that have been downloaded.  It has to be called at the end of
@@ -81,7 +77,7 @@ convert_all_links (void)
   double secs;
   int file_count = 0;
 
-  struct wget_timer *timer = wtimer_new ();
+  struct ptimer *timer = ptimer_new ();
 
   int cnt;
   char **file_array;
@@ -170,20 +166,18 @@ convert_all_links (void)
       free_urlpos (urls);
     }
 
-  wtimer_update (timer);
-  secs = wtimer_read (timer) / 1000;
-  wtimer_delete (timer);
+  secs = ptimer_measure (timer) / 1000;
+  ptimer_destroy (timer);
   logprintf (LOG_VERBOSE, _("Converted %d files in %.*f seconds.\n"),
             file_count, secs < 10 ? 3 : 1, secs);
 }
 
-static void write_backup_file PARAMS ((const char *, downloaded_file_t));
-static const char *replace_attr PARAMS ((const char *, int, FILE *,
-                                        const char *));
-static const char *replace_attr_refresh_hack PARAMS ((const char *, int, FILE *,
-                                                     const char *, int));
-static char *local_quote_string PARAMS ((const char *));
-static char *construct_relative PARAMS ((const char *, const char *));
+static void write_backup_file (const char *, downloaded_file_t);
+static const char *replace_attr (const char *, int, FILE *, const char *);
+static const char *replace_attr_refresh_hack (const char *, int, FILE *,
+                                             const char *, int);
+static char *local_quote_string (const char *);
+static char *construct_relative (const char *, const char *);
 
 /* Change the links in one HTML file.  LINKS is a list of links in the
    document, along with their positions and the desired direction of
@@ -395,6 +389,10 @@ construct_relative (const char *basefile, const char *linkfile)
   return link;
 }
 
+/* Used by write_backup_file to remember which files have been
+   written. */
+static struct hash_table *converted_files;
+
 static void
 write_backup_file (const char *file, downloaded_file_t downloaded_file_return)
 {
@@ -406,7 +404,6 @@ write_backup_file (const char *file, downloaded_file_t downloaded_file_return)
   /* Construct the backup filename as the original name plus ".orig". */
   size_t         filename_len = strlen (file);
   char*          filename_plus_orig_suffix;
-  static struct hash_table *converted_files;
 
   if (downloaded_file_return == FILE_DOWNLOADED_AND_HTML_EXTENSION_ADDED)
     {
@@ -465,8 +462,7 @@ write_backup_file (const char *file, downloaded_file_t downloaded_file_return)
     }
 }
 
-static int find_fragment PARAMS ((const char *, int, const char **,
-                                 const char **));
+static int find_fragment (const char *, int, const char **, const char **);
 
 /* Replace an attribute's original text with NEW_TEXT. */
 
@@ -834,8 +830,10 @@ register_html (const char *url, const char *file)
   string_set_add (downloaded_html_set, file);
 }
 
-/* Cleanup the data structures associated with recursive retrieving
-   (the variables above).  */
+static void downloaded_files_free (void);
+
+/* Cleanup the data structures associated with this file.  */
+
 void
 convert_cleanup (void)
 {
@@ -853,6 +851,9 @@ convert_cleanup (void)
     }
   if (downloaded_html_set)
     string_set_free (downloaded_html_set);
+  downloaded_files_free ();
+  if (converted_files)
+    string_set_free (converted_files);
 }
 \f
 /* Book-keeping code for downloaded files that enables extension
@@ -943,7 +944,7 @@ df_free_mapper (void *key, void *value, void *ignored)
   return 0;
 }
 
-void
+static void
 downloaded_files_free (void)
 {
   if (downloaded_files_hash)
@@ -953,3 +954,75 @@ downloaded_files_free (void)
       downloaded_files_hash = NULL;
     }
 }
+\f
+/* The function returns the pointer to the malloc-ed quoted version of
+   string s.  It will recognize and quote numeric and special graphic
+   entities, as per RFC1866:
+
+   `&' -> `&amp;'
+   `<' -> `&lt;'
+   `>' -> `&gt;'
+   `"' -> `&quot;'
+   SP  -> `&#32;'
+
+   No other entities are recognized or replaced.  */
+char *
+html_quote_string (const char *s)
+{
+  const char *b = s;
+  char *p, *res;
+  int i;
+
+  /* Pass through the string, and count the new size.  */
+  for (i = 0; *s; s++, i++)
+    {
+      if (*s == '&')
+       i += 4;                 /* `amp;' */
+      else if (*s == '<' || *s == '>')
+       i += 3;                 /* `lt;' and `gt;' */
+      else if (*s == '\"')
+       i += 5;                 /* `quot;' */
+      else if (*s == ' ')
+       i += 4;                 /* #32; */
+    }
+  res = (char *)xmalloc (i + 1);
+  s = b;
+  for (p = res; *s; s++)
+    {
+      switch (*s)
+       {
+       case '&':
+         *p++ = '&';
+         *p++ = 'a';
+         *p++ = 'm';
+         *p++ = 'p';
+         *p++ = ';';
+         break;
+       case '<': case '>':
+         *p++ = '&';
+         *p++ = (*s == '<' ? 'l' : 'g');
+         *p++ = 't';
+         *p++ = ';';
+         break;
+       case '\"':
+         *p++ = '&';
+         *p++ = 'q';
+         *p++ = 'u';
+         *p++ = 'o';
+         *p++ = 't';
+         *p++ = ';';
+         break;
+       case ' ':
+         *p++ = '&';
+         *p++ = '#';
+         *p++ = '3';
+         *p++ = '2';
+         *p++ = ';';
+         break;
+       default:
+         *p++ = *s;
+       }
+    }
+  *p = '\0';
+  return res;
+}