1034: Initialize l in deflate_read to shut up GCC
[elinks.git] / src / util / conv.h
blob605388bb4ebaca09b495ca1054ae31b60e96ed7a
1 #ifndef EL__UTIL_CONV_H
2 #define EL__UTIL_CONV_H
4 #include "util/string.h"
5 #include "util/time.h" /* timeval_T types */
7 static inline int
8 is_safe_in_shell(unsigned char c)
10 /* Note: '-' is often used to indicate a command-line option and thus
11 * is not always safe. */
12 return isasciialnum(c)
13 || c == '@' || c == '+' || c == '.'
14 || c == '/' || c == ':' || c == '_';
18 long strtolx(unsigned char *, unsigned char **);
20 /** Convert a decimal number to hexadecimal (lowercase) (0 <= @a a <= 15). */
21 static inline unsigned char
22 hx(register int a)
24 return a >= 10 ? a + 'a' - 10 : a + '0';
27 /** Convert a decimal number to hexadecimal (uppercase) (0 <= @a a <= 15). */
28 static inline unsigned char
29 Hx(register int a)
31 return a >= 10 ? a + 'A' - 10 : a + '0';
34 /** Convert an hexadecimal char ([0-9][a-z][A-Z]) to
35 * its decimal value (0 <= result <= 15).
36 * Returns -1 if parameter is not an hexadecimal char. */
37 static inline int
38 unhx(register unsigned char a)
40 if (isdigit(a)) return a - '0';
41 if (a >= 'a' && a <= 'f') return a - 'a' + 10;
42 if (a >= 'A' && a <= 'F') return a - 'A' + 10;
43 return -1;
46 /* These use granular allocation stuff. */
47 struct string *add_long_to_string(struct string *string, long number);
48 struct string *add_knum_to_string(struct string *string, long number);
49 struct string *add_xnum_to_string(struct string *string, off_t number);
50 struct string *add_duration_to_string(struct string *string, long seconds);
51 struct string *add_timeval_to_string(struct string *string, timeval_T *timeval);
53 #ifdef HAVE_STRFTIME
54 /** Uses strftime() to format @a time according to @a format and adds
55 * the result to @a string. If @a time is NULL, time(NULL) will be
56 * used.
57 * @relates string */
58 struct string *add_date_to_string(struct string *string,
59 const unsigned char *format,
60 const time_t *time);
61 #endif
64 /** @name Encoders:
65 * They encode and add to the string. This way we don't need to first allocate
66 * and encode a temporary string, add it and then free it. Can be used as
67 * backends for encoder.
68 * @{ */
70 /** A simple generic encoder. Should maybe take @a replaceable as a
71 * string so we could also use it for adding shell safe strings.
72 * @relates string */
73 struct string *
74 add_string_replace(struct string *string, unsigned char *src, int len,
75 unsigned char replaceable, unsigned char replacement);
77 /** @relates string */
78 #define add_optname_to_string(str, src, len) \
79 add_string_replace(str, src, len, '.', '*')
81 /** Maybe a bad name but it is actually the real name, but you may
82 * also think of it as adding the decoded option name.
83 * @relates string */
84 #define add_real_optname_to_string(str, src, len) \
85 add_string_replace(str, src, len, '*', '.')
87 /** Convert reserved chars to html @&@#xx;. This function copies bytes
88 * 0x80...0xFF unchanged, so the caller should ensure that the
89 * resulting HTML will be parsed with the same charset as the original
90 * string. (This function cannot use the @&@#160; syntax for non-ASCII,
91 * because HTML wants Unicode numbers there and this function does not
92 * know the charset of the input data.)
93 * @relates string */
94 struct string *add_html_to_string(struct string *string, const unsigned char *html, int htmllen);
96 /** Convert reserved or non-ASCII chars to html @&@#xx;. The resulting
97 * string can be correctly parsed in any charset where bytes
98 * 0x20...0x7E match ASCII.
99 * @relates string */
100 struct string *add_cp_html_to_string(struct string *string, int src_codepage,
101 const unsigned char *html, int htmllen);
103 /** Escapes @\ and " with a @\
104 * @relates string */
105 struct string *add_quoted_to_string(struct string *string, const unsigned char *q, int qlen);
107 /** Adds ', @a len bytes of @a src with all single-quotes converted to '\'',
108 * and ' to @a string.
109 * @relates string */
110 struct string *add_shell_quoted_to_string(struct string *string,
111 unsigned char *src, int len);
113 /* Escapes non shell safe chars with '_'.
114 * @relates string */
115 struct string *add_shell_safe_to_string(struct string *string, unsigned char *cmd, int cmdlen);
117 /** @} */
119 /* These are fast functions to convert integers to string, or to hexadecimal string. */
121 int elinks_ulongcat(unsigned char *s, unsigned int *slen, unsigned long number,
122 unsigned int width, unsigned char fillchar, unsigned int base,
123 unsigned int upper);
125 int elinks_longcat(unsigned char *s, unsigned int *slen, long number,
126 unsigned int width, unsigned char fillchar, unsigned int base,
127 unsigned int upper);
129 /* Type casting is enforced, to shorten calls. --Zas */
130 /** unsigned long to decimal string */
131 #define ulongcat(s, slen, number, width, fillchar) \
132 elinks_ulongcat((unsigned char *) (s), \
133 (unsigned int *) (slen), \
134 (unsigned long) (number), \
135 (unsigned int) (width), \
136 (unsigned char) (fillchar), \
137 (unsigned int) 10, \
138 (unsigned int) 0)
140 /** signed long to decimal string */
141 #define longcat(s, slen, number, width, fillchar) \
142 elinks_longcat((unsigned char *) (s), \
143 (unsigned int *) (slen), \
144 (long) (number), \
145 (unsigned int) (width), \
146 (unsigned char) (fillchar), \
147 (unsigned int) 10, \
148 (unsigned int) 0)
150 /** unsigned long to hexadecimal string */
151 #define ulonghexcat(s, slen, number, width, fillchar, upper) \
152 elinks_ulongcat((unsigned char *) (s), \
153 (unsigned int *) (slen), \
154 (unsigned long) (number), \
155 (unsigned int) (width), \
156 (unsigned char) (fillchar), \
157 (unsigned int) 16, \
158 (unsigned int) (upper))
161 /** Return 0 if starting with jan, 11 for dec, -1 for failure.
162 * @a month must be a lowercased string. */
163 int month2num(const unsigned char *month);
165 #include <string.h>
167 /** Trim starting and ending chars equal to @a c in string @a s.
168 * If @a len != NULL, it stores new string length in pointed integer.
169 * It returns @a s for convenience. */
170 static inline unsigned char *
171 trim_chars(unsigned char *s, unsigned char c, int *len)
173 int l = strlen(s);
174 unsigned char *p = s;
176 while (*p == c) p++, l--;
177 while (l && p[l - 1] == c) p[--l] = '\0';
179 memmove(s, p, l + 1);
180 if (len) *len = l;
182 return s;
185 /** Convert uppercase letters in @a string with the given @a length to
186 * lowercase. */
187 static inline void
188 convert_to_lowercase(unsigned char *string, int length)
190 for (length--; length >= 0; length--)
191 if (isupper(string[length]))
192 string[length] = tolower(string[length]);
195 /** This function drops control chars, nbsp char and limit the number
196 * of consecutive space chars to one. It modifies its argument. */
197 void clr_spaces(unsigned char *str);
199 /** Replace invalid chars in @a title with ' ' and trim all starting/ending
200 * spaces. */
201 void sanitize_title(unsigned char *title);
203 /** Returns 0 if @a url contains invalid chars, 1 if ok.
204 * It trims starting/ending spaces. */
205 int sanitize_url(unsigned char *url);
207 #endif