📄 dbm_embd.c
字号:
/****************************************************************************
File: dbm_embd.c
-----------------------------------------------------------------------------
Created : A. Beck, Hilscher GmbH
Date : 28-Jul-2001
Project : direct access of a DBM for embedded systems
-----------------------------------------------------------------------------
Description:
application LIB, containing library routines to handle a DBM file
Functions:
short dbm_init ( void * );
short dbm_get_num ( unsigned char *,
unsigned char * );
short dbm_get_num_of_set ( unsigned char ,
unsigned short * );
short dbm_fast_read ( unsigned char ,
unsigned short ,
unsigned short ,
void * ,
unsigned short * );
-----------------------------------------------------------------------------
Todo:
-----------------------------------------------------------------------------
Changes:
Name Date Version Description
-----------------------------------------------------------------------
Ab 28-Jul-2001 1.000 created
****************************************************************************/
#include <stdio.h>
#include <string.h>
#pragma pack (1)
#include "dbm_embd.h"
/* counter to hold the found number of table within the DBM file */
static unsigned char bDbmNumOfTables;
/* structure to fill in the current DBM file table sets and entrie counts */
static DBM_FILE_TABLES atDbm[DBM_TAB_MAX];
/* <ST> =================================================================================
Function: dbm_init
fills up the table names of the given DBM file into a
indexed structure
---------------------------------------------------------------------------------------
Input : void* ptDbmFile - pointer to the DBM file at offset 44
Output : -
Return : DBM_OK - init successful
DBM_DAT_DIR_FULL - too much entries found
================================================================================= <En> */
short dbm_init( void * ptDbmFile )
{
DBM_DATSTR * ptDbmDat;
DBM_SET_0 * ptDbmSet0;
unsigned short * pusTableEntry;
unsigned short usCounterA;
unsigned short usCounterB;
bDbmNumOfTables = 0;
ptDbmDat = (DBM_DATSTR *) ptDbmFile;
ptDbmSet0 = (DBM_SET_0 *) ptDbmFile;
pusTableEntry = (unsigned short *) ptDbmFile;
usCounterA = 0;
while ( usCounterA < ptDbmDat->akt_dat && bDbmNumOfTables < DBM_TAB_MAX )
{
atDbm[bDbmNumOfTables].pvStartAddress = (void *) ptDbmFile;
usCounterB = pusTableEntry[sizeof ( DBM_DATSTR ) / 2 + 2 * usCounterA] + 6;
atDbm[bDbmNumOfTables].usSetNumber = usCounterB;
ptDbmSet0 = (DBM_SET_0 *)&pusTableEntry[pusTableEntry[usCounterB]];
memcpy ( &atDbm[bDbmNumOfTables].szName, &ptDbmSet0->name, MAX_NAME_LENGTH );
atDbm[bDbmNumOfTables].usSetEntries = ptDbmSet0->eintraege;
atDbm[bDbmNumOfTables].usSetCount = ptDbmSet0->akt_eintraege;
usCounterA = usCounterA + 1;
bDbmNumOfTables = bDbmNumOfTables + 1;
}
if ( bDbmNumOfTables >= DBM_TAB_MAX )
{
return ( DBM_DAT_DIR_FULL );
}
else
{
return ( DBM_OK );
}
}
/* <ST> =================================================================================
Function: dbm_get_num
returns the handle to the given table name
---------------------------------------------------------------------------------------
Input : unsigned char *pszName - pointer to the character string of the
table name
unsigned char *pbHandle - pointer where to place the handle into
Output :
Return : DBM_OK - handle could be got
DBM_NO_DAT - table name not found
================================================================================= <En> */
short dbm_get_num( unsigned char *pszName,
unsigned char *pbHandle )
{
unsigned char bIndex;
*pbHandle = 0;
while ( *pbHandle < bDbmNumOfTables )
{
/* check for the given name if it can be found
somewhere in the initialized DBM table */
bIndex = 0;
while( pszName[bIndex] == atDbm[*pbHandle].szName[bIndex] && atDbm[*pbHandle].szName[bIndex])
{
bIndex++;
}
if( atDbm[*pbHandle].szName[bIndex] == 0 && pszName[bIndex] == 0 )
{
return ( DBM_OK );
}
*pbHandle = *pbHandle + 1;
}
/* entry not found in the current DBM file, return error */
return ( (DBM_NO_DAT) );
}
/* <ST> =================================================================================
Function: dbm_get_num_of_set
returns the number of data sets of a table
---------------------------------------------------------------------------------------
Input : unsigned char bHandle - handle of the table
unsigned short *pusNumberOfSets - pointer where to place the
the found number into
Output :
Return : DBM_OK - execution successful
DBM_NO_DAT - handle parameter not valid
================================================================================= <En> */
short dbm_get_num_of_set( unsigned char bHandle,
unsigned short * pusNumberOfSets )
{
if ( bHandle < bDbmNumOfTables )
{
*pusNumberOfSets = atDbm[bHandle].usSetCount;
return ( 0 );
}
else
{
*pusNumberOfSets = 0;
return ( DBM_NO_DAT );
}
}
/* <ST> =================================================================================
Function: dbm_get_num_of_set
returns the number of data sets of a table
---------------------------------------------------------------------------------------
Input : unsigned char bHandle - handle of the table
unsigned short usSetNumber - set number starting with 1
unsigned short usMaximumLength - the maximum reserved buffer
length where the data set
shall be copied to
void *pvDestinationPointer - pointer to the destination
buffer
unsigned short *pusRealLength - pointer to the real length
variable, where the function
places the real length of
the read data set
Output :
Return : DBM_OK - execution successful
DBM_NO_DAT - handle parameter not valid
DBM_NO_SET - usSetNumber out of range
DBM_SET_DEL - data set is not available
DBM_NOT_ENOUGH_FREESPACE - usMaximumLength indicated to less
freespace for the data set
================================================================================= <En> */
short dbm_fast_read ( unsigned char bHandle,
unsigned short usSetNumber,
unsigned short usMaximumLength,
void * pvDestinationPointer,
unsigned short * pusRealLength )
{
unsigned short * pusTableEntry;
DBM_SET * pvSetEntry;
*pusRealLength = 0;
if ( bHandle < bDbmNumOfTables )
{
if ( usSetNumber <= atDbm[bHandle].usSetEntries )
{
pusTableEntry = atDbm[bHandle].pvStartAddress;
if ( pusTableEntry[atDbm[bHandle].usSetNumber + usSetNumber] )
{
pvSetEntry = (DBM_SET *)&pusTableEntry[pusTableEntry[atDbm[bHandle].usSetNumber + usSetNumber]];
*pusRealLength = pvSetEntry->usmax - 12;
if ( pvSetEntry->bUngerade & 0x01 )
{
*pusRealLength = *pusRealLength - 1;
}
if ( *pusRealLength <= usMaximumLength )
{
memcpy ( pvDestinationPointer, &pvSetEntry->ausData[0], *pusRealLength );
return ( 0 );
}
else
{
*pusRealLength = 0;
return ( DBM_NOT_ENOUGH_FREESPACE );
}
}
else
{
return ( DBM_SET_DEL );
}
}
else
{
return ( DBM_NO_SET );
}
}
else
{
return ( DBM_NO_DAT );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -