/*   -*- coding: utf-8 -*-  */
/****************************************************************************
*
*      Module  : SxPath
*      Fichier : SxPathEasy.c
*      Auteur  : J. Ducloy
*
***************************************************************************/

#include "StrDict.h"
#include <stdio.h>
#include <string.h>
#include "SxPath.h"
#include "Except.h"
#include <stdlib.h>

StrDict *SxPathListCompiledFirst =NULL;

SxmlNode *SxPathEasyGetFirst(SxmlNode *n, char *pathStr)
{
  SxPathResult *compiledPath;
  if(!n)return NULL;
  if (SxPathListCompiledFirst) 
    {
      compiledPath=(SxPathResult *)StrDictSearch(SxPathListCompiledFirst, pathStr);
    }
  else 
    {
      SxPathListCompiledFirst=NewStrDict();
      compiledPath=NULL;
    }
  if (!compiledPath)
    {
      compiledPath=SxPathFirstCompile(pathStr);
      if (!compiledPath)return NULL;
      StrDictAddNewDatum(SxPathListCompiledFirst, strdup(pathStr), (char *)compiledPath);
    }
  return SxPathFirstResultNode(compiledPath, n);
}

char *SxPathEasyGetFirstText(SxmlNode *n, char *pathStr)
{
  SxmlNode *resNode;
  resNode=SxPathEasyGetFirst(n, pathStr);
  if (resNode) return SxmlGetFirstText(resNode);
  return NULL;
}
