#include <stdio.h>
#include "pslib.h"

void psMetadataPrint(psMetadata *md, int level)
{
    psMetadataIterator *iter = psMetadataIteratorAlloc(md, PS_LIST_HEAD, NULL);	// Iterator
    psMetadataItem *item = NULL;	// Metadata item
    while (item = psMetadataGetAndIncrement(iter)) {
	// Indent to level
	for (int i = 0; i < level; i++) {
	    printf(" ");
	}
	printf ("%s: ", item->name);
	switch (item->type) {
	  case PS_META_STR:
	    printf("%s", item->data.V);
	    break;
	  case PS_META_S32:
	    printf("%d", item->data.S32);
	    break;
	  case PS_META_F32:
	    printf("%f", item->data.F32);
	    break;
	  case PS_META_F64:
	    printf("%e", item->data.F64);
	    break;
	  case PS_META_MULTI:
	    printf("\n");
	    psMetadataPrint(item->data.V, level + 1);
	    break;
	  default:
	    printf("NON-PRINTABLE");
	}
	if (item->comment[0]) {
	    printf(" (%s)", item->comment);
	}
	printf("\n");
    }
    psFree(iter);
}    


int main(int argc, char *argv[])
{
    if (argc != 2) {
	printf("Usage: test_psMetadataParseConfig CONFIGFILE\n");
	exit(EXIT_FAILURE);
    }
    int badLines = 0;			// Number of bad lines in camera configuration
    psMetadata *simple = psMetadataParseConfig(NULL, &badLines, argv[1], true); // Camera config
    if (! simple) {
	psErrorStackPrint(stderr, "Unable to parse metadata config file.\n", badLines);
	return EXIT_FAILURE;
    }
    if (badLines > 0) {
	printf("%d bad lines in configuration file.\n", badLines);
    }
    psMetadataPrint(simple, 0);
    psFree(simple);

    return EXIT_SUCCESS;
}
