📄 utils.c
字号:
read() refuses to read more than 4K from a file at a single chunk anyway. But other Unixes might optimize it better, and it doesn't *hurt* anything, so I'm leaving it. */ /* Normally, we grow SIZE exponentially to make the number of calls to read() and realloc() logarithmic in relation to file size. However, read() can read an amount of data smaller than requested, and it would be unreasonable to double SIZE every time *something* was read. Therefore, we double SIZE only when the length exceeds half of the entire allocated size. */ size <<= 1; fm->content = xrealloc (fm->content, size); } nread = read (fd, fm->content + fm->length, size - fm->length); if (nread > 0) /* Successful read. */ fm->length += nread; else if (nread < 0) /* Error. */ goto lose; else /* EOF */ break; } if (!inhibit_close) close (fd); if (size > fm->length && fm->length != 0) /* Due to exponential growth of fm->content, the allocated region might be much larger than what is actually needed. */ fm->content = xrealloc (fm->content, fm->length); fm->mmap_p = 0; return fm; lose: if (!inhibit_close) close (fd); xfree (fm->content); xfree (fm); return NULL;}/* Release the resources held by FM. Specifically, this calls munmap() or xfree() on fm->content, depending whether mmap or malloc/read were used to read in the file. It also frees the memory needed to hold the FM structure itself. */voidread_file_free (struct file_memory *fm){#ifdef HAVE_MMAP if (fm->mmap_p) { munmap (fm->content, fm->length); } else#endif { xfree (fm->content); } xfree (fm);}/* Free the pointers in a NULL-terminated vector of pointers, then free the pointer itself. */voidfree_vec (char **vec){ if (vec) { char **p = vec; while (*p) xfree (*p++); xfree (vec); }}/* Append vector V2 to vector V1. The function frees V2 and reallocates V1 (thus you may not use the contents of neither pointer after the call). If V1 is NULL, V2 is returned. */char **merge_vecs (char **v1, char **v2){ int i, j; if (!v1) return v2; if (!v2) return v1; if (!*v2) { /* To avoid j == 0 */ xfree (v2); return v1; } /* Count v1. */ for (i = 0; v1[i]; i++); /* Count v2. */ for (j = 0; v2[j]; j++); /* Reallocate v1. */ v1 = (char **)xrealloc (v1, (i + j + 1) * sizeof (char **)); memcpy (v1 + i, v2, (j + 1) * sizeof (char *)); xfree (v2); return v1;}/* A set of simple-minded routines to store strings in a linked list. This used to also be used for searching, but now we have hash tables for that. *//* It's a shame that these simple things like linked lists and hash tables (see hash.c) need to be implemented over and over again. It would be nice to be able to use the routines from glib -- see www.gtk.org for details. However, that would make Wget depend on glib, and I want to avoid dependencies to external libraries for reasons of convenience and portability (I suspect Wget is more portable than anything ever written for Gnome). *//* Append an element to the list. If the list has a huge number of elements, this can get slow because it has to find the list's ending. If you think you have to call slist_append in a loop, think about calling slist_prepend() followed by slist_nreverse(). */slist *slist_append (slist *l, const char *s){ slist *newel = (slist *)xmalloc (sizeof (slist)); slist *beg = l; newel->string = xstrdup (s); newel->next = NULL; if (!l) return newel; /* Find the last element. */ while (l->next) l = l->next; l->next = newel; return beg;}/* Prepend S to the list. Unlike slist_append(), this is O(1). */slist *slist_prepend (slist *l, const char *s){ slist *newel = (slist *)xmalloc (sizeof (slist)); newel->string = xstrdup (s); newel->next = l; return newel;}/* Destructively reverse L. */slist *slist_nreverse (slist *l){ slist *prev = NULL; while (l) { slist *next = l->next; l->next = prev; prev = l; l = next; } return prev;}/* Is there a specific entry in the list? */intslist_contains (slist *l, const char *s){ for (; l; l = l->next) if (!strcmp (l->string, s)) return 1; return 0;}/* Free the whole slist. */voidslist_free (slist *l){ while (l) { slist *n = l->next; xfree (l->string); xfree (l); l = n; }}/* Sometimes it's useful to create "sets" of strings, i.e. special hash tables where you want to store strings as keys and merely query for their existence. Here is a set of utility routines that makes that transparent. */voidstring_set_add (struct hash_table *ht, const char *s){ /* First check whether the set element already exists. If it does, do nothing so that we don't have to free() the old element and then strdup() a new one. */ if (hash_table_contains (ht, s)) return; /* We use "1" as value. It provides us a useful and clear arbitrary value, and it consumes no memory -- the pointers to the same string "1" will be shared by all the key-value pairs in all `set' hash tables. */ hash_table_put (ht, xstrdup (s), "1");}/* Synonym for hash_table_contains... */intstring_set_contains (struct hash_table *ht, const char *s){ return hash_table_contains (ht, s);}static intstring_set_free_mapper (void *key, void *value_ignored, void *arg_ignored){ xfree (key); return 0;}voidstring_set_free (struct hash_table *ht){ hash_table_map (ht, string_set_free_mapper, NULL); hash_table_destroy (ht);}static intfree_keys_and_values_mapper (void *key, void *value, void *arg_ignored){ xfree (key); xfree (value); return 0;}/* Another utility function: call free() on all keys and values of HT. */voidfree_keys_and_values (struct hash_table *ht){ hash_table_map (ht, free_keys_and_values_mapper, NULL);}/* Engine for legible and legible_large_int; add thousand separators to numbers printed in strings. */static char *legible_1 (const char *repr){ static char outbuf[48]; int i, i1, mod; char *outptr; const char *inptr; /* Reset the pointers. */ outptr = outbuf; inptr = repr; /* Ignore the sign for the purpose of adding thousand separators. */ if (*inptr == '-') { *outptr++ = '-'; ++inptr; } /* How many digits before the first separator? */ mod = strlen (inptr) % 3; /* Insert them. */ for (i = 0; i < mod; i++) *outptr++ = inptr[i]; /* Now insert the rest of them, putting separator before every third digit. */ for (i1 = i, i = 0; inptr[i1]; i++, i1++) { if (i % 3 == 0 && i1 != 0) *outptr++ = ','; *outptr++ = inptr[i1]; } /* Zero-terminate the string. */ *outptr = '\0'; return outbuf;}/* Legible -- return a static pointer to the legibly printed long. */char *legible (long l){ char inbuf[24]; /* Print the number into the buffer. */ number_to_string (inbuf, l); return legible_1 (inbuf);}/* Write a string representation of LARGE_INT NUMBER into the provided buffer. The buffer should be able to accept 24 characters, including the terminating zero. It would be dangerous to use sprintf, because the code wouldn't work on a machine with gcc-provided long long support, but without libc support for "%lld". However, such platforms will typically not have snprintf and will use our version, which does support "%lld" where long longs are available. */static voidlarge_int_to_string (char *buffer, LARGE_INT number){ snprintf (buffer, 24, LARGE_INT_FMT, number);}/* The same as legible(), but works on LARGE_INT. */char *legible_large_int (LARGE_INT l){ char inbuf[48]; large_int_to_string (inbuf, l); return legible_1 (inbuf);}/* Count the digits in a (long) integer. */intnumdigit (long number){ int cnt = 1; if (number < 0) { number = -number; ++cnt; } while ((number /= 10) > 0) ++cnt; return cnt;}/* A half-assed implementation of INT_MAX on machines that don't bother to define one. */#ifndef INT_MAX# define INT_MAX ((int) ~((unsigned)1 << 8 * sizeof (int) - 1))#endif#define ONE_DIGIT(figure) *p++ = n / (figure) + '0'#define ONE_DIGIT_ADVANCE(figure) (ONE_DIGIT (figure), n %= (figure))#define DIGITS_1(figure) ONE_DIGIT (figure)#define DIGITS_2(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_1 ((figure) / 10)#define DIGITS_3(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_2 ((figure) / 10)#define DIGITS_4(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_3 ((figure) / 10)#define DIGITS_5(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_4 ((figure) / 10)#define DIGITS_6(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_5 ((figure) / 10)#define DIGITS_7(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_6 ((figure) / 10)#define DIGITS_8(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_7 ((figure) / 10)#define DIGITS_9(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_8 ((figure) / 10)#define DIGITS_10(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_9 ((figure) / 10)/* DIGITS_<11-20> are only used on machines with 64-bit longs. */#define DIGITS_11(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_10 ((figure) / 10)#define DIGITS_12(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_11 ((figure) / 10)#define DIGITS_13(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_12 ((figure) / 10)#define DIGITS_14(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_13 ((figure) / 10)#define DIGITS_15(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_14 ((figure) / 10)#define DIGITS_16(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_15 ((figure) / 10)#define DIGITS_17(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_16 ((figure) / 10)#define DIGITS_18(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_17 ((figure) / 10)#define DIGITS_19(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_18 ((figure) / 10)/* Print NUMBER to BUFFER in base 10. This should be completely equivalent to `sprintf(buffer, "%ld", number)', only much faster. The speedup may make a difference in programs that frequently convert numbers to strings. Some implementations of sprintf, particularly the one in GNU libc, have been known to be extremely slow compared to this function. Return the pointer to the location where the terminating zero was printed. (Equivalent to calling buffer+strlen(buffer) after the function is done.) BUFFER should be big enough to accept as many bytes as you expect the number to take up. On machines with 64-bit longs the maximum needed size is 24 bytes. That includes the digits needed for the largest 64-bit number, the `-' sign in case it's negative, and the terminating '\0'. */char *number_to_string (char *buffer, long number){ char *p = buffer; long n = number;#if (SIZEOF_LONG != 4) && (SIZEOF_LONG != 8) /* We are running in a strange or misconfigured environment. Let sprintf cope with it. */ sprintf (buffer, "%ld", n); p += strlen (buffer);#else /* (SIZEOF_LONG == 4) || (SIZEOF_LONG == 8) */ if (n < 0) { if (n < -INT_MAX) { /* We cannot print a '-' and assign -n to n because -n would overflow. Let sprintf deal with this border case. */ sprintf (buffer, "%ld", n); p += strlen (buffer); return p; } *p++ = '-'; n = -n; } if (n < 10) { DIGITS_1 (1); } else if (n < 100) { DIGITS_2 (10); } else if (n < 1000) { DIGITS_3 (100); } else if (n < 10000) { DIGITS_4 (1000); } else if (n < 100000) { DIGITS_5 (10000); } else if (n < 1000000) { DIGITS_6 (100000); } else if (n < 10000000) { DIGITS_7 (1000000); } else if (n < 100000000) { DIGITS_8 (10000000); } else if (n < 1000000000) { DIGITS_9 (100000000); }#if SIZEOF_LONG == 4 /* ``if (1)'' serves only to preserve editor indentation. */ else if (1) { DIGITS_10 (1000000000); }#else /* SIZEOF_LONG != 4 */ else if (n < 10000000000L) { DIGITS_10 (1000000000L); } else if (n < 100000000000L) { DIGITS_11 (10000000000L); } else if (n < 1000000000000L) { DIGITS_12 (100000000000L); } else if (n < 10000000000000L) { DIGITS_13 (1000000000000L); } else if (n < 100000000000000L) { DIGITS_14 (10000000000000L); } else if (n < 1000000000000000L) { DIGITS_15 (100000000000000L); } else if (n < 10000000000000000L) { DIGITS_16 (1000000000000000L); } else if (n < 100000000000000000L) { DIGITS_17 (10000000000000000L); } else if (n < 1000000000000000000L) { DIGITS_18 (100000000000000000L); } else { DIGITS_19 (1000000000000000000L); }#endif /* SIZEOF_LONG != 4 */ *p = '\0';#endif /* (SIZEOF_LONG == 4) || (SIZEOF_LONG == 8) */ return p;}#undef ONE_DIGIT#undef ONE_DIGIT_ADVANCE#undef DIGITS_1#undef DIGITS_2#undef DIGITS_3#undef DIGITS_4#undef DIGITS_5#undef DIGITS_6#undef DIGITS_7#undef DIGITS_8#undef DIGITS_9#undef DIGITS_10#undef DIGITS_11#undef DIGITS_12#undef DIGITS_13#undef DIGITS_14#undef DIGITS_15#undef DIGITS_16#undef DIGITS_17#undef DIGITS_18#undef DIGITS_19/* Support for timers. */#undef TIMER_WINDOWS#undef TIMER_GETTIMEOFDAY#undef TIMER_TIME/* Depending on the OS and availability of gettimeofday(), one and only one of the above constants will be defined. Virtually all modern Unix systems will define TIMER_GETTIMEOFDAY; Windows will use TIMER_WINDOWS. TIMER_TIME is a catch-all method for non-Windows systems without gettimeofday. #### Perhaps we should also support ftime(), which exists on old BSD 4.2-influenced systems? (It also existed under MS DOS Borland C, if memory serves me.) */#ifdef WINDOWS# define TIMER_WINDOWS#else /* not WINDOWS */# ifdef HAVE_GETTIMEOFDAY# define TIMER_GETTIMEOFDAY# else# define TIMER_TIME# endif#endif /* not WINDOWS */#ifdef TIMER_GETTIMEOFDAYtypedef struct timeval wget_sys_time;#endif#ifdef TIMER_TIMEtypedef time_t wget_sys_time;#endif#ifdef TIMER_WINDOWStypedef ULARGE_INTEGER wget_sys_time;#endifstruct wget_timer { /* The starting point in time which, subtracted from the current time, yields elapsed time. */ wget_sys_time start; /* The most recent elapsed time, calculated by wtimer_elapsed(). Measured in milliseconds. */ double elapsed_last;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -