Страница: 1 |
|
Вопрос: Файл настроек программы
|
Добавлено: 03.09.08 10:35
|
|
Автор вопроса: Евгений
|
Здравствуйте. Програмлю недавно, оттого глупый вопрос. А как обычно хранят настройки задаваемые пользователем в программе? Я понимаю, что обычно либо в файле, либо в реестре. Но как их считать/создать?
Ведь файл типа
[BD]
name=bla-bla-bla
pass=asdasdasd
[profile]
...
Как разбирать такой файл?
Заранее спасибо.
Ответить
|
Номер ответа: 1 Автор ответа: Arseny
ICQ: 298826769
Вопросов: 53 Ответов: 1732
|
Профиль | | #1
|
Добавлено: 03.09.08 12:43
|
На вот тебе класс для работы с INI:
'*************************************************************************
'* Êëàññ äëÿ ðàáîòû ñ INI-ôàéëàìè *
'*-----------------------------------------------------------------------*
'* Îïèñàíèå: *
'* AddSection - äîáàâëÿåò íîâóþ ñåêöèþ *
'* AddValue - äîáàâëÿåò íîâûé ïàðàìåòð â ñåêöèþ *
'* Filename - èìÿ ôàéëà. Åñëè ôàéë ñóùåñòâóåò, îí çàãðóæàåòñÿ. *
'* RemoveSection - óäàëÿåò ñåêöèþ *
'* RemoveValue - óäàëÿåò ïàðàìåòð èç ñåêöèè *
'* SectionExists - âîçâðàùàåò True, åñëè ñåêöèÿ ñóùåñòâóåò *
'* SectionName - âîçâðàùàåò èìÿ ñåêöèè ïî åå íîìåðó *
'* SectionsCount - âîçâðàùàåò êîëè÷åñòâî ñåêöèé *
'* UpdateFile - ñîõðàíÿåò ôàéë *
'* Value - âîçâðàùàåò/óñòàíàâëèâàåò çíà÷åíèå ïàðàìåòðà *
'* ValueExists - âîçâðàùàåò True, åñëè ïàðàìåòð ñóùåñòâóåò *
'* ValueName - âîçâðàùàåò èìÿ ïàðàìåòðà ïî åãî íîìåðó *
'* ValuesCount - âîçâðàùàåò êîëè÷åñòâî ïàðàìåòðîâ â çàäàííîé ñåêöèè *
'*-----------------------------------------------------------------------*
'* 17 èþíÿ 2000 10:43 *
'* Copyright © 2000 Êîçûðåâ Äìèòðèé <mitriy@e-mail.ru> *
'*************************************************************************
Option Explicit
' Âñå ôóíêöèè âîçâðàùàþò True, åñëè íåò îøèáêè
Private mFilename As String
Private AllSections() As tSection
Private Type tValue
Name As String
cntValue As String
End Type
Private Type tSection
Name As String
Values() As tValue
End Type
Public Function RemoveSection( ByVal Section As String) As Boolean
Dim i&, uSN$, a&
uSN = UCase$(Section)
' Ïðîñìîòð âñåõ ñåêöèé
For i = 1 To UBound(AllSections)
If UCase$(AllSections(i). Name) = uSN Then
' Íóæíàÿ ñåêöèÿ åñòü, ñäâèãàåì ïàðàìåòðû "âíèç"
For a = i To UBound(AllSections) - 1
AllSections = AllSections(a + 1)
Next a
' Óäàëÿåì
ReDim Preserve AllSections( UBound(AllSections) - 1)
RemoveSection = True
Exit Function
End If
Next i
End Function
Public Property Get SectionExists( ByVal Section As String) As Boolean
Dim i&, uSN$
uSN = UCase$(Section)
' Ïðîñòî ïðîáåæèìñÿ ïî âññåì ñåêöèÿì è ïîñìîòðèì, åñòü ëè çàäàííàÿ
For i = 1 To UBound(AllSections)
If UCase$(AllSections(i). Name) = uSN Then
' Åñòü!
SectionExists = True
Exit Property
End If
Next i
End Property
Public Function UpdateFile() As Boolean
' Èìÿ ôàéëà íå çàäàíî - êóäà ñîõðàíÿòü-òî?
If Len(mFilename) = 0 Then Exit Function
Dim i&, a&
Open mFilename For Output Access Write Lock Write As #1
For i = 1 To UBound(AllSections)
' Çàïèñü âñåõ ñåêöèé
With AllSections(i)
Print #1, "[" & . Name & "]"
' Çàïèñü âñåõ ïàðàìåòðîâ â ñåêöèè
For a = 1 To UBound(.Values)
Print #1, .Values . Name & "=" & .Values .cntValue
Next a
End With
Print #1, vbNullString
Next i
Close #1
UpdateFile = True
End Function
Public Property Get ValueExists( ByVal Section As String, ByVal ValueName As String) As Boolean
Dim i&, uSN$, uVN$, a&
uSN = UCase$(Section)
uVN = UCase$(ValueName)
' Ïðîñìîòð âñåõ ñåêöèé
For i = 1 To UBound(AllSections)
If UCase$(AllSections(i). Name) = uSN Then
' Íóæíàÿ ñåêöèÿ, òåïåðü èùåì â íåé ïàðàìåòð
For a = 1 To UBound(AllSections(i).Values)
If AllSections(i).Values . Name = uVN Then
' Èñêîìûé ïàðàìåòð
ValueExists = True
Exit Property
End If
Next a
Exit Property
End If
Next i
End Property
Public Property Get ValueName( ByVal Section As String, ByVal Index As Long) As String
' Ïðèìå÷àíèå: åñëè ïàðàìåòð èëè ñåêöèÿ íàéäåíû íå áóäóò, òî ñâîéñòâî
' âåðíåò vbNullChar - ñèìâîë ñ íóëåâûì êîäîì
Dim uSN$, i&
uSN = UCase$(Section)
' Ïðîñìîòð âñåõ ñåêöèé
For i = 1 To UBound(AllSections)
If UCase$(AllSections(i). Name) = uSN Then
'Íóæíàÿ ñåêöèÿ
If Index > 0 And Index <= UBound(AllSections(i).Values) Then
' Íóæíûé ïàðàìåòð
ValueName = AllSections(i).Values(Index). Name
Exit Property
Else
' Èíäåêñ âíå ãðàíèö äèàïàçîíà...
ValueName = vbNullChar
End If
Exit Property
End If
Next
ValueName = vbNullChar
End Property
Public Function RemoveValue( ByVal Section As String, ByVal ValueName As String) As Boolean
Dim i&, a&, uVN$, uSN$, c&
uSN = UCase$(Section)
uVN = UCase$(ValueName)
' Ïðîñìîð âñåõ ñåêöèé
For i = 1 To UBound(AllSections)
If UCase$(AllSections(i). Name) = uSN Then
' Íóæíàÿ ñåêöèÿ åñòü
For a = 1 To UBound(AllSections(i).Values)
If UCase$(AllSections(i).Values . Name) = uVN Then
' Íóæíûé ïàðàìåòð åñòü
With AllSections(i)
' Ñäâèã ïàðàìåòðîâ "âíèç" íà îäèí
For c = a To UBound(.Values) - 1
.Values = .Values(a + 1)
Next c
' Óäàëåíèå ïàðàìåòðà
ReDim Preserve .Values( UBound(.Values) - 1)
RemoveValue = True
Exit Function
End With
End If
Next
Exit Function
End If
Next i
End Function
Public Function AddValue( ByVal Section As String, ByVal ValueName As String, Optional ByVal lValue As String = vbNullString) As Boolean
Dim uSN$, i&, uVN$, a&
uSN = UCase$(Section)
uVN = UCase$(ValueName)
' Ïðîñìîòðèì-êà âñå ñåêöèè...
For i = 1 To UBound(AllSections)
If UCase$(AllSections(i). Name) = uSN Then
add_value:
' Åñòü íóæíàÿ ñåêöèÿ!
For a = 1 To UBound(AllSections(i).Values)
If UCase$(AllSections(i).Values . Name) = uVN Then
' Òàêàÿ ñòðîêà óæå åñòü
Exit Function
End If
Next a
' Äîáàâëÿåì íîâûé ïàðàìåòð
With AllSections(i)
ReDim Preserve .Values( UBound(.Values) + 1)
.Values( UBound(.Values)). Name = ValueName
.Values( UBound(.Values)).cntValue = lValue
End With
AddValue = True
Exit Function
End If
Next i
' Ñåêöèè òàêîé íåò, äîáàâëÿåì...
AddSection Section
GoTo add_value
End Function
Public Property Get ValuesCount( ByVal Section As String) As Long
' Ïðèìå÷àíèå: åñëè ñåêöèÿ íå íàéäåíà, ñâîéñòâî âåðíåò
' çíà÷åíèå -1.
Dim uSN$, i&
uSN = UCase$(Section)
' Ïðîñìîòð âñåõ ñåêöèé
For i = 1 To UBound(AllSections)
If UCase$(AllSections(i). Name) = uSN Then
' Íóæíàÿ ñåêöèÿ
ValuesCount = UBound(AllSections(i).Values)
Exit Property
End If
Next
ValuesCount = -1
End Property
Public Property Let Value( ByVal Section As String, ByVal ValueName As String, ByVal vData As String)
Dim i&, a&, uVN$, uSN$
uSN = UCase$(Section)
uVN = UCase$(ValueName)
' Ïðîñìîòð âñåõ ñåêöèé
For i = 1 To UBound(AllSections)
If UCase$(AllSections(i). Name) = uSN Then
' Ïðîñìîòð ïàðàìåòðîâ â ñåêöèè
For a = 1 To UBound(AllSections(i).Values)
If UCase$(AllSections(i).Values . Name) = uVN Then
' Íàøëè ïàðàìåòð, îáíîâëÿåì åãî çíà÷åíèå...
AllSections(i).Values .cntValue = vData
Exit Property
End If
Next
' Íå íàøëè ïàðàìåòð, äîáàâëÿåì íîâûé...
With AllSections(i)
ReDim Preserve .Values( UBound(.Values) + 1)
.Values( UBound(.Values)).cntValue = vData
.Values( UBound(.Values)). Name = ValueName
End With
Exit Property
End If
Next i
' Íå íàøëè ñîîòâåñòâóþùóþ ñåêöèþ, äîáàâëÿåì íîâóþ...
ReDim Preserve AllSections( UBound(AllSections) + 1)
With AllSections( UBound(AllSections))
. Name = Section
ReDim .Values(1)
.Values(1). Name = ValueName
.Values(1).cntValue = vData
End With
End Property
Public Property Get Value( ByVal Section As String, ByVal ValueName As String) As String
' Ïðèìå÷àíèå: åñëè ïàðàìåòð íàéäåí íå áóäåò, òî ñâîéñòâî
' âåðíåò vbNullChar - ñèìâîë ñ íóëåâûì êîäîì
Dim i&, a&, uVN$, uSN$
uSN = UCase$(Section)
uVN = UCase$(ValueName)
' Ïðîñìîòð âñåõ ñåêöèé
For i = 1 To UBound(AllSections)
If UCase$(AllSections(i). Name) = uSN Then
' Ïðîñìîòð âñåõ ïàðàìåòðîâ â ñåêöèè
For a = 1 To UBound(AllSections(i).Values)
If UCase$(AllSections(i).Values . Name) = uVN Then
' Íàøëè íóæíûé ïàðàìåòð
Value = AllSections(i).Values .cntValue
Exit Property
End If
Next
Value = vbNullChar
Exit Property
End If
Next i
Value = vbNullChar
End Property
Public Property Get SectionName( ByVal Index As Long) As String
' Çäåñü âñå ïðîñòî: ïðîâåðêà - ïîïàäàåò ëè èíäåêñ â ãðàíèöû
' ìàññèâà è âîçâðàò èìåíè ñåêöèè.
' Ïðèìå÷àíèå: åñëè ñåêöèÿ íàéäåíà íå áóäåò, òî ñâîéñòâî
' âåðíåò vbNullChar - ñèìâîë ñ íóëåâûì êîäîì
If Index > 0 And Index <= UBound(AllSections) Then
SectionName = AllSections(Index). Name
Else
SectionName = vbNullChar
End If
End Property
Public Function AddSection( ByVal Section As String) As Boolean
Dim uSN$, i&
uSN = UCase$(Section)
For i = 1 To UBound(AllSections)
If UCase$(AllSections(i). Name) = uSN Then
' Óæå òàêàÿ ñåêöèÿ åñòü...
Exit Function
End If
Next i
' Äîáàâëÿåì ýëåìåíò ê ìàññèâó ñåêöèé
ReDim Preserve AllSections( UBound(AllSections) + 1)
With AllSections( UBound(AllSections))
. Name = Section
ReDim .Values(0)
End With
AddSection = True
End Function
Public Property Get SectionsCount() As Long
SectionsCount = UBound(AllSections)
End Property
Public Property Let FileName( ByVal vData As String)
Dim s&, v&, l$, t&
mFilename = vData
ReDim AllSections(0)
' Ôàéë â íàëè÷èè?
If Len(Dir(mFilename)) = 0 Then
' Åñëè íåò, òî...
Exit Property
End If
' Ñ÷èòûâàåì ôàéë
Open mFilename For Input Access Read Lock Write As #1
Do While Not EOF(1)
Line Input #1, l
' Êîììåíòàðèè åñòü?
t = InStr(1, l, ";"
l = LTrim$(RTrim$(l))
' Îòêîììåíòèðîâàíà âñÿ ñòðîêà
If t = 1 Or Len(l) = 0 Then GoTo skip_line
' Êîììåíòàðèè â êîíöå ñòðîêè
If t > 1 Then l = Left$(l, t - 1)
If Left$(l, 1) = "[" Then
' Íàøëè ñåêöèþ
t = InStr(1, l, "]"
If t < 3 Then GoTo skip_line
l = Mid$(l, 2, t - 2)
' Ñåêöèé ñòàëî íà îäíó áîëüøå
s = s + 1
' Ñòðîê â íîâîé ñåêöèè ïîêà íåò
v = 0
ReDim Preserve AllSections(s)
AllSections(s). Name = l
ReDim AllSections(s).Values(0)
Else
' Íàøëè ïàðàìåòð â ñåêöèþ
' Åñëè ðàíååå íå áûëî íàéäåíî íè îäíîé ñåêöèè - ïðîïóñêàåì
If s = 0 Then GoTo skip_line
' Ðàçäåëÿåì ñòðîêó íà íàçâàíèå ïàðàìåòðà è íà åãî çíà÷åíèå
t = InStr(1, l, "="
If t < 2 Then GoTo skip_line
v = v + 1
' Äîáàâëÿåì ïàðàìåòð
With AllSections(s)
ReDim Preserve .Values(v)
.Values(v). Name = Left$(l, t - 1)
If t < Len(l) Then .Values(v).cntValue = Mid$(l, t + 1, Len(l) - t)
End With
End If
skip_line:
Loop
Close #1
End Property
Public Property Get FileName() As String
FileName = mFilename
End Property
Private Sub Class_Initialize()
ReDim AllSections(0)
End Sub
Private Sub Class_Terminate()
ReDim AllSections(0)
End Sub
Ответить
|
Страница: 1 |
Поиск по форуму