/*
 * pxio.c
 *
 * Copyright (C) 2007  Joshua Hoblitt
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * program; see the file COPYING. If not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVB_CONFIG_H
#include <config.h>
#endif

#include <stdbool.h>

#include <pslib.h>

#include "pxtools.h"
#include "pxio.h"

bool convertIdToStr(psArray *mds)
{
    PS_ASSERT_PTR_NON_NULL(mds, false);

    char *ids[] = {"guide_id", "chip_id", "cam_id", "det_id", "warp_id", "diff_id", "stack_id", NULL};

    // loop over array of metadatas
    for (long i = 0; i < psArrayLength(mds); i++) {
        psMetadata *md = mds->data[i];
        // loop over array of ID fields
        for (int j = 0; ids[j] != NULL; j++) {
            char *name = ids[j];
            bool status = false;
            psS64 numeric_id = psMetadataLookupS64(&status, md, name);
            if (!status) {
                continue;
//                psError(PS_ERR_UNKNOWN, false, "failed to lookup value for %s",
//                        name);
 //               return false;
            }
            psMetadataRemoveKey(md, name);
            psString string_id = psDBIntToString((psU64)numeric_id);
            psMetadataAddStr(md, PS_LIST_HEAD, name, 0, NULL, string_id);
            psFree(string_id);
        }
    }

    return true;
}
