1 /*
2  *	HT Editor
3  *	strtools.h
4  *
5  *	Copyright (C) 1999-2002 Stefan Weyergraf (stefan@weyergraf.de)
6  *	Copyright (C) 1999-2003 Sebastian Biallas (sb@biallas.net)
7  *
8  *	This program is free software; you can redistribute it and/or modify
9  *	it under the terms of the GNU General Public License version 2 as
10  *	published by the Free Software Foundation.
11  *
12  *	This program is distributed in the hope that it will be useful,
13  *	but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *	GNU General Public License for more details.
16  *
17  *	You should have received a copy of the GNU General Public License
18  *	along with this program; if not, write to the Free Software
19  *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #ifndef __STRTOOLS_H__
23 #define __STRTOOLS_H__
24 
25 #include "io/types.h"
26 #include "data.h"
27 
28 #include <cstring>
29 
30 char *ht_strdup(const char *str);
31 char *ht_strndup(const char *str, size_t maxlen);
32 size_t ht_strlcpy(char *s1, const char *s2, size_t maxlen);
33 size_t ht_strlcat(char *s1, const char *s2, size_t maxlen);
34 int ht_strncmp(const char *s1, const char *s2, size_t max);
35 int ht_strnicmp(const char *s1, const char *s2, size_t max);
36 int ht_stricmp(const char *s1, const char *s2);
37 
38 size_t ht_strcicomm(const char *s1, const char *s2);
39 size_t ht_strccomm(const char *s1, const char *s2);
40 
ht_strend(char * s)41 static inline char *ht_strend(char *s)
42 {
43 	return s+strlen(s);
44 }
ht_strend(const char * s)45 static inline const char *ht_strend(const char *s)
46 {
47 	return s+strlen(s);
48 }
49 
50 int escape_special_str(char *result, int resultmaxlen, const char *s, const char *specialchars = NULL, bool bit7 = true);
51 int escape_special(char *result, int resultmaxlen, const void *s, int len, const char *specialchars = NULL, bool bit7 = true);
52 int unescape_special_str(char *result, int resultmaxlen, const char *s);
53 int unescape_special(void *result, int resultmaxlen, const char *s);
54 int bin2str(char *result, const void *s, int len);
55 void wide_char_to_multi_byte(char *result, const byte *unicode, int maxlen);
56 
57 void memdowncase(byte *buf, int len);
58 byte *ht_memmem(const byte *haystack, int haystack_len, const byte *needle, int needle_len);
59 
60 /* common string parsing functions */
61 bool is_whitespace(char c);
62 void non_whitespaces(const char *&str);
63 void whitespaces(const char *&str);
64 bool waitforchar(const char *&str, char b);
65 
66 /* string evaluation functions */
67 bool parseIntStr(char *&str, uint64 &u64, int defaultbase);
68 bool parseIntStr(const char *&str, uint64 &u64, int defaultbase);
69 bool str2int(const char *str, uint64 &u64, int defaultbase = 10);
70 
71 /* hex/string functions */
72 int hexdigit(char a);
73 
74 bool hexb_ex(uint8 &result, const char *s);
75 bool hexw_ex(uint16 &result, const char *s);
76 bool hexd_ex(uint32 &result, const char *s);
77 
78 /*
79  *	ht_string_list
80  */
81 class ht_string_list: public Array {
82 public:
ht_string_list()83 	ht_string_list()
84 		: Array(true)
85 	{
86 	}
87 
88 	/* new */
get_string(uint i)89 	const char *get_string(uint i)
90 	{
91 		return ((String *)get(findByIdx(i)))->contentChar();
92 	}
93 
insert_string(const char * s)94 	void insert_string(const char *s)
95 	{
96 		insert(new String(s));
97 	}
98 };
99 
100 #endif /* !__STRTOOLS_H__ */
101