Commit e022267769ff58e22ad2f455966137aab668356b

Thomas de Grivel 2017-12-21T11:45:10

bus7

diff --git a/build/so/Makefile b/build/so/Makefile
index 8447fa2..f96ffc1 100644
--- a/build/so/Makefile
+++ b/build/so/Makefile
@@ -7,8 +7,8 @@ CFLAGS = -O0 -ggdb -W -Wall -Werror -fpic
 
 LDFLAGS = -fPIC -shared
 LIBS =
-HEADERS = ord.h string7.h strings7.h
-OBJECTS = ord.o string7.o strings7.o
+HEADERS = bus7.h ord.h string7.h strings7.h
+OBJECTS = bus7.o ord.o string7.o strings7.o
 
 SRC = ${HEADERS} ${OBJECTS:%.o=%.c}
 
diff --git a/bus7.c b/bus7.c
new file mode 100644
index 0000000..1c194a4
--- /dev/null
+++ b/bus7.c
@@ -0,0 +1,4 @@
+
+#include "bus7.h"
+
+CODE_BUS7(8000)
diff --git a/bus7.h b/bus7.h
new file mode 100644
index 0000000..6a7d3c4
--- /dev/null
+++ b/bus7.h
@@ -0,0 +1,138 @@
+
+#ifndef BUS7_H
+#define BUS7_H
+
+#include "string7.h"
+#include "strings7.h"
+
+#define STRUCT_BUS7(count) struct bus7_ ## count
+#define S_BUS7(count) s_bus7_ ## count
+#define TYPEDEF_BUS7(count)                     \
+  typedef                                       \
+  STRUCT_BUS7(count)                            \
+       S_BUS7(count);                           \
+       
+#define STRUCT__BUS7(count)                     \
+  STRUCT_BUS7(count)                            \
+  {                                             \
+    unsigned in;                                \
+    unsigned out;                               \
+    unsigned len;                               \
+    strings7 r[count];                          \
+    strings7 e;                                 \
+  };                                            \
+
+#define INIT_BUS7(var)                          \
+  var.in = var.out = var.len = 0;
+
+#define BUS7_FIRST(count) bus7_ ## count ## _first
+#define PROTO_BUS7_FIRST(count)                         \
+  strings7 * BUS7_FIRST(count) (S_BUS7(count) *b)
+#define DEF_BUS7_FIRST(count)                   \
+  PROTO_BUS7_FIRST(count)                       \
+  {                                             \
+    if (!b || !b->len)                          \
+      return 0;                                 \
+    return b->r + b->out;                       \
+  }                                             \
+
+#define BUS7_LAST(count) bus7_ ## count ## _last
+#define PROTO_BUS7_LAST(count)                          \
+  strings7 * BUS7_LAST(count) (S_BUS7(count) *b)
+#define DEF_BUS7_LAST(count)                    \
+  PROTO_BUS7_LAST(count)                        \
+  {                                             \
+    if (!b || !b->len)                          \
+      return 0;                                 \
+    return b->r + b->in;                        \
+  }                                             \
+
+#define BUS7_LEN(count) bus7_ ## count ## _len
+#define PROTO_BUS7_LEN(count)                   \
+  unsigned BUS7_LEN(count) (S_BUS7(count) *b)
+#define DEF_BUS7_LEN(count)                     \
+  PROTO_BUS7_LEN(count)                         \
+  {                                             \
+    if (!b)                                     \
+      return 0;                                 \
+    return b->len;                              \
+  }                                             \
+
+#define BUS7_NTH(count) bus7_ ## count ## _nth
+#define PROTO_BUS7_NTH(count)                                   \
+  strings7 * BUS7_NTH(count) (S_BUS7(count) *b, unsigned n)
+#define DEF_BUS7_NTH(count)                             \
+  PROTO_BUS7_NTH(count)                                 \
+  {                                                     \
+    strings7 *r = BUS7_FIRST(count)(b);                 \
+    if (!r)                                             \
+      return 0;                                         \
+    unsigned l = count - (r - b->r);                    \
+    while (1) {                                         \
+      if (l == 0 || *r >= 0 || l < (unsigned) -*r) {    \
+        if (b->out < b->in)                             \
+          return 0;                                     \
+        r = b->r;                                       \
+        l = b->in;                                      \
+        if (l == 0 || *r >= 0 || l < (unsigned) -*r)    \
+          return 0;                                     \
+      }                                                 \
+      if (n == 0)                                       \
+        return r;                                       \
+      l += *r;                                          \
+      n--;                                              \
+      r -= *r;                                          \
+    }                                                   \
+  }                                                     \
+
+#define BUS7_STRING7(count) bus7_ ## count ## _string7
+#define PROTO_BUS7_STRING7(count)                   \
+  string7 * BUS7_STRING7(count) (string7 *s, S_BUS7(count) *b)
+#define DEF_BUS7_STRING7(count)                                         \
+  PROTO_BUS7_STRING7(count)                                             \
+  {                                                                     \
+    if (!b || !s || !b->len)                                            \
+      return 0;                                                         \
+    if ((b->out < b->in && b->out + b->r < s && s < b->in + b->r) ||    \
+        (b->out >= b->in && ((b->out + b->r < s && s < count + b->r) || \
+                             (b->r < s && s < b->in + b->r))))          \
+      return s;                                                         \
+    strings7 *r = BUS7_FIRST(count)(b);                                 \
+    if (!r)                                                             \
+      return 0;                                                         \
+    unsigned l = count - (r - b->r);                                    \
+    while (1) {                                                         \
+      if (l == 0 || *r >= 0 || l < (unsigned) -*r) {                    \
+        if (b->out < b->in)                                             \
+          return 0;                                                     \
+        r = b->r;                                                       \
+        l = b->in;                                                      \
+        if (l == 0 || *r >= 0 || l < (unsigned) -*r)                    \
+          return 0;                                                     \
+      }                                                                 \
+      if (string7_eq(r, s))                                             \
+        return r;                                                       \
+      l += *r;                                                          \
+      r -= *r;                                                          \
+    }                                                                   \
+  }                                                                     \
+
+#define HEADER_BUS7(count)                      \
+  TYPEDEF_BUS7(count)                           \
+  STRUCT__BUS7(count)                           \
+  PROTO_BUS7_FIRST(count);                      \
+  PROTO_BUS7_LAST(count);                       \
+  PROTO_BUS7_LEN(count);                        \
+  PROTO_BUS7_NTH(count);                        \
+  PROTO_BUS7_STRING7(count);
+
+#define CODE_BUS7(count)                        \
+  DEF_BUS7_FIRST(count)                         \
+  DEF_BUS7_LAST(count)                          \
+  DEF_BUS7_LEN(count)                           \
+  DEF_BUS7_NTH(count)                           \
+  DEF_BUS7_STRING7(count)
+
+HEADER_BUS7(8000)
+
+#endif /* BUS7_H */