📄 convert.c
字号:
{ hash_table_remove (dl_url_file_map, mapping_url); xfree (mapping_url); xfree (mapping_file); } /* Continue mapping. */ return 0;}/* Remove all associations from various URLs to FILE from dl_url_file_map. */static voiddissociate_urls_from_file (const char *file){ /* Can't use hash_table_iter_* because the table mutates while mapping. */ hash_table_for_each (dl_url_file_map, dissociate_urls_from_file_mapper, (char *) file);}/* Register that URL has been successfully downloaded to FILE. This is used by the link conversion code to convert references to URLs to references to local files. It is also being used to check if a URL has already been downloaded. */voidregister_download (const char *url, const char *file){ char *old_file, *old_url; ENSURE_TABLES_EXIST; /* With some forms of retrieval, it is possible, although not likely or particularly desirable. If both are downloaded, the second download will override the first one. When that happens, dissociate the old file name from the URL. */ if (hash_table_get_pair (dl_file_url_map, file, &old_file, &old_url)) { if (0 == strcmp (url, old_url)) /* We have somehow managed to download the same URL twice. Nothing to do. */ return; if (match_except_index (url, old_url) && !hash_table_contains (dl_url_file_map, url)) /* The two URLs differ only in the "index.html" ending. For example, one is "http://www.server.com/", and the other is "http://www.server.com/index.html". Don't remove the old one, just add the new one as a non-canonical entry. */ goto url_only; hash_table_remove (dl_file_url_map, file); xfree (old_file); xfree (old_url); /* Remove all the URLs that point to this file. Yes, there can be more than one such URL, because we store redirections as multiple entries in dl_url_file_map. For example, if URL1 redirects to URL2 which gets downloaded to FILE, we map both URL1 and URL2 to FILE in dl_url_file_map. (dl_file_url_map only points to URL2.) When another URL gets loaded to FILE, we want both URL1 and URL2 dissociated from it. This is a relatively expensive operation because it performs a linear search of the whole hash table, but it should be called very rarely, only when two URLs resolve to the same file name, *and* the "<file>.1" extensions are turned off. In other words, almost never. */ dissociate_urls_from_file (file); } hash_table_put (dl_file_url_map, xstrdup (file), xstrdup (url)); url_only: /* A URL->FILE mapping is not possible without a FILE->URL mapping. If the latter were present, it should have been removed by the above `if'. So we could write: assert (!hash_table_contains (dl_url_file_map, url)); The above is correct when running in recursive mode where the same URL always resolves to the same file. But if you do something like: wget URL URL then the first URL will resolve to "FILE", and the other to "FILE.1". In that case, FILE.1 will not be found in dl_file_url_map, but URL will still point to FILE in dl_url_file_map. */ if (hash_table_get_pair (dl_url_file_map, url, &old_url, &old_file)) { hash_table_remove (dl_url_file_map, url); xfree (old_url); xfree (old_file); } hash_table_put (dl_url_file_map, xstrdup (url), xstrdup (file));}/* Register that FROM has been redirected to TO. This assumes that TO is successfully downloaded and already registered using register_download() above. */voidregister_redirection (const char *from, const char *to){ char *file; ENSURE_TABLES_EXIST; file = hash_table_get (dl_url_file_map, to); assert (file != NULL); if (!hash_table_contains (dl_url_file_map, from)) hash_table_put (dl_url_file_map, xstrdup (from), xstrdup (file));}/* Register that the file has been deleted. */voidregister_delete_file (const char *file){ char *old_url, *old_file; ENSURE_TABLES_EXIST; if (!hash_table_get_pair (dl_file_url_map, file, &old_file, &old_url)) return; hash_table_remove (dl_file_url_map, file); xfree (old_file); xfree (old_url); dissociate_urls_from_file (file);}/* Register that FILE is an HTML file that has been downloaded. */voidregister_html (const char *url, const char *file){ if (!downloaded_html_set) downloaded_html_set = make_string_hash_table (0); string_set_add (downloaded_html_set, file);}static void downloaded_files_free (void);/* Cleanup the data structures associated with this file. */voidconvert_cleanup (void){ if (dl_file_url_map) { free_keys_and_values (dl_file_url_map); hash_table_destroy (dl_file_url_map); dl_file_url_map = NULL; } if (dl_url_file_map) { free_keys_and_values (dl_url_file_map); hash_table_destroy (dl_url_file_map); dl_url_file_map = NULL; } if (downloaded_html_set) string_set_free (downloaded_html_set); downloaded_files_free (); if (converted_files) string_set_free (converted_files);}/* Book-keeping code for downloaded files that enables extension hacks. *//* This table should really be merged with dl_file_url_map and downloaded_html_files. This was originally a list, but I changed it to a hash table beause it was actually taking a lot of time to find things in it. */static struct hash_table *downloaded_files_hash;/* We're storing "modes" of type downloaded_file_t in the hash table. However, our hash tables only accept pointers for keys and values. So when we need a pointer, we use the address of a downloaded_file_t variable of static storage. */ static downloaded_file_t *downloaded_mode_to_ptr (downloaded_file_t mode){ static downloaded_file_t v1 = FILE_NOT_ALREADY_DOWNLOADED, v2 = FILE_DOWNLOADED_NORMALLY, v3 = FILE_DOWNLOADED_AND_HTML_EXTENSION_ADDED, v4 = CHECK_FOR_FILE; switch (mode) { case FILE_NOT_ALREADY_DOWNLOADED: return &v1; case FILE_DOWNLOADED_NORMALLY: return &v2; case FILE_DOWNLOADED_AND_HTML_EXTENSION_ADDED: return &v3; case CHECK_FOR_FILE: return &v4; } return NULL;}/* Remembers which files have been downloaded. In the standard case, should be called with mode == FILE_DOWNLOADED_NORMALLY for each file we actually download successfully (i.e. not for ones we have failures on or that we skip due to -N). When we've downloaded a file and tacked on a ".html" extension due to -E, call this function with FILE_DOWNLOADED_AND_HTML_EXTENSION_ADDED rather than FILE_DOWNLOADED_NORMALLY. If you just want to check if a file has been previously added without adding it, call with mode == CHECK_FOR_FILE. Please be sure to call this function with local filenames, not remote URLs. */downloaded_file_tdownloaded_file (downloaded_file_t mode, const char *file){ downloaded_file_t *ptr; if (mode == CHECK_FOR_FILE) { if (!downloaded_files_hash) return FILE_NOT_ALREADY_DOWNLOADED; ptr = hash_table_get (downloaded_files_hash, file); if (!ptr) return FILE_NOT_ALREADY_DOWNLOADED; return *ptr; } if (!downloaded_files_hash) downloaded_files_hash = make_string_hash_table (0); ptr = hash_table_get (downloaded_files_hash, file); if (ptr) return *ptr; ptr = downloaded_mode_to_ptr (mode); hash_table_put (downloaded_files_hash, xstrdup (file), ptr); return FILE_NOT_ALREADY_DOWNLOADED;}static voiddownloaded_files_free (void){ if (downloaded_files_hash) { hash_table_iterator iter; for (hash_table_iterate (downloaded_files_hash, &iter); hash_table_iter_next (&iter); ) xfree (iter.key); hash_table_destroy (downloaded_files_hash); downloaded_files_hash = NULL; }}/* 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: `&' -> `&' `<' -> `<' `>' -> `>' `"' -> `"' SP -> ` ' 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 = 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;}/* * vim: et ts=2 sw=2 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -