Command line interface
BlitzMax Forums/BlitzMax Programming/Command line interface
| ||
Hi all, can blitz open RTF files? The code below is a command line interface (almost) but the openfile command keeps returning a null object error. Also the text field for typing randomly clears itself while you are trying to type.Strict Import MaxGui.Drivers 'Declare gui elements and program structure Local window:TGadget Local button:TGadget Local maintimer:TTimer Global textfield:TGadget Global cmdout:TGadget window = CreateWindow ("",100,100,800,600,Null,WINDOW_DEFAULT|WINDOW_CLIENTCOORDS) textfield = CreateTextField (10,GadgetHeight (window) - 25,520,22,window) button = CreateButton ("OK",530,GadgetHeight (window) - 25,80,24,window,BUTTON_OK) cmdout = CreateTextArea (10,GadgetHeight (window) - 120,600,90,window,TEXTAREA_READONLY) maintimer = CreateTimer (60,Null) SetTextAreaText (cmdout,"Welcome!") ActivateGadget textfield Global protoCMD$ [] = ["open","close","fileview","save","closeapp"] Global eveid, command$', ret = False 'Welcome to Main Loop 'Population 11 lines Repeat 'check the user input and act accordinly eveid = PollEvent () If eveid <> 0 If EventID () = EVENT_WINDOWCLOSE Then Exit 'Check the command input command$ = TextFieldText (textfield) If command$ <> "" If EventSource () = button runfun () EndIf EndIf EndIf WaitTimer (maintimer) Forever End Function runfun () Local cmdsub$, cmdpra$, a$, text$, tni Local file:TStream '= New TStream Local skipread tni = Instr (command$," ") 'Extract the command from the string If tni <> 0 cmdsub$ = Left (command$,tni - 1) cmdpra$ = Right(command$,tni - 1) Else cmdsub$ = command$ EndIf 'Extract the prammaters from the string maybe 'Test for valid command and print to screen For a$ = EachIn protoCMD$ If a$ = cmdsub$ text $ = "Valid command" EndIf Next 'condition for file open '<Here be Dragons!> - can't figure out why file returns null object If cmdsub$ = "open" And cmdpra$ <> "" If FileType (cmdpra$) = 1 file = OpenFile (cmdpra) While Not Eof (file) AddTextAreaText (cmdout, ReadLine (file)) Wend CloseStream (file) Else AddTextAreaText (cmdout, "File not found") EndIf EndIf 'Update window view for user AddTextAreaText (cmdout,"~n" + text$)' + " " + eveid) SetGadgetText (textfield,"") EndFunction |
| ||
Hi, Blitz can open any file including RTF, but it's up to user to parse the information correctly. For instance if I type "hello" in Wordpad and save it as an RTF-file, information inside the file is stored in RTF-format like: {\rtf1\ansi\ansicpg1252\deff0\deflang1035{\fonttbl{\f0\fnil\fcharset0 Calibri;}} {\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\lang11\f0\fs22 hello\par } To solve null problems just insert "debugstop" before code you want to inspect to see what is really happening and are the variables really holding the values that you are expecting. EDIT: If you simply want to open file with default viewer then OpenURL() might do the trick. -Henri |
| ||
Thanks Henri, type tni = Instr (command$," ") 'Extract the command from the string If tni <> 0 cmdsub$ = Left (command$,tni - 1) cmdpra$ = Right(command$,(Len command$) - tni) Else cmdsub$ = command$ EndIf insted of tni = Instr (command$," ") 'Extract the command from the string If tni <> 0 cmdsub$ = Left (command$,tni - 1) cmdpra$ = Right(command$,tni - 1) Else cmdsub$ = command$ EndIf and it seems to work. Also the textfield clearing itself seems to have stopped but I am not sure if it will come back as I can't remember fixing it. So I was wondering whether you had that problem. |
| ||
Hi, no I didn't experience textfield clearing, but I only tested it briefly. There are also convenience methods in String type which might help you (see MaxIDE right panel for Help / Strings). For instance, "Split" is a easy way to split sentence like command line with space separator like: Local s:String = "Open Some FILE" Local a:String[] = s.split(" ") If a[0].toupper() = "OPEN" Then Print "Trying to open file.." If a[1].toupper() = "SOME" Then Print "..named " + a[1] + ".." If a[2].toupper() = "FILE" Then Print "..and " + a[2] + "." -Henri |
| ||
Hi Henri, how does split work and how would it be used to get more than two phrases (or any other number) from a command input? |
| ||
HiLocal cmd$="OPEN SOME FILE" Local words$[]=cmd.split(" ") ' --> split each time you find a ' ' (=space), you can use what you want (, * | **** etc) So if want to parse more phrases, you could decide for a 'terminator' (like ;) |
| ||
Hi degac, I don't fully understand whats happening but it seems to work. Is there any way to write the process with one .split(" ") instead of two? I only want it to split for a space. Local com$[], text$ = "Command not valid" 'split each commands... For Local p$=EachIn command.split(";") Print p 'parse each phrase com=p.split(" ") com=com[..3] 'for security Select com[0].toupper() 'com[0] is the MAIN command (OPEN,SAVE,view etc..) Case "open" text$ = "OPEN File '"+com[1]+"'" Case "save" text$ = "SAVE file '"+com[1]+"'" End Select Next |
| ||
Hi degac I was looking for something like this http://www.dotnetperls.com/split. Am I right in saying that in Blitz, split also returns an array? |
| ||
Yes. -Henri |