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

/***********************************************************************
*
*        Module   : Wicri
*        Fichier  : WicriPaysFromEn.c
*        Auteur   : J. DUCLOY
*        Date     : janvier 2013
*
************************************************************************/

#include "WicriPaysFromEn.h"
#include "WicriAmerique.h"
#include "Buffer.h"
#include "RegExp.h"
#include "Utf8Converter.h"
#include <ctype.h>
#include "StrDict.h"

StrDict        *WicriTableEn2Pays=NULL;
StrDict        *WicriTableEnLc2Pays=NULL;
StrDict        *WicriTableIsoC2ToPays=NULL;
StrDict        *WicriTableIsoC3ToPays=NULL;
StrDict        *WicriTableOldIsoC3ToPays=NULL;
StrDict        *WicriTableCitiesEn2Pays=NULL;
StrDict        *WicriTableBigCitiesFr=NULL;
StrDict        *WicriTableUsaState2Fr=NULL;
/* StrDict        *WicriTableUsaCodeState2Fr=NULL; */
StrDict        *WicriTableRegionsFr=NULL;
RegExp *WicriTableRegExpUsaZip=NULL;
/* extern RegExp *WicriRegExpZip; */

char *WicriPaysFromEnglish(char *enStr)
{
  if(!WicriTableEn2Pays)
    {
      char *DILIB;
      Buffer* bufTableName;
      DILIB=getenv("DILIB");
      bufTableName=NewBuffer();
      BufferStrcpy(bufTableName, DILIB);
      BufferStrcat(bufTableName, "/data/Wicri/Metadata/CountryToPays.tab"); 
      WicriTableEn2Pays=StrDictFromFile(BufferString(bufTableName));
    }
  if (!enStr)return NULL;
  return StrDictSearch(WicriTableEn2Pays, enStr);
}

char *WicriPaysFromEnglishLc(char *enStr)
{
  if(!WicriTableEnLc2Pays)
    {
      char *DILIB;
      Buffer* bufTableName;
      DILIB=getenv("DILIB");
      bufTableName=NewBuffer();
      BufferStrcpy(bufTableName, DILIB);
      BufferStrcat(bufTableName, "/data/Wicri/Metadata/CountryLcToPays.tab"); 
      WicriTableEnLc2Pays=StrDictFromFile(BufferString(bufTableName));
    }
  if (!enStr)return NULL;
  return StrDictSearch(WicriTableEnLc2Pays, enStr);
}

SxmlNode *WicriSetPaysFromEnglish(SxmlNode *affiliation, char *countryEnStr)  /* returns CountryNode if success */
{
  char *pays;
  SxmlNode *retNode;
  if ((pays=WicriPaysFromEnglish(countryEnStr)))
    {
      SxmlAppendChild(affiliation, retNode=SxmlLeafCreate("country", pays));
      SxmlSetAttribute(retNode, "xml:lang", "fr");
      return retNode;
    }
  return NULL;
}

SxmlNode *WicriSetPaysFromEnglishLc(SxmlNode *affiliation, char *countryEnStr)
{
  char *pays;
  SxmlNode *retNode;
  if ((pays=WicriPaysFromEnglishLc(countryEnStr)))
    {
      SxmlAppendChild(affiliation, retNode=SxmlLeafCreate("country", pays));
      SxmlSetAttribute(retNode, "xml:lang", "fr");
      SxmlSetAttribute(retNode, "wicri:curation", "lc");
      return retNode;
    }
  return NULL;
}


SxmlNode *WicriPaysFromCityByKey(char *enStr)
{
  char *placeStr;
  if(!WicriTableBigCitiesFr)
    {
      char *DILIB;
      Buffer* bufTableName;
      DILIB=getenv("DILIB");
      bufTableName=NewBuffer();
      BufferStrcpy(bufTableName, DILIB);
      BufferStrcat(bufTableName, "/data/Wicri/Metadata/CitiesToPays.tab");
      WicriTableBigCitiesFr=StrDictFromFile(BufferString(bufTableName));
    }
  if (!enStr)return NULL;
  placeStr= StrDictSearch(WicriTableBigCitiesFr, enStr);
  if (!placeStr)return NULL;
  return SxmlFromString(placeStr);
}


