From 5f2c66437e1722c51adf6782a032f200b7beca87 Mon Sep 17 00:00:00 2001
From: Anders Blomdell <anders.blomdell@control.lth.se>
Date: Thu, 18 Apr 2013 15:34:21 +0200
Subject: [PATCH] Minor refatorings and cleanups.

---
 lib/c/Makefile                                | 28 +++++------
 lib/c/labcomm.c                               | 35 +++-----------
 lib/c/labcomm.h                               |  7 +--
 lib/c/labcomm_compat_arm_cortexm3.h           |  5 ++
 lib/c/labcomm_compat_vxworks.h                | 12 +++++
 ...iter.c => labcomm_dynamic_buffer_writer.c} |  5 +-
 ...iter.h => labcomm_dynamic_buffer_writer.h} |  3 +-
 lib/c/labcomm_fd_reader_writer.h              |  4 +-
 lib/c/labcomm_private.h                       | 47 +++++--------------
 lib/c/test/test_labcomm_errors.c              |  9 +++-
 10 files changed, 66 insertions(+), 89 deletions(-)
 create mode 100644 lib/c/labcomm_compat_arm_cortexm3.h
 create mode 100644 lib/c/labcomm_compat_vxworks.h
 rename lib/c/{labcomm_dynamic_buffer_reader_writer.c => labcomm_dynamic_buffer_writer.c} (91%)
 rename lib/c/{labcomm_dynamic_buffer_reader_writer.h => labcomm_dynamic_buffer_writer.h} (87%)

diff --git a/lib/c/Makefile b/lib/c/Makefile
index 742ae35..25c86bb 100644
--- a/lib/c/Makefile
+++ b/lib/c/Makefile
@@ -3,10 +3,10 @@
 # Use LLVM clang if it's found.
 CC = $(shell hash clang 2>/dev/null && echo clang || echo gcc)
 CFLAGS = -g -Wall -Werror -I. -Itest
-LDFLAGS = -L .
+LDFLAGS = -L.
 LDLIBS_TEST = -lcunit -llabcomm
 
-OBJS= labcomm.o labcomm_dynamic_buffer_reader_writer.o labcomm_fd_reader_writer.o labcomm_mem_reader.o labcomm_mem_writer.o
+OBJS= labcomm.o labcomm_dynamic_buffer_writer.o labcomm_fd_reader_writer.o labcomm_mem_reader.o labcomm_mem_writer.o
 LABCOMMC_PATH=../../compiler
 LABCOMMC_JAR=$(LABCOMMC_PATH)/labComm.jar
 
@@ -55,20 +55,16 @@ run-test: $(TEST_DIR)/test_labcomm $(TEST_DIR)/test_labcomm_errors |$(TEST_DIR)
 	test/test_labcomm
 	test/test_labcomm_errors
 
-$(TEST_DIR)/test_labcomm_errors: $(TEST_DIR)/test_labcomm_errors.o liblabcomm.a |$(TEST_DIR)
-	$(CC) $(CFLAGS) $(LDFLAGS) -llabcomm -o $@ $^
+$(TEST_DIR)/%.o: $(TEST_DIR)/%.c
+	$(CC) $(CFLAGS) -o $@ -c $<
 
-$(TEST_DIR)/test_labcomm_errors.o: $(TEST_DIR)/test_labcomm_errors.c $(TEST_DIR)/test_labcomm_errors.h |$(TEST_DIR)
-	cd test; $(CC) $(CFLAGS) -I .. -c $(patsubst $(TEST_DIR)/%, %, $^)
+$(TEST_DIR)/%: $(TEST_DIR)/%.o
+	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) $(LDLIBS_TEST)
 
