Jump to content

Save to ini file


Chobby
 Share

Recommended Posts

Well, im making a program to store IPs for a game server..

I got it to read the ips and corresponding informations from the ini file .. But I need some help on a problem..

There is this option (in the right side of the GUI) to add ips .. Then, I want it to be able to save the new addons to the ini file, both manually (ie a 'save' menuitem) and when the gui is closed..

Any help?? :s

Also, feel free to clean up in any code which doesnt belong there ;) There might be a couple of loose ends, or ends ending in a msgbox instead of something concrete - that is still under development! :)

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.10.0
 Author:         Chob

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <GUIConstants.au3>
#include <GuiListView.au3>
#include <IE.au3>

; VERSION
$ver = "0.1 Beta"

; GUI
GUICreate("Clan Apathy Server Tool",520,450)

; TOP MENU
$filemenu = GUICtrlCreateMenu ("Files")
$saveitem = GUICtrlCreateMenuItem ("Save IP list",$filemenu)
GUICtrlCreateMenuItem("",$filemenu)
$exititem = GUICtrlCreateMenuItem ("Quit",$filemenu)


$linksmenu = GUICtrlCreateMenu ("Links")
$weblink = GUICtrlCreateMenuItem ("Clan Website",$linksmenu)
$forumlink = GUICtrlCreateMenuItem ("Clan forums",$linksmenu)
GUICtrlCreateMenuItem ("",$linksmenu)
$serverlink = GUICtrlCreateMenuItem ("Clan server statistics",$linksmenu)
GUICtrlCreateMenuItem ("",$linksmenu)
$jk2files = GUICtrlCreateMenuItem ("Jedi Knight II files",$linksmenu)
GUICtrlCreateMenuItem("",$linksmenu)
$geoip = GUICtrlCreateMenuItem("Geobytes IP locator",$linksmenu)
$seoip = GUICtrlCreateMenuItem("Seomoz IP locator",$linksmenu)

$aboutmenu = GUICtrlCreateMenu ("Help")
$changeitem = GUICtrlCreateMenuItem ("Changelog",$aboutmenu)
GUICtrlCreateMenuItem("",$aboutmenu)
$aboutitem = GUICtrlCreateMenuItem ("About",$aboutmenu)

; TABS
$tab=GUICtrlCreateTab (10,10,505,410)

; IP-LIST TAB
$tabb0=GUICtrlCreateTabItem ("IP List")

; SEARCH
GUICtrlCreateLabel("Search for a match", 360,35)
$search=GUICtrlCreateInput( "IP",310,50,100,17)
$sbtn=GUICtrlCreateButton("Search",420,50,70,17)

; ADD
GUICtrlCreateLabel("Add a new entry", 370,70)
GUICtrlCreateLabel("IP:",310,90,30,17)
Dim $addIP=GUICtrlCreateInput("",375,90,100,17)
GUICtrlCreateLabel("Name:",310,110,30,17)
Dim $addName=GUICtrlCreateInput("",375,110,100,17)
GUICtrlCreateLabel("Country:",310,130,45,17)
Dim $addCountry=GUICtrlCreateInput("",375,130,100,17)
GUICtrlCreateLabel("Clan:",310,150,30,17)
Dim $addClan=GUICtrlCreateInput("",375,150,40,17)
GUICtrlCreateLabel("Ban?:",310,170,30,17)
$addBan=GUICtrlCreateInput("",375,170,40,17)
$subbtn=GUICtrlCreateButton("Add",330,200,70,17)
$resbrn=GUICtrlCreateButton("Reset",410,200,70,17)

; ADMIN HANDBOOK
$gethb=GUICtrlCreateButton("Open admin handbook",335,397,150,17)

; DELETE ENTRY
$delbtn=GUICtrlCreateButton("Delete entry",350,297,120,17)

$newline = GUICtrlRead($addIP) & "|" & GUICtrlRead($addName) & "|" & GUICtrlRead($addCountry) & "|" & GUICtrlRead($addClan) & "|" & GUICtrlRead($addBan)

; LISTVIEW
Dim $listview = GUICtrlCreateListView("IP                   |Name|Country|Clan|Ban?",17,35,285,378)
$ipArray = IniReadSectionNames('list.ini')
if not @error Then
    dim $item[UBound($ipArray)+1]
    For $i = 1 to UBound($ipArray)-1
        $keyArray = IniReadSection('list.ini',$ipArray[$i])
        If Not @error Then
            $name=''
            $country=''
            $clan=''
            $ban=''
            For $k = 1 to UBound($keyArray,1)-1
                If StringLower($keyArray[$k][0])='name' Then $name = $keyArray[$k][1]
                If StringLower($keyArray[$k][0])='country' Then $country = $keyArray[$k][1]
                If StringLower($keyArray[$k][0])='clan' Then $clan = $keyArray[$k][1]
                If StringLower($keyArray[$k][0])='ban' Then $ban = $keyArray[$k][1]
            Next
        EndIf
        $item[$i] = GUICtrlCreateListViewItem($ipArray[$i] & '|' & $name & "|" & $country & "|" & $clan & "|" & $ban,$listview)
    Next
EndIf
; RIGHTCLICK MENU
$1menu=GUICtrlCreateContextMenu ($listview)
$1delete=GUICtrlCreateMenuitem("Delete" , $1menu)
$1play=GUICtrlCreateMenuitem("Edit" , $1menu)

; FILE OPEN/SAVE

$file = FileOpen("list.txt",1)

