makesdna: add shared utility module

Currently only a single function was duplicated which isn't so bad,
this change is to allow DNA versioning code to be shared between
dna_genfile.c and makesdna.c.
This commit is contained in:
2019-02-12 17:03:16 +11:00
parent f96bfde4a5
commit d968db82b7
6 changed files with 102 additions and 69 deletions

View File

@@ -24,6 +24,8 @@
#ifndef __DNA_GENFILE_H__
#define __DNA_GENFILE_H__
#include "intern/dna_utils.h"
struct SDNA;
/**
@@ -94,7 +96,6 @@ void *DNA_struct_reconstruct(
const struct SDNA *newsdna, const struct SDNA *oldsdna,
const char *compflags, int oldSDNAnr, int blocks, const void *data);
int DNA_elem_array_size(const char *str);
int DNA_elem_offset(struct SDNA *sdna, const char *stype, const char *vartype, const char *name);
bool DNA_struct_find(const struct SDNA *sdna, const char *stype);

View File

@@ -33,6 +33,7 @@ blender_include_dirs(
# -----------------------------------------------------------------------------
# Build makesdna executable
set(SRC
dna_utils.c
makesdna.c
../../blenlib/intern/BLI_memarena.c
../../../../intern/guardedalloc/intern/mallocn.c
@@ -78,9 +79,12 @@ set(INC_SYS
)
set(SRC
dna_utils.c
dna_genfile.c
${CMAKE_CURRENT_BINARY_DIR}/dna.c
${SRC_DNA_INC}
dna_utils.h
)
set_source_files_properties(

View File

@@ -134,48 +134,6 @@
# define MAKE_ID(a, b, c, d) ((int)(d) << 24 | (int)(c) << 16 | (b) << 8 | (a))
#endif
/* ************************* MAKE DNA ********************** */
/* allowed duplicate code from makesdna.c */
/**
* 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;
}
}
}
/* ************************* END MAKE DNA ********************** */
/* ************************* DIV ********************** */
void DNA_sdna_free(SDNA *sdna)

View File

@@ -0,0 +1,63 @@
/*
* 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 "BLI_sys_types.h"
#include "dna_utils.h"
/**
* 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;
}
}
}

View File

@@ -0,0 +1,25 @@
/*
* 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.
*/
/** \file \ingroup DNA
*/
#ifndef __DNA_UTILS_H__
#define __DNA_UTILS_H__
int DNA_elem_array_size(const char *str);
#endif /* __DNA_UTILS_H__ */

View File

@@ -49,6 +49,8 @@
#include "BLI_sys_types.h" /* for intptr_t support */
#include "BLI_memarena.h"
#include "dna_utils.h"
#define SDNA_MAX_FILENAME_LENGTH 255
/* Included the path relative from /source/blender/ here, so we can move */
@@ -209,11 +211,6 @@ static int preprocess_include(char *maindata, int len);
*/
static int convert_include(const char *filename);
/**
* Determine how many bytes are needed for an array.
*/
static int arraysize(const char *str);
/**
* Determine how many bytes are needed for each struct.
*/
@@ -723,25 +720,6 @@ static int convert_include(const char *filename)
return 0;
}
static int arraysize(const char *str)
{
int a, mul = 1;
const char *cp = NULL;
for (a = 0; str[a]; a++) {
if (str[a] == '[') {
cp = &(str[a + 1]);
}
else if (str[a] == ']' && cp) {
/* if 'cp' is a preprocessor definition, it will evaluate to 0,
* the caller needs to check for this case and throw an error */
mul *= atoi(cp);
}
}
return mul;
}
static bool check_field_alignment(int firststruct, int structtype, int type, int len,
const char *name, const char *detail)
{
@@ -798,7 +776,9 @@ static int calculate_structlens(int firststruct)
has_pointer = 1;
/* has the name an extra length? (array) */
int mul = 1;
if (cp[namelen - 1] == ']') mul = arraysize(cp);
if (cp[namelen - 1] == ']') {
mul = DNA_elem_array_size(cp);
}
if (mul == 0) {
fprintf(stderr, "Zero array size found or could not parse %s: '%.*s'\n",
@@ -843,7 +823,9 @@ static int calculate_structlens(int firststruct)
else if (typelens_native[type]) {
/* has the name an extra length? (array) */
int mul = 1;
if (cp[namelen - 1] == ']') mul = arraysize(cp);
if (cp[namelen - 1] == ']') {
mul = DNA_elem_array_size(cp);
}
if (mul == 0) {
fprintf(stderr, "Zero array size found or could not parse %s: '%.*s'\n",