Subversion Repositories Kolibri OS

Rev

Rev 7886 | Rev 7972 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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