⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keyvalue.c

📁 Windows CE 6.0 BSP for VOIPAC Board (PXA270) Version 2b.
💻 C
📖 第 1 页 / 共 2 页
字号:
    OALMSG( OAL_FUNC && OAL_KEYVAL, (TEXT("-OALKeyValueAddFile(rc=0x%x)\r\n"), rc ));
    return( rc );
}

//------------------------------------------------------------------------------
//  Function:  ProcessFile
//
//  Desc...
//

static OAL_KEYVALUE_STATUS ProcessFile( POAL_KEYVALUE pKeyValue, 
                                        UINT32 StartAddr, 
                                        UINT32 Length )
{
OAL_KEYVALUE_STATUS  rc;
UINT8  *pRead;
UINT8  *pEnd;
UINT32  LineNum;
UINT8  *pLine;
UINT8   Line [OAL_KEYVALUE_MAX_LINE];
UINT8   Key  [OAL_KEYVALUE_MAX_KEY ];
UINT8   Value[OAL_KEYVALUE_MAX_VALUE ];

    OALMSG( OAL_FUNC && OAL_KEYVAL && OAL_VERBOSE, 
    (TEXT("+ProcessFile( 0x%x, 0x%x, %d )\r\n"), pKeyValue, StartAddr, Length ));

    // Setup the start and end of file

    pRead = (UINT8 *)StartAddr;
    pEnd = (UINT8 *)(StartAddr + Length);
    LineNum = 0;

    // Process byte stream line by line

    do
    {
        // Get a line and advance read pointer

        pRead += getline( pRead, pEnd, Line, sizeof(Line) );

        OALMSG( OAL_INFO && OAL_KEYVAL && OAL_VERBOSE, 
        (TEXT("INFO: Line: '%S'\r\n" ), Line ));

        // Keep track of line num for error reporting

        LineNum++;                                  

        // Ignore comment lines or empty lines

        if( strlen( Line ) == 0 )   continue;       // empty line
        if( Line[0]        == '#' ) continue;       // comment is '#' in col 0

        // Access the tokens

        pLine = Line;
        pLine += gettoken( pLine, Key,   sizeof(Key) );
                 gettoken( pLine, Value, sizeof(Value) );

        // Add the key-value pair. This call validates the tokens.

        if( (rc = OALKeyValueAdd( pKeyValue, Key, Value )) != OAL_KEYVALUE_SUCCESS )
        {
            return( rc );           // failed - function indicates error
        }
    }
    while( pRead < pEnd );

    // Successfully processed "file" - indicate success

    rc = OAL_KEYVALUE_SUCCESS;

    OALMSG( OAL_FUNC && OAL_KEYVAL && OAL_VERBOSE, 
    (TEXT("-ProcessFile(rc=%d)\r\n"), rc ));
    return( rc );
}

//------------------------------------------------------------------------------
//  Function:  gettoken
//
//  Simplified get token function. Copies token into passed buffer and returns
//  the number of characters processed.
//

static UINT32 gettoken( UINT8 *pLine, UINT8 *pToken, UINT32 TokenLen )
{
UINT8  *pRead;
UINT32  Count;
UINT    rc;

    OALMSG( OAL_FUNC && OAL_KEYVAL && OAL_VERBOSE, 
    (TEXT("+gettoken( 0x%x, 0x%x )\r\n"), pLine, pToken ));

    pRead = pLine;
    Count = 0;

    // Eat any preceeding whitespace

    while( ((*pRead == ' ') || (*pRead == '\t')) && (*pRead) )
    { 
        pRead++; 
    }

    // Copy token

    while( (*pRead != ' ') && (*pRead != '\t') && (*pRead) && (Count < TokenLen) ) 
    { 
        *pToken++ = *pRead++; 
        Count++;
    }

    // Null terminate

    *pToken = 0;

    // Indicate bytes processed

    rc = pRead - pLine;

    OALMSG( OAL_FUNC && OAL_KEYVAL && OAL_VERBOSE, 
    (TEXT("-gettoken( rc=%d )\r\n"), rc ));
    return( rc );
}

