Page 1 of 1

PureINI Editor 0.9 BETA

Posted: Wed Dec 01, 2004 11:59 pm
by localmotion34
here is a program that i wrote to edit and create INI files. i needed a way to quickly write INI because i am beginning to write an RPG game, and the data and information will be stored in INI for ease of access (at least for me). notepad is hard to use for INI, and hackman INI editor is buggy. it isnt 100% complete because i am going to include an option to ENCODE strings so that people cant hack my settings and data in the RPG. im not gonna release that coding algorithm.

the interface is pretty much self explanatory, with all you having to do is create sections and keys via an editabe treeview, and then clicking on a key and entering the string in. the key value is updated while you type. you can preview the output in an editor gadget and copy the output and paste wherever you want.

CAUTION: DO NOT DO NOT mess with system INI files unless you make a backup. i kept it so that when you open an existing INI, PureINI reads the file, stores the data, and then CLOSES the original INI so there is no conflict.

Code: Select all




;- Window Constants
;
Enumeration
  #Window_0
EndEnumeration

;- MenuBar Constants
;
Enumeration
  #MenuBar_0
EndEnumeration

Enumeration
  #MENU_1
  #MENU_4
  #MENU_5
  #MENU_7
  #MENU_8
  #menu_9
  #menu_10
EndEnumeration

;- Gadget Constants
;

#Tree_0=200
#Button_0=201
#Button_1=202
#String_1=203
#String_2=204
#Text_1=205
#Text_3=206
#Text_2=207
#Button_2=208
#Text_4=209


