From 41aa651d218353900e3bc0f070891f47ed31cb7b Mon Sep 17 00:00:00 2001
From: Felipe Castro <fefcas@gmail.com>
Date: Sat, 17 Aug 2013 17:26:49 -0300
Subject: [PATCH 23/23] | [build][examples] Added dumping for header
 variables.

---
 examples/Makefile.am         |    8 +-
 examples/dump.c              |  462 +++++++++++++++++++++---------------------
 examples/dump_objects.in.c   |    6 +-
 examples/dump_variables.in.c |   72 +++++++
 src/dwg.c                    |  271 ++++++++++++-------------
 src/dwg.h                    |    2 +
 6 files changed, 452 insertions(+), 369 deletions(-)
 create mode 100644 examples/dump_variables.in.c

diff --git a/examples/Makefile.am b/examples/Makefile.am
index 1e96d55..4c30739 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -38,7 +38,7 @@ get_bmp_SOURCES = get_bmp.c
 
 dump_SOURCES = dump.c
 
-BUILT_SOURCES = dump_objects.c
+BUILT_SOURCES = dump_objects.c dump_variables.c
 
 AM_CFLAGS = -Wextra -I$(top_srcdir)/src
 
@@ -48,8 +48,12 @@ TESTS_ENVIRONMENT = \
 
 TESTS = alive.test
 
-EXTRA_DIST = example.dwg sample.dwg suffix.c load_dwg.py dump_objects.in.c $(TESTS)
+EXTRA_DIST = example.dwg sample.dwg suffix.c load_dwg.py dump_objects.in.c  dump_variables.in.c $(TESTS)
 
 dump_objects.c: dump_objects.in.c
 	$(CPP) -P $(AM_CFLAGS) dump_objects.in.c > dump_objects.c
 	indent dump_objects.c
+
+dump_variables.c: dump_variables.in.c
+	$(CPP) -P $(AM_CFLAGS) dump_variables.in.c > dump_variables.c
+	indent dump_variables.c
diff --git a/examples/dump.c b/examples/dump.c
index 59c017e..6aa7593 100644
--- a/examples/dump.c
+++ b/examples/dump.c
@@ -25,253 +25,263 @@
 #include "dwg.h"
 #include "logging.h"
 
-#include "dump_objects.c"
+#include "dump_variables.c"
 
+#include "dump_objects.c"
 void
