/*
 * This file is part of the Poliqarp suite.
 * 
 * Copyright (C) 2004-2009 by Instytut Podstaw Informatyki Polskiej
 * Akademii Nauk (IPI PAN; Institute of Computer Science, Polish
 * Academy of Sciences; cf. www.ipipan.waw.pl).  All rights reserved.
 * 
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file gpl.txt included in the packaging
 * of this file.  (See http://www.gnu.org/licenses/translations.html for
 * unofficial translations.)
 * 
 * A commercial license is available from IPI PAN (contact
 * Michal.Ciesiolka@ipipan.waw.pl or ipi@ipipan.waw.pl for more
 * information).  Licensees holding a valid commercial license from IPI
 * PAN may use this file in accordance with that license.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

#ifndef HAVE_HEADER_PARSER_H
#define HAVE_HEADER_PARSER_H

#include <poliqarp-config.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

#include <assert.h>
#include <time.h>

#include <libxml/parser.h>

#include <foostring/foostring.h>
#include <sakura/common/memory-arena.h>
#include "viewport.h"
#include "binary-pool.h"
#include "text-stack.h"
#include "utils.h"

#include <sakura/abi.h>
#include "bp-parser.h"

#include "lisp/lisp.h"
#include "lisp/lisp-file-stream.h"
#include <sakura/regexp.h>
#include "list.h"


lptr lw_meta_date(lisp_env * env, lptr name, lptr path);
lptr lw_meta_single(lisp_env * env, lptr name, lptr path);
lptr lw_meta_multi(lisp_env * env, lptr name, lptr rest);

/* --------------------------------------------------------------- */

typedef struct header_parser {
   xmlSAXHandler sax_handler;
   xmlParserCtxtPtr xml_parser;

   /* buffer for putting <tag>text between tags</tag> together */
   string_t text_buf;
   bool accumulate;
   /* buffer for storing tag/names/and:attributes */
   struct text_stack text_stack;

   /* arena */
   struct marena arena;

   /* dictionaries */
   struct binary_pool value_pool;
   struct binary_pool key_pool;

   struct list_item *meta_list;
   struct list_item *alias_list;

   char *base_name;

   /* currently processing */
   const char *filename;
   const char *dirname;

   size_t num_errors;

   FILE *meta_stream;
} header_parser;

bp_parser *header_parser_create(const char *base_name, bool incremental);

void header_parser_destroy(bp_parser * base);

int header_parser_pre_process(bp_parser * base);
int header_parser_post_process(bp_parser * base);

void header_parser_stats(bp_parser * base);

void
header_parser_work(bp_parser_env * env,
   bp_parser * base,
   const char *pathname, const char *target_dir, const char *target_file);

void
header_parser_merge(bp_parser_env * env,
   struct bp_parser *base,
   const char *pathname, const char *target_dir, const char *target_file);

#endif
