Develop and Download Open Source Software

Browse Subversion Repository

Contents of /baslib.pas

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations) (download) (as text)
Mon Nov 7 12:03:00 2011 UTC (12 years, 4 months ago) by shiraishikazuo
File MIME type: text/x-pascal
File size: 58093 byte(s)


1 unit baslib;
2 {$IFDEF FPC}
3 {$MODE DELPHI}{$H+}
4 {$ENDIF}
5 {$INLINE ON}
6 (***************************************)
7 (* Copyright (C) 2009, SHIRAISHI Kazuo *)
8 (***************************************)
9
10
11 interface
12 uses
13 Classes, SysUtils,
14 base,base2,textfile,arrays, mathc;
15
16 function FloatVal( const s:ansistring):double;
17
18 type
19
20 TDeviceRef=class;
21 PtTextDevice = ^TTextDevice;
22
23 TChannelList = class(TStringList)
24 constructor Create;
25 procedure SetRefference(chn:integer; ch:TDeviceRef); // 経路引数をセットする
26 function channel(chn:integer):TTextDevice; overload;
27 function channel(chn0:double):TTextDevice; overload;
28 procedure open(chn0:double; const FName:FNameStr;
29 const amode,rectp,orgtp:string;
30 len:integer; insideofwhen:boolean); overload;
31 procedure OpenPrinter(chn0:double; insideofwhen:boolean); overload;
32 procedure close(chn0:double); overload;
33 function channel(chn0:Complex):TTextDevice; overload;
34 procedure open(chn0:complex; const FName:FNameStr;
35 const amode,rectp,orgtp:string;
36 len:integer; insideofwhen:boolean); overload;
37 procedure OpenPrinter(chn:complex; insideofwhen:boolean); overload;
38 procedure close(chn0:complex); overload;
39 public
40 destructor destroy; override; overload;
41 private
42 procedure assign(chn:integer; dev:TTextDevice; insideofwhen:boolean);
43 //function pchannel(chn:integer):PtTextDevice; overload;
44 //function pchannel(chn:double) :PtTextDevice; overload;
45 //function pchannel(chn:Complex):PtTextDevice; overload;
46 end;
47 // プログラム単位はTChannelList形の変数ChannelListを持つ。
48 // (経路番号式を参照する文を持つときに限る。)
49 // プログラム単位はtry~finallyでchannelListを解放する。
50
51
52 TDeviceRef=class //副プログラムの経路引数として用いる
53 owner:TChannelList;
54 index:integer;
55 constructor create(CL1:TChannelList; ix1:integer);
56 end;
57
58
59
60 type
61 TStdIO = class(TTextDevice)
62
63 end; // 0番の経路として使う予定。未実装。
64
65 PROCEDURE PRINT(args:array of const);
66
67
68
69
70 {*****************}
71 {Numeric functions}
72 {*****************}
73 const
74 DefaultExcode=$03EA0BBD; // 1002 3005
75 var
76 excode:LongWord=DefaultExcode;
77
78 function SGN(const x:double):double;inline; overload;
79 function TAN(const x:extended):extended;inline; overload;
80 function COT(const x:extended):extended;inline; overload;
81 function SEC(const x:extended):extended;inline; overload;
82 function CSC(const x:extended):extended;inline; overload;
83 function ATN(const x:extended):extended;inline; overload;
84 function ACOS(const x:extended):extended;inline; overload;
85 function ASIN(const x:extended):extended;inline; overload;
86 function TANDEG(const x:extended):extended; overload;
87 function COTDEG(const x:extended):extended; overload;
88 function SECDEG(const x:extended):extended;inline;overload;
89 function CSCDEG(const x:extended):extended;inline;overload;
90 function ATNDEG(const x:extended):extended;inline;overload;
91 function ACOSDEG(const x:extended):extended;inline;overload;
92 function ASINDEG(const x:extended):extended;inline;overload;
93 function ANGLE(const x,y:extended):extended;inline;overload;
94 function ANGLEDEG(const x,y:extended):extended;inline;overload;
95 function COSH(const x:extended):extended;inline; overload;
96 function SINH(const x:extended):extended;inline; overload;
97 function SQRT(const x:extended):extended;inline; overload;
98 function EXP(const x:extended):extended; inline; overload;
99 function LOG(const x:extended):extended;inline; overload;
100 function LOG2(const x:extended):extended;inline; overload;
101 function LOG10(const x:extended):extended;inline; overload;
102 function Power(const x,y:extended):extended;inline; overload;
103 function EPS(x:double):double;inline; overload;
104 function BINT( x:double):double;inline; overload;
105 function BCEIL(x:double):double;inline; overload;
106 function BMOD( x,y:double):double;inline; overload;
107 function REMAINDER( x,y:double):double;inline; overload;
108 function ROUND( x:double):double; overload;inline;overload;
109 function ROUND( x,y:double):double; overload; overload;
110 function TRUNCATE( x,y:double):double;inline; overload;
111 function BMIN(const x,y:double):double;inline; overload;
112 function BMAX(const x,y:double):double;inline; overload;
113
114
115 function PERM( n,r:double):double; overload;
116 function COMB( n,r:double):double; overload;
117 function FACT(const x:double):double; overload;
118
119 function BitNOT(a:Int64):Int64; overload;
120 function BitNOT(a:double):Int64; overload;
121 function BitAND(a,b:int64):int64; overload;
122 function BitAND(a,b:double):int64; overload;
123 function BitAND(a:int64;b:double):int64; overload;
124 function BitAND(a:double;b:int64):int64; overload;
125 function BitOR(a,b:int64):int64; overload;
126 function BitOR(a,b:double):int64; overload;
127 function BitOR(a:int64;b:double):int64; overload;
128 function BitOR(a:double;b:int64):int64; overload;
129 function BitXOR(a,b:int64):int64; overload;
130 function BitXOR(a,b:double):int64; overload;
131 function BitXOR(a:int64;b:double):int64; overload;
132 function BitXOR(a:double;b:int64):int64; overload;
133
134
135 function EXTYPE(const E:Exception; ExcodeRec:LongWord):Integer;
136 function EXMess(const E:Exception; ExcodeRec:LongWord):string;
137 function PropagatedExtype(t:integer):integer;
138
139 {****************}
140 {STRING Functions}
141 {****************}
142 function SubString(s:string; i1,i2:integer):string;overload;
143 function SubString(s:string; i1,i2:double):string;overload;
144 function SubStringByte(s:string; i1,i2:integer):string;overload;
145 function SubStringByte(s:string; i1,i2:double):string;overload;
146 function basicORD(s:AnsiString; CharacterByte:boolean):integer;
147 function pos2(const a,b:ansistring):integer;
148 function pos3(const a,b:ansistring; m0:double):integer; overload;
149 function UTF8Pos2(const a,b:ansistring):integer;
150 function UTF8Pos3(const a,b:ansistring; m0:double):integer;
151 function BVAL2(const s:string):int64;
152 function BVAL16(const s:string):int64;
153 function VAL(const s:string):double;
154 function STR_s(x:double):string; overload;
155 function CHR_s(x:double):string; overload;
156 function CHRbyte(x:double):string; overload;
157 function USING_s(const s:string; x:double):string; overload;
158 function USING_ss(const s:string; x:double):string; overload;
159 function REPEAT_s(const s:string; x:double):string; overload;
160 function LTRIM_s(s:string):string;
161 function RTRIM_s(s:string):string;
162 function BSTR_s(x:double; n:integer):string; overload;
163 function DATE_s:ansistring;
164 function TIME_s:ansistring;
165
166 function Mid_s(s:string; i1,i2:integer):string; overload;
167 function Mid_s(s:string; i1,i2:double):string; overload;
168 function Mid_sByte(s:string; i1,i2:integer):string; overload;
169 function Mid_sByte(s:string; i1,i2:double):string; overload;
170 function Left_sByte(s:string;i:integer):string; overload;
171 function Left_s(s:string;i:integer):string; overload;
172 function Left_sByte(s:string; i:double):string; overload;
173 function Left_s(s:string;i:double):string; overload;
174 function Right_sByte(s:string;i:integer):string; overload;
175 function Right_s(s:string;i:integer):string; overload;
176 function Right_sByte(s:string; i:double):string; overload;
177 function Right_s(s:string;i:double):string; overload;
178
179 function confirm_s(s:string):string;
180
181 {String Variable}
182 procedure SubstSubstringByte(var v:AnsiString; i,j:integer; const s:ansistring);overload;
183 procedure SubstSubstringByte(var v:AnsiString; i,j:double; const s:ansistring); overload;
184 procedure SubstSubstring(var v:AnsiString; i,j:integer; const s:ansistring); overload;
185 procedure SubstSubstring(var v:AnsiString; i,j:double; const s:ansistring); overload;
186 type
187 TStrVar=class
188 PVar:PString;
189 constructor create (P:PString);
190 procedure setstring(const s:string);virtual;
191 function getstring:string;virtual;
192 property str:string read getstring write setstring;
193 end;
194
195 TStrVar2=class(TStrVar)
196 left,right:integer;
197 constructor create (P:PString; l,r:integer); overload;
198 constructor create (P:PString; l,r:double); overload;
199 constructor create (P:PString; l,r:complex); overload;
200 constructor create (P:PString; l:complex;r:double); overload;
201 constructor create (P:PString; l:double; r:complex); overload;
202 procedure setstring(const s:string);override;
203 function getstring:string;override;
204 end;
205
206 TStrVarByte=class(TStrVar2)
207 procedure setstring(const s:string);override;
208 function getstring:string;override;
209 end;
210
211
212 {Let Statements}
213 procedure LET(const p:Array of PDouble; x:double);
214 procedure LETS(const p:Array of TStrVar; const s:string);
215
216 { Ask Statement}
217 procedure AskFile(ch:TTextDEvice;
218 expAccess,expDatum,expErasable,expFileType,expName,
219 expOrganization,expPointer,expRecsize1,expRecType,
220 expSetter,expCharin,expTypeahead,expEchoControl,expEcho:TStrVar;
221 expMargin,expRecSize2,expZonewidth,expCharacterPending,expFilesize:Pdouble);
222
223 {GOSUB~RETURN}
224 type
225 TGosubStack=class
226 public
227 constructor create(ssize:integer);
228 destructor destroy;override;
229 procedure push(i:integer);
230 function pop:integer;
231 private
232 p:integer;
233 a:PIntArray;
234 size:integer;
235 end;
236 var
237 GosubStack:TGosubStack;
238
239
240 {Misc.}
241 procedure BreakPr(s:Ansistring);
242 procedure Wait(n:extended); overload;
243 procedure ShowMess(const s:string);
244 procedure FileDelete(const s:string);
245 procedure FileGetName(const s:string; svar:TStrVar; aux:integer);
246 procedure FileSplitname(const s:string; svar1,svar2,svar3:TStrVar);
247 function Files(const s:string):integer;
248 procedure FileReName(const s1,s2:string);
249 procedure FileList(const s:string; a:TArray1S);
250 procedure SetDirectory(const s:string);
251 procedure AskDirectory(svar:TStrVar);
252 procedure swap(var x,y:double);overload;
253 procedure swap(var x,y:integer);overload;
254 procedure swap(var x,y:string);overload;
255 function GetKeyState(const x:double):integer;
256
257 {Chain,Execute}
258 function ShellExec(const s1:string; args:array of const; opWaitFor:boolean):boolean;
259
260 {PackDbl$, UnPackDbl}
261 function PackDbl_s(d:Double):AnsiString;
262 function UnPackDbl(const s:string):double;
263 function DWord_s(x:DWord):AnsiString; overload;
264 function Word_s(x:DWord):AnsiString; overload;
265 function Byte_s(x:Dword):ansistring; overload;
266 function DWord_s(x:double):AnsiString;overload;
267 function Word_s(x:double):AnsiString; overload;
268 function Byte_s(x:double):ansistring; overload;
269
270 {Initialize}
271
272 procedure init(TextMode,GraphMode,UseCharInput:boolean; BMPSize1:TypBMPsize; VirtualStackSize:Cardinal);
273 procedure finish;
274
275 implementation
276 uses Forms,Dialogs,Controls,math,Process,FileUtil,LCLProc, lclintf,
277 vstack,arithmet,float,math2sub,format,charinp,basinit,sconsts,
278 paintfrm,graphsys,textfrm;
279
280 procedure SubstSubstring(var v:AnsiString; i,j:integer; const s:ansistring);
281 var
282 k,p:integer;
283 begin
284 if i<=0 then i:=1;
285 k:=UTF8length(v);
286 if j>k then j:=k;
287 UTF8delete(v,i,j-i+1);
288 UTF8insert(s,v,i);
289 end;
290
291 procedure SubstSubstring(var v:AnsiString; i,j:double; const s:ansistring);
292 begin
293 SubstSubstring(v, LongIntRound(i),LongIntRound(j),s)
294 end;
295
296
297
298 procedure SubstSubstringByte(var v:AnsiString; i,j:integer; const s:ansistring);
299 var
300 p:integer;
301 begin
302 if i<=0 then i:=1;
303 if j>length(v) then j:=length(v);
304 if j-i+1 =length(s) then
305 for p:=i to j do
306 v[p]:=s[p-i+1]
307 else
308 begin
309 delete(v,i,j-i+1);
310 insert(s,v,i);
311 end;
312 end;
313
314 procedure SubstSubstringByte(var v:AnsiString; i,j:double; const s:ansistring);
315 begin
316 SubstSubstringByte(v, LongIntRound(i),LongIntRound(j),s)
317 end;
318
319 function FloatVal( const s:ansistring):double;
320 var
321 c:integer;
322 begin
323 try
324 System.Val(s,result,c); {!!!!!!!! 要修正?? !!!!!!!}
325 if c<>0 then
326 if s='' then
327 result:=0
328 else
329 setexception(8101);
330 except
331 on EMathError do setexception(1006)
332 end;
333 end;
334
335 {*************}
336 { ChannelList }
337 {*************}
338
339 constructor TChannelList.create;
340 var
341 i:integer;
342 begin
343 inherited create;
344 Capacity:=100;
345 for i:=0 to Capacity-1 do
346 add('');
347 objects[0]:=console;
348 end;
349
350 procedure TChannelList.Setrefference(chn:integer; ch:TDeviceRef); // 経路引数をセットする
351 begin
352 if (chn>0) and (chn<Capacity) and (objects[chn]=nil) then
353 begin
354 Objects[chn]:=ch;
355 end
356 else
357 setexception(7001)
358 end;
359
360 function TChannelList.channel(chn:integer):TTextDevice;
361 begin
362 if (chn>=0) and (chn<Capacity) and (objects[chn]<>nil) then
363 if objects[chn] is TDeviceRef then
364 with TDeviceRef(objects[chn]) do
365 result:=owner.channel(index)
366 else {if objects[chn] is TTextDevice then}
367 result:=TTextDevice(objects[chn])
368 else
369 setexception(7004)
370 end;
371
372 function TChannelList.channel(chn0:double):TTextDevice;
373 begin
374 result:=channel(LongintRound(chn0))
375 end;
376
377 function DecideAccessMode(const s:ansistring; var am:AccessMode):boolean;
378 begin
379 result:=true;
380 if s=AccessModeLiteral[amOUTIN] then
381 am:=amOutin
382 else if s=AccessModeLiteral[amINPUT] then
383 am:=amInput
384 else if s=AccessModeLiteral[amOUTPUT] then
385 am:=amOUTput
386 else
387 result:=false
388 end;
389
390 function DecideRecordType(const s:ansistring; var rc:RecordType):boolean;
391 begin
392 result:=true;
393 if s=RecordTypeLiteral[rcDisplay] then
394 rc:=rcDisplay
395 else if s=RecordTypeLiteral[rcInternal] then
396 rc:=rcInternal
397 else if s=RecordTypeLiteral[rcCSV] then
398 rc:=rcCSV
399 else
400 result:=false
401 end;
402
403 function DecideOrgType(const s:ansistring; var og:OrganizationType):boolean;
404 begin
405 result:=true;
406 if s=OrganizationTypeLiteral[orgSEQ] then
407 og:=orgSEQ
408 else if s=OrganizationTypeLiteral[orgSTREAM] then
409 og:=orgSTREAM
410 else
411 result:=false
412 end;
413
414
415 procedure TChannelList.open(chn0:double; const FName:FNameStr;const amode,rectp,orgtp:string;
416 len:integer; insideofwhen:boolean);
417 var
418 ttext:TTextDevice;
419 chn:integer;
420 am:AccessMode;
421 rc:RecordType;
422 og:OrganizationType;
423 begin
424 chn:=LongIntRound(chn0);
425
426 if chn=0 then
427 if insideofwhen then setexception(7002) else exit;
428
429 if (chn<0) or (chn>=Capacity) then
430 setexception(7001);
431
432
433 if DecideAccessMode(amode,am)
434 and DecideRecordType(rectp,rc)
435 and DecideOrgType(orgtp,og)
436 {and DecideRecSize(rsize.evalS)} then
437 begin
438 if rc=rcDisplay then
439 {$IFDEF Windows}
440 if isCommPortName(FName) then
441 ttext:=TCommFile.create
442 else
443 {$ENDIF}
444 ttext:=TTextFile.create
445 else if rc=rcInternal then
446 ttext:=TInternalFile.create
447 else
448 ttext:=TCSVfile.create;
449
450 ttext.open(Fname,am,og,len);
451 if (chn<count) and (objects[chn] is TDeviceRef) then
452 with TDeviceRef(objects[chn]) do owner.assign(index,ttext,insideofwhen)
453 else
454 assign(chn,ttext,insideofwhen);
455 end
456 else
457 setexception(7001);
458 end;
459
460 procedure TChannelList.assign(chn:integer; dev:TTextDevice; insideofwhen:boolean);
461 begin
462 if (chn=0) then
463 begin
464 dev.free;
465 if insideofwhen then setexception(7002) else exit;
466 end;
467
468 if (chn<count) and (objects[chn]=nil) then
469 Objects[chn]:=dev
470 else
471 setexception(7003)
472 end;
473
474
475 procedure TChannelList.OpenPrinter(chn0:double; insideofwhen:boolean);
476 var
477 ttext:TTextDevice;
478 chn:integer;
479 begin
480 chn:=LongIntRound(chn0);
481 if chn=0 then
482 if insideofwhen then setexception(7002) else exit;
483
484 if (chn<0) or (chn>=Capacity) then
485 setexception(7001);
486
487 ttext:=TLocalPrinter.create;
488 ttext.open('',amOUTPUT,orgSeq,1024{dummy});
489 if (chn<count) and (objects[chn] is TDeviceRef) then
490 with TDeviceRef(objects[chn]) do owner.assign(index,ttext,insideofwhen)
491 else
492 assign(chn,ttext,insideofwhen);
493 end;
494
495 procedure TChannelList.close(chn0:double);
496 var
497 ttext:TTextDevice;
498 chn:integer;
499 begin
500 chn:=longintround(chn0);
501 ttext:=channel(chn);
502 if ttext<>nil then
503 begin
504 ttext.close;
505 ttext.free;
506 end
507 else
508 setexception(7004);
509 if chn<count then
510 if (objects[chn] is TDeviceRef) then
511 with TDeviceRef(objects[chn]) do owner.objects[index]:=nil
512 else
513 objects[chn]:=nil;
514 ttext:=nil;
515 end;
516
517 destructor TChannelList.destroy;
518 var
519 i:integer;
520 begin
521 for i:=1 to Count-1 do //0番(#0)を残す
522 Objects[i].free;
523 clear;
524 inherited destroy;
525 end;
526
527 constructor TDeviceRef.create(CL1:TChannelList; ix1:integer);
528 begin
529 inherited create;
530 owner:=CL1;
531 index:=ix1
532 end;
533
534 function TChannelList.channel(chn0:Complex):TTextDevice; overload;
535 begin
536 result:=channel(testreal(chn0))
537 end;
538
539 procedure TChannelList.open(chn0:complex; const FName:FNameStr;
540 const amode,rectp,orgtp:string;
541 len:integer; insideofwhen:boolean); overload;
542 begin
543 open(testreal(chn0),FName,amode,rectp,orgtp,len,insideofwhen)
544 end;
545
546 procedure TChannelList.OpenPrinter(chn:complex; insideofwhen:boolean); overload;
547 begin
548 OpenPrinter(testreal(chn),insideofwhen)
549 end;
550
551 procedure TChannelList.close(chn0:complex); overload;
552 begin
553 close(testreal(chn0))
554 end;
555
556 {
557 function TChannelList.pchannel(chn:integer):PtTextDevice;
558 begin
559 // ???
560 end;
561
562 function TChannelList.pchannel(chn:double):PtTextDevice;
563 begin
564 result:=pchannel(LongintRound(chn))
565 end;
566
567
568 function TChannelList.pchannel(chn:Complex):PtTextDevice; overload;
569 begin
570 result:=pchannel(testreal(chn));
571 end;
572 }
573
574 {*****************}
575 {Numeric functions}
576 {*****************}
577 function SGN(const x:double):double; inline;
578 begin
579 result:=sign(x)
580 end;
581
582 function TAN(const x:extended):extended;inline;
583 begin
584 excode:=$03EB03EB; // 1003 1003
585 result:=math.tan(x);
586 excode:=DefaultExcode
587 end;
588
589 function COT(const x:extended):extended; inline;
590 var
591 s,c:extended;
592 begin
593 excode:=$03EB03EB; // 1003 1003
594 //{$IFDEF Windows}
595 result:=math.cot(x);
596 //{$ELSE}
597 //{$MAXFPUREGISTERS 0}
598 //s:=system.sin(x);
599 //c:=system.cos(x);
600 //result:=c / s ;
601 //{$ENDIF}
602 excode:=DefaultExcode
603 end;
604
605 function SEC(const x:extended):extended;inline;
606 begin
607 excode:=$03EB03EB; // 1003 1003
608 result:=math.sec(x);
609 excode:=DefaultExcode
610 end;
611
612 function CSC(const x:extended):extended;inline;
613 begin
614 excode:=$03EB03EB; // 1003 1003
615 result:=math.csc(x);
616 excode:=DefaultExcode
617 end;
618
619 function ATN(const x:extended):extended; inline;
620 begin
621 result:=arctan(x)
622 end;
623
624 function ACOS(const x:extended):extended;inline;
625 begin
626 excode:=$03EB0BBF; // 1003 3007
627 result:=math2sub.acos(x);
628 excode:=DefaultExcode
629 end;
630
631 function ASIN(const x:extended):extended;inline;
632 begin
633 excode:=$03EB0BBF; // 1003 3007
634 result:=math2sub.asin(x);
635 excode:=DefaultExcode
636 end;
637
638 function TANDEG(const x:extended):extended;
639 var
640 y:extended;
641 begin
642 excode:=$03EB03EB; // 1003 1003
643 y:=math2sub.cosdeg(x);
644 if y=0 then
645 setexception(1003)
646 else
647 result:=math2sub.sindeg(x)/y;
648 excode:=DefaultExcode
649 end;
650
651 function COTDEG(const x:extended):extended;
652 var
653 y:extended;
654 begin
655 excode:=$03EB03EB; // 1003 1003
656 y:=math2sub.sindeg(x);
657 if y=0 then
658 setexception(1003)
659 else
660 result:=math2sub.cosdeg(x)/y;
661 excode:=DefaultExcode
662 end;
663
664 function SECDEG(const x:extended):extended;inline;
665 begin
666 excode:=$03EB03EB; // 1003 1003
667 result:=math2sub.secdeg(x);
668 excode:=DefaultExcode
669 end;
670
671 function CSCDEG(const x:extended):extended;inline;
672 begin
673 excode:=$03EB03EB; // 1003 1003
674 result:=math2sub.cscdeg(x);
675 excode:=DefaultExcode
676 end;
677
678
679 function ACOSDEG(const x:extended):extended;inline;
680 begin
681 result:= acos(x)*degree
682 end;
683
684 function ASINDEG(const x:extended):extended;inline;
685 begin
686 result:= asin(x)*degree
687 end;
688
689 function ATNDEG(const x:extended):extended;inline;
690 begin
691 result:=arctan(x)*degree
692 end;
693
694 function ANGLE(const x,y:extended):extended;inline;
695 begin
696 if (x=0) and (y=0) then
697 setexception(3008)
698 else
699 result:=ArcTan2(y,x)
700 end;
701
702 function ANGLEdeg(const x,y:extended):extended;inline;
703 begin
704 result:=ANGLE(x,y)*degree
705 end;
706
707 function COSH(const x:extended):extended;inline;
708 begin
709 excode:=$03EB0000; // 1003 0
710 result:=math2sub.cosh(x);
711 excode:=DefaultExcode
712 end;
713
714 function SINH(const x:extended):extended;inline;
715 begin
716 excode:=$03EB0000; // 1003 0
717 result:=math2sub.sinh(x);
718 excode:=DefaultExcode
719 end;
720
721
722 function SQRT(const x:extended):extended;inline;
723 begin
724 if x<0 then setexception(3005);
725 result:=system.SQRT(x)
726 end;
727
728 var LogMaxExtended:extended;
729 function EXP(const x:extended):extended; inline;
730 begin
731 if x>=LogMaxExtended then setexception(1003);
732 //excode:=$03EB03EB; // 1003 1003
733 result:=system.exp(x);
734 //excode:=DefaultExcode;
735 end;
736
737
738 function LOG(const x:extended):extended;inline;
739 begin
740 if x<=0 then setexception(3004);
741 //excode:=$03EB0BBC; // 1003 3004
742 result:=ln(x);
743 //excode:=DefaultExcode
744 end;
745
746 function LOG2(const x:extended):extended;inline;
747 begin
748 if x<=0 then setexception(3004);
749 //excode:=$03EB0BBC; // 1003 3004
750 result:=Math.log2(x);
751 //excode:=DefaultExcode
752 end;
753
754 function LOG10(const x:extended):extended;inline;
755 begin
756 if x<=0 then setexception(3004);
757 //excode:=$03EB0BBC; // 1003 3004
758 result:=Math.log10(x);
759 //excode:=DefaultExcode
760 end;
761
762
763 function EPS(x:double):double;inline;
764 begin
765 float.FEPS(x);
766 result:=x;
767 end;
768
769
770 function BINT(x:double):double; inline;
771 begin
772 float.FFloor(x);
773 result:=x;
774 end;
775
776 function BCEIL(x:double):double;inline;
777 begin
778 float.FCEIL(x);
779 result:=x;
780 end;
781
782
783 function BMOD(x,y:double):double;inline;
784 begin
785 excode:=$03EB0BBE; // 1003 3006
786 float.BasicMod(x,y);
787 result:=x;
788 excode:=DefaultExcode
789 end;
790
791 function REMAINDER(x,y:double):double;inline;
792 begin
793 excode:=$03EB0BBE; // 1003 3006
794 result:=x-y*system.int(x/y);
795 excode:=DefaultExcode
796 end;
797
798 function Power(const x,y:extended):extended;inline;
799 begin
800 if x>0 then
801 begin
802 {$IFNDEF Windows}
803 excode:=$03EA03EA; // 1002 1002
804 {$ENDIF}
805 result:=math.power(x,y);
806 {$IFNDEF Windows}
807 excode:=DefaultExcode;
808 {$ENDIF}
809 end
810 else if x=0 then
811 if y=0 then
812 result:=1
813 else if y<0 then
814 setexception(3003)
815 else
816 result:=0
817 else { x<0 }
818 if system.int(y)=y then
819 result:=math.power(x,y)
820 else
821 setexception(3002)
822 end;
823
824
825
826 function power10(i:integer):extended;
827 var
828 x,y:extended;
829 begin
830 x:=10.;
831 y:=1.;
832 if i<0 then begin x:=1./x ; i:=-i end;
833 while i>0 do
834 begin
835 if i mod 2 =1 then
836 y:=y*x;
837 i:=i div 2;
838 if i>0 then x:=x*x;
839 end;
840 power10:=y
841 end;
842
843 procedure fround2(var x,n:double);
844 var
845 e:extended;
846 begin
847 e:=power10(LongIntRound(n));
848 x:=x*e+0.5;
849 FFLOOR(x);
850 x:=x/e;
851 end;
852
853 procedure ftruncate(var x,n:double);
854 var
855 e:extended;
856 begin
857 e:=power10(LongIntRound(n));
858 x:=x*e;
859 x:=int(x);
860 x:=x/e;
861 end;
862
863 function ROUND(x:double):double;overload; inline;
864 begin
865 float.FROUND(x);
866 result:=x
867 end;
868
869 function ROUND(x,y:double):double;overload; inline;
870 begin
871 fround2(x,y);
872 result:=x
873 end;
874
875 function TRUNCATE(x,y:double):double; inline;
876 begin
877 ftruncate(x,y);
878 result:=x
879 end;
880
881 function BMIN(const x,y:double):double; inline;
882 begin
883 if x<y then
884 result:=x
885 else
886 result:=y
887 end;
888
889 function BMAX(const x,y:double):double; inline;
890 begin
891 if x>y then
892 result:=x
893 else
894 result:=y
895 end;
896
897
898 function PERM( n,r:double):double;
899 var
900 i,k:longint;
901 begin
902 k:=LongIntRound(r);
903 if k<0 then
904 begin
905 result:=0;
906 invalidoperation
907 end
908 else
909 begin
910 result:=1;
911 for i:=1 to k do
912 begin
913 result:=result*n;
914 n:=n-1;
915 end;
916 end;
917 end;
918
919 function COMB( n,r:double):double;
920 var
921 i,k:longint;
922 m:double;
923 x:extended;
924 begin
925 FROUND(r);
926 k:=LongIntRound(r);
927 if k<0 then
928 x:=0
929 else if (k>n/2) and (n=int(n)) and (n>0) then
930 x:=comb(n,n-r)
931 else
932 begin
933 x:=1;
934 m:=1;
935 for i:=1 to k do
936 begin
937 x:=x*n/m;
938 n:=n-1;
939 m:=m+1;
940 end;
941 end;
942 result:=x;
943 end;
944
945 function FACT(const x:double):double;
946 begin
947 result:=PERM(LongIntRound(x),x)
948 end;
949
950 {**************}
951 {BIT Operations}
952 {**************}
953 function BitNOT(a:Int64):Int64; overload;
954 begin
955 result:=not a
956 end;
957
958 function BitNOT(a:double):Int64; overload;
959 begin
960 result:=not LongIntRound(a)
961 end;
962
963 function BitAND(a,b:int64):int64; overload;
964 begin
965 result:=a and b
966 end;
967
968 function BitAND(a,b:double):int64; overload;
969 begin
970 result:=LongIntRound(a) and LongIntRound(b)
971 end;
972
973 function BitAND(a:int64;b:double):int64; overload;
974 begin
975 result:=a and LongIntRound(b)
976 end;
977
978 function BitAND(a:double;b:int64):int64; overload;
979 begin
980 result:=LongIntRound(a) and b
981 end;
982
983 function BitOR(a,b:int64):int64; overload;
984 begin
985 result:=a or b
986 end;
987
988 function BitOR(a,b:double):int64; overload;
989 begin
990 result:=LongIntRound(a) or LongIntRound(b)
991 end;
992
993 function BitOR(a:int64;b:double):int64; overload;
994 begin
995 result:=a or LongIntRound(b)
996 end;
997
998 function BitOR(a:double;b:int64):int64; overload;
999 begin
1000 result:=LongIntRound(a) or b
1001 end;
1002
1003 function BitXOR(a,b:int64):int64; overload;
1004 begin
1005 result:=a xor b
1006 end;
1007
1008 function BitXOR(a,b:double):int64; overload;
1009 begin
1010 result:=LongIntRound(a) xor LongIntRound(b)
1011 end;
1012
1013 function BitXOR(a:int64;b:double):int64; overload;
1014 begin
1015 result:=a xor LongIntRound(b)
1016 end;
1017
1018 function BitXOR(a:double;b:int64):int64; overload;
1019 begin
1020 result:=LongIntRound(a) xor b
1021 end;
1022
1023
1024
1025 {****************}
1026 {String Functions}
1027 {****************}
1028 function SubStringByte(s:string; i1,i2:integer):string;overload;
1029 begin
1030 result:=Copy(s,i1,i2-i1+1)
1031 end;
1032
1033 function SubStringByte(s:string; i1,i2:double):string;overload;
1034 begin
1035 result:=SubStringByte(s,LongIntRound(i1),LongintRound(i2))
1036 end;
1037
1038
1039
1040 function SubString(s:string; i1,i2:integer):string;overload;
1041 begin
1042 result:=UTF8Copy(s,i1,i2-i1+1)
1043 end;
1044
1045 function SubString(s:string; i1,i2:double):string;overload;
1046 begin
1047 result:=SubString(s,LongIntRound(i1),LongintRound(i2))
1048 end;
1049
1050 function Mid_s(s:string; i1,i2:integer):string;overload;
1051 begin
1052 result:=UTF8Copy(s,i1,i2)
1053 end;
1054
1055 function Mid_s(s:string; i1,i2:double):string;overload;
1056 begin
1057 result:=Mid_s(s,LongintRound(i1),LongintRound(i2))
1058 end;
1059
1060 function Mid_sByte(s:string; i1,i2:integer):string;overload;
1061 begin
1062 result:=Copy(s,i1,i2)
1063 end;
1064
1065 function Mid_sByte(s:string; i1,i2:double):string;overload;
1066 begin
1067 result:=Mid_sByte(s,LongintRound(i1),LongintRound(i2))
1068 end;
1069
1070 function Left_sByte(s:string;i:integer):string;overload;
1071 begin
1072 result:=Copy(s,1,i)
1073 end;
1074
1075 function Left_sByte(s:string; i:double):string;overload;
1076 begin
1077 result:=Left_sByte(s,LongintRound(i))
1078 end;
1079
1080 function Left_s(s:string;i:integer):string;overload;
1081 begin
1082 result:=UTF8Copy(s,1,i)
1083 end;
1084
1085 function Left_s(s:string;i:double):string;overload;
1086 begin
1087 result:=Left_s(s,LongintRound(i))
1088 end;
1089
1090 function Right_sByte(s:string;i:integer):string;overload;
1091 begin
1092 result:=copy(s,length(s)-i+1,i)
1093 end;
1094
1095 function Right_s(s:string;i:integer):string;overload;
1096 var
1097 len:integer;
1098 begin
1099 len:=Utf8Length(s);
1100 result:=UTF8copy(s,len-i+1,i)
1101 end;
1102
1103 function Right_sByte(s:string; i:double):string;overload;
1104 begin
1105 result:=Right_sByte(s,LongintRound(i))
1106 end;
1107
1108 function Right_s(s:string;i:double):string;overload;
1109 begin
1110 result:=Right_s(s,LongintRound(i))
1111 end;
1112
1113
1114 function basicORD(s:AnsiString; CharacterByte:boolean):integer;
1115 var
1116 i:integer;
1117 charlen:integer;
1118 begin
1119 if Length(s)=1 then
1120 basicORD:=ord(s[1])
1121 else if (Length(s)=3) and (byte(s[1])<128) then
1122 begin
1123 s:=AnsiUpperCase(s);
1124 if (length(s)=3) and (copy(s,1,2)='LC') then
1125 basicORD:=ord(s[3])+32
1126 else
1127 begin
1128 for i:=0 to 39 do
1129 if s=CharNameTBL1[i] then begin basicORD:=CharNameTBL2[i]; exit end;
1130 basicORD:=0;
1131 setexceptionwith('ORD',4003);
1132 end ;
1133 end
1134 else if characterbyte then
1135 begin
1136 basicORD:=0;
1137 setexceptionwith('ORD',4003);
1138 end
1139 else
1140 begin
1141 BASICOrd:=UTF8CharacterToUnicode(PChar(s),charlen);
1142 if charlen<length(s) then setexceptionwith('ORD',4003);
1143 end;
1144 end;
1145
1146 function pos2(const a,b:ansistring):integer; inline;
1147 begin
1148 if b<>'' then
1149 pos2:=pos(b,a)
1150 else
1151 pos2:=1;
1152 end;
1153
1154 function pos3(const a,b:ansistring; m0:double):integer;
1155 var
1156 temp1,temp3:integer;
1157 temp2:ansistring;
1158 m:integer;
1159 begin
1160 m:=longintround(m0);
1161 if m<=length(a) then
1162 begin
1163 temp1:=base.max(1,base.min(m,length(a)+1));
1164 temp2:=copy(a,temp1,maxint);
1165 temp3:=pos2(temp2,b);
1166 if temp3=0 then
1167 pos3:=0
1168 else
1169 pos3:=temp3+temp1-1
1170 end
1171 else
1172 pos3:=0;
1173 end;
1174
1175 function UTF8Pos2(const a,b:ansistring):integer;
1176 begin
1177 if b<>'' then
1178 result:=UTF8Pos(b,a)
1179 else
1180 result:=1;
1181 end;
1182
1183 function UTF8Pos3(const a,b:ansistring; m0:double):integer;
1184 var
1185 temp1,temp3:integer;
1186 temp2:ansistring;
1187 m:integer;
1188 begin
1189 m:=LongIntRound(m0);
1190 if m<=Utf8length(a) then
1191 begin
1192 temp1:=base.max(1,base.min(m,Utf8length(a)+1));
1193 temp2:=Utf8copy(a,temp1,maxint);
1194 temp3:=UTF8Pos2(temp2,b);
1195 if temp3=0 then
1196 Result:=0
1197 else
1198 Result:=temp3+temp1-1
1199 end
1200 else
1201 result:=0;
1202 end;
1203
1204
1205 function BVAL2(const s:string):int64;
1206 var
1207 i:integer;
1208 t:int64;
1209 begin
1210 result:=0;
1211 t:=1;
1212 i:=length(s);
1213 while i>0 do
1214 begin
1215 case s[i] of
1216 '0' : ;
1217 '1' : result:=result + t;
1218 else setexceptionwith('BVAL',4201);
1219 end;
1220 t:=t*2;
1221 dec(i)
1222 end;
1223 end;
1224
1225 function BVAL16(const s:string):int64;
1226 var
1227 i:integer;
1228 t:int64;
1229 c:char;
1230 begin
1231 result:=0;
1232 t:=1;
1233 i:=length(s);
1234 while i>0 do
1235 begin
1236 c:=s[i];
1237 case c of
1238 '0'..'9' : result:=result + t * (ord(c)-ord('0'));
1239 'A'..'F' : result:=result + t * (ord(c)-ord('A')+10);
1240 'a'..'f' : result:=result + t * (ord(c)-ord('a')+10);
1241 else setexceptionwith('BVAL',4201);
1242 end;
1243 t:=t*16;
1244 dec(i)
1245 end;
1246 end;
1247
1248
1249 function VAL(const s:string):double;
1250 var
1251 n:number;
1252 begin
1253 try
1254 Nval(s,n);
1255 checkrangedecimal(n,1004);
1256 except
1257 on E:EExtype do
1258 begin
1259 if E.extype>=3000 then
1260 base.extype:=4001;
1261 if (E.extype>=1000) and (E.extype <1004) then
1262 base.extype:=1004;
1263 if Base.extype>0 then
1264 raise EExtype.create(base.extype)
1265 end;
1266 end;
1267 result:=extendedVal(n);
1268 end;
1269
1270
1271 function STR_s(x:double):string;
1272 var
1273 n:number;
1274 begin
1275 convert(x,n);
1276 result:=Trim(Dstr(n));
1277 end;
1278
1279 function CHRbyte(x:double):string;
1280 var
1281 b:byte;
1282 begin
1283 b:=LongintRound(x);
1284 result:=chr(b)
1285 end;
1286
1287 function CHR_s(x:double):string;
1288 begin
1289 result:=UnicodeToUTF8(LongIntRound(x));
1290 end;
1291
1292 function USING_ss(const s:string; x:double):string;
1293 var
1294 i,c:integer;
1295 begin
1296 i:=1;
1297 TestFormatItem(s);
1298 result:=formatDouble(x,s,i,c);
1299 if (c<>0) then
1300 setexceptionwith('USING$',c)
1301 end;
1302
1303 function USING_s(const s:string; x:double):string;
1304 var
1305 i,c:integer;
1306 begin
1307 i:=1;
1308 TestFormatItem(s);
1309 result:=formatDouble(x,s,i,c);
1310 end;
1311
1312 function REPEAT_s(const s:string; x:double):string;
1313 var
1314 m,len:int64;
1315 l:longint;
1316 i:integer;
1317 begin
1318 result:='';
1319 m:=system.Round(x);
1320 l:=Length(s);
1321 if (m>=0) then
1322 begin
1323 len:=m*l;
1324 if (len<0) or (len>=MaxLongInt) then setexception(1051);
1325 try
1326 setlength(result,len);
1327 if len>0 then
1328 if l=1 then
1329 FillChar(result[1],len,s[1])
1330 else
1331 for i:=0 to m-1 do move(s[1],result[1+i*l],l) ;
1332 except
1333 setexception(OutOfMemory)
1334 end;
1335 end
1336 else
1337 setexception(4010);
1338 end;
1339
1340 function LTRIM_s(s:string):string;
1341 var
1342 i:integer;
1343 begin
1344 i:=0;
1345 while (i<length(s)) and (s[i+1]=' ') do inc(i);
1346 delete(s,1,i);
1347 result:=s
1348 end;
1349
1350 function RTRIM_s(s:string):string;
1351 var
1352 i:integer;
1353 begin
1354 i:=Length(s);
1355 while (i>0) and (s[i]=' ') do dec(i);
1356 delete(s,i+1,length(s)-i);
1357 result:=s
1358 end;
1359
1360 function BSTR_s(x:double; n:integer):string;
1361 var
1362 t:double;
1363 i:integer;
1364 begin
1365 if (x<0.0) then setexceptionwith('BSTR$',4203);
1366 t:=system.int(x);
1367 if x-t<0.5 then x:=t else x:=t+1;
1368 if x=0 then
1369 result:='0'
1370 else
1371 if n=2 then
1372 begin
1373 result:='';
1374 while x>0 do
1375 begin
1376 t:=x/2;
1377 x:=system.int(t);
1378 if x=t then
1379 result:='0' + result
1380 else
1381 result:='1'+result;
1382 end;
1383 end
1384 else if n=16 then
1385 begin
1386 result:='';
1387 while x>0 do
1388 begin
1389 t:=x/16;
1390 x:=system.int(t);
1391 i:=system.round(16*(t-x));
1392 if i<10 then
1393 result:=chr(ord('0')+ i) + result
1394 else
1395 result:=chr(ord('A')-10 + i) + result
1396
1397 end;
1398 end
1399 end;
1400
1401
1402 function format2(i:integer):ansistring;
1403 var
1404 s:ansistring;
1405 begin
1406 system.str(i:2,s);
1407 if s[1]=' ' then s[1]:='0';
1408 format2:=s
1409 end;
1410
1411 function DATE_s:ansistring;
1412 var
1413 y,m,d,w:WORD;
1414 begin
1415 decodedate(date,y,m,d);
1416 {getdate(y,m,d,w);}
1417 system.str(y:4,result);
1418 result:=result+format2(m)+format2(d);
1419 end;
1420
1421 function TIME_s:ansistring;
1422 var
1423 h,m,sec,msec:WORD;
1424 begin
1425 DecodeTime(Time, h, m, Sec, MSec);
1426 { gettime(h,m,sec,s100);}
1427 result:=format2(h)+':'+format2(m)+':'+format2(sec);
1428 end;
1429
1430 function confirm_s(s:string):string;
1431 begin
1432 result:=YesNoLiteral[MessageDlg(s,mtConfirmation,[mbYes,mbNo],0)=mrYes]
1433 end;
1434 {***************}
1435 {EXTYPE function}
1436 {***************}
1437
1438 function EXTYPE(const E:Exception; ExcodeRec:LongWord):Integer;
1439 begin
1440 if E is EOverflow then
1441 result:= excodeRec div $10000
1442 else if ( E is EdivByZero) or (E is EZeroDivide) then
1443 if excodeRec=defaultexcode then
1444 result:=3001
1445 else
1446 result:=excodeRec mod $10000
1447 else if E is EMathError then // 算術演算の誤り
1448 result:=excodeRec mod $10000 // 3004 LOG, 3005 SQR, 3006 MOD, 3007 ACOS ASIN
1449 else if E is EExtype then
1450 result:=EExtype(E).Extype
1451 else
1452 result:=0;
1453 end;
1454
1455 function Exmess(const E:Exception; ExcodeRec:LongWord):string;
1456 var
1457 i:integer;
1458 begin
1459 result:='';
1460 i:=EXTYPE(E, ExcodeRec);
1461 if i<>0 then
1462 begin
1463 case i mod 100000 of
1464 0 : result:='' ;
1465 1001 : result:=s_Extype1001;
1466 1002 : result:=s_Extype1002;
1467 1003 : result:=s_Extype1003;
1468 1006 : result:=s_Extype1006;
1469 1007 : result:=s_Extype1007;
1470 1008 : result:=s_Extype1008;
1471 1050..1106: result:=s_Extype1050;
1472 1004..1005,
1473 1009..1049,
1474 1107..1999: result:=s_Extype1000;
1475
1476 2001 : result:=s_Extype2001;
1477 3000 : result:=s_Extype3000;
1478 3001 : result:=s_Extype3001;
1479 3002 : result:=s_Extype3002;
1480 3003 : result:=s_Extype3003;
1481 3004 : result:=s_Extype3004 + '(LOG)';
1482 3005 : result:=s_Extype3004 + '(SQR)'+ EOL + 'Otherwise' + EOL + 'Unknown Fault (wrong FPU usage)';
1483 3006 : result:=s_Extype3004 + '(MOD)';
1484 3007 : result:=s_Extype3004 + '(ASIN or ACOS)';
1485 3008 : result:=s_Extype3004 + '(ANGLE)';
1486 3009 : result:=s_Extype3009;
1487 4000..4299:
1488 begin
1489 result:=s_Extype3004;
1490 case i mod 100000 of
1491 4004: result:=result + '(SIZE)';
1492 4005: result:=result + '(TAB)';
1493 4008: result:=result + '(LBOUND)';
1494 4009: result:=result + '(UBOUND)';
1495 4010: result:=result + '(REPEAT$)';
1496 else
1497 end;
1498 end;
1499 5001,5002 : result:=s_Extype5001;
1500 6001..6402: result:=s_Extype6001;
1501 7001 : result:=s_Extype7001;
1502 7003 : result:=s_Extype7003;
1503 7004 : result:=s_Extype7004;
1504 7101 : result:=s_Extype7101;
1505 7102 : result:=s_Extype7102;
1506 7103 : result:=s_Extype7103;
1507 7301 : result:=s_EXtype7301;
1508 7302 : result:=s_EXtype7302;
1509 7303 : result:=s_EXtype7303;
1510 7305 : result:=s_Extype7305;
1511 7308 : result:=s_Extype7308;
1512 7317 : result:=s_Extype7317;
1513 7318 : result:=s_Extype7318;
1514
1515 7005..7100,7104..7300,7311..7316,7320..7402
1516 : result:=s_Extype7000;
1517 8001 : result:=s_Extype8001;
1518 8011 : result:=s_Extype8011;
1519 8012 : result:=s_Extype8012;
1520 8013 : result:=s_Extype8013;
1521 8101 : result:=s_Extype8101;
1522 8002,8003,8102,8103: result:=s_Extype8002;
1523 8105 : result:=s_Extype8105;
1524 8120 : result:=s_Extype8120;
1525 8201 : result:=s_Extype8201;
1526 8202 : result:=s_Extype8202;
1527 8401 : result:=s_Extype8401;
1528 8402 : result:=s_Extype8402;
1529 9000 : result:=s_Extype9000;
1530 9002 : result:=s_Extype9002;
1531 9003 : result:=s_Extype9003;
1532 9004 : result:=s_Extype9004;
1533 9102 : result:=s_Extype9102;
1534 10002 : result:=s_Extype10002;
1535 10004 : result:=s_Extype10004;
1536 11004 : result:=s_Extype11004;
1537 11051 : result:=s_Extype11051;
1538 12004 : result:=s_Extype12004;
1539 outofmemory : result:=s_OutoOfMemory;
1540 virtualStackOverflow: result:=s_VStackOverflow;
1541 stackoverflow: result:=s_StackOverflow;
1542 ArraySizeOverflow: result:=s_ArraySizeOverflow;
1543 TextOverFlow: result:=s_OutputOverflow;
1544 systemErr : result:='system error';
1545 else result:='' ;
1546 end;
1547 result:='extype '+inttostr(i mod 100000)+EOL + result;
1548 end;
1549 if not (E is EExtype) then
1550 begin
1551 if Result<>'' then
1552 Result:=Result +EOL+ 'or' + EOL;
1553 Result:= Result + E.Message + EOL + E.classname;
1554 if Pos('Run-Time', E.Message)>0 then
1555 if Pos('202',E.Message)>0 then
1556 result:=Result + EOL + 'Stack overflow'
1557 else if Pos('203',E.Message)>0 then
1558 result:=Result + EOL + 'Heap overflow'
1559 else
1560 end;
1561 end;
1562
1563 function PropagatedExtype(t:integer):integer;
1564 begin
1565 result:=t;
1566 if (result>0) and (result<100000) then
1567 result:=100000 + result
1568 else if (result<0) and (result>-100000) then
1569 result:=result-100000
1570 end;
1571
1572 {String Variable}
1573
1574 constructor TStrVar.create (p:PString);
1575 begin
1576 inherited create;
1577 PVar:=p
1578 end;
1579
1580 procedure TStrVar.setstring(const s:string);
1581 begin
1582 PVar^:=s;
1583 end;
1584
1585 function TStrVar.getstring:string;
1586 begin
1587 result:=PVar^
1588 end;
1589
1590
1591 constructor TStrVar2.create (P:PString; l,r:integer); overload;
1592 begin
1593 inherited create(p);
1594 left:=l;
1595 right:=r
1596 end;
1597
1598 constructor TStrVar2.create (P:PString; l,r:double); overload;
1599 begin
1600 create(P,LongintRound(l),LongintRound(r))
1601 end;
1602
1603 constructor TStrVar2.create (P:PString; l,r:complex); overload;
1604 begin
1605 create(P,testreal(l),testreal(r))
1606 end;
1607 constructor TStrVar2.create (P:PString; l:complex;r:double); overload;
1608 begin
1609 create(P,testreal(l),testreal(r))
1610 end;
1611 constructor TStrVar2.create (P:PString; l:double; r:complex); overload;
1612 begin
1613 create(P,testreal(l),testreal(r))
1614 end;
1615
1616 procedure TStrVar2.setstring(const s:string);
1617 begin
1618 SubstSubstring(Pvar^,left,right,s)
1619 end;
1620
1621 function TStrVar2.getstring:string;
1622 begin
1623 result:=substring(PVar^, left, right)
1624 end;
1625
1626 procedure TStrVarByte.setstring(const s:string);
1627 begin
1628 SubstSubstringByte(Pvar^,left,right,s)
1629 end;
1630
1631 function TStrVarByte.getstring:string;
1632 begin
1633 result:=substringbyte(PVar^, left, right)
1634 end;
1635
1636
1637
1638
1639 {Let Statements}
1640 procedure LET(const p:Array of PDouble; x:double);
1641 var
1642 i:integer;
1643 begin
1644 for i:=0 to High(p) do
1645 p[i]^:=x;
1646 end;
1647
1648
1649 procedure LETS(const p:Array of TStrVar; const s:string);
1650 var
1651 i:integer;
1652 begin
1653 for i:=0 to High(p) do
1654 begin
1655 p[i].str:=s;
1656 p[i].free
1657 end;
1658 end;
1659
1660 procedure BreakPr(s:Ansistring);
1661 begin
1662 if MessageDlg(s, mtCustom, [mbOk,MbAbort], 230)=MrAbort then
1663 raise EStop.create;
1664 end;
1665
1666 procedure Wait(n:extended);
1667 var
1668 svCtrlBreakHit:boolean;
1669 finish:extended;
1670 begin
1671 svCtrlBreakHit:=CtrlBreakHit;
1672 CtrlBreakHit:=false;
1673 try
1674 finish:=Date+time+n/(24*3600);
1675 except
1676 setexception(12004);
1677 end;
1678 repeat
1679 sleep(10);IdleImmediately;
1680 until (Date+time-Finish>=0) or CtrlBreakHit;
1681 CtrlBreakHit:=CtrlBreakHit or SvCtrlBreakHit;
1682 end;
1683
1684 procedure ShowMess(const s:string);
1685 begin
1686 if MessageDlg(s, mtCustom, [mbOk], 800)<>mrOk then
1687 CtrlBreakHit:=true;
1688 end;
1689
1690
1691
1692 {PRINT Statements}
1693
1694
1695 PROCEDURE PRINT(args:array of const);
1696 begin
1697 console.PRINT([],rsNone,false,args)
1698 end;
1699
1700 {swap}
1701 procedure swap(var x,y:double);overload;
1702 var
1703 t:double;
1704 begin
1705 t:=x;
1706 x:=y;
1707 y:=t
1708 end;
1709
1710 procedure swap(var x,y:integer);overload;
1711 var
1712 t:integer;
1713 begin
1714 t:=x;
1715 x:=y;
1716 y:=t
1717 end;
1718
1719 procedure swap(var x,y:string);overload;
1720 var
1721 t:string;
1722 begin
1723 t:=x;
1724 x:=y;
1725 y:=t
1726 end;
1727
1728 function GetKeyState(const x:double):integer;
1729 begin
1730 Application.ProcessMessages;
1731 result:=lclintf.GetKeyState(LongIntRound(x));
1732 end;
1733
1734
1735
1736 { File st}
1737
1738 procedure FileDelete(const s:string);
1739 begin
1740 if FileExists(s) then
1741 if DeleteFile(s) then
1742 else
1743 setexception(9000)
1744 else
1745 setexception(9003)
1746 end;
1747
1748 procedure FileGetName(const s:string; svar:TStrVar; aux:integer);
1749 var
1750 dlg:TOpenDialog;
1751 begin
1752 if aux=2 then
1753 dlg:=TSaveDialog.create(nil)
1754 else
1755 dlg:=TOpenDialog.create(nil);
1756 with svar do
1757 with dlg do
1758 begin
1759 if aux=1 then options:=[ofPathMustExist,ofFileMustExist];
1760 options:=options + [ofOldStyleDialog]; //Lazarus 0.9.28.2 では必須
1761 if s='' then
1762 begin
1763 DefaultExt:='txt' ;
1764 Filter:=s_TextFile+'|*.TXT;*.kw*;*.log;*'+BasExt+';*'+LibExt+'|'
1765 +s_BitMap +'|*.bmp'+ '|'
1766 +s_AllFile +'|*.*';
1767 end
1768 else
1769 begin
1770 if pos('|',s)=0 then
1771 begin
1772 DefaultExt:=s ;
1773 Filter:=s + s_FILE + '|' + '*.' +s
1774 end
1775 else
1776 begin
1777 Filter:=s
1778 end;
1779 end;
1780 execute;
1781 str:=FileName;
1782 free;
1783 end;
1784 svar.free;
1785 end;
1786
1787 procedure FileSplitname(const s:string; svar1,svar2,svar3:TStrVar);
1788 var
1789 name,ext:string;
1790 i:integer;
1791 begin
1792 svar1.str:=ExtractFilePath(s);
1793 name:=ExtractFileName(s);
1794 i:=lastDelimiter('.',name);
1795 ext:=copy(name,i,maxint);
1796 name:=copy(name,1,i-1);
1797 svar2.str:=name;
1798 svar3.str:=ext;
1799 svar3.free;
1800 svar2.free;
1801 svar1.free;
1802 end;
1803
1804 procedure FileList(const s:string; a:TArray1S);
1805 var
1806 Rec:TSearchRec;
1807 i:integer;
1808 begin
1809 if a<>nil then
1810 begin
1811 i:=0;
1812 try
1813 if FindFirst(Utf8ToSys(s),0,Rec)=0 then
1814 begin
1815 if a.Size<=i then SetException(5001);
1816 with a do elements^[i]:=SysToUtf8(Rec.Name);
1817 inc(i);
1818 while FindNext(Rec)=0 do
1819 begin
1820 if a.size<=i then SetException(5001);
1821 with a do elements^[i]:=SysToUtf8(Rec.Name);
1822 inc(i);
1823 end;
1824 end;
1825 finally
1826 FindClose(Rec);
1827 end;
1828 a.Resize(i);
1829 end;
1830 end;
1831
1832 procedure FileReName(const s1,s2:string);
1833 begin
1834 if FileExists(UTF8ToSys(s1)) then
1835 begin
1836 if FileExists(Utf8ToSys(s2)) then
1837 setexception(9004)
1838 else if not RenameFile(Utf8ToSys(s1),Utf8ToSys(s2)) then
1839 setexception(9000)
1840 end
1841 else
1842 setexception(9003);
1843 end;
1844
1845 function Files(const s:string):integer;
1846 var
1847 Rec:TSearchRec;
1848 begin
1849 result:=0;
1850 try
1851 if FindFirst(Utf8ToSys(s),0,Rec)=0 then
1852 begin
1853 inc(result);
1854 while FindNext(Rec)=0 do
1855 inc(result);
1856 end;
1857 finally
1858 FindClose(Rec);
1859 end;
1860 end;
1861
1862 procedure SetDirectory(const s:string);
1863 var
1864 exty:integer;
1865 begin
1866 try
1867 chDir(UTF8ToSys(s))
1868 except
1869 on E:EInOutError do
1870 begin
1871 if E.ErrorCode=21 then
1872 exty:=9002
1873 else
1874 exty:=9008;
1875 setexception(exty);
1876 end;
1877 end;
1878 end;
1879
1880 procedure AskDirectory(svar:TStrVar);
1881 begin
1882 svar.str:=SysToUTF8(GetCurrentDir);
1883 svar.free
1884 end;
1885
1886 {***}
1887 {ASK}
1888 {***}
1889
1890 function AskMargin(ch:TTextDevice):integer;far;
1891 begin
1892 if (ch.rectype=rcDisplay) then
1893 AskMargin:=ch.Margin
1894 else
1895 AskMargin:=0;
1896 end;
1897
1898 function AskZoneWidth(ch:TTextDevice):integer;far;
1899 begin
1900 if (ch.rectype=rcDisplay)then
1901 AskZoneWidth:=ch.ZoneWidth
1902 else
1903 AskZoneWidth:=0;
1904 end;
1905
1906 function AskCharacterPending(ch:TTextDevice):integer;far;
1907 begin
1908 IdleImmediately;
1909 result:=ch.AskCharacterPending
1910 end;
1911
1912
1913 function AskFILETYPE(ch:TTextDevice):AnsiString;
1914 begin
1915 if ch.TrueFile then
1916 result:='FILE'
1917 else
1918 result:='DEVICE'
1919 end;
1920
1921 function AskEcho(ch:TTextDevice):AnsiString;
1922 begin
1923 if ch.echoOn then
1924 result:='ON'
1925 else
1926 result:='OFF'
1927 end;
1928
1929 procedure AskFile(ch:TTextDEvice;
1930 expAccess,expDatum,expErasable,expFileType,expName,
1931 expOrganization,expPointer,expRecsize1,expRecType,
1932 expSetter,expCharin,expTypeahead,expEchoControl,expEcho:TStrVar;
1933 expMargin,expRecSize2,expZonewidth,expCharacterPending,expFilesize:Pdouble);
1934 begin
1935 if (ch<>nil) and ch.isopen then
1936 begin
1937 if expAccess<>nil then
1938 with expAccess do
1939 begin
1940 str:=AccessModeLiteral[ch.AMode];
1941 free;
1942 end;
1943 if expDatum<>nil then
1944 with expDatum do
1945 begin
1946 str:=ch.Datum;
1947 free;
1948 end;
1949 if expErasable<>nil then
1950 with expErasable do
1951 begin
1952 str:=YesNoLiteral[((ch.amode=amOUTIN) and ch.TrueFile) or (ch is TLocalPrinter)];
1953 free;
1954 end;
1955 if expFileType<>nil then
1956 with expFiletype do
1957 begin
1958 str:=askFileType(ch);
1959 free;
1960 end;
1961 if expName<>nil then
1962 with expName do
1963 begin
1964 str:=ch.Name;
1965 free;
1966 end;
1967 if expOrganization<>nil then
1968 with expOrganization do
1969 begin
1970 str:=OrganizationTypeLiteral[ch.OrgType];
1971 free;
1972 end;
1973 if expPointer<>nil then
1974 with expPointer do
1975 begin
1976 str:=ch.askPointer;
1977 free;
1978 end;
1979 if expRecType<>nil then
1980 with expRecType do
1981 begin
1982 str:=RecordTypeLiteral[ch.Rectype];
1983 free;
1984 end;
1985 if expSetter<>nil then
1986 with expSetter do
1987 begin
1988 str:=YesNoLiteral[ch.TrueFile];
1989 free;
1990 end;
1991 if expCharin<>nil then
1992 with expCharin do
1993 begin
1994 str:=YesNoLiteral[(ch.rectype=rcDisplay) and (ch.AMode in [amOutin,amInput])];
1995 free;
1996 end;
1997 if expTypeahead<>nil then
1998 begin
1999 with expTypeAhead do
2000 begin
2001 str:=YesNoLiteral[ch.AskTypeAhead];
2002 free;
2003 end;
2004 if UseCharInput and (ch=console) then
2005 charinput.show;
2006 end;
2007 if expEchoControl<>nil then
2008 with expEchoControl do
2009 begin
2010 str:=YesNoLiteral[ch=console];
2011 free;
2012 end;
2013 if expEcho<>nil then
2014 with expEcho do
2015 begin
2016 str:=askEcho(ch);
2017 free;
2018 end;
2019 if expMargin<>nil then expMargin^:=askMargin(ch);
2020 if expZonewidth<>nil then expZonewidth^:=askZonewidth(ch);
2021 if expCharacterPending<>nil then expCharacterPending^:=askCharacterPending(ch);
2022 if expFileSize<>nil then expFilesize^:=(ch.askfilesize);
2023
2024 if expRecsize1<>nil then
2025 with expRecSize1 do
2026 begin
2027 str:='VARIABLE';
2028 free;
2029 end;
2030 if expRecSize2<>nil then expRecSize2^:=ch.leng;
2031 end
2032 else
2033 begin
2034 if expAccess<>nil then
2035 with expAccess do
2036 begin
2037 str:='';
2038 free;
2039 end;
2040 if expDatum<>nil then
2041 with expDatum do
2042 begin
2043 str:='';
2044 free;
2045 end;
2046 if expErasable<>nil then
2047 with expErasable do
2048 begin
2049 str:='';
2050 free;
2051 end;
2052 if expFileType<>nil then
2053 with expFiletype do
2054 begin
2055 str:='';
2056 free;
2057 end;
2058 if expName<>nil then
2059 with expName do
2060 begin
2061 str:='';
2062 free;
2063 end;
2064 if expOrganization<>nil then
2065 with expOrganization do
2066 begin
2067 str:='';
2068 free;
2069 end;
2070 if expPointer<>nil then
2071 with expPointer do
2072 begin
2073 str:='';
2074 free;
2075 end;
2076 if expRecType<>nil then
2077 with expRecType do
2078 begin
2079 str:='';
2080 free;
2081 end;
2082 if expSetter<>nil then
2083 with expSetter do
2084 begin
2085 str:='';
2086 free;
2087 end;
2088 if expCharin<>nil then
2089 with expCharin do
2090 begin
2091 str:='';
2092 free;
2093 end;
2094 if expTypeahead<>nil then
2095 with expTypeAhead do
2096 begin
2097 str:='';
2098 free;
2099 end;
2100 if expEchoControl<>nil then
2101 with expEchoControl do
2102 begin
2103 str:='';
2104 free;
2105 end;
2106 if expEcho<>nil then
2107 with expEcho do
2108 begin
2109 str:='';
2110 free;
2111 end;
2112 if expMargin<>nil then expMargin^:=0;
2113 if expZonewidth<>nil then expZonewidth^:=0;
2114 if expCharacterPending<>nil then expCharacterPending^:=0;
2115 if expFileSize<>nil then expFilesize^:=0;
2116
2117 if expRecsize1<>nil then
2118 with expRecSize1 do
2119 begin
2120 str:='';
2121 free;
2122 end;
2123 if expRecSize2<>nil then expRecSize2^:=0;
2124 end
2125
2126 end;
2127
2128
2129 {Chain,Execute}
2130 function ShellExecSub(s1,s2:string; opWaitFor:boolean):boolean;
2131 var
2132 AProcess: TProcess;
2133 begin
2134 result:=false;
2135 AProcess := TProcess.Create(nil);
2136 AProcess.CommandLine :=s1 + ' ' +s2;
2137 if opWaitFor then
2138 AProcess.Options := AProcess.Options + [poWaitOnExit];
2139 try
2140 try
2141 AProcess.Execute;
2142 result:=Aprocess.ExitStatus=0;
2143 finally
2144 AProcess.Free;
2145 end;
2146 except
2147 end;
2148 end;
2149
2150 function ShellExec(const s1:string; args:array of const; opWaitFor:boolean):boolean;
2151 var
2152 i:integer;
2153 s2:string;
2154 begin
2155 s2:='';
2156 for i:=0 to high(args) do
2157 with args[i] do
2158 case VType of
2159 VTInteger: s2:=s2+' '+IntToStr(VInteger);
2160 VtExtended: s2:=s2+' '+FloatToStr(VExtended^);
2161 VtInt64: s2:=s2+' '+IntToStr(Vint64^);
2162 VtChar: s2:=s2+' '+VChar;
2163 VtString: s2:=s2+' '+QuotedStr(VString^);
2164 VtAnsiString:s2:=s2+' '+QuotedStr(string(VAnsiString));
2165 end;
2166 result:=ShellExecSub(Utf8ToSys(s1),UTF8ToSys(s2),opWaitFor)
2167 end;
2168
2169 {****************}
2170 {Pack$ and Unpack}
2171 {****************}
2172 function PackDbl_s(d:Double):AnsiString;
2173 var
2174 s:string[8];
2175 begin
2176 move(d,s[1],8);
2177 setlength(s,8);
2178 result:=s;
2179 end;
2180
2181 function DWord_s(x:Dword):AnsiString;
2182 var
2183 s:string[4];
2184 begin
2185 move(x,s[1],4);
2186 setlength(s,4);
2187 result:=s;
2188 end;
2189
2190 function Word_s(x:DWord):AnsiString;
2191 var
2192 s:string[2];
2193 begin
2194 move(x,s[1],2);
2195 setlength(s,2);
2196 result:=s;
2197 end;
2198
2199 function Byte_s(x:DWord):ansistring;
2200 var
2201 s:string[1];
2202 begin
2203 move(x,s[1],1);
2204 setlength(s,1);
2205 result:=s;
2206 end;
2207
2208 function DWord_s(x:double):AnsiString;
2209 var
2210 d:DWord;
2211 s:string[4];
2212 begin
2213 d:=Trunc(x);
2214 move(d,s[1],4);
2215 setlength(s,4);
2216 result:=s;
2217 end;
2218
2219 function Word_s(x:double):AnsiString;
2220 var
2221 w:word;
2222 s:string[2];
2223 begin
2224 w:=Trunc(x);
2225 move(w,s[1],2);
2226 setlength(s,2);
2227 result:=s;
2228 end;
2229
2230 function Byte_s(x:double):ansistring;
2231 var
2232 b:byte;
2233 s:string[1];
2234 begin
2235 b:=Trunc(x);
2236 move(b,s[1],1);
2237 setlength(s,1);
2238 result:=s;
2239 end;
2240
2241 function UnPackDbl(const s:string):double;
2242 var
2243 d:double;
2244 begin
2245 move(s[1],d,8);
2246 result:=d;
2247 end;
2248
2249
2250
2251 {*************}
2252 {GOSUB~RETURN}
2253 {*************}
2254
2255 constructor TGosubStack.create(ssize:integer);
2256 begin
2257 inherited create;
2258 size:=ssize;
2259 Getmem(a,size*SizeOf(integer));
2260 end;
2261
2262 destructor TGosubStack.destroy;
2263 begin
2264 freemem(a,Size*SizeOf(integer));
2265 inherited destroy;
2266 end;
2267
2268 procedure TGosubStack.push(i:integer);
2269 begin
2270 if p>=size then SetExceptionWith('GOSUB Stack overflow',GosubStackOverflow);
2271 a^[p]:=i;
2272 inc(p);
2273 end;
2274
2275 function TGosubStack.pop:integer;
2276 begin
2277 if p=0 then SetException(10002);
2278 dec(p);
2279 result:=a^[p];
2280 end;
2281
2282 {**********}
2283 {Initialize}
2284 {**********}
2285
2286 procedure init(TextMode,GraphMode,UseCharInput:boolean; BMPSize1:TypBMPsize; VirtualStackSize:Cardinal);
2287 begin
2288 InitSeed; //2010/07/18
2289 InitVirtualStack(VirtualStackSize);
2290
2291 if GraphMode then
2292 CreateFormsG
2293 else
2294 CreateFormsT;
2295
2296 base.extype:=0;
2297 base.BMPSize:=BMPSize1;
2298 //CurrentOperation:=nil;
2299 statusmes.clear;
2300
2301 LocalPrinter:=TLocalPrinter.create;
2302
2303 console:=TConsole.create ;
2304 //PConsole.ttext:=console; //2008.11.3
2305 if textmode then
2306 textForm.Visible:=true;
2307
2308 PaintForm.Caption:=ChangeFileExt(TextForm.Caption,'.bmp');
2309 PaintForm.initial;
2310 InitGraphics;
2311 if graphmode {and (nextGraphmode=ScreenBitmapMode)} then
2312 begin
2313 PaintForm.Visible:=true;
2314 PaintForm.WindowState:=wsNormal;
2315 PaintForm.BringToFront;
2316 end
2317 else
2318 PaintForm.Visible:=false;
2319
2320
2321 if UseCharInput then
2322 begin
2323 charinput.init;
2324 //CharInput.Show
2325 end;
2326
2327 Set8087CW(controlWORD);
2328 asm
2329 mov initialESP,esp
2330 end;
2331 end;
2332
2333 procedure finish;
2334 begin
2335 MyGraphSys.finish;
2336 console.free;
2337 LocalPrinter.free;
2338 end;
2339
2340 initialization
2341 GosubStack:=TGosubStack.create(128);
2342 LogMaxExtended:=system.ln(MaxExtended);
2343 finalization
2344 GosubStack.free;
2345 end.
2346

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26