-dump_object (Dwg_Object * obj)
+dump_objects (Dwg_Data * dwg)
 {
-  Dwg_Version_Type dwg_version = obj->parent->header.version;
+  unsigned int i;
+  Dwg_Version_Type dwg_version = dwg->header.version;
+  Dwg_Object *obj;
 
-  switch (obj->type)
+  for (i = 0; i < dwg->num_objects; i++)
     {
-    case DWG_TYPE_TEXT:
-      dump_TEXT (obj);
-      break;
-    case DWG_TYPE_ATTRIB:
-      dump_ATTRIB (obj);
-      break;
-    case DWG_TYPE_ATTDEF:
-      dump_ATTDEF (obj);
-      break;
-    case DWG_TYPE_BLOCK:
-      dump_BLOCK (obj);
-      break;
-    case DWG_TYPE_ENDBLK:
-      dump_ENDBLK (obj);
-      break;
-    case DWG_TYPE_SEQEND:
-      dump_SEQEND (obj);
-      break;
-    case DWG_TYPE_INSERT:
-      dump_INSERT (obj);
-      break;
-    case DWG_TYPE_MINSERT:
-      dump_MINSERT (obj);
-      break;
-    case DWG_TYPE_VERTEX_2D:
-      dump_VERTEX_2D (obj);
-      break;
-    case DWG_TYPE_VERTEX_3D:
-      dump_VERTEX_3D (obj);
-      break;
-    case DWG_TYPE_VERTEX_MESH:
-      dump_VERTEX_MESH (obj);
-      break;
-    case DWG_TYPE_VERTEX_PFACE:
-      dump_VERTEX_PFACE (obj);
-      break;
-    case DWG_TYPE_VERTEX_PFACE_FACE:
-      dump_VERTEX_PFACE_FACE (obj);
-      break;
-    case DWG_TYPE_POLYLINE_2D:
-      dump_POLYLINE_2D (obj);
-      break;
-    case DWG_TYPE_POLYLINE_3D:
-      dump_POLYLINE_3D (obj);
-      break;
-    case DWG_TYPE_ARC:
-      dump_ARC (obj);
-      break;
-    case DWG_TYPE_CIRCLE:
-      dump_CIRCLE (obj);
-      break;
-    case DWG_TYPE_LINE:
-      dump_LINE (obj);
-      break;
-    case DWG_TYPE_DIMENSION_ORDINATE:
-      dump_DIMENSION_ORDINATE (obj);
-      break;
-    case DWG_TYPE_DIMENSION_LINEAR:
-      dump_DIMENSION_LINEAR (obj);
-      break;
-    case DWG_TYPE_DIMENSION_ALIGNED:
-      dump_DIMENSION_ALIGNED (obj);
-      break;
-    case DWG_TYPE_DIMENSION_ANG3PT:
-      dump_DIMENSION_ANG3PT (obj);
-      break;
-    case DWG_TYPE_DIMENSION_ANG2LN:
-      dump_DIMENSION_ANG2LN (obj);
-      break;
-    case DWG_TYPE_DIMENSION_RADIUS:
-      dump_DIMENSION_RADIUS (obj);
-      break;
-    case DWG_TYPE_DIMENSION_DIAMETER:
-      dump_DIMENSION_DIAMETER (obj);
-      break;
-    case DWG_TYPE_POINT:
-      dump_POINT (obj);
-      break;
-    case DWG_TYPE__3DFACE:
-      dump__3DFACE (obj);
-      break;
-    case DWG_TYPE_POLYLINE_PFACE:
-      dump_POLYLINE_PFACE (obj);
-      break;
-    case DWG_TYPE_POLYLINE_MESH:
-      dump_POLYLINE_MESH (obj);
-      break;
-    case DWG_TYPE_SOLID:
-      dump_SOLID (obj);
-      break;
-    case DWG_TYPE_TRACE:
-      dump_TRACE (obj);
-      break;
-    case DWG_TYPE_SHAPE:
-      dump_SHAPE (obj);
-      break;
-    case DWG_TYPE_VIEWPORT:
-      dump_VIEWPORT (obj);
-      break;
-    case DWG_TYPE_ELLIPSE:
-      dump_ELLIPSE (obj);
-      break;
-    case DWG_TYPE_SPLINE:
-      dump_SPLINE (obj);
-      break;
-    case DWG_TYPE_REGION:
-      dump_REGION (obj);
-      break;
-    case DWG_TYPE_3DSOLID:
-      dump__3DSOLID (obj);
-      break;
-      /* Check the type of the object */
-    case DWG_TYPE_BODY:
-      dump_BODY (obj);
-      break;
-    case DWG_TYPE_RAY:
-      dump_RAY (obj);
-      break;
-    case DWG_TYPE_XLINE:
-      dump_XLINE (obj);
-      break;
-    case DWG_TYPE_DICTIONARY:
-      dump_DICTIONARY (obj);
-      break;
-    case DWG_TYPE_MTEXT:
-      dump_MTEXT (obj);
-      break;
-    case DWG_TYPE_LEADER:
-      dump_LEADER (obj);
-      break;
-    case DWG_TYPE_TOLERANCE:
-      dump_TOLERANCE (obj);
-      break;
-    case DWG_TYPE_MLINE:
-      dump_MLINE (obj);
-      break;
-    case DWG_TYPE_BLOCK_CONTROL:
-      dump_BLOCK_CONTROL (obj);
-      break;
-    case DWG_TYPE_BLOCK_HEADER:
-      dump_BLOCK_HEADER (obj);
-      break;
-    case DWG_TYPE_LAYER_CONTROL:
-      dump_LAYER_CONTROL (obj);
-      break;
-    case DWG_TYPE_LAYER:
-      dump_LAYER (obj);
-      break;
-    case DWG_TYPE_SHAPEFILE_CONTROL:
-      dump_SHAPEFILE_CONTROL (obj);
-      break;
-    case DWG_TYPE_SHAPEFILE:
-      dump_SHAPEFILE (obj);
-      break;
-    case DWG_TYPE_LTYPE_CONTROL:
-      dump_LTYPE_CONTROL (obj);
-      break;
-    case DWG_TYPE_LTYPE:
-      dump_LTYPE (obj);
-      break;
-    case DWG_TYPE_VIEW_CONTROL:
-      dump_VIEW_CONTROL (obj);
-      break;
-    case DWG_TYPE_VIEW:
-      dump_VIEW (obj);
-      break;
-    case DWG_TYPE_UCS_CONTROL:
-      dump_UCS_CONTROL (obj);
-      break;
-    case DWG_TYPE_UCS:
-      dump_UCS (obj);
-      break;
-    case DWG_TYPE_VPORT_CONTROL:
-      dump_VPORT_CONTROL (obj);
-      break;
-    case DWG_TYPE_VPORT:
-      dump_VPORT (obj);
-      break;
-    case DWG_TYPE_APPID_CONTROL:
-      dump_APPID_CONTROL (obj);
-      break;
-    case DWG_TYPE_APPID:
-      dump_APPID (obj);
-      break;
-    case DWG_TYPE_DIMSTYLE_CONTROL:
-      dump_DIMSTYLE_CONTROL (obj);
-      break;
-    case DWG_TYPE_DIMSTYLE:
-      dump_DIMSTYLE (obj);
-      break;
-    case DWG_TYPE_VP_ENT_HDR_CONTROL:
-      dump_VP_ENT_HDR_CONTROL (obj);
-      break;
-    case DWG_TYPE_VP_ENT_HDR:
-      dump_VP_ENT_HDR (obj);
-      break;
-    case DWG_TYPE_GROUP:
-      dump_GROUP (obj);
-      break;
-    case DWG_TYPE_MLINESTYLE:
-      dump_MLINESTYLE (obj);
-      break;
-    default:
-      if (obj->type == obj->parent->dwg_ot_layout)
-	dump_LAYOUT (obj);
-      else
-	printf ("Unknown type: %u\n", obj->type);
+      obj = &dwg->object[i];
+      printf ("[%u] ", i);
+
+      switch (obj->type)
+	{
+	case DWG_TYPE_TEXT:
+	  dump_TEXT (obj);
+	  break;
+	case DWG_TYPE_ATTRIB:
+	  dump_ATTRIB (obj);
+	  break;
+	case DWG_TYPE_ATTDEF:
+	  dump_ATTDEF (obj);
+	  break;
+	case DWG_TYPE_BLOCK:
+	  dump_BLOCK (obj);
+	  break;
+	case DWG_TYPE_ENDBLK:
+	  dump_ENDBLK (obj);
+	  break;
+	case DWG_TYPE_SEQEND:
+	  dump_SEQEND (obj);
+	  break;
+	case DWG_TYPE_INSERT:
+	  dump_INSERT (obj);
+	  break;
+	case DWG_TYPE_MINSERT:
+	  dump_MINSERT (obj);
+	  break;
+	case DWG_TYPE_VERTEX_2D:
+	  dump_VERTEX_2D (obj);
+	  break;
+	case DWG_TYPE_VERTEX_3D:
+	  dump_VERTEX_3D (obj);
+	  break;
+	case DWG_TYPE_VERTEX_MESH:
+	  dump_VERTEX_MESH (obj);
+	  break;
+	case DWG_TYPE_VERTEX_PFACE:
+	  dump_VERTEX_PFACE (obj);
+	  break;
+	case DWG_TYPE_VERTEX_PFACE_FACE:
+	  dump_VERTEX_PFACE_FACE (obj);
+	  break;
+	case DWG_TYPE_POLYLINE_2D:
+	  dump_POLYLINE_2D (obj);
+	  break;
+	case DWG_TYPE_POLYLINE_3D:
+	  dump_POLYLINE_3D (obj);
+	  break;
+	case DWG_TYPE_ARC:
+	  dump_ARC (obj);
+	  break;
+	case DWG_TYPE_CIRCLE:
+	  dump_CIRCLE (obj);
+	  break;
+	case DWG_TYPE_LINE:
+	  dump_LINE (obj);
+	  break;
+	case DWG_TYPE_DIMENSION_ORDINATE:
+	  dump_DIMENSION_ORDINATE (obj);
+	  break;
+	case DWG_TYPE_DIMENSION_LINEAR:
+	  dump_DIMENSION_LINEAR (obj);
+	  break;
+	case DWG_TYPE_DIMENSION_ALIGNED:
+	  dump_DIMENSION_ALIGNED (obj);
+	  break;
+	case DWG_TYPE_DIMENSION_ANG3PT:
+	  dump_DIMENSION_ANG3PT (obj);
+	  break;
+	case DWG_TYPE_DIMENSION_ANG2LN:
+	  dump_DIMENSION_ANG2LN (obj);
+	  break;
+	case DWG_TYPE_DIMENSION_RADIUS:
+	  dump_DIMENSION_RADIUS (obj);
+	  break;
+	case DWG_TYPE_DIMENSION_DIAMETER:
+	  dump_DIMENSION_DIAMETER (obj);
+	  break;
+	case DWG_TYPE_POINT:
+	  dump_POINT (obj);
+	  break;
+	case DWG_TYPE__3DFACE:
+	  dump__3DFACE (obj);
+	  break;
+	case DWG_TYPE_POLYLINE_PFACE:
+	  dump_POLYLINE_PFACE (obj);
+	  break;
+	case DWG_TYPE_POLYLINE_MESH:
+	  dump_POLYLINE_MESH (obj);
+	  break;
+	case DWG_TYPE_SOLID:
+	  dump_SOLID (obj);
+	  break;
+	case DWG_TYPE_TRACE:
+	  dump_TRACE (obj);
+	  break;
+	case DWG_TYPE_SHAPE:
+	  dump_SHAPE (obj);
+	  break;
+	case DWG_TYPE_VIEWPORT:
+	  dump_VIEWPORT (obj);
+	  break;
+	case DWG_TYPE_ELLIPSE:
+	  dump_ELLIPSE (obj);
+	  break;
+	case DWG_TYPE_SPLINE:
+	  dump_SPLINE (obj);
+	  break;
+	case DWG_TYPE_REGION:
+	  dump_REGION (obj);
+	  break;
+	case DWG_TYPE_3DSOLID:
+	  dump__3DSOLID (obj);
+	  break;
+	  /* Check the type of the object */
+	case DWG_TYPE_BODY:
+	  dump_BODY (obj);
+	  break;
+	case DWG_TYPE_RAY:
+	  dump_RAY (obj);
+	  break;
+	case DWG_TYPE_XLINE:
+	  dump_XLINE (obj);
+	  break;
+	case DWG_TYPE_DICTIONARY:
+	  dump_DICTIONARY (obj);
+	  break;
+	case DWG_TYPE_MTEXT:
+	  dump_MTEXT (obj);
+	  break;
+	case DWG_TYPE_LEADER:
+	  dump_LEADER (obj);
+	  break;
+	case DWG_TYPE_TOLERANCE:
+	  dump_TOLERANCE (obj);
+	  break;
+	case DWG_TYPE_MLINE:
+	  dump_MLINE (obj);
+	  break;
+	case DWG_TYPE_BLOCK_CONTROL:
+	  dump_BLOCK_CONTROL (obj);
+	  break;
+	case DWG_TYPE_BLOCK_HEADER:
+	  dump_BLOCK_HEADER (obj);
+	  break;
+	case DWG_TYPE_LAYER_CONTROL:
+	  dump_LAYER_CONTROL (obj);
+	  break;
+	case DWG_TYPE_LAYER:
+	  dump_LAYER (obj);
+	  break;
+	case DWG_TYPE_SHAPEFILE_CONTROL:
+	  dump_SHAPEFILE_CONTROL (obj);
+	  break;
+	case DWG_TYPE_SHAPEFILE:
+	  dump_SHAPEFILE (obj);
+	  break;
+	case DWG_TYPE_LTYPE_CONTROL:
+	  dump_LTYPE_CONTROL (obj);
+	  break;
+	case DWG_TYPE_LTYPE:
+	  dump_LTYPE (obj);
+	  break;
+	case DWG_TYPE_VIEW_CONTROL:
+	  dump_VIEW_CONTROL (obj);
+	  break;
+	case DWG_TYPE_VIEW:
+	  dump_VIEW (obj);
+	  break;
+	case DWG_TYPE_UCS_CONTROL:
+	  dump_UCS_CONTROL (obj);
+	  break;
+	case DWG_TYPE_UCS:
+	  dump_UCS (obj);
+	  break;
+	case DWG_TYPE_VPORT_CONTROL:
+	  dump_VPORT_CONTROL (obj);
+	  break;
+	case DWG_TYPE_VPORT:
+	  dump_VPORT (obj);
+	  break;
+	case DWG_TYPE_APPID_CONTROL:
+	  dump_APPID_CONTROL (obj);
+	  break;
+	case DWG_TYPE_APPID:
+	  dump_APPID (obj);
+	  break;
+	case DWG_TYPE_DIMSTYLE_CONTROL:
+	  dump_DIMSTYLE_CONTROL (obj);
+	  break;
+	case DWG_TYPE_DIMSTYLE:
+	  dump_DIMSTYLE (obj);
+	  break;
+	case DWG_TYPE_VP_ENT_HDR_CONTROL:
+	  dump_VP_ENT_HDR_CONTROL (obj);
+	  break;
+	case DWG_TYPE_VP_ENT_HDR:
+	  dump_VP_ENT_HDR (obj);
+	  break;
+	case DWG_TYPE_GROUP:
+	  dump_GROUP (obj);
+	  break;
+	case DWG_TYPE_MLINESTYLE:
+	  dump_MLINESTYLE (obj);
+	  break;
+	default:
+	  if (obj->type == obj->parent->dwg_ot_layout)
+	    dump_LAYOUT (obj);
+	  else
+	    printf ("Unknown type: %u\n", obj->type);
+	}
     }
 }
 
 int
 main (int argc, char *argv[])
 {
-  unsigned int i;
-  int success;
+  int fail;
   Dwg_Data dwg;
 
   if (1 == argc)
     {
       puts ("No input file specified");
-      return 1;
+      return -1;
     }
 
   dwg.num_objects = 0;
-  success = dwg_read_file (argv[1], &dwg);
-  if (success)
-    return (success);
+  fail = dwg_read_file (argv[1], &dwg);
+  if (fail)
+    return (fail);
 
-  puts ("-------------------");
-  puts ("DWG dumped data");
-  printf ("num_objects: %lu\n", dwg.num_objects);
-  puts ("-------------------");
+  puts ("-- DWG dumped data --");
+  printf ("   version: %s\n", dwg_version_code (&dwg));
+  puts ("");
 
-  for (i = 0; i < dwg.num_objects; i++)
-    {
-      printf ("[%u] ", i);
-      dump_object (&dwg.object[i]);
-    }
+  puts ("-- VARIABLES --");
+  dump_variables (&dwg);
+  puts ("");
+
+  puts ("-- OBJECTS --");
+  printf ("   num_objects: %lu\n", dwg.num_objects);
+  dump_objects (&dwg);
+  puts ("");
 
   return 0;
 }
