#include <stdio.h>
#include "starpasr.h"
#include "datablk.h"
#include "pasrmain.h"
#include "iterator.h"

void starfilelistitemvalue(id item, char** name, int level, char* line){
  id list=[item getAllItemsNamed:name[0]], iter, current;
  char newline[150];
  level--;
  if (list){
    iter=[[WDCListIterator alloc] initFrom:list];
    while ([iter goNext]){
      current=[[iter getCurrentObject] getContent];
      sprintf(newline, "%s %s", line, [[current getValue] cStringValue]);
      if (!level) printf("%s\n", newline);
      else starfilelistitemvalue(current, &name[1], level, newline);
    }
    [list free];
    [iter free];
  }
  else
    printf("%s\n", line);
}


main(argc, argv)
int argc;
char** argv;
{
  id item;
  int numofcol, i;
  char *file, *dataname, *itemnames[10];
  starParser=[WDCStarParser alloc];

  if (argc>=4){
    file=*(argv+1);
    dataname=*(argv+2);
    numofcol=argc-3;
    for (i=0; i<numofcol; i++){
	itemnames[i]=*(argv+3+i);
    }
  }
  else {
    printf("usage: %s filename blockname itemname(s)\n", argv[0]);
    exit(1);
  }

  [starParser parseStarFile:file];
  if (item=[[starParser getStarFileNamed:file] getDataItem:dataname]){ 
    starfilelistitemvalue([item getValue], itemnames, numofcol, "");
  }
  else
    printf("Can not locate such information\n");
  return 0;
}

  


