问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

求Linux c版 ini文件解析 库或者代码

发布网友 发布时间:2022-05-23 21:02

我来回答

3个回答

热心网友 时间:2023-11-27 05:57

下面是一个VB6的类的源代码,来自国*站的一个技术专家,类名:CSetting,可以读写ini配置文件,或者读写注册表配置。
Option Explicit
'Settings interface class
'Copyright ?2000 - Stan Schultes
'Written for VBPJ Getting Started September, 2000

'CSetting mode enum
Public Enum csModes
csModeRegistry = 0
csModeINI = 1
End Enum

'class member variables
Private m_eSaveMode As csModes

'mole-level variables
Private msININame As String 'name of .INI file
Private msAppName As String 'app name for settings

'INI setting APIs
Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal SectionName As String, ByVal KeyName As String, ByVal Default As Long, ByVal FileName As String) As Long
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal SectionName As String, ByVal KeyName As String, ByVal Default As String, ByVal ReturnedString As String, ByVal StringSize As Long, ByVal FileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal SectionName As String, ByVal KeyName As String, ByVal KeyValue As String, ByVal FileName As String) As Long
'there is no WritePrivateProfileInt declaration...

'Error definitions
Private Const mklErrOffset As Long = vbObjectError + 512
Private Const mklErrNotNumeric As Long = mklErrOffset + 1
Private Const mksErrNotNumeric As String = "The setting value returned was not numeric"
Private Const mklErrWriting As Long = mklErrOffset + 2
Private Const mksErrWriting As String = "Error writing Setting "
Private Const mklErrReading As Long = mklErrOffset + 3
Private Const mksErrReading As String = "Error reading Setting "

Public Function Init(Optional ByVal AppPath As String, Optional ByVal AppName As Variant, Optional ByVal Mode As csModes = csModeRegistry)
'Set up class variables, default to Registry mode
If IsMissing(AppPath) Then
msININame = App.Path
Else
msININame = Trim$(AppPath)
End If
If IsMissing(AppName) Then
msAppName = App.EXEName
Else
msAppName = Trim$(AppName)
End If
'default to the app's .exe path
msININame = msININame & "\" & msAppName & ".ini"
' msININame = App.Path & "\" & msAppName & ".ini"
SaveMode = Mode
End Function

Public Function GetSettingStr(ByVal Section As String, ByVal KeyName As String, ByVal DefaultValue As String) As String
'returns a string setting
Dim lRet As Long
Dim sBuf As String * 128

On Error GoTo GetSettingStr_Error

If Len(msAppName) = 0 Then Init
Select Case m_eSaveMode
Case csModeRegistry
GetSettingStr = GetSetting(msAppName, Section, KeyName, DefaultValue)
Case Else
lRet = GetPrivateProfileString(Section, KeyName, DefaultValue, sBuf, Len(sBuf), msININame)
GetSettingStr = TrimNull(sBuf)
End Select
GetSettingStr_Exit:
Exit Function

GetSettingStr_Error:
Err.Raise mklErrReading, "CSetting.GetSettingStr", mksErrReading & "(" & Err & ", " & Error & ")"
End Function

Public Function GetSettingInt(ByVal Section As String, ByVal KeyName As String, ByVal DefaultValue As Integer) As Integer
'returns a numeric setting
Dim sSetting As String

On Error GoTo GetSettingInt_Error

If Len(msAppName) = 0 Then Init
Select Case m_eSaveMode
Case csModeRegistry
sSetting = GetSetting(msAppName, Section, KeyName, CStr(DefaultValue))
'check if value is numeric
If IsNumeric(sSetting) Then
GetSettingInt = CInt(sSetting)
Else
'match behavior of .INI return
GetSettingInt = 0
'or - return error if desired
'On Error GoTo 0 'disable handler to return error
'Err.Raise mklErrNotNumeric, "CSetting.GetSettingInt", mksErrNotNumeric
End If
Case Else
'returns 0 if not numeric
GetSettingInt = GetPrivateProfileInt(Section, KeyName, DefaultValue, msININame)
End Select
GetSettingInt_Exit:
Exit Function

GetSettingInt_Error:
Err.Raise mklErrReading, "CSetting.GetSettingInt", mksErrReading & "(" & Err & ", " & Error & ")"
End Function

Public Sub SaveSettingStr(ByVal Section As String, ByVal KeyName As String, ByVal Setting As String)
'saves a string setting
Dim lRet As Long

On Error GoTo SaveSettingStr_Error

If Len(msAppName) = 0 Then Init
Select Case m_eSaveMode
Case csModeRegistry
SaveSetting msAppName, Section, KeyName, Setting
Case Else
lRet = WritePrivateProfileString(Section, KeyName, Setting, msININame)
End Select
SaveSettingStr_Exit:
Exit Sub

SaveSettingStr_Error:
Err.Raise mklErrWriting, "CSetting.SaveSettingStr", mksErrWriting & "(" & Err & ", " & Error & ")"
End Sub

Public Sub SaveSettingInt(ByVal Section As String, ByVal KeyName As String, Setting As Integer)
'saves a numeric setting
Dim lRet As Long

On Error GoTo SaveSettingInt_Error

