Subversion Repositories Kolibri OS

Rev

Rev 7972 | Rev 8330 | 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 ]\n"
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();
19
	void set();
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
 
50
:void collection_int::set(dword pos, _in) {
51
	while (pos >= count) add(0);
52
	EAX = pos * sizeof(dword) + buf;
53
	ESDWORD[EAX] = _in;
54
}
55
 
56
:void collection_int::swap(dword pos1, pos2) {
57
	while (pos1 >= count) add(0);
58
	while (pos2 >= count) add(0);
59
	EAX = pos1 * sizeof(dword) + buf;
60
	EBX = pos2 * sizeof(dword) + buf;
61
	ESDWORD[EAX] >< ESDWORD[EBX];
62
}
63
 
64
:dword collection_int::len(dword pos) {
65
	if (pos<0) || (pos+1>=count) return 0;
66
	return get(pos+1) - get(pos);
67
}
68
 
69
:dword collection_int::get_last() {
70
	return get(count-1);
71
}
72
 
73
:void collection_int::pop() {
74
	if (count>0) count--;
75
}
76
 
77
:void collection_int::drop() {
78
	count = 0;
79
}
80
 
81
/*========================================================
82
=                                                        =
6285 leency 83
=                       String                           =
84
=                                                        =
85
========================================================*/
86
 
5959 leency 87
struct collection
88
{
5965 leency 89
	int realloc_size, count;
90
	dword data_start;
5959 leency 91
	dword data_size;
7972 leency 92
	collection_int offset;
5965 leency 93
	int add();
7049 leency 94
	int addn();
7738 leency 95
	dword get(); //get_name_by_pos
96
	dword get_pos_by_name();
5959 leency 97
	void drop();
5965 leency 98
	void increase_data_size();
7878 leency 99
	dword get_last();
7885 leency 100
	bool pop();
5959 leency 101
};
102
 
7286 leency 103
:void collection::increase_data_size() {
5965 leency 104
	int filled_size;
105
	if (realloc_size<4096) realloc_size = 4096;
106
	if (!data_size) {
107
		data_size = realloc_size;
108
		data_start = malloc(realloc_size);
109
	}
110
	else {
111
		data_size = data_size + realloc_size;
112
		data_start = realloc(data_start, data_size);
113
	}
5959 leency 114
}
115
 
7286 leency 116
:int collection::add(dword in) {
7049 leency 117
	return addn(in, strlen(in));
118
}
119
 
7286 leency 120
:int collection::addn(dword in, len) {
7972 leency 121
	if (offset.get(count)+len+2 > data_size) {
5965 leency 122
		increase_data_size();
7049 leency 123
		addn(in, len);
124
		return 1;
5965 leency 125
	}
7972 leency 126
	strncpy(data_start+offset.get(count), in, len);
5959 leency 127
	count++;
7972 leency 128
	offset.set(count, offset.get(count-1) + len + 1);
5965 leency 129
	return 1;
5959 leency 130
}
131
 
7286 leency 132
:dword collection::get(dword pos) {
5974 leency 133
	if (pos<0) || (pos>=count) return 0;
7972 leency 134
	return data_start + offset.get(pos);
5959 leency 135
}
136
 
7878 leency 137
:dword collection::get_last() {
138
	return get(count-1);
139
}
140
 
7738 leency 141
:dword collection::get_pos_by_name(dword name) {
142
	dword i;
143
	for (i=0; i
7972 leency 144
		if (strcmp(data_start + offset.get(i), name)==0) return i;
7738 leency 145
	}
146
	return -1;
147
}
148
 
7286 leency 149
:void collection::drop() {
5965 leency 150
	if (data_start) free(data_start);
7771 leency 151
	data_size = 0;
152
	data_start = 0;
7972 leency 153
	offset.drop();
7771 leency 154
	count = 0;
5959 leency 155
}
156
 
7885 leency 157
:bool collection::pop() {
158
	if (count>0) count--;
7878 leency 159
}
6285 leency 160
 
5959 leency 161
#endif