/************************************************************************
* string.c
* voxelands - 3d voxel world sandbox game
* Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>
************************************************************************/

#include "common.h"

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

/* remove whitespace from beginning and end of a string */
char* trim(char* str)
{
	int l;

	while (*str && isspace(*str)) {
		str++;
	}

	l = strlen(str);
	while (--l > -1 && (!str[l] || isspace(str[l]))) {
		str[l] = 0;
	}

	return str;
}

/* allocate memory and copy a string */
char* strdup(const char* str)
{
	char* r;
	int l;

	if (!str)
		return NULL;

	l = strlen(str);
	l++;

	r = malloc(l);
	if (!r)
		return NULL;

	if (!memcpy(r,str,l)) {
		free(r);
		return NULL;
	}

	return r;
}
