Subversion Repositories Kolibri OS

Rev

Rev 7886 | Rev 7972 | 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
=                                                        =
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();
7878 leency 23
	dword get_last();
7885 leency 24
	bool pop();
5959 leency 25
};
26
 
7286 leency 27
:void collection::increase_data_size() {
5965 leency 28
	int filled_size;
29
	if (realloc_size<4096) realloc_size = 4096;
30
	if (!data_size) {
31
		data_size = realloc_size;
32
		data_start = malloc(realloc_size);
33
	}
34
	else {
35
		data_size = data_size + realloc_size;
36
		data_start = realloc(data_start, data_size);
37
	}
5959 leency 38
}
39
 
7286 leency 40
:int collection::add(dword in) {
7049 leency 41
	return addn(in, strlen(in));
42
}
43
 
7286 leency 44
:int collection::addn(dword in, len) {
7878 leency 45
	if (count >= 4000) {
46
		debugln("collection: more than 4000 elements!");
47
		return 0;
48
	}
7049 leency 49
	if (element_offset[count]+len+2 > data_size) {
5965 leency 50
		increase_data_size();
7049 leency 51
		addn(in, len);
52
		return 1;
5965 leency 53
	}
7049 leency 54
	strncpy(data_start+element_offset[count], in, len);
5959 leency 55
	count++;
7049 leency 56
	element_offset[count] = element_offset[count-1] + len + 1;
5965 leency 57
	return 1;
5959 leency 58
}
59
 
7286 leency 60
:dword collection::get(dword pos) {
5974 leency 61
	if (pos<0) || (pos>=count) return 0;
5965 leency 62
	return data_start + element_offset[pos];
5959 leency 63
}
64
 
7878 leency 65
:dword collection::get_last() {
66
	return get(count-1);
67
}
68
 
7738 leency 69
:dword collection::get_pos_by_name(dword name) {
70
	dword i;
71
	for (i=0; i
72
		if (strcmp(data_start + element_offset[i], name)==0) return i;
73
	}
74
	return -1;
75
}
76
 
7286 leency 77
:void collection::drop() {
5965 leency 78
	if (data_start) free(data_start);
7771 leency 79
	data_size = 0;
80
	data_start = 0;
81
	element_offset[count] = 0;
82
	count = 0;
5959 leency 83
}
84
 
7885 leency 85
:bool collection::pop() {
86
	if (count>0) count--;
7878 leency 87
}
6285 leency 88
 
89
/*========================================================
90
=                                                        =
91
=                       Integer                          =
92
=                                                        =
93
========================================================*/
94
 
7043 leency 95
struct collection_int
6285 leency 96
{
7885 leency 97
	dword buf;
98
	dword buf_size;
99
	unsigned count;
100
	void alloc();
101
	void add();
6285 leency 102
	dword get();
7930 leency 103
	dword len();
7286 leency 104
	dword get_last();
105
	void pop();
6285 leency 106
	void drop();
107
};
108
 
7885 leency 109
:void collection_int::alloc() {
110
	if (!buf) {
111
		buf_size = 4096;
112
		buf = malloc(4096);
113
	} else {
114
		buf_size += 4096;
115
		buf = realloc(buf, buf_size);
116
	}
117
}
118
 
119
:void collection_int::add(dword _in) {
120
	if (!buf) || (count * sizeof(dword) >= buf_size) alloc();
121
	EAX = count * sizeof(dword) + buf;
122
	ESDWORD[EAX] = _in;
6285 leency 123
	count++;
124
}
125
 
7286 leency 126
:dword collection_int::get(dword pos) {
6285 leency 127
	if (pos<0) || (pos>=count) return 0;
7885 leency 128
	return ESDWORD[pos * sizeof(dword) + buf];
6285 leency 129
}
130
 
7930 leency 131
:dword collection_int::len(dword pos) {
132
	if (pos<0) || (pos+1>=count) return 0;
133
	return get(pos+1) - get(pos);
134
}
135
 
7286 leency 136
:dword collection_int::get_last() {
7885 leency 137
	return get(count-1);
7286 leency 138
}
139
 
140
:void collection_int::pop() {
141
	if (count>0) count--;
142
}
143
 
144
:void collection_int::drop() {
7886 leency 145
	count = 0;
6285 leency 146
}
147
 
5959 leency 148
#endif