Page 1 of 1

Select-Case section maker tool

Posted: Fri Dec 27, 2013 3:00 am
by Zebuddi123
Hi To All & Merry Xmas + New Year :D

Here is a small tool that produces a Select case EndSelect section that may be useful to some :!: Basically a small GUI tool that takes a list of words / constants / numbers etc a cvs list if you like, delimeter can be whatever and can be specified in the string gadget.

Features a checkboxgadget for quoted strings ie ( Case "foobar" ), so you could pass 100 constants to the clipboard via copy from a browser/text editor/source code etc press the make button and you have a Select case EndSelect section complete in a jiffy.

after initial processing you can alter the delimiter - reprocess with the original clipboard data. On Exit the processed data is copied to the clipboard ready for pasting into the ide. just saved me hours this afternoon :shock: :lol:

Should be cross platform :?

code updated 03:00 codeline --

Code: Select all

clipboard_text.s=old_clipboard.text.s
--- caused initial error no clipboard data on first run should be ----

Code: Select all

old_clipboard_text.s=clipboard_text.s


Update: Added --- Default section

Zebuddi. :)

Code: Select all

EnableExplicit

Global Window_0
Global Window_0_Button_MakeCase
Global Window_0_String_Delimeter
Global Window_0_Editor_1
Global Window_0_Text_Delimeter
Global Window_0_Option_String
Global Window_0_Option_Constant
Global Window_0_Option_Numb
Global StatusBar_Window_0 
Global clipboard_text.s  ,  delimeter.s  ,  Finished_data.s  ,  collacted_string.s  ,  regex_spaces.i  ,  delimeter_position.i  ,  numb.i  ,  i.i  ,  extracted_word.s , ls.i , delimeter_length.i , counter.i
Global old_clipboard_text.s , old_delimeter.s ,c.i
Define.l Event  

Global NewList cbdata.s()

