Welcome to mirror list, hosted at ThFree Co, Russian Federation.

fields.c « project « src - github.com/windirstat/premake-4.x-stable.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 49e131d2802eebf7385762b75532290b394bd348 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
 * \file   fields.c
 * \brief  Project object fields enumeration and handling.
 * \author Copyright (c) 2007-2008 Jason Perkins and the Premake project
 */

#include <assert.h>
#include <stdlib.h>
#include "premake.h"
#include "fields.h"
#include "base/strings.h"


DEFINE_CLASS(Fields)
{
	Strings* values;
	int      count;
};


/**
 * Create a new, empty collection of fields.
 * \param   info    Metadata about the field collection.
 * \returns A new collection of fields.
 */
Fields fields_create(struct FieldInfo* info)
{
	int i;
	Fields fields;

	assert(info);

	fields = ALLOC_CLASS(Fields);
	
	/* figure out how many fields are in the collection */
	for (i = 0; info[i].name != NULL; ++i);
	fields->count = i;

	/* initialize the values */
	fields->values = (Strings*)malloc(sizeof(Strings) * fields->count);
	for (i = 0; i < fields->count; ++i)
	{
		fields->values[i] = strings_create();
	}

	return fields;
}


/**
 * Destroy a collection of fields and release the associated memory.
 * \param   fields   The collection of fields to destroy.
 */
void fields_destroy(Fields fields)
{
	int i;

	assert(fields);

	for (i = 0; i < fields->count; ++i)
	{
		strings_destroy(fields->values[i]);
	}
	free(fields->values);
	free(fields);
}


/**
 * Add a new value to the end of an existing list.
 * \param   fields   The collection of fields.
 * \param   index    The index of the list to contain the new value.
 * \param   value    The value to add.
 */
void fields_add_value(Fields fields, int index, const char* value)
{
	assert(fields);
	assert(index >= 0 && index < fields->count);
	strings_add(fields->values[index], value);
}


/**
 * Retrieve the value of a string (single value) field.
 * \param   fields   The collection of fields.
 * \param   index    The index of the field to query.
 * \returns The field value if set, or NULL.
 */
const char* fields_get_value(Fields fields, int index)
{
	Strings values;
	assert(fields);
	assert(index >= 0 && index < fields->count);

	values = fields->values[index];
	if (strings_size(values) > 0)
	{
		return strings_item(values, 0);
	}
	else
	{
		return NULL;
	}
}


/**
 * Retrieve the list of values for a field.
 * \param   fields    The collection of fields.
 * \param   index     The index of fields to query.
 * \returns The list of values stored in the field.
 */
Strings fields_get_values(Fields fields, int index)
{
	assert(fields);
	assert(index >= 0 && index < fields->count);
	return fields->values[index];
}


/**
 * Sets the value of a string (single value) field.
 * \param   fields   The collection of fields.
 * \param   index    The index of the field to set.
 * \param   value    The new value of the field.
 */
void fields_set_value(Fields fields, int index, const char* value)
{
	Strings values;

	assert(fields);
	assert(index >= 0 && index < fields->count);
	assert(value);

	values = fields->values[index];
	if (strings_size(values) == 0)
	{
		strings_add(values, value);
	}
	else
	{
		strings_set(values, 0, value);
	}
}


/**
 * Sets the list of values associated with a field. The field will subsequently
 * "own" the list, and take responsibility for destroying it with the field set.
 * \param   fields   The collection of fields.
 * \param   index    The index of the field to set.
 * \param   values   The list of new values for the field.
 */
void fields_set_values(Fields fields, int index, Strings values)
{
	assert(fields);
	assert(index >= 0 && index < fields->count);
	assert(values);

	if (fields->values[index] != NULL)
	{
		strings_destroy(fields->values[index]);
	}

	fields->values[index] = values;
}