Subversion Repositories Kolibri OS

Rev

Rev 8330 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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