/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2023 Martin Preuss, all rights reserved.
 *
 * The license for this file can be found in the file COPYING which you
 * should have received along with this file.
 ****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

//#define DISABLE_DEBUGLOG


#include "aqhome/http/content_p.h"

#include <gwenhywfar/debug.h>




GWEN_INHERIT_FUNCTIONS(AQH_HTTP_CONTENT)
GWEN_TREE2_FUNCTIONS(AQH_HTTP_CONTENT, AQH_HttpContent)





AQH_HTTP_CONTENT *AQH_HttpContent_new(const char *name)
{
  AQH_HTTP_CONTENT *cp;

  GWEN_NEW_OBJECT(AQH_HTTP_CONTENT, cp);
  GWEN_INHERIT_INIT(AQH_HTTP_CONTENT, cp);
  GWEN_TREE2_INIT(AQH_HTTP_CONTENT, cp, AQH_HttpContent);

  cp->name=name?strdup(name):NULL;

  return cp;
}



void AQH_HttpContent_free(AQH_HTTP_CONTENT *cp)
{
  if (cp) {
    GWEN_TREE2_FINI(AQH_HTTP_CONTENT, cp, AQH_HttpContent);
    GWEN_INHERIT_FINI(AQH_HTTP_CONTENT, cp);

    free(cp->name);

    GWEN_FREE_OBJECT(cp);
  }
}



const char *AQH_HttpContent_GetName(const AQH_HTTP_CONTENT *cp)
{
  return cp?cp->name:NULL;
}



int AQH_HttpContent_AddOpeningContent(AQH_HTTP_CONTENT *cp, int mode, GWEN_BUFFER *buffer)
{
  return (cp && cp->addOpeningContentFn)?(cp->addOpeningContentFn(cp, mode, buffer)):0;
}



int AQH_HttpContent_AddClosingContent(AQH_HTTP_CONTENT *cp, int mode, GWEN_BUFFER *buffer)
{
  return (cp && cp->addClosingContentFn)?(cp->addClosingContentFn(cp, mode, buffer)):0;
}



void AQH_HttpContent_SetAddOpeningContentFn(AQH_HTTP_CONTENT *cp, AQH_HTTP_CONTENT_ADD_OPENING_CONTENT_FN f)
{
  if (cp)
    cp->addOpeningContentFn=f;
}



void AQH_HttpContent_SetAddClosingContentFn(AQH_HTTP_CONTENT *cp, AQH_HTTP_CONTENT_ADD_CLOSING_CONTENT_FN f)
{
  if (cp)
    cp->addClosingContentFn=f;
}



AQH_HTTP_CONTENT *AQH_HttpContent_Tree2_FindChildByName(AQH_HTTP_CONTENT *parent, const char *name)
{
  if (parent) {
    AQH_HTTP_CONTENT *c;

    c=AQH_HttpContent_Tree2_GetFirstChild(parent);
    while(c) {
      const char *s;

      s=AQH_HttpContent_GetName(c);
      if (s && *s && strcasecmp(s, name)==0)
        return c;
      c=AQH_HttpContent_Tree2_GetNext(c);
    }
  }

  return NULL;
}




