pushpull: new module.
[ccan] / ccan / pushpull / _info
1 #include "config.h"
2 #include <stdio.h>
3 #include <string.h>
4
5 /**
6  * pushpull - simple marshalling/unmarshalling routines
7  *
8  * This code lets you clearly add simple types into a buffer (the push
9  * functions) and remove them (the pull functions).  The buffer stores
10  * the values as little-endian for machine portability.  The pull functions
11  * don't need to be checked on every call, but error state is kept so you
12  * can check if there was an error at the end.
13  *
14  * The normal way to use this is to create your own higher-level marshal
15  * and unmarshal functions in terms of these.
16  *
17  * Author: Rusty Russell <rusty@rustcorp.com.au>
18  * License: CC0 (Public domain)
19  *
20  * Example:
21  *      #include <ccan/pushpull/push.h>
22  *      #include <ccan/pushpull/pull.h>
23  *      #include <ccan/err/err.h>
24  *      #include <string.h>
25  *      #include <stdio.h>
26  *      #include <unistd.h>
27  *
28  *      int main(int argc, char *argv[])
29  *      {
30  *              if (argv[1] && !strcmp(argv[1], "push")) {
31  *                      int i;
32  *                      char *buf = malloc(1);
33  *                      size_t len = 0;
34  *
35  *                      // We ignore allocation failure!
36  *                      for (i = 2; i < argc; i++)
37  *                              push_u32(&buf, &len, atol(argv[i]));
38  *
39  *                      write(STDOUT_FILENO, buf, len);
40  *              } else if (argc == 2 && !strcmp(argv[1], "pull")) {
41  *                      int r, max = 32;
42  *                      size_t len = 0;
43  *                      char *buf = malloc(max);
44  *                      const char *p;
45  *                      uint32_t val;
46  *
47  *                      while ((r = read(STDIN_FILENO, buf+len, max-len)) > 0) {
48  *                              len += r;
49  *                              if (len == max) {
50  *                                      max *= 2;
51  *                                      // We crash on allocation failure
52  *                                      buf = realloc(buf, max);
53  *                              }
54  *                      }
55  *
56  *                      p = buf;
57  *                      while (pull_u32(&p, &len, &val))
58  *                              printf("%u ", val);
59  *              } else
60  *                      errx(1, "Usage: %s [push|pull] [<number>...]", argv[0]);
61  *              return 0;
62  *      }
63  */
64 int main(int argc, char *argv[])
65 {
66         /* Expect exactly one argument */
67         if (argc != 2)
68                 return 1;
69
70         if (strcmp(argv[1], "depends") == 0) {
71                 printf("ccan/endian\n");
72                 return 0;
73         }
74
75         return 1;
76 }