Changed autogenerated file naming from foo.c to foo.pb.c
authorPetteri Aimonen <jpa@npb.mail.kapsi.fi>
Tue, 23 Aug 2011 16:16:33 +0000 (16:16 +0000)
committerPetteri Aimonen <jpa@npb.mail.kapsi.fi>
Tue, 23 Aug 2011 16:16:33 +0000 (16:16 +0000)
git-svn-id: https://svn.kapsi.fi/jpa/nanopb@965 e3a754e5-d11d-0410-8d38-ebb782a927b9

example/Makefile
example/client.c
example/server.c
generator/nanopb_generator.py
pb_encode.c
tests/Makefile
tests/decode_unittests.c
tests/encode_unittests.c

index efc4a8d..14dd9fa 100644 (file)
@@ -3,9 +3,12 @@ DEPS=../pb_decode.c ../pb_decode.h ../pb_encode.c ../pb_encode.h ../pb.h
 
 all: server client
 
-%: %.c $(DEPS) fileproto.h
-       $(CC) $(CFLAGS) -o $@ $< ../pb_decode.c ../pb_encode.c fileproto.c common.c
+clean:
+       rm -f server client fileproto.pb.c fileproto.pb.h
 
-fileproto.h: fileproto.proto ../generator/nanopb_generator.py
+%: %.c $(DEPS) fileproto.pb.h fileproto.pb.c
+       $(CC) $(CFLAGS) -o $@ $< ../pb_decode.c ../pb_encode.c fileproto.pb.c common.c
+
+fileproto.pb.c fileproto.pb.h: fileproto.proto ../generator/nanopb_generator.py
        protoc -I. -I../generator -I/usr/include -ofileproto.pb $<
        python ../generator/nanopb_generator.py fileproto.pb
index f052057..95112e4 100644 (file)
@@ -20,7 +20,7 @@
 #include <pb_encode.h>
 #include <pb_decode.h>
 
-#include "fileproto.h"
+#include "fileproto.pb.h"
 #include "common.h"
 
 bool printfile_callback(pb_istream_t *stream, const pb_field_t *field, void *arg)
index 04f88f0..aba0667 100644 (file)
@@ -20,7 +20,7 @@
 #include <pb_encode.h>
 #include <pb_decode.h>
 
-#include "fileproto.h"
+#include "fileproto.pb.h"
 #include "common.h"
 
 bool listdir_callback(pb_ostream_t *stream, const pb_field_t *field, const void *arg)
index 2826851..f09be34 100644 (file)
@@ -382,7 +382,7 @@ if __name__ == '__main__':
         print "Usage: " + sys.argv[0] + " file.pb"
         print "where file.pb has been compiled from .proto by:"
         print "protoc -ofile.pb file.proto"
-        print "Output fill be written to file.h and file.c"
+        print "Output fill be written to file.pb.h and file.pb.c"
         sys.exit(1)
     
     data = open(sys.argv[1]).read()
@@ -390,8 +390,8 @@ if __name__ == '__main__':
     enums, messages = parse_file(fdesc.file[0])
     
     noext = os.path.splitext(sys.argv[1])[0]
-    headername = noext + '.h'
-    sourcename = noext + '.c'
+    headername = noext + '.pb.h'
+    sourcename = noext + '.pb.c'
     headerbasename = os.path.basename(headername)
     
     print "Writing to " + headername + " and " + sourcename