;- Fonts
;
Global FontID1
FontID1 = LoadFont(1, "Txt", 10, #PB_Font_Underline)
Global FontID2
FontID2 = LoadFont(2, "Times New Roman", 10)
Global FontID3
FontID3 = LoadFont(3, "Arial", 10)
Global FontID4
FontID4 = LoadFont(4, "Arial", 10, #PB_Font_Underline)
;- Structures and Lists
Structure info
  key.l
  section.l
  value.s
EndStructure

Dim inilist.s(2000)
NewList elements.info()

;- Procedures
Procedure.l writeINI(section.s,key.s,value.s,inifile.s)
  result=WritePrivateProfileString_(section,key,value,inifile)
  ProcedureReturn result
EndProcedure

Procedure Callback(Window.l, message.l, wParam.l, lParam.l) 
  result = #PB_ProcessPureBasicEvents 
  If message = #WM_NOTIFY 
    *lp.NMHDR = lParam 
    Select *lp\code 
      Case #TVN_BEGINLABELEDIT 
        
      Case #TVN_ENDLABELEDIT 
        *pvdi.NMTVDISPINFO = lParam 
        If *pvdi\item\pszText <> #Null 
          Text.s = PeekS(*pvdi\item\pszText) 
          
          If Text = "" 
            result = #False 
          Else 
            result = #True 
          EndIf 
          
        EndIf 
    EndSelect 
  EndIf 
  ProcedureReturn result 
  
EndProcedure 
Procedure Open_Window_0()
  If OpenWindow(#Window_0, 395, 142, 570, 456,  #PB_Window_SystemMenu |  #PB_Window_TitleBar|#PB_Window_WindowCentered , "PureINI Editor - By Localmotion34")
    If CreateMenu(#MenuBar_0, WindowID())
      MenuTitle("File")
      MenuItem(#MENU_1, "New")
      MenuItem(#MENU_4, "Open")
      MenuBar()
      MenuItem(#MENU_5, "Save")
      MenuItem(#MENU_7, "Save As...")
      MenuBar()
      MenuItem(#MENU_8, "Exit")
      MenuTitle("Edit")
      MenuItem(#menu_9,"Copy")
    EndIf
    
    If CreateGadgetList(WindowID())
      TreeGadget(#Tree_0, 1, 10, 130, 420,#PB_Tree_AlwaysShowSelection)
      ButtonGadget(#Button_0, 140, 10, 120, 20, "New Section")
      ButtonGadget(#Button_1, 140, 30, 120, 20, "New Key")
      EditorGadget(#String_1, 140, 140, 420, 290)
      StringGadget(#String_2, 140, 80, 120, 20, "")
      TextGadget(#Text_1, 140, 60, 120, 20, "Value", #PB_Text_Center)
      SetGadgetFont(#Text_1, FontID4)
      TextGadget(#Text_3, 310, 10, 120, 20, "Currently Editing Section:")
      TextGadget(#Text_2, 310, 30, 120, 20, "", #PB_Text_Border|#PB_Text_Center)
      ButtonGadget(#Button_2, 300, 60, 140, 40, "Test Output")
      TextGadget(#Text_4, 140, 120, 320, 20, "Quick Output", #PB_Text_Center)
      SetWindowLong_(GadgetID(#Tree_0),#GWL_STYLE,GetWindowLong_(GadgetID(#Tree_0),#GWL_STYLE) | #TVS_EDITLABELS) 
    EndIf
  EndIf
  SetWindowCallback(@Callback())
EndProcedure

Procedure updateINI()
  SetGadgetText(#String_1,"")
  selectedItem=0
  Repeat
    currentitem=GadgetItemID(#Tree_0,selectedItem)
    If currentitem=#Null
      Break
    EndIf 
    message1=SendMessage_(GadgetID(#Tree_0),#TVM_GETNEXTITEM ,#TVGN_CHILD,currentitem)
    If message1 =#Null
      inilist(selectedItem)=GetGadgetItemText(#Tree_0,selectedItem,0)
      AddGadgetItem(#String_1,-1, "["+inilist(selectedItem)+"]")
      AddGadgetItem(#String_1,-1,"")
      selectedItem=selectedItem+1
    ElseIf message1 <> #Null
      inilist(selectedItem)=GetGadgetItemText(#Tree_0,selectedItem,0)
      AddGadgetItem(#String_1,-1, "["+inilist(selectedItem)+"]")
      nodeitems=CountTreeGadgetNodeItems(#Tree_0, selectedItem)
      For a=selectedItem+1 To selectedItem+nodeitems
        inilist(a)=GetGadgetItemText(#Tree_0,a,0)
        SelectElement(elements(),a)
        AddGadgetItem(#String_1,-1,inilist(a)+"=" +elements()\value)
      Next 
      AddGadgetItem(#String_1,-1,"")
      selectedItem=selectedItem+nodeitems+1
    EndIf 
  Until currentitem=0
EndProcedure 

Procedure openINI(file$)
  If file$=""
  Else 
    ClearList(elements())
    ClearGadgetItemList(#Tree_0)
    SetGadgetText(#String_1,"")
    SetGadgetText(#String_2,"")
    OpenFile(99, file$) 
    Repeat
      currenttext$=ReadString()
      If currenttext$=""
      Else 
        If Asc(currenttext$)=91
          Length=Len(currenttext$)
          Result$ = Left(currenttext$, Length-1)
          Length=Len(Result$)
          Result1$ = Right(Result$, Length-1)
          thisitem=AddGadgetItem(#Tree_0,-1,Result1$)
          AddElement(elements())
        ElseIf Asc(currenttext$)<>59
          result2$=StringField(currenttext$,1,"=")
          result3$=StringField(currenttext$,2,"=")
          OpenTreeGadgetNode(#Tree_0)
          AddGadgetItem(#Tree_0,-1,result2$)
          AddElement(elements())
          elements()\value=result3$
          CloseTreeGadgetNode(#Tree_0)
        EndIf 
      EndIf 
    Until Eof(99)
    CloseFile(99)
  EndIf 
EndProcedure
Procedure saveINI()
  savedfile$=SaveFileRequester("Please Choose A File To Save","","INI Files|*.INI",0)
  files$=GetExtensionPart(savedfile$)
  If files$="ini"
    savefile$=savedfile$
  Else
    savefile$=savedfile$ + ".ini"
  EndIf
  If savefile$=""
  Else 
    OpenFile(100,savefile$)
    selectedItem=0
    Repeat
      currentitem=GadgetItemID(#Tree_0,selectedItem)
      If currentitem=#Null
        Break
      EndIf 
      message1=SendMessage_(GadgetID(#Tree_0),#TVM_GETNEXTITEM ,#TVGN_CHILD,currentitem)
      If message1 =#Null
        inilist(selectedItem)=GetGadgetItemText(#Tree_0,selectedItem,0)
        WriteStringN("["+inilist(selectedItem)+"]")
        selectedItem=selectedItem+1
      ElseIf message1 <> #Null
        inilist(selectedItem)=GetGadgetItemText(#Tree_0,selectedItem,0)
        WriteStringN("["+inilist(selectedItem)+"]")
        nodeitems=CountTreeGadgetNodeItems(#Tree_0, selectedItem)
        For a=selectedItem+1 To selectedItem+nodeitems
          inilist(a)=GetGadgetItemText(#Tree_0,a,0)
          SelectElement(elements(),a)
          WriteStringN(inilist(a)+"=" +elements()\value)
        Next 
        selectedItem=selectedItem+nodeitems+1
      EndIf 
    Until currentitem=#Null
    CloseFile(100)
  EndIf 
EndProcedure 

CreatePopupMenu(100)
MenuItem(100,"Remove Item")
MenuItem(101,"Expand All")
MenuItem(102,"Collapse All")

;- Program
Open_Window_0()
Repeat
  Event = WaitWindowEvent()
  Select Event 
    Case  #PB_EventMenu
      MenuID = EventMenuID()
      Select MenuID
        Case #MENU_1
          proceed=MessageRequester("Info","Are You Sure You Want To Proceed? All Unsaved Information Will Be Lost",#PB_MessageRequester_YesNo)
          If proceed=6
            ClearList(elements())
            SetGadgetText(#String_1,"")
            ClearGadgetItemList(#Tree_0)
          EndIf 
        Case #MENU_4
          openedfile$=OpenFileRequester("Please Specify An INI File To Open","","INI File|*.ini",0) 
          openINI(openedfile$)
        Case #MENU_8
          End 
        Case 100
          selectedentry=GetGadgetState(#Tree_0)
          currentitem=GadgetItemID(#Tree_0,selectedentry)
          message1=SendMessage_(GadgetID(#Tree_0),#TVM_GETNEXTITEM ,#TVGN_CHILD,currentitem)
          If message1 =#Null
            SelectElement(elements(),selectedentry)
            DeleteElement(elements())
            RemoveGadgetItem(#Tree_0,selectedentry)
          ElseIf message1 <> #Null
            nodenumber=CountTreeGadgetNodeItems(#Tree_0,selectedentry)
            RemoveGadgetItem(#Tree_0,selectedentry)
            For a=selectedentry To selectedentry+nodenumber
              SelectElement(elements(),selectedentry)
              DeleteElement(elements())
            Next 
          EndIf 
        Case 101
          For b=0 To CountGadgetItems(#Tree_0)-1
            SetGadgetItemState(#Tree_0, b, #PB_Tree_Expanded )
          Next 
        Case 102
          For b=0 To CountGadgetItems(#Tree_0)-1
            SetGadgetItemState(#Tree_0, b, #PB_Tree_Collapsed)
          Next 
        Case #MENU_7
          saveINI()
        Case #menu_9
          *Buffer = AllocateMemory(64000) 
          SendMessage_(GadgetID(#String_1), #EM_GETSELTEXT, 0, *Buffer) 
          TextCapture$ = PeekS(*Buffer) 
          SetClipboardText(TextCapture$)
          FreeMemory(*Buffer) 
          
      EndSelect 
    Case  #PB_EventGadget
      GadgetID = EventGadgetID()
      Select GadgetID 
        Case #Tree_0
          If EventType()=#PB_EventType_LeftDoubleClick
            selectedItem=GetGadgetState(#Tree_0)
            currentitem=GadgetItemID(#Tree_0,selectedItem) 
            SendMessage_(GadgetID(#Tree_0),#TVM_EDITLABEL,0,currentitem)
          ElseIf EventType()=#PB_EventType_RightClick
            DisplayPopupMenu(100,WindowID())
          ElseIf EventType()=#PB_EventType_LeftClick
            selectedItem=GetGadgetState(#Tree_0)
            currentitem=GadgetItemID(#Tree_0,selectedItem)
            If SendMessage_(GadgetID(#Tree_0),#TVM_GETNEXTITEM ,#TVGN_PARENT,currentitem)<>#Null
              DisableGadget(#String_2,0)
              SelectElement(elements(),selectedItem)
              SetGadgetText(#String_2,elements()\value)
            Else
              DisableGadget(#String_2,1)
              SetGadgetText(#String_2,"")
              SetGadgetText(#Text_2,GetGadgetItemText(#Tree_0,GetGadgetState(#Tree_0),0))
            EndIf 
          EndIf 
        Case #Button_0
          LastElement(elements())
          AddElement(elements())
          current=ListIndex(elements())
          AddGadgetItem(#Tree_0,-1,"New Section"+ " " + Str(current))
          elements()\section=1
          elements()\key=0
          elements()\value=""
        Case #Button_1
          selectedItem=GetGadgetState(#Tree_0)
          If selectedItem=-1
          Else 
            currentitem=GadgetItemID(#Tree_0,selectedItem)
            If SendMessage_(GadgetID(#Tree_0),#TVM_GETNEXTITEM ,#TVGN_PARENT,currentitem)=#Null 
              currentselection=GetGadgetState(#Tree_0)
              currentitem=OpenTreeGadgetNode(#Tree_0,currentselection)
              itemtoadd=CountTreeGadgetNodeItems(#Tree_0, currentselection)
              AddGadgetItem(#Tree_0,currentselection+itemtoadd+1,"New Key")
              CloseTreeGadgetNode(#Tree_0,currentselection)
              SelectElement(elements(),GetGadgetState(#Tree_0)+itemtoadd)
              AddElement(elements())
              elements()\section=0
              elements()\key=1
              elements()\value=""
            EndIf 
          EndIf 
          
        Case #Button_2
          updateINI()
        Case #String_2
          If EventType()=#PB_EventType_Change 
            If CountGadgetItems(#Tree_0)=0
            Else 
              SelectElement(elements(),GetGadgetState(#Tree_0))
              elements()\value=GetGadgetText(#String_2)
            EndIf 
          EndIf 
      EndSelect 
      
  EndSelect 
  
Until Event = #PB_EventCloseWindow
  
End
ENJOY

Posted: Thu Dec 02, 2004 3:59 am
by RJP Computing
Thanks for the code. This is very handy. I have a project that I can use this on. Thanks a bunch for sharing.