//------------------------------------------------------------------------------
//  Function:  getline
//
//  Processes the byte stream into lines.
//

static UINT32 getline( UINT8 *pStart, UINT8 *pEnd, UINT8 *pLine, UINT32 LineLen )
{
UINT32  rc;
UINT8  *pRead;
UINT32  Count;
BOOL    Done;

    OALMSG( OAL_FUNC && OAL_KEYVAL && OAL_VERBOSE, 
    (TEXT("+getline( 0x%x, 0x%x, 0x%x, %d )\r\n"), pStart, pEnd, pLine, LineLen ));

    // Process all characters up to \r\n. Also support the 
    // possibility of just a \n.

    Done = FALSE;
    pRead = pStart;
    Count = 0;

    // Process characters until newline, end of stream, or exceed Line bufer.

    do
    {
        switch( *pRead )
        {
        case '\r':

            pRead++;                // eat the CR/LF
            pRead++;
            Done = TRUE;
            break;

        case '\n':                  // just LF 

            pRead++;
            Done = TRUE;
            break;

        default:

            *pLine++ = *pRead++;    // copy to line buffer
            Count++;
        }
    }
    while( (pStart < pEnd) && (Count < LineLen) && !Done );

    // Terminate the line

    *pLine = 0;

    // Indicate total number of characters processed

    rc = pRead - pStart;

    OALMSG( OAL_FUNC && OAL_KEYVAL && OAL_VERBOSE, 
    (TEXT("-getline(rc=%d)\r\n"), rc ));
    return( rc );
}

//------------------------------------------------------------------------------
//  Function:  StoreLiteral
//
//  This function stores the literal string into the table, copying from
//  the bottom of the table. It returns the offset into the table for
//  storage by the OAL_KEYVALUE_PAIR info type.
//

static UINT32 StoreLiteral( POAL_KEYVALUE pKeyValue, char *Literal, UINT32 Length )
{
UINT8   *pStart;
UINT32  rc;

    OALMSG( OAL_FUNC && OAL_KEYVAL && OAL_VERBOSE, 
    (TEXT("+StoreLiteral( 0x%x, '%S', %d )\r\n"), pKeyValue, Literal, Length ));

    // The pEnd points to the last position in the table

    pStart = pKeyValue->pEnd - (Length-1);

    // Copy the literal into the table

    memcpy( pStart, Literal, Length );

    // Update the end pointer to be ahead of copy point

    pKeyValue->pEnd = pStart - 1;

    // Return the offset from the start of the table

    rc = pStart - pKeyValue->pTable;

    // Indicate status

    OALMSG( OAL_FUNC && OAL_KEYVAL && OAL_VERBOSE, 
    (TEXT("-StoreLiteral(rc=0x%x)\r\n"), rc ));
    return( rc );
}

//------------------------------------------------------------------------------
//  Section: Test Support
//
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//  Function:  OALKeyValueTestMain
//
//  Desc...
//

