html_special(): move va_end() call outside the switch and make variables
[elinks.git] / src / util / conv.h
blobd0f4446d1b5aa60a93a266604850894083573d4d
2 #ifndef EL__UTIL_CONV_H
3 #define EL__UTIL_CONV_H
5 #include "util/string.h"
6 #include "util/time.h" /* timeval_T types */
8 static inline int
9 is_safe_in_shell(unsigned char c)
11 /* Note: '-' is often used to indicate a command-line option and thus
12 * is not always safe. */
13 return isasciialnum(c)
14 || c == '@' || c == '+' || c == '.'
15 || c == '/' || c == ':' || c == '_';
19 long strtolx(unsigned char *, unsigned char **);
21 /* Convert a decimal number to hexadecimal (lowercase) (0 <= a <= 15). */
22 static inline unsigned char
23 hx(register int a)
25 return a >= 10 ? a + 'a' - 10 : a + '0';
28 /* Convert a decimal number to hexadecimal (uppercase) (0 <= a <= 15). */
29 static inline unsigned char
30 Hx(register int a)
32 return a >= 10 ? a + 'A' - 10 : a + '0';
35 /* Convert an hexadecimal char ([0-9][a-z][A-Z]) to
36 * its decimal value (0 <= result <= 15)
37 * returns -1 if parameter is not an hexadecimal char. */
38 static inline int
39 unhx(register unsigned char a)
41 if (isdigit(a)) return a - '0';
42 if (a >= 'a' && a <= 'f') return a - 'a' + 10;
43 if (a >= 'A' && a <= 'F') return a - 'A' + 10;
44 return -1;
47 /* These use granular allocation stuff. */
48 struct string *add_long_to_string(struct string *string, long number);
49 struct string *add_knum_to_string(struct string *string, long number);
50 struct string *add_xnum_to_string(struct string *string, off_t number);
51 struct string *add_duration_to_string(struct string *string, long seconds);
52 struct string *add_timeval_to_string(struct string *string, timeval_T *timeval);
54 #ifdef HAVE_STRFTIME
55 /* Uses strftime() to add @fmt time format to @string. If @time is NULL
56 * time(NULL) will be used. */
57 struct string *add_date_to_string(struct string *string, unsigned char *format, time_t *time);
58 #endif
61 /* Encoders: */
62 /* They encode and add to the string. This way we don't need to first allocate
63 * and encode a temporary string, add it and then free it. Can be used as
64 * backends for encoder. */
66 /* A simple generic encoder. Should maybe take @replaceable as a string so we
67 * could also use it for adding shell safe strings. */
68 struct string *
69 add_string_replace(struct string *string, unsigned char *src, int len,
70 unsigned char replaceable, unsigned char replacement);
72 #define add_optname_to_string(str, src, len) \
73 add_string_replace(str, src, len, '.', '*')
75 /* Maybe a bad name but it is actually the real name, but you may also think of
76 * it as adding the decoded option name. */
77 #define add_real_optname_to_string(str, src, len) \
78 add_string_replace(str, src, len, '*', '.')
80 /* Convert reserved chars to html &#xx */
81 struct string *add_html_to_string(struct string *string, unsigned char *html, int htmllen);
83 /* Escapes \ and " with a \ */
84 struct string *add_quoted_to_string(struct string *string, unsigned char *q, int qlen);
86 /* Adds ', |len| bytes of |src| with all single-quotes converted to '\'',
87 * and ' to |string|. */
88 struct string *add_shell_quoted_to_string(struct string *string,
89 unsigned char *src, int len);
91 /* Escapes non shell safe chars with '_'. */
92 struct string *add_shell_safe_to_string(struct string *string, unsigned char *cmd, int cmdlen);
95 /* These are fast functions to convert integers to string, or to hexadecimal string. */
97 int elinks_ulongcat(unsigned char *s, unsigned int *slen, unsigned long number,
98 unsigned int width, unsigned char fillchar, unsigned int base,
99 unsigned int upper);
101 int elinks_longcat(unsigned char *s, unsigned int *slen, long number,
102 unsigned int width, unsigned char fillchar, unsigned int base,
103 unsigned int upper);
105 /* Type casting is enforced, to shorten calls. --Zas */
106 /* unsigned long to decimal string */
107 #define ulongcat(s, slen, number, width, fillchar) \
108 elinks_ulongcat((unsigned char *) (s), \
109 (unsigned int *) (slen), \
110 (unsigned long) (number), \
111 (unsigned int) (width), \
112 (unsigned char) (fillchar), \
113 (unsigned int) 10, \
114 (unsigned int) 0)
116 /* signed long to decimal string */
117 #define longcat(s, slen, number, width, fillchar) \
118 elinks_longcat((unsigned char *) (s), \
119 (unsigned int *) (slen), \
120 (long) (number), \
121 (unsigned int) (width), \
122 (unsigned char) (fillchar), \
123 (unsigned int) 10, \
124 (unsigned int) 0)
126 /* unsigned long to hexadecimal string */
127 #define ulonghexcat(s, slen, number, width, fillchar, upper) \
128 elinks_ulongcat((unsigned char *) (s), \
129 (unsigned int *) (slen), \
130 (unsigned long) (number), \
131 (unsigned int) (width), \
132 (unsigned char) (fillchar), \
133 (unsigned int) 16, \
134 (unsigned int) (upper))
137 /* XXX: Compatibility only. Remove these at some time. --Zas */
138 #define snprint(str, len, num) ulongcat(str, NULL, num, len, 0);
139 #define snzprint(str, len, num) longcat(str, NULL, num, len, 0);
141 /* Return 0 if starting with jan, 11 for dec, -1 for failure.
142 * @month must be a lowercased string. */
143 int month2num(const unsigned char *month);
145 #include <string.h>
147 /* Trim starting and ending chars equal to @c in string @s.
148 * If @len != NULL, it stores new string length in pointed integer.
149 * It returns @s for convenience. */
150 static inline unsigned char *
151 trim_chars(unsigned char *s, unsigned char c, int *len)
153 int l = strlen(s);
154 unsigned char *p = s;
156 while (*p == c) p++, l--;
157 while (l && p[l - 1] == c) p[--l] = '\0';
159 memmove(s, p, l + 1);
160 if (len) *len = l;
162 return s;
165 /* Convert uppercase letters in @string with the given @length to lowercase. */
166 static inline void
167 convert_to_lowercase(unsigned char *string, int length)
169 for (length--; length >= 0; length--)
170 if (isupper(string[length]))
171 string[length] = tolower(string[length]);
174 /* This function drops control chars, nbsp char and limit the number of consecutive
175 * space chars to one. It modifies its argument. */
176 void clr_spaces(unsigned char *str);
178 /* Replace invalid chars in @title with ' ' and trim all starting/ending
179 * spaces. */
180 void sanitize_title(unsigned char *title);
182 /* Returns 0 if @url contains invalid chars, 1 if ok.
183 * It trims starting/ending spaces. */
184 int sanitize_url(unsigned char *url);
186 #endif