forked from fantaisie-software/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMisc.pb
386 lines (322 loc) · 11.2 KB
/
Misc.pb
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
; --------------------------------------------------------------------------------------------
; Copyright (c) Fantaisie Software. All rights reserved.
; Dual licensed under the GPL and Fantaisie Software licenses.
; See LICENSE and LICENSE-FANTAISIE in the project root for license information.
; --------------------------------------------------------------------------------------------
; Returns the size of the value of type at *pointer in
; the data that comes from the debugger.
;
; Note: structures return 0, as they are not presented as a single value,
; but only as its structure fields
;
; Handles array, list, map etc
;
Procedure GetValueSize(type, *Pointer, Is64bit)
If IS_POINTER(type) Or IS_INTEGER(type)
If Is64bit
ProcedureReturn 8
Else
ProcedureReturn 4
EndIf
Else
Select type & #TYPEMASK
Case #TYPE_BYTE, #TYPE_ASCII: ProcedureReturn 1
Case #TYPE_WORD, #TYPE_UNICODE: ProcedureReturn 2
Case #TYPE_LONG: ProcedureReturn 4
Case #TYPE_STRUCTURE: ProcedureReturn 0 ; has no value
Case #TYPE_FLOAT: ProcedureReturn 4
Case #TYPE_CHARACTER: ProcedureReturn 4 ; its translated into a long actually
Case #TYPE_DOUBLE: ProcedureReturn 8
Case #TYPE_QUAD: ProcedureReturn 8
; strings are stored in the format of the external debugger
Case #TYPE_FIXEDSTRING, #TYPE_STRING
ProcedureReturn MemoryStringLengthBytes(*Pointer) + #CharSize
Case #TYPE_ARRAY
; string with dimensions (ascii)
ProcedureReturn MemoryAsciiLength(*Pointer) + 1
Case #TYPE_LINKEDLIST
; size, current element (both integer)
If Is64bit
ProcedureReturn 8*2
Else
ProcedureReturn 4*2
EndIf
Case #TYPE_MAP
; size (integer), iscurrent (byte), current element (external)
If Is64bit
Size = 8
Else
Size = 4
EndIf
If PeekB(*Pointer + Size) ; IsCurrent
ProcedureReturn Size + 1 + MemoryStringLengthBytes(*Pointer + Size + 1) + #CharSize
Else
ProcedureReturn Size + 1
EndIf
EndSelect
EndIf
EndProcedure
; Specify the type to detect "ByRef" parameters (for Array,List,Map)
Procedure.s ScopeName(scope, type = 0)
Select scope
Case #SCOPE_MAIN
ProcedureReturn "Main"
Case #SCOPE_GLOBAL
ProcedureReturn "Global"
Case #SCOPE_THREADED
ProcedureReturn "Threaded"
Case #SCOPE_LOCAL
If IS_PARAMETER(type)
ProcedureReturn "ByRef"
Else
ProcedureReturn "Local"
EndIf
Case #SCOPE_STATIC
ProcedureReturn "Static"
Case #SCOPE_SHARED
ProcedureReturn "Shared"
Case #SCOPE_PARAMETER
ProcedureReturn "ByRef" ; for the WatchList only!
Default
ProcedureReturn ""
EndSelect
EndProcedure
; prefix a module name if it is not empty
Procedure.s ModuleName(Name$, ModuleName$)
If ModuleName$ = ""
ProcedureReturn Name$
Else
ProcedureReturn ModuleName$ + "::" + Name$
EndIf
EndProcedure
Procedure.s GetDebuggerFile(*Debugger.DebuggerData, LineNumber)
FileNumber = DebuggerLineGetFile(LineNumber)
If FileNumber > *Debugger\NbIncludedFiles
ProcedureReturn ""
ElseIf FileNumber = 0
ProcedureReturn *Debugger\FileName$
Else
*Pointer = *Debugger\IncludedFiles
For i = 0 To FileNumber ; count from 0 to skip the "sourcepath" at the start
*Pointer + MemoryAsciiLength(*Pointer) + 1
Next i
FileName$ = PeekUTF8(*Pointer)
; NOTE: the FileName$ can contain "../", so we need to remove this
;
*Cursor.Character = @FileName$
While *Cursor\c
If *Cursor\c = Asc(#Separator)
If PeekS(*Cursor, 4) = #Separator + ".." + #Separator
; remove the previous directory name
*BackCursor.Character = *Cursor - #CharSize
While *BackCursor >= @FileName$
If *BackCursor\c = Asc(#Separator)
Break
EndIf
*BackCursor - #CharSize
Wend
If *BackCursor < @FileName$
; oops, more ".." in the string than directories!
ProcedureReturn ""
EndIf
; poking in the string is ok, since it can only get shorter by this
PokeS(*BackCursor, PeekS(*Cursor + 3*#CharSize))
; Make sure the cursor stays inside the string.
; Otherwise, if removing a large dir towards the string end, *Cursor might
; end up outside of the valid string and create an endless loop
*Cursor = *BackCursor
ElseIf PeekS(*Cursor, 3) = #Separator + "." + #Separator
; simply remove this reference to the own directory
PokeS(*Cursor, PeekS(*Cursor + 2*#CharSize))
Else
*Cursor + #CharSize
EndIf
Else
*Cursor + #CharSize
EndIf
Wend
ProcedureReturn FileName$
EndIf
EndProcedure
Procedure.s GetDebuggerRelativeFile(*Debugger.DebuggerData, LineNumber)
FileName$ = GetDebuggerFile(*Debugger, LineNumber)
If *Debugger\IncludedFiles
SourcePath$ = PeekUTF8(*Debugger\IncludedFiles) ; first is the source path
FileName$ = CreateRelativePath(SourcePath$, FileName$)
EndIf
If FileName$ = ""
ProcedureReturn Language("FileStuff","NewSource")
EndIf
ProcedureReturn FileName$
EndProcedure
; get a string representation of the flags register
;
; /* Flags register
; |11|10|F|E|D|C|B|A|9|8|7|6|5|4|3|2|1|0|
; | | | | | | | | | | | | | | | | | '--- CF Carry Flag
; | | | | | | | | | | | | | | | | '--- 1
; | | | | | | | | | | | | | | | '--- PF Parity Flag
; | | | | | | | | | | | | | | '--- 0
; | | | | | | | | | | | | | '--- AF Auxiliary Flag
; | | | | | | | | | | | | '--- 0
; | | | | | | | | | | | '--- ZF Zero Flag
; | | | | | | | | | | '--- SF Sign Flag
; | | | | | | | | | '--- TF Trap Flag (Single Step)
; | | | | | | | | '--- IF Interrupt Flag
; | | | | | | | '--- DF Direction Flag
; | | | | | | '--- OF Overflow flag
; | | | | | '--- ?
; | | | | '--- IOPL I/O Privilege Level (286+ only)
; | | | '--- NT Nested Task Flag (286+ only)
; | | '--- 0
; | '--- RF Resume Flag (386+ only)
; '---- VM Virtual Mode Flag (386+ only)
; */
; Procedure.s GetFlagsRegisterString(Flags.l)
; Result$ = ""
;
; If Flags & 1<< 0: Result$ + "CF, ": EndIf
; If Flags & 1<< 2: Result$ + "PF, ": EndIf
; If Flags & 1<< 4: Result$ + "AF, ": EndIf
; If Flags & 1<< 6: Result$ + "ZF, ": EndIf
; If Flags & 1<< 7: Result$ + "SF, ": EndIf
; If Flags & 1<< 8: Result$ + "TF, ": EndIf
; If Flags & 1<< 9: Result$ + "IF, ": EndIf
; If Flags & 1<<10: Result$ + "DF, ": EndIf
; If Flags & 1<<11: Result$ + "OF, ": EndIf
; If Flags & 1<<13: Result$ + "IOPL, ": EndIf
; If Flags & 1<<14: Result$ + "NT, ": EndIf
; If Flags & 1<<16: Result$ + "RF, ": EndIf
; If Flags & 1<<17: Result$ + "VM, ": EndIf
;
; If Result$ = ""
; ProcedureReturn ""
; Else
; ProcedureReturn Left(Result$, Len(Result$)-2) ; cut the last ", "
; EndIf
;
; EndProcedure
; Val for hex values
;
Procedure ValHex(String$)
String$ = UCase(String$)
*Cursor.Character = @String$
Result = 0
While *Cursor\c
Result << 4
If *Cursor\c >= '0' And *Cursor\c <= '9'
Result + *Cursor\c - '0'
ElseIf *Cursor\c >= 'A' And *Cursor\c <= 'F'
Result + *Cursor\c - 'A' + 10
Else
Break
EndIf
*Cursor + #CharSize
Wend
ProcedureReturn Result
EndProcedure
; replacements for the normal functions in Debug output/variable viewer and such
; for now it just cuts any remaining 0's, but this can easily be extended
; to allow other display of floats too (like 123e-5 or something)
;
Procedure.s StrF_Debug(Value.f)
String$ = StrF(Value, 14) ; 14 digits should be ok for normal float
If FindString(String$, ".", 1) = 0
ProcedureReturn String$
Else
length = Len(String$)
While Mid(String$, length, 1) = "0"
length - 1
Wend
If Mid(String$, length, 1) = "."
length + 1
EndIf
ProcedureReturn Left(String$, length)
EndIf
EndProcedure
Procedure.s StrD_Debug(Value.d, Digits = 16)
String$ = StrD(Value, Digits)
If FindString(String$, ".", 1) = 0
ProcedureReturn String$
Else
length = Len(String$)
While Mid(String$, length, 1) = "0"
length - 1
Wend
If Mid(String$, length, 1) = "."
length + 1
EndIf
ProcedureReturn Left(String$, length)
EndIf
EndProcedure
Procedure.s StrD_Science(Value.d)
abs.d = Abs(Value)
exp.i = 0
If Value = 0.0
ProcedureReturn "0"
ElseIf abs >= 1.0 And abs < 10.0
ProcedureReturn StrD_Debug(Value)
ElseIf abs < 1.0
While abs < 1.0
abs * 10.0
Value * 10.0
exp + 1
Wend
ProcedureReturn StrD_Debug(Value, 10) + "E-" + Str(exp)
Else
While abs >= 10.0
abs / 10.0
Value / 10.0
exp + 1
Wend
ProcedureReturn StrD_Debug(Value, 10) + "E" + Str(exp)
EndIf
EndProcedure
Procedure.s DebuggerTitle(FileName$)
If DisplayFullPath
ProcedureReturn FileName$
Else
ProcedureReturn GetFilePart(FileName$)
EndIf
EndProcedure
Procedure CatchImageDPI(Image, *Address)
Result = CatchImage(Image, *Address)
If Image = #PB_Any
Image = Result
EndIf
ResizeImage(Image, DesktopScaledX(ImageWidth(Image)), DesktopScaledY(ImageHeight(Image)))
ProcedureReturn Result
EndProcedure
Procedure ScrollEditorGadgetToEnd(Gadget)
CompilerIf #CompileLinuxGtk
Static *Mark
If *Mark = 0
*Mark = gtk_text_mark_new_("EndDocumentMark", #True);
EndIf
*Buffer = gtk_text_view_get_buffer_(GadgetID(Gadget))
gtk_text_buffer_get_end_iter_(*Buffer, @iter.GtkTextIter)
gtk_text_buffer_add_mark_(*Buffer, *Mark , @iter)
gtk_text_view_scroll_to_mark_(GadgetID(Gadget), *Mark , 0.0, #True, 0.0, 0.17) ; gtk_text_view_scroll_to_iter_() is broken, so use the mark system
gtk_text_buffer_delete_mark_(*Buffer, *Mark)
CompilerEndIf
CompilerIf #CompileLinuxQt
Qt_ScrollEditorToBottom(GadgetID(Gadget))
CompilerEndIf
CompilerIf #CompileWindows
SendMessage_(GadgetID(Gadget), #WM_VSCROLL, #SB_BOTTOM, #Null)
CompilerEndIf
CompilerIf #CompileMac
; Scroll the editor gadget to the bottom
; This function is very very slow, that's why we defer it (https://www.purebasic.fr/english/viewtopic.php?f=24&t=55924)
TextViewLength = CocoaMessage(0, CocoaMessage(0, GadgetID(Gadget), "string"), "length")
; Avoid scrolling if we've insufficient length. This may occur if an empty string is logged.
; Supplying a negative location value will crash/stall the application due to NSRange being
; composed of unsigned integers, meaning -1 is misinterpreted as the massive 2^64-1.
; See: https://github.com/fantaisie-software/purebasic/issues/224
If TextViewLength > 0
Range.NSRange\location = TextViewLength - 1
Range\length = 1
CocoaMessage(0, GadgetID(Gadget), "scrollRangeToVisible:@", @Range);
EndIf
CompilerEndIf
EndProcedure