static MI_Result _PutQualifiers()

in LCM/codec/mof/mofserializer.c [1026:1190]


static MI_Result _PutQualifiers(
    _Inout_ Buf* out,
    MI_Uint32 flags,
    _In_ const MI_QualifierSet* qset,
    MI_Boolean indent)
{
    MI_Uint32 count;
    MI_Uint32 i;
    typedef struct _Info
    {
        const MI_Char* name;
        size_t len;
        MI_Uint32 flag;
    }
    Info;
    static const Info info[] =
    {
#if defined(EMIT_UPPERCASE_FLAG_QUALIFIERS)
        { LIT("KEY"), MI_FLAG_KEY },
        { LIT("IN"), MI_FLAG_IN },
        { LIT("OUT"), MI_FLAG_OUT },
        { LIT("REQUIRED"), MI_FLAG_REQUIRED },
        { LIT("STATIC"), MI_FLAG_STATIC },
        { LIT("ABSTRACT"), MI_FLAG_ABSTRACT },
        { LIT("TERMINAL"), MI_FLAG_TERMINAL },
        { LIT("EXPENSIVE"), MI_FLAG_EXPENSIVE },
        { LIT("STREAM"), MI_FLAG_STREAM },
#else
        { LIT("Key"), MI_FLAG_KEY },
        { LIT("In"), MI_FLAG_IN },
        { LIT("Out"), MI_FLAG_OUT },
        { LIT("Required"), MI_FLAG_REQUIRED },
        { LIT("Static"), MI_FLAG_STATIC },
        { LIT("Abstract"), MI_FLAG_ABSTRACT },
        { LIT("Terminal"), MI_FLAG_TERMINAL },
        { LIT("Expensive"), MI_FLAG_EXPENSIVE },
        { LIT("Stream"), MI_FLAG_STREAM },
#endif
    };
    MI_Uint32 qmask = 0;
    MI_Uint32 qcount = 0;

    RETERR(MI_QualifierSet_GetQualifierCount(qset, &count));

    /* Bail out if there are no qualifiers */
    if (count == 0)
    {
        const MI_Uint32 MASK =
            MI_FLAG_KEY |
            MI_FLAG_IN |
            MI_FLAG_OUT |
            MI_FLAG_REQUIRED |
            MI_FLAG_STATIC |
            MI_FLAG_ABSTRACT |
            MI_FLAG_TERMINAL |
            MI_FLAG_EXPENSIVE |
            MI_FLAG_STREAM;

        if ((flags & MASK) == 0)
            return MI_RESULT_OK;
    }

    /* Indent? */
    if (indent)
        RETERR(Buf_Put(out, LIT("    ")));

    /* Put opening bracket */
    RETERR(Buf_Put(out, LIT("[")));

    /* Put In(False) qualifier (for parameters only) */

    if ((flags & MI_FLAG_PARAMETER) && !(flags & MI_FLAG_IN))
    {
        if (qcount)
            RETERR(Buf_Put(out, LIT(", ")));

#if defined(EMIT_UPPERCASE_FLAG_QUALIFIERS)
        Buf_Put(out, LIT("IN(False)"));
#else
        Buf_Put(out, LIT("In(False)"));
#endif
        qcount++;
        qmask |= MI_FLAG_IN;
    }

    /* Put flag qualifiers */

    for (i = 0; i < MI_COUNT(info); i++)
    {
        if (flags & info[i].flag)
        {
            if (qcount)
                RETERR(Buf_Put(out, LIT(", ")));

            RETERR(Buf_Put(out, info[i].name, info[i].len));
            qcount++;
            qmask |= info[i].flag;
        }
    }

    /* Put ordinary qualifiers */
    for (i = 0; i < count; i++)
    {
        const MI_Char* qname;
        MI_Type qtype;
        MI_Uint32 qflags;
        MI_Value qvalue;
        Aliases aliases = { 0, 0, 0, NULL };
        MI_Boolean found = MI_FALSE;
        MI_Uint32 j;

        /* Get the qualifier value */
        RETERR(MI_QualifierSet_GetQualifierAt(
            qset,
            i,
            &qname,
            &qtype,
            &qflags,
            &qvalue));

        if (!qname)
            return MI_RESULT_FAILED;

        /* Skip this qualifier if already emitted as part of flags above */
        for (j = 0; j < MI_COUNT(info); j++)
        {
            if (Tcscasecmp(qname, info[j].name) == 0 && (info[j].flag & qmask))
            {
                found = MI_TRUE;
                break;
            }
        }

        if (found)
            continue;

        /* Put comma */
        if (qcount)
            RETERR(Buf_Put(out, LIT(", ")));

        /* Put the qualifier name */
        RETERR(Buf_PutStr(out, qname));

        /* Put value (expect for boolean trues, where true is implicit) */
        if (!(qtype == MI_BOOLEAN && qvalue.boolean))
        {
            /* Put opening parenthesis for non-arrays */
            if (!_IsArrayType(qtype))
                RETERR(Buf_Put(out, LIT("(")));

            /* Put the qualifier value */
            RETERR(_PutValue(out, &qvalue, qtype, &aliases));

            /* Put closing parenthesis for non-arrays */
            if (!_IsArrayType(qtype))
                RETERR(Buf_Put(out, LIT(")")));
        }

        qcount++;
    }

    RETERR(Buf_Put(out, LIT("]\n")));

    return MI_RESULT_OK;
}