/*   -*- coding: utf-8 -*-  */

/*

This program takes data that come from Semantic MediaWiki in CSV format and 
convert them in an Xml based format.
 */

#include <stdio.h>     /* for printf */
#include <stdlib.h>    /* for exit */
#include "Buffer.h"
#include "SxmlNode.h"

extern char *optarg;
extern int   optind;
int getopt();
Buffer *bufSubstr;

SxmlNode *appendNewCol(SxmlNode *lineXml, char *lineStr)
{
  char *posSep;
  if(lineStr[0]==',')
    {
      SxmlAppendChild(lineXml, SxmlElementCreate("c"));
      return (appendNewCol(lineXml,lineStr+1));
    }
  if(lineStr[0]=='\"') 
    {
      char *posDquot;
      posDquot=strchr(lineStr+1,'\"');
      if (posDquot)
	{
	  BufferStrncpy(bufSubstr, lineStr+1, posDquot-lineStr-1 );
	  SxmlAppendChild
	    (lineXml, 
	     SxmlLeafCreate("c", BufferString(bufSubstr)));
	  if (posDquot[1]==',')
	    return appendNewCol(lineXml, posDquot+2);
	  return  lineXml;
	}
    }
  if(lineStr[0]=='\0')  return lineXml;
  posSep=strchr(lineStr,',');
  if (!posSep)
    {
      SxmlAppendChild (lineXml,SxmlLeafCreate("c", lineStr));
      return  lineXml;
    }
  BufferStrncpy(bufSubstr, lineStr, posSep-lineStr );
  SxmlAppendChild
    (lineXml, 
     SxmlLeafCreate("c", BufferString(bufSubstr)));
  return appendNewCol(lineXml, posSep+1);
}

SxmlNode *csvRowToSxml(char *lineStr)
{
  SxmlNode *lineXml;
  lineXml=SxmlElementCreate("row");
  if(lineStr[0]==',')
    {
      SxmlAppendChild(lineXml, SxmlElementCreate("c"));
      return (appendNewCol(lineXml,lineStr+1));
    }
  if(lineStr[0]=='\"') return (appendNewCol(lineXml,lineStr));
  return lineXml;
}

int main (int argc, char **argv) {
   int cOption;
   Buffer *bufInput;
   SxmlNode *firstLine;
   char *listProperty;
   SxmlNode *colTitle;
   SxmlNode *rowXml;
   SxmlNode *colXml;
   SxmlNode *nextColXml;
   int headerEcho;

   bufInput=NewBuffer();
   bufSubstr=NewBuffer();
   BufferGets(bufInput);
   headerEcho=0;
   firstLine=csvRowToSxml(BufferString(bufInput));
   if (!(SxmlIsLeaf(SxmlFirstChild(firstLine))))
     {
       SxmlAppendChild(SxmlFirstChild(firstLine), SxmlTextCreate("Page"));
     }

   while((cOption=getopt(argc,argv,"hl:"))!=EOF)
     {switch (cOption)
	 {
	 case 'h':
	   headerEcho=1;
	   break;
	 case 'l':
	   listProperty=optarg;
	   SxmlReset(firstLine);
	   while ((colTitle=SxmlNextNode(firstLine)))	 
	     {
	       if (strcmp(SxmlLeafText(colTitle),listProperty)==0)
		 {
		   SxmlSetAttribute(colTitle,"type","list");
		 }
	     }
	   break;
	 }
     }

   if (headerEcho) {SxmlPrint(firstLine);putchar('\n');}
   while (BufferGets(bufInput))
     { 
       rowXml=csvRowToSxml(BufferString(bufInput));
       if (!rowXml) continue;
       SxmlReset(firstLine);
       colXml=SxmlFirstChild(rowXml);
       while ((colTitle=SxmlNextNode(firstLine))&&(colXml))	 
	 {
	   nextColXml=SxmlNextSibling(colXml);
	   if (SxmlIsLeaf(colXml))
	     {
	       SxmlSetAttribute(colXml, "property", SxmlLeafText(colTitle));
	       if (SxmlHasAttribute(colTitle,"type","list"))
		 {
		   char *posComma;
		   char *strItem;
		   strItem=SxmlLeafText(colXml);
		   while ((posComma=strchr(strItem,',')))
		     {
		       BufferStrncpy(bufSubstr,strItem, posComma-strItem);
		       strItem=posComma+1;
		       SxmlAppendChild(colXml, SxmlLeafCreate("i",BufferString(bufSubstr)));
		     }
		   SxmlAppendChild(colXml, SxmlLeafCreate("i",strItem));
		   SxmlFree(SxmlFirstChild(colXml));
		 }
	     }
	   else
	     {
	       SxmlRemoveChild(colXml);
	     }
	   colXml=nextColXml;
	 }
       SxmlPrint(rowXml);
       SxmlFree(rowXml);
       putchar ('\n');
     }
   exit(EXIT_SUCCESS);
}
