[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [54755] trunk/blender/source/blender: patch [#34103] Add explanatory comments to dna_genfile.[ch]

Campbell Barton ideasman42 at gmail.com
Fri Feb 22 14:35:33 CET 2013


Revision: 54755
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=54755
Author:   campbellbarton
Date:     2013-02-22 13:35:32 +0000 (Fri, 22 Feb 2013)
Log Message:
-----------
patch [#34103] Add explanatory comments to dna_genfile.[ch]
from Lawrence D'Oliveiro (ldo)

Modified Paths:
--------------
    trunk/blender/source/blender/blenloader/intern/readfile.c
    trunk/blender/source/blender/blenloader/intern/writefile.c
    trunk/blender/source/blender/makesdna/DNA_genfile.h
    trunk/blender/source/blender/makesdna/intern/dna_genfile.c
    trunk/blender/source/blender/makesrna/intern/rna_define.c

Modified: trunk/blender/source/blender/blenloader/intern/readfile.c
===================================================================
--- trunk/blender/source/blender/blenloader/intern/readfile.c	2013-02-22 12:24:08 UTC (rev 54754)
+++ trunk/blender/source/blender/blenloader/intern/readfile.c	2013-02-22 13:35:32 UTC (rev 54755)
@@ -41,6 +41,7 @@
 #include <math.h> // for fabs
 #include <stdarg.h> /* for va_start/end */
 
+#include "BLI_utildefines.h"
 #ifndef WIN32
 #  include <unistd.h> // for read close
 #else
@@ -829,7 +830,7 @@
 	
 	for (bhead = blo_firstbhead(fd); bhead; bhead = blo_nextbhead(fd, bhead)) {
 		if (bhead->code == DNA1) {
-			int do_endian_swap = (fd->flags & FD_FLAGS_SWITCH_ENDIAN) ? 1 : 0;
+			const bool do_endian_swap = (fd->flags & FD_FLAGS_SWITCH_ENDIAN) != 0;
 			
 			fd->filesdna = DNA_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
 			if (fd->filesdna) {
@@ -952,11 +953,11 @@
 	fd->filedes = -1;
 	fd->gzfiledes = NULL;
 	
-		/* XXX, this doesn't need to be done all the time,
-		 * but it keeps us re-entrant,  remove once we have
-		 * a lib that provides a nice lock. - zr
-		 */
-	fd->memsdna = DNA_sdna_from_data(DNAstr,  DNAlen,  0);
+	/* XXX, this doesn't need to be done all the time,
+	 * but it keeps us re-entrant,  remove once we have
+	 * a lib that provides a nice lock. - zr
+	 */
+	fd->memsdna = DNA_sdna_from_data(DNAstr, DNAlen, false);
 	
 	fd->datamap = oldnewmap_new();
 	fd->globmap = oldnewmap_new();

Modified: trunk/blender/source/blender/blenloader/intern/writefile.c
===================================================================
--- trunk/blender/source/blender/blenloader/intern/writefile.c	2013-02-22 12:24:08 UTC (rev 54754)
+++ trunk/blender/source/blender/blenloader/intern/writefile.c	2013-02-22 13:35:32 UTC (rev 54755)
@@ -89,6 +89,8 @@
 #  include "BLI_winstuff.h"
 #endif
 
+#include "BLI_utildefines.h"
+
 /* allow writefile to use deprecated functionality (for forward compatibility code) */
 #define DNA_DEPRECATED_ALLOW
 
@@ -198,7 +200,7 @@
 
 	if (wd == NULL) return NULL;
 
-	wd->sdna = DNA_sdna_from_data(DNAstr, DNAlen, 0);
+	wd->sdna = DNA_sdna_from_data(DNAstr, DNAlen, false);
 
 	wd->file= file;
 

Modified: trunk/blender/source/blender/makesdna/DNA_genfile.h
===================================================================
--- trunk/blender/source/blender/makesdna/DNA_genfile.h	2013-02-22 12:24:08 UTC (rev 54754)
+++ trunk/blender/source/blender/makesdna/DNA_genfile.h	2013-02-22 13:35:32 UTC (rev 54755)
@@ -35,9 +35,13 @@
 
 struct SDNA;
 
-extern const unsigned char DNAstr[];  /* DNA.c */
-extern const int DNAlen;
+/* DNAstr contains the prebuilt SDNA structure defining the layouts of the types
+ * used by this version of Blender. It is defined in a file dna.c, which is
+ * generated by the makesdna program during the build process (see makesdna.c). */
+extern const unsigned char DNAstr[];
+extern const int DNAlen; /* length of DNAstr */
 
+/* primitive (non-struct, non-pointer/function/array) types--do not change ordering! */
 typedef enum eSDNA_Type {
 	SDNA_TYPE_CHAR     = 0,
 	SDNA_TYPE_UCHAR    = 1,
@@ -56,7 +60,7 @@
 /* define so switch statements don't complain */
 #define SDNA_TYPE_VOID 9
 
-struct SDNA *DNA_sdna_from_data(const void *data, const int datalen, int do_endian_swap);
+struct SDNA *DNA_sdna_from_data(const void *data, const int datalen, bool do_endian_swap);
 void DNA_sdna_free(struct SDNA *sdna);
 
 int DNA_struct_find_nr(struct SDNA *sdna, const char *str);

Modified: trunk/blender/source/blender/makesdna/intern/dna_genfile.c
===================================================================
--- trunk/blender/source/blender/makesdna/intern/dna_genfile.c	2013-02-22 12:24:08 UTC (rev 54754)
+++ trunk/blender/source/blender/makesdna/intern/dna_genfile.c	2013-02-22 13:35:32 UTC (rev 54755)
@@ -29,7 +29,10 @@
 /** \file blender/makesdna/intern/dna_genfile.c
  *  \ingroup DNA
  *
- * Functions for struct-dna, the genetic file dot c!
+ * Lowest-level functions for decoding the parts of a saved .blend
+ * file, including interpretation of its SDNA block and conversion of
+ * contents of other parts according to the differences between that
+ * SDNA and the SDNA of the current (running) version of Blender.
  */
 
 
@@ -134,12 +137,11 @@
  *
  */
 
-/* local */
-static int le_int(int temp);
-static short le_short(short temp);
-
 /* ************************* ENDIAN STUFF ********************** */
 
+/**
+ * converts a short between big/little endian.
+ */
 static short le_short(short temp)
 {
 	short new;
@@ -151,7 +153,9 @@
 	return new;
 }
 
-
+/**
+ * converts an int between big/little endian.
+ */
 static int le_int(int temp)
 {
 	int new;
@@ -169,6 +173,10 @@
 /* ************************* MAKE DNA ********************** */
 
 /* allowed duplicate code from makesdna.c */
+
+/**
+ * parses the "[n]" on the end of an array name and returns the number of array elements n.
+ */
 int DNA_elem_array_size(const char *astr, int len)
 {
 	int a, mul = 1;
@@ -207,14 +215,23 @@
 	MEM_freeN(sdna);
 }
 
-static int ispointer(const char *name)
+/**
+ * Return true if the name indicates a pointer of some kind.
+ */
+static bool ispointer(const char *name)
 {
 	/* check if pointer or function pointer */
 	return (name[0] == '*' || (name[0] == '(' && name[1] == '*'));
 }
 
-static int elementsize(SDNA *sdna, short type, short name)
-/* call with numbers from struct-array */
+/**
+ * Returns the size of struct fields of the specified type and name.
+ *
+ * \param type  Index into sdna->types/typelens
+ * \param name  Index into sdna->names,
+ * needed to extract possible pointer/array information.
+ */
+static int elementsize(const SDNA *sdna, short type, short name)
 {
 	int mul, namelen, len;
 	const char *cp;
@@ -225,16 +242,20 @@
 	namelen = strlen(cp);
 	/* is it a pointer or function pointer? */
 	if (ispointer(cp)) {
-		/* has the naam an extra length? (array) */
+		/* has the name an extra length? (array) */
 		mul = 1;
-		if (cp[namelen - 1] == ']') mul = DNA_elem_array_size(cp, namelen);
+		if (cp[namelen - 1] == ']') {
+			mul = DNA_elem_array_size(cp, namelen);
+		}
 		
 		len = sdna->pointerlen * mul;
 	}
 	else if (sdna->typelens[type]) {
-		/* has the naam an extra length? (array) */
+		/* has the name an extra length? (array) */
 		mul = 1;
-		if (cp[namelen - 1] == ']') mul = DNA_elem_array_size(cp, namelen);
+		if (cp[namelen - 1] == ']') {
+			mul = DNA_elem_array_size(cp, namelen);
+		}
 		
 		len = mul * sdna->typelens[type];
 		
@@ -262,6 +283,9 @@
 }
 #endif
 
+/**
+ * Returns a pointer to the start of the struct info for the struct with the specified name.
+ */
 static short *findstruct_name(SDNA *sdna, const char *str)
 {
 	int a;
@@ -272,19 +296,26 @@
 
 		sp = sdna->structs[a];
 		
-		if (strcmp(sdna->types[sp[0]], str) == 0) return sp;
+		if (strcmp(sdna->types[sp[0]], str) == 0) {
+			return sp;
+		}
 	}
 	
 	return NULL;
 }
 
+/**
+ * Returns the index of the struct info for the struct with the specified name.
+ */
 int DNA_struct_find_nr(SDNA *sdna, const char *str)
 {
 	short *sp = NULL;
 
 	if (sdna->lastfind < sdna->nr_structs) {
 		sp = sdna->structs[sdna->lastfind];
-		if (strcmp(sdna->types[sp[0]], str) == 0) return sdna->lastfind;
+		if (strcmp(sdna->types[sp[0]], str) == 0) {
+			return sdna->lastfind;
+		}
 	}
 
 #ifdef WITH_DNA_GHASH
@@ -311,8 +342,10 @@
 
 /* ************************* READ DNA ********************** */
 
-static void init_structDNA(SDNA *sdna, int do_endian_swap)
-/* in sdna->data the data, now we convert that to something understandable */
+/**
+ * In sdna->data the data, now we convert that to something understandable
+ */
+static void init_structDNA(SDNA *sdna, bool do_endian_swap)
 {
 	int *data, *verg, gravity_fix = -1;
 	intptr_t nr;
@@ -481,7 +514,7 @@
 
 		if (sp[1] != 2 || (sdna->pointerlen != 4 && sdna->pointerlen != 8)) {
 			printf("ListBase struct error! Needs it to calculate pointerize.\n");
-			exit(0);
+			exit(1);
 			/* well, at least sizeof(ListBase) is error proof! (ton) */
 		}
 		
@@ -506,7 +539,10 @@
 	}
 }
 
-SDNA *DNA_sdna_from_data(const void *data, const int datalen, int do_endian_swap)
+/**
+ * Constructs and returns a decoded SDNA structure from the given encoded SDNA data block.
+ */
+SDNA *DNA_sdna_from_data(const void *data, const int datalen, bool do_endian_swap)
 {
 	SDNA *sdna = MEM_mallocN(sizeof(*sdna), "sdna");
 	
@@ -525,7 +561,11 @@
 
 /* ******************* HANDLE DNA ***************** */
 
-static void recurs_test_compflags(SDNA *sdna, char *compflags, int structnr)
+/**
+ * Used by #DNA_struct_get_compareflags (below) to recursively mark all structs
+ * containing a field of type structnr as changed between old and current SDNAs.
+ */
+static void recurs_test_compflags(const SDNA *sdna, char *compflags, int structnr)
 {
 	int a, b, typenr, elems;
 	short *sp;
@@ -554,75 +594,70 @@
 	
 }
 
-/* Unsure of exact function - compares the sdna argument to
- * newsdna and sets up the information necessary to convert
- * data written with a dna of oldsdna to in-memory data with a
- * structure defined by the newsdna sdna (I think). -zr
- */
 
-/* well, the function below is just a lookup table to speed
- * up reading files. doh! -ton
+/**
+ * Constructs and returns an array of byte flags with one element for each struct in oldsdna,
+ * indicating how it compares to newsdna:
+ *
+ * flag value:
+ * - 0  Struct has disappeared (values of this struct type will not be loaded by the current Blender)
+ * - 1  Struct is the same (can be loaded with straight memory copy after any necessary endian conversion)
+ * - 2  Struct is different in some way (needs to be copied/converted field by field)
  */
-
-
-char *DNA_struct_get_compareflags(SDNA *sdna, SDNA *newsdna)
+char *DNA_struct_get_compareflags(SDNA *oldsdna, SDNA *newsdna)
 {
-	/* flag: 0: doesn't exist anymore (or not yet)
-	 *       1: is equal
-	 *       2: is different
-	 */
 	int a, b;
-	short *spold, *spcur;
+	const short *sp_old, *sp_new;
 	const char *str1, *str2;
 	char *compflags;
 	
-	if (sdna->nr_structs == 0) {
+	if (oldsdna->nr_structs == 0) {
 		printf("error: file without SDNA\n");
 		return NULL;
 	}
-		
-	compflags = MEM_callocN(sdna->nr_structs, "compflags");
 
-	/* we check all structs in 'sdna' and compare them with 
+	compflags = MEM_callocN(oldsdna->nr_structs, "compflags");
+
+	/* we check all structs in 'oldsdna' and compare them with 
 	 * the structs in 'newsdna'
 	 */
 	
-	for (a = 0; a < sdna->nr_structs; a++) {

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list