A few years ago, I wanted to change my program from INI files to DBF.
At that time, we developed a function to convert INI files to dbf and then to access the keys.
I think it will be easiest if you continue to use the DESKTOP program, INI files online. You are used to so many entries.
/*
GetDBProfString( cSection, cEntry, uDefault, cIniFile )
- Syntax similar to GetPvProfString() function
- If file extention of cIniFile is "ini", then the call is forwarded to GetPvProgString().
So, in - your application you can use this function in the place of GetPvProgString at all places.
- Otherwise this function reads from cIniFile, forcing extention "dbf".
- If the dbf does not exist, returns the default value
- If the dbf exists, then opens the dbf and chceks for the keys
- If the keys exist, returns value from dbf
- Otherwise returns default
WriteDBProfString( cSection, cEntry, uValue, cIniFile )
- Syntax similar to WritePProfString() function
- If file extention of cIniFile is "ini", then the call is forwarded to WritePProfString().
So, in - your application you can use this function in the place of WritePProfString at all places.
- Otherwise this function writes cIniFile, forcing extention "dbf".
- If the dbf does not exist, dbf is created
- Opens the dbf and chceks for the keys
- If the keys exist, updates the value in the dbf
- Otherwise appends a new entry with the values.
EditIniDBF( cDbf )
- If the dbf does not exist and user wants to create, then the dbf is created
- User can edit values and/or append new values.
*/
I am not yet sure whether you should load all INI entries into a hash at program start or if you need an entry/key then simply access the database.
How do you solve the problem with the INI settings in Mod Harbour?
Best regards,
Otto
- Code: Select all Expand view RUN
#include "fivewin.ch"
/*
GetDBProfString( cSection, cEntry, uDefault, cIniFile )
- Syntax similar to GetPvProfString() function
- If file extention of cIniFile is "ini", then the call is forwarded to GetPvProgString().
So, in - your application you can use this function in the place of GetPvProgString at all places.
- Otherwise this function reads from cIniFile, forcing extention "dbf".
- If the dbf does not exist, returns the default value
- If the dbf exists, then opens the dbf and chceks for the keys
- If the keys exist, returns value from dbf
- Otherwise returns default
WriteDBProfString( cSection, cEntry, uValue, cIniFile )
- Syntax similar to WritePProfString() function
- If file extention of cIniFile is "ini", then the call is forwarded to WritePProfString().
So, in - your application you can use this function in the place of WritePProfString at all places.
- Otherwise this function writes cIniFile, forcing extention "dbf".
- If the dbf does not exist, dbf is created
- Opens the dbf and chceks for the keys
- If the keys exist, updates the value in the dbf
- Otherwise appends a new entry with the values.
EditIniDBF( cDbf )
- If the dbf does not exist and user wants to create, then the dbf is created
- User can edit values and/or append new values.
*/
#define TEST
REQUEST DBFCDX
static hIni := {=>}
//----------------------------------------------------------------------------//
#ifdef TEST
function Main()
local cDbf := "c:\fwh\samples\xplan_ini.dbf"
EditIniDBF( cDbf )
/* ? "test"
? GetDBProfString( "files", "fields", 23, "rama.dbf" )
WriteDBProfString( "files", "fields", 77, "rama.dbf" )
WriteDBProfString( "files", "fields", 99, "ini\ranga.dbf" )
? GetDBProfString( "files", "fields", 23, "ini\ranga.dbf" )
*/
return nil
#endif
//----------------------------------------------------------------------------//
function GetDBProfString( cSection, cEntry, uDefault, cIniFile )
local uRet, cAlias
cIniFile := AllTrim( cIniFile )
if Lower( cFileExt( cIniFile ) ) == "ini"
return GetPvProfString( cSection, cEntry, uDefault, cIniFile )
endif
uRet := uDefault
cIniFile := TrueName( cFileSetExt( cIniFile, "dbf" ) )
cAlias := GetIniAlias( cIniFile )
if .not. Empty( cAlias )
cSection := LTrim( cSection )
cEntry := LTrim( cEntry )
if ( cAlias )->( DBSEEK( MakeKey( cSection, cEntry ) ) )
uRet := ( cAlias )->X_VALUE
if ValType( uDefault ) == 'N'
uRet := Val( uRet )
endif
endif
endif
return uRet
//----------------------------------------------------------------------------//
function WriteDBProfString( cSection, cEntry, uValue, cIniFile )
local cAlias
cIniFile := AllTrim( cIniFile )
if Lower( cFileExt( cIniFile ) ) == "ini"
return WritePProString( cSection, cEntry, uValue, cIniFile )
endif
cIniFile := TrueName( cFileSetExt( cIniFile, "dbf" ) )
cAlias := OpenIniDBF( cIniFile )
if ( cAlias )->( DBSEEK( MakeKey( cSection, cEntry ) ) )
do while !( cAlias )->( DBRLOCK() )
enddo
( cAlias )->X_VALUE := LTrim( cValToChar( uValue ) )
DBRUNLOCK()
else
do while .t.
( cAlias )->( DBAPPEND() )
if .not. NetErr()
EXIT
endif
enddo
( cAlias )->X_SECTION := LTrim( cSection )
( cAlias )->X_KEY := LTrim( cEntry )
( cAlias )->X_VALUE := LTrim( cValToChar( uValue ) )
endif
return nil
//----------------------------------------------------------------------------//
function EditIniDBF( cDbf )
local cAlias
cDbf := TrueName( cFileSetExt( cDbf, "dbf" ) )
if .not. File( cDbf )
if .not. MsgNoYes( cDbf + " does not exist" + CRLF + ;
"Create new DBF?" )
return nil
endif
endif
cAlias := OpenIniDBF( cDbf )
XBROWSER cAlias TITLE cDBF FASTEDIT
// DO NOT CLOSE THE DBF. IT SHOULD REMAIN OPEN
return nil
//----------------------------------------------------------------------------//
static function GetIniAlias( cIniDbf )
local cAlias
cIniDBF := UPPER( cIniDBF )
if HHasKey( hIni, cIniDbf )
cAlias := hIni[ cIniDBF ]
endif
if Empty( cAlias ) .and. File( cIniDBF )
cAlias := OpenIniDbf( cIniDBF )
endif
return cAlias
//----------------------------------------------------------------------------//
static function OpenIniDBF( cIniDbf )
local cAlias
if .not. ( PROCNAME( 1 ) == "GETINIALIAS" )
cAlias := GetIniAlias( @cIniDBF )
endif
if Empty( cAlias )
if .not. File( cIniDBF )
CreateIniDBF( cIniDBF )
elseif .not. File( cFileSetExt( cIniDBF, "cdx" ) )
CreateIniIndex( cIniDBF )
endif
cAlias := cGetNewAlias( "INI" )
USE ( cIniDBF ) NEW SHARED ALIAS ( cAlias ) VIA "DBFCDX"
SET ORDER TO TAG SECKEY
SET FILTER TO !DELETED()
GO TOP
hIni[ Upper( cIniDBF ) ] := cAlias
endif
return cAlias
//----------------------------------------------------------------------------//
static function CreateIniDBF( cDbf )
field X_SECTION, X_KEY
local cPath := cFilePath( cDbf )
if .not. lIsDir( cPath )
lMkFullPath( cPath )
endif
DBCREATE( cDbf, ;
{ { "X_SECTION", "C", 30, 0 } ,;
{ "X_KEY", "C", 30, 0 } ,;
{ "X_VALUE", "C", 100, 0 } ,;
{ "X_COMMENTS", "C", 200, 0 } }, ;
"DBFCDX", .T., "DBINI" )
INDEX ON UPPER(X_SECTION)+UPPER(X_KEY) TAG SECKEY
INDEX ON DELETED() TAG DELETED
CLOSE DBINI
return nil
//----------------------------------------------------------------------------//
static function CreateIniIndex( cDbf )
field X_SECTION, X_KEY
USE ( cDBF ) NEW ALIAS "INICDX" EXCLUSIVE VIA "DBINI"
if .not. USED()
? "Can not open " + cDBF + "Exclusively"
QUIT
endif
INDEX ON UPPER(X_SECTION)+UPPER(X_KEY) TAG SECKEY
INDEX ON DELETED() TAG DELETED
CLOSE DBINI
return .t.
//----------------------------------------------------------------------------//
static function MakeKey( cSection, cKey )
return PADR( UPPER( LTRIM( cSection ) ), 30 ) + ;
PADR( UPPER( LTRIM( cKey ) ), 30 )
//----------------------------------------------------------------------------//