Procedure OpenWindow_Window_0()
	Window_0 = OpenWindow(#PB_Any , 482 ,  41 ,  400 ,  430,  "Select-Case  section maker" ,  #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
	If Window_0
		StatusBar_Window_0 = CreateStatusBar(#PB_Any, WindowID(Window_0))
		If StatusBar_Window_0
			AddStatusBarField(#PB_Ignore)
			StatusBarText(StatusBar_Window_0, 0, "Entries :  ")
		EndIf
		Window_0_Button_MakeCase = ButtonGadget(#PB_Any ,  10 ,  10 ,  70 ,  20 ,  "Make")
		Window_0_Text_Delimeter = TextGadget(#PB_Any ,  85, 13, 60, 16 ,  "Delimeter=")
		Window_0_String_Delimeter = StringGadget(#PB_Any ,  145 ,  10 ,  170 ,  19 ,  "")
		Window_0_Option_String = CheckBoxGadget(#PB_Any, 320, 10, 60, 20, "Quoted")
		Window_0_Editor_1 = EditorGadget(#PB_Any ,  10 ,  35 ,  380 ,  355)
	EndIf
EndProcedure
Procedure reset()
	clipboard_text.s = ""   :    delimeter.s  = "" :    Finished_data.s  = "" :    collacted_string.s  = ""  :    extracted_word.s = "" 
	delimeter_position.i  = 0 :    numb.i  = 0  :    i.i  = 0 : ls.i = 0: delimeter_length.i = 0 : counter.i = 0
	ClearGadgetItems(Window_0_String_Delimeter)	
EndProcedure


regex_spaces.i=CreateRegularExpression(#PB_Any , "\s+") ; reduces multipule consecutive white spaces to one single one

OpenWindow_Window_0()
ClearClipboard()
Repeat
	Event = WaitWindowEvent()
	Select Event
			
		Case #PB_Event_Gadget
			
			Select EventGadget()
					
				Case Window_0_Button_MakeCase
					
					reset()
					ClearList(cbdata())
					
					delimeter.s=GetGadgetText(Window_0_String_Delimeter) : 
					clipboard_text.s=GetClipboardText() 
					CallDebugger
					c+1
					Debug "run "+Str(c)+"  "+ clipboard_text.s
					If old_delimeter.s<>delimeter Or delimeter.s>"" ; check to see if delimeter has changed  and data already processed if true revert to original clipboard text
						old_clipboard_text.s=clipboard_text.s                  ; for reprocessing
						old_delimeter.s=delimeter.s	
					EndIf
					
						
;{ -- error checking						
					If delimeter.s = "" Or clipboard_text.s = "" Or CountString(clipboard_text,delimeter.s)=0
						
						If delimeter.s = ""
							MessageRequester("Delimeter Error" , "Delimeter String Can Not Be  Blank")
							
						ElseIf clipboard_text.s = ""
							MessageRequester("Data Error" , "ClipBoard Cleared @ Program Start ..... No Data on The Clipboard")
						ElseIf CountString(clipboard_text,delimeter.s)=0
							MessageRequester("Delimeter Error" , "No " + Chr(34) + delimeter.s + Chr(34) + "  Match`s in the ClipBoard Data")
						EndIf
;}						
					Else			
;{ --  Main Processing	
						clipboard_text.s = ReplaceString(clipboard_text.s , Chr(13) , "" , #PB_String_NoCase , 1) ; remove CR`s
						clipboard_text.s = ReplaceString(clipboard_text.s , Chr(10) , "" , #PB_String_NoCase , 1) ; remove LF`s
						clipboard_text.s = ReplaceString(clipboard_text.s , Chr(9) , "" , #PB_String_NoCase , 1)   ; remove Tab`s 
						clipboard_text.s = Trim(clipboard_text.s)
						clipboard_text.s = ReplaceRegularExpression(regex_spaces , clipboard_text.s  , " ")              ; reduce white space`s to single white space
						clipboard_text.s = ReplaceString(clipboard_text.s , " , " , ",")                                                             ; replace white spaced comma`s to just a comma seperator
						delimeter_position.i = 0
						delimeter.s = GetGadgetText(Window_0_String_Delimeter)
						
						;{ -- check for multipul white space as the delimeter  and show in window tiltle as wont show clearly in string gadget
						delimeter_length.i =Len(delimeter.s)
						For i = 1 To delimeter_length.i
							If Mid(delimeter.s , i , 1) = " "
								counter.i+1
							EndIf
						Next
						
						If counter.i = delimeter_length.i
							SetWindowTitle(Window_0 , " USING --- " + Str(delimeter_length.i) +Chr(34)+" "+Chr(34) + " white space" )
						EndIf
						;}

						numb = CountString(clipboard_text.s , delimeter.s)
						
						For i.i = 0 To numb.i
							delimeter_position.i = FindString(clipboard_text.s , delimeter.s, 1)
							extracted_word.s = Mid(clipboard_text.s  ,  0  ,  delimeter_position.i-1)
							AddElement(cbdata()) : cbdata()=extracted_word.s
							
							;{ -- check to see if we need data items quoted							
							If GetGadgetState(Window_0_Option_String)=1
								cbdata() ="Case " + Chr(34) + cbdata() +  Chr(34)  + Chr(13)+Chr(13)
							Else
								cbdata()="Case " + cbdata()  +  Chr(13) + Chr(13)
								cbdata()=ReplaceString(cbdata() , Chr(34) , "")
							EndIf
							;}
							
							clipboard_text.s = ReplaceString(clipboard_text.s ,  extracted_word.s+delimeter.s , "" ,  #PB_String_NoCase , 1 , 1) ;remove the last data item and delimeter from the clipboard text$ 
							clipboard_text.s = Trim(clipboard_text.s)+delimeter.s  ; add a delimeter to end of clipboard text$ so we dont miss the last data item out
							collacted_string.s+Chr(9)+cbdata()  ; collate the data item`s and Tab them
							
						Next	
						
						
						collacted_string.s = Left(collacted_string , Len(collacted_string.s)-( Len(Chr(13)) )) ; remove the last CRLF else "EndSelect" jumps down two places
						
						Finished_data.s = "Select " + Chr(13) + collacted_string.s + Chr(13) +Chr(9)+"Default "+Chr(13)+Chr(13)+ "EndSelect"  ; prepared data
						
						ClearGadgetItems(Window_0_Editor_1)
						SetGadgetText(Window_0_Editor_1 , Finished_data.s)
						ls.i=ListSize(cbdata())
						StatusBarText(StatusBar_Window_0 , 0 , "Entries: "+Str(ls.i))
						
					EndIf
					
			EndSelect
			
		Case #PB_Event_CloseWindow
			
			Select EventWindow()
				Case Window_0
					ClearClipboard()
					SetClipboardText(Finished_data.s)
					CloseWindow(Window_0)
					Window_0 = 0
					FreeList(cbdata())
					Break
			EndSelect
			
	EndSelect
ForEver

Re: Select-Case section maker tool

Posted: Fri Dec 27, 2013 8:15 am
by jamirokwai
Hi Zebuddi,

could you give an example, please?
I tried your tool on my Mac (PB 5.21), but could not get it to work.

Thanks,
J.

Re: Select-Case section maker tool

Posted: Fri Dec 27, 2013 1:24 pm
by Zebuddi123
Hi Jamirolwai hope this helps -- you should be able if you chose to create a select section with hundreds/thousands case statements :lol: from a list in seconds.

if you use a white space as the delimiter, because the white space is not visible in the string gadget the window titlebar will indicate that you are using white space/s else delimiter is shown as normal in the string gadget.

Copy a list
Image

Enter a space in the delim string gadget and click make. you will see it translated into a select section as a visualizer, exiting the program copies the select section to the clipboard for pasting into the ide.
Image

Same again with a CVS type list. Clicking the quoted checkbox gadget and make button again will encase the list in quotes
Image


Zebuddi. :)

Re: Select-Case section maker tool

Posted: Fri Dec 27, 2013 4:59 pm
by Tenaja
I did get it to work...seems like "a start" to me.

Type this in...

Code: Select all

1,2,3,4,5
Run the program, type in the comma into the delimiter box, copy the numbers to the clipboard, and hit go. It generates case 1, case 2, etc, on separate lines.

For me, however, I typically just use copy/paste (i.e. linefeed tab tab case) to replace the commas.

Re: Select-Case section maker tool

Posted: Fri Dec 27, 2013 9:53 pm
by yrreti
Hi Zebuddi123

I liked your idea for this, but decided to add a few features that I would like in it too.
1) I added a ;- name line checkbox which adds it to each Case line end. Very useful in the IDE for
quickly going to that Case for troubleshooting or code changes.
2) I added a Copy Results (to the clipboard) so you can easily paste the results into a program
your working on.
3) I added one more line space between each Case line to make it a little easier to add code under
a Case line and still have a separating line space.

A future project if you have some time, or if I get some time. Would be to be able to do the
same thing with: If ElseIf EndIf
It really shouldn't be too hard to do with the existing code. I just ran out of time today.

Thanks for your code and the nice idea. :D

I attached my added changes if you would like to try them.

Code: Select all

EnableExplicit

Global Window_0
Global Window_0_Button_MakeCase
Global Window_0_String_Delimeter
Global Window_0_Button_CopyResults
Global Window_0_Editor_1
Global Window_0_Text_Delimeter
Global Window_0_Option_String1
Global Window_0_Option_String2
Global Window_0_Option_Constant
Global Window_0_Option_Numb
Global StatusBar_Window_0
Global clipboard_text.s  ,  delimeter.s  ,  Finished_data.s  ,  collacted_string.s  ,  regex_spaces.i  ,  delimeter_position.i  ,  numb.i  ,  i.i  ,  extracted_word.s , ls.i , delimeter_length.i , counter.i
Global old_clipboard_text.s , old_delimeter.s ,c.i
Global cbdata
Global cbdata1
Global cbdata2
Global cbdata$
Global Text$

Define.l Event

Global NewList cbdata.s()

Procedure OpenWindow_Window_0()
  Window_0 = OpenWindow(#PB_Any , 482 ,  41 ,  490 ,  430,  "Select-Case  Section Maker" ,  #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
  If Window_0
    StatusBar_Window_0 = CreateStatusBar(#PB_Any, WindowID(Window_0))
    If StatusBar_Window_0
      AddStatusBarField(#PB_Ignore)
      StatusBarText(StatusBar_Window_0, 0, "Entries :  ")
    EndIf
    Window_0_Button_MakeCase = ButtonGadget(#PB_Any ,  10 ,  10 ,  70 ,  20 ,  "Make")
    Window_0_Text_Delimeter = TextGadget(#PB_Any ,  85, 13, 60, 16 ,  "Delimeter=")
    Window_0_String_Delimeter = StringGadget(#PB_Any ,  142 ,  10 ,  90 ,  19 ,  "")
    Window_0_Option_String1 = CheckBoxGadget(#PB_Any, 240, 10, 60, 20, "Quoted")
    Window_0_Option_String2 = CheckBoxGadget(#PB_Any, 300, 10, 95, 20, ";- Code ref name")
    Window_0_Button_CopyResults = ButtonGadget(#PB_Any ,  402 ,  10 ,  80 ,  20 ,  "Copy Results")
    Window_0_Editor_1 = EditorGadget(#PB_Any ,  10 ,  35 ,  430 ,  355)
  EndIf
EndProcedure
Procedure reset()
  clipboard_text.s = ""   :    delimeter.s  = "" :    Finished_data.s  = "" :    collacted_string.s  = ""  :    extracted_word.s = ""
  delimeter_position.i  = 0 :    numb.i  = 0  :    i.i  = 0 : ls.i = 0: delimeter_length.i = 0 : counter.i = 0
  ClearGadgetItems(Window_0_String_Delimeter)
EndProcedure


regex_spaces.i=CreateRegularExpression(#PB_Any , "\s+") ; reduces multipule consecutive white spaces to one single one

OpenWindow_Window_0()
ClearClipboard()
Repeat
  Event = WaitWindowEvent()
  Select Event
        
      Case #PB_Event_Gadget
        
        Select EventGadget()
            Case  Window_0_Button_CopyResults
              Text$ = GetGadgetText(Window_0_Editor_1)
              ClearClipboard()
              SetClipboardText(Text$)
              
            Case Window_0_Button_MakeCase
              
              reset()
              ClearList(cbdata())
              
              delimeter.s=GetGadgetText(Window_0_String_Delimeter) :
              ;If delimeter.s = ""  adding a space didn't work ? but no time to check further.
              ;  delimeter.s = " "
              ;EndIf
              clipboard_text.s=GetClipboardText()
              ;CallDebugger
              c+1
              ;Debug "run "+Str(c)+"  "+ clipboard_text.s
              If old_delimeter.s<>delimeter Or delimeter.s>"" ; check to see if delimeter has changed  and data already processed if true revert to original clipboard text
                old_clipboard_text.s=clipboard_text.s                  ; for reprocessing
                old_delimeter.s=delimeter.s
              EndIf
              
              
              ;{ -- error checking
              
              If delimeter.s = "" Or clipboard_text.s = "" Or CountString(clipboard_text,delimeter.s)=0
                
                If delimeter.s = ""
                  MessageRequester("Delimeter Error" , "Delimeter String Can Not Be  Blank")
                  
                ElseIf clipboard_text.s = ""
                  MessageRequester("Data Error" , "ClipBoard Cleared @ Program Start ..... No Data on The Clipboard")
                ElseIf CountString(clipboard_text,delimeter.s)=0
                  MessageRequester("Delimeter Error" , "No " + Chr(34) + delimeter.s + Chr(34) + "  Match`s in the ClipBoard Data")
                EndIf
                ;}
              Else
                ;{ --  Main Processing
                clipboard_text.s = ReplaceString(clipboard_text.s , Chr(13) , "" , #PB_String_NoCase , 1) ; remove CR`s
                clipboard_text.s = ReplaceString(clipboard_text.s , Chr(10) , "" , #PB_String_NoCase , 1) ; remove LF`s
                clipboard_text.s = ReplaceString(clipboard_text.s , Chr(9) , "" , #PB_String_NoCase , 1)   ; remove Tab`s
                clipboard_text.s = Trim(clipboard_text.s)
                clipboard_text.s = ReplaceRegularExpression(regex_spaces , clipboard_text.s  , " ")              ; reduce white space`s to single white space
                clipboard_text.s = ReplaceString(clipboard_text.s , " , " , ",")                                                             ; replace white spaced comma`s to just a comma seperator
                delimeter_position.i = 0
                delimeter.s = GetGadgetText(Window_0_String_Delimeter)
                
                ;{ -- check for multipul white space as the delimeter  and show in window tiltle as wont show clearly in string gadget
                delimeter_length.i =Len(delimeter.s)
                For i = 1 To delimeter_length.i
                  If Mid(delimeter.s , i , 1) = " "
                    counter.i+1
                  EndIf
                Next
                
                If counter.i = delimeter_length.i
                  SetWindowTitle(Window_0 , " USING --- " + Str(delimeter_length.i) +Chr(34)+" "+Chr(34) + " white space" )
                EndIf
                ;}
                
                ;{ -- check to see if we need data items quoted
                cbdata=0
                cbdata1=0
                cbdata2=0
                If GetGadgetState(Window_0_Option_String1)=1
                  cbdata1=1
                EndIf
                If GetGadgetState(Window_0_Option_String2)=1
                  cbdata2=2
                EndIf
                cbdata=cbdata1+cbdata2
                
                numb = CountString(clipboard_text.s , delimeter.s)
                
                For i.i = 0 To numb.i
                  delimeter_position.i = FindString(clipboard_text.s , delimeter.s, 1)
                  extracted_word.s = Mid(clipboard_text.s  ,  0  ,  delimeter_position.i-1)
                  AddElement(cbdata()) : cbdata()=extracted_word.s
                  
                  ;{ -- check to see if we need data items quoted
                  Select cbdata
                      Case 0
                        cbdata()="Case " + cbdata()  +  Chr(13) + Chr(13)+ Chr(13)
                        cbdata()=ReplaceString(cbdata() , Chr(34) , "")
                      Case 1
                        cbdata() ="Case " + Chr(34) + cbdata() +  Chr(34)  + Chr(13)+Chr(13)+ Chr(13)
                      Case 2
                        cbdata$=cbdata()
                        cbdata()="Case " + cbdata()  +"   ;- "+cbdata$+ Chr(13)+ Chr(13)+ Chr(13)
                        cbdata()=ReplaceString(cbdata() , Chr(34) , "")
                      Case 3
                        cbdata$=cbdata()
                        cbdata()="Case " + Chr(34)+ cbdata()  + Chr(34)+ "   ;- "+cbdata$+ Chr(13)+ Chr(13)+ Chr(13)
                  EndSelect
                  
                  ;}
                  
                  clipboard_text.s = ReplaceString(clipboard_text.s ,  extracted_word.s+delimeter.s , "" ,  #PB_String_NoCase , 1 , 1) ;remove the last data item and delimeter from the clipboard text$
                  clipboard_text.s = Trim(clipboard_text.s)+delimeter.s  ; add a delimeter to end of clipboard text$ so we dont miss the last data item out
                  collacted_string.s+Chr(9)+cbdata()  ; collate the data item`s and Tab them
                  
                Next
                
                
                collacted_string.s = Left(collacted_string , Len(collacted_string.s)-( Len(Chr(13)) )) ; remove the last CRLF else "EndSelect" jumps down two places
                
                Finished_data.s = "Select " + Chr(13) + collacted_string.s + Chr(13) +Chr(9)+"Default "+Chr(13)+Chr(13)+ "EndSelect"  ; prepared data
                
                ClearGadgetItems(Window_0_Editor_1)
                SetGadgetText(Window_0_Editor_1 , Finished_data.s)
                ls.i=ListSize(cbdata())
                StatusBarText(StatusBar_Window_0 , 0 , "Entries: "+Str(ls.i))
                
              EndIf
              
        EndSelect
        
      Case #PB_Event_CloseWindow
        
        Select EventWindow()
            Case Window_0
              ClearClipboard()
              SetClipboardText(Finished_data.s)
              CloseWindow(Window_0)
              Window_0 = 0
              FreeList(cbdata())
              Break
        EndSelect
        
  EndSelect
ForEver

Re: Select-Case section maker tool

Posted: Sat Dec 28, 2013 1:51 am
by IdeasVacuum
yrreti, code does not run? Seems ok if line 142 is commented out.

Re: Select-Case section maker tool

Posted: Sat Dec 28, 2013 3:17 am
by yrreti
Yes IdeasVacuum :oops:

I just noticed, but you got there before I had a chance to correct it.
I had deleted a code test part before I pasted it here, and in the process I missed the last Endif. :oops:
Code is corrected above. It does work now.

Re: Select-Case section maker tool

Posted: Sat Dec 28, 2013 4:26 am
by yrreti
As long as I had this open, I decided to go ahead and add the choice of creating either a
Select-Case
or an
If ElseIf EndIf code grouping.
But when in the If ElseIf EndIf mode. I disabled the Quoted selection.

It wasn't that difficult to do, but the credit goes to Zebuddi123 for the nice idea in the first place.

The code is listed here below.

Code: Select all

EnableExplicit

Global Window_0
Global Window_0_Button_MakeCase
Global Window_0_String_Delimeter
Global Window_0_Button_CopyResults
Global Window_0_Editor_1
Global Window_0_Text_Delimeter
Global Window_0_Option_String1
Global Window_0_Option_String2
Global Window_0_Option_String3
Global Window_0_Option_Constant
Global Window_0_Option_Numb
Global StatusBar_Window_0
Global clipboard_text.s  ,  delimeter.s  ,  Finished_data.s  ,  collacted_string.s  ,  regex_spaces.i  ,  delimeter_position.i  ,  numb.i  ,  i.i  ,  extracted_word.s , ls.i , delimeter_length.i , counter.i
Global old_clipboard_text.s , old_delimeter.s ,c.i
Global cbdata
Global cbdata1
Global cbdata2
Global cbdata$
Global Text$
Global SelectCase

Define.l Event

Global NewList cbdata.s()

Procedure OpenWindow_Window_0()
  Window_0 = OpenWindow(#PB_Any , 482 ,  41 ,  490 ,  430,  "Select-Case   or   If ElseIf EndIf   Section Maker" ,  #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
  If Window_0
    StatusBar_Window_0 = CreateStatusBar(#PB_Any, WindowID(Window_0))
    If StatusBar_Window_0
      AddStatusBarField(#PB_Ignore)
      StatusBarText(StatusBar_Window_0, 0, "Entries :  ")
    EndIf
    Window_0_Button_MakeCase = ButtonGadget(#PB_Any ,  10 ,  10 ,  70 ,  20 ,  "Make")
    Window_0_Text_Delimeter = TextGadget(#PB_Any ,  85, 13, 60, 16 ,  "Delimeter=")
    Window_0_String_Delimeter = StringGadget(#PB_Any ,  142 ,  10 ,  90 ,  19 ,  "")
    Window_0_Option_String1 = CheckBoxGadget(#PB_Any, 240, 0, 190, 17, "= Select-Case    else   If ElseIf EndIf")
    Window_0_Option_String2 = CheckBoxGadget(#PB_Any, 240, 18, 60, 20, "Quoted")
    Window_0_Option_String3 = CheckBoxGadget(#PB_Any, 300, 18, 95, 20, ";- Code ref name")
    Window_0_Button_CopyResults = ButtonGadget(#PB_Any ,  402 ,  16 ,  80 ,  20 ,  "Copy Results")
    Window_0_Editor_1 = EditorGadget(#PB_Any ,  10 ,  40 ,  430 ,  355)
  EndIf
EndProcedure
Procedure reset()
  clipboard_text.s = ""   :    delimeter.s  = "" :    Finished_data.s  = "" :    collacted_string.s  = ""  :    extracted_word.s = ""
  delimeter_position.i  = 0 :    numb.i  = 0  :    i.i  = 0 : ls.i = 0: delimeter_length.i = 0 : counter.i = 0
  ClearGadgetItems(Window_0_String_Delimeter)
EndProcedure


regex_spaces.i=CreateRegularExpression(#PB_Any , "\s+") ; reduces multipule consecutive white spaces to one single one

OpenWindow_Window_0()
ClearClipboard()
DisableGadget(Window_0_Option_String2,1)

Repeat
  Event = WaitWindowEvent()
  Select Event
        
      Case #PB_Event_Gadget
        
        Select EventGadget()
            Case  Window_0_Button_CopyResults
              Text$ = GetGadgetText(Window_0_Editor_1)
              ClearClipboard()
              SetClipboardText(Text$)
              
            Case Window_0_Option_String1
              
                SelectCase=0
                If GetGadgetState(Window_0_Option_String1)=1
                  SelectCase=1
                  DisableGadget(Window_0_Option_String2,0)
                Else
                  DisableGadget(Window_0_Option_String2,1)
                  SetGadgetState(Window_0_Option_String2,#PB_Checkbox_Unchecked)
                EndIf              
              
            Case Window_0_Button_MakeCase
              
              reset()
              ClearList(cbdata())
              
              delimeter.s=GetGadgetText(Window_0_String_Delimeter) :
              ;If delimeter.s = ""  adding a space didn't work ? but no time to check further.
              ;  delimeter.s = " "
              ;EndIf
              clipboard_text.s=GetClipboardText()
              ;CallDebugger
              c+1
              ;Debug "run "+Str(c)+"  "+ clipboard_text.s
              If old_delimeter.s<>delimeter Or delimeter.s>"" ; check to see if delimeter has changed  and data already processed if true revert to original clipboard text
                old_clipboard_text.s=clipboard_text.s                  ; for reprocessing
                old_delimeter.s=delimeter.s
              EndIf
              
              
              ;{ -- error checking
              
              If delimeter.s = "" Or clipboard_text.s = "" Or CountString(clipboard_text,delimeter.s)=0
                
                If delimeter.s = ""
                  MessageRequester("Delimeter Error" , "Delimeter String Can Not Be  Blank")
                  
                ElseIf clipboard_text.s = ""
                  MessageRequester("Data Error" , "ClipBoard Cleared @ Program Start ..... No Data on The Clipboard")
                ElseIf CountString(clipboard_text,delimeter.s)=0
                  MessageRequester("Delimeter Error" , "No " + Chr(34) + delimeter.s + Chr(34) + "  Match`s in the ClipBoard Data")
                EndIf
                ;}
              Else
                ;{ --  Main Processing
                clipboard_text.s = ReplaceString(clipboard_text.s , Chr(13) , "" , #PB_String_NoCase , 1) ; remove CR`s
                clipboard_text.s = ReplaceString(clipboard_text.s , Chr(10) , "" , #PB_String_NoCase , 1) ; remove LF`s
                clipboard_text.s = ReplaceString(clipboard_text.s , Chr(9) , "" , #PB_String_NoCase , 1)   ; remove Tab`s
                clipboard_text.s = Trim(clipboard_text.s)
                clipboard_text.s = ReplaceRegularExpression(regex_spaces , clipboard_text.s  , " ")              ; reduce white space`s to single white space
                clipboard_text.s = ReplaceString(clipboard_text.s , " , " , ",")                                                             ; replace white spaced comma`s to just a comma seperator
                delimeter_position.i = 0
                delimeter.s = GetGadgetText(Window_0_String_Delimeter)
                
                ;{ -- check for multipul white space as the delimeter  and show in window tiltle as wont show clearly in string gadget
                delimeter_length.i =Len(delimeter.s)
                For i = 1 To delimeter_length.i
                  If Mid(delimeter.s , i , 1) = " "
                    counter.i+1
                  EndIf
                Next
                
                If counter.i = delimeter_length.i
                  SetWindowTitle(Window_0 , " USING --- " + Str(delimeter_length.i) +Chr(34)+" "+Chr(34) + " white space" )
                EndIf
                ;}
                
                ;{ -- check to see if we need data items quoted              
                
                cbdata=0
                cbdata1=0
                cbdata2=0
                If GetGadgetState(Window_0_Option_String2)=1
                  cbdata1=1
                EndIf
                If GetGadgetState(Window_0_Option_String3)=1
                  cbdata2=2
                EndIf
                cbdata=cbdata1+cbdata2
                

                
                numb = CountString(clipboard_text.s , delimeter.s)
                
                For i.i = 0 To numb.i
                  delimeter_position.i = FindString(clipboard_text.s , delimeter.s, 1)
                  extracted_word.s = Mid(clipboard_text.s  ,  0  ,  delimeter_position.i-1)
                  AddElement(cbdata()) : cbdata()=extracted_word.s
                  
                  ;{ -- check to see if we need data items quoted
                  If SelectCase=1
                    Select cbdata
                        Case 0
                          cbdata()="Case " + cbdata()  +  Chr(13) + Chr(13)+ Chr(13)
                          cbdata()=ReplaceString(cbdata() , Chr(34) , "")
                        Case 1
                          cbdata() ="Case " + Chr(34) + cbdata() +  Chr(34)  + Chr(13)+Chr(13)+ Chr(13)
                        Case 2
                          cbdata$=cbdata()
                          cbdata()="Case " + cbdata()  +"   ;- "+cbdata$+ Chr(13)+ Chr(13)+ Chr(13)
                          cbdata()=ReplaceString(cbdata() , Chr(34) , "")
                        Case 3
                          cbdata$=cbdata()
                          cbdata()="Case " + Chr(34)+ cbdata()  + Chr(34)+ "   ;- "+cbdata$+ Chr(13)+ Chr(13)+ Chr(13)
                    EndSelect
                  Else
                    Select cbdata
                        Case 0
                          If i=0
                            cbdata()="If EventGadget = " + cbdata()  +  Chr(13) + Chr(13)+ Chr(13)
                            cbdata()=ReplaceString(cbdata() , Chr(34) , "")
                          Else
                            cbdata()="ElseIf EventGadget = " + cbdata()  +  Chr(13) + Chr(13)+ Chr(13)
                            cbdata()=ReplaceString(cbdata() , Chr(34) , "")
                          EndIf
                        Case 1
                          If i=0
                            cbdata() ="If EventGadget = " + Chr(34) + cbdata() +  Chr(34)  + Chr(13)+Chr(13)+ Chr(13)
                          Else
                            cbdata() ="ElseIf EventGadget = " + Chr(34) + cbdata() +  Chr(34)  + Chr(13)+Chr(13)+ Chr(13)
                          EndIf
                        Case 2
                          cbdata$=cbdata()
                          If i=0
                            cbdata()="If EventGadget = " + cbdata()  +"   ;- "+cbdata$+ Chr(13)+ Chr(13)+ Chr(13)
                            cbdata()=ReplaceString(cbdata() , Chr(34) , "")
                          Else
                            cbdata()="ElseIf EventGadget = " + cbdata()  +"   ;- "+cbdata$+ Chr(13)+ Chr(13)+ Chr(13)
                            cbdata()=ReplaceString(cbdata() , Chr(34) , "")
                          EndIf
                        Case 3
                          cbdata$=cbdata()
                          If i=0
                            cbdata()="If EventGadget = " + Chr(34)+ cbdata()  + Chr(34)+ "   ;- "+cbdata$+ Chr(13)+ Chr(13)+ Chr(13)
                          Else
                            cbdata()="ElseIf EventGadget = " + Chr(34)+ cbdata()  + Chr(34)+ "   ;- "+cbdata$+ Chr(13)+ Chr(13)+ Chr(13)
                          EndIf
                    EndSelect
                  EndIf
                  ;}
                  
                  clipboard_text.s = ReplaceString(clipboard_text.s ,  extracted_word.s+delimeter.s , "" ,  #PB_String_NoCase , 1 , 1) ;remove the last data item and delimeter from the clipboard text$
                  clipboard_text.s = Trim(clipboard_text.s)+delimeter.s  ; add a delimeter to end of clipboard text$ so we dont miss the last data item out
                  collacted_string.s+Chr(9)+cbdata()  ; collate the data item`s and Tab them
                  
                Next
                
                collacted_string.s = Left(collacted_string , Len(collacted_string.s)-( Len(Chr(13)) )) ; remove the last CRLF else "EndSelect" jumps down two places
                If SelectCase=0
                  Finished_data.s = "       " + Chr(13) + collacted_string.s + Chr(13) +Chr(9)+"EndIf"+Chr(13)+Chr(13)  ; prepared data
                Else
                  Finished_data.s = "Select " + Chr(13) + collacted_string.s + Chr(13) +Chr(9)+"Default "+Chr(13)+Chr(13)+ "EndSelect"  ; prepared data
                EndIf
                ClearGadgetItems(Window_0_Editor_1)
                SetGadgetText(Window_0_Editor_1 , Finished_data.s)
                ls.i=ListSize(cbdata())
                StatusBarText(StatusBar_Window_0 , 0 , "Entries: "+Str(ls.i))
                
              EndIf
              
        EndSelect
        
      Case #PB_Event_CloseWindow
        
        Select EventWindow()
            Case Window_0
              ClearClipboard()
              SetClipboardText(Finished_data.s)
              CloseWindow(Window_0)
              Window_0 = 0
              FreeList(cbdata())
              Break
        EndSelect
        
  EndSelect
ForEver

Re: Select-Case section maker tool

Posted: Sat Dec 28, 2013 9:15 am
by jamirokwai
Hi Zebuddi,

thanks for your explanation! It wasn't me being dumb, but the code below does not work on OS X PB 5.21, 64 - the Debugger dies on line 120: "Invalid Memory Access", which is quiet annoying.
You get the correct results, when you replace line 120 with another one:

clipboard_text.s = ReplaceString(clipboard_text.s , extracted_word.s+delimeter.s , ""); , #PB_String_NoCase , 1 , 1) ;remove the last data item and delimeter from the clipboard text$

with
clipboard_text.s = ReplaceString(clipboard_text.s , extracted_word.s+delimeter.s , "") ;remove the last data item and delimeter from the clipboard text$

Interestingly, the examples from the docs work:

Code: Select all

Debug ReplaceString("This is Art", " is", " was") ; Will display "This was Art"
  Debug ReplaceString("Hello again, hello again", "HELLO", "oh no...", 1, 10) ; Will display "Hello again, oh no... again"

  test$ = "Bundy, Barbie, Buddy"
  ReplaceString(test$, "B", "Z", 2, 1)  ; all B gets changed to Z  (directly in memory, no valid return-value here)
  Debug test$   ; Output of the changed string
Thanks for this tool, could come in handy at some time :-)
And why not making it a tool which you can call from the Tools-menu?

Code: Select all

EnableExplicit

Global Window_0
Global Window_0_Button_MakeCase
Global Window_0_String_Delimeter
Global Window_0_Editor_1
Global Window_0_Text_Delimeter
Global Window_0_Option_String
Global Window_0_Option_Constant
Global Window_0_Option_Numb
Global StatusBar_Window_0 
Global clipboard_text.s  ,  delimeter.s  ,  Finished_data.s  ,  collacted_string.s  ,  regex_spaces.i  ,  delimeter_position.i  ,  numb.i  ,  i.i  ,  extracted_word.s , ls.i , delimeter_length.i , counter.i
Global old_clipboard_text.s , old_delimeter.s ,c.i
Define.l Event  

Global NewList cbdata.s()

Procedure OpenWindow_Window_0()
   Window_0 = OpenWindow(#PB_Any , 482 ,  41 ,  400 ,  430,  "Select-Case  section maker" ,  #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
   If Window_0
      StatusBar_Window_0 = CreateStatusBar(#PB_Any, WindowID(Window_0))
      If StatusBar_Window_0
         AddStatusBarField(#PB_Ignore)
         StatusBarText(StatusBar_Window_0, 0, "Entries :  ")
      EndIf
      Window_0_Button_MakeCase = ButtonGadget(#PB_Any ,  10 ,  10 ,  70 ,  20 ,  "Make")
      Window_0_Text_Delimeter = TextGadget(#PB_Any ,  85, 13, 60, 16 ,  "Delimeter=")
      Window_0_String_Delimeter = StringGadget(#PB_Any ,  145 ,  10 ,  170 ,  19 ,  "")
      Window_0_Option_String = CheckBoxGadget(#PB_Any, 320, 10, 60, 20, "Quoted")
      Window_0_Editor_1 = EditorGadget(#PB_Any ,  10 ,  35 ,  380 ,  355)
   EndIf
EndProcedure
Procedure reset()
   clipboard_text.s = ""   :    delimeter.s  = "" :    Finished_data.s  = "" :    collacted_string.s  = ""  :    extracted_word.s = "" 
   delimeter_position.i  = 0 :    numb.i  = 0  :    i.i  = 0 : ls.i = 0: delimeter_length.i = 0 : counter.i = 0
   ClearGadgetItems(Window_0_String_Delimeter)   
EndProcedure


regex_spaces.i=CreateRegularExpression(#PB_Any , "\s+") ; reduces multipule consecutive white spaces to one single one

OpenWindow_Window_0()
ClearClipboard()
Repeat
   Event = WaitWindowEvent()
   Select Event
         
      Case #PB_Event_Gadget
         
         Select EventGadget()
               
            Case Window_0_Button_MakeCase
               
               reset()
               ClearList(cbdata())
               
               delimeter.s=GetGadgetText(Window_0_String_Delimeter) : 
               clipboard_text.s=GetClipboardText() 
               CallDebugger
               c+1
               Debug "run "+Str(c)+"  "+ clipboard_text.s
               If old_delimeter.s<>delimeter Or delimeter.s>"" ; check to see if delimeter has changed  and data already processed if true revert to original clipboard text
                  old_clipboard_text.s=clipboard_text.s                  ; for reprocessing
                  old_delimeter.s=delimeter.s   
               EndIf
               
                  
;{ -- error checking                  
               If delimeter.s = "" Or clipboard_text.s = "" Or CountString(clipboard_text,delimeter.s)=0
                  
                  If delimeter.s = ""
                     MessageRequester("Delimeter Error" , "Delimeter String Can Not Be  Blank")
                     
                  ElseIf clipboard_text.s = ""
                     MessageRequester("Data Error" , "ClipBoard Cleared @ Program Start ..... No Data on The Clipboard")
                  ElseIf CountString(clipboard_text,delimeter.s)=0
                     MessageRequester("Delimeter Error" , "No " + Chr(34) + delimeter.s + Chr(34) + "  Match`s in the ClipBoard Data")
                  EndIf
;}                  
               Else         
;{ --  Main Processing   
                  clipboard_text.s = ReplaceString(clipboard_text.s , Chr(13) , "" , #PB_String_NoCase , 1) ; remove CR`s
                  clipboard_text.s = ReplaceString(clipboard_text.s , Chr(10) , "" , #PB_String_NoCase , 1) ; remove LF`s
                  clipboard_text.s = ReplaceString(clipboard_text.s , Chr(9) , "" , #PB_String_NoCase , 1)   ; remove Tab`s 
                  clipboard_text.s = Trim(clipboard_text.s)
                  clipboard_text.s = ReplaceRegularExpression(regex_spaces , clipboard_text.s  , " ")              ; reduce white space`s to single white space
                  clipboard_text.s = ReplaceString(clipboard_text.s , " , " , ",")                                                             ; replace white spaced comma`s to just a comma seperator
                  delimeter_position.i = 0
                  delimeter.s = GetGadgetText(Window_0_String_Delimeter)
                  
                  ;{ -- check for multipul white space as the delimeter  and show in window tiltle as wont show clearly in string gadget
                  delimeter_length.i =Len(delimeter.s)
                  For i = 1 To delimeter_length.i
                     If Mid(delimeter.s , i , 1) = " "
                        counter.i+1
                     EndIf
                  Next
                  
                  If counter.i = delimeter_length.i
                     SetWindowTitle(Window_0 , " USING --- " + Str(delimeter_length.i) +Chr(34)+" "+Chr(34) + " white space" )
                  EndIf
                  ;}

                  numb = CountString(clipboard_text.s , delimeter.s)
                  
                  For i.i = 0 To numb.i
                     delimeter_position.i = FindString(clipboard_text.s , delimeter.s, 1)
                     extracted_word.s = Mid(clipboard_text.s  ,  0  ,  delimeter_position.i-1)
                     AddElement(cbdata()) : cbdata()=extracted_word.s
                     
                     ;{ -- check to see if we need data items quoted                     
                     If GetGadgetState(Window_0_Option_String)=1
                        cbdata() ="Case " + Chr(34) + cbdata() +  Chr(34)  + Chr(13)+Chr(13)
                     Else
                        cbdata()="Case " + cbdata()  +  Chr(13) + Chr(13)
                        cbdata()=ReplaceString(cbdata() , Chr(34) , "")
                     EndIf
                     ;}
                     
                     clipboard_text.s = ReplaceString(clipboard_text.s ,  extracted_word.s+delimeter.s , "" ,  #PB_String_NoCase , 1 , 1) ;remove the last data item and delimeter from the clipboard text$ 
                     clipboard_text.s = Trim(clipboard_text.s)+delimeter.s  ; add a delimeter to end of clipboard text$ so we dont miss the last data item out
                     collacted_string.s+Chr(9)+cbdata()  ; collate the data item`s and Tab them
                     
                  Next   
                  
                  
                  collacted_string.s = Left(collacted_string , Len(collacted_string.s)-( Len(Chr(13)) )) ; remove the last CRLF else "EndSelect" jumps down two places
                  
                  Finished_data.s = "Select " + Chr(13) + collacted_string.s + Chr(13) +Chr(9)+"Default "+Chr(13)+Chr(13)+ "EndSelect"  ; prepared data
                  
                  ClearGadgetItems(Window_0_Editor_1)
                  SetGadgetText(Window_0_Editor_1 , Finished_data.s)
                  ls.i=ListSize(cbdata())
                  StatusBarText(StatusBar_Window_0 , 0 , "Entries: "+Str(ls.i))
                  
               EndIf
               
         EndSelect
         
      Case #PB_Event_CloseWindow
         
         Select EventWindow()
            Case Window_0
               ClearClipboard()
               SetClipboardText(Finished_data.s)
               CloseWindow(Window_0)
               Window_0 = 0
               FreeList(cbdata())
               Break
         EndSelect
         
   EndSelect
ForEver

Re: Select-Case section maker tool

Posted: Sat Dec 28, 2013 2:51 pm
by yrreti
Hi jamirokwai
Thanks for this tool, could come in handy at some time :-)
And why not making it a tool which you can call from the Tools-menu?
:?: Were you going to add some changes to his code for the above suggestion?
Because the code you posted here is the same code that Zebuddi123 posted in his first post. :?
I know that you could run it directly from the tools menu, but thought that you might have
presented an interesting change in his code for doing that.
Also if you want to add a choice of If ElseIF EndIf besides Select-Case, see my post above yours.
It' just Zebuddi123's code with all the changes that I made added to it to accomplish that.

Re: Select-Case section maker tool

Posted: Sun Dec 29, 2013 8:32 am
by jamirokwai
yrreti wrote:Hi jamirokwai
Thanks for this tool, could come in handy at some time :-)
And why not making it a tool which you can call from the Tools-menu?
:?: Were you going to add some changes to his code for the above suggestion?
Because the code you posted here is the same code that Zebuddi123 posted in his first post. :?
I know that you could run it directly from the tools menu, but thought that you might have
presented an interesting change in his code for doing that.
Also if you want to add a choice of If ElseIF EndIf besides Select-Case, see my post above yours.
It' just Zebuddi123's code with all the changes that I made added to it to accomplish that.
Ah, sorry. I was distracted when posting...
No its the same as the one Zebuddi123 posted in his first post.