index 2a4d8e1..d333cfd 100644 (file)
@@ -67,6 +67,10 @@ bool checkreturn pb_write(pb_ostream_t *stream, const uint8_t *buf, size_t count
 
 /* Main encoding stuff */
 
+/* Callbacks don't need this function because they usually know the data type
+ * without examining the field structure.
+ * Therefore it is static for now.
+ */
 static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *field,
                          const void *pData, size_t count, pb_encoder_t func)
 {
@@ -74,6 +78,9 @@ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie
     const void *p;
     size_t size;
     
+    if (count == 0)
+        return true;
+    
     if (PB_LTYPE(field->type) < PB_LTYPE_LAST_PACKABLE)
     {
         if (!pb_encode_tag(stream, PB_WT_STRING, field->tag))
index 38bdb25..5035af8 100644 (file)
@@ -1,12 +1,12 @@
 CFLAGS=-ansi -Wall -Werror -I .. -g -O0 --coverage
 LDFLAGS=--coverage
-DEPS=../pb_decode.h ../pb_encode.h ../pb.h person.h unittests.h
+DEPS=../pb_decode.h ../pb_encode.h ../pb.h person.pb.h unittests.h unittestproto.pb.h
 TESTS=test_decode1 test_encode1 decode_unittests encode_unittests
 
 all: $(TESTS) run_unittests breakpoints
 
 clean:
-       rm -f $(TESTS) *.o *.gcda *.gcno
+       rm -f $(TESTS) person.pb* *.o *.gcda *.gcno
 
 %.o: %.c $(DEPS)
        $(CC) $(CFLAGS) -c -o $@ $<
@@ -16,14 +16,16 @@ pb_encode.o: ../pb_encode.c $(DEPS)
 pb_decode.o: ../pb_decode.c $(DEPS)
        $(CC) $(CFLAGS) -c -o $@ $<
 
-test_decode1: test_decode1.o pb_decode.o person.o
-test_encode1: test_encode1.o pb_encode.o person.o
-decode_unittests: decode_unittests.o pb_decode.o person.o
-encode_unittests: encode_unittests.o pb_encode.o person.o
+test_decode1: test_decode1.o pb_decode.o person.pb.o
+test_encode1: test_encode1.o pb_encode.o person.pb.o
+decode_unittests: decode_unittests.o pb_decode.o unittestproto.pb.o
+encode_unittests: encode_unittests.o pb_encode.o unittestproto.pb.o
 
-person.c person.h: person.proto
-       protoc -I. -I../generator -I/usr/include -operson.pb $<
-       python ../generator/nanopb_generator.py person.pb
+%.pb: %.proto
+       protoc -I. -I../generator -I/usr/include -o$@ $<
+
+%.pb.c %.pb.h: %.pb
+       python ../generator/nanopb_generator.py $<
 
 breakpoints: ../*.c *.c
        grep -n 'return false;' $^ | cut -d: -f-2 | xargs -n 1 echo b > $@
index 6f2b77e..006ad90 100644 (file)
@@ -2,6 +2,7 @@
 #include <string.h>
 #include "pb_decode.h"
 #include "unittests.h"
+#include "unittestproto.pb.h"
 
 #define S(x) pb_istream_from_buffer((uint8_t*)x, sizeof(x) - 1)
 
@@ -15,24 +16,6 @@ bool stream_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
     return true;
 }
 
-typedef struct { size_t data_count; int32_t data[10]; } IntegerArray;
-const pb_field_t IntegerArray_fields[] = {
-    {1, PB_HTYPE_ARRAY | PB_LTYPE_VARINT, offsetof(IntegerArray, data),
-    pb_delta(IntegerArray, data_count, data),
-    pb_membersize(IntegerArray, data[0]),
-    pb_membersize(IntegerArray, data) / pb_membersize(IntegerArray, data[0])},
-    
-    PB_LAST_FIELD
-};
-
-typedef struct { pb_callback_t data; } CallbackArray;
-const pb_field_t CallbackArray_fields[] = {
-    {1, PB_HTYPE_CALLBACK | PB_LTYPE_VARINT, offsetof(CallbackArray, data),
-    0, pb_membersize(CallbackArray, data), 0},
-    
-    PB_LAST_FIELD
-};
-
 /* Verifies that the stream passed to callback matches the byte array pointed to by arg. */
 bool callback_check(pb_istream_t *stream, const pb_field_t *field, void *arg)
 {
@@ -224,6 +207,8 @@ int main()
         IntegerArray dest;
         
         COMMENT("Testing pb_decode with packed int32 field")
+        TEST((s = S("\x0A\x00"), pb_decode(&s, IntegerArray_fields, &dest)
+            && dest.data_count == 0))
         TEST((s = S("\x0A\x01\x01"), pb_decode(&s, IntegerArray_fields, &dest)
             && dest.data_count == 1 && dest.data[0] == 1))
         TEST((s = S("\x0A\x0A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A"), pb_decode(&s, IntegerArray_fields, &dest)
index ee7f7a3..5578a0a 100644 (file)
@@ -2,6 +2,7 @@
 #include <string.h>
 #include "pb_encode.h"
 #include "unittests.h"
+#include "unittestproto.pb.h"
 
 bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
 {
@@ -163,6 +164,27 @@ int main()
         TEST(WRITES(pb_enc_string(&s, NULL, &value), "\x00"))
     }
     
+    {
+        uint8_t buffer[10];
+        pb_ostream_t s;
+        IntegerArray msg = {5, {1, 2, 3, 4, 5}};
+        
+        COMMENT("Test pb_encode with int32 array")
+        
+        TEST(WRITES(pb_encode(&s, IntegerArray_fields, &msg), "\x0A\x05\x01\x02\x03\x04\x05"))
+        
+        msg.data_count = 0;
+        TEST(WRITES(pb_encode(&s, IntegerArray_fields, &msg), ""))
+        
+        msg.data_count = 10;
+        TEST(!pb_encode(&s, IntegerArray_fields, &msg))
+    }
+    
+    {
+        
+        
+    }
+    
     if (status != 0)
         fprintf(stdout, "\n\nSome tests FAILED!\n");