diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-08-30 13:27:40 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-08-30 13:27:40 -0800 |
| commit | be9d437504da47201b6b6aaedd79a3b9efbdef2f (patch) | |
| tree | a40079d0d32be58402c6205800a656f67a7215d9 | |
| parent | 2b720e5535b67834e7bf6eb32a504fe4bca9d3c0 (diff) | |
| download | bindletools-be9d437504da47201b6b6aaedd79a3b9efbdef2f.tar.gz bindletools-be9d437504da47201b6b6aaedd79a3b9efbdef2f.tar.bz2 bindletools-be9d437504da47201b6b6aaedd79a3b9efbdef2f.tar.xz bindletools-be9d437504da47201b6b6aaedd79a3b9efbdef2f.zip | |
examples: adding example-stack
| -rw-r--r-- | Makefile.am | 8 | ||||
| -rw-r--r-- | examples/example-stack.c | 156 |
2 files changed, 164 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am index 9881145..103b19f 100644 --- a/Makefile.am +++ b/Makefile.am | |||
| @@ -158,8 +158,10 @@ endif | |||
| 158 | if ENABLE_EXAMPLES | 158 | if ENABLE_EXAMPLES |
| 159 | noinst_PROGRAMS += examples/example-bsearch | 159 | noinst_PROGRAMS += examples/example-bsearch |
| 160 | noinst_PROGRAMS += examples/example-queue | 160 | noinst_PROGRAMS += examples/example-queue |
| 161 | noinst_PROGRAMS += examples/example-stack | ||
| 161 | examples_DATA += examples/example-bsearch.c | 162 | examples_DATA += examples/example-bsearch.c |
| 162 | examples_DATA += examples/example-queue.c | 163 | examples_DATA += examples/example-queue.c |
| 164 | examples_DATA += examples/example-stack.c | ||
| 163 | endif | 165 | endif |
| 164 | 166 | ||
| 165 | 167 | ||
| @@ -175,6 +177,12 @@ examples_example_queue_LDADD = lib/libbindle.la | |||
| 175 | examples_example_queue_SOURCES = examples/example-queue.c | 177 | examples_example_queue_SOURCES = examples/example-queue.c |
| 176 | 178 | ||
| 177 | 179 | ||
| 180 | # macros for examples/example-stack | ||
| 181 | examples_example_stack_DEPENDENCIES = lib/libbindle.la | ||
| 182 | examples_example_stack_LDADD = lib/libbindle.la | ||
| 183 | examples_example_stack_SOURCES = examples/example-stack.c | ||
| 184 | |||
| 185 | |||
| 178 | # macros for src/bindletools | 186 | # macros for src/bindletools |
| 179 | src_bindletools_DEPENDENCIES = lib/libbindle.la | 187 | src_bindletools_DEPENDENCIES = lib/libbindle.la |
| 180 | src_bindletools_LDADD = lib/libbindle.la | 188 | src_bindletools_LDADD = lib/libbindle.la |
diff --git a/examples/example-stack.c b/examples/example-stack.c new file mode 100644 index 0000000..42062d5 --- /dev/null +++ b/examples/example-stack.c | |||
| @@ -0,0 +1,156 @@ | |||
| 1 | /* | ||
| 2 | * Bindle Binaries Tools | ||
| 3 | * Copyright (C) 2022 David M. Syzdek <david@syzdek.net>. | ||
| 4 | * All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions are | ||
| 8 | * met: | ||
| 9 | * | ||
| 10 | * * Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * * Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * * Neither the name of David M. Syzdek nor the | ||
| 16 | * names of its contributors may be used to endorse or promote products | ||
| 17 | * derived from this software without specific prior written permission. | ||
| 18 | * | ||
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M. SYZDEK BE LIABLE FOR | ||
| 23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 29 | * SUCH DAMAGE. | ||
| 30 | */ | ||
| 31 | #define _EXAMPLES_EXAMPLE_STACK_C 1 | ||
| 32 | |||
| 33 | |||
| 34 | /////////////// | ||
| 35 | // // | ||
| 36 | // Headers // | ||
| 37 | // // | ||
| 38 | /////////////// | ||
| 39 | #pragma mark - Headers | ||
| 40 | |||
| 41 | #include <stdio.h> | ||
| 42 | #include <stdlib.h> | ||
| 43 | #include <string.h> | ||
| 44 | #include <stdint.h> | ||
| 45 | #include <bindle.h> | ||
| 46 | |||
| 47 | |||
| 48 | /////////////////// | ||
| 49 | // // | ||
| 50 | // Definitions // | ||
| 51 | // // | ||
| 52 | /////////////////// | ||
| 53 | #pragma mark - Definitions | ||
| 54 | |||
| 55 | #undef PROGRAM_NAME | ||
| 56 | #define PROGRAM_NAME "example-stack" | ||
| 57 | |||
| 58 | |||
| 59 | ////////////////// | ||
| 60 | // // | ||
| 61 | // Data Types // | ||
| 62 | // // | ||
| 63 | ////////////////// | ||
| 64 | #pragma mark - Data Types | ||
| 65 | |||
| 66 | struct wc | ||
| 67 | { | ||
| 68 | uintptr_t value; | ||
| 69 | const char * name; | ||
| 70 | }; | ||
| 71 | |||
| 72 | |||
| 73 | ///////////////// | ||
| 74 | // // | ||
| 75 | // Variables // | ||
| 76 | // // | ||
| 77 | ///////////////// | ||
| 78 | #pragma mark - Variables | ||
| 79 | |||
| 80 | struct wc wcs[] = | ||
| 81 | { | ||
| 82 | { 0xFFFFFF, "white" }, { 0xC0C0C0, "silver" }, | ||
| 83 | { 0x808080, "gray" }, { 0x000000, "black" }, | ||
| 84 | { 0xFF0000, "red" }, { 0x800000, "maroon" }, | ||
| 85 | { 0xFFFF00, "yellow" }, { 0x808000, "olive" }, | ||
| 86 | { 0x00FF00, "lime" }, { 0x008000, "green" }, | ||
| 87 | { 0x00FFFF, "aqua" }, { 0x008080, "teal" }, | ||
| 88 | { 0x0000FF, "blue" }, { 0x000080, "navy" }, | ||
| 89 | { 0xFF00FF, "fuchsia" }, { 0x800080, "purple" } | ||
| 90 | }; | ||
| 91 | #define nr_wcs (sizeof(wcs) / sizeof(wcs[0])) | ||
| 92 | |||
| 93 | |||
| 94 | ////////////////// | ||
| 95 | // // | ||
| 96 | // Prototypes // | ||
| 97 | // // | ||
| 98 | ////////////////// | ||
| 99 | #pragma mark - Prototypes | ||
| 100 | |||
| 101 | int | ||
| 102 | main( | ||
| 103 | void ); | ||
| 104 | |||
| 105 | |||
| 106 | ///////////////// | ||
| 107 | // // | ||
| 108 | // Functions // | ||
| 109 | // // | ||
| 110 | ///////////////// | ||
| 111 | #pragma mark - Functions | ||
| 112 | |||
| 113 | int | ||
| 114 | main( | ||
| 115 | void ) | ||
| 116 | { | ||
| 117 | size_t pos; | ||
| 118 | size_t nel; | ||
| 119 | size_t width; | ||
| 120 | ssize_t rc; | ||
| 121 | struct wc ** list; | ||
| 122 | struct wc * obj; | ||
| 123 | struct wc ** res; | ||
| 124 | |||
| 125 | nel = 0; | ||
| 126 | list = NULL; | ||
| 127 | width = sizeof(struct wc *); | ||
| 128 | |||
| 129 | for(pos = 0; (pos < nr_wcs); pos++) | ||
| 130 | { | ||
| 131 | obj = &wcs[pos]; | ||
| 132 | printf("pushing: #%06x %s\n", (unsigned)obj->value, obj->name); | ||
| 133 | rc = bindle_push(&obj, (void **)&list, &nel, width, realloc); | ||
| 134 | if (rc == -1) | ||
| 135 | { | ||
| 136 | fprintf(stderr, "error adding %s to stack\n", wcs[pos].name); | ||
| 137 | free(list); | ||
| 138 | return(1); | ||
| 139 | }; | ||
| 140 | }; | ||
| 141 | |||
| 142 | if ((res = bindle_peek(list, nel, width)) != NULL) | ||
| 143 | printf("peeked: #%06x %s\n", (unsigned)(*res)->value, (*res)->name); | ||
| 144 | if ((res = bindle_peek(list, nel, width)) != NULL) | ||
| 145 | printf("peeked: #%06x %s\n", (unsigned)(*res)->value, (*res)->name); | ||
| 146 | |||
| 147 | while((res = bindle_pop(list, &nel, width)) != NULL) | ||
| 148 | printf("popped: #%06x %s\n", (unsigned)(*res)->value, (*res)->name); | ||
| 149 | |||
| 150 | free(list); | ||
| 151 | |||
| 152 | return(1); | ||
| 153 | } | ||
| 154 | |||
| 155 | /* end of source */ | ||
| 156 | |||
