Plugin API: add malloc()
[MonkeyD.git] / src / str.c
blob49a5a6de03ff4ed4a42a066ac267570206181709
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2008, Eduardo Silva P.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #define _GNU_SOURCE
23 #include <string.h>
25 #include <ctype.h>
26 #include <stdlib.h>
28 #include "request.h"
29 #include "utils.h"
30 #include "memory.h"
32 #include <stdio.h>
34 /* Return a buffer with a new string from string */
35 char *mk_string_copy_substr(const char *string, int pos_init, int pos_end)
37 unsigned int size, bytes;
38 char *buffer=0;
40 size = (unsigned int) (pos_end - pos_init ) + 1;
41 if(size<=2) size=4;
43 buffer = malloc(size);
45 if(!buffer){
46 return NULL;
49 if(pos_init > pos_end)
51 mk_mem_free(buffer);
52 return NULL;
55 bytes = pos_end - pos_init;
56 memcpy(buffer, string+pos_init, bytes);
57 buffer[bytes]='\0';
59 return (char *) buffer;
62 int mk_string_char_search(char *string, int c, int n)
64 int i;
66 if(n<0)
68 n = strlen(string);
71 for(i=0; i<n; i++)
73 if(string[i]==c)
74 return i;
77 return -1;
79 /* Get position of a substring.
80 * Original version taken from google, modified in order
81 * to send the position instead the substring.
84 int _mk_string_search(char *string, char *search, int n)
86 char *np;
87 int res;
89 np = strcasestr(string, search);
90 if(!np)
92 return -1;
95 res = np-string;
96 if(res>n && n>=0)
98 return -1;
100 return (np-string);
103 int mk_string_search(char *string, char *search)
105 return _mk_string_search(string, search, -1);
108 /* lookup char in reverse order */
109 int mk_string_search_r(char *string, char search, int n)
111 int i,j;
113 if(n>=0){
114 j = n;
116 else{
117 j = strlen(string);
120 for(i=j;i>=0;i--)
122 if(string[i]==search){
123 return i;
127 return -1;
130 int mk_string_search_n(char *string, char *search, int n)
132 return _mk_string_search(string, search, n);
136 char *mk_string_remove_space(char *buf)
138 size_t bufsize;
139 int new_i=0, i, len, spaces=0;
140 char *new_buf=0;
142 len = strlen(buf);
143 for(i=0; i<len; i++)
145 if(buf[i] == ' '){
146 spaces++;
150 bufsize = len+1-spaces;
151 if(bufsize <= 1){
152 return NULL;
155 new_buf = mk_mem_malloc(bufsize);
157 for(i=0; i<len; i++)
159 if(buf[i] != ' '){
160 new_buf[new_i] = buf[i];
161 new_i++;
165 return new_buf;
168 char *mk_string_casestr(char *heystack, char *needle)
170 if(!heystack || !needle)
172 return NULL;
175 return strcasestr(heystack, needle);
178 char *mk_string_dup(const char *s)
180 if(!s)
181 return NULL;
183 return strdup(s);
186 int mk_string_array_count(char *arr[])
188 int i=0;
190 for(i=0; arr[i]; i++){}
191 return i;