Rev 7973 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5959 | leency | 1 | #ifndef INCLUDE_COLLECTION_H |
2 | #define INCLUDE_COLLECTION_H |
||
3 | #print "[include |
||
4 | |||
6285 | leency | 5 | /*======================================================== |
6 | = = |
||
7972 | leency | 7 | = Integer = |
8 | = = |
||
9 | ========================================================*/ |
||
10 | |||
11 | struct collection_int |
||
12 | { |
||
13 | dword buf; |
||
14 | dword buf_size; |
||
15 | unsigned count; |
||
16 | void alloc(); |
||
17 | void add(); |
||
18 | dword get(); |
||
8330 | leency | 19 | dword set(); |
7972 | leency | 20 | void swap(); |
21 | dword len(); |
||
22 | dword get_last(); |
||
23 | void pop(); |
||
24 | void drop(); |
||
25 | }; |
||
26 | |||
27 | :void collection_int::alloc() { |
||
28 | if (!buf) { |
||
29 | buf_size = 4096; |
||
30 | buf = malloc(4096); |
||
31 | } else { |
||
32 | buf_size += 4096; |
||
33 | buf = realloc(buf, buf_size); |
||
34 | } |
||
35 | } |
||
36 | |||
37 | :void collection_int::add(dword _in) { |
||
38 | if (!buf) || (count * sizeof(dword) >= buf_size) alloc(); |
||
39 | EAX = count * sizeof(dword) + buf; |
||
40 | ESDWORD[EAX] = _in; |
||
41 | count++; |
||
42 | } |
||
43 | |||
44 | :dword collection_int::get(dword pos) { |
||
45 | if (pos<0) || (pos>=count) return 0; |
||
46 | return ESDWORD[pos * sizeof(dword) + buf]; |
||
47 | } |
||
48 | |||
49 | |||
8330 | leency | 50 | :dword collection_int::set(dword pos, _in) { |
7972 | leency | 51 | while (pos >= count) add(0); |
52 | EAX = pos * sizeof(dword) + buf; |
||
53 | ESDWORD[EAX] = _in; |
||
8330 | leency | 54 | return ESDWORD[EAX]; |
7972 | leency | 55 | } |
56 | |||
57 | :void collection_int::swap(dword pos1, pos2) { |
||
58 | while (pos1 >= count) add(0); |
||
59 | while (pos2 >= count) add(0); |
||
60 | EAX = pos1 * sizeof(dword) + buf; |
||
61 | EBX = pos2 * sizeof(dword) + buf; |
||
62 | ESDWORD[EAX] >< ESDWORD[EBX]; |
||
63 | } |
||
64 | |||
65 | :dword collection_int::len(dword pos) { |
||
66 | if (pos<0) || (pos+1>=count) return 0; |
||
67 | return get(pos+1) - get(pos); |
||
68 | } |
||
69 | |||
70 | :dword collection_int::get_last() { |
||
71 | return get(count-1); |
||
72 | } |
||
73 | |||
74 | :void collection_int::pop() { |
||
75 | if (count>0) count--; |
||
76 | } |
||
77 | |||
78 | :void collection_int::drop() { |
||
79 | count = 0; |
||
80 | } |
||
81 | |||
82 | /*======================================================== |
||
83 | = = |
||
6285 | leency | 84 | = String = |
85 | = = |
||
86 | ========================================================*/ |
||
87 | |||
5959 | leency | 88 | struct collection |
89 | { |
||
5965 | leency | 90 | int realloc_size, count; |
91 | dword data_start; |
||
5959 | leency | 92 | dword data_size; |
7972 | leency | 93 | collection_int offset; |
5965 | leency | 94 | int add(); |
7049 | leency | 95 | int addn(); |
7738 | leency | 96 | dword get(); //get_name_by_pos |
97 | dword get_pos_by_name(); |
||
5959 | leency | 98 | void drop(); |
5965 | leency | 99 | void increase_data_size(); |
7878 | leency | 100 | dword get_last(); |
7885 | leency | 101 | bool pop(); |
5959 | leency | 102 | }; |
103 | |||
7286 | leency | 104 | :void collection::increase_data_size() { |
5965 | leency | 105 | int filled_size; |
106 | if (realloc_size<4096) realloc_size = 4096; |
||
107 | if (!data_size) { |
||
108 | data_size = realloc_size; |
||
109 | data_start = malloc(realloc_size); |
||
110 | } |
||
111 | else { |
||
112 | data_size = data_size + realloc_size; |
||
113 | data_start = realloc(data_start, data_size); |
||
114 | } |
||
5959 | leency | 115 | } |
116 | |||
7286 | leency | 117 | :int collection::add(dword in) { |
7049 | leency | 118 | return addn(in, strlen(in)); |
119 | } |
||
120 | |||
7286 | leency | 121 | :int collection::addn(dword in, len) { |
7972 | leency | 122 | if (offset.get(count)+len+2 > data_size) { |
5965 | leency | 123 | increase_data_size(); |
7049 | leency | 124 | addn(in, len); |
125 | return 1; |
||
5965 | leency | 126 | } |
7972 | leency | 127 | strncpy(data_start+offset.get(count), in, len); |
5959 | leency | 128 | count++; |
7972 | leency | 129 | offset.set(count, offset.get(count-1) + len + 1); |
5965 | leency | 130 | return 1; |
5959 | leency | 131 | } |
132 | |||
7286 | leency | 133 | :dword collection::get(dword pos) { |
5974 | leency | 134 | if (pos<0) || (pos>=count) return 0; |
7972 | leency | 135 | return data_start + offset.get(pos); |
5959 | leency | 136 | } |
137 | |||
7878 | leency | 138 | :dword collection::get_last() { |
139 | return get(count-1); |
||
140 | } |
||
141 | |||
7738 | leency | 142 | :dword collection::get_pos_by_name(dword name) { |
143 | dword i; |
||
144 | for (i=0; i |
||
7972 | leency | 145 | if (strcmp(data_start + offset.get(i), name)==0) return i; |
7738 | leency | 146 | } |
147 | return -1; |
||
148 | } |
||
149 | |||
7286 | leency | 150 | :void collection::drop() { |
5965 | leency | 151 | if (data_start) free(data_start); |
7771 | leency | 152 | data_size = 0; |
153 | data_start = 0; |
||
7972 | leency | 154 | offset.drop(); |
7771 | leency | 155 | count = 0; |
5959 | leency | 156 | } |
157 | |||
7885 | leency | 158 | :bool collection::pop() { |
159 | if (count>0) count--; |
||
7878 | leency | 160 | } |
6285 | leency | 161 | |
5959 | leency | 162 | #endif0)>4096)>0)>>0)> |