config.c
[voxelands-alt.git] / src / lib / string.c
blob1d911446b9cc009ffb00719e660f8c1e06148210
1 /************************************************************************
2 * string.c
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
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.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
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 ************************************************************************/
20 #include "common.h"
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ctype.h>
26 /* remove whitespace from beginning and end of a string */
27 /* TODO: utf8 support? */
28 char* trim(char* str)
30 int l;
32 while (*str && isspace(*str)) {
33 str++;
36 l = strlen(str);
37 while (--l > -1 && (!str[l] || isspace(str[l]))) {
38 str[l] = 0;
41 return str;
44 /* allocate memory and copy a string */
45 char* strdup(const char* str)
47 char* r;
48 int l;
50 if (!str)
51 return NULL;
53 l = strlen(str);
54 l++;
56 r = malloc(l);
57 if (!r)
58 return NULL;
60 if (!memcpy(r,str,l)) {
61 free(r);
62 return NULL;
65 return r;