/*****************************************************************************
*
* MODULE:       DBF driver 
*   	    	
* AUTHOR(S):    Radim Blazek
*
* PURPOSE:      Simple driver for reading and writing dbf files     
*
* COPYRIGHT:    (C) 2000 by the GRASS Development Team
*
*               This program is free software under the GNU General Public
*   	    	License (>=v2). Read the file COPYING that comes with GRASS
*   	    	for details.
*
*****************************************************************************/
#include <stdlib.h> 
#include <string.h>
#include <dirent.h>
#include <gis.h>
#include <dbmi.h>
#include "globals.h"
#include "proto.h" 

int
db__driver_open_database (handle)
    dbHandle *handle;
{
    char   *name;
    int    len; 
    dbConnection connection;
    char   buf[1024];
    DIR    *dir;
    struct dirent *ent;
    char **tokens;
    int no_tokens, n;

    db.name[0] = '\0';
    db.tables = NULL;
    db.atables = 0;
    db.ntables = 0;
    
    db_get_connection( &connection );
    name = db_get_handle_dbname(handle);
    
    /* if name is empty use connection.databaseName*/
    if( strlen(name) == 0 )
    {
        name = connection.databaseName;
    } 

    strcpy ( db.name, name );
    
    /* open database dir and read table ( *.dbf files ) names 
     * to structure */ 

    /* parse variables in db.name if present */
    if ( db.name[0] == '$' )
    {
     tokens = G_tokenize (db.name, "/");
     no_tokens=G_number_of_tokens(tokens);
     db.name[0] = '\0'; /* re-init */
     
     for (n = 0; n < no_tokens ; n++)
     {
       if ( tokens[n][0] == '$' )
       {
         G_strchg(tokens[n],'$', ' ' );
         G_chop(tokens[n]);
         strcat(db.name, G__getenv(tokens[n]) );
       }
       else
         strcat(db.name, tokens[n]);
         
       strcat(db.name, "/");
     }
     G_free_tokens(tokens);
    }

    dir = opendir(db.name);
    if (dir == NULL)
      {
	append_error ( "Cannot open dbf database: %s\n", name );
	report_error( );
	return DB_FAILED;
      }
    
    while ( ( ent = readdir (dir) ) )
      {         
	len = strlen ( ent->d_name ) - 4;      
	if ( (len > 0) && ( G_strcasecmp ( ent->d_name + len, ".dbf") == 0) )
          {
	    strcpy ( buf, ent->d_name );
	    buf[len] = '\0';
            add_table ( buf, ent->d_name );
	  }
      } 
    
    closedir ( dir );
    return DB_OK;
}

int
db__driver_close_database()
{
    int i;

    for ( i = 0; i < db.ntables; i++)
      {
	save_table (i);  
	free_table (i);  
      }
    free ( db.tables );
    
    return DB_OK;
}

