/*
 * freescope - Free source browser
 * Copyright (C) 2001  Olivier Deme
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

/*
 * FILE:        dbinf.h
 *
 * DESCRIPTION: This file implements the class DbInf that manages the database
 */

#ifndef _DBINF_H_
#define _DBINF_H_

/************/
/* INCLUDES */
/************/

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

#include <vector>
#include <fstream>
#include <slist>
#include <list>
#include <hash_map>
#include <string>
#include <time.h>

#include "arch.h"
#include "defs.h"
#include "parser.h"

/*********************/
/* NAMESPACE SYMBOLS */
/*********************/

using std::fstream;
using std::slist;
using std::hash_map;
using std::string;


/********************/
/* TYPE DEFINITIONS */
/********************/

typedef struct
{
    string        file;
    line_nbr_t    lineno;
    string        scope;
} match_t;

typedef struct
{
    string file;
    time_t timestamp;
} file_time_t;

typedef Uint32 file_ptr_t;


/*********************/
/* CLASS DEFINITIONS */
/*********************/

template <>
class hash<string>
{
public:
    size_t operator()(string const &str) const
    {
        hash<char const *> h;
        return (h(str.c_str()));
    }
};  

class DbInf
{
    // TYPE DEFINITIONS
private:
    typedef slist<file_ptr_t> pos_t;
    typedef unsigned int     symbol_code_t;
    typedef unsigned short   file_code_t;
    typedef hash_map<symbol_code_t,
                     string,
                     hash<symbol_code_t> >   cont_symcode_string_t;
    typedef hash_map<string,
                     symbol_code_t,
                     hash<string> >          cont_string_symcode_t;
    typedef hash_map<string,
                     time_t,
                     hash<string> >          cont_string_time_t;
    typedef list<string>                     completions_t;

    // CONSTANTS
    static const size_t        BUFFER_SIZE;
    static const size_t        RECORD_LENGTH;
    static const symbol_code_t SYMBOL_NEW_FILE;
    static const symbol_code_t SYMBOL_FREE_RECORD;

    // CONSTRUCTORS
private:
             DbInf ();

public:
    virtual ~DbInf ();

    // METHODS
public:
    static DbInf* instance           ();
    static int    add_entry          (const symbol_type_t type,
                                      const char* symbol,
                                      const size_t symbol_length,
                                      const char* scope,
                                      const size_t scope_length,
                                      const line_nbr_t lineno);
           void   open               (const string& filename);
           void   flush              ();
           void   find               (const string& symbol,
                                      const symbol_type_t type,
                                      list<match_t>& result);
           time_t get_timestamp      (const string& file);
           void   add_timestamp      (const string& file,
                                      const time_t timestamp);
           int    load_symbols       ();
           void   set_mod_file       (bool val) { m_do_existing_file = val; };
           void   set_file           (const string& file);
           string next_completion    (const string& stub, int& index,
                                      int& total);
           string prev_completion    (const string& stub, int& index,
                                      int& total);
           void   invalidate_records (const list<string>& files);
           bool   is_file_in_db      (string file);
           void dump                 ();
    
protected:
    int  load_files       ();
    void find_completions (const string& stub);

private:
    void   update_records     ();
    void   find_free_place    ();

    // ATTRIBUTES
private:
    static DbInf*                       m_instance;
           fstream                      m_db_file;
           file_ptr_t                   m_st_lower;
           file_ptr_t                   m_st_upper;
           file_ptr_t                   m_ft_lower;
           file_ptr_t                   m_ft_upper;
           bool                         m_db_modified;
           bool                         m_do_existing_file;
           string                       m_file;
           string                       m_old_file;
           string                       m_stub;
           completions_t                m_completions;
           int                          m_completion_index;
           list<string>::iterator       m_it_completion;
           cont_symcode_string_t        m_code_symbol;
           cont_string_symcode_t        m_symbol_code;
           cont_string_time_t           m_time_stamps;
           list<string>                 m_files;
           list<string>                 m_files_copy;
           vector<char>                 m_buffer;
};

#endif // _DBINF_H_


syntax highlighted by Code2HTML, v. 0.9.1