SxmlNode *WicriPaysFromRegionByKey(char *enStr)
{
  char *placeStr;
  if(!WicriTableBigCitiesFr)
    {
      char *DILIB;
      Buffer* bufTableName;
      DILIB=getenv("DILIB");
      bufTableName=NewBuffer();
      BufferStrcpy(bufTableName, DILIB);
      BufferStrcat(bufTableName, "/data/Wicri/Metadata/RegionsToPays.tab");
      WicriTableRegionsFr=StrDictFromFile(BufferString(bufTableName));
    }
  if (!enStr)return NULL;
  placeStr= StrDictSearch(WicriTableRegionsFr, enStr);
  if (!placeStr)return NULL;
  return SxmlFromString(placeStr);
}



SxmlNode *WicriSetPaysOrgFromUniv(SxmlNode *affiliation, char *countryEnStr)
{
  SxmlNode *orgNode;
  if ((orgNode=WicriUnivFrFromSubstr(countryEnStr)))
    {
      SxmlNode *country;
      SxmlNode *placeName;
      SxmlNode *orgName;
      country=SxmlGetFirstChildByTagName(orgNode,"country");
      if (!country) return NULL;
      SxmlAppendChild (affiliation, SxmlClone(country));
      placeName=SxmlGetFirstChildByTagName(orgNode,"placeName");
      if (placeName)SxmlAppendChild (affiliation, SxmlClone(placeName));
      orgName=SxmlGetFirstChildByTagName(orgNode,"orgName");
      while (orgName)
	{
	  SxmlAppendChild (affiliation, SxmlClone(orgName));
	  orgName=SxmlGetNextSiblingByTagName(orgName,"orgName");
	}
      return affiliation;
    }
  return NULL;
}

SxmlNode *WicriAffiliationSetPaysFromCity(SxmlNode *affiliation, char *countryEnStr)
{
  SxmlNode *placeNode;

  if ((placeNode=WicriPaysFromCityByKey(countryEnStr)))
    {
      SxmlNode *country;
      SxmlNode *placeName;
      country=SxmlGetFirstChildByTagName(placeNode,"country");
      if (!country) return NULL;
      SxmlAppendChild (affiliation, SxmlClone(country));
      placeName=SxmlGetFirstChildByTagName(placeNode,"placeName");
      if (placeName)SxmlAppendChild (affiliation, SxmlClone(placeName));
      return affiliation;
    }
  return NULL;
}

SxmlNode *WicriAffiliationSetPaysFromRegion(SxmlNode *affiliation, char *countryEnStr)
{
  SxmlNode *placeNode;

  if ((placeNode=WicriPaysFromRegionByKey(countryEnStr)))
    {
      SxmlNode *country;
      SxmlNode *placeName;
      country=SxmlGetFirstChildByTagName(placeNode,"country");
      if (!country) return NULL;
      SxmlAppendChild (affiliation, SxmlClone(country));
      placeName=SxmlGetFirstChildByTagName(placeNode,"placeName");
      if (placeName)SxmlAppendChild (affiliation, SxmlClone(placeName));
      return affiliation;
    }
  return NULL;
}

SxmlNode *WicriAffiliationSetPaysCityFromUniv(SxmlNode *affiliation, char *countryEnStr)
{
  SxmlNode *placeNode;

  if ((placeNode=WicriPaysFromCityByKey(countryEnStr)))
    {
      SxmlNode *country;
      SxmlNode *placeName;
      country=SxmlGetFirstChildByTagName(placeNode,"country");
      if (!country) return NULL;
      SxmlAppendChild (affiliation, SxmlClone(country));
      placeName=SxmlGetFirstChildByTagName(placeNode,"placeName");
      if (placeName)SxmlAppendChild (affiliation, SxmlClone(placeName));
      return affiliation;
    }
  return NULL;
}

char *WicriPaysFromIsoC2(char *c2Str)
{
  if(!WicriTableIsoC2ToPays)
    {
      char *DILIB;
      Buffer* bufTableName;
      DILIB=getenv("DILIB");
      bufTableName=NewBuffer();
      BufferStrcpy(bufTableName, DILIB);
      BufferStrcat(bufTableName, "/data/Wicri/GeoTables//Pays/IsoC2ToPays.tab");
      WicriTableIsoC2ToPays=StrDictFromFile(BufferString(bufTableName));
    }
  if (!c2Str)return NULL;
  return StrDictSearch(WicriTableIsoC2ToPays, c2Str);
}

