blob: 2e70e15ea39ae725959376351c4e554ebaeaa339 [file] [log] [blame]
Brian Silverman1885bd02014-02-13 12:28:12 -08001require File.dirname(__FILE__) + '/load.rb'
2
Brian Silverman074392a2014-03-24 16:32:44 -07003# TODO(brians): Special-case Time too and float/double if we can find a good way to do it.
4GenericTypeNames = ['float', 'double', 'char', '::aos::time::Time']
5IntegerSizes = [8, 16, 32, 64]
Brian Silverman1885bd02014-02-13 12:28:12 -08006
Brian Silvermancbbc3c42014-02-22 18:33:17 -08007WriteIffChanged.open(ARGV[0]) do |output|
Brian Silverman1885bd02014-02-13 12:28:12 -08008 output.puts <<END
9// This file is generated by #{File.expand_path(__FILE__)}.
10// DO NOT EDIT BY HAND!
11
12#include <sys/types.h>
13#include <stdint.h>
14#include <inttypes.h>
15#include <stdio.h>
16
17#include "aos/common/byteorder.h"
Brian Silverman96e6d5a2014-03-24 15:55:40 -070018#include "aos/common/time.h"
Brian Silverman074392a2014-03-24 16:32:44 -070019#include "aos/common/print_field_helpers.h"
Brian Silverman1885bd02014-02-13 12:28:12 -080020
21namespace aos {
22
Brian Silvermand4b1ed52014-02-15 15:26:04 -080023bool PrintField(char *output, size_t *output_bytes, const void *input,
Brian Silverman1885bd02014-02-13 12:28:12 -080024 size_t *input_bytes, uint32_t type) {
25 switch (type) {
Brian Silverman074392a2014-03-24 16:32:44 -070026#{GenericTypeNames.collect do |name|
Brian Silverman1885bd02014-02-13 12:28:12 -080027 message_element = Target::MessageElement.new(name, 'value')
28 statement = MessageElementStmt.new(name, 'value')
29 message_element.size = statement.size
30 print_args = []
31 message_element.fetchPrintArgs(print_args)
32 next <<END2
33 case #{message_element.getTypeID()}:
34 {
35 if (*input_bytes < #{statement.size}) return false;
36 *input_bytes -= #{statement.size};
37 #{name} value;
38 to_host(static_cast<const char *>(input), &value);
39 int ret = snprintf(output, *output_bytes,
40 "#{statement.toPrintFormat()}",
41 #{print_args[0]});
42 if (ret < 0) return false;
43 if (static_cast<unsigned int>(ret) >= *output_bytes) return false;
Brian Silverman074392a2014-03-24 16:32:44 -070044 *output_bytes -= ret;
Brian Silverman1885bd02014-02-13 12:28:12 -080045 return true;
46 }
47END2
48end.join('')}
Brian Silverman074392a2014-03-24 16:32:44 -070049#{IntegerSizes.collect do |size|
50 [true, false].collect do |signed|
51 size_bytes = size / 8
52 name = "#{signed ? '' : 'u'}int#{size}_t"
53 message_element = Target::MessageElement.new(name, 'value')
54 message_element.size = size_bytes
55 next <<END2
56 case #{message_element.getTypeID()}:
57 {
58 if (*input_bytes < #{size_bytes}) return false;
59 *input_bytes -= #{size_bytes};
60 #{name} value;
61 to_host(static_cast<const char *>(input), &value);
62 return PrintInteger<#{name}>(output, value, output_bytes);
63 }
64END2
65 end
66end.flatten.join('')}
67#{
68 message_element = Target::MessageElement.new('bool', 'value')
69 message_element.size = 1
70 r = <<END2
71 case #{message_element.getTypeID()}:
72 {
73 if (*input_bytes < 1) return false;
74 if (*output_bytes < 1) return false;
75 *input_bytes -= 1;
76 bool value = static_cast<const char *>(input)[0];
77 *output_bytes += 1;
78 *output = value ? 'T' : 'f';
79 return true;
80 }
81END2
82}
Brian Silverman1885bd02014-02-13 12:28:12 -080083 default:
84 return false;
85 }
86}
87
88} // namespace aos
89END
90end