diff --git a/examples/dump_objects.in.c b/examples/dump_objects.in.c
index 31b4da5..84d946e 100644
--- a/examples/dump_objects.in.c
+++ b/examples/dump_objects.in.c
@@ -11,8 +11,8 @@
 /*****************************************************************************/
 
 /**
- *     \file       objects_dump.c.in
- *     \brief      Dump object contents. It is pre-preprocessed to generate 'objects_dump.c'.
+ *     \file       dump_objects.in.c
+ *     \brief      Dump object contents. It is pre-preprocessed to generate 'dump_objects.c'.
  *     \author     written by Rodrigo Rodrigues da Silva
  *     \author     modified by Felipe E. F. de Castro
  *     \version    
@@ -20,7 +20,7 @@
  */
 
 /* Definitions copied from ../src/common.h
- * They are needed in the ".spec" files, to generate 'objects_dump.c'.
+ * They are needed in the ".spec" file.
  */
 #define VERSION(v) if (obj->parent->header.version == v)
 #define VERSIONS(v1,v2) if (obj->parent->header.version >= v1 && obj->parent->header.version <= v2)
diff --git a/examples/dump_variables.in.c b/examples/dump_variables.in.c
new file mode 100644
index 0000000..d1fcd38
--- /dev/null
+++ b/examples/dump_variables.in.c
@@ -0,0 +1,72 @@
+/*****************************************************************************/
+/*  LibreDWG - free implementation of the DWG file format                    */
+/*                                                                           */
+/*  Copyright (C) 2013 Free Software Foundation, Inc.                        */
+/*                                                                           */
+/*  This library is free software, licensed under the terms of the GNU       */
+/*  General Public License as published by the Free Software Foundation,     */
+/*  either version 3 of the License, or (at your option) any later version.  */
+/*  You should have received a copy of the GNU General Public License        */
+/*  along with this program.  If not, see <http://www.gnu.org/licenses/>.    */
+/*****************************************************************************/
+
+/**
+ *     \file       dump_variables.c.in
+ *     \brief      Dump variables contents. It is pre-preprocessed to generate 'dump_variables.c'.
+ *     \author     written by Rodrigo Rodrigues da Silva
+ *     \author     modified by Felipe E. F. de Castro
+ *     \version    
+ *     \copyright  GNU General Public License (version 3 or later)
+ */
+
+/* Definitions originally copied from ../src/common.h
+ * They are needed in the ".spec" file.
+ */
+#define PRE(v) if (dwg->header.version < v)
+#define UNTIL(v) if (dwg->header.version <= v)
+#define LATER_VERSIONS else
+#define VERSION(v) if (dwg->header.version == v)
+#define VERSIONS(v1,v2) if (dwg->header.version >= v1 && dwg->header.version <= v2)
+#define OTHER_VERSIONS else
+#define SINCE(v) if (dwg->header.version >= v)
+#define PRIOR_VERSIONS else
+/* End of copied definitions  */
+
+#define FIELD(name,type)\
+  printf (#name " = " FORMAT_##type "\n", dwg->header_vars.name);
+
+#define FIELD_VALUE(name) dwg->header_vars.name
+
+#define ANYCODE -1
+#define FIELD_HANDLE(name, handle_code)\
+  printf (#name " = HANDLE(%1X.%d.%lu) absolute:%lu\n",\
+        dwg->header_vars.name->handleref.code,\
+        dwg->header_vars.name->handleref.size,\
+        dwg->header_vars.name->handleref.value,\
+        dwg->header_vars.name->absolute_ref);
+
+#define FIELD_B(name) FIELD(name, B);
+#define FIELD_BB(name) FIELD(name, BB);
+#define FIELD_BS(name) FIELD(name, BS);
+#define FIELD_BL(name) FIELD(name, BL);
+#define FIELD_BD(name) FIELD(name, BD);
+#define FIELD_RC(name) FIELD(name, RC);
+#define FIELD_RS(name) FIELD(name, RS);
+#define FIELD_RD(name) FIELD(name, RD);
+#define FIELD_RL(name) FIELD(name, RL);
+#define FIELD_TV(name) FIELD(name, TV);
+#define FIELD_T FIELD_TV	/*TODO: implement version dependant string fields */
+#define FIELD_BT(name) FIELD(name, BT);
+
+#define FIELD_2RD(name) FIELD(name.x, RD); FIELD(name.y, RD);
+#define FIELD_2BD(name) FIELD(name.x, BD); FIELD(name.y, BD);
+#define FIELD_3RD(name) FIELD(name.x, RD); FIELD(name.y, RD); FIELD(name.z, RD);
+#define FIELD_3BD(name) FIELD(name.x, BD); FIELD(name.y, BD); FIELD(name.z, BD);
+#define FIELD_CMC(name)\
+  printf (#name " = color index %d\n", dwg->header_vars.name.index);
+
+void
+dump_variables (Dwg_Data * dwg)
+{
+#  include "header_variables.spec"
+}
diff --git a/src/dwg.c b/src/dwg.c
index 96e65d8..d7c7eb4 100644
--- a/src/dwg.c
+++ b/src/dwg.c
@@ -39,7 +39,7 @@ char version_codes[8][7] =
   { "------", "AC1012", "AC1014", "AC1015", "AC1018", "AC1021", "AC1024", "------" };
 
 int
-dwg_read_file(char *filename, Dwg_Data *dwg_data)
+dwg_read_file (char *filename, Dwg_Data * dwg_data)
 {
   int sign;
   FILE *fp;
@@ -47,114 +47,116 @@ dwg_read_file(char *filename, Dwg_Data *dwg_data)
   size_t size;
   Bit_Chain bit_chain;
 
-  if (stat(filename, &attrib))
+  if (stat (filename, &attrib))
     {
-      LOG_ERROR("File not found: %s \n", filename)
-      return -1;
+      LOG_ERROR ("File not found: %s \n", filename) return -1;
     }
   if (!S_ISREG (attrib.st_mode))
     {
-      LOG_ERROR("Error: %s \n", filename)
-      return -1;
+      LOG_ERROR ("Error: %s \n", filename) return -1;
     }
-  fp = fopen(filename, "rb");
+  fp = fopen (filename, "rb");
   if (!fp)
     {
-      LOG_ERROR("Could not open file: %s\n", filename)
-      return -1;
+      LOG_ERROR ("Could not open file: %s\n", filename) return -1;
     }
 
   /* Load file to memory */
   bit_chain.bit = 0;
   bit_chain.byte = 0;
   bit_chain.size = attrib.st_size;
-  bit_chain.chain = (unsigned char *) malloc(bit_chain.size);
+  bit_chain.chain = (unsigned char *) malloc (bit_chain.size);
   if (!bit_chain.chain)
     {
-      LOG_ERROR("Not enough memory.\n")
-      fclose(fp);
+      LOG_ERROR ("Not enough memory.\n") fclose (fp);
       return -1;
     }
   size = 0;
-  size = fread(bit_chain.chain, sizeof(char), bit_chain.size, fp);
+  size = fread (bit_chain.chain, sizeof (char), bit_chain.size, fp);
   if (size != bit_chain.size)
     {
-      LOG_ERROR("Could not read the entire file (%lu out of %lu): %s \n",
-                (long unsigned int) size, bit_chain.size, filename)
-      fclose(fp);
-      free(bit_chain.chain);
+      LOG_ERROR ("Could not read the entire file (%lu out of %lu): %s \n",
+		 (long unsigned int) size, bit_chain.size, filename)
+	fclose (fp);
+      free (bit_chain.chain);
       return -1;
     }
-  fclose(fp);
+  fclose (fp);
 
   /* Decode the dwg structure */
   dwg_data->bit_chain = &bit_chain;
 
-  if (dwg_decode_data(&bit_chain, dwg_data))
+  if (dwg_decode_data (&bit_chain, dwg_data))
     {
-      LOG_ERROR("Failed to decode file: %s \n", filename)
-      free(bit_chain.chain);
+      LOG_ERROR ("Failed to decode file: %s \n", filename)
+	free (bit_chain.chain);
       return -1;
     }
-  free(bit_chain.chain);
+  free (bit_chain.chain);
 
   return 0;
 }
 
+char *
+dwg_version_code (Dwg_Data * dwg)
+{
+  if (dwg->header.version > R_BEFORE && dwg->header.version < R_AFTER)
+    return (version_codes[dwg->header.version]);
+  else
+    return ("UNKNOW");
+}
 
 /* if write support is enabled */
-#ifdef USE_WRITE 
+#ifdef USE_WRITE
 
 int
-dwg_write_file(char *filename, Dwg_Data *dwg_data)
+dwg_write_file (char *filename, Dwg_Data * dwg_data)
 {
   FILE *dt;
   struct stat atrib;
   Bit_Chain bit_chain;
-  bit_chain.version = (Dwg_Version_Type)dwg_data->header.version;
-
-   /* Encode the DWG struct */
-   bit_chain.size = 0;
-   if (dwg_encode_chains (dwg_data, &bit_chain))
-     {
-       LOG_ERROR("Failed to encode datastructure.\n")
-       if (bit_chain.size > 0)
-       free (bit_chain.chain);
-       return -1;
-     }
-
-   /* try opening the output file in write mode */
-   if (!stat (filename, &atrib))
-     {
-       LOG_ERROR("The file already exists. We won't overwrite it.")
-       return -1;
-     }
-   dt = fopen (filename, "w");
-   if (!dt)
-     {
-       LOG_ERROR("Failed to create the file: %s\n", filename)
-       return -1;
-     }
-  
-   /* Write the data into the file */
-   if (fwrite (bit_chain.chain, sizeof (char), bit_chain.size, dt) != bit_chain.size)
-     {
-       LOG_ERROR("Failed to write data into the file: %s\n", filename)
-       fclose (dt);
-       free (bit_chain.chain);
-       return -1;
-     }
-   fclose (dt);
-
-   if (bit_chain.size > 0)
-   free (bit_chain.chain);
+  bit_chain.version = (Dwg_Version_Type) dwg_data->header.version;
+
+  /* Encode the DWG struct */
+  bit_chain.size = 0;
+  if (dwg_encode_chains (dwg_data, &bit_chain))
+    {
+      LOG_ERROR ("Failed to encode datastructure.\n") if (bit_chain.size > 0)
+	free (bit_chain.chain);
+      return -1;
+    }
+
+  /* try opening the output file in write mode */
+  if (!stat (filename, &atrib))
+    {
+      LOG_ERROR ("The file already exists. We won't overwrite it.") return -1;
+    }
+  dt = fopen (filename, "w");
+  if (!dt)
+    {
+      LOG_ERROR ("Failed to create the file: %s\n", filename) return -1;
+    }
+
+  /* Write the data into the file */
+  if (fwrite (bit_chain.chain, sizeof (char), bit_chain.size, dt) !=
+      bit_chain.size)
+    {
+      LOG_ERROR ("Failed to write data into the file: %s\n", filename)
+	fclose (dt);
+      free (bit_chain.chain);
+      return -1;
+    }
+  fclose (dt);
+
+  if (bit_chain.size > 0)
+    free (bit_chain.chain);
 
   return 0;
 }
-#endif /* USE_WRITE */ 
+#endif /* USE_WRITE */
 
 unsigned char *
-dwg_bmp(Dwg_Data *stk, long int *size)
+dwg_bmp (Dwg_Data * stk, long int *size)
 {
   char num_pictures;
   char code;
@@ -163,179 +165,172 @@ dwg_bmp(Dwg_Data *stk, long int *size)
   long int header_size;
   Bit_Chain *dat;
 
-  dat = (Bit_Chain*) &stk->picture;
+  dat = (Bit_Chain *) & stk->picture;
   dat->bit = 0;
   dat->byte = 0;
 
-  bit_read_RL(dat);
-  num_pictures = bit_read_RC(dat);
-  LOG_INFO("num_pictures: %i \n", num_pictures)
-
-  *size = 0;
+  bit_read_RL (dat);
+  num_pictures = bit_read_RC (dat);
+  LOG_INFO ("num_pictures: %i \n", num_pictures) * size = 0;
   plene = 0;
   header_size = 0;
   for (i = 0; i < num_pictures; i++)
     {
-      code = bit_read_RC(dat);
-      LOG_TRACE("\t%i - Code: %i \n", i, code)
-      LOG_TRACE("\t\t Adress: 0x%lx \n", bit_read_RL (dat))
-      bit_read_RL(dat);
+      code = bit_read_RC (dat);
+      LOG_TRACE ("\t%i - Code: %i \n", i, code)
+	LOG_TRACE ("\t\t Adress: 0x%lx \n", bit_read_RL (dat))
+	bit_read_RL (dat);
       if (code == 1)
-        {
-          header_size += bit_read_RL(dat);
-          LOG_TRACE("\t\t Header size: %li \n", header_size)
-        }
+	{
+	  header_size += bit_read_RL (dat);
+	LOG_TRACE ("\t\t Header size: %li \n", header_size)}
       else if (code == 2 && plene == 0)
-        {
-          *size = bit_read_RL(dat);
-          plene = 1;
-          LOG_TRACE("\t\t BMP size: %li \n", *size)
-        }
+	{
+	  *size = bit_read_RL (dat);
+	  plene = 1;
+	LOG_TRACE ("\t\t BMP size: %li \n", *size)}
       else if (code == 3)
-        {
-          bit_read_RL(dat);
-          LOG_TRACE("\t\t WMF size: 0x%lx \n", bit_read_RL (dat))
-        }
+	{
+	  bit_read_RL (dat);
+	LOG_TRACE ("\t\t WMF size: 0x%lx \n", bit_read_RL (dat))}
       else
-        {
-          bit_read_RL(dat);
-          LOG_TRACE("\t\t Size: 0x%lx \n", bit_read_RL (dat))
-        }
+	{
+	  bit_read_RL (dat);
+	LOG_TRACE ("\t\t Size: 0x%lx \n", bit_read_RL (dat))}
     }
   dat->byte += header_size;
-  LOG_TRACE("Current adress: 0x%lx \n", dat->byte)
-
-  if (*size > 0)
+  LOG_TRACE ("Current adress: 0x%lx \n", dat->byte) if (*size > 0)
     return (dat->chain + dat->byte);
   else
     return NULL;
 }
 
 double
-dwg_model_x_min(Dwg_Data *dwg)
+dwg_model_x_min (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMIN_MSPACE.x;
 }
 
 double
-dwg_model_x_max(Dwg_Data *dwg)
+dwg_model_x_max (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMAX_MSPACE.x;
 }
 
 double
-dwg_model_y_min(Dwg_Data *dwg)
+dwg_model_y_min (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMIN_MSPACE.y;
 }
 
 double
-dwg_model_y_max(Dwg_Data *dwg)
+dwg_model_y_max (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMAX_MSPACE.y;
 }
 
 double
-dwg_model_z_min(Dwg_Data *dwg)
+dwg_model_z_min (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMIN_MSPACE.z;
 }
 
 double
-dwg_model_z_max(Dwg_Data *dwg)
+dwg_model_z_max (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMAX_MSPACE.z;
 }
 
 double
-dwg_page_x_min(Dwg_Data *dwg)
+dwg_page_x_min (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMIN_PSPACE.x;
 }
 
 double
-dwg_page_x_max(Dwg_Data *dwg)
+dwg_page_x_max (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMAX_PSPACE.x;
 }
 
 double
-dwg_page_y_min(Dwg_Data *dwg)
+dwg_page_y_min (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMIN_PSPACE.y;
 }
 
 double
-dwg_page_y_max(Dwg_Data *dwg)
+dwg_page_y_max (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMAX_PSPACE.y;
 }
 
 unsigned int
-dwg_get_layer_count(Dwg_Data *dwg)
+dwg_get_layer_count (Dwg_Data * dwg)
 {
   return dwg->layer_control->tio.object->tio.LAYER_CONTROL->num_entries;
 }
 
 Dwg_Object_LAYER **
-dwg_get_layers(Dwg_Data *dwg)
+dwg_get_layers (Dwg_Data * dwg)
 {
   int i;
-  Dwg_Object_LAYER **layers = (Dwg_Object_LAYER **) malloc(
-		               dwg_get_layer_count(dwg) *
-                               sizeof (Dwg_Object_LAYER*));
-  for (i=0; i < dwg_get_layer_count(dwg); i++)
+  Dwg_Object_LAYER **layers =
+    (Dwg_Object_LAYER **) malloc (dwg_get_layer_count (dwg) *
+				  sizeof (Dwg_Object_LAYER *));
+  for (i = 0; i < dwg_get_layer_count (dwg); i++)
     {
-      layers[i] = dwg->layer_control->tio.object->tio.LAYER_CONTROL->
-                  layers[i]->obj->tio.object->tio.LAYER;
+      layers[i] =
+	dwg->layer_control->tio.object->tio.LAYER_CONTROL->layers[i]->obj->
+	tio.object->tio.LAYER;
     }
   return layers;
 }
 
 long unsigned int
-dwg_get_object_count(Dwg_Data *dwg)
+dwg_get_object_count (Dwg_Data * dwg)
 {
   return dwg->num_objects;
 }
 
 long unsigned int
-dwg_get_object_object_count(Dwg_Data *dwg)
+dwg_get_object_object_count (Dwg_Data * dwg)
 {
   return dwg->num_objects - dwg->num_entities;
 }
 
 long unsigned int
-dwg_get_entity_count(Dwg_Data *dwg)
+dwg_get_entity_count (Dwg_Data * dwg)
 {
   return dwg->num_entities;
 }
 
 Dwg_Object_Entity **
-dwg_get_entities(Dwg_Data *dwg)
+dwg_get_entities (Dwg_Data * dwg)
 {
   long unsigned int i, ent_count = 0;
-  Dwg_Object_Entity **entities = (Dwg_Object_Entity **) malloc(
-                                  dwg_get_entity_count(dwg) *
-                                  sizeof (Dwg_Object_Entity*));
-  for (i=0; i < dwg->num_objects; i++)
+  Dwg_Object_Entity **entities =
+    (Dwg_Object_Entity **) malloc (dwg_get_entity_count (dwg) *
+				   sizeof (Dwg_Object_Entity *));
+  for (i = 0; i < dwg->num_objects; i++)
     {
       if (dwg->object[i].supertype == DWG_SUPERTYPE_ENTITY)
-        {
-          entities[ent_count] = dwg->object[i].tio.entity;
-          ent_count++;
-        }
+	{
+	  entities[ent_count] = dwg->object[i].tio.entity;
+	  ent_count++;
+	}
     }
   return entities;
 }
 
 Dwg_Object_LAYER *
-dwg_get_entity_layer(Dwg_Object_Entity *ent)
+dwg_get_entity_layer (Dwg_Object_Entity * ent)
 {
   return ent->layer->obj->tio.object->tio.LAYER;
 }
 
-Dwg_Object*
-dwg_next_object(Dwg_Object *obj)
+Dwg_Object *
+dwg_next_object (Dwg_Object * obj)
 {
   if ((obj->index + 1) > obj->parent->num_objects - 1)
     return 0;
@@ -343,7 +338,7 @@ dwg_next_object(Dwg_Object *obj)
 }
 
 int
-dwg_get_object(Dwg_Object *obj, Dwg_Object_Ref *ref)
+dwg_get_object (Dwg_Object * obj, Dwg_Object_Ref * ref)
 {
   if (ref->obj)
     {
@@ -353,8 +348,8 @@ dwg_get_object(Dwg_Object *obj, Dwg_Object_Ref *ref)
   return -1;
 }
 
-Dwg_Object*
-get_first_owned_object(Dwg_Object *hdr_obj, Dwg_Object_BLOCK_HEADER *hdr)
+Dwg_Object *
+get_first_owned_object (Dwg_Object * hdr_obj, Dwg_Object_BLOCK_HEADER * hdr)
 {
   unsigned int version = hdr_obj->parent->header.version;
 
@@ -370,31 +365,31 @@ get_first_owned_object(Dwg_Object *hdr_obj, Dwg_Object_BLOCK_HEADER *hdr)
     }
 }
 
-Dwg_Object*
-get_next_owned_object(Dwg_Object* hdr_obj, Dwg_Object* current,
-                      Dwg_Object_BLOCK_HEADER* hdr)
+Dwg_Object *
+get_next_owned_object (Dwg_Object * hdr_obj, Dwg_Object * current,
+		       Dwg_Object_BLOCK_HEADER * hdr)
 {
   unsigned int version = hdr_obj->parent->header.version;
 
   if (R_13 <= version && version <= R_2000)
     {
-      if (current == hdr->last_entity->obj) 
-        return 0;
-      return dwg_next_object(current);
+      if (current == hdr->last_entity->obj)
+	return 0;
+      return dwg_next_object (current);
     }
 
   if (version >= R_2004)
     {
       hdr->__iterator++;
       if (hdr->__iterator == hdr->owned_object_count)
-        return 0;
+	return 0;
       return hdr->entities[hdr->__iterator]->obj;
     }
 }
 
 void
-dwg_free(Dwg_Data *dwg)
+dwg_free (Dwg_Data * dwg)
 {
   if (dwg->header.section)
-    free(dwg->header.section);
+    free (dwg->header.section);
 }
diff --git a/src/dwg.h b/src/dwg.h
index a136519..ecd018e 100644
--- a/src/dwg.h
+++ b/src/dwg.h
@@ -2993,6 +2993,8 @@ typedef struct _dwg_struct
 int
 dwg_read_file(char *filename, Dwg_Data * dwg);
 
+char * dwg_version_code (Dwg_Data * dwg);
+
 #ifdef USE_WRITE
 int
 dwg_write_file(char *filename, Dwg_Data * dwg_data);
-- 
1.7.10.4

