1 /*
2  * audstrings.h
3  * Copyright 2009-2012 John Lindgren and Ariadne Conill
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions, and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  *    this list of conditions, and the following disclaimer in the documentation
13  *    provided with the distribution.
14  *
15  * This software is provided "as is" and without any warranty, express or
16  * implied. In no event shall the authors be liable for any damages arising from
17  * the use of this software.
18  */
19 
20 #ifndef LIBAUDCORE_STRINGS_H
21 #define LIBAUDCORE_STRINGS_H
22 
23 #include <stdarg.h>
24 #include <stdint.h>
25 
26 #include <initializer_list>
27 
28 #include <libaudcore/index.h>
29 #include <libaudcore/objects.h>
30 
31 int strcmp_safe(const char * a, const char * b, int len = -1);
32 int strcmp_nocase(const char * a, const char * b, int len = -1);
33 int strlen_bounded(const char * s, int len = -1);
34 
35 StringBuf str_copy(const char * s, int len = -1);
36 StringBuf str_concat(const std::initializer_list<const char *> & strings);
37 #ifdef _WIN32
38 StringBuf str_printf(const char * format, ...)
39     __attribute__((__format__(gnu_printf, 1, 2)));
40 void str_append_printf(StringBuf & str, const char * format, ...)
41     __attribute__((__format__(gnu_printf, 2, 3)));
42 #else
43 StringBuf str_printf(const char * format, ...)
44     __attribute__((__format__(__printf__, 1, 2)));
45 void str_append_printf(StringBuf & str, const char * format, ...)
46     __attribute__((__format__(__printf__, 2, 3)));
47 #endif
48 StringBuf str_vprintf(const char * format, va_list args);
49 void str_append_vprintf(StringBuf & str, const char * format, va_list args);
50 
51 bool str_has_prefix_nocase(const char * str, const char * prefix);
52 bool str_has_suffix_nocase(const char * str, const char * suffix);
53 
54 unsigned str_calc_hash(const char * str);
55 
56 const char * strstr_nocase(const char * haystack, const char * needle);
57 const char * strstr_nocase_utf8(const char * haystack, const char * needle);
58 
strstr_nocase(char * haystack,const char * needle)59 static inline char * strstr_nocase(char * haystack, const char * needle)
60 {
61     return (char *)strstr_nocase((const char *)haystack, needle);
62 }
strstr_nocase_utf8(char * haystack,const char * needle)63 static inline char * strstr_nocase_utf8(char * haystack, const char * needle)
64 {
65     return (char *)strstr_nocase_utf8((const char *)haystack, needle);
66 }
67 
68 StringBuf str_tolower(const char * str);
69 StringBuf str_tolower_utf8(const char * str);
70 StringBuf str_toupper(const char * str);
71 StringBuf str_toupper_utf8(const char * str);
72 
73 void str_replace_char(char * string, char old_c, char new_c);
74 
75 StringBuf str_decode_percent(const char * str, int len = -1);
76 StringBuf str_encode_percent(const char * str, int len = -1);
77 
78 StringBuf str_convert(const char * str, int len, const char * from_charset,
79                       const char * to_charset);
80 StringBuf str_from_locale(const char * str, int len = -1);
81 StringBuf str_to_locale(const char * str, int len = -1);
82 
83 /* Requires: aud_init() */
84 StringBuf str_to_utf8(const char * str,
85                       int len); // no "len = -1" to avoid ambiguity
86 StringBuf str_to_utf8(StringBuf && str);
87 
88 StringBuf filename_normalize(StringBuf && filename);
89 StringBuf filename_contract(StringBuf && filename);
90 StringBuf filename_expand(StringBuf && filename);
91 StringBuf filename_get_parent(const char * filename);
92 StringBuf filename_get_base(const char * filename);
93 StringBuf filename_build(const std::initializer_list<const char *> & elems);
94 StringBuf filename_to_uri(const char * filename);
95 StringBuf uri_to_filename(const char * uri, bool use_locale = true);
96 
97 void uri_parse(const char * uri, const char ** base_p, const char ** ext_p,
98                const char ** sub_p, int * isub_p);
99 
100 StringBuf uri_get_scheme(const char * uri);
101 StringBuf uri_get_extension(const char * uri);
102 
103 /* Requires: aud_init() */
104 StringBuf uri_to_display(const char * uri);
105 StringBuf uri_get_display_base(const char * uri);
106 StringBuf uri_construct(const char * path, const char * reference);
107 StringBuf uri_deconstruct(const char * uri, const char * reference);
108 
109 int str_compare(const char * a, const char * b);
110 int str_compare_encoded(const char * a, const char * b);
111 
112 Index<String> str_list_to_index(const char * list, const char * delims);
113 StringBuf index_to_str_list(const Index<String> & index, const char * sep);
114 
115 int str_to_int(const char * string);
116 double str_to_double(const char * string);
117 void str_insert_int(StringBuf & string, int pos, int val);
118 void str_insert_double(StringBuf & string, int pos, double val);
119 StringBuf int_to_str(int val);
120 StringBuf double_to_str(double val);
121 
122 bool str_to_int_array(const char * string, int * array, int count);
123 StringBuf int_array_to_str(const int * array, int count);
124 bool str_to_double_array(const char * string, double * array, int count);
125 StringBuf double_array_to_str(const double * array, int count);
126 
127 /* Requires: aud_init() */
128 StringBuf str_format_time(int64_t milliseconds);
129 
130 #endif /* LIBAUDCORE_STRINGS_H */
131