Change the FSF address and update LICENSE with the new address and some texts
[MonkeyD.git] / src / str.c
blob0832b5bd36332afbef338c0ecf02d933f6810463
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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"
31 #include "str.h"
33 #include <stdio.h>
35 /* Return a buffer with a new string from string */
36 char *mk_string_copy_substr(const char *string, int pos_init, int pos_end)
38 unsigned int size, bytes;
39 char *buffer = 0;
41 size = (unsigned int) (pos_end - pos_init) + 1;
42 if (size <= 2)
43 size = 4;
45 buffer = malloc(size);
47 if (!buffer) {
48 return NULL;
51 if (pos_init > pos_end) {
52 mk_mem_free(buffer);
53 return NULL;
56 bytes = pos_end - pos_init;
57 memcpy(buffer, string + pos_init, bytes);
58 buffer[bytes] = '\0';
60 return (char *) buffer;
63 int mk_string_char_search(char *string, int c, int n)
65 int i;
67 if (n < 0) {
68 n = strlen(string);
71 for (i = 0; i < n; i++) {
72 if (string[i] == c)
73 return i;
76 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) {
91 return -1;
94 res = np - string;
95 if (res > n && n >= 0) {
96 return -1;
98 return (np - string);
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)
109 int i, j;
111 if (n >= 0) {
112 j = n;
114 else {
115 j = strlen(string);
118 for (i = j; i >= 0; i--) {
119 if (string[i] == search) {
120 return i;
124 return -1;
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)
134 size_t bufsize;
135 int new_i = 0, i, len, spaces = 0;
136 char *new_buf = 0;
138 len = strlen(buf);
139 for (i = 0; i < len; i++) {
140 if (buf[i] == ' ') {
141 spaces++;
145 bufsize = len + 1 - spaces;
146 if (bufsize <= 1) {
147 return NULL;
150 new_buf = mk_mem_malloc(bufsize);
152 for (i = 0; i < len; i++) {
153 if (buf[i] != ' ') {
154 new_buf[new_i] = buf[i];
155 new_i++;
159 return new_buf;
162 char *mk_string_casestr(char *heystack, char *needle)
164 if (!heystack || !needle) {
165 return NULL;
168 return strcasestr(heystack, needle);
171 char *mk_string_dup(const char *s)
173 if (!s)
174 return NULL;
176 return strdup(s);
179 int mk_string_array_count(char *arr[])
181 int i = 0;
183 for (i = 0; arr[i]; i++) {
185 return i;
188 struct mk_string_line *mk_string_split_line(char *line)
190 unsigned int i = 0, len, val_len;
191 int end;
192 char *val;
193 struct mk_string_line *sl = 0, *new, *p;
195 if (!line) {
196 return NULL;
199 len = strlen(line);
201 while (i < len) {
202 end = mk_string_char_search(line + i, ' ', len - i);
204 if (end >= 0 && end + i < len) {
205 end += i;
206 val = mk_string_copy_substr(line, i, end);
207 val_len = end - i;
209 else {
210 val = mk_string_copy_substr(line, i, len);
211 val_len = len - i;
212 end = len;
216 /* Alloc node */
217 new = mk_mem_malloc(sizeof(struct mk_string_line));
218 new->val = val;
219 new->len = val_len;
220 new->next = NULL;
222 /* Link node */
223 if (!sl) {
224 sl = new;
226 else {
227 p = sl;
228 while (p->next) {
229 p = p->next;
232 p->next = new;
234 i = end + 1;
237 return sl;