File: utils.cpp

package info (click to toggle)
openhpi 3.8.0-2.3
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 31,888 kB
  • sloc: ansic: 225,326; cpp: 63,687; java: 16,472; cs: 15,161; python: 11,884; sh: 11,508; makefile: 4,945; perl: 1,529; xml: 36; asm: 13
file content (115 lines) | stat: -rw-r--r-- 3,111 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*      -*- c++ -*-
 *
 * (C) Copyright Pigeon Point Systems. 2011
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTUTILSLITY or FITNESS FOR A PARTICULAR PURPOSE.  This
 * file and program are licensed under a BSD style license.  See
 * the Copying file included with the OpenHPI distribution for
 * full licensing terms.
 *
 * Author(s):
 *        Anton Pak <anton.pak@pigeonpoint.com>
 */

#include <stddef.h>
#include <string.h>

#include <algorithm>
#include <cstdio>

#include "utils.h"


namespace TA {


/**************************************************************
 * Entity Path Helpers
 *************************************************************/
void MakeUnspecifiedHpiEntityPath( SaHpiEntityPathT& ep )
{
    for ( size_t i = 0; i < SAHPI_MAX_ENTITY_PATH; ++i ) {
        ep.Entry[i].EntityType     = SAHPI_ENT_UNSPECIFIED;
        ep.Entry[i].EntityLocation = 0;
    }
}


/**************************************************************
 * Text Buffer Helpers
 *************************************************************/
void MakeHpiTextBuffer( SaHpiTextBufferT& tb, const char * s )
{
    if ( s ) {
        MakeHpiTextBuffer( tb, s, strlen( s ) );
    }
}

void MakeHpiTextBuffer( SaHpiTextBufferT& tb, const char * s, size_t size )
{
    tb.DataType   = SAHPI_TL_TYPE_TEXT;
    tb.Language   = SAHPI_LANG_ENGLISH;
    if ( s ) {
        tb.DataLength = std::min<size_t>( size, SAHPI_MAX_TEXT_BUFFER_LENGTH );
        if ( tb.DataLength != 0 ) {
            memcpy( tb.Data, s, tb.DataLength );
        } else {
            tb.Data[0] = '\0';
        }
    } else {
        tb.DataLength = 0;
    }
}

void MakeHpiTextBuffer( SaHpiTextBufferT& tb, char c, size_t size )
{
    if ( size > 0 ) {
        tb.DataType   = SAHPI_TL_TYPE_TEXT;
        tb.Language   = SAHPI_LANG_ENGLISH;
        tb.DataLength = std::min<size_t>( size, SAHPI_MAX_TEXT_BUFFER_LENGTH );
        memset( &tb.Data[0], c, tb.DataLength );
    } else {
        tb.DataLength = 0;
    }
}

void FormatHpiTextBuffer( SaHpiTextBufferT& tb, const char * fmt, ... )
{
    va_list ap;

    va_start(ap, fmt );
    vFormatHpiTextBuffer( tb, fmt, ap );
    va_end( ap );
}

void vFormatHpiTextBuffer( SaHpiTextBufferT& tb, const char * fmt, va_list ap )
{
    char * p = reinterpret_cast<char*>( &(tb.Data[0]) );

    tb.DataType   = SAHPI_TL_TYPE_TEXT;
    tb.Language   = SAHPI_LANG_ENGLISH;
    int len = vsnprintf( p, SAHPI_MAX_TEXT_BUFFER_LENGTH, fmt, ap );
    tb.DataLength = ( len < 0 ) ? 0 : (SaHpiUint8T)(len);
}

void AppendToTextBuffer( SaHpiTextBufferT& tb, const SaHpiTextBufferT& appendix )
{
    if ( tb.DataType != appendix.DataType ) {
        return;
    }
    if ( tb.Language != appendix.Language ) {
        return;
    }
    size_t len = std::min<size_t>( SAHPI_MAX_TEXT_BUFFER_LENGTH - tb.DataLength,
                                   appendix.DataLength );
    if ( len > 0 ) {
        memcpy( &tb.Data[tb.DataLength], &appendix.Data[0], len );
    }
    tb.DataLength += len;
}


}; // namespace TA