Pointer doesnt work properly with specific name
Posted: Thu Aug 04, 2016 10:07 am
Hello everyone
I got a really strange situation, but most of times its just me not seeing something obvious.
I have wrote a procedure, that splits text by arguments
Example: "echo hello world" -> echo, hello, world
Example: "echo "hello world" alotofspaces" -> echo, hello world, alotofspaces
Procedure works as I need, but problem starts with saving results. Im passing a pointer *Arg to the procedure (*ArgArray there), and procedure must write pointer to each splitted argument. As much as I know about pointers, it must work.
Actually you dont need to take a look at procedure at all.
Weird thing is that last Debug *Arg shows not an address. Last time I just got value 2.
Tho If you remove QueryPerformanceCounter *Arg shows right address. Probably thats something about stack / pointer register idk. Not really that good yet.
But If you change *Arg on, example, *Argz, everything gonna work okay.
So, whats wrong with *Arg? I have no idea, if only *Arg is not used somewhere under PB, but I really dubt.
Im really interested in reason. Thanks.
I got a really strange situation, but most of times its just me not seeing something obvious.
I have wrote a procedure, that splits text by arguments
Example: "echo hello world" -> echo, hello, world
Example: "echo "hello world" alotofspaces" -> echo, hello world, alotofspaces
Procedure works as I need, but problem starts with saving results. Im passing a pointer *Arg to the procedure (*ArgArray there), and procedure must write pointer to each splitted argument. As much as I know about pointers, it must work.
Actually you dont need to take a look at procedure at all.
Code: Select all
Procedure Engine_ParseCommandLine(Command.s, *ArgArray)
Argc = 0 : Symbol.s = "" : BackToDQuote = #False :
*SelectionStart = @Command
LenOfCmd = Len(Command)
For *CursorPos = @Command To @Command+LenOfCmd
Symbol.s = PeekS(*CursorPos, 1)
IgnoreEmptyArgument = #False
If Symbol = " " Or *CursorPos = @Command+LenOfCmd
Argument.s = PeekS(*SelectionStart, *CursorPos-*SelectionStart)
*SelectionStart = *CursorPos+1
ElseIf Symbol = #DOUBLEQUOTE$
Argument.s = PeekS(*SelectionStart, *CursorPos-*SelectionStart)
; About situation, when SomeRandomArgument"SecondArgument"
BackToDQuote = #True : Goto ParseCommandLine_AddArgument : ParseCommandLine_DQuote:
If *CursorPos+1 < @Command+LenOfCmd
Pos = FindString(PeekS(*CursorPos+1), #DOUBLEQUOTE$)
If Pos
Argument = PeekS(*CursorPos+1, Pos-1) ; We gonna copy everything from dquote to dquote
*CursorPos+Pos ; Moving loop till second dquote (skip all these characters in quotes)
*SelectionStart = *CursorPos+1 ; Moving our selection pointer right after closing dquote
IgnoreEmptyArgument = #True
Else
Argument = PeekS(*CursorPos+1) ; Second (close) dquote not found, so we gonna copy everything till end
*CursorPos = @Command+LenOfCmd ; Moving loop in end.
EndIf
EndIf
EndIf
;
ParseCommandLine_AddArgument:
If Argument.s <> "" Or IgnoreEmptyArgument = #True; Ignoring empty spaces
Argument = ReplaceString(Argument, "\dq", #DQUOTE$)
*NewArg = AllocateMemory(Len(Argument.s)+1) ; Allocate memory for new argument
PokeS(*NewArg, Argument) ; Writing Argument intro memory
PokeI(*ArgArray+Argc*4, *NewArg) ; Writing pointer to our argument intro pointers array
Argument = "" : Argc+1
EndIf
If BackToDQuote = #True : BackToDQuote = #False : Goto ParseCommandLine_DQuote : EndIf
Next
ProcedureReturn Argc
EndProcedure
*Arg = AllocateMemory(28)
Debug *Arg
timer = 0
timer2 = 0
QueryPerformanceCounter_(@timer);
Engine_ParseCommandLine("echo hello world", *Arg)
QueryPerformanceCounter_(@timer2) ; Remove this line
Debug timer2-timer
Debug *ArgTho If you remove QueryPerformanceCounter *Arg shows right address. Probably thats something about stack / pointer register idk. Not really that good yet.
But If you change *Arg on, example, *Argz, everything gonna work okay.
Code: Select all
*Argz = AllocateMemory(28)
Debug *Argz
timer = 0
timer2 = 0
QueryPerformanceCounter_(@timer);
Engine_ParseCommandLine("echo hello world", *Argz)
QueryPerformanceCounter_(@timer2)
Debug timer2-timer
Debug *ArgzIm really interested in reason. Thanks.