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

dna_utils.c « intern « makesdna « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f1a04eae7aa9aa246b1345b5851b23f8e55baf0d (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
/*
 * 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 2
 * 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, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Copyright (C) 2018 Blender Foundation.
 */

/** \file \ingroup DNA
 *
 * Utilities for stand-alone makesdna.c and Blender to share.
 */

#include <string.h>

#include "BLI_sys_types.h"
#include "BLI_utildefines.h"
#include "BLI_assert.h"

#include "BLI_memarena.h"

#include "dna_utils.h"

/* -------------------------------------------------------------------- */
/** \name Struct Member Evaluation
 * \{ */

/**
 * Parses the `[n1][n2]...` on the end of an array name
 * and returns the number of array elements `n1 * n2 ...`.
 */
int DNA_elem_array_size(const char *str)
{
	int result = 1;
	int current = 0;
	while (true) {
		char c = *str++;
		switch (c) {
			case '\0':
				return result;
			case '[':
				current = 0;
				break;
			case ']':
				result *= current;
				break;
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
				current = current * 10 + (c - '0');
				break;
			default:
				break;
		}
	}
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Struct Member Manipulation
 * \{ */

static bool is_identifier(const char c)
{
	return ((c >= 'a' && c <= 'z') ||
	        (c >= 'A' && c <= 'Z') ||
	        (c >= '0' && c <= '9') ||
	        (c == '_'));
}

uint DNA_elem_id_offset_start(const char *elem_dna)
{
	uint elem_dna_offset = 0;
	while (!is_identifier(elem_dna[elem_dna_offset])) {
		elem_dna_offset++;
	}
	return elem_dna_offset;
}

uint DNA_elem_id_offset_end(const char *elem_dna)
{
	uint elem_dna_offset = 0;
	while (is_identifier(elem_dna[elem_dna_offset])) {
		elem_dna_offset++;
	}
	return elem_dna_offset;
}

/**
 * Check if 'var' matches '*var[3]' for eg,
 * return true if it does, with start/end offsets.
 */
bool DNA_elem_id_match(
        const char *elem_search, const int elem_search_len,
        const char *elem_dna,
        uint *r_elem_dna_offset)
{
	BLI_assert(strlen(elem_search) == elem_search_len);
	const uint elem_dna_offset = DNA_elem_id_offset_start(elem_dna);
	const char *elem_dna_trim = elem_dna + elem_dna_offset;
	if (strncmp(elem_search, elem_dna_trim, elem_search_len) == 0) {
		const char c = elem_dna_trim[elem_search_len];
		if (c == '\0' || !is_identifier(c)) {
			*r_elem_dna_offset = elem_dna_offset;
			return true;
		}
	}
	return false;
}

/**
 * Return a renamed dna name, allocated from \a mem_arena.
 */
char *DNA_elem_id_rename(
        struct MemArena *mem_arena,
        const char *elem_src, const int elem_src_len,
        const char *elem_dst, const int elem_dst_len,
        const char *elem_dna_src, const int elem_dna_src_len,
        const uint elem_dna_offset_start)
{
	BLI_assert(strlen(elem_src) == elem_src_len);
	BLI_assert(strlen(elem_dst) == elem_dst_len);
	BLI_assert(strlen(elem_dna_src) == elem_dna_src_len);
	BLI_assert(DNA_elem_id_offset_start(elem_dna_src) == elem_dna_offset_start);
	UNUSED_VARS_NDEBUG(elem_src);

	const int elem_final_len = (elem_dna_src_len - elem_src_len) + elem_dst_len;
	char *elem_dna_dst = BLI_memarena_alloc(mem_arena, elem_final_len + 1);
	uint i = 0;
	if (elem_dna_offset_start != 0) {
		memcpy(elem_dna_dst, elem_dna_src, elem_dna_offset_start);
		i = elem_dna_offset_start;
	}
	memcpy(&elem_dna_dst[i], elem_dst, elem_dst_len + 1);
	i += elem_dst_len;
	uint elem_dna_offset_end = elem_dna_offset_start + elem_src_len;
	if (elem_dna_src[elem_dna_offset_end] != '\0') {
		const int elem_dna_tail_len = (elem_dna_src_len - elem_dna_offset_end);
		memcpy(&elem_dna_dst[i], &elem_dna_src[elem_dna_offset_end], elem_dna_tail_len + 1);
		i += elem_dna_tail_len;
	}
	BLI_assert((strlen(elem_dna_dst) == elem_final_len) && (i == elem_final_len));
	return elem_dna_dst;
}

/** \} */