From d968db82b7aac37cd788a490997376004b5ce9eb Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 12 Feb 2019 17:03:16 +1100 Subject: 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. --- source/blender/makesdna/intern/CMakeLists.txt | 4 ++ source/blender/makesdna/intern/dna_genfile.c | 42 ------------------ source/blender/makesdna/intern/dna_utils.c | 63 +++++++++++++++++++++++++++ source/blender/makesdna/intern/dna_utils.h | 25 +++++++++++ source/blender/makesdna/intern/makesdna.c | 34 ++++----------- 5 files changed, 100 insertions(+), 68 deletions(-) create mode 100644 source/blender/makesdna/intern/dna_utils.c create mode 100644 source/blender/makesdna/intern/dna_utils.h (limited to 'source/blender/makesdna/intern') diff --git a/source/blender/makesdna/intern/CMakeLists.txt b/source/blender/makesdna/intern/CMakeLists.txt index ad84ffc68c1..4d6ad4b4389 100644 --- a/source/blender/makesdna/intern/CMakeLists.txt +++ b/source/blender/makesdna/intern/CMakeLists.txt @@ -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( diff --git a/source/blender/makesdna/intern/dna_genfile.c b/source/blender/makesdna/intern/dna_genfile.c index 3d0f4fa1aab..04fd7596e1b 100644 --- a/source/blender/makesdna/intern/dna_genfile.c +++ b/source/blender/makesdna/intern/dna_genfile.c @@ -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) diff --git a/source/blender/makesdna/intern/dna_utils.c b/source/blender/makesdna/intern/dna_utils.c new file mode 100644 index 00000000000..515315cce00 --- /dev/null +++ b/source/blender/makesdna/intern/dna_utils.c @@ -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; + } + } +} diff --git a/source/blender/makesdna/intern/dna_utils.h b/source/blender/makesdna/intern/dna_utils.h new file mode 100644 index 00000000000..69b265be27a --- /dev/null +++ b/source/blender/makesdna/intern/dna_utils.h @@ -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__ */ diff --git a/source/blender/makesdna/intern/makesdna.c b/source/blender/makesdna/intern/makesdna.c index f957bf637c3..e2616d91b27 100644 --- a/source/blender/makesdna/intern/makesdna.c +++ b/source/blender/makesdna/intern/makesdna.c @@ -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", -- cgit v1.2.3