source: Include/basic/function.sbp@ 258

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

Prompt.sbp内を名前空間に入れた。EnvironmentのMachineName, UserName, GetFolderPathを実装。

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