1 /*
2  *  Tvheadend - advanced string functions
3  *  Copyright (C) 2007 Andreas Öman
4  *  Copyright (C) 2014-2018 Jaroslav Kysela
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #ifndef TVHEADEND_STRINGS_H
20 #define TVHEADEND_STRINGS_H
21 
22 #include "build.h"
23 
tvh_strlen(const char * s)24 static inline size_t tvh_strlen(const char *s)
25 {
26   return (s) ? strlen(s) : 0;
27 }
28 
tvh_str_default(const char * s,const char * dflt)29 static inline const char *tvh_str_default(const char *s, const char *dflt)
30 {
31   return s && s[0] ? s : dflt;
32 }
33 
34 void tvh_str_set(char **strp, const char *src);
35 int tvh_str_update(char **strp, const char *src);
36 
37 #define tvh_strdupa(n) \
38   ({ int tvh_l = strlen(n); \
39      char *tvh_b = alloca(tvh_l + 1); \
40      memcpy(tvh_b, n, tvh_l + 1); })
41 
tvh_strbegins(const char * s1,const char * s2)42 static inline const char *tvh_strbegins(const char *s1, const char *s2)
43 {
44   while(*s2)
45     if(*s1++ != *s2++)
46       return NULL;
47   return s1;
48 }
49 
50 #ifndef ENABLE_STRLCPY
strlcpy(char * dst,const char * src,size_t size)51 static inline size_t strlcpy(char *dst, const char *src, size_t size)
52 {
53   size_t ret = strlen(src);
54   if (size) {
55     size_t len = ret >= size ? size - 1 : ret;
56     memcpy(dst, src, len);
57     dst[len] = '\0';
58   }
59   return ret;
60 }
61 #endif
62 
63 #ifndef ENABLE_STRLCAT
strlcat(char * dst,const char * src,size_t count)64 static inline size_t strlcat(char *dst, const char *src, size_t count)
65 {
66   size_t dlen = strlen(dst);
67   size_t len = strlen(src);
68   size_t res = dlen + len;
69 
70   dst += dlen;
71   count -= dlen;
72   if (len >= count)
73     len = count - 1;
74   memcpy(dst, src, len);
75   dst[len] = '\0';
76   return res;
77 }
78 #endif
79 
80 #define tvh_strlcatf(buf, size, ptr, fmt...) \
81   do { int __r = snprintf((buf) + ptr, (size) - ptr, fmt); \
82        ptr = __r >= (size) - ptr ? (size) - 1 : ptr + __r; } while (0)
83 
mystrset(char ** p,const char * s)84 static inline void mystrset(char **p, const char *s)
85 {
86   free(*p);
87   *p = s ? strdup(s) : NULL;
88 }
89 
tvh_strhash(const char * s,unsigned int mod)90 static inline unsigned int tvh_strhash(const char *s, unsigned int mod)
91 {
92   unsigned int v = 5381;
93   while(*s)
94     v += (v << 5) + v + *s++;
95   return v % mod;
96 }
97 
98 int put_utf8(char *out, int c);
99 
100 char *utf8_lowercase_inplace(char *s);
101 
102 #endif /* TVHEADEND_STRINGS_H */
103