$tabb1=GUICtrlCreateTabItem ("Ranges")
$tabb2=GUICtrlCreateTabitem ("Members")
$tabb3=GUICtrlCreateTabItem ("Server Commands")
$tabb4=GUICtrlCreateTabItem ("Alerts")

GUISetState ()

While 1
    $msg = GUIGetMsg()
    
    Select
    
    Case $msg = $sbtn
            Local $tofind = GUICtrlRead($search)
            If $tofind <> '' Then
                
                Local $found = _GUICtrlListView_FindInText ($listview, $tofind)
                If $found > -1 Then
                    MsgBox(0, "Found text", "the text" & @CR & $tofind & @CR & "is in item " & $found + 1)
                EndIf;else not found etc
            EndIf

    Case $msg = $subbtn             ; Done
        Local $sortAsc = False
            GUICtrlCreateListViewItem(GUICtrlRead($addIP) & '|' & GUICtrlRead($addName) & '|' & GUICtrlRead($addCountry) & '|' & GUICtrlRead($addClan) & '|' & GUICtrlRead($addBan), $listview)
            _GUICtrlListView_SimpleSort ($listview, $sortAsc, 0)
            ClearIps()
        
    Case $msg = $1delete            ; Done
        DeleteItem()
        
    Case $msg = $1play              ; Not done
        edit()
    
    Case $msg = $resbrn             ; Done
        ClearIps()
        
    Case $msg = $delbtn             ; Done
        DeleteItem()
        
    Case $msg = $gethb              ; Not done
        Run("notepad.exe")
        FileOpen("extras/handbook.txt",0)  
        
    Case $msg = $aboutitem          ; Done
        MsgBox(0,"About..","Clan Apathys Server Tool is created by <Ca>Chobby" & @LF & @LF & "                        Copyright © 2008" & @LF & "                         Version " & $ver, 15)
        
    Case $msg = $weblink            ; Done
        _IECreate ("http://www.clan-apathy.net")
        
    Case $msg = $forumlink          ; Done
        _IECreate ("http://www.even-flow.dk/test/")
    
    Case $msg = $serverlink         ; Done
        _IECreate ("http://www.serverspy.net/site/search/server.html?a=72.5.248.18:28070&g=5")
    
    Case $msg = $jk2files           ; Done
        _IECreate ("http://jediknight3.filefront.com/")
        
    Case $msg = $geoip              ; Done
        _IECreate ("http://www.geobytes.com/IpLocator.htm?GetLocation")
        
    Case $msg = $seoip              ; Done
        _IECreate ("http://www.seomoz.org/ip2loc/ip2loc.php")
                
    Case $msg = $saveitem
        FileWrite($file,$listview)
        FileClose($file)

    Case $msg = $GUI_EVENT_CLOSE    ; Done
        Exitloop
    
    Case $msg = $exititem           ; Done
        ExitLoop
    
    EndSelect
Wend

; === FUNCTIONS ===

Func edit()
    MsgBox(64, "You Right Clicked ", "Play: " & GUICtrlRead(GUICtrlRead($listview)) & " ?")
EndFunc

Func ClearIps()
;don't create the controls again to clear them
    GUICtrlSetData($addIP, "")
    GUICtrlSetData($addName, "")
    GUICtrlSetData($addCountry, '')
    GUICtrlSetData($addClan, '')
    GUICtrlSetData($addBan, '')
EndFunc ;==>ClearIps

Func DeleteItem()
    $delselect = GUICtrlRead($listview)
    GUICtrlDelete($delselect)
EndFunc ;==>DeleteItem

list.ini file (just as an example)

[111.111.111.111]
name=aaa
country=aaa
clan=aaa
ban=No

[222.222.222.222]
name=bbb
country=bbb
clan=bbb
ban=Yes
Link to comment
Share on other sites

You will need something like the following

IniWrite($Ini, $addIP, "name", $addName)
IniWrite($Ini, $addIP, "country", $addCountry)
IniWrite($Ini, $addIP, "clan", $addClan)
IniWrite($Ini, $addIP, "ban", $addBan)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

You will need something like the following

IniWrite($Ini, $addIP, "name", $addName)
IniWrite($Ini, $addIP, "country", $addCountry)
IniWrite($Ini, $addIP, "clan", $addClan)
IniWrite($Ini, $addIP, "ban", $addBan)
Why didnt I stumble upon that when I read through the help files??? :)

Anyways, thanks a lot!! I found a way to work it out:

Case $msg = $subbtn             ; Done
        Local $sortAsc = False
            GUICtrlCreateListViewItem(GUICtrlRead($addIP) & '|' & GUICtrlRead($addName) & '|' & GUICtrlRead($addCountry) & '|' & GUICtrlRead($addClan) & '|' & GUICtrlRead($addBan), $listview)
            _GUICtrlListView_SimpleSort ($listview, $sortAsc, 0)
            $section = GUICtrlRead($addIP)
            $inidata = "name=" & GUICtrlRead($addName) & @LF & "country=" & GUICtrlRead($addCountry) & @LF & "clan=" & GUICtrlRead($addClan) & @LF & "ban=" & GUICtrlRead($addBan)
            IniWriteSection("list.ini",$section,$inidata)
            ClearIps()

Now it will be saving in the second the addition is made ... Works like a charm!! Used IniWriteSection for it all, instead of your suggestion.. But thanks a lot anyways!!

Link to comment
Share on other sites

Why didnt I stumble upon that when I read through the help files??? :)

Anyways, thanks a lot!! I found a way to work it out:

<snipped>

Now it will be saving in the second the addition is made ... Works like a charm!! Used IniWriteSection for it all, instead of your suggestion.. But thanks a lot anyways!!

That works too.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...