-
Notifications
You must be signed in to change notification settings - Fork 2
/
blocks.c
177 lines (149 loc) · 3.34 KB
/
blocks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* Bvim - BVi IMproved, binary analysis framework
*
* Copyright 1996-2004 by Gerhard Buergmann <[email protected]>
* Copyright 2011 by Anton Kochkov <[email protected]>
*
* This file is part of Bvim.
*
* Bvim is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bvim is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Bvim. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include "bvim.h"
#include "blocks.h"
#include "bscript.h"
extern core_t core;
/* =============== Blocks list/buffer abstractions =============== */
/* Preallocate some memory for future blocks allocation */
int InitBlocksList(buf_t *buf, int N)
{
int i = 0;
buf->blocks = (block_link)malloc((N + 1)*(sizeof *(buf->blocks)));
for (i = 0; i < N + 1; i++) {
buf->blocks[i].next = &(buf->blocks[i + 1]);
}
buf->blocks[N].next = NULL;
return 0;
}
int BlockAdd(buf_t *buf, struct block_item i)
{
block_link t = NULL;
t = (block_link)malloc(sizeof(*t));
if (t != NULL) {
t->item = i;
if (buf->blocks != NULL) {
t->next = buf->blocks->next;
buf->blocks->next = t;
} else {
buf->blocks = t;
buf->blocks->next = NULL;
}
} else {
return -1;
}
return 0;
}
void BlockFree(buf_t *buf, block_link x)
{
BlockInsertNext(buf->blocks, x);
}
void BlockInsertNext(block_link x, block_link t)
{
t->next = x->next;
x->next = t;
}
block_link BlockDeleteNext(block_link x)
{
block_link t = NULL;
if (x != NULL) {
t = x->next;
x->next = t->next;
}
return t;
}
block_link BlockNext(block_link x)
{
return x->next;
}
struct block_item BlockGet(block_link x)
{
return x->item;
}
/* ============= Blocks interface/handlers ================ */
/* Iterator of any functions on blocks list,
* where result - expected result for function
* All blocks are unique */
int blocks__Iterator(buf_t *buf, int (*(func))(), int result)
{
block_link t;
t = buf->blocks;
while (t != NULL)
{
if (t->item.id != 0) {
if ((*(func))(&(t->item)) == result) {
return 0;
}
}
t = t->next;
}
return -1;
}
int blocks__Add(buf_t *buf, struct block_item b) {
BlockAdd(buf, b);
return 0;
}
int blocks__DelByID(buf_t *buf, int id) {
return 0;
}
int blocks__DelByName(buf_t *buf, char* name) {
return 0;
}
struct block_item* blocks__GetByID(buf_t *buf, unsigned int id) {
block_link t;
t = buf->blocks;
while (t != NULL)
{
if (t->item.id == id) return &(t->item);
t = t->next;
}
return NULL;
}
struct block_item* blocks__GetByName(buf_t *buf, char* name) {
block_link t;
t = buf->blocks;
while (t != NULL)
{
if (!strcmp(t->item.name, name)) return &(t->item);
t = t->next;
}
return NULL;
}
/* Do not use this in exported API! */
int blocks__Init(buf_t *buf)
{
//InitBlocksList(buf, 10);
return 0;
}
/* Do not use this in exported API! */
int blocks__Destroy(buf_t *buf)
{
block_link t;
t = buf->blocks;
while (t != NULL)
{
free(t);
t = t->next;
}
return 0;
}