-$(TEST_DIR)/test_labcomm.o: $(TEST_DIR)/test_labcomm.c $(TEST_GEN_DIR)/test_sample.h |$(TEST_DIR)
-	$(CC) -c $(CFLAGS) -o $@ $<
-
-$(TEST_DIR)/test_labcomm: $(TEST_DIR)/test_labcomm.o $(TEST_GEN_DIR)/test_sample.o liblabcomm.a
-	$(CC) $(CFLAGS) $(LDFLAGS) $(filter-out %.a,$^) $(LDLIBS) $(LDLIBS_TEST) -o $@
-
-$(TEST_GEN_DIR)/%.c $(TEST_GEN_DIR)/%.h: $(TESTDATA_DIR)/%.lc $(LABCOMMC_JAR) |$(TEST_GEN_DIR)
-	java -jar $(LABCOMMC_JAR) --c=$(patsubst %.h,%.c,$@) --h=$(patsubst %.c,%.h,$@) $<
+$(TEST_GEN_DIR)/%.c $(TEST_GEN_DIR)/%.h: $(TESTDATA_DIR)/%.lc \
+					 $(LABCOMMC_JAR) | $(TEST_GEN_DIR)
+	java -jar $(LABCOMMC_JAR) \
+		--c=$(patsubst %.h,%.c,$@) --h=$(patsubst %.c,%.h,$@) $<
 
 $(LABCOMMC_JAR):
 	@echo "======Building LabComm compiler======"
@@ -83,7 +79,11 @@ clean:
 	$(RM) test/*.o
 	$(RM) test/*.gch
 	$(RM) test/test_labcomm_errors
+	$(RM) test/testdata/gen/*.[cho]
 	$(RM) $(TEST_DIR)/test_labcomm
 
 distclean: clean
 	$(RM) liblabcomm.a
+
+# Extra dependencies
+$(TEST_DIR)/test_labcomm:  $(TEST_GEN_DIR)/test_sample.o
diff --git a/lib/c/labcomm.c b/lib/c/labcomm.c
index 3b790e5..7539be7 100644
--- a/lib/c/labcomm.c
+++ b/lib/c/labcomm.c
@@ -1,36 +1,16 @@
-#include <errno.h>
-#include <string.h>
-
-#ifndef __VXWORKS__
-  #ifdef ARM_CORTEXM3_CODESOURCERY
-    #include <string.h>
-  #else
-    #include <strings.h>
-  #endif
-#endif
-
-#ifndef ARM_CORTEXM3_CODESOURCERY
-  #include <stdlib.h>
-#endif
-
-// Some projects can not use stdio.h.
-#ifndef LABCOMM_NO_STDIO
+#ifdef LABCOMM_COMPAT
+  #include LABCOMM_COMPAT
+#else
   #include <stdio.h>
+  #include <strings.h>
 #endif
 
-#ifdef __VXWORKS__
-  #if (CPU == PPC603)
-    #undef _LITTLE_ENDIAN
-  #endif
-  #if (CPU == PENTIUM4)
-    #undef _BIG_ENDIAN
-  #endif
-#endif
-
+#include <errno.h>
+#include <string.h>
 #include "labcomm.h"
 #include "labcomm_private.h"
 #include "labcomm_ioctl.h"
-#include "labcomm_dynamic_buffer_reader_writer.h"
+#include "labcomm_dynamic_buffer_writer.h"
 
 typedef struct labcomm_sample_entry {
   struct labcomm_sample_entry *next;
@@ -360,7 +340,6 @@ int labcomm_encoder_ioctl(struct labcomm_encoder *encoder,
   return result;
 }
 
-
 static void collect_flat_signature(
   labcomm_decoder_t *decoder,
   labcomm_encoder_t *signature_writer)
diff --git a/lib/c/labcomm.h b/lib/c/labcomm.h
index 44af7cf..baedb8d 100644
--- a/lib/c/labcomm.h
+++ b/lib/c/labcomm.h
@@ -155,15 +155,10 @@ struct labcomm_encoder *labcomm_encoder_new(
   void *writer_context);
 void labcomm_encoder_free(
   struct labcomm_encoder *encoder);
+
 /* See labcomm_ioctl.h for predefined ioctl_action values */
 int labcomm_encoder_ioctl(struct labcomm_encoder *encoder, 
 			  int ioctl_action,
 			  ...);
 
