1 /******************************************************************************
2 *
3 * Copyright (C) 2006, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * ID: collection 18.10.06 0.0.A.
11 *
12 * Author: Alexey Krivonogov ( gentee )
13 *
14 ******************************************************************************/
15
16 #include "collection.h"
17 #include "../bytecode/cmdlist.h"
18 #include "../vm/vmtype.h"
19 //! temporary
20 /*#include "../os/user/defines.h"
21 #include "../genteeapi/gentee.h"
22 */
23
24 //--------------------------------------------------------------------------
25
26 puint STDCALL collect_add( pcollect pclt, puint input, uint type )
27 {
28 // uint value;
29
30 pclt->count++;
31 buf_appenduint( ( pbuf )pclt, type );
32 if ( (( povmtype )PCMD( type ))->stsize == 2 )
33 {
34 pclt->flag |= 0x01;
35 buf_append( ( pbuf )pclt, ( pubyte )input, sizeof( long64 ));
36 return input + 2;
37 }
38 else
39 {
40 /* value = *input;
41
42 switch ( type )
43 {
44 case TUbyte: value = *( pubyte )input; break;
45 case TByte: value = *( pbyte )input; break;
46 case TUshort: value = *( pushort )input; break;
47 case TShort: value = *( pshort )input; break;
48 default: value = *input; break;
49 }*/
50 buf_appenduint( ( pbuf )pclt, *input );
51 }
52 return input + 1;
53 }
54
55 pubyte STDCALL collect_addptr( pcollect pclt, pubyte ptr )
56 {
57 pstr ps = str_new( ptr );
58
59 collect_add( pclt, ( puint )&ps, TStr );
60 return ptr + mem_len( ptr ) + 1;
61 }
62
63 /*-----------------------------------------------------------------------------
64 * Id: collection_opind F4
65 *
66 * Summary: Gets a value of a collection element. Don't use if the collection
67 contains double, ulong or long types.
68 *
69 * Title: collection[ i ]
70 *
71 * Return: A value of the collection element.
72 *
73 * Define: method uint collection.index( uint ind )
74 *
75 -----------------------------------------------------------------------------*/
76
77 /*-----------------------------------------------------------------------------
78 * Id: collection_ptr F2
79 *
80 * Summary: Gets a pointer to a collection element.
81 *
82 * Params: ind - Element index starts at zero.
83 *
84 * Return: A pointer to a collection element, or zero on error.
85 *
86 * Define: method uint collection.ptr( uint ind )
87 *
88 -----------------------------------------------------------------------------*/
89
90 pubyte STDCALL collect_index( pcollect pclt, uint index )
91 {
92 uint i;
93 uint off = 0;
94
95 if ( index >= pclt->count )
96 return 0;
97
98 if ( pclt->flag & 0x01 )
99 {
100 for( i = 0; i < index; i++ )
101 {
102 // print("Type=%i\n", *( puint )(
103 // buf_ptr( ( pbuf )pclt ) + pclt->pfor ) );
104 off += sizeof( uint ) + ((( povmtype )PCMD( *( puint )(
105 buf_ptr( ( pbuf )pclt ) + off ) ))->stsize << 2 );
106 }
107 }
108 else
109 off = ( index << 3 );
110 // print("Index = %i t=%i pfor=%i off = %i\n", index,
111 // *( puint )buf_ptr( ( pbuf )pclt ), pclt->pfor, pclt->pfor + sizeof( uint ) );
112 return buf_ptr( ( pbuf )pclt ) + off + sizeof( uint );
113 }
114
115 /*-----------------------------------------------------------------------------
116 * Id: collection_gettype F2
117 *
118 * Summary: Gets an element type of a collection.
119 *
120 * Params: ind - Element index starts at zero.
121 *
122 * Return: An element type of a collection or zero on error.
123 *
124 * Define: method uint collection.gettype( uint ind )
125 *
126 -----------------------------------------------------------------------------*/
127
128 uint STDCALL collect_gettype( pcollect pclt, uint index )
129 {
130 if ( index < pclt->count )
131 return *(( puint )collect_index( pclt, index ) - 1 );
132 return 0;
133 }
134
135 pubyte STDCALL collect_copy( pcollect pclt, pubyte data )
136 {
137 uint itype;
138 pvoid object;
139 uint count = *( puint )data;
140
141 data += sizeof( uint );
142 // pclt->owner = 1;
143 while ( count-- )
144 {
145 itype = *data++;
146
147 switch ( itype )
148 {
149 case TBuf:
150 case TStr:
151 object = type_new( itype, data );
152 // if ( itype == TStr )
153 // print("Str=%s type = %i\n", str_ptr( ( pstr )object ), itype );
154 collect_add( pclt, ( puint )&object, itype );
155 if ( itype == TStr )
156 data += mem_len( data ) + 1;
157 else
158 data += *( puint )data + sizeof( uint );
159 break;
160 case TCollection:
161 object = type_new( itype, NULL );
162 data = collect_copy( ( pcollect )object, data );
163 collect_add( pclt, ( puint )&object, itype );
164 break;
165 default:
166 data = ( pubyte )collect_add( pclt, ( puint )data, itype );
167 break;
168 }
169 }
170
171 return data;
172 }
173
174 /*-----------------------------------------------------------------------------
175 ** Id: collection_oplen F4
176 *
177 * Summary: Gets the amount of elements in the collection.
178 *
179 * Return: The count of the collection items.
180 *
181 * Define: operator uint *( collection left )
182 *
183 -----------------------------------------------------------------------------*/
184
185 uint STDCALL collect_count( pcollect pclt )
186 {
187 return pclt->count;
188 }
189
190 void STDCALL collect_delete( pcollect pclt )
191 {
192
193 }
194
195 // !!! сделать collect_delete() с учетом поля owner. Добавить поле.
196
197 //--------------------------------------------------------------------------
198 /*
199 void STDCALL collection_clear( par pa )
200 {
201 // buf_clear( &pa->data );
202 }
203 */
204 //--------------------------------------------------------------------------
205
Редактировать