home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / tool / prg_sup / kpini / iniedit.txt < prev    next >
Text File  |  1995-02-26  |  16KB  |  570 lines

  1. '***************************************************************************
  2. '** INIEDIT.BAS ** First Public Release
  3. '*************************************************
  4. '** VB Module for simplifying .INI file operations
  5. '***************************************************************************
  6. 'Copyright (C)Karl E. Peterson, March 1995, CIS 72302,3707.
  7. '***************************************************************************
  8. 'Finally, some example code that exercises the routines in INIFILE.BAS!
  9. 'This project, INIEDIT, is provided AS-IS, with no warranties expressed or
  10. 'implied.  Use it at your own risk, preferably on a copy of "real" INI files
  11. 'so you're not timid about adding and deleting data.
  12. '
  13. 'You are free to use this module as you see fit.  If you like it, I'd really
  14. 'appreciate hearing that!  If you don't like it, or have problems with it,
  15. 'I'd like to know that too.
  16. '***************************************************************************
  17.  
  18. Option Explicit
  19. '
  20. ' Flag variables
  21. '
  22. Dim fWarn As Integer
  23. Dim fEdit As Integer
  24. Dim fWinIni As Integer
  25. '
  26. ' Name and section of current INI file
  27. '
  28. Dim IniFile As String
  29. Dim IniSection As String
  30. '
  31. ' Some garbage text for temporary settings
  32. '
  33. Const Garbage = "!@#$%^&*!@#$%^&*"
  34. '
  35. ' Windows API call used to control textbox
  36. '
  37. Declare Function SendMessage Lib "User" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any) As Long
  38. '
  39. ' Edit Control Messages
  40. '
  41. Const WM_CUT = &H300
  42. Const WM_COPY = &H301
  43. Const WM_PASTE = &H302
  44. Const WM_CLEAR = &H303
  45. Const WM_UNDO = &H304
  46. Const EM_CANUNDO = &H416     'WM_USER + 22
  47. Const EM_GETMODIFY = &H408   'WM_USER + 8
  48. '
  49. ' File menu constants
  50. '
  51. Const mfOpen = 0
  52. Const mfExit = 2
  53. '
  54. ' Edit menu constants
  55. '
  56. Const meUndo = 0
  57. Const meCut = 2
  58. Const meCopy = 3
  59. Const mePaste = 4
  60. '
  61. ' Option menu constants
  62. '
  63. Const moAddSect = 0
  64. Const moDelSect = 1
  65. Const moAddEnt = 3
  66. Const moDelEnt = 4
  67. Const moUpdate = 6
  68. Const moWarn = 8
  69. Const moEdit = 9
  70. '
  71. ' File Open/Save Dialog Flags
  72. '
  73. Const OFN_READONLY = &H1&
  74. Const OFN_OVERWRITEPROMPT = &H2&
  75. Const OFN_HIDEREADONLY = &H4&
  76. Const OFN_NOCHANGEDIR = &H8&
  77. Const OFN_SHOWHELP = &H10&
  78. Const OFN_NOVALIDATE = &H100&
  79. Const OFN_ALLOWMULTISELECT = &H200&
  80. Const OFN_EXTENSIONDIFFERENT = &H400&
  81. Const OFN_PATHMUSTEXIST = &H800&
  82. Const OFN_FILEMUSTEXIST = &H1000&
  83. Const OFN_CREATEPROMPT = &H2000&
  84. Const OFN_SHAREAWARE = &H4000&
  85. Const OFN_NOREADONLYRETURN = &H8000&
  86.  
  87. Private Sub EditMenuToggle ()
  88.    If TypeOf Me.ActiveControl Is TextBox Then
  89.       '
  90.       ' Determine if last edit can be undone
  91.       '
  92.       Me.mEdit(meUndo).Enabled = SendMessage(Me.ActiveControl.hWnd, EM_CANUNDO, 0, 0&)
  93.       '
  94.       ' See if there's anything to cut, copy, or delete
  95.       '
  96.       Me.mEdit(meCut).Enabled = Me.ActiveControl.SelLength
  97.       Me.mEdit(meCopy).Enabled = Me.ActiveControl.SelLength
  98.       'Me.mEdit(meDelete).Enabled = Me.ActiveControl.SelLength
  99.       '
  100.       ' See if there's anything to paste
  101.       '
  102.       Me.mEdit(mePaste) = Clipboard.GetFormat(1)
  103.    Else
  104.       '
  105.       ' If active control is not a textbox then disable all
  106.       '
  107.       Me.mEdit(meUndo).Enabled = False
  108.       Me.mEdit(meCut).Enabled = False
  109.       Me.mEdit(meCopy).Enabled = False
  110.       Me.mEdit(mePaste).Enabled = False
  111.       'Me.mEdit(mDelete).Enabled = False
  112.    End If
  113. End Sub
  114.  
  115. Private Sub EditPerform (EditFunction As Integer)
  116.    Dim nRet As Integer
  117.    '
  118.    ' A "wrapper" function for SendMessage
  119.    ' Requests function passed in EditFunction
  120.    ' Beeps if active control is not a textbox
  121.    '
  122.    If TypeOf Me.ActiveControl Is TextBox Then
  123.       nRet = SendMessage(Me.ActiveControl.hWnd, EditFunction, 0, 0&)
  124.    Else
  125.       Beep
  126.    End If
  127. End Sub
  128.  
  129. Sub Form_Load ()
  130.    '
  131.    ' Initialize flag variables
  132.    '
  133.    fWarn = Me.mOpt(moWarn).Checked
  134.    fEdit = Me.mOpt(moEdit).Checked
  135.    '
  136.    ' Position form
  137.    '
  138.    Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
  139.    '
  140.    ' Setup controls
  141.    '
  142.    lblEntry = ""
  143.    txtEntry = ""
  144.    CmDialog1.Flags = OFN_HIDEREADONLY Or OFN_FILEMUSTEXIST Or OFN_NOREADONLYRETURN
  145.    '
  146.    ' Warning: Use at own risk!
  147.    '
  148.    Dim msg As String
  149.    Dim cr As String
  150.    cr = Chr$(13) + Chr$(10)
  151.    msg = "Thank you for trying INIEDIT, the demo for KPINI routines!" + cr + cr
  152.    msg = msg + "This demo is provided AS-IS!  Please use a copy of vital INI's" + cr
  153.    msg = msg + "rather than risk valuable data.  No warranties, expressed or" + cr
  154.    msg = msg + "implied, are conveyed."
  155.    MsgBox msg, 48, "Have Fun!"
  156. End Sub
  157.  
  158. Sub Form_Unload (Cancel As Integer)
  159.    '
  160.    ' Clear any remnants from memory
  161.    '
  162.    If Len(IniFile) Then
  163.       WinIniFlushCache
  164.       PrivIniFlushCache
  165.    End If
  166. End Sub
  167.  
  168. Private Sub IniAddEntry ()
  169.    Dim NewEntry As String
  170.    Dim nRet As Integer
  171.    Dim tmp As String
  172.    '
  173.    ' Get name of new entry
  174.    '
  175.    NewEntry = Trim(InputBox("Enter name of new entry", "Add Entry to " & IniSection))
  176.    If Len(NewEntry) Then
  177.       If fWinIni Then
  178.          '
  179.          ' Check if an existing entry with this name already
  180.          ' exists in Win.Ini, otherwise create it by writing
  181.          ' a dummy entry then clearing its value.
  182.          '
  183.          tmp$ = WinGetString(NewEntry, Garbage)
  184.          If tmp$ = Garbage Then 'entry doesn't already exist
  185.             nRet = WinPutString(NewEntry, Garbage)
  186.             WinClearEntry NewEntry
  187.          End If
  188.       Else
  189.          '
  190.          ' Check if an existing entry with this name already
  191.          ' exists in Private.Ini, otherwise create it by writing
  192.          ' a dummy entry then clearing its value.
  193.          '
  194.          tmp$ = PrivGetString(NewEntry, Garbage)
  195.          If tmp$ = Garbage Then
  196.             nRet = PrivPutString(NewEntry, Garbage)
  197.             PrivClearEntry NewEntry
  198.          End If
  199.       End If
  200.       '
  201.       ' Re-read section to insure update.
  202.       '
  203.       IniRead IniSection
  204.       '
  205.       ' Highlight new entry in listbox.
  206.       '
  207.       For nRet = (List2.ListCount - 1) To 0 Step -1
  208.          If UCase$(Left$(List2.List(nRet), InStr(List2.List(nRet), "=") - 1)) = UCase$(NewEntry) Then
  209.             List2.ListIndex = nRet
  210.             txtEntry.SetFocus
  211.             txtEntry.SelStart = Len(txtEntry)
  212.             Exit For
  213.          End If
  214.       Next nRet
  215.    End If
  216. End Sub
  217.  
  218. Private Sub IniAddSection ()
  219.    Dim NewSection As String
  220.    Dim nRet As Integer
  221.    '
  222.    ' Get name of new section.
  223.    '
  224.    NewSection = Trim(InputBox("Enter name of new section", "Add Section"))
  225.    If Len(Trim(NewSection)) Then
  226.       If fWinIni Then
  227.          '
  228.          ' Register new section name, then check if it already
  229.          ' exists in Win.Ini, otherwise create it by writing
  230.          ' a dummy entry then deleting it.
  231.          '
  232.          WinIniRegister NewSection
  233.          If Not WinSectExist() Then
  234.             nRet = WinPutString(Garbage, "Temporary Entry")
  235.             WinDeleteEntry Garbage
  236.          End If
  237.       Else
  238.          '
  239.          ' Register new section name, then check if it already
  240.          ' exists in Private.Ini, otherwise create it by writing
  241.          ' a dummy entry then deleting it.
  242.          '
  243.          PrivIniRegister NewSection, IniFile
  244.          If Not PrivSectExist() Then
  245.             nRet = PrivPutString(Garbage, "Temporary Entry")
  246.             PrivDeleteEntry Garbage
  247.          End If
  248.       End If
  249.       '
  250.       ' Re-read all sections within the INI file.
  251.       '
  252.       IniOpen IniFile
  253.       '
  254.       ' Highlight new section in listbox.
  255.       '
  256.       For nRet = (List1.ListCount - 1) To 0 Step -1
  257.          If UCase$(List1.List(nRet)) = UCase$(NewSection) Then
  258.             List1.ListIndex = nRet
  259.             Exit For
  260.          End If
  261.       Next nRet
  262.    End If
  263. End Sub
  264.  
  265. Private Sub IniChoose ()
  266.    '
  267.    ' Retrieve name of INI file to read.
  268.    '
  269.    On Error Resume Next
  270.    CmDialog1.Action = 1
  271.    If Err = 0 Then
  272.       If (CmDialog1.Flags And OFN_EXTENSIONDIFFERENT) = 0 Then
  273.          IniOpen (CmDialog1.Filename)
  274.       End If
  275.    End If
  276. End Sub
  277.  
  278. Private Sub IniDeleteEntry ()
  279.    Dim msg As String
  280.    Dim rsp As Integer
  281.    Dim entry As String
  282.    '
  283.    ' Parse name of entry from highlighted listbox item.
  284.    '
  285.    entry = Left$(List2.List(List2.ListIndex), InStr(List2.List(List2.ListIndex), "=") - 1)
  286.    '
  287.    ' Make sure user knows it's history if Warnings turned on.
  288.    '
  289.    If fWarn Then
  290.       msg = "You are about to permanantly remove entry: " & entry
  291.       msg = msg & Chr$(13) & Chr$(10) & Chr$(13) & Chr$(10)
  292.       msg = msg & "Are you absolutely sure you want to do that?"
  293.       rsp = MsgBox(msg, 4 + 48, "Warning!")
  294.    Else
  295.       rsp = 6  'yes
  296.    End If
  297.    '
  298.    ' Blast it!
  299.    '
  300.    If rsp = 6 Then
  301.       If fWinIni Then
  302.          WinDeleteEntry entry
  303.       Else
  304.          PrivDeleteEntry entry
  305.       End If
  306.       IniRead IniSection
  307.    End If
  308. End Sub
  309.  
  310. Private Sub IniDeleteSection ()
  311.    Dim msg As String
  312.    Dim rsp As Integer
  313.    '
  314.    ' Make sure user knows it's history if Warnings turned on.
  315.    '
  316.    If fWarn Then
  317.       msg = "You are about to permanantly remove section: " & IniSection & "!"
  318.       msg = msg & Chr$(13) & Chr$(10) & Chr$(13) & Chr$(10)
  319.       msg = msg & "Are you absolutely sure you want to do that?"
  320.       rsp = MsgBox(msg, 4 + 48, "Warning!")
  321.    Else
  322.       rsp = 6  'yes
  323.    End If
  324.    '
  325.    ' Blast it!
  326.    '
  327.    If rsp = 6 Then
  328.       If fWinIni Then
  329.          WinDeleteSection
  330.       Else
  331.          PrivDeleteSection
  332.       End If
  333.       IniOpen IniFile
  334.    End If
  335. End Sub
  336.  
  337. Private Sub IniEditEntry (EditText As String)
  338.    Dim eq As Integer
  339.    '
  340.    ' Parse highlighted listbox item into Label and Textbox.
  341.    '
  342.    eq = InStr(EditText, "=")
  343.    If eq Then
  344.       lblEntry = Left(EditText, eq - 1)
  345.       txtEntry = Mid(EditText, eq + 1)
  346.    End If
  347. End Sub
  348.  
  349. Private Sub IniOpen (NewIniFile As String)
  350.    Dim sTable() As String
  351.    Dim Sections As Integer
  352.    Dim i As Integer
  353.    '
  354.    ' Store name of INI file for module-wide usage.
  355.    ' Update form caption.
  356.    '
  357.    IniFile = NewIniFile
  358.    Me.Caption = "IniEdit -- " & IniFile
  359.    '
  360.    ' Determine if we're using Win.Ini or a private INI.
  361.    ' Register appropriately, and read sections.
  362.    '
  363.    If UCase$(Right$(IniFile, 8)) = "\WIN.INI" Then
  364.       fWinIni = True
  365.       WinIniRegister ""
  366.       Sections = WinGetSectionsEx(sTable())
  367.    Else
  368.       fWinIni = False
  369.       PrivIniRegister "", IniFile
  370.       Sections = PrivGetSectionsEx(sTable())
  371.    End If
  372.    '
  373.    ' Fill List with all sections, and trigger second
  374.    ' list to fill with all entries from first section.
  375.    '
  376.    List1.Clear
  377.    Label1 = Sections & " &Sections:"
  378.    If Sections Then
  379.       For i = 0 To Sections - 1
  380.          List1.AddItem sTable(i)
  381.       Next i
  382.       List1.ListIndex = 0
  383.    End If
  384. End Sub
  385.  
  386. Private Sub IniRead (Section As String)
  387.    Dim eTable() As String
  388.    Dim Entries As Integer
  389.    Dim i As Integer
  390.    '
  391.    ' Store section for module-wide usage.
  392.    '
  393.    IniSection = Section
  394.    '
  395.    ' Register appropriate section and read all entries.
  396.    '
  397.    If fWinIni Then
  398.       WinIniRegister IniSection
  399.       Entries = WinGetSectEntriesEx(eTable())
  400.    Else
  401.       PrivIniRegister IniSection, IniFile
  402.       Entries = PrivGetSectEntriesEx(eTable())
  403.    End If
  404.    '
  405.    ' Clear controls to accept new data.
  406.    '
  407.    lblEntry = ""
  408.    txtEntry = ""
  409.    List2.Clear
  410.    '
  411.    ' Fill list with all entries and their data.
  412.    ' Trigger edit control update.
  413.    '
  414.    Label2 = Entries & " &Entries:"
  415.    If Entries Then
  416.       For i = 0 To Entries - 1
  417.          List2.AddItem eTable(0, i) + "=" + eTable(1, i)
  418.       Next i
  419.       List2.ListIndex = 0
  420.    End If
  421. End Sub
  422.  
  423. Private Sub IniUpdateEntry ()
  424.    Dim nRet As Integer
  425.    Dim entry As String
  426.    Dim value As String
  427.    '
  428.    ' Parse entry and value from controls.
  429.    '
  430.    entry = Left$(List2.List(List2.ListIndex), InStr(List2.List(List2.ListIndex), "=") - 1)
  431.    value = Trim$(txtEntry)
  432.    '
  433.    ' Update entry with new value.
  434.    '
  435.    If fWinIni Then
  436.       nRet = WinPutString(entry, value)
  437.    Else
  438.       nRet = PrivPutString(entry, value)
  439.    End If
  440.    '
  441.    ' Re-read section to reflect update in controls.
  442.    '
  443.    IniRead IniSection
  444.    '
  445.    ' Highlight updated entry in newly-filled list.
  446.    '
  447.    For nRet = (List2.ListCount - 1) To 0 Step -1
  448.       If UCase$(Left$(List2.List(nRet), InStr(List2.List(nRet), "=") - 1)) = UCase$(entry) Then
  449.          List2.ListIndex = nRet
  450.          txtEntry.SetFocus
  451.          txtEntry.SelStart = Len(txtEntry)
  452.          Exit For
  453.       End If
  454.    Next nRet
  455. End Sub
  456.  
  457. Sub List1_Click ()
  458.    '
  459.    ' Read highlighted section into other list.
  460.    '
  461.    Me.MousePointer = 11
  462.       IniRead (List1.List(List1.ListIndex))
  463.    Me.MousePointer = 0
  464. End Sub
  465.  
  466. Sub List2_Click ()
  467.    '
  468.    ' Put highlighted list element into textbox
  469.    '
  470.    IniEditEntry (List2.List(List2.ListIndex))
  471. End Sub
  472.  
  473. Sub mEdit_Click (Index As Integer)
  474.    '
  475.    ' Call generic routine to perform requested action.
  476.    ' Same routine could be called from a toolbar event.
  477.    '
  478.    Select Case Index
  479.       Case meUndo
  480.          EditPerform WM_UNDO
  481.       Case meCut
  482.          EditPerform WM_CUT
  483.       Case meCopy
  484.          EditPerform WM_COPY
  485.       Case mePaste
  486.          EditPerform WM_PASTE
  487.       'Case meDelete
  488.       '   EditPerform WM_CLEAR
  489.    End Select
  490. End Sub
  491.  
  492. Sub mFile_Click (Index As Integer)
  493.    Select Case Index
  494.       Case mfOpen
  495.          IniChoose
  496.       Case mfExit
  497.          Unload Me
  498.    End Select
  499. End Sub
  500.  
  501. Sub mMain_Click (Index As Integer)
  502.    Select Case Index
  503.       Case 0 ' File
  504.       Case 1 ' Edit
  505.          If fEdit Then
  506.             EditMenuToggle
  507.          Else
  508.             Me.mEdit(meUndo).Enabled = False
  509.             Me.mEdit(meCut).Enabled = False
  510.             Me.mEdit(mePaste).Enabled = False
  511.             Me.mEdit(meCopy).Enabled = False
  512.          End If
  513.       Case 2 ' Options
  514.          If fEdit = True And Len(IniFile) > 0 Then
  515.             Me.mOpt(moAddSect).Enabled = True
  516.             Me.mOpt(moDelSect).Enabled = True
  517.             Me.mOpt(moAddEnt).Enabled = True
  518.             Me.mOpt(moDelEnt).Enabled = True
  519.             Me.mOpt(moUpdate).Enabled = True
  520.             Me.mOpt(moWarn).Enabled = True
  521.          Else
  522.             Me.mOpt(moAddSect).Enabled = False
  523.             Me.mOpt(moDelSect).Enabled = False
  524.             Me.mOpt(moAddEnt).Enabled = False
  525.             Me.mOpt(moDelEnt).Enabled = False
  526.             Me.mOpt(moUpdate).Enabled = False
  527.             Me.mOpt(moWarn).Enabled = False
  528.          End If
  529.    End Select
  530. End Sub
  531.  
  532. Sub mOpt_Click (Index As Integer)
  533.    Select Case Index
  534.       Case moAddSect
  535.          IniAddSection
  536.  
  537.       Case moDelSect
  538.          IniDeleteSection
  539.  
  540.       Case moAddEnt
  541.          IniAddEntry
  542.  
  543.       Case moDelEnt
  544.          IniDeleteEntry
  545.  
  546.       Case moUpdate
  547.          IniUpdateEntry
  548.  
  549.       Case moWarn
  550.          Me.mOpt(moWarn).Checked = Not Me.mOpt(moWarn).Checked
  551.          fWarn = Me.mOpt(moWarn).Checked
  552.  
  553.       Case moEdit
  554.          Me.mOpt(moEdit).Checked = Not Me.mOpt(moEdit).Checked
  555.          fEdit = Me.mOpt(moEdit).Checked
  556.  
  557.    End Select
  558. End Sub
  559.  
  560. Sub txtEntry_KeyPress (KeyAscii As Integer)
  561.    '
  562.    ' Update INI entry if user presses Enter
  563.    '
  564.    If KeyAscii = 13 Then 'Enter
  565.       IniUpdateEntry
  566.       KeyAscii = 0
  567.    End If
  568. End Sub
  569.  
  570.