BOOL OALKeyValueTestMain( VOID )
{
BOOL            rc;
OAL_KEYVALUE    KeyValue;
UINT8           Table[ 256 ];
UINT8           Value[ 32 ];
UINT32          ValueLen;

    OALMSG( OAL_FUNC && OAL_KEYVAL, (TEXT("+OALKeyValueTestMain\r\n")));

    // Init the component

    OALKeyValueInit( &KeyValue, Table, sizeof(Table) );

    // Add some key-value pairs

    OALKeyValueAdd( &KeyValue,   "1Key1",     "1Literal1" );
    OALKeyValueAdd( &KeyValue,  "22Key22",   "22Literal22" );
    OALKeyValueAdd( &KeyValue, "333Key333", "333Literal333" );

    // Get value by key

    ValueLen = 32;
    if( OALKeyValueGet( &KeyValue, "333Key333", Value, &ValueLen ) == OAL_KEYVALUE_SUCCESS )
    {
        OALMSG( OAL_FUNC, (TEXT("Value '%S' Length: %d\r\n"), Value, ValueLen ));
    }
    else
    {
        OALMSG( OAL_FUNC, (TEXT("Value NOT found\r\n")));
    }

    ValueLen = 32;
    if( OALKeyValueGet( &KeyValue, "22Key22", Value, &ValueLen ) == OAL_KEYVALUE_SUCCESS )
    {
        OALMSG( OAL_FUNC, (TEXT("Value '%S' Length: %d\r\n"), Value, ValueLen ));
    }
    else
    {
        OALMSG( OAL_FUNC, (TEXT("Value NOT found\r\n")));
    }

    ValueLen = 32;
    if( OALKeyValueGet( &KeyValue, "1Key1", Value, &ValueLen ) == OAL_KEYVALUE_SUCCESS )
    {
        OALMSG( OAL_FUNC, (TEXT("Value '%S' Length: %d\r\n"), Value, ValueLen ));
    }
    else
    {
        OALMSG( OAL_FUNC, (TEXT("Value NOT found\r\n")));
    }

    // Get value length only

    ValueLen = 32;
    if( OALKeyValueGet( &KeyValue, "1Key1", NULL, &ValueLen ) == OAL_KEYVALUE_SUCCESS )
    {
        OALMSG( OAL_FUNC, (TEXT("Value NULL  Length: %d\r\n"), ValueLen ));
    }
    else
    {
        OALMSG( OAL_FUNC, (TEXT("Value NOT found\r\n")));
    }

    // INI File Interface

    OALKeyValueAddFile( &KeyValue, pTOC, "primexsys.ini" );

    // Indicate success

    rc = TRUE;
    OALMSG( OAL_FUNC, (TEXT("-OALKeyValueTestMain(rc=%d)\r\n"), rc ));
    return( rc );
}

//------------------------------------------------------------------------------
//  Function:  DumpCB
//
//  Dump the control block for testing
//

#ifdef DUMP_CONTEXT

static void DumpCB( POAL_KEYVALUE pKeyValue )
{

UINT32              i;
POAL_KEYVALUE_PAIR  pPair;

    OALMSG( OAL_FUNC && OAL_KEYVAL, (TEXT("+DumpCB( 0x%x )\r\n"), pKeyValue ));

    // Dump the control block

    OALMSG( OAL_FUNC && OAL_KEYVAL, (TEXT("Signature 0x%x\r\n"), pKeyValue->Signature ));
    OALMSG( OAL_FUNC && OAL_KEYVAL, (TEXT("Count.... %d\r\n"),   pKeyValue->Count ));
    OALMSG( OAL_FUNC && OAL_KEYVAL, (TEXT("Space     %d\r\n"),   pKeyValue->Space ));
    OALMSG( OAL_FUNC && OAL_KEYVAL, (TEXT("TableSize %d\r\n"),   pKeyValue->TableSize ));
    OALMSG( OAL_FUNC && OAL_KEYVAL, (TEXT("pEnd..... 0x%x\r\n"), pKeyValue->pEnd ));
    OALMSG( OAL_FUNC && OAL_KEYVAL, (TEXT("pTable... 0x%x\r\n"), pKeyValue->pTable ));

    // Dump the key-value pairs

    OALMSG( OAL_FUNC && OAL_KEYVAL, (TEXT("Key-Value Pairs:\r\n")));

    for( i=0, pPair = (POAL_KEYVALUE_PAIR )pKeyValue->pTable;
         i < pKeyValue->Count;
         i++, pPair++ )
    {
        OALMSG( OAL_FUNC && OAL_KEYVAL, (TEXT("Key   %S\r\n"), pKeyValue->pTable + pPair->KeyOffset ));
        OALMSG( OAL_FUNC && OAL_KEYVAL, (TEXT("Value %S\r\n"), pKeyValue->pTable + pPair->ValueOffset ));
    }

    // Indicate status

    OALMSG( OAL_FUNC && OAL_KEYVAL, (TEXT("-DumpCB\r\n")));

}
#else

static void DumpCB( POAL_KEYVALUE pKeyValue ) {}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -