Skip to content

Commit

Permalink
release R2102
Browse files Browse the repository at this point in the history
  • Loading branch information
maierkomor committed Feb 27, 2021
1 parent 0ba3902 commit 3578ec9
Show file tree
Hide file tree
Showing 30 changed files with 1,213 additions and 177 deletions.
4 changes: 2 additions & 2 deletions .hg_archival.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
repo: 74e97a8352b0714d95edee6a4e88879188f84695
node: e4ffcdf1b68d181b4fd4053628f0039dd190aba7
node: 8c6fd17949c867af77d3cb932e5fb19ac040984c
branch: default
tag: R2101
tag: R2102
13 changes: 13 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
Release R2102:
==============
- fix: potential bus error with write_u64 on little endian
- fix: do not set valid flag when parsing ascii fails
- fix: stringtype handling may fallback to C type
- fix: multiple options handling
- fix: return type must be ssize_t if function returns <0
- fix: build fixes for examples
- enhancement: support for setting bytes with hex encoded data
- enhancement: updated syscfg example to demo setByName
- enhancement: added doxygen comments to the generated files
- enhancement: opimization for use of empty string as invalid value

Release R2101:
==============
- enhancement: support option used for messages
Expand Down
5 changes: 4 additions & 1 deletion examples/ascii_output/ip4_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,18 @@ int main()

// print ASCII representation
nc.toASCII(cout);
cout << endl;

// modification of contents can also be done by setting by name
nc.setByName("hostname","myhost");
nc.setByName("degC","19.8");
nc.toASCII(cout);
cout << endl;
cout << endl;

// binary output:
cout << "encoded binary has " << nc.calcSize() << " bytes\n"
"binary output:\n\n";
"binary output:\n";

uint8_t buf[nc.calcSize()];
nc.toMemory(buf,sizeof(buf)); // serialze to buffer
Expand Down
2 changes: 1 addition & 1 deletion examples/datatypes/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ WFC = ../../bin/wfc
CXX = g++
CXXFLAGS= -I../../include -Os

CXXSRC = datatypes.cpp test_dt.cpp ../../lib/wfc_support.cpp
CXXSRC = datatypes.cpp test_dt.cpp
CXXOBJ = $(CXXSRC:%.cpp=%.o)

test_dt: $(CXXOBJ)
Expand Down
13 changes: 12 additions & 1 deletion examples/syscfg/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
CXXFLAGS = -Os -g -I../../include
CXXSRCS = main_stream.cpp syscfg_efix.cpp syscfg_edyn.cpp
CXXOBJS = $(CXXSRCS:%.cpp=%.o)
DEPS = $(CXXSRCS:%.cpp=.%.d)

all: pc edyn efix

.%.d: %.cpp
$(CXX) $(CXXFLAGS) -MG -MT$(@:.%.d=%.o) -MM $< > $@

syscfg_pc.cpp: syscfg.wfc
../../bin/wfc -tpc -o syscfg_pc syscfg.wfc

Expand All @@ -22,11 +26,18 @@ main_efix.o: main_posix.cpp syscfg_efix.cpp


pc: syscfg_pc.o main_stream.o
$(CXX) $(CXXFLAGS) -I../../include main_stream.o syscfg_pc.o ../../lib/wfc_support.cpp -o $@
$(CXX) $(CXXFLAGS) -I../../include main_stream.o syscfg_pc.o -o $@

edyn: syscfg_edyn.o main_edyn.o
$(CXX) $(CXXFLAGS) -I../../include main_edyn.o syscfg_edyn.o -o $@

efix: syscfg_efix.o main_efix.o
$(CXX) $(CXXFLAGS) -I../../include main_efix.o syscfg_efix.o -o $@

clean:
rm -f $(CXXOBJS) syscfg_pc.* syscfg_edy.* syscfg_efix.*

.depend: syscfg_pc.cpp syscfg_edyn.cpp syscfg_efix.cpp | $(DEPS)
cat $(DEPS) > .depend

include .depend
21 changes: 21 additions & 0 deletions examples/syscfg/ip4ascii.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include <stdlib.h>

inline void ip4_ascii(std::ostream &str, uint32_t ip)
{
str << (ip & 0xff) << '.' << ((ip >> 8) & 0xff) << '.' << ((ip >> 16) & 0xff) << '.' << ((ip >> 24 ) & 0xff);
}


static int parse_ipv4(uint32_t *ip, const char *str)
{
uint8_t b[4];
int n = 0;
int r = sscanf(str,"%hhu.%hhu.%hhu.%hhu%n",b+0,b+1,b+2,b+3,&n);
if (4 == r) {
*ip = ((uint32_t)b[0]) | ((uint32_t)b[1]<<8) | ((uint32_t)b[2]<<16) | ((uint32_t)b[3]<<24);
return n;
}
std::cerr << "parser error " << n << "\n";
return -1;
}
60 changes: 58 additions & 2 deletions examples/syscfg/main_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ void saveConfig(const Config &cfg)
out.open(CfgName); // open file
out << str; // write to file
if (out.good())
cout << "writing config successful\n";
cout << "\nwriting config successful\n";
else
cerr << "error writing config\n";
cerr << "\nerror writing config\n";
}


Expand All @@ -56,6 +56,18 @@ int readConfig(Config &cfg)
}


static uint32_t str_to_ip(const char *str)
{
uint8_t b[4];
int n = sscanf(str,"%hhu.%hhu.%hhu.%hhu",b+0,b+1,b+2,b+3);
if (4 == n) {
uint32_t ip = ((uint32_t)b[0]<<24) | ((uint32_t)b[1]<<16) | ((uint32_t)b[2]<<8) | (uint32_t)b[3];
return ip;
}
return 0;
}


