Ooops
March 01, 2021, 11:14:42 PM

Author Topic: [bb] Commandline Splitting by Dan [ 1+ years ago ]  (Read 517 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Commandline Splitting by Dan [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : Commandline Splitting
Author : Dan
Posted : 1+ years ago

Description : This function Splits the Space separated Commandline parameters into the CMDLine type.
Usefull for getting the Filenames dropped from the explorer to the .exe file.

The Space chars between " " are ignored, so the whole parameter is passed to the Type.

With the Return value, you can check if any parameters were passed, or how many are in.


Code :
Code: BlitzBasic
  1. ; Version: 1.0
  2. ; Author: Dan (at www.blitzbasic.com forum)
  3. ; Email: -
  4. ; Copyright: PD
  5. ; Description: Returns space separated Commandline parameters
  6. ;              Parameter in Quotes ".." is passed as is
  7. ;              Read the description from the CommandLine_Init function
  8. ;====================================================================
  9.  
  10. ;Put type declaration at the start:
  11.  
  12. Type CMDLine
  13.      Field arg$
  14. End Type
  15.  
  16. ;Demo: uncomment to test, set the commandline in the IDE's options
  17. ;BB ide Menu: Program/Program Command Line ...
  18. ;c:b3d est.bmp /hlp "c:b 3dfile name.txt" c:windowswin.com /PNG /type bla bla .jpg c:dd1.jpg/ lala
  19.  
  20. ;Graphics 1650,1050,32,2
  21. ;
  22. ;A=Commandline_Init()   ;Must be called first, to split the commandline into parts, it returns a number of Commands.
  23. ;;
  24. ;Print "Commandline Captured: "+a
  25. ;For x=1 To a
  26. ;       Print x+" "+Commandline_Read$(x)
  27. ;       Delay 100  ; needed only for the demo display slowdown
  28. ;Next
  29. ;WaitKey
  30. ;/Demo
  31.  
  32. Function Commandline_Read$(nr)
  33. ; Returns the commandline at index nr
  34.         If nr>0
  35.                 this.CMDLine = First CMDLine
  36.                 For this.CMDLine = Each CMDLine
  37.                         x=x+1
  38.                         If nr=x Then Return thisarg$
  39.                 Next
  40.         End If
  41.         Return False
  42. End Function
  43.  
  44. Function Commandline_Init()
  45. ;This function returns the number of commandline, added to the CMDline type
  46. ;To use this function in your program you have to declare the rules for passing the commandline to this app
  47. ;
  48. ;This function works as :
  49. ; Every commandline parameter will be split at the 1st space position
  50. ; example:/hlp c:files1.bmp
  51. ; will return 2 as parameter value and
  52. ;/hlp
  53. ;c:files1.bmp
  54. ;
  55. ;Space characters are stripped from each parameter, but not if they were enclosed in the Double Quote "
  56. ; singlequote ' and Doublesinglequote '' are ignored
  57. ; if the filename has space chars in it eg: My Birthdayphoto.jpg  the file passed to this program
  58. ; from the commandline shall be like this: "My Birthdaysphoto.jpg" (enclosed in quotes)
  59. ; This is automaticaly done in windows (8.1) when dropping files onto the .exe
  60. ; If the file/pathname contains a space char " " in it, the whole drive:path/filename.ext shall be enclosed in quotes
  61. ; eg. "c:my filesmy file.jpe g"
  62. ; This function does not check if the file exists, because the parameter can be nonfile, so it is on your part to do this
  63. ; This function returns an value of maximum parameters added, so you can check if anything were added aswell
  64. ; and access the single items within the limits of this value
  65. ; Call this function like: A=Commandline_Init()
  66.  
  67.         qa$=CommandLine()
  68.         CMD_start=1
  69.         CMD_Quote=0
  70.         CMD_index=0
  71.         x=0
  72.         ca$=""
  73.         a$=""
  74.         If Len(qa$)=0 Goto CMDreadEnd
  75.                
  76.                 .CMDread
  77.                 x=x+1  
  78.                 ca$=ca$+Mid$(qa$,x,1)
  79.                
  80. ;    Print LSet ("x="+x,6)+":"+Right$(Hex$(Asc(ca$)),2)+" = "+ca$+" --"+CMD_Start+" ->"+CMD_Quote    ; Uncomment for debug             
  81.                 a$=Trim(Mid$(qa$,CMD_Start,X-CMD_Start))
  82.                 If ca$=" " And CMD_Quote=0
  83.                         If CMD_Start=1
  84.                                 If a$>""
  85.                                         CMD_index=CMD_AddType(CMD_Index,a$)
  86.                                 EndIf
  87.                                
  88.                                 CMD_start=x
  89.                         Else
  90.                             A$=Trim(Mid$(qa$,CMD_Start+1,X-CMD_Start))
  91.                                 If A$>""
  92.                                         CMD_index=CMD_AddType(CMD_Index,a$)
  93.                                 EndIf
  94.                                
  95.                                 CMD_Start=x
  96.                         EndIf
  97.                 EndIf
  98.                
  99.                 If ca$=Chr$(34)
  100.                         If x-CMD_Start>0 And CMD_Quote=0
  101.                                 If a$>""
  102.                                         CMD_index=CMD_AddType(CMD_Index,a$)
  103.                                 EndIf
  104.                                 CMD_Start=x
  105.                                
  106.                         EndIf
  107.                         If CMD_Quote=0
  108.                                 CMD_Start=x+1
  109.                                 CMD_Quote=1
  110.                         Else
  111.                             If a$>""
  112.                                         CMD_index=CMD_AddType(CMD_Index,a$)
  113.                                 EndIf  
  114.                                 CMD_start=x+1
  115.                                 CMD_Quote=0
  116.                                
  117.                         EndIf
  118.                 EndIf
  119.                
  120.                 If x>Len(qa$)
  121.                         If A$>""
  122.                                 CMD_index=CMD_AddType(CMD_Index,a$)
  123.                         EndIf
  124.                         Goto CMDreadEnd
  125.                 EndIf
  126.                
  127.                 ca$=""
  128.                
  129.                 Goto CMDread
  130.                
  131.                 .CMDreadEnd
  132.     ;Print CMD_Index                                                                                                                                                                    ;Uncomment for debug
  133.                 Return CMD_index
  134.                
  135. End Function
  136.  
  137. Function CMD_AddType(index,A$)
  138. ;Used to shorten the code from the Commandline_Init() function
  139.                                         CMD_index=index+1
  140.                                         This.Cmdline = New CMDLine
  141.                                         thisarg$=A$
  142.                                         Return CMD_index
  143. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal