1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are met:
5 
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer.
8 
9  * Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12 
13  * The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15 
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef FONTFORGE_UCHAR_H
29 #define FONTFORGE_UCHAR_H
30 
31 #include <fontforge-config.h>
32 
33 #include "basics.h"
34 #include "charset.h"
35 
36 #include <memory.h>
37 #include <stdarg.h>
38 #include <string.h>
39 
40 #pragma push_macro("PRINTF_FORMAT_ATTRIBUTE")
41 #ifdef __GNUC__
42 #  if defined(__USE_MINGW_ANSI_STDIO) && __USE_MINGW_ANSI_STDIO != 0
43 #    define PRINTF_FORMAT_ATTRIBUTE(x, y) __attribute__((format(gnu_printf, x, y)))
44 #  else
45 #    define PRINTF_FORMAT_ATTRIBUTE(x, y) __attribute__((format(printf, x, y)))
46 #  endif
47 #else
48 #  define PRINTF_FORMAT_ATTRIBUTE(x, y)
49 #endif
50 
51 extern char *copy(const char *);
52 extern char *copyn(const char *,long);
53 extern unichar_t *u_copy(const unichar_t*);
54 extern unichar_t *u_copyn(const unichar_t*, long);
55 extern unichar_t *u_copynallocm(const unichar_t *pt, long n, long m);
56 extern unichar_t *uc_copyn(const char *, int);
57 extern unichar_t *uc_copy(const char*);
58 extern unichar_t *u_concat(const unichar_t*,const unichar_t*);
59 extern char      *cu_copyn(const unichar_t *pt,int len);
60 extern char      *cu_copy(const unichar_t*);
61 
62 extern char *vsmprintf(const char *fmt, va_list args);
63 extern char *smprintf(const char *fmt, ...) PRINTF_FORMAT_ATTRIBUTE(1, 2);
64 
65 extern long uc_strcmp(const unichar_t *,const char *);
66 extern long u_strcmp(const unichar_t *, const unichar_t *);
67 extern long uc_strncmp(const unichar_t *,const char *,int);
68 extern long u_strncmp(const unichar_t *, const unichar_t *,int);
69 extern long uc_strmatch(const unichar_t *,const char *);
70 extern long uc_strnmatch(const unichar_t *,const char *,int);
71 extern long u_strnmatch(const unichar_t *str1, const unichar_t *str2, int len);
72 extern long u_strmatch(const unichar_t *, const unichar_t *);
73 extern int    strmatch(const char *,const char *);
74 extern int    strnmatch(const char *str1, const char *str2, int n);
75 extern void uc_strcpy(unichar_t *, const char *);
76 extern void cu_strcpy(char *, const unichar_t *);
77 extern void u_strcpy(unichar_t *, const unichar_t *);
78 extern void u_strncpy(unichar_t *, const unichar_t *,int);
79 extern void cu_strncpy(char *to, const unichar_t *from, int len);
80 extern void uc_strncpy(unichar_t *to, const char *from, int len);
81 /**
82  * Like strncpy but passing a null 'from' will simply null terminate
83  * to[0] to give a blank result rather than a crash.
84  */
85 extern char *cc_strncpy(char *to, const char *from, int len);
86 extern void uc_strcat(unichar_t *, const char *);
87 extern void uc_strncat(unichar_t *, const char *,int len);
88 extern void cu_strcat(char *, const unichar_t *);
89 extern void cu_strncat(char *, const unichar_t *,int len);
90 extern void u_strcat(unichar_t *, const unichar_t *);
91 extern void u_strncat(unichar_t *, const unichar_t *, int len);
92 extern int  u_strlen(const unichar_t *);
93 /**
94  * Like strlen() but passing a null pointer gets a 0 length
95  */
96 extern int  c_strlen(const char *);
97 extern unichar_t *u_strchr(const unichar_t *,unichar_t);
98 extern unichar_t *u_strrchr(const unichar_t *,unichar_t);
99 extern unichar_t *uc_strstr(const unichar_t *,const char *);
100 extern unichar_t *u_strstr(const unichar_t *,const unichar_t *);
101 extern unichar_t *uc_strstrmatch(const unichar_t *,const char *);
102 extern unichar_t *u_strstrmatch(const unichar_t *,const unichar_t *);
103 extern char      *  strstrmatch(const char *,const char *);
104 
105 extern char *u_to_c(const unichar_t *);
106 extern unichar_t *c_to_u(const char *);
107 
108 extern unsigned long u_strtoul(const unichar_t *,unichar_t **,int);
109 extern long   u_strtol(const unichar_t *,unichar_t **,int);
110 extern double u_strtod(const unichar_t *,unichar_t **);
111 
112 /*
113  * Convert the integer 'v' to a string and return it.
114  * You do not own the return value, it is an internal buffer
115  * so you should copy it before using the function again
116  */
117 extern char*  c_itostr( int v );
118 
119 extern char *strstart(const char *initial,const char *full);
120 extern char *strstartmatch(const char *initial,const char *full);
121 extern unichar_t *u_strstartmatch(const unichar_t *initial, const unichar_t *full);
122 extern unichar_t *cu_strstartmatch(const char *initial, const unichar_t *full);
123 
124 #define utf82u_strncpy utf82U_strncpy
125 extern int32 utf8_ildb(const char **utf8_text);
126 #define UTF8IDPB_NOZERO 1	/* Allow for 0 encoded as a non-zero utf8 0xc0:0x80 char */
127 #define UTF8IDPB_OLDLIMIT 2	/* Today's utf8 is agreed to be limited to {0..0x10FFFF} */
128 #define UTF8IDPB_UCS2 8		/* Encode {0...0xffff} as 16bit ucs2 type values */
129 #define UTF8IDPB_UTF16 16	/* Encode {0...0x10ffff} as 16bit utf16 type values */
130 #define UTF8IDPB_UTF32 32	/* Encode {0...0x10ffff} as 32bit utf32 type values */
131 extern char *utf8_idpb(char *utf8_text,uint32 ch,int flags);
132 extern char *utf8_db(char *utf8_text);
133 extern char *utf8_ib(char *utf8_text);
134 extern int utf8_valid(const char *str);
135 extern void utf8_truncatevalid(char *str);
136 extern char *latin1_2_utf8_strcpy(char *utf8buf,const char *lbuf);
137 extern char *latin1_2_utf8_copy(const char *lbuf);
138 extern char *utf8_2_latin1_copy(const char *utf8buf);
139 extern long utf8_strlen(const char *utf8_str);	 /* Count how many characters in the string NOT bytes */
140 extern long utf82u_strlen(const char *utf8_str); /* Count how many shorts needed to represent in UCS2 */
141 extern void utf8_strncpy(register char *to, const char *from, int len); /* copy n characters NOT bytes */
142 extern char *def2utf8_copy(const char *from);
143 extern char *utf82def_copy(const char *ufrom);
144 extern char *utf8_strchr(const char *utf8_str, int search_char);
145 
146 extern unichar_t *utf82u_strncpy(unichar_t *ubuf,const char *utf8buf,int len);
147 extern unichar_t *utf82u_strcpy(unichar_t *ubuf,const char *utf8buf);
148 extern void       utf82u_strcat(unichar_t *ubuf,const char *utf8buf);
149 extern unichar_t *utf82u_copyn(const char *utf8buf,int len);
150 extern unichar_t *utf82u_copy(const char *utf8buf);
151 extern char *u2utf8_strcpy(char *utf8buf,const unichar_t *ubuf);
152 extern char *u2utf8_strncpy(char *utf8buf,const unichar_t *ubuf,int len);
153 extern char *u2utf8_copy(const unichar_t *ubuf);
154 extern char *u2utf8_copyn(const unichar_t *ubuf,int len);
155 extern unichar_t *encoding2u_strncpy(unichar_t *uto, const char *from, int n, enum encoding cs);
156 extern char *u2encoding_strncpy(char *to, const unichar_t *ufrom, size_t n, enum encoding cs);
157 extern unichar_t *def2u_strncpy(unichar_t *uto, const char *from, size_t n);
158 extern char *u2def_strncpy(char *to, const unichar_t *ufrom, size_t n);
159 extern unichar_t *def2u_copy(const char *from);
160 extern char *u2def_copy(const unichar_t *ufrom);
161 
162 extern int uAllAscii(const unichar_t *str);
163 extern int AllAscii(const char *);
164 extern char *StripToASCII(const char *utf8_str);
165 
166 extern char *copytolower(const char *);
167 extern int endswith(const char *haystack,const char *needle);
168 extern int endswithi(const char *haystack,const char *needle);
169 extern int endswithi_partialExtension( const char *haystack,const char *needle);
170 
171 /**
172  * Remove trailing \n or \r from the given string. No memory
173  * allocations are performed, null is injected over these terminators
174  * to trim the string.
175  *
176  * This function is designed to be impotent if called with a string
177  * that does not end with \n or \r. ie, you don't need to redundantly
178  * check if there is a newline at the end of string and not call here
179  * if there is no newline. You can just call here with any string and
180  * be assured that afterwards there will be no trailing newline or
181  * carrage return character found at the end of the string pointed to
182  * by 'p'.
183  */
184 extern char* chomp( char* p );
185 
186 /**
187  * Return true if the haystack plain string ends with the string
188  * needle. Return 0 otherwise.
189  *
190  * Needles which are larger than the haystack are handled.
191  *
192  * No new strings are allocated, freed, or returned.
193  */
194 int endswith(const char *haystack,const char *needle);
195 
196 /**
197  * Return true if the haystack unicode string ends with the string needle.
198  * Return 0 otherwise.
199  *
200  * Needles which are larger than the haystack are handled.
201  *
202  * No new strings are allocated, freed, or returned.
203  */
204 extern int u_endswith(const unichar_t *haystack,const unichar_t *needle);
205 
206 extern int u_startswith(const unichar_t *haystack,const unichar_t *needle);
207 extern int uc_startswith(const unichar_t *haystack,const char* needle);
208 
209 /**
210  * In the string 's' replace all occurances of 'orig' with 'replacement'.
211  * If you set free_s to true then the string 's' will be freed by this function.
212  * Normally you want to set free_s to 0 to avoid that. The case you will want to
213  * use free_s to 1 is chaining many calls like:
214  *
215  * char* s = copy( input );
216  * s = str_replace_all( s, "foo", "bar", 1 );
217  * s = str_replace_all( s, "baz", "gah", 1 );
218  * // use s
219  * free(s);
220  * // no leaks in the above.
221  *
222  * Note that 's' is first copied before the first call to replace_all in the above
223  * so it can be freed without concern. This also allows the ordering of replace_all
224  * in the above to be changed without having to worry about the free_s flag.
225  */
226 extern char* str_replace_all( char* s, char* orig, char* replacement, int free_s );
227 
228 
229 int toint( char* v );
230 char* tostr( int v );
231 
232 #pragma pop_macro("PRINTF_FORMAT_ATTRIBUTE")
233 
234 #endif /* FONTFORGE_UCHAR_H */
235