char *WicriPaysFromIsoC3(char *c3Str)
{
  if(!WicriTableIsoC3ToPays)
    {
      char *DILIB;
      Buffer* bufTableName;
      DILIB=getenv("DILIB");
      bufTableName=NewBuffer();
      BufferStrcpy(bufTableName, DILIB);
      BufferStrcat(bufTableName, "/data/Wicri/Metadata/IsoC3ToPays.tab");
      WicriTableIsoC3ToPays=StrDictFromFile(BufferString(bufTableName));
    }
  if (!c3Str)return NULL;
  return StrDictSearch(WicriTableIsoC3ToPays, c3Str);
}

char *WicriPaysFromOldIsoC3(char *c3Str)
{
  if(!WicriTableOldIsoC3ToPays)
    {
      char *DILIB;
      Buffer* bufTableName;
      DILIB=getenv("DILIB");
      bufTableName=NewBuffer();
      BufferStrcpy(bufTableName, DILIB);
      BufferStrcat(bufTableName, "/data/Wicri/Metadata/IsoOldC3ToPays.tab");
      WicriTableOldIsoC3ToPays=StrDictFromFile(BufferString(bufTableName));
    }
  if (!c3Str)return NULL;
  return StrDictSearch(WicriTableOldIsoC3ToPays, c3Str);
}

char *WicriUsaState2Fr(char *enStr)
{
  char *stateFr;
  char *beginCode;
  char *beginZip;
  if(!WicriTableUsaState2Fr)
    {
      char *DILIB;
      Buffer* bufTableName;
      DILIB=getenv("DILIB");
      bufTableName=NewBuffer();
      BufferStrcpy(bufTableName, DILIB);
      BufferStrcat(bufTableName, "/data/Wicri/GeoTables/USA/StatesToFr.tab");
      WicriTableUsaState2Fr=StrDictFromFile(BufferString(bufTableName));
    }
  if (!enStr)return NULL;
  stateFr=StrDictSearch(WicriTableUsaState2Fr, enStr);
  if (stateFr) return stateFr;
  /*
  if(!WicriTableUsaCodeState2Fr)
    {
      char *DILIB;
      Buffer* bufTableName;
      DILIB=getenv("DILIB");
      bufTableName=NewBuffer();
      BufferStrcpy(bufTableName, DILIB);
      BufferStrcat(bufTableName, "/data/Wicri/GeoTables/USA/CodeStatesToFr.tab");
      WicriTableUsaCodeState2Fr=StrDictFromFile(BufferString(bufTableName));
    }
  */
  if(!WicriTableRegExpUsaZip)
    {
      WicriTableRegExpUsaZip=RegExpCreate("[A-Z][A-Z] [0-9][0-9][0-9][0-9][0-9]");
    }
  /* if ((beginZip=RegExpFind(WicriRegExpZip, enStr))) */
  if ((beginZip=WicriFindZip5(enStr)))
    {
      if ((beginCode=RegExpFind(WicriTableRegExpUsaZip, enStr)))
	{
	  char *code;
	  code=malloc(3);
	  strncpy(code, beginCode, 2);
	  /* stateFr=StrDictSearch(WicriTableUsaCodeState2Fr, code); */
	  stateFr=WicriAmeriqueGetUsaStateNameFrFromCode(code);
	  free(code);
	  if (stateFr) return stateFr;
	}
      else
	{
	  static Buffer *bufState=NULL;
	  if (!bufState) bufState=NewBuffer();
	  if (beginZip>enStr)
	    {
	      BufferStrncpy(bufState, enStr, beginZip-1-enStr);
	      stateFr=StrDictSearch(WicriTableUsaState2Fr, BufferString(bufState));
	      if (stateFr) return stateFr;
	    }
	}
    }
  if (strlen(enStr)==2)
    {
      /* stateFr=StrDictSearch(WicriTableUsaCodeState2Fr, enStr); */
      stateFr=WicriAmeriqueGetUsaStateNameFrFromCode(enStr);
      if (stateFr) return stateFr;
    }
  return NULL;
}

