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

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

_System_CThreadCollectionでのクラスインスタンスへのポインタの使用を除去、参照変数構文へ。

File size: 21.0 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
[394]245Function Asc(buf As String) 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
[394]255 If String.IsNullOrEmpty(s) Then
[121]256 AscW = 0
[394]257 'ArgumentNullExceptionに変えるかも
[121]258 Else
[394]259 If _System_IsHighSurrogate(s[0]) Then
260 '有効なサロゲートペアになっていない場合には、
261 '例外を投げるようにしたほうがよいかもしれない。
262 If s.Length > 1 Then
263 If _System_IsLowSurrogate(s[0]) Then
264 AscW = ((s[0] And &h3FF) As DWord << 10) Or (s[1] And &h3FF)
[400]265 AscW += &h10000
[394]266 Exit Function
267 End If
268 End If
[121]269 Else
270 AscW = s[0]
271 End If
272 End If
273End Function
274
275Function ChrW(c As UCSCHAR) As String
276 If c <= &hFFFF Then
[272]277 Return New String(c As StrChar, 1)
[394]278 ElseIf c <= &h10FFFF Then
[400]279 c -= &h10000
[394]280 Dim t[1] As WCHAR
281 t[0] = (&hD800 Or (c >> 10)) As WCHAR
282 t[1] = (&hDC00 Or (c And &h3FF)) As WCHAR
[272]283 Return New String(t, 2)
[121]284 Else
[394]285 Throw New System.ArgumentOutOfRangeException("ChrW: c is invalid Unicode code point.", "c")
[121]286 End If
287End Function
288#endif
289
[1]290Function Date$() As String
[289]291 Dim date = System.DateTime.Now
[272]292 Dim buf = New System.Text.StringBuilder(10)
[1]293
294 'year
[272]295 buf.Append(date.Year)
[1]296
297 'month
[272]298 If date.Month < 10 Then
299 buf.Append("/0")
[1]300 Else
[272]301 buf.Append("/")
[1]302 End If
[272]303 buf.Append(date.Month)
[1]304
305 'day
[272]306 If date.Day < 10 Then
307 buf.Append("/0")
[1]308 Else
[272]309 buf.Append("/")
[1]310 End If
[272]311 buf.Append(date.Day)
312
313 Date$ = buf.ToString
[1]314End Function
315
[167]316Function Hex$(x As DWord) As String
[388]317 Imports ActiveBasic.Strings.Detail
318 Hex$ = FormatIntegerX(x, 1, 0, None)
[167]319End Function
320
[121]321Function Hex$(x As QWord) As String
[388]322 Imports ActiveBasic.Strings.Detail
323 Hex$ = FormatIntegerLX(x, 1, 0, None)
[1]324End Function
325
326Function InStr(StartPos As Long, buf1 As String, buf2 As String) As Long
[167]327 Dim i As Long, i2 As Long, i3 As Long
[1]328
[167]329 Dim len1 = buf1.Length
330 Dim len2 = buf2.Length
[1]331
332 If len2=0 Then
333 InStr=StartPos
334 Exit Function
335 End If
336
[10]337 StartPos--
[1]338 If StartPos<0 Then
339 'error
340 InStr=0
341 Exit Function
342 End If
343
344 i=StartPos:InStr=0
345 While i<=len1-len2
346 i2=i:i3=0
347 Do
348 If i3=len2 Then
349 InStr=i+1
350 Exit Do
351 End If
352 If buf1[i2]<>buf2[i3] Then Exit Do
353
[10]354 i2++
355 i3++
[1]356 Loop
357 If InStr Then Exit While
[10]358 i++
[1]359 Wend
360End Function
361
[272]362Function Left$(s As String, length As Long) As String
363 Left$ = s.Substring(0, System.Math.Min(s.Length, length))
[1]364End Function
365
[272]366Function Mid$(s As String, startPos As Long) As String
367 startPos--
368 Mid$ = s.Substring(startPos)
369End Function
[1]370
[272]371Function Mid$(s As String, startPos As Long, readLength = 0 As Long) As String
372 startPos--
373 Dim length = s.Length
374 Mid$ = s.Substring(System.Math.Min(startPos, length), System.Math.Min(readLength, length - startPos))
[1]375End Function
376
[272]377Function Oct$(n As QWord) As String
[388]378 Imports ActiveBasic.Strings.Detail
379 Oct$ = FormatIntegerLO(n, 1, 0, None)
[272]380End Function
[1]381
[272]382Function Oct$(n As DWord) As String
[388]383 Imports ActiveBasic.Strings.Detail
384 Oct$ = FormatIntegerO(n, 1, 0, None)
[1]385End Function
386
[272]387Function Right$(s As String, length As Long) As String
388 Right$ = s.Substring(System.Math.Max(0, s.Length - length), s.Length)
[1]389End Function
390
391Function Space$(length As Long) As String
[208]392 Return New String(&h20 As StrChar, length)
[1]393End Function
394
[385]395Sub _ecvt_support(buf As *StrChar, count As Long, size As Long)
[1]396 Dim i As Long
[385]397 If buf[count] = 9 Then
398 buf[count] = 0
399 If count = 0 Then
400 For i = size To 1 Step -1
401 buf[i] = buf[i-1]
[1]402 Next
[385]403 buf[0] = 1
[1]404 Else
[385]405 _ecvt_support(buf, count-1, size)
[1]406 End If
407 Else
[385]408 buf[count]++
[1]409 End If
410End Sub
[385]411
412Sub _ecvt(buffer As *StrChar, value As Double, count As Long, ByRef dec As Long, ByRef sign As Boolean)
[1]413 Dim i As Long, i2 As Long
414
415 '値が0の場合
[167]416 If value = 0 Then
[385]417 ActiveBasic.Strings.ChrFill(buffer, count As SIZE_T, &h30 As StrChar)
418 buffer[count] = 0
[123]419 dec = 0
420 sign = 0
[1]421 Exit Function
422 End If
423
424 '符号の判断(同時に符号を取り除く)
[167]425 If value < 0 Then
[385]426 sign = True
[167]427 value = -value
[1]428 Else
[385]429 sign = False
[1]430 End If
431
432 '正規化
[167]433 dec = 1
434 While value < 0.999999999999999 'value<1
[119]435 value *= 10
436 dec--
[1]437 Wend
[167]438 While 9.99999999999999 <= value '10<=value
[119]439 value /= 10
440 dec++
[1]441 Wend
442
[385]443 For i = 0 To count - 1
444 buffer[i] = Int(value) As StrChar
[167]445 value = (value-CDbl(Int(value))) * 10
[1]446 Next
447
[119]448 i--
[167]449 If value >= 5 Then
[1]450 '切り上げ処理
[385]451 _ecvt_support(buffer, i, count)
[1]452 End If
453
[385]454 For i = 0 To count - 1
455 buffer[i] += &H30
[1]456 Next
[385]457 buffer[i] = 0
458End Sub
[1]459
460Function Str$(dbl As Double) As String
[388]461 Imports ActiveBasic.Math
462 Imports ActiveBasic.Strings
463 If IsNaN(dbl) Then
[1]464 Return "NaN"
[388]465 ElseIf IsInf(dbl) Then
[1]466 If dbl > 0 Then
467 Return "Infinity"
468 Else
469 Return "-Infinity"
470 End If
471 End If
[385]472 Dim dec As Long, sign As Boolean
473 Dim buffer[32] As StrChar, temp[15] As StrChar
474 Dim i = 0 As Long
[1]475
476 '浮動小数点を文字列に変換
[385]477 _ecvt(temp, dbl, 15, dec, sign)
[1]478
479 '符号の取り付け
480 If sign Then
[167]481 buffer[i] = Asc("-")
[10]482 i++
[1]483 End If
484
[385]485 If dec > 15 Or dec < -3 Then
486 '指数表示
[167]487 buffer[i] = temp[0]
[10]488 i++
[167]489 buffer[i] = Asc(".")
[10]490 i++
[388]491 ChrCopy(VarPtr(buffer[i]), VarPtr(temp[1]), 14 As SIZE_T)
[167]492 i += 14
[385]493 buffer[i] = 0
[388]494 Return MakeStr(buffer) + SPrintf("e%+03d", New System.Int32(dec - 1))
[1]495 End If
496
497 '整数部
[385]498 Dim i2 = dec
499 Dim i3 = 0
[1]500 If i2>0 Then
501 While i2>0
502 buffer[i]=temp[i3]
[10]503 i++
504 i3++
505 i2--
[1]506 Wend
507 buffer[i]=Asc(".")
[10]508 i++
[1]509 Else
510 buffer[i]=&H30
[10]511 i++
[1]512 buffer[i]=Asc(".")
[10]513 i++
[1]514
515 i2=dec
516 While i2<0
517 buffer[i]=&H30
[10]518 i++
519 i2++
[1]520 Wend
521 End If
522
523 '小数部
524 While i3<15
525 buffer[i]=temp[i3]
[10]526 i++
527 i3++
[1]528 Wend
529
530 While buffer[i-1]=&H30
[10]531 i--
[1]532 Wend
[10]533 If buffer[i-1]=Asc(".") Then i--
[1]534
535 buffer[i]=0
536 Return MakeStr(buffer)
537End Function
[167]538
[385]539Function Str$(x As Int64) As String
540 Imports ActiveBasic.Strings.Detail
541 Return FormatIntegerEx(TraitsIntegerD[1], x As QWord, 1, 0, None)
[1]542End Function
543
[269]544Function Str$(x As QWord) As String
[385]545 Imports ActiveBasic.Strings.Detail
546 Return FormatIntegerEx(TraitsIntegerU[1], x, 1, 0, None)
[269]547End Function
548
549Function Str$(x As Long) As String
[385]550 Imports ActiveBasic.Strings.Detail
551 Return FormatIntegerEx(TraitsIntegerD[0], x, 1, 0, None)
[269]552End Function
553
554Function Str$(x As DWord) As String
[385]555 Imports ActiveBasic.Strings.Detail
556 Return FormatIntegerEx(TraitsIntegerU[0], x, 1, 0, None)
[269]557End Function
558
559Function Str$(x As Word) As String
[385]560 Return Str$(x As DWord)
[269]561End Function
562
563Function Str$(x As Integer) As String
[385]564 Return Str$(x As Long)
[269]565End Function
566
567Function Str$(x As Byte) As String
[385]568 Return Str$(x As DWord)
[269]569End Function
570
571Function Str$(x As SByte) As String
[385]572 Return Str$(x As Long)
[269]573End Function
574
575Function Str$(x As Single) As String
576 Return Str$(x As Double)
577End Function
578
579Function Str$(b As Boolean) As String
580 If b Then
581 Return "True"
582 Else
583 Return "False"
584 End If
585End Function
586
[272]587Function String$(n As Long, s As StrChar) As String
588 Return New String(s, n)
589End Function
590
591#ifdef _AB4_COMPATIBILITY_STRING$_
592Function String$(n As Long, s As String) As String
593 If n < 0 Then
594 'Throw ArgumentOutOfRangeException
595 End If
[1]596
[272]597 Dim buf = New System.Text.StringBuilder(s.Length * n)
[1]598 Dim i As Long
[272]599 For i = 0 To n
600 buf.Append(s)
[1]601 Next
602End Function
[272]603#else
604Function String$(n As Long, s As String) As String
605 If String.IsNullOrEmpty(s) Then
606 Return New String(0 As StrChar, n)
[388]607 Else
[272]608 Return New String(s[0], n)
609 End If
610End Function
611#endif
[1]612
613Function Time$() As String
[289]614 Dim time = System.DateTime.Now
[1]615
[272]616 Dim buf = New System.Text.StringBuilder(8)
[1]617
618 'hour
[272]619 If time.Hour < 10 Then
620 buf.Append("0")
[1]621 End If
[272]622 buf.Append(time.Hour)
[1]623
624 'minute
[272]625 If time.Minute < 10 Then
626 buf.Append(":0")
[1]627 Else
[272]628 buf.Append(":")
[1]629 End If
[272]630 buf.Append(time.Minute)
[1]631
632 'second
[272]633 If time.Second < 10 Then
634 buf.Append(":0")
[1]635 Else
[272]636 buf.Append(":")
[1]637 End If
[272]638 buf.Append(time.Second)
639 Time$ = buf.ToString
[1]640End Function
641
[167]642Function Val(buf As *StrChar) As Double
[1]643 Dim i As Long, i2 As Long, i3 As Long, i4 As Long
644 Dim temporary As String
[167]645 Dim TempPtr As *StrChar
[1]646 Dim dbl As Double
647 Dim i64data As Int64
648
649 Val=0
650
[400]651 While ActiveBasic.CType.IsSpace(buf[0])
[269]652 buf = VarPtr(buf[1])
[1]653 Wend
654
655 If buf[0]=Asc("&") Then
[145]656 temporary = New String( buf )
[272]657 temporary = temporary.ToUpper()
[123]658 TempPtr = StrPtr(temporary)
[400]659 If TempPtr(1) = Asc("O") Then
[1]660 '8進数
661 i=2
662 While 1
663 '数字以外の文字の場合は抜け出す
664 i3=TempPtr[i]-&H30
665 If Not (0<=i3 And i3<=7) Then Exit While
666
[167]667 TempPtr[i]=i3 As StrChar
[1]668 i++
669 Wend
670 i--
671
672 i64data=1
673 While i>=2
[214]674 Val += ( i64data * TempPtr[i] ) As Double
[1]675
[123]676 i64data *= &O10
[10]677 i--
[1]678 Wend
679 ElseIf TempPtr(1)=Asc("H") Then
680 '16進数
681 i=2
682 While 1
683 '数字以外の文字の場合は抜け出す
684 i3=TempPtr[i]-&H30
685 If Not(0<=i3 and i3<=9) Then
686 i3=TempPtr[i]-&H41+10
687 If Not(&HA<=i3 and i3<=&HF) Then Exit While
688 End If
689
[167]690 TempPtr[i]=i3 As StrChar
[1]691 i++
692 Wend
693 i--
694
695 i64data=1
696 While i>=2
[22]697 Val += (i64data*TempPtr[i]) As Double
[1]698
[10]699 i64data *= &H10
[1]700 i--
701 Wend
702 End If
703 Else
704 '10進数
[394]705#ifdef __STRING_IS_NOT_UNICODE
[251]706 sscanf(buf,"%lf",VarPtr(Val))
[394]707#else
708 swscanf(buf,ToWCStr("%lf"),VarPtr(Val))
709#endif
[1]710 End If
711End Function
712
713
714'--------------
715' ファイル関数
716'--------------
717
718Function Eof(FileNum As Long) As Long
719 FileNum--
[400]720 Dim dwCurrent = SetFilePointer(_System_hFile(FileNum), 0,NULL, FILE_CURRENT)
721 Dim dwEnd = SetFilePointer(_System_hFile(FileNum), 0, NULL, FILE_END)
722 SetFilePointer(_System_hFile(FileNum), dwCurrent, NULL, FILE_BEGIN)
[1]723
724 If dwCurrent>=dwEnd Then
725 Eof=-1
726 Else
727 Eof=0
728 End If
729End Function
730
731Function Lof(FileNum As Long) As Long
[142]732 Lof = GetFileSize(_System_hFile(FileNum-1), 0)
[1]733End Function
734
735Function Loc(FileNum As Long) As Long
[10]736 FileNum--
[1]737
[272]738 Dim NowPos = SetFilePointer(_System_hFile(FileNum), 0, 0, FILE_CURRENT)
739 Dim BeginPos = SetFilePointer(_System_hFile(FileNum), 0, 0, FILE_BEGIN)
740 SetFilePointer(_System_hFile(FileNum), NowPos - BeginPos, 0, FILE_BEGIN)
[1]741
[272]742 Loc = NowPos - BeginPos
[1]743End Function
744
745
746'------------------
747' メモリ関連の関数
748'------------------
749
750Function malloc(stSize As SIZE_T) As VoidPtr
[145]751 Return _System_pGC->__malloc(stSize,_System_GC_FLAG_NEEDFREE or _System_GC_FLAG_ATOMIC)
[1]752End Function
753
754Function calloc(stSize As SIZE_T) As VoidPtr
[145]755 Return _System_pGC->__malloc(stSize,_System_GC_FLAG_NEEDFREE or _System_GC_FLAG_ATOMIC or _System_GC_FLAG_INITZERO)
[1]756End Function
757
758Function realloc(lpMem As VoidPtr, stSize As SIZE_T) As VoidPtr
759 If lpMem = 0 Then
760 Return malloc(stSize)
761 Else
[145]762 Return _System_pGC->__realloc(lpMem,stSize)
[1]763 End If
764End Function
765
766Sub free(lpMem As VoidPtr)
[145]767 _System_pGC->__free(lpMem)
[1]768End Sub
769
770Function _System_malloc(stSize As SIZE_T) As VoidPtr
[400]771 Return HeapAlloc(_System_hProcessHeap, 0, stSize)
[1]772End Function
773
774Function _System_calloc(stSize As SIZE_T) As VoidPtr
[400]775 Return HeapAlloc(_System_hProcessHeap, HEAP_ZERO_MEMORY, stSize)
[1]776End Function
777
778Function _System_realloc(lpMem As VoidPtr, stSize As SIZE_T) As VoidPtr
779 If lpMem = 0 Then
780 Return HeapAlloc(_System_hProcessHeap, 0, stSize)
781 Else
782 Return HeapReAlloc(_System_hProcessHeap, 0, lpMem, stSize)
783 End If
784End Function
785
786Sub _System_free(lpMem As VoidPtr)
[400]787 HeapFree(_System_hProcessHeap, 0, lpMem)
[1]788End Sub
789
790
791'--------
792' その他
793'--------
794
[123]795Sub _splitpath(path As PCSTR, drive As PSTR, dir As PSTR, fname As PSTR, ext As PSTR)
[1]796 Dim i As Long, i2 As Long, i3 As Long, length As Long
[167]797 Dim buffer[MAX_PATH] As SByte
[1]798
799 '":\"をチェック
800 If not(path[1]=&H3A and path[2]=&H5C) Then Exit Sub
801
802 'ドライブ名をコピー
803 If drive Then
804 drive[0]=path[0]
805 drive[1]=path[1]
806 drive[2]=0
807 End If
808
809 'ディレクトリ名をコピー
810 i=2
811 i2=0
812 Do
[123]813 If IsDBCSLeadByte(path[i]) <> FALSE and path[i + 1] <> 0 Then
[1]814 If dir Then
815 dir[i2]=path[i]
816 dir[i2+1]=path[i+1]
817 End If
818
[10]819 i += 2
820 i2 += 2
[1]821 Continue
822 End If
823
824 If path[i]=0 Then Exit Do
825
826 If path[i]=&H5C Then '"\"記号であるかどうか
827 i3=i2+1
828 End If
829
830 If dir Then dir[i2]=path[i]
831
[10]832 i++
833 i2++
[1]834 Loop
835 If dir Then dir[i3]=0
[22]836 i3 += i-i2
[1]837
838 'ファイル名をコピー
839 i=i3
840 i2=0
841 i3=-1
842 Do
[123]843'#ifdef UNICODE
844' If _System_IsSurrogatePair(path[i], path[i + 1]) Then
845'#else
846 If IsDBCSLeadByte(path[i]) <> FALSE and path[i + 1] <> 0 Then
847'#endif
[1]848 If fname Then
849 fname[i2]=path[i]
850 fname[i2+1]=path[i+1]
851 End If
852
[10]853 i += 2
854 i2 += 2
[1]855 Continue
856 End If
857
858 If path[i]=0 Then Exit Do
859
860 If path[i]=&H2E Then '.'記号であるかどうか
861 i3=i2
862 End If
863
864 If fname Then fname[i2]=path[i]
865
[10]866 i++
867 i2++
[1]868 Loop
869 If i3=-1 Then i3=i2
870 If fname Then fname[i3]=0
[10]871 i3 += i-i2
[1]872
873 '拡張子名をコピー
874 If ext Then
875 If i3 Then
876 lstrcpy(ext,path+i3)
877 End If
878 else ext[0]=0
879 End If
880End Sub
881
882Function GetBasicColor(ColorCode As Long) As Long
883 Select Case ColorCode
884 Case 0
885 GetBasicColor=RGB(0,0,0)
886 Case 1
887 GetBasicColor=RGB(0,0,255)
888 Case 2
889 GetBasicColor=RGB(255,0,0)
890 Case 3
891 GetBasicColor=RGB(255,0,255)
892 Case 4
893 GetBasicColor=RGB(0,255,0)
894 Case 5
895 GetBasicColor=RGB(0,255,255)
896 Case 6
897 GetBasicColor=RGB(255,255,0)
898 Case 7
899 GetBasicColor=RGB(255,255,255)
900 End Select
901End Function
902
[167]903Function _System_BSwap(x As Word) As Word
904 Dim src = VarPtr(x) As *Byte
[223]905 Dim dst = VarPtr(_System_BSwap) As *Byte
[167]906 dst[0] = src[1]
907 dst[1] = src[0]
908End Function
909
910Function _System_BSwap(x As DWord) As DWord
911 Dim src = VarPtr(x) As *Byte
[223]912 Dim dst = VarPtr(_System_BSwap) As *Byte
[167]913 dst[0] = src[3]
914 dst[1] = src[2]
915 dst[2] = src[1]
916 dst[3] = src[0]
917End Function
918
919Function _System_BSwap(x As QWord) As QWord
920 Dim src = VarPtr(x) As *Byte
[223]921 Dim dst = VarPtr(_System_BSwap) As *Byte
[167]922 dst[0] = src[7]
923 dst[1] = src[6]
924 dst[2] = src[5]
925 dst[3] = src[4]
926 dst[4] = src[3]
927 dst[5] = src[2]
928 dst[6] = src[1]
929 dst[7] = src[0]
930End Function
931
[394]932Function _System_HashFromUInt(x As QWord) As Long
933 Return (HIDWORD(x) Xor LODWORD(x)) As Long
934End Function
935
936Function _System_HashFromUInt(x As DWord) As Long
937 Return x As Long
938End Function
939
[175]940Function _System_HashFromPtr(p As VoidPtr) As Long
[394]941 Return _System_HashFromUInt(p As ULONG_PTR)
[175]942End Function
943
[355]944/*!
[388]945@brief ObjPtrの逆。ABオブジェクトを指すポインタをObject型へ変換。
[355]946@author Egtra
947@date 2007/08/24
948@param[in] p COMインタフェースを指すポインタ
949@return Object参照型
950*/
[303]951Function _System_PtrObj(p As VoidPtr) As Object
952 SetPointer(VarPtr(_System_PtrObj), p)
953End Function
954
[355]955/*!
956@brief IUnknownその他COMインタフェースを指すポインタをIUnknown参照型へ変換。
957@author Egtra
958@date 2007/09/24
959@param[in] p COMインタフェースを指すポインタ
960@return IUnknown参照型
961*/
962Function _System_PtrUnknown(p As VoidPtr) As IUnknown
963 SetPointer(VarPtr(_System_PtrUnknown), p)
964End Function
965
[142]966'--------
967' 文字列関数その2
968'--------
[119]969Function _System_IsSurrogatePair(wcHigh As WCHAR, wcLow As WCHAR) As Boolean
[394]970 If _System_IsHighSurrogate(wcHigh) Then
971 If _System_IsLowSurrogate(wcLow) Then
[119]972 Return True
973 End If
974 End If
975 Return False
976End Function
[1]977
[394]978Function _System_IsHighSurrogate(c As WCHAR) As Boolean
979 Return &hD800 <= c And c < &hDC00
980End Function
981
982Function _System_IsLowSurrogate(c As WCHAR) As Boolean
983 Return &hDC00 <= c And c < &hE000
984End Function
985
[142]986Function _System_IsDoubleUnitChar(lead As WCHAR, trail As WCHAR) As Boolean
[126]987 Return _System_IsSurrogatePair(lead, trail)
[142]988End Function
989
990Function _System_IsDoubleUnitChar(lead As SByte, trail As SByte) As Boolean
[126]991 Return IsDBCSLeadByte(lead) <> FALSE
992End Function
993
[175]994Function _System_GetHashFromWordArray(p As *Word, n As SIZE_T) As Long
995 Dim hash = 0 As DWord
996 Dim i As Long
997 For i = 0 To ELM(n)
998 hash = ((hash << 16) + p[i]) Mod &h7fffffff
999 Next
1000 _System_GetHashFromWordArray = hash As Long
1001End Function
1002
[1]1003#endif '_INC_FUNCTION
Note: See TracBrowser for help on using the repository browser.