7 #include <mhl/memory.h>
9 #define mhl_str_dup(str) ((str ? strdup(str) : strdup("")))
10 #define mhl_str_ndup(str,len) ((str ? strndup(str,len) : strdup("")))
11 #define mhl_str_len(str) ((str ? strlen(str) : 0))
13 static inline char * mhl_str_dup_range(const char * s_start
, const char * s_bound
)
15 return mhl_str_ndup(s_start
, s_bound
- s_start
);
18 static inline char* mhl_str_trim(char* str
)
20 if (!str
) return NULL
; /* NULL string ?! bail out. */
22 /* find the first non-space */
23 char* start
; for (start
=str
; ((*str
) && (!isspace(*str
))); str
++);
26 if (!(*str
)) { *str
= 0; return str
; }
28 /* get the size (cannot be empty - catched above) */
29 size_t _sz
= strlen(str
);
31 /* find the proper end */
33 for (end
=(str
+_sz
-1); ((end
>str
) && (isspace(*end
))); end
--);
34 end
[1] = 0; /* terminate, just to be sure */
36 /* if we have no leading spaces, just trucate */
37 if (start
==str
) { end
++; *end
= 0; return str
; }
39 /* if it' only one char, dont need memmove for that */
40 if (start
==end
) { str
[0]=*start
; str
[1]=0; return str
; }
42 /* by here we have a (non-empty) region between start end end */
43 memmove(str
,start
,(end
-start
+1));
47 static inline void mhl_str_toupper(char* str
)
54 #define __STR_CONCAT_MAX 32
55 /* _NEVER_ call this function directly ! */
56 static inline char* __mhl_str_concat_hlp(const char* base
, ...)
58 static const char* arg_ptr
[__STR_CONCAT_MAX
];
59 static size_t arg_sz
[__STR_CONCAT_MAX
];
66 arg_sz
[0] = totalsize
= strlen(base
);
73 /* note: we use ((char*)(1)) as terminator - NULL is a valid argument ! */
74 while ((a
= va_arg(args
, char*))!=(char*)1)
79 arg_sz
[count
] = strlen(a
);
80 totalsize
+= arg_sz
[count
];
86 return mhl_str_dup("");
88 /* now as we know how much to copy, allocate the buffer */
89 char* buffer
= (char*)mhl_mem_alloc_u(totalsize
+2);
90 char* current
= buffer
;
92 for (x
=0; x
<count
; x
++)
94 memcpy(current
, arg_ptr
[x
], arg_sz
[x
]);
102 #define mhl_str_concat(...) (__mhl_str_concat_hlp(__VA_ARGS__, (char*)(1)))
104 static inline char* mhl_str_reverse(char* ptr
)
106 if (!ptr
) return NULL
; /* missing string */
107 if (!(ptr
[0] && ptr
[1])) return ptr
; /* empty or 1-ch string */
109 size_t _sz
= strlen(ptr
);
111 char* end
= ptr
+_sz
-1;
126 * strcpy is unsafe on overlapping memory areas, so define memmove-alike
127 * string function. Has sense only when dest <= src.
129 static inline char * mhl_strmove(char * dest
, const char * src
)
131 size_t n
= strlen (src
) + 1; /* + '\0' */
135 return memmove(dest
, src
, n
);
138 static inline char* mhl_str_dir_plus_file(const char* dirname
, const char* filename
)
140 /* make sure we have valid strings */
147 /* skip leading slashes on filename */
148 while (*filename
== '/')
151 /* skip trailing slashes on dirname */
152 int dnlen
= strlen(dirname
);
153 while (dnlen
&& (dirname
[dnlen
-1]=='/'))
156 int fnlen
= strlen(filename
);
157 char* buffer
= mhl_mem_alloc_z(dnlen
+fnlen
+2); /* enough space for dirname, /, filename, zero */
160 memcpy(ptr
, dirname
, dnlen
);
164 memcpy(ptr
, filename
, fnlen
);
171 #endif /* __MHL_STRING_H */