blob: 09db9c910f5ed91e39ff6130dd47f696d2d630c1 (
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;
}
|