char *WicriStrPaysFromEnglishCities(char *enStr)
{
  if(!WicriTableCitiesEn2Pays)
    {
      char *DILIB;
      Buffer* bufTableName;
      DILIB=getenv("DILIB");
      bufTableName=NewBuffer();
      BufferStrcpy(bufTableName, DILIB);
      BufferStrcat(bufTableName, "/data/Wicri/GeoTables//Pays/LargeCitiesFr.tab");
      WicriTableCitiesEn2Pays=StrDictFromFile(BufferString(bufTableName));
    }
  if (!enStr)return NULL;
  return StrDictSearch(WicriTableCitiesEn2Pays, enStr);
}

SxmlNode *WicriFindPaysInAffEn(char *aff)
{
  char *posComma;
  SxmlNode *countryNode;
  char *pays;
  if (!aff)return NULL;
  posComma=strrchr(aff, ',');
  if (!posComma)
    {
      countryNode=SxmlLeafCreate("wicri:noCountry", aff);
      return countryNode;
    }
  if ((pays=WicriPaysFromEnglish(posComma+2)))
    {
      countryNode=SxmlLeafCreate("country", pays);
      SxmlSetAttribute(countryNode, "xml:lang", "fr");
      return countryNode;
    }
  countryNode=SxmlLeafCreate("wicri:noCountry", posComma+2);
  return countryNode;
  
}

char *WicriJumpDigit(char *str)
{
  if (str[0]==' ')return str+1;
  if (isdigit(str[0]))return WicriJumpDigit(str+1);
  return str;
}

char *WicriFindPaysFromUrl(SxmlNode *affiliation, char *url)
{
  char *posAt;
  char *posSpace;
  char *posDot;
  posAt=strchr(url, '@');
  if (!posAt)return NULL;
  if ((posSpace=strchr(posAt, ' ')))
    {
      posDot=NULL;
    }
  else posDot=strrchr(url, '.');
  if (posDot)
    {
      char *country;
      static char *code=NULL;
      if (!code)code = malloc(3);
      code[2]='\0';

      if (strlen(posDot)==1)  /* dot en fin */
	{
	  if (strncmp(posDot-3,"edu",3)==0) return "États-Unis";
	  if (strncmp(posDot-2,"uk",2)==0) return "Royaume-Uni";
	  code[0]=toupper(posDot[-2]);
	  code[1]=toupper(posDot[-1]);
	}
      else
	{
	  if (strcmp(posDot+1,"edu")==0) return "États-Unis";
	  if (strcmp(posDot+1,"com")==0) return NULL;
	  if (strcmp(posDot+1,"uk")==0) return "Royaume-Uni";
	  code[0]=toupper(posDot[1]);
	  code[1]=toupper(posDot[2]);
	}
      country=WicriPaysFromIsoC2(code);
      return country;
    }
  return NULL;
}

SxmlNode *WicriAffiliationSetPaysFromUrl(SxmlNode *affiliation, char *url)
{
  SxmlNode *retNode;
  SxmlNode *nodeToDelete;
  char *country;
  country=WicriFindPaysFromUrl(affiliation, url);
  if (!country)return NULL;
  if ((nodeToDelete=SxmlGetFirstChildByTagName(affiliation, "wicri:noCountry"))) SxmlFree(nodeToDelete);
  SxmlAppendChild(affiliation, retNode=SxmlLeafCreate("country", country));
  SxmlSetAttribute (retNode, "wicri:rule","url");
  SxmlSetAttribute(affiliation,  "wicri:level", "1");
  return retNode;
}

