Cleanup: move utility functions into dna_utils
authorCampbell Barton <ideasman42@gmail.com>
Tue, 12 Feb 2019 06:56:35 +0000 (17:56 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 12 Feb 2019 06:56:35 +0000 (17:56 +1100)
Rename old/new to src/dst since renaming happens in both directions
when versioning.

source/blender/makesdna/intern/dna_genfile.c
source/blender/makesdna/intern/dna_utils.c
source/blender/makesdna/intern/dna_utils.h

index 04fd759..6d4eab1 100644 (file)
@@ -1367,39 +1367,6 @@ int DNA_elem_type_size(const eSDNA_Type elem_nr)
 /** \name Version Patch DNA
  * \{ */
 
-static bool is_identifier(const char c)
-{
-       return ((c >= 'a' && c <= 'z') ||
-               (c >= 'A' && c <= 'Z') ||
-               (c >= '0' && c <= '9') ||
-               (c == '_'));
-}
-
-/**
- * Check if 'var' matches '*var[3]' for eg,
- * return true if it does, with start/end offsets.
- */
-static char str_member_match(
-        const char *member_search, const int member_search_len,
-        const char *member_dna,
-        uint *r_member_dna_offset)
-{
-       BLI_assert(strlen(member_search) == member_search_len);
-       int member_dna_offset = 0;
-       while (!is_identifier(member_dna[member_dna_offset])) {
-               member_dna_offset++;
-       }
-       const char *member_dna_trim = member_dna + member_dna_offset;
-       if (strncmp(member_search, member_dna_trim, member_search_len) == 0) {
-               const char c = member_dna_trim[member_search_len];
-               if (c == '\0' || !is_identifier(c)) {
-                       *r_member_dna_offset = member_dna_offset;
-                       return true;
-               }
-       }
-       return false;
-}
-
 static bool DNA_sdna_patch_struct_nr(
         SDNA *sdna, const int struct_name_old_nr, const char *struct_name_new)
 {
@@ -1438,28 +1405,18 @@ static bool DNA_sdna_patch_struct_member_nr(
                const char *member_dna_old = sdna->names[sp[1]];
                /* Start & end offsets in 'member_dna_old'. */
                uint member_dna_offset_start;
-               if (str_member_match(member_old, member_old_len, member_dna_old, &member_dna_offset_start)) {
+               if (DNA_elem_id_match(member_old, member_old_len, member_dna_old, &member_dna_offset_start)) {
                        if (sdna->mem_arena == NULL) {
                                sdna->mem_arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
                        }
                        const int member_dna_old_len = strlen(member_dna_old);
-                       const int member_final_len = (member_dna_old_len - member_old_len) + member_new_len;
-                       char *member_dna_new = BLI_memarena_alloc(sdna->mem_arena, member_final_len + 1);
-                       uint i = 0;
-                       if (member_dna_offset_start != 0) {
-                               memcpy(member_dna_new, member_dna_old, member_dna_offset_start);
-                               i = member_dna_offset_start;
-                       }
-                       memcpy(&member_dna_new[i], member_new, member_new_len + 1);
-                       i += member_new_len;
-                       uint member_dna_offset_end = member_dna_offset_start + member_old_len;
-                       if (member_dna_old[member_dna_offset_end] != '\0') {
-                               const int member_dna_tail_len = (member_dna_old_len - member_dna_offset_end);
-                               memcpy(&member_dna_new[i], &member_dna_old[member_dna_offset_end], member_dna_tail_len + 1);
-                               i += member_dna_tail_len;
-                       }
-                       BLI_assert((strlen(member_dna_new) == member_final_len) && (i == member_final_len));
-                       // printf("Struct member rename: '%s.%s' -> '%s'\n", member_old, member_dna_old, member_dna_new);
+                       const char *member_dna_new = DNA_elem_id_rename(
+                               sdna->mem_arena,
+                               member_old, member_old_len,
+                               member_new, member_new_len,
+                               member_dna_old, member_dna_old_len,
+                               member_dna_offset_start);
+
                        sdna->names[sp[1]] = member_dna_new;
                        return true;
                }
index 515315c..f1a04ea 100644 (file)
  * 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 ...`.
@@ -61,3 +71,94 @@ int DNA_elem_array_size(const char *str)
                }
        }
 }
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \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;
+}
+
+/** \} */
index 69b265b..38fce41 100644 (file)
 #ifndef __DNA_UTILS_H__
 #define __DNA_UTILS_H__
 
+struct MemArena;
+
 int DNA_elem_array_size(const char *str);
 
+uint DNA_elem_id_offset_start(const char *elem_dna);
+uint DNA_elem_id_offset_end(const char *elem_dna);
+bool DNA_elem_id_match(
+        const char *elem_search, const int elem_search_len,
+        const char *elem_dna,
+        uint *r_elem_dna_offset);
+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);
+
 #endif /* __DNA_UTILS_H__ */