/*
 * 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 BP_PARSER_H
#define BP_PARSER_H

#include <poliqarp-config.h>

#include <stdio.h>
#include <stdlib.h>

#include <libxml/parser.h>

#include "binary-pool.h"

typedef struct bp_parser_env {
   size_t num_errors;
} bp_parser_env;

void create_bp_parser_env(struct bp_parser_env *env);
void destroy_bp_parser_env(struct bp_parser_env *env);

typedef struct bp_parser {
   int num_errors;
   bool incremental;
   void (*destroy)(struct bp_parser *this);
   int (*preprocess)(struct bp_parser *this);
   int (*postprocess)(struct bp_parser *this);
   void (*stats)(struct bp_parser *this);
   void (*work)(struct bp_parser_env *env, struct bp_parser *this,
      const char *pathname, const char *target_dir, const char *target_file);
   void (*merge)(struct bp_parser_env *env, struct bp_parser *this,
      const char *pathname, const char *target_dir, const char *target_file);
} bp_parser;

#define PARTIAL_FILE_OK             0
#define PARTIAL_FILE_NEEDS_REMAKE   1
#define PARTIAL_FILE_GONE           2

int check_partial_file(const char *partial_file, const char *xml_file);
FILE *open_file_w_filter(const char *pathname, const char *mode, bool *is_pipe);
void bp_write_pool(struct bp_parser *base, struct binary_pool *pool,
   char *pool_name, char *base_name);
void bp_write_pool2(struct bp_parser *base, struct binary_pool *pool,
   char *pool_name, char *base_name);
int bp_read_pool(struct bp_parser *base, struct binary_pool *pool,
   char *pool_name, char *base_name);

/** 
 * Opens a file (possibly with filter) with the given filename; then 
 * reads data from this stream and feeds them to the given XML parser
 * until the end of file. Logs progress and records the time of parsing
 * if it's sufficiently long.
 * @param dir the name of the directory that the file is in; it is given
 *    for logging purposes, since a chdir() to this directory must have
 *    been invoked before.
 * @return 0 on success, -1 on failure
 */
int generic_parse_loop(const char *dir, const char *filename, 
   xmlParserCtxtPtr xml_parser);

#define CORPUS_MARKER "poliqarp"

extern xmlSAXHandler bp_dummy_sax_handler;

#endif /* BP_PARSER_H */