SxmlNode *WicriAffiliationSetPaysFromLetterZip(SxmlNode* affiliation, char *subField)
{
  SxmlNode *retNode;
  if(!(isdigit(subField[2])))return NULL;
  if(!(isdigit(subField[3])))return NULL;
  if(!(isdigit(subField[4])))return NULL;
  if(!(isdigit(subField[4])))return NULL;
  switch(subField[0])
    {
    case 'B':
       SxmlAppendChild(affiliation, retNode=SxmlLeafCreate("country", "Belgique"));
       SxmlSetAttribute (retNode, "wicri:rule","zip");
       SxmlSetAttribute(affiliation, "wicri:level", "0");
       return retNode;
    case 'L':
       SxmlAppendChild(affiliation, retNode=SxmlLeafCreate("country", "Luxembourg (pays)"));
       SxmlSetAttribute (retNode, "wicri:rule","zip");
       SxmlSetAttribute(affiliation, "wicri:level", "0");
       return retNode;
    }
  if(!(isdigit(subField[5])))return NULL;
  switch(subField[0])
    {
    case 'D':
      SxmlAppendChild(affiliation, retNode=SxmlLeafCreate("country", "Allemagne"));
      SxmlSetAttribute (retNode, "wicri:rule","zip");
      SxmlSetAttribute(affiliation, "wicri:level", "0");
      return retNode;
    case 'F':
      SxmlAppendChild(affiliation, retNode=SxmlLeafCreate("country", "France"));
      SxmlSetAttribute (retNode, "wicri:rule","zip");
      SxmlSetAttribute(affiliation, "wicri:level", "0");
      return retNode;
    }
  return NULL;
}

SxmlNode *WicriAffiliationSetPaysFromSubFieldEn(SxmlNode *affiliation, char *subField)
{
  SxmlNode *retNode;
  char *stateUsa;
  static Buffer *lcBuf=NULL;
  char *lcStr;
  SxmlNode *countryNode;

  if (*subField=='\0')return NULL;
  WicriRegionInSubFieldCountry=NULL;
  if ((retNode=WicriSetPaysFromEnglish(affiliation, subField)))
    {
      SxmlSetAttribute(affiliation, "wicri:level", "1");
      return retNode;
    }
  if (!lcBuf) lcBuf=NewBuffer();
  lcStr=Utf8StringToSort(subField);
  BufferStrcpy(lcBuf, lcStr);
  if ((retNode=WicriSetPaysFromEnglishLc(affiliation, BufferString(lcBuf))))
    {
      SxmlSetAttribute(affiliation, "wicri:level", "1");
      return retNode;
    }
  if ((subField[1]=='-') && (retNode=WicriAffiliationSetPaysFromLetterZip(affiliation, subField)))
    {
      WicriRegionInSubFieldCountry=subField;
      return retNode;
    }
  if (isdigit(subField[0]))
    {
      char *posSpace;
      posSpace=strchr(subField, ' ');
      /*      if ((posSpace)&&(retNode=WicriSetPaysFromEnglish(affiliation, posSpace+1))) */
      if ((posSpace)&&(retNode=WicriAffiliationSetPaysFromSubFieldEn(affiliation, posSpace+1)))
	{
	  SxmlSetAttribute(affiliation, "wicri:level", "1");
	  return retNode;
	}
    }
  if (strstr(subField, "Univ"))
    {
      if ((retNode=WicriSetPaysOrgFromUniv(affiliation, subField)))
	{
	  SxmlSetAttribute(affiliation, "wicri:level", "4");
	  return retNode;
	}
    }
  if (strchr(subField,'@'))
    {
      if((retNode=WicriAffiliationSetPaysFromUrl(affiliation, subField)))
	{
	  SxmlSetAttribute(affiliation, "wicri:level", "1");
	  return retNode;
	}
    }
  if ((retNode=WicriAffiliationSetPaysFromRegion(affiliation, subField)))
    {
      SxmlSetAttribute(affiliation, "wicri:level", "2");
      return retNode;
    }
  if ((stateUsa=WicriUsaState2Fr(subField)))
    {
      SxmlNode *placeName;
      SxmlNode *region;
      SxmlAppendChild(affiliation, retNode=SxmlLeafCreate("country", "États-Unis"));
      SxmlSetAttribute(retNode, "xml:lang", "fr");
      SxmlAppendChild(affiliation, placeName=SxmlElementCreate("placeName"));
      SxmlAppendChild(placeName,region=SxmlLeafCreate("region", stateUsa));
      SxmlSetAttribute(region, "type", "state");
      SxmlSetAttribute(affiliation, "wicri:level", "2");
      return retNode;
    }
  if ((retNode=WicriAffiliationSetPaysFromCity(affiliation, subField)))
    {
      SxmlSetAttribute(affiliation, "wicri:level", "3");
      return retNode;
    }
  if ((retNode=WicriAffiliationSetPaysCityFromUniv(affiliation, subField)))
    {
      SxmlSetAttribute(affiliation, "wicri:level", "3");
      return retNode;
    }
  if ((retNode=WicriSetPaysOrgFromUniv(affiliation, subField)))
    {
      SxmlSetAttribute(affiliation, "wicri:level", "4");
      return retNode;
    }
  /*               cas particulier               */
  if (strncmp(subField, "UK ",3)==0) 
    {
      SxmlAppendChild(affiliation, countryNode=SxmlLeafCreate("country", "Royaume-Uni"));
      SxmlSetAttribute(affiliation, "wicri:level", "1");
      return countryNode;
    }
  if (strncmp(subField, "Canada  ",7)==0) 
    {
      SxmlAppendChild(affiliation, countryNode=SxmlLeafCreate("country", "Canada"));
      SxmlSetAttribute(affiliation, "wicri:level", "1");
      return countryNode;
    }
  return NULL;
}

