| 1 | // This file is part of SmallBASIC |
| 2 | // |
| 3 | // strings |
| 4 | // |
| 5 | // This program is distributed under the terms of the GPL v2.0 or later |
| 6 | // Download the GNU Public License (GPL) from www.gnu.org |
| 7 | // |
| 8 | // Copyright(C) 2000 Nicholas Christopoulos |
| 9 | |
| 10 | /** |
| 11 | * @defgroup str Strings |
| 12 | */ |
| 13 | |
| 14 | #if !defined(_sb_str_h) |
| 15 | #define _sb_str_h |
| 16 | |
| 17 | #include "common/sys.h" |
| 18 | #include "lib/str.h" |
| 19 | |
| 20 | #if defined(__cplusplus) |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
| 24 | // SJIS |
| 25 | //Begin: Modified by Araiguma |
| 26 | #define IsJIS1Font(x) (((x) >= 0x81 && (x) <= 0x9f) || ((x) >= 0xe0 && (x) <= 0xef)) |
| 27 | /**< SJIS charset, true if the first character is belongs to sjis encode @ingroup str */ |
| 28 | #define IsJIS2Font(x) ((x) >= 0x40 && (x) <= 0xfc) |
| 29 | /**< SJIS charset, true if the second character is belongs to sjis encode @ingroup str */ |
| 30 | #define IsJISFont(x) (IsJIS1Font((x >> 8) & 0xff) && IsJIS2Font(x & 0xff)) |
| 31 | /**< SJIS charset, true if the character is belongs to sjis encode @ingroup str */ |
| 32 | // End : Modified by Araiguma |
| 33 | // Big5 |
| 34 | #define IsBig51Font(x) ((x) >= 0x81 && (x) <= 0xfe) |
| 35 | /**< BIG5 charset, true if the first character is belongs to big5 encode @ingroup str */ |
| 36 | #define IsBig52Font(x) (((x) >= 0x40 && (x) <= 0x7e) || ((x) >= 0xa1 && (x) <= 0xfe)) |
| 37 | /**< BIG5 charset, true if the second character is belongs to big5 encode @ingroup str */ |
| 38 | #define IsBig5Font(x) (IsBig51Font((x >> 8) & 0xff) && IsBig52Font(x & 0xff)) |
| 39 | /**< BIG5 charset, true if the character is belongs to big5 encode @ingroup str */ |
| 40 | |
| 41 | // generic multibyte |
| 42 | #define IsGMB1Font(x) ((x) >= 0x81 && (x) <= 0xfe) |
| 43 | /**< generic-multibyte charset, true if the character is belongs to gmb encode @ingroup str */ |
| 44 | #define IsGMB2Font(x) ((x) >= 0x21 && (x) <= 0xff) |
| 45 | /**< generic-multibyte charset, true if the second character is belongs to gmb encode @ingroup str */ |
| 46 | #define IsGMBFont(x) (IsGMB1Font((x >> 8) & 0xff) && IsGMB2Font(x & 0xff)) |
| 47 | /**< generic-multibyte charset, true if the character is belongs to gmb encode @ingroup str */ |
| 48 | |
| 49 | #include "languages/chars.en.h" |
| 50 | |
| 51 | /** |
| 52 | * @ingroup str |
| 53 | * |
| 54 | * returns true if the character is an alphabet symbol |
| 55 | * |
| 56 | * @param ch the character |
| 57 | * @return non-zero if the character is an alphabet symbol |
| 58 | */ |
| 59 | int is_alpha(int ch); |
| 60 | |
| 61 | /** |
| 62 | * @ingroup str |
| 63 | * |
| 64 | * returns true if the character is an alphabet symbol or a digit |
| 65 | * |
| 66 | * @param ch the character |
| 67 | * @return non-zero if the character is an alphanumeric symbol |
| 68 | */ |
| 69 | int is_alnum(int ch); |
| 70 | |
| 71 | /** |
| 72 | * @ingroup str |
| 73 | * |
| 74 | * returns true if the character is a 'space' |
| 75 | * |
| 76 | * @param ch the character |
| 77 | * @return non-zero if the character is a 'space' |
| 78 | */ |
| 79 | int is_space(int ch); |
| 80 | |
| 81 | /** |
| 82 | * @ingroup str |
| 83 | * |
| 84 | * returns true if the character is a white-space character |
| 85 | * |
| 86 | * @param ch the character |
| 87 | * @return non-zero if the character is a white-space character |
| 88 | */ |
| 89 | int is_wspace(int c); |
| 90 | |
| 91 | /** |
| 92 | * @ingroup str |
| 93 | * |
| 94 | * returns true if all the characters of the string are digits |
| 95 | * |
| 96 | * @param text the text |
| 97 | * @return true if all the characters of the string are digits |
| 98 | */ |
| 99 | int is_all_digits(const char *text); |
| 100 | |
| 101 | /** |
| 102 | * @ingroup str |
| 103 | * |
| 104 | * returns true if the string is a number |
| 105 | * |
| 106 | * @param name the string |
| 107 | * @return true if the string is a number |
| 108 | */ |
| 109 | int is_number(const char *str); |
| 110 | |
| 111 | /** |
| 112 | * @ingroup str |
| 113 | * |
| 114 | * converts a string to upper-case |
| 115 | * |
| 116 | * @param str the string |
| 117 | * @return the str |
| 118 | */ |
| 119 | char *strupper(char *str); |
| 120 | |
| 121 | /** |
| 122 | * @ingroup str |
| 123 | * |
| 124 | * converts a string to lower-case |
| 125 | * |
| 126 | * @param str the string |
| 127 | * @return the str |
| 128 | */ |
| 129 | char *strlower(char *str); |
| 130 | |
| 131 | /** |
| 132 | * @ingroup str |
| 133 | * |
| 134 | * Returns the number of a complex constant numeric expression |
| 135 | * |
| 136 | * type <=0 = error |
| 137 | * 1 = int32_t |
| 138 | * 2 = double |
| 139 | * |
| 140 | * Warning: octals are different from C (QB compatibility: 009 = 9) |
| 141 | * |
| 142 | * @param text the source text, the text does not need to contains only the expression |
| 143 | * @param dest buffer to return the string of the expression |
| 144 | * @param type the type (1=integer-number, 2=real-number, otherwise error) |
| 145 | * @param lv the integer value |
| 146 | * @param dv the real value |
| 147 | * @return a pointer in 'text' that points to the next position |
| 148 | */ |
| 149 | char *get_numexpr(char *text, char *dest, int *type, var_int_t *lv, var_num_t *dv); |
| 150 | |
| 151 | /** |
| 152 | * @ingroup str |
| 153 | * |
| 154 | * removes all the leading/trailing white-spaces |
| 155 | * |
| 156 | * <b>Example</b> |
| 157 | * @code |
| 158 | * before " Hello World " -> after "Hello World" |
| 159 | * @endcode |
| 160 | * |
| 161 | * @param str the string |
| 162 | */ |
| 163 | void str_alltrim(char *str); |
| 164 | |
| 165 | /** |
| 166 | * @ingroup str |
| 167 | * |
| 168 | * returns the value of a string that contains an integer number in binary form |
| 169 | * |
| 170 | * @param str the string |
| 171 | * @return the number |
| 172 | */ |
| 173 | long bintol(const char *str); |
| 174 | |
| 175 | /** |
| 176 | * @ingroup str |
| 177 | * |
| 178 | * returns the value of a string that contains an integer number in octal form |
| 179 | * |
| 180 | * @param str the string |
| 181 | * @return the number |
| 182 | */ |
| 183 | long octtol(const char *str); |
| 184 | |
| 185 | /** |
| 186 | * @ingroup str |
| 187 | * |
| 188 | * returns the value of a string that contains an integer number in hexadecimal form |
| 189 | * |
| 190 | * @param str the string |
| 191 | * @return the number |
| 192 | */ |
| 193 | long hextol(const char *str); |
| 194 | |
| 195 | /** |
| 196 | * @ingroup str |
| 197 | * |
| 198 | * returns the value of a string that contains a real number in decimal form |
| 199 | * |
| 200 | * @param str the string |
| 201 | * @return the number |
| 202 | */ |
| 203 | var_num_t sb_strtof(const char *str); |
| 204 | |
| 205 | /** |
| 206 | * @ingroup str |
| 207 | * |
| 208 | * returns the value of a string that contains an integer number in decimal form |
| 209 | * |
| 210 | * @param str the string |
| 211 | * @return the number |
| 212 | */ |
| 213 | long xstrtol(const char *str); |
| 214 | |
| 215 | /** |
| 216 | * @ingroup str |
| 217 | * |
| 218 | * converts a real number to string |
| 219 | * |
| 220 | * @param num the number |
| 221 | * @param dest is the buffer to store the string |
| 222 | * @return a pointer to dest |
| 223 | */ |
| 224 | char *ftostr(var_num_t num, char *dest); |
| 225 | |
| 226 | /** |
| 227 | * @ingroup str |
| 228 | * |
| 229 | * converts an integer number to string |
| 230 | * |
| 231 | * @param num the number |
| 232 | * @param dest is the buffer to store the string |
| 233 | * @return a pointer to dest |
| 234 | */ |
| 235 | char *ltostr(var_int_t num, char *dest); |
| 236 | |
| 237 | /** |
| 238 | * @ingroup str |
| 239 | * |
| 240 | * compare to strings case insensitively |
| 241 | * |
| 242 | * @param |
| 243 | * @param |
| 244 | * @return |
| 245 | */ |
| 246 | int strcaselessn(const char *s1, int s1n, const char *s2, int s2n); |
| 247 | |
| 248 | /** |
| 249 | * @ingroup str |
| 250 | * |
| 251 | * locate the substring 's2' into 's1' and return a pointer of the |
| 252 | * first occurence (in 's1') or NULL if not found. |
| 253 | * |
| 254 | * the difference with strstr() is that the q_strstr do the search ignoring everything |
| 255 | * that included in the specified pairs. |
| 256 | * |
| 257 | * example |
| 258 | * @code |
| 259 | * q_strstr("Hello (world of) of", "of", "()"); |
| 260 | * @endcode |
| 261 | * |
| 262 | * @param s1 the text |
| 263 | * @param s2 the substring |
| 264 | * @param pairs ignore the string that is included in any of that pairs |
| 265 | * @return on success a pointer to 's1' in the place which the 's2' is starting; otherwise NULL |
| 266 | */ |
| 267 | char *q_strstr(const char *s1, const char *s2, const char *pairs); |
| 268 | |
| 269 | /** |
| 270 | * @ingroup str |
| 271 | * |
| 272 | * returns the real-number of a complex-constant numeric expression |
| 273 | * |
| 274 | * @param source is the text |
| 275 | * @returns the real-number |
| 276 | */ |
| 277 | var_num_t numexpr_sb_strtof(char *source); |
| 278 | |
| 279 | /** |
| 280 | * @ingroup str |
| 281 | * |
| 282 | * returns the integer-number of a complex-constant numeric expression |
| 283 | * |
| 284 | * @param source is the text |
| 285 | * @returns the real-number |
| 286 | */ |
| 287 | var_int_t numexpr_strtol(char *source); |
| 288 | |
| 289 | /** |
| 290 | * @ingroup str |
| 291 | * |
| 292 | * returns the string 'source' enclosed by pairs |
| 293 | * |
| 294 | * @param source the source-string |
| 295 | * @param pairs the pair |
| 296 | * @return a newly allocated string of the enclosed result |
| 297 | */ |
| 298 | char *encldup(const char *source, const char *pairs); |
| 299 | |
| 300 | /** |
| 301 | * @ingroup str |
| 302 | * |
| 303 | * returns the string 'source' that is located inside the of some pair of 'pairs' |
| 304 | * |
| 305 | * @param source the source-string |
| 306 | * @param pairs the pairs |
| 307 | * @param ignpairs ignore the string that is included in any of that pairs |
| 308 | * @return a newly allocated string of the disclosed result |
| 309 | */ |
| 310 | char *discldup(const char *source, const char *pairs, const char *ignpairs); |
| 311 | |
| 312 | /** |
| 313 | * @ingroup str |
| 314 | * |
| 315 | * The squeeze function removes all duplicated white-spaces from the string, include all the leading/trailing white-spaces. |
| 316 | * |
| 317 | * <b>Example</b> |
| 318 | * @code |
| 319 | * source=" Hello World " -> result="Hello World" |
| 320 | * @endcode |
| 321 | * |
| 322 | * @param source is the source string |
| 323 | * @return a newly created string |
| 324 | */ |
| 325 | char *sqzdup(const char *source); |
| 326 | |
| 327 | /** |
| 328 | * @ingroup str |
| 329 | * |
| 330 | * translates the 'what' with the 'with' |
| 331 | * |
| 332 | * @param src is the source string |
| 333 | * @param what is what to search |
| 334 | * @param with is what to replace with |
| 335 | * @return a newly created string |
| 336 | */ |
| 337 | char *transdup(const char *src, const char *what, const char *with, int ignore_case); |
| 338 | |
| 339 | /** |
| 340 | * @ingroup str |
| 341 | * |
| 342 | * removes all the leading/trailing white-spaces |
| 343 | * |
| 344 | * <b>Example</b> |
| 345 | * @code |
| 346 | * source=" Hello World " -> result="Hello World" |
| 347 | * @endcode |
| 348 | * |
| 349 | * @param source is the source string |
| 350 | * @return a newly created string |
| 351 | */ |
| 352 | char *trimdup(const char *str); |
| 353 | |
| 354 | /** |
| 355 | * @ingroup str |
| 356 | * |
| 357 | * Converts a string which contains c-style special characters |
| 358 | * to normal text. |
| 359 | * |
| 360 | * @param source the string |
| 361 | * @return a newly created string |
| 362 | */ |
| 363 | char *cstrdup(const char *source); |
| 364 | |
| 365 | /** |
| 366 | * @ingroup str |
| 367 | * |
| 368 | * Converts a string to c-style string |
| 369 | * |
| 370 | * @param source the string |
| 371 | * @return a newly created string |
| 372 | */ |
| 373 | char *bstrdup(const char *source); |
| 374 | |
| 375 | /** |
| 376 | * @ingroup str |
| 377 | * |
| 378 | * returns a pointer of 'source' on the base-name |
| 379 | * |
| 380 | * @param source the string |
| 381 | * @param delim the delimiter |
| 382 | * @return pointer to base |
| 383 | */ |
| 384 | const char *baseof(const char *source, int delim); |
| 385 | |
| 386 | /** |
| 387 | * @ingroup sys |
| 388 | * |
| 389 | * dumps a memory block in hex |
| 390 | * |
| 391 | * @param block is the block |
| 392 | * @param size is the size of the block |
| 393 | */ |
| 394 | void hex_dump(const unsigned char *block, int size); |
| 395 | |
| 396 | /** |
| 397 | * @ingroup str |
| 398 | * |
| 399 | * string buffer |
| 400 | */ |
| 401 | typedef struct cstr { |
| 402 | int size; |
| 403 | int length; |
| 404 | char *buf; |
| 405 | } cstr; |
| 406 | |
| 407 | /** |
| 408 | * @ingroup str |
| 409 | * |
| 410 | * string buffer constructor |
| 411 | */ |
| 412 | void cstr_init(cstr *cs, int size); |
| 413 | |
| 414 | /** |
| 415 | * @ingroup str |
| 416 | * |
| 417 | * append to string buffer |
| 418 | */ |
| 419 | void cstr_append(cstr *cs, const char *str); |
| 420 | |
| 421 | /** |
| 422 | * @ingroup str |
| 423 | * |
| 424 | * append to string buffer |
| 425 | */ |
| 426 | void cstr_append_i(cstr *cs, const char *str, int len); |
| 427 | |
| 428 | #if defined(__cplusplus) |
| 429 | } |
| 430 | #endif |
| 431 | #endif |
| 432 | |