File: db.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (116 lines) | stat: -rw-r--r-- 2,579 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*****************************************************************************
*
* 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;
}