blob: 5111a13148ca2392f0719494180e866cf12a8131 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
/**
* \file string.c
* \brief Dynamic string handling.
* \author Copyright (c) 2007-2008 Jason Perkins and the Premake project
*/
#include <stdlib.h>
#include <string.h>
#include "premake.h"
#include "base/string.h"
DEFINE_CLASS(string)
{
char* contents;
int capacity;
};
/**
* Create a new dynamic string object from an existing C string.
* \param value The C string value.
* \returns A new dynamic string object containing a copy of the string.
*/
string string_create(const char* value)
{
if (value != NULL)
{
string str = ALLOC_CLASS(string);
str->capacity = strlen(value) + 1;
str->contents = (char*)malloc(str->capacity);
strcpy(str->contents, value);
return str;
}
else
{
return NULL;
}
}
/**
* Destroy a dynamic string object and free the associated memory.
* \param str The string to destroy.
*/
void string_destroy(string str)
{
if (str != NULL)
{
free(str->contents);
free(str);
}
}
/**
* Return the contents of a dynamic string as a C string.
* \param str The string to query.
* \returns The C string value.
*/
const char* string_cstr(string str)
{
if (str != NULL)
return str->contents;
else
return NULL;
}
|