-
Notifications
You must be signed in to change notification settings - Fork 0
/
RGBALib.sbp
2723 lines (2316 loc) · 65.5 KB
/
RGBALib.sbp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'RGBA_CRT's Librarys
#include <RGBADef.sbp>
#include <EasyIO.sbp>
/* Todo :
HTTP系をクラスに
*/
'--------------------------------------------------------
' 要求APIが環境依存(Win95デフォルトから外れるもの)
'--------------------------------------------------------
' %fが使える
Declare Function printfvc cdecl Lib "msvcrt.dll" Alias "printf" (str as BytePtr,format as BytePtr, ...) As Long
Declare Function printfd cdecl Lib "msvcrt.dll" Alias "printf" (format as BytePtr, v1 AS Double,...) As Long
Declare Function sprintfvc cdecl Lib "msvcrt.dll" Alias "sprintf" (str as BytePtr,format as BytePtr, ...) As Long
Declare Function sprintfd cdecl Lib "msvcrt.dll" Alias "sprintf" (str as BytePtr,format as BytePtr, d1 AS double,...) As Long
'---------------
' Macros
'---------------
Const Abs16bit(b) = (b xor (b >> 15)) - (b >> 15) As Word
Const ChangeEndianWord(x) = ((x>>8) and &HFF) or (x and &HFF)<<8
'----------------------------
' GUI Utility
'----------------------------
Sub PumpMessage()
Dim msg As MSG
while PeekMessage( msg, NULL, 0, 0, PM_REMOVE )
TranslateMessage( msg )
DispatchMessage( msg )
Wend
EndSub
Function GetDlgItemHex(hWnd AS HWND) AS Long
Dim buf as BytePtr,Length AS Long
Length=GetWindowTextLength(hWnd)
buf=calloc(Length+5)
GetWindowText(hWnd,buf,Length+1)
Dim i as Long,mode as Long
Do
if buf[i]=&H20 Then
i++
Continue'Skip Space
Elseif buf[i]=&H30 Then '0'
if buf[i+1]=&H78 Or buf[i+1]=&H58 Then 'x'
'16進決定,0xを&Hに変更
buf[i]=&H26: buf[i+1]=&H48
EndIf
EndIf
ExitDo
Loop
GetDlgItemHex=Val(buf+i) AS Long
free(buf)
EndFunction
Function GetDlgItemDouble(hWnd AS HWND) AS Double
Dim buf as BytePtr,Length AS Long
Length=GetWindowTextLength(hWnd)
buf=calloc(Length+5)
GetWindowText(hWnd,buf,Length+1)
GetDlgItemDouble=Val(buf)' AS Long
free(buf)
EndFunction
'Auto Allocate
Function GetWndTextAA(hWnd AS HWND) AS BytePtr
Dim buf as BytePtr,Length AS Long
Length=GetWindowTextLength(hWnd)
GetWndTextAA=calloc(Length+5)
GetWindowText(hWnd,GetWndTextAA,Length+1)
EndFunction
/*
'エラーメッセージ表示
Function ErrMes(hOwner AS HWND,Text As BytePtr,Title As BytePtr,Flag As Dword) As Long
SetForegroundWindow(hOwner)
MessageBeep(MB_ICONHAND)
ErrMes=MessageBox(hOwner,Text,ProgramName+" - "+MakeStr(Title),MB_ICONERROR Or Flag)
EndFunction*/
'エラーメッセージ表示(TST用)
Function ErrMes(hOwner AS HWND,Text As BytePtr,Title As BytePtr,Flag As Dword)(ErrCode As DWord) As Long
SetForegroundWindow(hOwner)
MessageBeep(MB_ICONHAND)
if ErrCode=0 Then
ErrMes=MessageBox(hOwner,Text,sprintfStr("%s - %s",ProgramName AS DWord,Title AS DWord),MB_ICONERROR Or Flag)
' DBM(Text)
Else
Dim SysErrMes AS BytePtr
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_IGNORE_INSERTS, _
NULL, ErrCode, LANG_USER_DEFAULT, VarPtr(SysErrMes), 0, NULL)
ErrMes=MessageBoxf(hOwner,ProgramName+" - "+MakeStr(Title),MB_ICONERROR Or Flag,ex"%s\nErrCode : %d\n%s",Text As DWord,ErrCode,SysErrMes AS DWord)
free(SysErrMes)
EndIf
EndFunction
'printfのMessageBox版
Function MessageBoxf(hWnd AS HWND,lpCaption AS BytePtr,uType AS DWord,lpFormat As BytePtr)(a As DWord ,b As DWord ,c As DWord ,d As DWord ,e As DWord ,f As DWord ,g As DWord ,h As DWord ,i As DWord ,j As DWord ,k As DWord ,l As DWord ,m As DWord ,n As DWord ) AS Long
Dim buf[1024] As Byte 'wsprintfがそもそも1024バイトしか対応してない
wsprintf(buf,lpFormat,a,b,c,d,e,f,g,h,i,j,k,l,m,n)
MessageBoxf=MessageBox(hWnd,buf,lpCaption,uType)
EndFunction
'EXEC 終了コード取得
Function RunAndGetExitCode(App As BytePtr,CmdLine As BytePtr) AS Long
Dim si AS STARTUPINFO
Dim pi As PROCESS_INFORMATION
Dim Child As HANDLE
Dim r As DWORD
if CreateProcess(App,CmdLine,ByVal NULL,ByVal NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,si,pi)=FALSE Then'CREATE_NEW_CONSOLE を 0にすれば非表示
RunAndGetExitCode=-1
ExitFunction
EndIf
' 子プロセス起動成功
Child = pi.hProcess
' 不要なスレッドハンドルをクローズする
CloseHandle(pi.hThread)
' 子プロセスの終了待ち
r = WaitForSingleObject(Child, INFINITE)
if r<>WAIT_OBJECT_0 then
'wait error!
RunAndGetExitCode=-1
ExitFunction
EndIf
' 子プロセスの終了コードを取得
Dim exitCode As DWORD
if GetExitCodeProcess(Child, exitCode)=FALSE then
RunAndGetExitCode=-1
ExitFunction
EndIf
if Child<>0 then CloseHandle(Child)
RunAndGetExitCode=exitCode
EndFunction
' requires InitCommonControls()
Function CreateTooltip(hOwner AS HWND, hTooltip As *HANDLE, stTool As *TOOLINFO, text As BytePtr) AS BOOL
SetDWord(hTooltip,_
CreateWindowEx( 0 , TOOLTIPS_CLASS ,_
NULL , TTS_ALWAYSTIP ,_
CW_USEDEFAULT , CW_USEDEFAULT ,_
CW_USEDEFAULT , CW_USEDEFAULT ,_
hOwner , NULL , GetModuleHandle(0) ,_
NULL
)
)
GetClientRect(hOwner , ByVal VarPtr(stTool->rect))
stTool->cbSize = sizeof(TOOLINFO)
stTool->uFlags = TTF_SUBCLASS
stTool->hwnd = hOwner
stTool->lpszText = text
CreateTooltip = SendMessage(GetDWord(hTooltip) As HWND, TTM_ADDTOOL , 0 , stTool As Long)
End Function
'----------------------
' Binaly
'----------------------
Function isSameBin(dat1 AS BytePtr, dat2 AS BytePtr,length aS DWord) AS BOOL
Dim i AS DWord
For i = 0 To length-1
if dat1[i]<>dat2[i] Then
isSameBin=FALSE
ExitFunction
EndIf
Next i
isSameBin=TRUE
End Function
Sub ChangeEndianDW(ByRef in As DWord)
Dim Tmp As Byte
Dim value As *Byte
value=VarPtr(in)
Tmp=value[0]
value[0]=value[3]
value[3]=Tmp
Tmp=value[1]
value[1]=value[2]
value[2]=Tmp
EndSub
Function ChangeEndianDW2(in As DWord) As DWord
Dim Tmp As Byte
Dim value As *Byte
value=VarPtr(in)
Tmp=value[0]
value[0]=value[3]
value[3]=Tmp
Tmp=value[1]
value[1]=value[2]
value[2]=Tmp
ChangeEndianDW2=GetDWord(value)
EndFunction
Function Str2Dw(Data As BytePtr) As DWORD
Str2Dw=GetDWord(Data)
'memcpy(VarPtr(Str2Dw),Data,4)
EndFunction
Function Dw2Str(Data As DWORD) As String
Dim Str[4] As Byte, i AS Long
memcpy(Str,VarPtr(Data),4)
For i = 0 To 3
if Str[i] < &H20 Then Str[i]=&H20
Next i
Str[4]=0
Dw2Str=MakeStr(Str)
EndFunction
'InStrのバイナリ版
Function InByteBin(Data As BytePtr,Size As DWORD,Bytes As Byte)(Jump AS Long) As Long
Dim i As DWORD,tmp As Byte
if Jump<>0 then i=Jump
Do
if Data[i]=Bytes Then InByteBin=i: ExitDo
i++
if i => Size then InByteBin=-1: ExitDo
Loop
EndFunction
Function SwapWord(val AS Word) AS Word
SwapWord = ((val<<8) And &HFF00) Or ((val>>8) And &H00FF)
End Function
Function ReverseDWord(val As DWord) AS DWord
'ABCD
'D D ; << 3
' CC ; << 1
' BB ; >> 1
'A A ; >>3
ReverseDWord = ((val<<24) And &HFF000000) Or ((val<<8) And &H00FF0000) Or ((val>>8) And &H0000FF00) Or ((val>>24) And &H000000FF)
EndFunction
'---------------
' Strings
'--------------
Function Wide2Ansi(utf16 AS WordPtr) AS BytePtr
'Function UTF16ToSJIS(utf16 AS WordPtr) AS BytePtr
Dim l AS DWord
' UnicodeからShift-JISに変換
l=WideCharToMultiByte(CP_ACP, 0, utf16, -1, 0, 0, 0, 0)
Wide2Ansi=calloc(l)
WideCharToMultiByte(CP_ACP, 0, utf16, -1, Wide2Ansi, l, 0, 0)
End Function
Function Ansi2Wide(ansi AS BytePtr) AS WordPtr
Dim l As Long
l=MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0)
Ansi2Wide=calloc(l*3)
MultiByteToWideChar(CP_ACP, 0, ansi, -1, Ansi2Wide, l)
End Function
Function UTF8ToUnicode(utf8Str AS BytePtr) As BytePtr
Dim l As Long
l=MultiByteToWideChar(CP_UTF8, 0, utf8Str As WordPtr, -1, NULL, 0)
UTF8ToUnicode=calloc(l*3)
MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, UTF8ToUnicode, l)
End Function
Function UTF8ToSJIS(utf8Str AS BytePtr) As BytePtr
Dim l As Long,uni AS BytePtr
l=MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, NULL, 0)
uni=calloc(l*3)
UTF8ToSJIS=calloc(l*3)
MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, uni, l)
WideCharToMultiByte(CP_ACP,0,uni,-1,UTF8ToSJIS,l*3,0,0)
free(uni)
End Function
Function UTF8toSJIS_Str(source As BytePtr) As String
Dim u2s As BytePtr
Dim u2s_out As BytePtr
Dim l As Long
' UTF-8からUnicodeに変換
l=MultiByteToWideChar(CP_UTF8, 0, source, -1, 0, 0)
u2s_out=calloc(l*2+2)
MultiByteToWideChar(CP_UTF8, 0, source, -1, u2s_out, l)
' UnicodeからShift-JISに変換
l=WideCharToMultiByte(CP_ACP, 0, u2s_out, -1, 0, 0, 0, 0)
UTF8toSJIS_Str=ZeroString(l)
WideCharToMultiByte(CP_ACP, 0, u2s_out, -1, UTF8toSJIS_Str, l, 0, 0)
' 後始末
free(u2s_out)
End Function
'BM法文字列サーチ(指定文字列内に特定の文字列があるか?=True or false)(バグあり)
Function SerchTextLine(Src As BytePtr,Text As BytePtr,sLen As DWORD,isFirst As DWORD) As Byte
Dim Skip[255] As DWORD
Dim tLen As DWORD'(,sLen As DWORD
Dim i As DWORD,j As Long,k As Long
Dim letSkip As Byte, letLast As Byte
Dim SP AS Long
tLen=lstrlen(Text)
if tLen=0 then SerchTextLine=FALSE: ExitFunction
For i=0 To 255 : Skip[i]=tLen : Next i
For i=0 To tLen-1
Skip[Text[i]]=tLen-1-i
if Skip[Text[i]]=0 then Skip[Text[i]]=1
Next i
tLen--
letLast=Text[tLen]
For i=tLen To sLen Step Skip[letSkip]
letSkip=Src[i]
if letSkip=letLast Then
j=i-1
k=tLen-1
While Src[j]=Text[k]
if k=0 then
Goto *FOUND
EndIf
j--
k--
Wend
EndIf
Next i
SerchTextLine=FALSE
ExitFunction
*FOUND
Dim Line As BytePtr
Dim EP As Long
SerchTextLine=TRUE
*RETRY
if Src[j-SP]= &H0A Or Src[j-SP]=&H0D Then if isFirst<>0 then SP--: Goto *RETRY
EP=InStrByte(Src+j,&H0D,sLen)'+j
if EP=0 then EP=sLen
if EP-SP<=1 then ExitFunction
EndFunction
'未デバッグ
Function FindStr(text AS BytePtr,size aS DWord,target AS BytePtr) aS Long
Dim i AS Long,tarLen AS Long
tarLen=lstrlen(target)
' [最適化]4バイト以上ある場合はCompareガードをDWordで比較する
if tarLen > 4 Then
Do
if GetDWord(text+i)=GetDWord(target) Then
if CompareString(LOCALE_SYSTEM_DEFAULT,0,text+i,tarLen,target,tarLen)=CSTR_EQUAL Then
ExitDo
Else
i+=tarLen
End If
Endif
i++
If text[i]=0 Then
FindStr=-1
ExitFunction
End If
if i>size Then
FindStr=-2
ExitFunction
End If
Loop
Else
Do
if text[i]=target[0] Then
if CompareString(LOCALE_SYSTEM_DEFAULT,0,text+i,tarLen,target,tarLen)=CSTR_EQUAL Then
ExitDo
Else
i+=tarLen
End If
Endif
i++
If text[i]=0 Then
FindStr=-1
ExitFunction
End If
if i>size Then
FindStr=-2
ExitFunction
End If
Loop
Endif
FindStr=i
End Function
'この関数は作り直したほうがいい
Function InStrByte(Data As BytePtr,Serch As Byte,DataSize As DWORD) As Long
Dim i as DWORD,si As Dword, sLen As Long
Do
if Data[i]=Serch then InStrByte=i : ExitDo
if i=DataSize then
InStrByte=-1
ExitFunction
EndIf
i++
Loop
EndFunction
'APIを使った高速バージョンのダブルクォーテーション抜き
Function DelDQ(Text As BytePtr) As BytePtr
Dim tmp[MAX_PATH] AS Byte
lstrcpy(tmp,Text)
PathRemoveBlanks(tmp)
PathUnquoteSpaces(tmp)
Dim txlen AS DWord
txlen=lstrlen(tmp)
DelDQ=calloc(txlen+2)
memcpy(DelDQ,tmp,txlen+1)
EndFunction
Function DelDQW(Text As WordPtr) As WordPtr
Dim tmp[MAX_PATH] AS Word
lstrcpyW(tmp,Text)
PathRemoveBlanksW(tmp)
PathUnquoteSpacesW(tmp)
Dim txlen AS DWord
txlen=lstrlenW(tmp)
DelDQW=calloc(txlen*2+4)
memcpy(DelDQW,tmp,txlen*2+2)
EndFunction
'LF,CR -> CRLF 戻り値は別メモリなので各自開放すること
Function ConvCRLF(Text As *Byte) As BytePtr
Dim ti As Long
dim ci as long
Dim l as Long
Dim lp as Long
Dim CRLFD As *Byte
ti=0
ci=0
l= lstrlen(Text)
CRLFD=calloc2(l+5)
Do
if Text[ti] = &H0A then '0A(LF)が発見され、
if Text[ti-1] <> &H0D then '0D(CR)その後ろがCR以外の場合
CRLFD[ci]=&H0D 'CRを追加
ci++
l++
lp++
CRLFD=realloc2(CRLFD,l+lp*2) '増えた分のメモリ確保
CRLFD[ci]=&H0A 'そしてLFを追加
Else
CRLFD[ci]=&H0A '違ったら(正しくCRLFになってたら)そのまま
EndIf
Elseif Text[ti] = &H0D then '↑と大体同じ
if Text[ti+1] <> &H0A then
CRLFD[ci]=&H0D
ci++
l++
lp++
CRLFD=realloc2(CRLFD,l+lp*2)
CRLFD[ci]=&H0A
Else
CRLFD[ci]=&H0D
EndIf
Else
CRLFD[ci]=Text[ti] '改行文字と無関係の場合
EndIf
ti++
ci++
if ti = l Then ExitDo
Loop
'メモリを確保し、戻り値をセット
ConvCRLF=calloc2(lstrlen(CRLFD)<<1)
lstrcpy(ConvCRLF,CRLFD)
'消す
free2(CRLFD)
EndFunction
'strcmpの文字数指定バージョン
Function win_strncmpi(str1 AS BytePtr,str2 AS BytePtr,Length AS DWord) AS BOOL
if CompareString(LOCALE_SYSTEM_DEFAULT,NORM_IGNORECASE,str1,Length,str2,Length) = CSTR_EQUAL Then
win_strncmpi=TRUE
Else
win_strncmpi=FALSE
EndIf
EndFunction
Function win_strncmp(str1 AS BytePtr,str2 AS BytePtr,Length AS DWord) AS BOOL
if CompareString(LOCALE_SYSTEM_DEFAULT,0,str1,Length,str2,Length) = CSTR_EQUAL Then
win_strncmp=TRUE
Else
win_strncmp=FALSE
EndIf
EndFunction
'ファイルパスが正しいかチェック
Function IsFilePath(FilePath AS BytePtr) As BOOL
Dim hFile AS HANDLE,dFilePath As BytePtr
if FilePath[0]=&H22 Then
dFilePath=DelDQ(FilePath)
Else
dFilePath=FilePath
EndIf
hFile=CreateFile(dFilePath,GENERIC_READ,FILE_SHARE_READ OR FILE_SHARE_WRITE,ByVal NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL)
If hFile=INVALID_HANDLE_VALUE Then
IsFilePath=FALSE
Else
IsFilePath=TRUE
CloseHandle(hFile)
EndIf
if FilePath<>dFilePath Then free(dFilePath)
EndFunction
'標準Windows
Function sprintfStr(lpFormat As BytePtr)(a As DWord ,b As DWord ,c As DWord ,d As DWord ,e As DWord ,f As DWord ,g As DWord ,h As DWord ,i As DWord ,j As DWord ,k As DWord ,l As DWord ,m As DWord ,n As DWord ) AS String
Dim Buf[PRINTF_BUFFER] AS Byte,ret AS Long
ret=wsprintf(Buf,lpFormat,a,b,c,d,e,f,g,h,i,j,k,l,m,n)
sprintfStr=MakeStr(Buf)
EndFunction
'使うとVCランタイム要求になる
Function doubleToFormatStr(lpFormat As BytePtr)(a As Double) AS String
Dim Buf[PRINTF_BUFFER] AS Byte,ret AS Long
ret=sprintfd(Buf,lpFormat,a)
doubleToFormatStr=MakeStr(Buf)
EndFunction
' =========================
' ファイルの容量などを単位付き文字列にする関数(64bit対応)
' =========================
Const DC2T_BUFFER_SIZE = 16
Function DataCap2Text(val AS QWord, text AS BytePtr, length AS DWord)(isLastDigit AS BOOL) AS BytePtr
Dim buffer[DC2T_BUFFER_SIZE-1] AS Byte
Const DC2T_UNIT_IDX_COUNT = 4
Dim unitTable[DC2T_UNIT_IDX_COUNT] = ["Byte", "KB", "MB", "GB", "TB"/*, "PB", "EB"*/] As BytePtr
Dim unitIndex AS Long
Dim lastDigit AS Long
Dim size As QWord
Dim sign As BytePtr
if val > &H8000000000000000 Then
sign = "-"
size = val * -1
Else
sign = ""
size = val
End If
unitIndex = 0
lastDigit = 0
Do
if size < 1024 Then ExitDo
if unitIndex >= DC2T_UNIT_IDX_COUNT Then ExitDo
lastDigit = ((size Mod 1024) * 10) / 1024
size = size/1024
unitIndex++
Loop
'QWordをcdeclの可変長引数に突っ込むと死ぬので
Dim dwSize AS DWord
dwSize = size
if lastDigit=0 Or isLastDigit=FALSE Then
wsprintf(buffer, ex"%s%d %s",sign,dwSize,unitTable[unitIndex])
Else
wsprintf(buffer, ex"%s%d.%d %s",sign,dwSize,lastDigit,unitTable[unitIndex])
Endif
if length < DC2T_BUFFER_SIZE Then
memcpy(text, buffer, length)
Else
memcpy(text, buffer, lstrlen(buffer)+1)
End If
End Function
' String型
Function DataCap2Text_Str(val AS QWord)(isLastDigit AS BOOL) As String
Dim buffer[DC2T_BUFFER_SIZE-1] AS Byte
DataCap2Text(val,buffer,DC2T_BUFFER_SIZE,isLastDigit)
DataCap2Text_Str=MakeStr(buffer)
End Function
' Print
Sub PrintDataCap(val AS QWord)(isLastDigit AS BOOL)
Dim buffer[DC2T_BUFFER_SIZE-1] AS Byte
DataCap2Text(val,buffer,DC2T_BUFFER_SIZE,isLastDigit)
PrintPtr(buffer)
End Sub
'(文字列, 変換結果を入れるところ, 基数, 1024か1000か)
Function rcAsciiToInt(_str AS BytePtr,result_num AS *Long)(_base AS DWord,b1024 AS BOOL) AS BOOL
Dim ci AS DWord, ni AS DWord 'char index, num index
Dim fNegative AS BOOL, fFixedBase AS BOOL
Dim base AS DWord, siZoom AS DWord
Dim num AS DWord
Dim state AS DWord
Dim str As BytePtr
'基数
'引数のbaseが0だとAuto, それ以外は固定, 最大16まで
if _base=<1 Then
base = 10
Else
base=_base
fFixedBase=TRUE
End If
'si接頭辞があるときに、1000で計算するか1024で計算するか
if b1024 Then
siZoom=1024
Else
siZoom=1000
End If
str=calloc(lstrlen(_str)+4)
lstrcpy(str,_str)
CharLower(str)
Do
'文字列の終了チェック
if str[ci]=0 Then ExitDo
'数値に入る前の処理
if state=0 Then
if str[ci] = &H2D Then
fNegative=TRUE
ci++
continue
Else if str[ci] = &H30 then
'16進 0x
if str[ci+1]=&H78 Then
base=16
ci+=2
continue
'2進 0b
Else if str[ci+1]=&H62 Then
base=2
ci+=2
continue
End If
End If
'数値だったら数値解析ステートに移る
if (str[ci] >= &H30 And str[ci] <= &H39) or _
(base=16 And (str[ci] >= &H61 And str[ci] <= &H66)) Then
state++
'固定基数なのに変わってたらアウト
if fFixedBase And (base <> _base) Then Goto *rcATOI_ERRNUM
Else
ci++
continue
End If
End If
'数値処理
if state=1 Then
if (str[ci] >= &H30 And str[ci] <= &H39) Then
if (str[ci]-&H30) => base Then Goto *rcATOI_ERRNUM
num=num * base
num=num + str[ci]-&H30
Elseif (base=16 And (str[ci] >= &H61 And str[ci] <= &H66)) Then
num = num * base
num = num + str[ci]-&H61 +10
Else
state++
Endif
End If
'SI 接頭辞処理
if state=2 Then
Select Case str[ci]
Case &H6B
num*=siZoom
Case &H6D
num*=siZoom*siZoom
Case &H67
num*=siZoom*siZoom*siZoom
Case &H74
num*=siZoom*siZoom*siZoom*siZoom
End Select
ci++
state++
End If
if state=3 Then
if isSameBin(str+ci,"bit",3) Then
' num=num/8
num=num>>3
End If
state++
End If
if state=4 Then ExitDo
ci++
Loop
if fNegative Then num*=-1
#ifdef _DEBUG
printf(ex"[atoi@RGBA-CRT]str = %s\nfNegative = %d\nbase = %d\nnum = %d\nnum = 0x%X\nnum = %dK\n\n", _
str As DWord, _
fNegative As DWord, _
base, _
num, _
num, _
(num/siZoom) As DWord)
#endif
rcAsciiToInt=TRUE
SetDWord(result_num,num)
ExitFunction
*rcATOI_ERRNUM
rcAsciiToInt=FALSE
End Function
'------------------------------
' Network library
'------------------------------
'URLのパーセントエンコードを解除
Function URLDecode(pFrom As BytePtr) As BytePtr
Dim i = 0 As DWord
Dim ASCII As Byte
URLDecode=calloc(MAX_PATH+5)
Do
if pFrom[i]=&H25 Then '% then
ASCII=Hex2Dec(Chr$(pFrom[i+1])+Chr$(pFrom[i+2])) AS Byte
lstrcat(URLDecode , Chr$(ASCII))
i=i+2
Else
lstrcat(URLDecode , Chr$(pFrom[i]))
EndIf
i++
if pFrom[i] = 0 Then ExitDo
Loop
End Function
'軽量化のため、移動
'Function GetHttpStatusText(HttpStatusNo As Long)
'-------------------------------------
' Dynamic DLL Loader + cdeclLoader
'-------------------------------------
Type DLL_FUNCTION_CELL
'関数ポインタを格納するための変数へのポインタ
ptr AS VoidPtr
'DLLから探す関数名
alias AS BytePtr
EndType
Function GetSystemErrorMessage(ByRef textBuffer AS BytePtr, ErrCode AS DWord) AS DWord
GetSystemErrorMessage = _
FormatMessage( _
FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_IGNORE_INSERTS, _
NULL, ErrCode, LANG_USER_DEFAULT, VarPtr(textBuffer), 0, NULL _
)
End Function
Function DynamicDllLoader(DllName AS BytePtr,func_list AS *DLL_FUNCTION_CELL,count AS Long, bMsgOutput AS BOOL)(opt_text AS BytePtr,hWnd AS HWND) As HINSTANCE
Dim hDll AS HINSTANCE
Dim mainErrMes AS BytePtr
' DLLロード
hDll=LoadLibrary(DllName)
' エラー処理
if hDll=0 Then
' エラーメッセージ作成(有効なら
if bMsgOutput Then
if opt_text=0 Then opt_text=""
mainErrMes=calloc(1024)
wsprintf(mainErrMes,ex"%s%sのロードに失敗しました。",DllName,opt_text)
End If
Goto *DDL_RETURN_ERROR
EndIf
' 関数ロードループ
Dim i AS Long
For i = 0 To count-1
' 代入先ポインタチェック
if func_list[i].ptr=0 or func_list[i].alias=0 Then Goto *DDL_RETURN_ERROR
'Func_List.ptrの先に関数ポインタを格納
SetDWord(func_list[i].ptr,GetProcAddress(hDll,func_list[i].alias))
'取得エラー
if GetDWord(func_list[i].ptr)=NULL Then
if bMsgOutput Then
if opt_text=0 Then opt_text=""
mainErrMes=calloc(1024)
wsprintf(mainErrMes,ex"%sから関数が見つかりませんでした。\nDLLのバージョンを確認してください。\nProcName : %s",DllName,func_list[i].alias)
End If
Goto *DDL_RETURN_ERROR
EndIf
Next i
' 正常終了
DynamicDllLoader=hDll
ExitFunction
*DDL_RETURN_ERROR
'エラー終了時
DynamicDllLoader=NULL
if hDll<>NULL Then _
FreeLibrary(hDll)
if mainErrMes<>NULL Then
Dim SysErrMes AS BytePtr, SysErrMesLen AS DWord
' システムエラーメッセージを取得
SysErrMesLen = GetSystemErrorMessage(ByVal VarPtr(SysErrMes),GetLastError())
if SysErrMesLen + lstrlen(mainErrMes) < 1024 Then
wsprintf(mainErrMes + lstrlen(mainErrMes), ex"\nErrorCode: %d\n%s",GetLastError(),SysErrMes)
Endif
MessageBox(hWnd,mainErrMes,"DLL Load Error",MB_ICONERROR)
LocalFree(SysErrMes) : SysErrMes = NULL
free(mainErrMes)
EndIf
EndFunction
'バイナリに実行可能属性追加
Function SetAsm(asm As *Byte, codesize As Long) As VoidPtr
SetAsm=VirtualAlloc(NULL, codesize, MEM_COMMIT, PAGE_EXECUTE_READWRITE) '追加
memcpy(SetAsm,asm,codesize)
End Function
'↑開放
Sub FreeAsm(func As VoidPtr)
VirtualFree(func, 0, MEM_DECOMMIT or MEM_RELEASE) '追加
End Sub
' ==========================================
' cdeclLoaderはabcdecl.sbpに引っ越しました
' ==========================================
/* ==========================================
* arg() : 可変引数 -> 配列化関数
* ==========================================
*
* ActiveBasicの可変引数は、省略部分を自動でゼロをセットするようになっている。
* printf的な関数を実装するときは省略可能引数を大量に並べる必要があるが、
* 引数が1つであれ4つであれ、ゼロをセットする命令が大量に並ぶことになる。
* argは引数の数分だけ関数を用意することでゼロをセットする命令を削減することができる。
* 詳しくは逆汗して
* ゼロをセットする処理よりこっちの方式のほうがメモリアクセスが多いので遅いかもしれない
*/
'スタックがそのまま配列になっているので、_argにコピーする
'スレッドセーフじゃないけど、滅多に使わないので現状このままで
Typedef ARGT = Long
Dim _arg[15] As ARGT
Function arg() As *ARGT
arg=_arg
End Function
Function arg(a As ARGT) As *ARGT
_arg[0]=a
arg=_arg
End Function
Function arg(a As ARGT,b As ARGT) As *ARGT
_arg[0]=a
_arg[1]=b
arg=_arg
End Function
Function arg(a As ARGT, b As ARGT, c As ARGT ) As *ARGT
memcpy(_arg,VarPtr(a),12)
arg=_arg
End Function
Function arg(a As ARGT, b As ARGT, c As ARGT, d As ARGT) As *ARGT
memcpy(_arg,VarPtr(a),16)
arg=_arg
End Function
Function arg(a As ARGT, b As ARGT, c As ARGT, d As ARGT,e As ARGT ) As *ARGT
memcpy(_arg,VarPtr(a),20)
arg=_arg
End Function
Function arg(a As ARGT,b As ARGT,c As ARGT ,d As ARGT ,e As ARGT ,f As ARGT) As *ARGT
memcpy(_arg,VarPtr(a),24)
arg=_arg
End Function
Function arg(a As ARGT,b As ARGT,c As ARGT ,d As ARGT ,e As ARGT ,f As ARGT,g As ARGT) As *ARGT
memcpy(_arg,VarPtr(a),28)
arg=_arg
End Function
Function arg(a As ARGT,b As ARGT,c As ARGT ,d As ARGT ,e As ARGT ,f As ARGT,g As ARGT,h As ARGT ) As *ARGT
memcpy(_arg,VarPtr(a),32)
arg=_arg
End Function
Function arg(a As ARGT,b As ARGT,c As ARGT ,d As ARGT ,e As ARGT ,f As ARGT,g As ARGT,h As ARGT ,i As ARGT) As *ARGT
memcpy(_arg,VarPtr(a),36)
arg=_arg
End Function
Function arg(a As ARGT,b As ARGT,c As ARGT ,d As ARGT ,e As ARGT ,f As ARGT ,g As ARGT ,h As ARGT ,i As ARGT ,j As ARGT ,k As ARGT ,l As ARGT ,m As ARGT ,n As ARGT) As *ARGT
memcpy(_arg,VarPtr(a),60)
arg=_arg
End Function
'printfはこちらを推奨だが、パフォーマンス対して変わらないのでセーフとする
Dim _print_buf[1024] As Byte,_print_len AS Long
Sub printf2(format AS BytePtr,arglist AS *ARGT)
_print_len=wvsprintf(_print_buf,format,arglist)
PrintPtr(_print_buf,_print_len)
Endsub
'------------------------------
' Console library
'------------------------------
Function GetConsoleCursorDw() AS DWord
Dim CmdPos As CONSOLE_SCREEN_BUFFER_INFO
GetConsoleScreenBufferInfo (_System_hConsoleOut,VarPtr(CmdPos))
memcpy(VarPtr(GetConsoleCursor),CmdPos.dwCursorPosition,sizeof(DWord))
EndFunction
SUb GetConsoleCursor(ByRef pos AS COORD)
Dim CmdPos As CONSOLE_SCREEN_BUFFER_INFO
GetConsoleScreenBufferInfo (_System_hConsoleOut,VarPtr(CmdPos))
memcpy(VarPtr(pos),VarPtr(CmdPos.dwCursorPosition),sizeof(DWord))
EndSub
Sub ConsoleReturnLine()
Dim CmdPos As CONSOLE_SCREEN_BUFFER_INFO
GetConsoleScreenBufferInfo (_System_hConsoleOut,VarPtr(CmdPos))
CmdPos.dwCursorPosition.X=0
SetConsoleCursorPosition(_System_hConsoleOut,GetDWord(VarPtr(CmdPos.dwCursorPosition)))
EndSub
Sub ConsoleLocateRelative(x As Long,y AS Long)
Dim CmdPos As CONSOLE_SCREEN_BUFFER_INFO
GetConsoleScreenBufferInfo (_System_hConsoleOut,VarPtr(CmdPos) AS DWORD)
CmdPos.dwCursorPosition.X += x
CmdPos.dwCursorPosition.Y += y
SetConsoleCursorPosition(_System_hConsoleOut,GetDWord(VarPtr(CmdPos.dwCursorPosition)))
EndSub
Sub ConsoleClearCurrentLine()
Dim CmdPos As CONSOLE_SCREEN_BUFFER_INFO
GetConsoleScreenBufferInfo (_System_hConsoleOut,VarPtr(CmdPos) AS DWORD)
CmdPos.dwCursorPosition.X = 0
SetConsoleCursorPosition(_System_hConsoleOut,GetDWord(VarPtr(CmdPos.dwCursorPosition)))
print String$(CmdPos.dwSize.X," ")
SetConsoleCursorPosition(_System_hConsoleOut,GetDWord(VarPtr(CmdPos.dwCursorPosition)))
EndSub
Sub ConsoleReturnChar(count AS Long)
Dim CmdPos As CONSOLE_SCREEN_BUFFER_INFO
GetConsoleScreenBufferInfo (_System_hConsoleOut,VarPtr(CmdPos))
CmdPos.dwCursorPosition.X-=count
SetConsoleCursorPosition(_System_hConsoleOut,GetDWord(VarPtr(CmdPos.dwCursorPosition)))
EndSub
Enum ABRLC_COLOR
CC_DEFAULT
CC_BLUE = &H01
CC_GREEN = &H02
CC_RED = &H04
End Enum
Sub SetConsoleColor(color AS ABRLC_COLOR)
Dim attr AS Word
if color=CC_DEFAULT Then
attr=CC_BLUE or CC_GREEN or CC_RED
Else
attr=color And &H07
End If
'attr = attr or &H56