/*   -*- coding: utf-8 -*-  */
#include "Utf8Converter.h"

char *Utf8ConvertNameToWikiSort(Utf8Converter *cv1)
{
  Utf8AlphaCharConvert(cv1,'C');
  while(Utf8AlphaCharConvert(cv1,'s'));
  switch(cv1->returnCode)
    {
    case 'n':
      if (Utf8ConverterJumpToAlpha(cv1))
	{
	  BufferStrcat(cv1->targetBuf," ");
	  return Utf8ConvertNameToWikiSort(cv1);
	}
    default:
      break;
    }
  return BufferString(cv1->targetBuf);
}


char *Utf8ConvertNameToLower(Utf8Converter *cv1)
{
  unsigned char c1;
  Utf8AlphaCharConvert(cv1,'A');
  while(Utf8AlphaCharConvert(cv1,'a'))continue;
  switch(cv1->returnCode)
    {
    case 'n':
      c1=cv1->currentPosition[0];
      switch(c1)
	{
	case '-':
	  BufferCharCat(cv1->targetBuf,'-');
	  cv1->currentPosition++;
	  return Utf8ConvertNameToLower(cv1);
	case '.':
	  BufferCharCat(cv1->targetBuf,'.');
	  cv1->currentPosition++;
	  if (cv1->currentPosition[0]=='-')
	    {
	      BufferCharCat(cv1->targetBuf,'-');
	      cv1->currentPosition++;
	      return Utf8ConvertNameToLower(cv1);
	    }
	  else
	    {
	      if (Utf8ConverterJumpToAlpha(cv1))
		{
		  BufferStrcat(cv1->targetBuf," ");
		  return Utf8ConvertNameToLower(cv1);
		}
	      break;
	    }
	case '\'':
	  BufferCharCat(cv1->targetBuf,'\'');
	  cv1->currentPosition++;
	  return Utf8ConvertNameToLower(cv1);
	case 0xE2:
	  if ((cv1->currentPosition[1]=='\x80')
	      &&(cv1->currentPosition[2]=='\x90'))
	    {
	      BufferCharCat(cv1->targetBuf,'-');
	      cv1->currentPosition+=3;
	      return Utf8ConvertNameToLower(cv1);
	    }

	default:
	  if (Utf8ConverterJumpToAlpha(cv1))
	    {
	      BufferStrcat(cv1->targetBuf," ");
	      return Utf8ConvertNameToLower(cv1);
	    }
	}
    default:
      break;
    }
  return BufferString(cv1->targetBuf);
}

Utf8Converter *Utf8NameConverter=NULL;

char *Utf8NameToWikiSort(char *s1)
{
  if (!s1)return NULL;
  if(!Utf8NameConverter)Utf8NameConverter=Utf8ConverterCreate();
   Utf8ConverterSetString(Utf8NameConverter,s1);
   return Utf8ConvertNameToWikiSort(Utf8NameConverter);
}

char *Utf8NameToLower(char *s1)
{
  if(!Utf8NameConverter)Utf8NameConverter=Utf8ConverterCreate();
   Utf8ConverterSetString(Utf8NameConverter,s1);
   return Utf8ConvertNameToLower(Utf8NameConverter);
}

char *Utf8NameToInitial(char *s1)
{
  if(!Utf8NameConverter)Utf8NameConverter=Utf8ConverterCreate();
   Utf8ConverterSetString(Utf8NameConverter,s1);
   return  Utf8AlphaCharConvert(Utf8NameConverter,'A');
}
