Rev 7738 | Rev 7878 | 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 | = = |
||
7 | = String = |
||
8 | = = |
||
9 | ========================================================*/ |
||
10 | |||
5959 | leency | 11 | struct collection |
12 | { |
||
5965 | leency | 13 | int realloc_size, count; |
14 | dword data_start; |
||
5959 | leency | 15 | dword data_size; |
7049 | leency | 16 | dword element_offset[4000]; |
5965 | leency | 17 | int add(); |
7049 | leency | 18 | int addn(); |
7738 | leency | 19 | dword get(); //get_name_by_pos |
20 | dword get_pos_by_name(); |
||
5959 | leency | 21 | void drop(); |
5965 | leency | 22 | void increase_data_size(); |
5959 | leency | 23 | }; |
24 | |||
7286 | leency | 25 | :void collection::increase_data_size() { |
5965 | leency | 26 | int filled_size; |
27 | if (realloc_size<4096) realloc_size = 4096; |
||
28 | if (!data_size) { |
||
29 | data_size = realloc_size; |
||
30 | data_start = malloc(realloc_size); |
||
31 | } |
||
32 | else { |
||
33 | data_size = data_size + realloc_size; |
||
34 | data_start = realloc(data_start, data_size); |
||
35 | } |
||
5959 | leency | 36 | } |
37 | |||
7286 | leency | 38 | :int collection::add(dword in) { |
7049 | leency | 39 | return addn(in, strlen(in)); |
40 | } |
||
41 | |||
7286 | leency | 42 | :int collection::addn(dword in, len) { |
7049 | leency | 43 | if (count >= 4000) return 0; |
44 | if (element_offset[count]+len+2 > data_size) { |
||
5965 | leency | 45 | increase_data_size(); |
7049 | leency | 46 | addn(in, len); |
47 | return 1; |
||
5965 | leency | 48 | } |
7049 | leency | 49 | strncpy(data_start+element_offset[count], in, len); |
5959 | leency | 50 | count++; |
7049 | leency | 51 | element_offset[count] = element_offset[count-1] + len + 1; |
5965 | leency | 52 | return 1; |
5959 | leency | 53 | } |
54 | |||
7286 | leency | 55 | :dword collection::get(dword pos) { |
5974 | leency | 56 | if (pos<0) || (pos>=count) return 0; |
5965 | leency | 57 | return data_start + element_offset[pos]; |
5959 | leency | 58 | } |
59 | |||
7738 | leency | 60 | :dword collection::get_pos_by_name(dword name) { |
61 | dword i; |
||
62 | for (i=0; i |
||
63 | if (strcmp(data_start + element_offset[i], name)==0) return i; |
||
64 | } |
||
65 | return -1; |
||
66 | } |
||
67 | |||
7286 | leency | 68 | :void collection::drop() { |
5965 | leency | 69 | if (data_start) free(data_start); |
7771 | leency | 70 | data_size = 0; |
71 | data_start = 0; |
||
72 | element_offset[count] = 0; |
||
73 | count = 0; |
||
5959 | leency | 74 | } |
75 | |||
6285 | leency | 76 | |
77 | /*======================================================== |
||
78 | = = |
||
79 | = Integer = |
||
80 | = = |
||
81 | ========================================================*/ |
||
82 | |||
7043 | leency | 83 | struct collection_int |
6285 | leency | 84 | { |
85 | int count; |
||
86 | dword element[4096*3]; |
||
87 | int add(); |
||
88 | dword get(); |
||
7286 | leency | 89 | dword get_last(); |
90 | void pop(); |
||
6285 | leency | 91 | void drop(); |
92 | }; |
||
93 | |||
7286 | leency | 94 | :int collection_int::add(dword in) { |
6285 | leency | 95 | if (count >= 4096*3) return 0; |
96 | element[count] = in; |
||
97 | count++; |
||
98 | return 1; |
||
99 | } |
||
100 | |||
7286 | leency | 101 | :dword collection_int::get(dword pos) { |
6285 | leency | 102 | if (pos<0) || (pos>=count) return 0; |
103 | return element[pos]; |
||
104 | } |
||
105 | |||
7286 | leency | 106 | :dword collection_int::get_last() { |
107 | return element[count]; |
||
108 | } |
||
109 | |||
110 | :void collection_int::pop() { |
||
111 | if (count>0) count--; |
||
112 | } |
||
113 | |||
114 | :void collection_int::drop() { |
||
6285 | leency | 115 | element[0] = |
116 | count = 0; |
||
117 | } |
||
118 | |||
5959 | leency | 119 | #endif0)>0)>4096)> |