-void labcomm_encoder_start(struct labcomm_encoder *e,
-                           labcomm_signature_t *s) ;
-
-//HERE BE DRAGONS: is the signature_t* needed here?
-void labcomm_encoder_end(struct labcomm_encoder *e,
-                           labcomm_signature_t *s) ;
 #endif
diff --git a/lib/c/labcomm_compat_arm_cortexm3.h b/lib/c/labcomm_compat_arm_cortexm3.h
new file mode 100644
index 0000000..512ad36
--- /dev/null
+++ b/lib/c/labcomm_compat_arm_cortexm3.h
@@ -0,0 +1,5 @@
+#ifndef ARM_CORTEXM3_CODESOURCERY
+#error "ARM_CORTEXM3_CODESOURCERY" not defined
+#endif
+
+#include <machine/endian.h>
diff --git a/lib/c/labcomm_compat_vxworks.h b/lib/c/labcomm_compat_vxworks.h
new file mode 100644
index 0000000..f05ee78
--- /dev/null
+++ b/lib/c/labcomm_compat_vxworks.h
@@ -0,0 +1,12 @@
+#ifndef  __VXWORKS__
+#error "__VXWORKS__" not defined
+#endif
+
+#if (CPU == PPC603)
+  #undef _LITTLE_ENDIAN
+#endif
+
+#if (CPU == PENTIUM4)
+  #undef _BIG_ENDIAN
+#endif
+
diff --git a/lib/c/labcomm_dynamic_buffer_reader_writer.c b/lib/c/labcomm_dynamic_buffer_writer.c
similarity index 91%
rename from lib/c/labcomm_dynamic_buffer_reader_writer.c
rename to lib/c/labcomm_dynamic_buffer_writer.c
index b363f17..c651f19 100644
--- a/lib/c/labcomm_dynamic_buffer_reader_writer.c
+++ b/lib/c/labcomm_dynamic_buffer_writer.c
@@ -1,8 +1,9 @@
-#include "labcomm_dynamic_buffer_reader_writer.h"
+#include "labcomm_dynamic_buffer_writer.h"
 
 int labcomm_dynamic_buffer_writer(
   labcomm_writer_t *w,
-  labcomm_writer_action_t action, ...)
+  labcomm_writer_action_t action,
+  ...)
 {
   switch (action) {
     case labcomm_writer_alloc: {
diff --git a/lib/c/labcomm_dynamic_buffer_reader_writer.h b/lib/c/labcomm_dynamic_buffer_writer.h
similarity index 87%
rename from lib/c/labcomm_dynamic_buffer_reader_writer.h
rename to lib/c/labcomm_dynamic_buffer_writer.h
index b03e466..1591b11 100644
--- a/lib/c/labcomm_dynamic_buffer_reader_writer.h
+++ b/lib/c/labcomm_dynamic_buffer_writer.h
@@ -9,6 +9,7 @@ extern int labcomm_dynamic_buffer_reader(
 
 extern int labcomm_dynamic_buffer_writer(
   labcomm_writer_t *writer, 
-  labcomm_writer_action_t action, ...);
+  labcomm_writer_action_t action,
+  ...);
 
 #endif
diff --git a/lib/c/labcomm_fd_reader_writer.h b/lib/c/labcomm_fd_reader_writer.h
index 1de5858..e5125d5 100644
--- a/lib/c/labcomm_fd_reader_writer.h
+++ b/lib/c/labcomm_fd_reader_writer.h
@@ -9,6 +9,8 @@ extern int labcomm_fd_reader(
 
 extern int labcomm_fd_writer(
   labcomm_writer_t *writer, 
-  labcomm_writer_action_t action, ...);
+  labcomm_writer_action_t action,
+  ...);
 
 #endif
+
diff --git a/lib/c/labcomm_private.h b/lib/c/labcomm_private.h
index ed5894c..9d66f44 100644
--- a/lib/c/labcomm_private.h
+++ b/lib/c/labcomm_private.h
@@ -1,14 +1,10 @@
 #ifndef _LABCOMM_PRIVATE_H_
 #define _LABCOMM_PRIVATE_H_
 
-#ifdef ARM_CORTEXM3_CODESOURCERY
-  #include <machine/endian.h>
+#ifdef LABCOMM_COMPAT
+  #include LABCOMM_COMPAT
 #else
   #include <endian.h>
-#endif
-
-// Some projects can not use stdio.h.
-#ifndef LABCOMM_NO_STDIO
   #include <stdio.h>
 #endif
 
@@ -233,6 +229,16 @@ void labcomm_internal_encode(
   labcomm_signature_t *signature, 
   void *value);
 
+/* Should these really be visible? */
+void labcomm_encoder_start(struct labcomm_encoder *e,
+                           labcomm_signature_t *s) ;
+
+//HERE BE DRAGONS: is the signature_t* needed here?
+void labcomm_encoder_end(struct labcomm_encoder *e,
+                           labcomm_signature_t *s) ;
+
+
+
 #if __BYTE_ORDER == __LITTLE_ENDIAN
 
 #define LABCOMM_ENCODE(name, type)					\
@@ -330,33 +336,4 @@ static inline void labcomm_encode_string(labcomm_encoder_t *e,
 
 void labcomm_encode_type_index(labcomm_encoder_t *e, labcomm_signature_t *s);
 
-static inline int labcomm_buffer_write(struct labcomm_writer *w, 
-                                       labcomm_writer_action_t action, ...)
-{
-  // If this gets called, it is an error, 
-  // so note error and let producer proceed
-  w->context = w;
-  w->pos = 0;
-  return 0;
-}
-
-
-static inline int labcomm_buffer_writer_error(struct labcomm_writer *w) 
-{
-  return w->context != NULL;
-} 
-
-
-static inline void labcomm_buffer_writer_setup(struct labcomm_writer *w,
-                                               void *data,
-                                               int length)
-{
-  w->context = NULL; // Used as error flag
-  w->data = data;
-  w->data_size = length;
-  w->count = length;
-  w->pos = 0;
-  w->write = labcomm_buffer_write;
-}
-
 #endif
diff --git a/lib/c/test/test_labcomm_errors.c b/lib/c/test/test_labcomm_errors.c
index 8d810d4..7808504 100644
--- a/lib/c/test/test_labcomm_errors.c
+++ b/lib/c/test/test_labcomm_errors.c
@@ -37,7 +37,7 @@ void reset_callback_erro_id()
   callback_error_id = -128;
 }
 
-int encoded_size_mock(void *voidp)
+int encoded_size_mock(struct labcomm_signature *signature, void *voidp)
 {
   return 0;
 }
@@ -50,7 +50,12 @@ int test_enc_not_reg_encoder_sign()
   labcomm_encoder_t *encoder = labcomm_encoder_new(labcomm_mem_writer, mcontext);
   labcomm_register_error_handler_encoder(encoder, test_callback);
   
-  labcomm_signature_t signature = {.type = 0, .name = "test_signature", .encoded_size = encoded_size_mock, .size = 0, .signature = (unsigned char *) "0"};
+  labcomm_signature_t signature = {
+    .type = 0, 
+    .name = "test_signature", 
+    .encoded_size = encoded_size_mock, 
+    .size = 0, 
+    .signature = (unsigned char *) "0"};
   encoder->do_encode(encoder, &signature, NULL);
 
   return assert_callback(LABCOMM_ERROR_ENC_NO_REG_SIGNATURE, __FUNCTION__, "");
-- 
GitLab