source: trunk/Include/basic/function.sbp@ 388

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

Stringなどで例外を投げるようにした。
#147の解決。
CType ASCII文字判定関数群の追加。

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