source: Include/basic/function.sbp@ 121

Last change on this file since 121 was 121, checked in by イグトランス (egtra), 17 years ago

#51対応

File size: 20.8 KB
RevLine 
[1]1'function.sbp
2
3
4#ifndef _INC_FUNCTION
5#define _INC_FUNCTION
6
7
8Const _System_PI = 3.14159265358979323846264
9Const _System_LOG2 = 0.6931471805599453094172321214581765680755
10Const _System_SQRT2 = 1.41421356237309504880168872421
11
12
[16]13#include <Classes/System/Math.ab>
14
15
[1]16'------------- サポート関数の定義 -------------
17
18Function ldexp(x As Double, n As Long) As Double
19 If x = 0 Then
20 ldexp = 0
21 Exit Function
22 End If
23 Dim pSrc As *QWord, pDest As *QWord
24 pSrc = VarPtr(x) As *QWord
25 pDest = VarPtr(ldexp) As *QWord
26
27 n += (pSrc[0] >> 52) As DWord And &h7FF
28
29 pDest[0] = n << 52 Or (pSrc[0] And &h800FFFFFFFFFFFFF)
30End Function
31
32Function frexp(x As Double, ByRef n As Long) As Double
33 If x = 0 Then
34 n = 0
35 frexp = 0
36 Exit Function
37 End If
38
39 Dim pSrc As *QWord, pDest As *QWord
40 pSrc = VarPtr(x) As *QWord
41 pDest = VarPtr(frexp) As *QWord
42 n = ((pSrc[0] >> 52) As DWord And &h7FF) - 1022
43 pDest[0] = (pSrc[0] And &h800FFFFFFFFFFFFF) Or &h3FE0000000000000
44End Function
45
46Function frexp(x As Single, ByRef n As Long) As Single
47 If x = 0 Then
48 n = 0
49 frexp = 0
50 Exit Function
51 End If
52
53 Dim pSrc As *DWord, pDest As *DWord
54 pSrc = VarPtr(x) As *DWord
55 pDest = VarPtr(frexp) As *DWord
56 n = ((pSrc[0] >> 23) And &hFF) - 126
57 pDest[0] = (pSrc[0] And &h807FFFFF) Or &h7E000000
58End Function
59
60Function ipow(x As Double, n As Long) As Double
61 Dim abs_n As Long
[10]62 Dim r = 1 As Double
[1]63
64 abs_n=Abs(n) As Long
65 While abs_n<>0
[10]66 If abs_n and 1 Then r *= x
[1]67 x = x * x
68 abs_n >>= 1 ' abs_n \= 2
69 Wend
70
71 If n>=0 Then
72 ipow=r
73 Else
74 ipow=1/r
75 End If
76End Function
77
78Function pow(x As Double, y As Double) As Double
79 If -LONG_MAX<=y and y<=LONG_MAX and y=CDbl(Int(y)) Then
80 pow=ipow(x,y As Long)
81 Exit Function
82 End If
83
84 If x>0 Then
85 pow=Exp(y*Log(x))
86 Exit Function
87 End If
88
89 If x<>0 or y<=0 Then
90 'error
91 End If
92
93 pow=0
94End Function
95
96#ifdef _WIN64
97
98Function _System_GetNaN() As Double
99 SetQWord(VarPtr(_System_GetNaN) As *QWord, &H7FF8000000000000)
100End Function
101
102Function _System_GetInf(sign As BOOL) As Double
103 Dim s = 0 As QWord
104 If sign Then s = 1 << 63
105 SetQWord(VarPtr(_System_GetInf) As *QWord, &h7FF0000000000000 Or s)
106End Function
107
108#else
109
110Function _System_GetNaN() As Double
111 Dim p As *DWord
112 p = VarPtr(_System_GetNaN) As *DWord
113 p[0] = 0
114 p[1] = &H7FF80000
115End Function
116
117Function _System_GetInf(sign As BOOL) As Double
118 Dim s = 0 As DWord
119 If sign Then s = (1 As DWord) << 31
120 Dim p As *DWord
121 p = VarPtr(_System_GetInf) As *DWord
122 p[0] = 0
123 p[1] = &h7FF00000 Or s
124End Function
125
126#endif
127
128' xの符号だけをyのものにした値を返す。
129' 引数 x 元となる絶対値
130' 引数 y 元となる符号
131Function CopySign(x As Double, y As Double) As Double
132 SetQWord(VarPtr(CopySign), (GetQWord(VarPtr(x)) And &h7fffffffffffffff) Or (GetQWord(VarPtr(y)) And &h8000000000000000))
133End Function
134
135Function CopySign(x As Single, y As Single) As Single
136 SetDWord(VarPtr(CopySign), (GetDWord(VarPtr(x)) And &h7fffffff) Or (GetDWord(VarPtr(y)) And &h80000000))
137End Function
138
139Function _System_SetSign(x As Double, isNegative As Long) As Double
140#ifdef _WIN64
[92]141 SetQWord(AddressOf(CopySign), (GetQWord(VarPtr(x)) And &h7fffffffffffffff) Or (isNegative << 63))
[1]142#else
[92]143 SetDWord(AddressOf(CopySign), GetDWord(VarPtr(x)))
144 SetDWord(AddressOf(CopySign) + SizeOf (DWord), GetQWord(VarPtr(x) + SizeOf (DWord)) And &h7fffffff Or (isNegative << 31))
[1]145#endif
146End Function
147
148Const RAND_MAX=&H7FFFFFFF
149Dim _System_RndNext=1 As DWord
150
151Function rand() As Long
152 _System_RndNext = _System_RndNext * 1103515245 + 12345
153 rand = _System_RndNext >> 1
154End Function
155
156Sub srand(dwSeek As DWord)
157 _System_RndNext = dwSeek
158End Sub
159
160
161'------------- ここからBasic標準関数の定義 -------------
162
163
164'------------------
165' データ型変換関数
166'------------------
167
168Function CDbl(number As Double) As Double
169 CDbl=number
170End Function
171
172Function _CUDbl(number As QWord) As Double
173 _CUDbl=number As Double
174End Function
175
176Function CDWord(num As Double) As DWord
[92]177 CDWord=num As DWord
[1]178End Function
179
180Function CInt(number As Double) As Long
[92]181 CInt=number As Long
[1]182End Function
183
184Function CSng(number As Double) As Single
[92]185 CSng=number As Single
[1]186End Function
187
188#ifdef _WIN64
189Function Fix(number As Double) As Long
190 Fix=number As Long
191End Function
192#else
193'Fix関数はコンパイラに組み込まれている
194'Function Fix(number As Double) As Long
195#endif
196
197Function Int(number As Double) As Long
198 Int=Fix(number)
199 If number<0 Then
200 If number<Fix(number) Then Int=Int-1
201 End If
202End Function
203
204
205'-------------------------------------
206' ポインタ関数(コンパイラに組み込み)
207'-------------------------------------
208
209'Function GetDouble(p As DWord) As Double
210'Function GetSingle(p As DWord) As Single
211'Function GetDWord(p As DWord) As DWord
212'Function GetWord(p As DWord) As Word
213'Function GetByte(p As DWord) As Byte
214'Sub SetDouble(p As DWord, dblData As Double)
215'Sub SetSingle(p As DWord, fltData As Single)
216'Sub SetDWord(p As DWord, dwData As DWord)
217'Sub SetWord(p As DWord, wData As Word)
218'Sub SetByte(p As DWord, byteData As Byte)
219
220
221'----------
222' 算術関数
223'----------
224
225Function Abs(number As Double) As Double
226 'Abs = Math.Abs(number)
227 If number < 0 then
228 return -number
229 Else
230 return number
231 End If
232End Function
233
234Function Exp(x As Double) As Double
235 Exp = Math.Exp(x)
236End Function
237
238Function Log(x As Double) As Double
239 Log = Math.Log(x)
240End Function
241
242Function Sgn(number As Double) As Long
243 Sgn = Math.Sign(number)
244End Function
245
246Function Sqr(number As Double) As Double
247 Sqr = Math.Sqrt(number)
248End Function
249
250Function Atn(number As Double) As Double
251 Atn = Math.Atan(number)
252End Function
253
254Function _Support_tan(x As Double, ByRef k As Long) As Double
255 Dim i As Long
256 Dim t As Double, x2 As Double
257
258 If x>=0 Then
259 k=Fix(x/(_System_PI/2)+0.5)
260 Else
261 k=Fix(x/(_System_PI/2)-0.5)
262 End If
263
264 x=(x-(CDbl(3217)/CDbl(2048))*k)+4.4544551033807686783083602485579e-6*k
265
266 x2=x*x
267 t=0
268
269 For i=19 To 3 Step -2
270 t=x2/(i-t)
271 Next
272
273 _Support_tan=x/(1-t)
274End Function
275
276Function Atn2(y As Double, x As Double) As Double
[10]277 Atn2 = Math.Atan2(y, x)
[1]278End Function
279
280Function Sin(number As Double) As Double
[94]281 Sin = Math.Sin(number)
[1]282End Function
283
284Function Cos(number As Double) As Double
285 Cos = Math.Cos(number)
286End Function
287
288Function Tan(number As Double) As Double
289 Tan = Math.Tan(number)
290End Function
291
[121]292Function IsNaN(ByVal x As Double) As Boolean
[1]293 Dim p As *DWord
294 p = VarPtr(x) As *DWord
295 IsNaN = FALSE
296 If (p[1] And &H7FF00000) = &H7FF00000 Then
297 If (p[0] <> 0) Or ((p[1] And &HFFFFF) <> 0) Then
[121]298 IsNaN = True
[1]299 End If
300 End If
301
302' IsNaN=FALSE
303End Function
304
[121]305Function IsInf(x As Double) As Boolean
[1]306 Dim p As *DWord, nan As Double
307 p = VarPtr(x) As *DWord
308 p[1] And= &h7fffffff
309 nan = _System_GetInf(FALSE)
310 IsInf = (memcmp(p As *Byte, VarPtr(nan), SizeOf (Double)) = 0)
311End Function
312
[121]313Function IsNaNOrInf(x As Double) As Boolean
[1]314 IsNaNOrInf = IsFinite(x)
315End Function
316
[121]317Function IsFinite(x As Double) As Boolean
[1]318 Dim p As *DWord, nan As Double
319 p = VarPtr(x) As *DWord
320' p[1] And= &h7ffe0000
321 p[1] And= &H7FF00000
322 p[0] = 0
323 nan = _System_GetInf(/*x,*/ FALSE)
[121]324 IsFinite = (memcmp(p As BytePtr, VarPtr(nan), SizeOf (Double)) = 0)
[1]325End Function
326
327Const RAND_UNIT = (1.0 / (LONG_MAX + 1.0))
328Function Rnd() As Double
329 Rnd = RAND_UNIT * rand()
330End Function
331
332Const HIBYTE(w) = (((w As Word) >> 8) and &HFF) As Byte
333Const LOBYTE(w) = ((w As Word) and &HFF) As Byte
334Const HIWORD(dw) = (((dw As DWord) >> 16) and &HFFFF) As Word
335Const LOWORD(dw) = ((dw As DWord) and &HFFFF) As Word
336
337Const MAKEWORD(a,b) = (((a As Word) and &HFF) or (((b As Word) and &HFF)<<8)) As Word
338Const MAKELONG(a,b) = (((a As DWord) and &HFFFF) or (((b As DWord) and &HFFFF)<<16)) As Long
339
340
341
342'------------
343' 文字列関数
344'------------
345
[110]346Function Asc(buf As String) As Char
[1]347 Asc = buf[0]
348End Function
349
[110]350Function Chr$(code As Char) As String
[121]351 Chr$ = ZeroString(1)
352 Chr$[0] = code
[1]353End Function
354
[121]355#ifdef UNICODE
356Function AscW(s As String) As UCSCHAR
357 If s.Length = 0 Then
358 AscW = 0
359 Else
360 If _System_IsSurrogatePair(s[0], s[1]) Then
361 AscW = ((s[0] And &h3FF) As DWord << 10) Or (s[1] And &h3FF)
362 Else
363 AscW = s[0]
364 End If
365 End If
366End Function
367
368Function ChrW(c As UCSCHAR) As String
369 If c <= &hFFFF Then
370 ChrW.ReSize(1)
371 ChrW[0] = c As WCHAR
372 ElseIf c < &h10FFFF Then
373 ChrW.ReSize(2)
374 ChrW[0] = &hD800 Or (c >> 10)
375 ChrW[1] = &hDC00 Or (c And &h3FF)
376 Else
377 ' OutOfRangeException
378 End If
379End Function
380#endif
381
[1]382Function Date$() As String
383 Dim st As SYSTEMTIME
384 GetLocalTime(st)
385
386 'year
387 Date$=Str$(st.wYear)
388
389 'month
390 If st.wMonth<10 Then
391 Date$=Date$+"/0"
392 Else
393 Date$=Date$+"/"
394 End If
395 Date$=Date$+Str$(st.wMonth)
396
397 'day
398 If st.wDay<10 Then
399 Date$=Date$+"/0"
400 Else
401 Date$=Date$+"/"
402 End If
403 Date$=Date$+Str$(st.wDay)
404End Function
405
[121]406Dim _System_HexadecimalTable[&h10] = [&h30, &h31, &h32, &h33, &h34, &h35, &h36, &h37, &h38, &h39, &h41, &h42, &h43, &h44, &h45, &h46] As Byte
407
408Function Hex$(x As DWord) As String
409 Dim i = 0
410 Hex$ = ZeroString(8)
411 While (x And &hf0000000) = 0
412 x <<= 4
413 Wend
414 While x <> 0
415 Hex$[i] = _System_HexadecimalTable[(x And &hf0000000) >> 28] As Char
416 x <<= 4
417 i++
418 Wend
419 Hex$.ReSize(i)
[1]420End Function
421
[121]422Function Hex$(x As QWord) As String
423 Hex$ = Hex$((x >> 32) As DWord) + Hex$((x And &hffffffff) As DWord)
[1]424End Function
425
426Function InStr(StartPos As Long, buf1 As String, buf2 As String) As Long
427 Dim len1 As Long, len2 As Long, i As Long, i2 As Long, i3 As Long
428
429 len1=Len(buf1)
430 len2=Len(buf2)
431
432 If len2=0 Then
433 InStr=StartPos
434 Exit Function
435 End If
436
[10]437 StartPos--
[1]438 If StartPos<0 Then
439 'error
440 InStr=0
441 Exit Function
442 End If
443
444 i=StartPos:InStr=0
445 While i<=len1-len2
446 i2=i:i3=0
447 Do
448 If i3=len2 Then
449 InStr=i+1
450 Exit Do
451 End If
452 If buf1[i2]<>buf2[i3] Then Exit Do
453
[10]454 i2++
455 i3++
[1]456 Loop
457 If InStr Then Exit While
[10]458 i++
[1]459 Wend
460End Function
461
462Function Left$(buf As String, length As Long) As String
[121]463 Left$ = ZeroString(length)
464 memcpy(StrPtr(Left$), StrPtr(buf), SizeOf (Char) * length)
[1]465End Function
466
467Function Mid$(buf As String, StartPos As Long)(ReadLength As Long) As String
468 Dim length As Long
469
[10]470 StartPos--
[1]471 If StartPos<0 Then
472 'error
473 'Debug
474 Exit Function
475 End If
476
477 length=Len(buf)
478 If length<=StartPos Then Exit Function
479
480 If ReadLength=0 Then
481 ReadLength=length-StartPos
482 End If
483
484 If ReadLength>length-StartPos Then
485 ReadLength=length-StartPos
486 End If
487
488 Mid$=ZeroString(ReadLength)
[121]489 memcpy(StrPtr(Mid$), VarPtr(buf[StartPos]), SizeOf (Char) * ReadLength)
[1]490End Function
491
492Function Oct$(num As DWord) As String
493 Dim i As DWord, i2 As DWord
494
495 For i=10 To 1 Step -1
496 If (num\CDWord(8^i)) And &H07 Then
497 Exit For
498 End If
499 Next
500
501 Oct$=ZeroString(i+1)
502 i2=0
503 Do
504 Oct$[i2]=Asc("0")+((num\CDWord(8^i)) And &H07)
505 If i=0 Then Exit Do
506 i--
507 i2++
508 Loop
509End Function
510
511Function Right$(buf As String, length As Long) As String
512 Dim i As Long
513
514 i=Len(buf)
515 If i>length Then
516 Right$=ZeroString(length)
[121]517 memcpy(StrPtr(Right$), VarPtr(buf[i-length]), SizeOf (Char) * length)
[1]518 Else
519 Right$=buf
520 End If
521End Function
522
523Function Space$(length As Long) As String
524 Space$=ZeroString(length)
525 FillMemory(StrPtr(Space$),length,&H20)
526End Function
527
[110]528Dim _System_ecvt_buffer[16] As Char
[1]529Sub _ecvt_support(count As Long)
530 Dim i As Long
531 If _System_ecvt_buffer[count]=9 Then
532 _System_ecvt_buffer[count]=0
533 If count=0 Then
534 For i=16 To 1 Step -1
535 _System_ecvt_buffer[i]=_System_ecvt_buffer[i-1]
536 Next
537 _System_ecvt_buffer[0]=1
538 Else
539 _ecvt_support(count-1)
540 End If
541 Else
[110]542 _System_ecvt_buffer[count]=_System_ecvt_buffer[count]+1 As Char
[1]543 End If
544End Sub
[119]545Function _ecvt(value As Double, count As Long, ByRef dec As Long, ByRef sign As Long) As *Char
546 Dim temp As *Char
[1]547 Dim i As Long, i2 As Long
548
549 _ecvt=_System_ecvt_buffer
550
551 '値が0の場合
552 If value=0 Then
553 FillMemory(_System_ecvt_buffer,count,&H30)
554 _System_ecvt_buffer[count]=0
555 dec=0
556 sign=0
557 Exit Function
558 End If
559
560 '符号の判断(同時に符号を取り除く)
561 If value<0 Then
562 sign=1
563 value=-value
564 Else
565 sign=0
566 End If
567
568 '正規化
569 dec=1
570 While value<0.999999999999999 'value<1
[119]571 value *= 10
572 dec--
[1]573 Wend
574 While 9.99999999999999<=value '10<=value
[119]575 value /= 10
576 dec++
[1]577 Wend
578
579 For i=0 To count-1
[110]580 _System_ecvt_buffer[i]=Int(value) As Char
[1]581
582 value=(value-CDbl(Int(value)))*10
583 Next
584 _System_ecvt_buffer[i]=0
585
[119]586 i--
[1]587 If value>=5 Then
588 '切り上げ処理
589 _ecvt_support(i)
590 End If
591
592 For i=0 To count-1
[119]593 _System_ecvt_buffer[i] += &H30
[1]594 Next
595 _System_ecvt_buffer[i]=0
596End Function
597
598Function Str$(dbl As Double) As String
599 If IsNaN(dbl) Then
600 Return "NaN"
601 ElseIf IsInf(dbl) Then
602 If dbl > 0 Then
603 Return "Infinity"
604 Else
605 Return "-Infinity"
606 End If
607 End If
608 Dim dec As Long, sign As Long
[110]609 Dim buffer[32] As Char, temp As *Char
[1]610 Dim i As Long, i2 As Long, i3 As Long
611
612 '浮動小数点を文字列に変換
613 temp=_ecvt(dbl,15,dec,sign)
614
615 i=0
616
617 '符号の取り付け
618 If sign Then
619 buffer[i]=Asc("-")
[10]620 i++
[1]621 End If
622
623 If dec>15 Then
624 '指数表示(桁が大きい場合)
625 buffer[i]=temp[0]
[10]626 i++
[1]627 buffer[i]=Asc(".")
[10]628 i++
[1]629 memcpy(buffer+i,temp+1,14)
[10]630 i+=14
[1]631 buffer[i]=Asc("e")
[10]632 i++
[1]633 wsprintf(buffer+i,"+%03d",dec-1)
634
635 Return MakeStr(buffer)
636 End If
637
638 If dec<-3 Then
639 '指数表示(桁が小さい場合)
640 buffer[i]=temp[0]
[10]641 i++
[1]642 buffer[i]=Asc(".")
[10]643 i++
[1]644 memcpy(buffer+i,temp+1,14)
[10]645 i+=14
[1]646 buffer[i]=Asc("e")
[10]647 i++
[1]648 wsprintf(buffer+i,"%03d",dec-1)
649
650 Return MakeStr(buffer)
651 End If
652
653 '整数部
654 i2=dec
655 i3=0
656 If i2>0 Then
657 While i2>0
658 buffer[i]=temp[i3]
[10]659 i++
660 i3++
661 i2--
[1]662 Wend
663 buffer[i]=Asc(".")
[10]664 i++
[1]665 Else
666 buffer[i]=&H30
[10]667 i++
[1]668 buffer[i]=Asc(".")
[10]669 i++
[1]670
671 i2=dec
672 While i2<0
673 buffer[i]=&H30
[10]674 i++
675 i2++
[1]676 Wend
677 End If
678
679 '小数部
680 While i3<15
681 buffer[i]=temp[i3]
[10]682 i++
683 i3++
[1]684 Wend
685
686 While buffer[i-1]=&H30
[10]687 i--
[1]688 Wend
[10]689 If buffer[i-1]=Asc(".") Then i--
[1]690
691 buffer[i]=0
692 Return MakeStr(buffer)
693End Function
694Function Str$(value As LONG_PTR) As String
[110]695 Dim temp[255] As Char
[121]696#ifdef _WIN64
697 _sntprintf(temp, Len (temp) / SizeOf (Char), "%I64d", value)
698#else
699 _sntprintf(temp, Len (temp) / SizeOf (Char), "%d", value)
700#endif
701 Str$ = temp
[1]702End Function
703
704Function String$(num As Long, buf As String) As String
705 Dim dwStrPtr As DWord
706 Dim length As Long
707
708 length=Len(buf)
709
710 'バッファ領域を確保
711 String$=ZeroString(length*num)
712
713 '文字列をコピー
714 Dim i As Long
715 For i=0 To num-1
[121]716 memcpy(VarPtr(String$[i*length]),StrPtr(buf),SizeOf (Char) * length)
[1]717 Next
718End Function
719
720Function Time$() As String
721 Dim st As SYSTEMTIME
722
723 GetLocalTime(st)
724
725 'hour
726 If st.wHour<10 Then
727 Time$="0"
728 End If
729 Time$=Time$+Str$(st.wHour)
730
731 'minute
732 If st.wMinute<10 Then
733 Time$=Time$+":0"
734 Else
735 Time$=Time$+":"
736 End If
737 Time$=Time$+Str$(st.wMinute)
738
739 'second
740 If st.wSecond<10 Then
741 Time$=Time$+":0"
742 Else
743 Time$=Time$+":"
744 End If
745 Time$=Time$+Str$(st.wSecond)
746End Function
747
[119]748Function Val(buf As *Char) As Double
[1]749 Dim i As Long, i2 As Long, i3 As Long, i4 As Long
750 Dim temporary As String
[119]751 Dim TempPtr As *Char
[1]752 Dim dbl As Double
753 Dim i64data As Int64
754
755 Val=0
756
757 While buf[0]=Asc(" ") or buf[0]=Asc(Ex"\t")
758 buf++
759 Wend
760
761 If buf[0]=Asc("&") Then
[121]762 temporary=buf
[1]763 TempPtr=StrPtr(temporary)
764 CharUpper(TempPtr)
765 If TempPtr(1)=Asc("O") Then
766 '8進数
767 i=2
768 While 1
769 '数字以外の文字の場合は抜け出す
770 i3=TempPtr[i]-&H30
771 If Not (0<=i3 And i3<=7) Then Exit While
772
[110]773 TempPtr[i]=i3 As Char
[1]774 i++
775 Wend
776 i--
777
778 i64data=1
779 While i>=2
780 Val=Val+i64data*TempPtr[i]
781
782 i64data=i64data*&O10
[10]783 i--
[1]784 Wend
785 ElseIf TempPtr(1)=Asc("H") Then
786 '16進数
787 i=2
788 While 1
789 '数字以外の文字の場合は抜け出す
790 i3=TempPtr[i]-&H30
791 If Not(0<=i3 and i3<=9) Then
792 i3=TempPtr[i]-&H41+10
793 If Not(&HA<=i3 and i3<=&HF) Then Exit While
794 End If
795
[110]796 TempPtr[i]=i3 As Char
[1]797 i++
798 Wend
799 i--
800
801 i64data=1
802 While i>=2
[22]803 Val += (i64data*TempPtr[i]) As Double
[1]804
[10]805 i64data *= &H10
[1]806 i--
807 Wend
808 End If
809 Else
810 '10進数
811 If buf[0]=&H2D Then
812 'マイナス値
813 i4=1
814 buf++
815 Else
816 'プラス値
817 i4=0
818 If buf[0]=&H2B Then
819 buf++
820 End If
821 End If
822
823 i=0
824
825 While 1
826 '数字以外の文字の場合は抜け出す
827 i3=buf[i]-&H30
828 If Not (0<=i3 And i3<=9) Then Exit While
829
830 i++
831 Wend
832
833 '整数部
834 dbl=1
835 i3=i-1
836 While i3>=0
[10]837 Val += dbl*(buf[i3]-&H30)
[1]838
[10]839 dbl *= 10
[1]840 i3--
841 Wend
842
843 If buf[i]=Asc(".") Then
844 '小数部
845 i++
846 dbl=10
847 While 1
848 '数字以外の文字の場合は抜け出す
849 i3=buf[i]-&H30
850 If Not (0<=i3 And i3<=9) Then Exit While
851
852 Val += (buf[i] - &H30) / dbl
853 dbl *= 10
854 i++
855 Wend
856 End If
857
858 If i4 Then Val=-Val
859 End If
860End Function
861
862
863'--------------
864' ファイル関数
865'--------------
866
867Function Eof(FileNum As Long) As Long
868 Dim dwCurrent As DWord, dwEnd As DWord
869
870 FileNum--
871
872 dwCurrent=SetFilePointer(_System_hFile(FileNum),0,NULL,FILE_CURRENT)
873 dwEnd=SetFilePointer(_System_hFile(FileNum),0,NULL,FILE_END)
874 SetFilePointer(_System_hFile(FileNum),dwCurrent,NULL,FILE_BEGIN)
875
876 If dwCurrent>=dwEnd Then
877 Eof=-1
878 Else
879 Eof=0
880 End If
881End Function
882
883Function Lof(FileNum As Long) As Long
884 Lof=GetFileSize(_System_hFile(FileNum-1),NULL)
885End Function
886
887Function Loc(FileNum As Long) As Long
888 Dim NowPos As Long, BeginPos As Long
889
[10]890 FileNum--
[1]891
892 NowPos=SetFilePointer(_System_hFile(FileNum),0,NULL,FILE_CURRENT)
893 BeginPos=SetFilePointer(_System_hFile(FileNum),0,NULL,FILE_BEGIN)
894 SetFilePointer(_System_hFile(FileNum),NowPos-BeginPos,NULL,FILE_BEGIN)
895
896 Loc=NowPos-BeginPos
897End Function
898
899
900'------------------
901' メモリ関連の関数
902'------------------
903
904Function malloc(stSize As SIZE_T) As VoidPtr
905 Return _System_GC.__malloc(stSize,_System_GC_FLAG_NEEDFREE)
906End Function
907
908Function calloc(stSize As SIZE_T) As VoidPtr
909 Return _System_GC.__malloc(stSize,_System_GC_FLAG_NEEDFREE or _System_GC_FLAG_INITZERO)
910End Function
911
912Function realloc(lpMem As VoidPtr, stSize As SIZE_T) As VoidPtr
913 If lpMem = 0 Then
914 Return malloc(stSize)
915 Else
916 Return _System_GC.__realloc(lpMem,stSize)
917 End If
918End Function
919
920Sub free(lpMem As VoidPtr)
921 _System_GC.__free(lpMem)
922End Sub
923
924
925Function _System_malloc(stSize As SIZE_T) As VoidPtr
926 Return HeapAlloc(_System_hProcessHeap,0,stSize)
927End Function
928
929Function _System_calloc(stSize As SIZE_T) As VoidPtr
930 Return HeapAlloc(_System_hProcessHeap,HEAP_ZERO_MEMORY,stSize)
931End Function
932
933Function _System_realloc(lpMem As VoidPtr, stSize As SIZE_T) As VoidPtr
934 If lpMem = 0 Then
935 Return HeapAlloc(_System_hProcessHeap, 0, stSize)
936 Else
937 Return HeapReAlloc(_System_hProcessHeap, 0, lpMem, stSize)
938 End If
939End Function
940
941Sub _System_free(lpMem As VoidPtr)
942 HeapFree(_System_hProcessHeap,0,lpMem)
943End Sub
944
945
946'--------
947' その他
948'--------
949
950Sub _splitpath(path As BytePtr, drive As BytePtr, dir As BytePtr, fname As BytePtr, ext As BytePtr)
951 Dim i As Long, i2 As Long, i3 As Long, length As Long
[110]952 Dim buffer[MAX_PATH] As Char
[1]953
954 '":\"をチェック
955 If not(path[1]=&H3A and path[2]=&H5C) Then Exit Sub
956
957 'ドライブ名をコピー
958 If drive Then
959 drive[0]=path[0]
960 drive[1]=path[1]
961 drive[2]=0
962 End If
963
964 'ディレクトリ名をコピー
965 i=2
966 i2=0
967 Do
[110]968#ifdef UNICODE
969' ToDo: サロゲートペアの認識
970#else
[1]971 If IsDBCSLeadByte(path[i])=TRUE and path[i+1]<>0 Then
972 If dir Then
973 dir[i2]=path[i]
974 dir[i2+1]=path[i+1]
975 End If
976
[10]977 i += 2
978 i2 += 2
[1]979 Continue
980 End If
[110]981#endif
[1]982
983 If path[i]=0 Then Exit Do
984
985 If path[i]=&H5C Then '"\"記号であるかどうか
986 i3=i2+1
987 End If
988
989 If dir Then dir[i2]=path[i]
990
[10]991 i++
992 i2++
[1]993 Loop
994 If dir Then dir[i3]=0
[22]995 i3 += i-i2
[1]996
997 'ファイル名をコピー
998 i=i3
999 i2=0
1000 i3=-1
1001 Do
1002 If IsDBCSLeadByte(path[i])=TRUE and path[i+1]<>0 Then
1003 If fname Then
1004 fname[i2]=path[i]
1005 fname[i2+1]=path[i+1]
1006 End If
1007
[10]1008 i += 2
1009 i2 += 2
[1]1010 Continue
1011 End If
1012
1013 If path[i]=0 Then Exit Do
1014
1015 If path[i]=&H2E Then '.'記号であるかどうか
1016 i3=i2
1017 End If
1018
1019 If fname Then fname[i2]=path[i]
1020
[10]1021 i++
1022 i2++
[1]1023 Loop
1024 If i3=-1 Then i3=i2
1025 If fname Then fname[i3]=0
[10]1026 i3 += i-i2
[1]1027
1028 '拡張子名をコピー
1029 If ext Then
1030 If i3 Then
1031 lstrcpy(ext,path+i3)
1032 End If
1033 else ext[0]=0
1034 End If
1035End Sub
1036
1037Function GetBasicColor(ColorCode As Long) As Long
1038 Select Case ColorCode
1039 Case 0
1040 GetBasicColor=RGB(0,0,0)
1041 Case 1
1042 GetBasicColor=RGB(0,0,255)
1043 Case 2
1044 GetBasicColor=RGB(255,0,0)
1045 Case 3
1046 GetBasicColor=RGB(255,0,255)
1047 Case 4
1048 GetBasicColor=RGB(0,255,0)
1049 Case 5
1050 GetBasicColor=RGB(0,255,255)
1051 Case 6
1052 GetBasicColor=RGB(255,255,0)
1053 Case 7
1054 GetBasicColor=RGB(255,255,255)
1055 End Select
1056End Function
1057
[119]1058Function _System_IsSurrogatePair(wcHigh As WCHAR, wcLow As WCHAR) As Boolean
1059 If &hD800 <= wcHigh And wcHigh < &hDC00 Then
1060 If &hDC00 <= wcLow And wcLow < &hE000 Then
1061 Return True
1062 End If
1063 End If
1064 Return False
1065End Function
[1]1066
1067#endif '_INC_FUNCTION
Note: See TracBrowser for help on using the repository browser.