source: Include/basic/function.sbp@ 129

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

#68修正完了

File size: 23.0 KB
Line 
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
13#include <Classes/System/Math.ab>
14
15
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
62 Dim r = 1 As Double
63
64 abs_n=Abs(n) As Long
65 While abs_n<>0
66 If abs_n and 1 Then r *= x
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
141 SetQWord(AddressOf(CopySign), (GetQWord(VarPtr(x)) And &h7fffffffffffffff) Or (isNegative << 63))
142#else
143 SetDWord(AddressOf(CopySign), GetDWord(VarPtr(x)))
144 SetDWord(AddressOf(CopySign) + SizeOf (DWord), GetQWord(VarPtr(x) + SizeOf (DWord)) And &h7fffffff Or (isNegative << 31))
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
177 CDWord=num As DWord
178End Function
179
180Function CInt(number As Double) As Long
181 CInt=number As Long
182End Function
183
184Function CSng(number As Double) As Single
185 CSng=number As Single
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
277 Atn2 = Math.Atan2(y, x)
278End Function
279
280Function Sin(number As Double) As Double
281 Sin = Math.Sin(number)
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
292Function IsNaN(ByVal x As Double) As Boolean
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
298 IsNaN = True
299 End If
300 End If
301
302' IsNaN=FALSE
303End Function
304
305Function IsInf(x As Double) As Boolean
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
313Function IsNaNOrInf(x As Double) As Boolean
314 IsNaNOrInf = IsFinite(x)
315End Function
316
317Function IsFinite(x As Double) As Boolean
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)
324 IsFinite = (memcmp(p As BytePtr, VarPtr(nan), SizeOf (Double)) = 0)
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
346Function Asc(buf As String) As Char
347 Asc = buf[0]
348End Function
349
350Function Chr$(code As Char) As String
351 Chr$ = ZeroString(1)
352 Chr$[0] = code
353End Function
354
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
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
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)
420End Function
421
422Function Hex$(x As QWord) As String
423 Hex$ = Hex$((x >> 32) As DWord) + Hex$((x And &hffffffff) As DWord)
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
437 StartPos--
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
454 i2++
455 i3++
456 Loop
457 If InStr Then Exit While
458 i++
459 Wend
460End Function
461
462Function Left$(buf As String, length As Long) As String
463 Left$ = ZeroString(length)
464 memcpy(StrPtr(Left$), StrPtr(buf), SizeOf (Char) * length)
465End Function
466
467Function Mid$(buf As String, StartPos As Long)(ReadLength As Long) As String
468 Dim length As Long
469
470 StartPos--
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)
489 memcpy(StrPtr(Mid$), VarPtr(buf[StartPos]), SizeOf (Char) * ReadLength)
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] = &h30 +((num \ CDWord(8 ^ i)) And &H07) ' &h30 = Asc("0")
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)
517 memcpy(StrPtr(Right$), VarPtr(buf[i-length]), SizeOf (Char) * length)
518 Else
519 Right$=buf
520 End If
521End Function
522
523Function Space$(length As Long) As String
524 Space$.ReSize(length, &H20 As Char)
525End Function
526
527Dim _System_ecvt_buffer[16] As Char
528Sub _ecvt_support(count As Long)
529 Dim i As Long
530 If _System_ecvt_buffer[count]=9 Then
531 _System_ecvt_buffer[count]=0
532 If count=0 Then
533 For i=16 To 1 Step -1
534 _System_ecvt_buffer[i]=_System_ecvt_buffer[i-1]
535 Next
536 _System_ecvt_buffer[0]=1
537 Else
538 _ecvt_support(count-1)
539 End If
540 Else
541 _System_ecvt_buffer[count]++
542 End If
543End Sub
544Function _ecvt(value As Double, count As Long, ByRef dec As Long, ByRef sign As Long) As *Char
545 Dim temp As *Char
546 Dim i As Long, i2 As Long
547
548 _ecvt=_System_ecvt_buffer
549
550 '値が0の場合
551 If value=0 Then
552 _System_FillChar(_System_ecvt_buffer, count, &H30)
553 _System_ecvt_buffer[count] = 0
554 dec = 0
555 sign = 0
556 Exit Function
557 End If
558
559 '符号の判断(同時に符号を取り除く)
560 If value<0 Then
561 sign=1
562 value=-value
563 Else
564 sign=0
565 End If
566
567 '正規化
568 dec=1
569 While value<0.999999999999999 'value<1
570 value *= 10
571 dec--
572 Wend
573 While 9.99999999999999<=value '10<=value
574 value /= 10
575 dec++
576 Wend
577
578 For i=0 To count-1
579 _System_ecvt_buffer[i]=Int(value) As Char
580
581 value=(value-CDbl(Int(value)))*10
582 Next
583 _System_ecvt_buffer[i]=0
584
585 i--
586 If value>=5 Then
587 '切り上げ処理
588 _ecvt_support(i)
589 End If
590
591 For i=0 To count-1
592 _System_ecvt_buffer[i] += &H30
593 Next
594 _System_ecvt_buffer[i]=0
595End Function
596
597Function Str$(dbl As Double) As String
598 If IsNaN(dbl) Then
599 Return "NaN"
600 ElseIf IsInf(dbl) Then
601 If dbl > 0 Then
602 Return "Infinity"
603 Else
604 Return "-Infinity"
605 End If
606 End If
607 Dim dec As Long, sign As Long
608 Dim buffer[32] As Char, temp As *Char
609 Dim i As Long, i2 As Long, i3 As Long
610
611 '浮動小数点を文字列に変換
612 temp=_ecvt(dbl,15,dec,sign)
613
614 i=0
615
616 '符号の取り付け
617 If sign Then
618 buffer[i]=Asc("-")
619 i++
620 End If
621
622 If dec>15 Then
623 '指数表示(桁が大きい場合)
624 buffer[i]=temp[0]
625 i++
626 buffer[i]=Asc(".")
627 i++
628 memcpy(VarPtr(buffer[i]), VarPtr(temp[1]), SizeOf (Char) * 14)
629 i+=14
630 buffer[i]=Asc("e")
631 i++
632 _stprintf(VarPtr(buffer[i]), "+%03d", dec-1)
633
634 Return MakeStr(buffer)
635 End If
636
637 If dec<-3 Then
638 '指数表示(桁が小さい場合)
639 buffer[i]=temp[0]
640 i++
641 buffer[i]=Asc(".")
642 i++
643 memcpy(VarPtr(buffer[i]), VarPtr(temp[1]), SizeOf (Char) * 14)
644 i+=14
645 buffer[i]=Asc("e")
646 i++
647 _stprintf(VarPtr(buffer[i]), "+%03d", dec-1)
648
649 Return MakeStr(buffer)
650 End If
651
652 '整数部
653 i2=dec
654 i3=0
655 If i2>0 Then
656 While i2>0
657 buffer[i]=temp[i3]
658 i++
659 i3++
660 i2--
661 Wend
662 buffer[i]=Asc(".")
663 i++
664 Else
665 buffer[i]=&H30
666 i++
667 buffer[i]=Asc(".")
668 i++
669
670 i2=dec
671 While i2<0
672 buffer[i]=&H30
673 i++
674 i2++
675 Wend
676 End If
677
678 '小数部
679 While i3<15
680 buffer[i]=temp[i3]
681 i++
682 i3++
683 Wend
684
685 While buffer[i-1]=&H30
686 i--
687 Wend
688 If buffer[i-1]=Asc(".") Then i--
689
690 buffer[i]=0
691 Return MakeStr(buffer)
692End Function
693Function Str$(value As LONG_PTR) As String
694 Dim temp[255] As Char
695#ifdef _WIN64
696 _sntprintf(temp, Len (temp) \ SizeOf (Char), "%I64d", value)
697#else
698 _sntprintf(temp, Len (temp) \ SizeOf (Char), "%d", value)
699#endif
700 Str$ = temp
701End Function
702
703Function String$(num As Long, buf As String) As String
704 Dim dwStrPtr As DWord
705 Dim length As Long
706
707 length=Len(buf)
708
709 'バッファ領域を確保
710 String$=ZeroString(length*num)
711
712 '文字列をコピー
713 Dim i As Long
714 For i=0 To num-1
715 memcpy(VarPtr(String$[i*length]), StrPtr(buf), SizeOf (Char) * length)
716 Next
717End Function
718
719Function Time$() As String
720 Dim st As SYSTEMTIME
721
722 GetLocalTime(st)
723
724 'hour
725 If st.wHour<10 Then
726 Time$="0"
727 End If
728 Time$=Time$+Str$(st.wHour)
729
730 'minute
731 If st.wMinute<10 Then
732 Time$=Time$+":0"
733 Else
734 Time$=Time$+":"
735 End If
736 Time$=Time$+Str$(st.wMinute)
737
738 'second
739 If st.wSecond<10 Then
740 Time$=Time$+":0"
741 Else
742 Time$=Time$+":"
743 End If
744 Time$=Time$+Str$(st.wSecond)
745End Function
746
747Function Val(buf As *Char) As Double
748 Dim i As Long, i2 As Long, i3 As Long, i4 As Long
749 Dim temporary As String
750 Dim TempPtr As *Char
751 Dim dbl As Double
752 Dim i64data As Int64
753
754 Val=0
755
756 While buf[0]=Asc(" ") or buf[0]=Asc(Ex"\t")
757 buf++
758 Wend
759
760 If buf[0]=Asc("&") Then
761 temporary = buf
762 temporary.ToUpper()
763 TempPtr = StrPtr(temporary)
764 If TempPtr(1)=Asc("O") Then
765 '8進数
766 i=2
767 While 1
768 '数字以外の文字の場合は抜け出す
769 i3=TempPtr[i]-&H30
770 If Not (0<=i3 And i3<=7) Then Exit While
771
772 TempPtr[i]=i3 As Char
773 i++
774 Wend
775 i--
776
777 i64data=1
778 While i>=2
779 Val += i64data * TempPtr[i]
780
781 i64data *= &O10
782 i--
783 Wend
784 ElseIf TempPtr(1)=Asc("H") Then
785 '16進数
786 i=2
787 While 1
788 '数字以外の文字の場合は抜け出す
789 i3=TempPtr[i]-&H30
790 If Not(0<=i3 and i3<=9) Then
791 i3=TempPtr[i]-&H41+10
792 If Not(&HA<=i3 and i3<=&HF) Then Exit While
793 End If
794
795 TempPtr[i]=i3 As Char
796 i++
797 Wend
798 i--
799
800 i64data=1
801 While i>=2
802 Val += (i64data*TempPtr[i]) As Double
803
804 i64data *= &H10
805 i--
806 Wend
807 End If
808 Else
809 '10進数
810 If buf[0]=&H2D Then
811 'マイナス値
812 i4=1
813 buf++
814 Else
815 'プラス値
816 i4=0
817 If buf[0]=&H2B Then
818 buf++
819 End If
820 End If
821
822 i=0
823
824 While 1
825 '数字以外の文字の場合は抜け出す
826 i3=buf[i]-&H30
827 If Not (0<=i3 And i3<=9) Then Exit While
828
829 i++
830 Wend
831
832 '整数部
833 dbl=1
834 i3=i-1
835 While i3>=0
836 Val += dbl*(buf[i3]-&H30)
837
838 dbl *= 10
839 i3--
840 Wend
841
842 If buf[i]=Asc(".") Then
843 '小数部
844 i++
845 dbl=10
846 While 1
847 '数字以外の文字の場合は抜け出す
848 i3=buf[i]-&H30
849 If Not (0<=i3 And i3<=9) Then Exit While
850
851 Val += (buf[i] - &H30) / dbl
852 dbl *= 10
853 i++
854 Wend
855 End If
856
857 If i4 Then Val=-Val
858 End If
859End Function
860
861
862'--------------
863' ファイル関数
864'--------------
865
866Function Eof(FileNum As Long) As Long
867 Dim dwCurrent As DWord, dwEnd As DWord
868
869 FileNum--
870
871 dwCurrent=SetFilePointer(_System_hFile(FileNum),0,NULL,FILE_CURRENT)
872 dwEnd=SetFilePointer(_System_hFile(FileNum),0,NULL,FILE_END)
873 SetFilePointer(_System_hFile(FileNum),dwCurrent,NULL,FILE_BEGIN)
874
875 If dwCurrent>=dwEnd Then
876 Eof=-1
877 Else
878 Eof=0
879 End If
880End Function
881
882Function Lof(FileNum As Long) As Long
883 Lof=GetFileSize(_System_hFile(FileNum-1),NULL)
884End Function
885
886Function Loc(FileNum As Long) As Long
887 Dim NowPos As Long, BeginPos As Long
888
889 FileNum--
890
891 NowPos=SetFilePointer(_System_hFile(FileNum),0,NULL,FILE_CURRENT)
892 BeginPos=SetFilePointer(_System_hFile(FileNum),0,NULL,FILE_BEGIN)
893 SetFilePointer(_System_hFile(FileNum),NowPos-BeginPos,NULL,FILE_BEGIN)
894
895 Loc=NowPos-BeginPos
896End Function
897
898
899'------------------
900' メモリ関連の関数
901'------------------
902
903Function malloc(stSize As SIZE_T) As VoidPtr
904 Return _System_GC.__malloc(stSize,_System_GC_FLAG_NEEDFREE)
905End Function
906
907Function calloc(stSize As SIZE_T) As VoidPtr
908 Return _System_GC.__malloc(stSize,_System_GC_FLAG_NEEDFREE or _System_GC_FLAG_INITZERO)
909End Function
910
911Function realloc(lpMem As VoidPtr, stSize As SIZE_T) As VoidPtr
912 If lpMem = 0 Then
913 Return malloc(stSize)
914 Else
915 Return _System_GC.__realloc(lpMem,stSize)
916 End If
917End Function
918
919Sub free(lpMem As VoidPtr)
920 _System_GC.__free(lpMem)
921End Sub
922
923
924Function _System_malloc(stSize As SIZE_T) As VoidPtr
925 Return HeapAlloc(_System_hProcessHeap,0,stSize)
926End Function
927
928Function _System_calloc(stSize As SIZE_T) As VoidPtr
929 Return HeapAlloc(_System_hProcessHeap,HEAP_ZERO_MEMORY,stSize)
930End Function
931
932Function _System_realloc(lpMem As VoidPtr, stSize As SIZE_T) As VoidPtr
933 If lpMem = 0 Then
934 Return HeapAlloc(_System_hProcessHeap, 0, stSize)
935 Else
936 Return HeapReAlloc(_System_hProcessHeap, 0, lpMem, stSize)
937 End If
938End Function
939
940Sub _System_free(lpMem As VoidPtr)
941 HeapFree(_System_hProcessHeap,0,lpMem)
942End Sub
943
944
945'--------
946' その他
947'--------
948
949Sub _splitpath(path As PCSTR, drive As PSTR, dir As PSTR, fname As PSTR, ext As PSTR)
950 Dim i As Long, i2 As Long, i3 As Long, length As Long
951 Dim buffer[MAX_PATH] As Char
952
953 '":\"をチェック
954 If not(path[1]=&H3A and path[2]=&H5C) Then Exit Sub
955
956 'ドライブ名をコピー
957 If drive Then
958 drive[0]=path[0]
959 drive[1]=path[1]
960 drive[2]=0
961 End If
962
963 'ディレクトリ名をコピー
964 i=2
965 i2=0
966 Do
967'#ifdef UNICODE
968' If _System_IsSurrogatePair(path[i], path[i + 1]) Then
969'#else
970 If IsDBCSLeadByte(path[i]) <> FALSE and path[i + 1] <> 0 Then
971'#endif
972 If dir Then
973 dir[i2]=path[i]
974 dir[i2+1]=path[i+1]
975 End If
976
977 i += 2
978 i2 += 2
979 Continue
980 End If
981
982 If path[i]=0 Then Exit Do
983
984 If path[i]=&H5C Then '"\"記号であるかどうか
985 i3=i2+1
986 End If
987
988 If dir Then dir[i2]=path[i]
989
990 i++
991 i2++
992 Loop
993 If dir Then dir[i3]=0
994 i3 += i-i2
995
996 'ファイル名をコピー
997 i=i3
998 i2=0
999 i3=-1
1000 Do
1001'#ifdef UNICODE
1002' If _System_IsSurrogatePair(path[i], path[i + 1]) Then
1003'#else
1004 If IsDBCSLeadByte(path[i]) <> FALSE and path[i + 1] <> 0 Then
1005'#endif
1006 If fname Then
1007 fname[i2]=path[i]
1008 fname[i2+1]=path[i+1]
1009 End If
1010
1011 i += 2
1012 i2 += 2
1013 Continue
1014 End If
1015
1016 If path[i]=0 Then Exit Do
1017
1018 If path[i]=&H2E Then '.'記号であるかどうか
1019 i3=i2
1020 End If
1021
1022 If fname Then fname[i2]=path[i]
1023
1024 i++
1025 i2++
1026 Loop
1027 If i3=-1 Then i3=i2
1028 If fname Then fname[i3]=0
1029 i3 += i-i2
1030
1031 '拡張子名をコピー
1032 If ext Then
1033 If i3 Then
1034 lstrcpy(ext,path+i3)
1035 End If
1036 else ext[0]=0
1037 End If
1038End Sub
1039
1040Function GetBasicColor(ColorCode As Long) As Long
1041 Select Case ColorCode
1042 Case 0
1043 GetBasicColor=RGB(0,0,0)
1044 Case 1
1045 GetBasicColor=RGB(0,0,255)
1046 Case 2
1047 GetBasicColor=RGB(255,0,0)
1048 Case 3
1049 GetBasicColor=RGB(255,0,255)
1050 Case 4
1051 GetBasicColor=RGB(0,255,0)
1052 Case 5
1053 GetBasicColor=RGB(0,255,255)
1054 Case 6
1055 GetBasicColor=RGB(255,255,0)
1056 Case 7
1057 GetBasicColor=RGB(255,255,255)
1058 End Select
1059End Function
1060
1061Function _System_IsSurrogatePair(wcHigh As WCHAR, wcLow As WCHAR) As Boolean
1062 If &hD800 <= wcHigh And wcHigh < &hDC00 Then
1063 If &hDC00 <= wcLow And wcLow < &hE000 Then
1064 Return True
1065 End If
1066 End If
1067 Return False
1068End Function
1069
1070Function _System_IsDoubleUnitChar(lead As Char, trail As Char) As Boolean
1071#ifdef UNICODE
1072 Return _System_IsSurrogatePair(lead, trail)
1073#else
1074 Return IsDBCSLeadByte(lead) <> FALSE
1075#endif
1076End Function
1077
1078Sub _System_FillChar(p As *Char, n As SIZE_T, c As Char)
1079 Dim i As SIZE_T
1080 For i = 0 To ELM(n)
1081 p[i] = c
1082 Next
1083End Sub
1084
1085Function _System_ASCII_IsUpper(c As Char) As Boolean
1086 Return c As DWord - &h41 < 26 ' &h41 = Asc("A")
1087End Function
1088
1089Function _System_ASCII_IsLower(c As Char) As Boolean
1090 Return c As DWord - &h61 < 26 ' &h61 = Asc("a")
1091End Function
1092
1093Function _System_ASCII_ToLower(c As Char) As Char
1094 If _System_ASCII_IsUpper(c) Then
1095 Return c Or &h20
1096 Else
1097 Return c
1098 End If
1099End Function
1100
1101Function _System_ASCII_ToUpper(c As Char) As Char
1102 If _System_ASCII_IsLower(c) Then
1103 Return c And (Not &h20)
1104 Else
1105 Return c
1106 End If
1107End Function
1108
1109Function _System_WideCharToMultiByte(s As PCWSTR) As PSTR
1110 Return _System_WideCharToMultiByte(s, lstrlenW(s) + 1, 0)
1111End Function
1112
1113Function _System_WideCharToMultiByte(s As PCWSTR, size As Long) As PSTR
1114 Return _System_WideCharToMultiByte(s, size, 0)
1115End Function
1116
1117Function _System_WideCharToMultiByte(ws As PCWSTR, size As Long, flag As DWord) As PSTR
1118 Dim sizeMBS = WideCharToMultiByte(CP_THREAD_ACP, flag, s, size, 0, 0, 0, 0)
1119 Dim mbs = malloc(sizeMBS) As PSTR
1120 WideCharToMultiByte(CP_THREAD_ACP, flag, s, size, mbs, sizeMBS, 0, 0)
1121 Return mbs
1122End Function
1123
1124Function _System_MultiByteToWideChar(s As PCSTR) As PWSTR
1125 Return _System_MultiByteToWideChar(s, lstrlenA(s) + 1, 0)
1126End Function
1127
1128Function _System_MultiByteToWideChar(s As PCSTR, size As Long) As PWSTR
1129 Return _System_MultiByteToWideChar(s, size, 0)
1130End Function
1131
1132Function _System_MultiByteToWideChar(s As PCSTR, size As Long, flag As DWord) As PWSTR
1133 Dim sizeMBS = MultiByteToWideChar(CP_THREAD_ACP, flag, s, size, 0, 0)
1134 Dim mbs = malloc(SizeOf (WCHAR) * sizeMBS) As PWSTR
1135 MultiByteToWideChar(CP_THREAD_ACP, flag, s, size, mbs, sizeMBS)
1136 Return mbs
1137End Function
1138
1139#endif '_INC_FUNCTION
Note: See TracBrowser for help on using the repository browser.