aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/examples
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2022-08-30 13:27:40 -0800
committerDavid M. Syzdek <david@syzdek.net>2022-08-30 13:27:40 -0800
commitbe9d437504da47201b6b6aaedd79a3b9efbdef2f (patch)
treea40079d0d32be58402c6205800a656f67a7215d9 /examples
parent2b720e5535b67834e7bf6eb32a504fe4bca9d3c0 (diff)
downloadbindletools-be9d437504da47201b6b6aaedd79a3b9efbdef2f.tar.gz
bindletools-be9d437504da47201b6b6aaedd79a3b9efbdef2f.tar.bz2
bindletools-be9d437504da47201b6b6aaedd79a3b9efbdef2f.tar.xz
bindletools-be9d437504da47201b6b6aaedd79a3b9efbdef2f.zip
examples: adding example-stack
Diffstat (limited to 'examples')
-rw-r--r--examples/example-stack.c156
1 files changed, 156 insertions, 0 deletions
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
66struct wc
67{
68 uintptr_t value;
69 const char * name;
70};
71
72
73/////////////////
74// //
75// Variables //
76// //
77/////////////////
78#pragma mark - Variables
79
80struct 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
101int
102main(
103 void );
104
105
106/////////////////
107// //
108// Functions //
109// //
110/////////////////
111#pragma mark - Functions
112
113int
114main(
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