int main()
{
Config cfg;
Expand All @@ -69,7 +81,51 @@ int main()
// do some work here
// e.g. produce ASCII output
cfg.toASCII(std::cout);

cout << "\nNow continue interactively, by setting gateway and netmask.\n"
"Therefore, enter parameter name and its argument separated by a singel space.\n"
"e.g. type 'netmask 24'\n";
char line[80];
do {
cin.getline(line,sizeof(line));
char *sp = strchr(line,' ');
if (sp) {
*sp = 0;
if (0 > cfg.setByName(line,sp+1))
cerr << "error parsing '" << line << "' = '" << sp+1 << "'\n";
}
} while (line[0] != 0);

// without setByName
/*
char line[80];
do {
// this code misses necessary checks to be more readable as an example
if (!memcmp(line,"gateway ",8)) {
uint32_t ip = str_to_ip(line+8);
if (ip)
cfg.set_gateway(ip);
else
cerr << "invalid IP address\n";
} else if (!memcmp(line,"ipv4 ",5)) {
uint32_t ip = str_to_ip(line+5);
if (ip)
cfg.set_ipv4(ip);
else
cerr << "invalid IP address '" << line+4 << "'\n";
} else if (!memcmp(line,"netmask ",8)) {
long v = strtol(line+8,0,0);
cfg.set_netmask(v);
} else if (line[0] != 0) {
cerr << "invalid parameter\n";
}
// ...
} while (line[0] != 0);
*/

cfg.toASCII(std::cout);
cout << "\n";
// save the config before terminating
saveConfig(cfg);
return 0;
Expand Down
9 changes: 6 additions & 3 deletions examples/syscfg/syscfg.wfc
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ option embedded_dyn : embedded {

option embedded_fix : embedded {
header=<fstring.h>;
/Config/hostname : stringtype = "FString<16>";
/Config/wifi_ssid: stringtype = "FString<32>";
/Config/wifi_pass: stringtype = "FString<32>";
/Config/hostname : stringtype = "FString<16>";
/Config/wifi_ssid: stringtype = "FString<32>";
/Config/wifi_pass: stringtype = "FString<32>";
}

option pc {
toASCII = toASCII;
endian = little;
optimize = speed;
header="ip4ascii.h";
/Config/ipv4: to_ascii = ip4_ascii, parse_ascii = parse_ipv4;
/Config/gateway: to_ascii = ip4_ascii, parse_ascii = parse_ipv4;
}

message Config
Expand Down
62 changes: 58 additions & 4 deletions include/bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define _BYTES_H

#include <inttypes.h>
#include <stdlib.h>

struct Bytes
{
Expand All @@ -29,14 +30,43 @@ struct Bytes
, len(0)
{ }

explicit Bytes(const char *d)
: len(strlen(d))
{
bytes = (char *)malloc(len);
memcpy(bytes,d,len);
}

Bytes(const char *d, size_t l)
: bytes(d)
: bytes((char *)malloc(l))
, len(l)
{ }
{ memcpy(bytes,d,l); }

Bytes(const Bytes &b)
: bytes((char *)malloc(b.len))
, len(b.len)
{ memcpy(bytes,b.bytes,len); }

~Bytes()
{ free(bytes); }

Bytes &operator = (const Bytes &b)
{
assign(b.bytes,b.len);
return *this;
}

Bytes &operator = (const char *cstr)
{
size_t l = strlen(cstr);
assign(cstr,l);
return *this;
}

void assign(const char *d, size_t l)
{
bytes = d;
bytes = (char*)realloc((void*)bytes,l);
memcpy(bytes,d,l);
len = l;
}

Expand All @@ -51,15 +81,39 @@ struct Bytes

void clear()
{
free((void*)bytes);
bytes = 0;
len = 0;
}

void push_back(char c)
{
size_t at = len;
++len;
bytes = (char *) realloc((void*)bytes,len);
bytes[at] = c;
}

bool operator != (const Bytes &r) const
{ return (len != r.len) || (0 != memcmp(bytes,r.bytes,len)); }

bool operator == (const Bytes &r) const
{ return (len == r.len) && (0 == memcmp(bytes,r.bytes,len)); }

bool operator == (const char *cstr) const
{
size_t l = strlen(cstr);
return (l == len) && (0 == memcmp(bytes,cstr,l));
}

bool operator != (const char *cstr) const
{
size_t l = strlen(cstr);
return (l != len) || (0 != memcmp(bytes,cstr,l));
}

private:
const char *bytes;
char *bytes;
size_t len;
};

Expand Down
8 changes: 3 additions & 5 deletions include/fstring.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020, Thomas Maier-Komor
* Copyright (C) 2020-2021, Thomas Maier-Komor
*
* This source file belongs to Wire-Format-Compiler.
*
Expand Down Expand Up @@ -44,13 +44,11 @@ class FString
l = len-1;
memcpy(str,s,l);
str[l] = 0;
str[len-1] = 0;
}

FString(const FString &a)
{
strcpy(str,a.str);
str[len-1] = 0;
}

FString &operator = (const FString &a)
Expand All @@ -61,8 +59,8 @@ class FString

FString &operator += (const FString &a)
{
strncat(str,a.str,len-1);
str[len-1] = 0;
size_t l = strlen(str);
strncat(str,a.str,len-l-1);
return *this;
}

Expand Down
Loading

0 comments on commit 3578ec9

Please sign in to comment.