1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
5 * Copyright (C) 2001-2010, Eduardo Silva P. <edsiper@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
36 /* Return a buffer with a new string from string */
37 char *mk_string_copy_substr(const char *string
, int pos_init
, int pos_end
)
39 unsigned int size
, bytes
;
42 size
= (unsigned int) (pos_end
- pos_init
) + 1;
46 buffer
= malloc(size
);
52 if (pos_init
> pos_end
) {
57 bytes
= pos_end
- pos_init
;
58 memcpy(buffer
, string
+ pos_init
, bytes
);
61 return (char *) buffer
;
64 int mk_string_char_search(char *string
, int c
, int n
)
72 for (i
= 0; i
< n
; i
++) {
80 /* Get position of a substring.
81 * Original version taken from google, modified in order
82 * to send the position instead the substring.
84 int _mk_string_search(char *string
, char *search
, int n
)
89 np
= strcasestr(string
, search
);
95 if (res
> n
&& n
>= 0) {
101 int mk_string_search(char *string
, char *search
)
103 return _mk_string_search(string
, search
, -1);
106 /* lookup char in reverse order */
107 int mk_string_search_r(char *string
, char search
, int n
)
118 for (i
= j
; i
>= 0; i
--) {
119 if (string
[i
] == search
) {
127 int mk_string_search_n(char *string
, char *search
, int n
)
129 return _mk_string_search(string
, search
, n
);
132 char *mk_string_remove_space(char *buf
)
135 int new_i
= 0, i
, len
, spaces
= 0;
139 for (i
= 0; i
< len
; i
++) {
145 bufsize
= len
+ 1 - spaces
;
150 new_buf
= mk_mem_malloc(bufsize
);
152 for (i
= 0; i
< len
; i
++) {
154 new_buf
[new_i
] = buf
[i
];
162 char *mk_string_casestr(char *heystack
, char *needle
)
164 if (!heystack
|| !needle
) {
168 return strcasestr(heystack
, needle
);
171 char *mk_string_dup(const char *s
)
179 int mk_string_array_count(char *arr
[])
183 for (i
= 0; arr
[i
]; i
++) {
188 struct mk_string_line
*mk_string_split_line(char *line
)
190 unsigned int i
= 0, len
, val_len
;
193 struct mk_string_line
*sl
= 0, *new, *p
;
202 end
= mk_string_char_search(line
+ i
, ' ', len
- i
);
204 if (end
>= 0 && end
+ i
< len
) {
212 val
= mk_string_copy_substr(line
, i
, end
);
216 val
= mk_string_copy_substr(line
, i
, len
);
223 new = mk_mem_malloc(sizeof(struct mk_string_line
));
246 char *mk_string_build(char **buffer
, unsigned long *len
,
247 const char *format
, ...)
252 static size_t _mem_alloc
= 64;
255 /* *buffer *must* be an empty/NULL buffer */
257 *buffer
= (char *) mk_mem_malloc(_mem_alloc
);
263 va_start(ap
, format
);
264 length
= vsnprintf(*buffer
, alloc
, format
, ap
);
266 if (length
>= alloc
) {
267 ptr
= realloc(*buffer
, length
+ 1);
274 length
= vsnprintf(*buffer
, alloc
, format
, ap
);