2007-07-27 05:35:13 -04:00
|
|
|
/** Conversion functions
|
|
|
|
* @file */
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
|
|
|
#include "intl/charsets.h" /* NBSP_CHAR */
|
|
|
|
#include "util/conv.h"
|
|
|
|
#include "util/error.h"
|
|
|
|
#include "util/string.h"
|
|
|
|
#include "util/time.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-07-27 05:35:13 -04:00
|
|
|
/** This function takes string @a s and stores the @a number (of a
|
|
|
|
* result width @a width) in string format there, starting at position
|
|
|
|
* [*@a slen]. If the number would take more space than @a width, it
|
|
|
|
* is truncated and only the _last_ digits of it are inserted to the
|
|
|
|
* string. If the number takes less space than @a width, it is padded
|
|
|
|
* by @a fillchar from left.
|
|
|
|
* @a base defined which base should be used (10, 16, 8, 2, ...)
|
|
|
|
* @a upper selects either hexa uppercased chars or lowercased chars.
|
2005-09-15 09:58:31 -04:00
|
|
|
*
|
2007-07-27 05:35:13 -04:00
|
|
|
* A NUL char is always added at the end of the string. @a s must point
|
|
|
|
* to a sufficiently large memory space, at least *@a slen + @a width + 1.
|
2005-09-15 09:58:31 -04:00
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
*
|
2007-07-27 05:35:13 -04:00
|
|
|
* @code
|
2005-09-15 09:58:31 -04:00
|
|
|
* elinks_ulongcat(s, NULL, 12345, 4, 0, 10, 0) : s = "2345"
|
|
|
|
* elinks_ulongcat(s, NULL, 255, 4, '*', 16, 1) : s = "**FF"
|
|
|
|
* elinks_ulongcat(s, NULL, 123, 5, '0', 10, 0) : s = "00123"
|
2007-07-27 05:35:13 -04:00
|
|
|
* @endcode
|
2005-09-15 09:58:31 -04:00
|
|
|
*
|
|
|
|
* Note that this function exists to provide a fast and efficient, however
|
|
|
|
* still quite powerful alternative to sprintf(). It is optimized for speed and
|
|
|
|
* is *MUCH* faster than sprintf(). If you can use it, use it ;-). But do not
|
|
|
|
* get too enthusiastic, do not use it in cases where it would break i18n.
|
2007-07-27 05:35:13 -04:00
|
|
|
*
|
|
|
|
* @returns 0 if OK or width needed for the whole number to fit there,
|
|
|
|
* if it had to be truncated. A negative value signs an error. */
|
2009-03-28 14:15:08 -04:00
|
|
|
NONSTATIC_INLINE int
|
2021-01-02 10:20:27 -05:00
|
|
|
elinks_ulongcat(char *s, unsigned int *slen,
|
2010-09-04 08:10:51 -04:00
|
|
|
unsigned long long number, unsigned int width,
|
2005-09-15 09:58:31 -04:00
|
|
|
unsigned char fillchar, unsigned int base,
|
|
|
|
unsigned int upper)
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
static const char unum[]= "0123456789ABCDEF";
|
|
|
|
static const char lnum[]= "0123456789abcdef";
|
|
|
|
const char *to_num = (upper ? unum : lnum);
|
2005-09-15 09:58:31 -04:00
|
|
|
unsigned int start = slen ? *slen : 0;
|
|
|
|
unsigned int nlen = 1; /* '0' is one char, we can't have less. */
|
|
|
|
unsigned int pos = start; /* starting position of the number */
|
2010-09-04 08:10:51 -04:00
|
|
|
unsigned long long q = number;
|
2005-09-15 09:58:31 -04:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (width < 1 || !s || base < 2 || base > 16) return -1;
|
|
|
|
|
|
|
|
/* Count the length of the number in chars. */
|
|
|
|
while (q > (base - 1)) {
|
|
|
|
nlen++;
|
|
|
|
q /= base;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If max. width attained, truncate. */
|
|
|
|
if (nlen > width) {
|
|
|
|
ret = nlen;
|
|
|
|
nlen = width;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slen) *slen += nlen;
|
|
|
|
|
|
|
|
/* Fill left space with fillchar. */
|
|
|
|
if (fillchar) {
|
|
|
|
/* ie. width = 4 nlen = 2 -> pad = 2 */
|
|
|
|
unsigned int pad = width - nlen;
|
|
|
|
|
|
|
|
if (pad > 0) {
|
|
|
|
/* Relocate the start of number. */
|
|
|
|
if (slen) *slen += pad;
|
|
|
|
pos += pad;
|
|
|
|
|
|
|
|
/* Pad. */
|
|
|
|
while (pad > 0) s[--pad + start] = fillchar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s[pos + nlen] = '\0';
|
|
|
|
|
|
|
|
/* Now write number starting from end. */
|
|
|
|
while (nlen > 0) {
|
|
|
|
s[--nlen + pos] = to_num[(number % base)];
|
|
|
|
number /= base;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-27 05:35:13 -04:00
|
|
|
/** Similar to elinks_ulongcat() but for @c long number. */
|
2009-03-28 14:15:08 -04:00
|
|
|
NONSTATIC_INLINE int
|
2021-01-02 10:20:27 -05:00
|
|
|
elinks_longcat(char *s, unsigned int *slen,
|
2010-09-04 08:10:51 -04:00
|
|
|
long long number, unsigned int width,
|
2005-09-15 09:58:31 -04:00
|
|
|
unsigned char fillchar, unsigned int base,
|
|
|
|
unsigned int upper)
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char *p = s;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (number < 0 && width > 0) {
|
|
|
|
if (slen) p[(*slen)++] = '-';
|
|
|
|
else *(p++) = '-';
|
|
|
|
number = -number;
|
|
|
|
width--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return elinks_ulongcat(p, slen, number, width, fillchar, base, upper);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-27 07:14:00 -04:00
|
|
|
/** @relates string */
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *
|
|
|
|
add_long_to_string(struct string *string, long long number)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char buffer[64];
|
2005-09-15 09:58:31 -04:00
|
|
|
int length = 0;
|
|
|
|
int width;
|
|
|
|
|
|
|
|
assert(string);
|
|
|
|
if_assert_failed { return NULL; }
|
|
|
|
|
|
|
|
width = longcat(buffer, &length, number, sizeof(buffer) - 1, 0);
|
|
|
|
if (width < 0 || !length) return NULL;
|
|
|
|
|
|
|
|
return add_bytes_to_string(string, buffer, length);
|
|
|
|
}
|
|
|
|
|
2007-07-27 07:14:00 -04:00
|
|
|
/** @relates string */
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *
|
|
|
|
add_knum_to_string(struct string *string, long long num)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
int ret;
|
2021-01-02 10:20:27 -05:00
|
|
|
char t[64];
|
2005-09-15 09:58:31 -04:00
|
|
|
int tlen = 0;
|
|
|
|
|
|
|
|
if (num && (num / (1024 * 1024)) * (1024 * 1024) == num) {
|
|
|
|
ret = longcat(&t, &tlen, num / (1024 * 1024), sizeof(t) - 2, 0);
|
|
|
|
t[tlen++] = 'M';
|
|
|
|
t[tlen] = '\0';
|
|
|
|
} else if (num && (num / 1024) * 1024 == num) {
|
|
|
|
ret = longcat(&t, &tlen, num / 1024, sizeof(t) - 2, 0);
|
|
|
|
t[tlen++] = 'k';
|
|
|
|
t[tlen] = '\0';
|
|
|
|
} else {
|
|
|
|
ret = longcat(&t, &tlen, num, sizeof(t) - 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0 || !tlen) return NULL;
|
|
|
|
|
|
|
|
add_bytes_to_string(string, t, tlen);
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2007-07-27 07:14:00 -04:00
|
|
|
/** @relates string */
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *
|
|
|
|
add_xnum_to_string(struct string *string, long long xnum)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char suff[3] = "\0i";
|
2005-09-15 09:58:31 -04:00
|
|
|
off_t d = -1;
|
|
|
|
|
|
|
|
/* XXX: I don't completely like the computation of d here. --pasky */
|
|
|
|
/* Mebi (Mi), 2^20 */
|
|
|
|
if (xnum >= 1024 * 1024) {
|
|
|
|
suff[0] = 'M';
|
2005-10-15 21:53:06 -04:00
|
|
|
d = (xnum * (int) 10 / (int) ((int) (1024 * 1024))) % 10;
|
2005-09-15 09:58:31 -04:00
|
|
|
xnum /= 1024*1024;
|
|
|
|
/* Kibi (Ki), 2^10 */
|
|
|
|
} else if (xnum >= 1024) {
|
|
|
|
suff[0] = 'K';
|
2005-10-15 21:53:06 -04:00
|
|
|
d = (xnum * (int) 10 / (int) 1024) % 10;
|
2005-09-15 09:58:31 -04:00
|
|
|
xnum /= 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_long_to_string(string, xnum);
|
|
|
|
|
2012-05-08 07:29:20 -04:00
|
|
|
if (d != -1) {
|
2005-09-15 09:58:31 -04:00
|
|
|
add_char_to_string(string, '.');
|
|
|
|
add_long_to_string(string, d);
|
|
|
|
}
|
|
|
|
add_char_to_string(string, ' ');
|
|
|
|
|
|
|
|
if (suff[0]) add_to_string(string, suff);
|
|
|
|
add_char_to_string(string, 'B');
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2007-07-27 07:14:00 -04:00
|
|
|
/** @relates string */
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *
|
|
|
|
add_duration_to_string(struct string *string, long seconds)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char q[64];
|
2005-09-15 09:58:31 -04:00
|
|
|
int qlen = 0;
|
|
|
|
|
|
|
|
if (seconds < 0) seconds = 0;
|
|
|
|
|
|
|
|
/* Days */
|
|
|
|
if (seconds >= (24 * 3600)) {
|
|
|
|
ulongcat(q, &qlen, (seconds / (24 * 3600)), 5, 0);
|
|
|
|
q[qlen++] = 'd';
|
|
|
|
q[qlen++] = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Hours and minutes */
|
|
|
|
if (seconds >= 3600) {
|
|
|
|
seconds %= (24 * 3600);
|
|
|
|
ulongcat(q, &qlen, (seconds / 3600), 4, 0);
|
|
|
|
q[qlen++] = ':';
|
|
|
|
ulongcat(q, &qlen, ((seconds / 60) % 60), 2, '0');
|
|
|
|
} else {
|
|
|
|
/* Only minutes */
|
|
|
|
ulongcat(q, &qlen, (seconds / 60), 2, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Seconds */
|
|
|
|
q[qlen++] = ':';
|
|
|
|
ulongcat(q, &qlen, (seconds % 60), 2, '0');
|
|
|
|
|
|
|
|
add_to_string(string, q);
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2007-07-27 07:14:00 -04:00
|
|
|
/** @relates string */
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *
|
|
|
|
add_timeval_to_string(struct string *string, timeval_T *timeval)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
return add_duration_to_string(string, timeval_to_seconds(timeval));
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_STRFTIME
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *
|
2021-01-02 10:20:27 -05:00
|
|
|
add_date_to_string(struct string *string, const char *fmt,
|
2007-03-20 03:21:41 -04:00
|
|
|
const time_t *date)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char buffer[MAX_STR_LEN];
|
2005-09-15 09:58:31 -04:00
|
|
|
time_t when_time = date ? *date : time(NULL);
|
|
|
|
struct tm *when_local = localtime(&when_time);
|
|
|
|
|
2018-08-18 14:53:40 -04:00
|
|
|
if (!when_local)
|
|
|
|
return NULL;
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
if (strftime(buffer, sizeof(buffer), fmt, when_local) <= 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return add_to_string(string, buffer);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Encoders and string changers */
|
|
|
|
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *
|
2021-01-02 10:20:27 -05:00
|
|
|
add_string_replace(struct string *string, char *src, int len,
|
2005-09-15 09:58:31 -04:00
|
|
|
unsigned char replaceable, unsigned char replacement)
|
|
|
|
{
|
|
|
|
int oldlength = string->length;
|
|
|
|
|
|
|
|
if (!add_bytes_to_string(string, src, len))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (src = string->source + oldlength; len; len--, src++)
|
|
|
|
if (*src == replaceable)
|
|
|
|
*src = replacement;
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *
|
2021-01-02 10:20:27 -05:00
|
|
|
add_html_to_string(struct string *string, const char *src2, int len)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
const unsigned char *src = (const unsigned char *)src2;
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
for (; len; len--, src++) {
|
2007-03-19 02:32:43 -04:00
|
|
|
if (*src < 0x20
|
2007-03-18 05:10:33 -04:00
|
|
|
|| *src == '<' || *src == '>' || *src == '&'
|
|
|
|
|| *src == '\"' || *src == '\'') {
|
2007-03-18 05:13:38 -04:00
|
|
|
int rollback_length = string->length;
|
|
|
|
|
|
|
|
if (!add_bytes_to_string(string, "&#", 2)
|
2010-09-04 08:10:51 -04:00
|
|
|
|| !add_long_to_string(string, (long long)*src)
|
2007-03-18 05:13:38 -04:00
|
|
|
|| !add_char_to_string(string, ';')) {
|
|
|
|
string->length = rollback_length;
|
2007-03-18 06:36:33 -04:00
|
|
|
string->source[rollback_length] = '\0';
|
2007-03-18 05:13:38 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2007-03-18 05:10:33 -04:00
|
|
|
} else {
|
2007-03-18 05:13:38 -04:00
|
|
|
if (!add_char_to_string(string, *src))
|
|
|
|
return NULL;
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *
|
|
|
|
add_cp_html_to_string(struct string *string, int src_codepage,
|
2021-01-02 10:20:27 -05:00
|
|
|
const char *src, int len)
|
2007-03-19 16:09:49 -04:00
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
const char *const end = src + len;
|
2007-03-19 16:09:49 -04:00
|
|
|
unicode_val_T unicode;
|
|
|
|
|
2008-10-18 06:51:04 -04:00
|
|
|
for (;;) {
|
|
|
|
unicode = cp_to_unicode(src_codepage,
|
2021-01-02 10:20:27 -05:00
|
|
|
(char **) &src, end);
|
2008-10-18 06:51:04 -04:00
|
|
|
if (unicode == UCS_NO_CHAR)
|
|
|
|
break;
|
2007-03-19 16:09:49 -04:00
|
|
|
|
|
|
|
if (unicode < 0x20 || unicode >= 0x7F
|
|
|
|
|| unicode == '<' || unicode == '>' || unicode == '&'
|
|
|
|
|| unicode == '\"' || unicode == '\'') {
|
|
|
|
int rollback_length = string->length;
|
|
|
|
|
|
|
|
if (!add_bytes_to_string(string, "&#", 2)
|
|
|
|
|| !add_long_to_string(string, unicode)
|
|
|
|
|| !add_char_to_string(string, ';')) {
|
|
|
|
string->length = rollback_length;
|
|
|
|
string->source[rollback_length] = '\0';
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!add_char_to_string(string, unicode))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
/* TODO Optimize later --pasky */
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *
|
2021-01-02 10:20:27 -05:00
|
|
|
add_quoted_to_string(struct string *string, const char *src, int len)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
for (; len; len--, src++) {
|
|
|
|
if (isquote(*src) || *src == '\\')
|
|
|
|
add_char_to_string(string, '\\');
|
|
|
|
add_char_to_string(string, *src);
|
|
|
|
}
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *
|
2022-02-21 12:46:35 -05:00
|
|
|
add_shell_quoted_to_string(struct string *string, const char *src, int len)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
add_char_to_string(string, '\'');
|
|
|
|
for (; len; len--, ++src)
|
|
|
|
if (*src == '\'')
|
|
|
|
add_to_string(string, "'\\''");
|
|
|
|
else
|
|
|
|
add_char_to_string(string, *src);
|
|
|
|
add_char_to_string(string, '\'');
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *
|
2022-02-21 12:46:35 -05:00
|
|
|
add_shell_safe_to_string(struct string *string, const char *cmd, int cmdlen)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
int prev_safe = 0;
|
|
|
|
|
|
|
|
for (; cmdlen; cmdlen--, cmd++) {
|
|
|
|
if ((*cmd == '-' && prev_safe) ||
|
|
|
|
(prev_safe = is_safe_in_shell(*cmd))) {
|
|
|
|
add_char_to_string(string, *cmd);
|
|
|
|
} else {
|
|
|
|
/* XXX: Not all programs we might exec are capable of
|
|
|
|
* decoding these. For some, we should just report
|
|
|
|
* an error rather than exec with an encoded string. */
|
|
|
|
add_char_to_string(string, '%');
|
|
|
|
add_char_to_string(string, hx((*cmd & 0xf0) >> 4));
|
|
|
|
add_char_to_string(string, hx(*cmd & 0x0f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long
|
2021-01-02 10:20:27 -05:00
|
|
|
strtolx(char *str, char **end)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
long num;
|
|
|
|
unsigned char postfix;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
num = strtol(str, (char **) end, 10);
|
|
|
|
if (errno) return 0;
|
|
|
|
if (!*end) return num;
|
|
|
|
|
2008-11-01 13:18:06 -04:00
|
|
|
postfix = c_toupper(**end);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (postfix == 'K') {
|
|
|
|
(*end)++;
|
|
|
|
if (num < -INT_MAX / 1024) return -INT_MAX;
|
|
|
|
if (num > INT_MAX / 1024) return INT_MAX;
|
|
|
|
return num * 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (postfix == 'M') {
|
|
|
|
(*end)++;
|
|
|
|
if (num < -INT_MAX / (1024 * 1024)) return -INT_MAX;
|
|
|
|
if (num > INT_MAX / (1024 * 1024)) return INT_MAX;
|
|
|
|
return num * (1024 * 1024);
|
|
|
|
}
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-01-02 10:20:27 -05:00
|
|
|
month2num(const char *str)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2022-01-17 11:07:46 -05:00
|
|
|
char month[3] = { (char)(str[0]|32), (char)(str[1]|32), (char)(str[2]|32) };
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
switch (month[0]) {
|
|
|
|
case 'j': /* jan, jun, jul */
|
|
|
|
if (month[1] == 'a') {
|
|
|
|
if (month[2] == 'n') return 0; /* jan */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (month[1] == 'u') {
|
|
|
|
if (month[2] == 'n') return 5; /* jun */
|
|
|
|
if (month[2] == 'l') return 6; /* jul */
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
case 'm': /* mar, may */
|
|
|
|
if (month[1] == 'a') {
|
|
|
|
if (month[2] == 'r') return 2; /* mar */
|
|
|
|
if (month[2] == 'y') return 4; /* may */
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
case 'a': /* apr, aug */
|
|
|
|
if (month[1] == 'p') {
|
|
|
|
if (month[2] == 'r') return 3; /* apr */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (month[1] == 'u' && month[2] == 'g') return 7; /* aug */
|
|
|
|
return -1;
|
|
|
|
case 's':
|
|
|
|
if (month[1] == 'e' && month[2] == 'p') return 8; /* sep */
|
|
|
|
return -1;
|
|
|
|
case 'o':
|
|
|
|
if (month[1] == 'c' && month[2] == 't') return 9; /* oct */
|
|
|
|
return -1;
|
|
|
|
case 'n':
|
|
|
|
if (month[1] == 'o' && month[2] == 'v') return 10; /* nov */
|
|
|
|
return -1;
|
|
|
|
case 'd':
|
|
|
|
if (month[1] == 'e' && month[2] == 'c') return 11; /* dec */
|
|
|
|
return -1;
|
|
|
|
case 'f':
|
|
|
|
if (month[1] == 'e' && month[2] == 'b') return 1; /* feb */
|
|
|
|
return -1;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-27 05:35:13 -04:00
|
|
|
/** This function drops control chars, nbsp char and limit the number
|
|
|
|
* of consecutive space chars to one. It modifies its argument. */
|
2005-09-15 09:58:31 -04:00
|
|
|
void
|
2021-01-02 10:20:27 -05:00
|
|
|
clr_spaces(char *str2)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
unsigned char *s;
|
2021-01-02 10:20:27 -05:00
|
|
|
unsigned char *str = (unsigned char *)str2;
|
2005-09-15 09:58:31 -04:00
|
|
|
unsigned char *dest = str;
|
|
|
|
|
|
|
|
assert(str);
|
|
|
|
|
|
|
|
for (s = str; *s; s++)
|
|
|
|
if (*s < ' ' || *s == NBSP_CHAR) *s = ' ';
|
|
|
|
|
|
|
|
for (s = str; *s; s++) {
|
|
|
|
if (*s == ' ' && (dest == str || s[1] == ' ' || !s[1]))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
*dest++ = *s;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dest = '\0';
|
|
|
|
}
|
|
|
|
|
2007-07-27 05:35:13 -04:00
|
|
|
/** Replace invalid chars in @a title with ' ' and trim all starting/ending
|
2008-11-16 17:56:18 -05:00
|
|
|
* spaces.
|
|
|
|
*
|
|
|
|
* update_bookmark() assumes this function does not switch translation
|
|
|
|
* tables. */
|
2005-09-15 09:58:31 -04:00
|
|
|
void
|
2021-01-02 10:20:27 -05:00
|
|
|
sanitize_title(char *title)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
int len = strlen(title);
|
|
|
|
|
|
|
|
if (!len) return;
|
|
|
|
|
|
|
|
while (len--) {
|
2021-02-27 03:51:23 -05:00
|
|
|
if ((unsigned char)title[len] < ' ' || title[len] == NBSP_CHAR)
|
2005-09-15 09:58:31 -04:00
|
|
|
title[len] = ' ';
|
|
|
|
}
|
|
|
|
trim_chars(title, ' ', NULL);
|
|
|
|
}
|
|
|
|
|
2007-07-27 05:35:13 -04:00
|
|
|
/** Returns 0 if @a url contains invalid chars, 1 if ok.
|
2005-09-15 09:58:31 -04:00
|
|
|
* It trims starting/ending spaces. */
|
|
|
|
int
|
2021-01-02 10:20:27 -05:00
|
|
|
sanitize_url(char *url)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
int len = strlen(url);
|
|
|
|
|
|
|
|
if (!len) return 1;
|
|
|
|
|
|
|
|
while (len--) {
|
2021-02-27 03:51:23 -05:00
|
|
|
if ((unsigned char)url[len] < ' ')
|
2005-09-15 09:58:31 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
trim_chars(url, ' ', NULL);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-10-18 21:36:00 -04:00
|
|
|
|
|
|
|
int c_tolower(int c) {
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'A': return 'a';
|
|
|
|
case 'B': return 'b';
|
|
|
|
case 'C': return 'c';
|
|
|
|
case 'D': return 'd';
|
|
|
|
case 'E': return 'e';
|
|
|
|
case 'F': return 'f';
|
|
|
|
case 'G': return 'g';
|
|
|
|
case 'H': return 'h';
|
|
|
|
case 'I': return 'i';
|
|
|
|
case 'J': return 'j';
|
|
|
|
case 'K': return 'k';
|
|
|
|
case 'L': return 'l';
|
|
|
|
case 'M': return 'm';
|
|
|
|
case 'N': return 'n';
|
|
|
|
case 'O': return 'o';
|
|
|
|
case 'P': return 'p';
|
|
|
|
case 'Q': return 'q';
|
|
|
|
case 'R': return 'r';
|
|
|
|
case 'S': return 's';
|
|
|
|
case 'T': return 't';
|
|
|
|
case 'U': return 'u';
|
|
|
|
case 'V': return 'v';
|
|
|
|
case 'W': return 'w';
|
|
|
|
case 'X': return 'x';
|
|
|
|
case 'Y': return 'y';
|
|
|
|
case 'Z': return 'z';
|
|
|
|
default: return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int c_toupper(int c) {
|
|
|
|
switch (c) {
|
|
|
|
case 'a': return 'A';
|
|
|
|
case 'b': return 'B';
|
|
|
|
case 'c': return 'C';
|
|
|
|
case 'd': return 'D';
|
|
|
|
case 'e': return 'E';
|
|
|
|
case 'f': return 'F';
|
|
|
|
case 'g': return 'G';
|
|
|
|
case 'h': return 'H';
|
|
|
|
case 'i': return 'I';
|
|
|
|
case 'j': return 'J';
|
|
|
|
case 'k': return 'K';
|
|
|
|
case 'l': return 'L';
|
|
|
|
case 'm': return 'M';
|
|
|
|
case 'n': return 'N';
|
|
|
|
case 'o': return 'O';
|
|
|
|
case 'p': return 'P';
|
|
|
|
case 'q': return 'Q';
|
|
|
|
case 'r': return 'R';
|
|
|
|
case 's': return 'S';
|
|
|
|
case 't': return 'T';
|
|
|
|
case 'u': return 'U';
|
|
|
|
case 'v': return 'V';
|
|
|
|
case 'w': return 'W';
|
|
|
|
case 'x': return 'X';
|
|
|
|
case 'y': return 'Y';
|
|
|
|
case 'z': return 'Z';
|
|
|
|
default: return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int c_isupper (int c)
|
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
|
|
|
case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
|
|
|
|
case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
|
|
|
|
case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
|
|
|
|
case 'Y': case 'Z':
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int c_islower (int c)
|
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
|
|
|
case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
|
|
|
|
case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
|
|
|
|
case 's': case 't': case 'u': case 'v': case 'w': case 'x':
|
|
|
|
case 'y': case 'z':
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|