SxmlNode *WicriAffiliationSetPaysFromFieldEn(SxmlNode* affiliation, char *field)
{
  char *posComma;
  SxmlNode *countryNode;
  char *beginSubField;
  char *beginAff;
  SxmlNode *retNode;
  static Buffer *bufSubField=NULL;
  posComma=strrchr(field, ',');
  beginAff=field;
  if (!posComma)
    {
      if ((countryNode=WicriAffiliationSetPaysFromSubFieldEn(affiliation,  field))) return countryNode;
      SxmlAppendChild(affiliation, WicriGenerNoCountry(beginAff,"no comma"));
      return NULL;
    }
  beginSubField=posComma+1;
  if (!bufSubField) bufSubField=NewBuffer();
  while (beginSubField[0]==' ') beginSubField++;
  if ( (beginSubField[0]=='(')&& (beginSubField[strlen(beginSubField)-1]==')'))
    {
      BufferStrcpy(bufSubField, beginSubField+1);
      BufferTailCut(bufSubField,1);
    }
  else  BufferStrcpy(bufSubField, beginSubField);
  if ((countryNode=WicriAffiliationSetPaysFromSubFieldEn(affiliation,  BufferString(bufSubField))))
    {
      char *level;
      static Buffer *bufArea=NULL;
      level=SxmlGetAttribute(affiliation, "wicri:level");
      if(!bufArea)bufArea=NewBuffer();
      BufferStrncpy(bufArea, beginAff, posComma-beginAff);
      switch(level[0])
	{
	case '1':
	  SxmlAppendChild(affiliation, SxmlLeafCreate("wicri:regionArea", BufferString(bufArea)));
	  break;
	case '2':
	  SxmlAppendChild(affiliation, SxmlLeafCreate("wicri:cityArea", BufferString(bufArea)));
	  break;
	case '3':
	  SxmlAppendChild(affiliation, SxmlLeafCreate("wicri:orgArea", BufferString(bufArea)));
	  break;
	default:
	  break;
	}
      return countryNode;
    }
  if ((retNode=WicriAffiliationSetUnivFromField(affiliation, field)))
    {
      countryNode=SxmlGetFirstChildByTagName(affiliation, "country");
      return countryNode;
    }
  return NULL;
}

