-
Notifications
You must be signed in to change notification settings - Fork 2
/
CELLSCOL.PAS
404 lines (371 loc) · 10.2 KB
/
CELLSCOL.PAS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
{/////////////////////////////////////////////////////////////////////////
//
// Dos Navigator Version 1.51 Copyright (C) 1991-99 RIT Research Labs
//
// This programs is free for commercial and non-commercial use as long as
// the following conditions are aheared to.
//
// Copyright remains RIT Research Labs, and as such any Copyright notices
// in the code are not to be removed. If this package is used in a
// product, RIT Research Labs should be given attribution as the RIT Research
// Labs of the parts of the library used. This can be in the form of a textual
// message at program startup or in documentation (online or textual)
// provided with the package.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. All advertising materials mentioning features or use of this software
// must display the following acknowledgement:
// "Based on Dos Navigator by RIT Research Labs."
//
// THIS SOFTWARE IS PROVIDED BY RIT RESEARCH LABS "AS IS" AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The licence and distribution terms for any publically available
// version or derivative of this code cannot be changed. i.e. this code
// cannot simply be copied and put under another distribution licence
// (including the GNU Public Licence).
//
//////////////////////////////////////////////////////////////////////////}
unit CellsCol;
interface
uses Objects, ObjType;
type
PCellRec =^TCellRec;
TCellRec = record
Col: Byte;
Row: Integer;
Options: Word;
Decimals: Byte;
Value: Real;
S: String;
end;
PCellCollection = ^TCellCollection;
TCellCollection = object(TSortedCollection)
procedure FreeItem(Item: Pointer); virtual;
function NewItem(Col: Byte; Row: Integer; S: String): PCellRec;
function ReplaceItem(Col: Byte; Row: Integer; S: String): PCellRec;
function Get(Col: Byte; Row: Integer): PCellRec;
procedure SetValue(Col: Byte; Row: Integer; AValue: Real);
procedure DelItem(Col: Byte; Row: Integer);
procedure PutItem(var S: TStream; Item: Pointer); virtual;
function GetItem(var S: TStream): Pointer; virtual;
function MakeFormatString(AValue: Real): String;
function Compare(K1, K2: Pointer): Integer; virtual;
end;
const
RCellCollection: TStreamRec = (
ObjType: otCellCollection;
VmtLink: Ofs(TypeOf(TCellCollection)^);
Load: @TCellCollection.Load;
Store: @TCellCollection.Store
);
MaxCellY = 4096;
LenDataRec = Sizeof(Byte) + SizeOf(Integer) + SizeOf(Word) +
SizeOf(Byte) + sizeof(Real) + SizeOf(Char);
{ Options :
x x x x x x x
³ ³ ³ ³ ³ ³ À¿
³ ³ ³ ³ ³ ÀÄÄÁÄ Cell format (Text, Formula, Value)
³ ³ ³ ³ ÀÄÄÄÄ¿
³ ³ ³ ÀÄÄÄÄÄÄÁÄ Justify options (Right, Left, Center)
³ ³ ÀÄÄÄÄÄÄÄÄ¿
³ ÀÄÄÄÄÄÄÄÄÄÄ´
ÀÄÄÄÄÄÄÄÄÄÄÄÄÁÄ Display format(As is, Dec, Comma, Exp, Currency,
Logic, Don't display)
}
coText = $0000;
coFormula = $0001;
coValue = $0002;
coLeft = $0004;
coCenter = $0008;
coRight = $000C;
coDec = $0010;
coComma = $0020;
coExp = $0030;
coBool = $0040;
coCurrency = $0050;
coDONT = $0060;
CommaChar : Char = ',';
SeparatorChar: Char = '|';
procedure RegisterCellCollection;
function MakeComma(S: String): String;
function GetCellCoord(S: String; var X: Byte;var Y:Integer): Boolean;
function GetCellName(X, Y: Integer): String;
{ function StoI(S: String): LongInt;
function ItoS(L: LongInt): String;
function UpCaseStr(S: String): String;
function DelSpaces(S: String): String;}
implementation
uses Memory, Advance;
procedure TCellCollection.FreeItem;
begin
FreeMem(Item, LenDataRec + Length(PCellRec(Item)^.S));
end;
function TCellCollection.MakeFormatString;
var S: String;
begin
Str(AValue:20:2, S);
DelLeft(S);
MakeFormatString := S;
end;
function TCellCollection.NewItem;
var P: PCellRec;
begin
P := MemAlloc(LenDataRec + Length(S));
P^.Row := Row; P^.Col := Col;
P^.S := S;
P^.Options := 0;
P^.Decimals := 0;
P^.Value := 0;
Insert(P);
NewItem := P;
end;
procedure TCellCollection.DelItem;
var I: Integer;
P: PCellRec;
begin
for I := 1 to Count do
begin
P := At(I - 1);
if (P^.Row = Row) and (P^.Col = Col) then
begin
AtFree(I - 1); Exit;
end;
end;
end;
procedure TCellCollection.SetValue;
var I: Integer;
P: PCellRec;
begin
for I := 1 to Count do
begin
P := At(I - 1);
if (P^.Row = Row) and (P^.Col = Col) then
begin
P^.Value := AValue;
Exit;
end;
end;
end;
function TCellCollection.ReplaceItem;
var I, D, O: Integer;
P: PCellRec;
begin
for I := 1 to Count do
begin
P := At(I - 1);
if (P^.Row = Row) and (P^.Col = Col) then
begin
O := P^.Options; D := P^.Decimals;
FreeItem(P);
P := MemAlloc(LenDataRec + Length(S));
P^.Row := Row; P^.Col := Col;
P^.S := S; AtPut(I - 1, P);
P^.Options := O; P^.Decimals := D;
ReplaceItem := P;
Exit;
end;
end;
ReplaceItem := NewItem(Col, Row, S);
end;
procedure TCellCollection.PutItem;
var P: PCellRec;
begin
P := Item;
S.Write(P^, LenDataRec + Length(P^.S));
end;
function TCellCollection.GetItem;
var R: TCellRec;
P: PCellRec;
begin
S.Read(R, LenDataRec);
S.Read(R.S[1], Byte(R.S[0]));
P := MemAlloc(LenDataRec + Byte(R.S[0]));
Move(R, P^, LenDataRec + Byte(R.S[0]));
GetItem := P;
end;
function TCellCollection.Get;
var I: Integer;
P: PCellRec;
begin
Get := nil;
for I := 1 to Count do
begin
P := At(I - 1);
if (P^.Row = Row) and (P^.Col = Col) then
begin Get := P; Exit end;
end;
end;
function TCellCollection.Compare;
var Key1, Key2: PCellRec;
begin
Compare := 0;
Key1 := K1; Key2 := K2;
if Key1 = Key2 then Compare := 0 else
if Key1 = nil then Compare := -1
else if Key2 = nil then Compare := 1
else if Key1^.Row < Key2^.Row then Compare := -1
else if Key1^.Row > Key2^.Row then Compare := 1
else if Key1^.Col < Key2^.Col then Compare := -1
else if Key1^.Col > Key2^.Col then Compare := 1;
end;
{-----------------------------------------------------------------------}
function GetCellCoord(S: String; var X: Byte;var Y:Integer): Boolean;
var S1: String[5];
I: Integer;
begin
UpStr(S);
S1:=''; I := 1;
if S[I] = '@' then Inc(I);
While (I <= Length(S)) and (S[I]>='A') and (S[I]<='Z') do Inc(I);
S1 := Copy(S, I+Byte(S[I]='@'), 100); S[0] := Char(I-1);
Val(S1, Y, I); Dec(Y);
GetCellCoord := not ((S[0]>#2) or (S='') or (S[0]=#2) and
(S[1]>'IV') or (Y<0) or (Y>MaxCellY));
if S[0] = #1 then X := Byte(S[1]) - 65 else X := (Byte(S[1]) - 64) * 26 + Byte(S[2]) - 65;
end;
function GetCellName(X, Y: Integer): String;
const Dig : String [26] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var S: String[10];
begin
Str(Y + 1, S);
if X < 26 then S := Dig[X + 1] + S
else begin S := Dig[X div 26] + Dig[X mod 26 + 1] + S end;
GetCellName := S;
end;
function MakeComma(S: String): String;
var S1: String;
I: Integer;
label Quit;
begin
S1 := S;
asm
lea di, S
mov cl, ss:[di]
xor dx, dx
jcxz Quit
mov al, '.'
@@1:
inc dl
inc di
cmp al, ss:[di]
jz @@2
dec cl
jnz @@1
inc dl
@@2:
mov al, dl
cmp al, 5
jc Quit
dec al
jz Quit
mov bl, 3
xor ah, ah
div bl
mov cl, al
or ah, ah
jnz @@6
dec cl
@@6:
xor ch, ch
mov dl, al
lea di, S
lea si, S1
add cl, ss:[di]
mov ss:[si], cl
mov bl, ah
or bl, bl
jnz @@3
mov bl, 3
dec dl
@@3:
inc si
or bl, bl
jnz @@4
mov al, CommaChar
mov ss:[si], al
mov bl, 3
dec dl
jnz @@5
mov bl, 255
jmp @@5
@@4:
inc di
mov al, ss:[di]
dec bl
@@5:
mov ss:[si], al
loop @@3
end;
Quit: MakeComma := S1;
end;
function ItoS(L: LongInt): String;
var S: String;
begin
Str(L, S);
ItoS := S;
end;
function StoI(S: String): LongInt;
var I: LongInt;
J: Integer;
begin
Val(S, I, J);
StoI := I;
end;
procedure RegisterCellCollection;
begin
RegisterType(RCellCollection);
end;
(*
const UpcaseChars : String =
' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`'+
'ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~'+
'
'+
'ABEKMHO°±²³´µ¶·¸¹º»¼½¾'+
'¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞß'+
'PCTXðñòóôõö÷øùúûüýþ'#255;
function DelSpaces(S: String): String;
begin
asm
lea bx, S
mov di, bx
mov si, bx
xor ch, ch
mov cl, ss:[bx]
or cl, cl
jz @Exit
@@1:
inc si
mov al, ss:[si]
cmp al, ' '
jz @@2
inc di
mov ss:[di], al
jmp @@3
@@2:
dec byte ptr ss:[bx]
@@3:
loop @@1
@Exit:
end;
DelSpaces := S;
end;
*)
end.