If Len(msAppName) = 0 Then Init
Select Case m_eSaveMode
Case csModeRegistry
SaveSetting msAppName, Section, KeyName, CStr(Setting)
Case Else
lRet = WritePrivateProfileString(Section, KeyName, CStr(Setting), msININame)
End Select
SaveSettingInt_Exit:
Exit Sub

SaveSettingInt_Error:
Err.Raise mklErrWriting, "CSetting.SaveSettingInt", mksErrWriting & "(" & Err & ", " & Error & ")"
End Sub

Public Property Let SaveMode(Mode As csModes)
'sets save mode, see csModes enum
m_eSaveMode = Mode
End Property

Public Property Get SaveMode() As Long
'returns save mode
SaveMode = CLng(m_eSaveMode)
End Property

Private Function TrimNull(ByVal InString As String) As String
'trims string at first Null character
Dim lPos As Long
TrimNull = Trim$(InString)
lPos = InStr(TrimNull, vbNullChar)
If lPos > 0 Then TrimNull = Left$(TrimNull, lPos - 1)
End Function

Private Sub Class_Initialize()
msININame = App.Path & "\" & App.EXEName & ".ini"
End Sub
使用方法:
在程序的全局模块中定义CSetting的类实例变量:(以下定义了两个,分别读写ini和注册表)
Public SetIni As CSetting '读写ini配置文件参数 '
Public SetReg As CSetting'读写注册表配置参数
在sub main 中初始化:(其中PATH_EXE是你的程序路径,放ini文件的路径)
Set SetIni = New CSetting
SetIni.Init PATH_Exe, App.Title, csModeINI

Set SetReg = New CSetting
SetReg.Init PATH_Exe, App.Title, csModeRegistry

这就可以随便用了。
dbPassword = SetIni.GetSettingStr(NameDB, "Password", "111111")
dbUserID = SetIni.GetSettingStr(NameDB, "UserID", "123456")
dbCatalog = SetIni.GetSettingStr(NameDB, "Catalog", "aaa")
dbDataSource = SetIni.GetSettingStr(NameDB, "DataSource", sky)

cnStr= "Provider=SQLOLEDB.1;Password=" & dbPassword & ";Persist Security Info=True;User ID=" & dbUserID & ";Initial Catalog=" & dbCatalog & ";Data Source=" & dbDataSource '
其它由你自己补充了。

热心网友 时间:2023-11-27 05:58

ini文件的内容大致什么样子?

热心网友 时间:2023-11-27 05:59

这个要给分,前面的都答非所问
http://sourceforge.net/projects/libini/
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
找专业防水队做完还漏水怎么维权 法院会受理房屋漏水造成的纠纷吗? 巴西龟最长活多久,家养!!! 养胃的药最好的是什么啊 婴儿积食发烧不愿吃药怎么办 板门穴位在哪个部位 手机设置放偷看的方法? 凝结水回收器生产厂家? 个人账户养老金预测公式:现有5万元,缴费20年,能领多少钱? 临沂比较有名的男装品牌 刚出来实习的影视后期生该找大公司还是小公司! 预算1.7万,能骑100的太子摩托车有哪些? 刚毕业的学生可以去哪的影视后期制作公司实习 你好?请问LXHIOV 1X240 mm2 Underground cable (Al)是国内什么电缆,谢谢 怎样促进男性生殖器发育 电缆3[4X(1X240)]十4X240代表什么含义 想进入影视公司当后期制作学徒或实习生有哪些渠道? “橡木桶陈酿”的英文 2*[2(ZC-YJV-1KV-1X240)]电缆前面的2*还有中括号里面2代表什么意思?急求答案,谢谢各位大神了 C盘里面有个配置文件 coreavc.ini 这个是什么? ZR-YJV-0.6/1KV 4[2(1X240mm)]+1(1X240)mm² 这个是什么电缆,具体一点。载流量是多少 150太子摩托车有哪些?如何选? 150的太子摩托车推荐 电气图纸中3*3(zbn-yjv-1kv-1x240)+1x120)是什么意思? 加载配置文件时出错 对路径C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config的访问 电缆2(3x240)+1x240 ZR-YJV-1KV-1X240直径是多少? 你跟男神表白之后事情都是怎样发展的? 太子摩托车要多少钱? 1×240的电缆是多少铜 什么太子摩托车好 影视动画制作实习生是做什么的 想找一份影视后期制作的工作,做实习生也行 影视编导实习前途咋样 中低音的英文歌 影视广告公司实习 木酒桶用英语怎么说 (顶级橡木桶干红葡萄酒)翻译成英文 周杰伦有什么中低音的歌曲可以唱的 欧洲进口橡木桶,商检要求提供质检证书。求签发机构名称和这个质检证书英文全称。 求翻译一段文字,懂英文或法文的请进,要求极度富有文采。 第五次机器人大战怎么说服一次就不行了? psp机器人大战mx超级系5冥王计划 铁甲龙要塞怎么闯? GBA第五次机器人大战具体怎么玩,各位大侠给点心得吧! 17岁女孩可以使用洗面奶,素颜霜,洗面奶,防晒霜吗? 第五次机器人大战怎么捕获忠诚度0的机体 GBC第5次机器人大战攻略 第二次超级机器人大战第五篇怎么杀比克 FC游戏第二次机器人大战,谁能杀5关的比克,告诉下。 第5次超级机器人大战