(file) Return to stringarray.c CVS log (file) (dir) Up to [OMI] / omi / base

File: [OMI] / omi / base / stringarray.c (download)
Revision: 1.3, Mon Apr 20 17:19:50 2015 UTC (9 years ago) by krisbash
Branch: MAIN
CVS Tags: OMI_1_0_8_2, OMI_1_0_8_1, HEAD
Changes since 1.2: +10 -10 lines
OMI 1.0.8-1

/*
**==============================================================================
**
** Open Management Infrastructure (OMI)
**
** Copyright (c) Microsoft Corporation
** 
** Licensed under the Apache License, Version 2.0 (the "License"); you may not 
** use this file except in compliance with the License. You may obtain a copy 
** of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
** MERCHANTABLITY OR NON-INFRINGEMENT. 
**
** See the Apache 2 License for the specific language governing permissions 
** and limitations under the License.
**
**==============================================================================
*/

#include "stringarray.h"
#include <pal/strings.h>
#include <pal/format.h>

MI_Result StringArray_Init(
    StringArray* self,
    size_t capacity,
    Batch* batch)
{
    self->batch = batch;
    self->size = 0;
    self->capacity = capacity;

    if (capacity)
    {
        self->data = Batch_Get(batch, sizeof(ZChar*) * capacity);

        if (!self->data)
            return MI_RESULT_FAILED;
    }
    else
        self->data = NULL;

    return MI_RESULT_OK;
}

MI_Result StringArray_Add(
    StringArray* self,
    const ZChar* str)
{
    ZChar* tmp;

    /* Enlarge data capacity if necessary */
    if (self->size == self->capacity)
        return MI_RESULT_FAILED;

    /* Allocate new string */
    tmp = (ZChar*)Batch_Tcsdup(self->batch, str);

    if (!tmp)
        return MI_RESULT_FAILED;

    /* Add to end of array */
    self->data[self->size++] = tmp;

    return MI_RESULT_OK;
}

void StringArray_Print(
    const StringArray* self,
    FILE* os)
{
    size_t i;

    Ftprintf(os, PAL_T("{"));

    for (i = 0; i < self->size; i++)
    {
        Ftprintf(os, PAL_T("%T"), tcs(self->data[i]));

        if (i + 1 != self->size)
            Ftprintf(os, PAL_T(", "));
    }

    Ftprintf(os, PAL_T("}"));
}

ViewCVS 0.9.2