/*                                          deprecated -> WicriAffiliationSetPaysFromFieldEn */
SxmlNode *WicriSetPaysInAffEn(char *aff, SxmlNode *affiliation)
{
  static Buffer *bufAff=NULL;
  char *posComma;
  char *posLastChar;
  char *cityText;
  SxmlNode *retNode;
  char *pays;
  char *stateUsa;
  char *posComma2;  /*  for sth like Univ. of Mass., Boston */

  if (!bufAff)bufAff=NewBuffer();

  BufferStrcpy(bufAff,aff);
  posComma=strrchr(BufferString(bufAff), ',');
  if (!posComma) return NULL;

  if ((retNode=WicriSetPaysFromEnglish(affiliation, posComma+2)))
    {
      char *posEndRegionArea;
      posEndRegionArea=posComma;
      posEndRegionArea[0]='\0';
      SxmlAppendChild(affiliation, SxmlLeafCreate("wicri:regionArea", strdup (BufferString(bufAff))));
      return retNode;
    }
  if (strstr(posComma+2, "Univ"))
    {
      if ((retNode=WicriSetPaysOrgFromUniv(affiliation, posComma+2))) return retNode;
    }

  if ((stateUsa=WicriUsaState2Fr(posComma+2)))
    {
      SxmlNode *placeName;
      SxmlNode *region;
      SxmlAppendChild(affiliation, retNode=SxmlLeafCreate("country", "États-Unis"));
      SxmlSetAttribute(retNode, "xml:lang", "fr");
      SxmlAppendChild(affiliation, placeName=SxmlElementCreate("placename"));
      SxmlAppendChild(placeName,region=SxmlLeafCreate("region", stateUsa));
      SxmlSetAttribute(region, "type", "state");
      SxmlAppendChild(affiliation, SxmlLeafCreateFromSubstr("wicri:cityArea", BufferString(bufAff), posComma-BufferString(bufAff)));
      return retNode;
    }

  posLastChar=BufferString(bufAff)+BufferLen(bufAff)-1;
  if (posLastChar[0]=='.')
    {
      char *toDel;
      posLastChar[0]='\0';
      retNode=WicriSetPaysInAffEn(toDel=strdup(BufferString(bufAff)), affiliation);
      free(toDel);
      return retNode;
    }

  if (isdigit(*(posComma+2)))
    {
      char *beginField;
      beginField=WicriJumpDigit(posComma+3);
      if ((pays=WicriPaysFromEnglish(beginField)))
	{
	  char *posEndRegionArea;

	  SxmlAppendChild(affiliation, retNode=SxmlLeafCreate("country", pays));
	  SxmlSetAttribute(retNode, "xml:lang", "fr");
	  posEndRegionArea=beginField-1;
	  posEndRegionArea[0]='\0';
	  SxmlAppendChild(affiliation, SxmlLeafCreate("wicri:regionArea", strdup (BufferString(bufAff))));
	  return retNode;
	}
    }

  if ((cityText=WicriStrPaysFromEnglishCities(posComma+2)))
    {
      SxmlNode *cityList;
      SxmlNode *countryNode;
      SxmlNode *placeName;
      SxmlNode *nextNode;
      SxmlNode *regName;

      cityList=SxmlFromString(cityText);
      countryNode=SxmlFirstChild(cityList);
      pays=SxmlLeafText(countryNode);
      SxmlAppendChild(affiliation,retNode=SxmlLeafCreate("country", pays));
      SxmlAppendChild(affiliation, placeName=SxmlElementCreate("placeName"));
      nextNode=SxmlNextSibling(countryNode);
      if (SxmlLength(cityList)>3)
	{
	  SxmlAppendChild(placeName, regName=SxmlLeafCreate("region",SxmlLeafText(nextNode)));
	  SxmlSetAttribute(regName,"type","state");
	  nextNode=SxmlNextSibling(nextNode);
	}
      while (nextNode)
	{
	  char *regOrCity;
	  regOrCity=SxmlLeafText(nextNode);
	  if ((nextNode=SxmlNextSibling(nextNode)))
	    {
	      SxmlAppendChild(placeName, regName=SxmlLeafCreate("region",regOrCity));
	      SxmlSetAttribute(regName,"type","region");
	    }
	  else
	    {
	      SxmlAppendChild(placeName, regName=SxmlLeafCreate("settlement",regOrCity));
	      SxmlSetAttribute(regName,"type","town");
	    }
	  
	}
      return retNode;
    }
  posComma2=strrchr(posComma-1, ',');
  if(posComma2)
    {
      if (strstr(posComma2+2, "Univ"))
	{
	  if ((retNode=WicriSetPaysOrgFromUniv(affiliation, posComma2+2))) return retNode;
	}
    }
  
  /* SxmlAppendChild(affiliation, retNode=WicriGenerNoCountry(posComma+2, "unknown")); */
      /* SxmlAppendChild(affiliation, retNode=SxmlLeafCreate("wicri:noCountry", posComma+2)); */
  return NULL;
}
