GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
driver_state.c
Go to the documentation of this file.
1 
15 #include <stdlib.h>
16 #include <grass/dbmi.h>
17 #include "dbstubs.h"
18 
19 
20 static dbDriverState state;
21 
26 {
27  db_zero((void *)&state, sizeof(state));
28 }
29 
35 dbDriverState *db__get_driver_state(void)
36 {
37  return &state;
38 }
39 
47 {
48  return state.open ? 1 : 0;
49 }
50 
57 void db__mark_database_open(const char *dbname, const char *dbschema)
58 {
59  state.dbname = db_store(dbname);
60  state.dbschema = db_store(dbschema);
61  state.open = 1;
62 }
63 
68 {
69  db_free(state.dbname);
70  db_free(state.dbschema);
71  state.open = 0;
72 }
73 
79 void db__add_cursor_to_driver_state(dbCursor * cursor)
80 {
81  dbCursor **list;
82  int i;
83 
84  /* find an empty slot in the cursor list */
85  list = state.cursor_list;
86  for (i = 0; i < state.ncursors; i++)
87  if (list[i] == NULL)
88  break;
89 
90  /* if not found, extend list */
91  if (i >= state.ncursors) {
92  list =
93  (dbCursor **) db_realloc((void *)list,
94  (i + 1) * sizeof(dbCursor *));
95  if (list == NULL)
96  return;
97  state.cursor_list = list;
98  state.ncursors = i + 1;
99  }
100 
101  /* add it in */
102  list[i] = cursor;
103 }
104 
110 void db__drop_cursor_from_driver_state(dbCursor * cursor)
111 {
112  int i;
113 
114  for (i = 0; i < state.ncursors; i++)
115  if (state.cursor_list[i] == cursor)
116  state.cursor_list[i] = NULL;
117 }
118 
123 {
124  int i;
125 
126  for (i = 0; i < state.ncursors; i++)
127  if (state.cursor_list[i])
128  db_driver_close_cursor(state.cursor_list[i]);
129 
130  if (state.cursor_list)
131  db_free(state.cursor_list);
132 
133  state.ncursors = 0;
134  state.cursor_list = NULL;
135 }
void db__add_cursor_to_driver_state(dbCursor *cursor)
Add cursor do driver state.
Definition: driver_state.c:79
int(* db_driver_close_cursor)()
void db__close_all_cursors(void)
Close all cursors.
Definition: driver_state.c:122
void * db_realloc(void *s, int n)
void db__mark_database_closed(void)
Mark database as closed.
Definition: driver_state.c:67
return NULL
Definition: dbfopen.c:1394
void db__mark_database_open(const char *dbname, const char *dbschema)
Mark database as opened.
Definition: driver_state.c:57
int db__test_database_open(void)
Test database connection.
Definition: driver_state.c:46
void db__drop_cursor_from_driver_state(dbCursor *cursor)
Drop cursor from driver state.
Definition: driver_state.c:110
void db_zero(void *s, int n)
char * db_store(const char *s)
void db__init_driver_state(void)
Initialize driver state.
Definition: driver_state.c:25
void db_free(void *s)
dbDriverState * db__get_driver_state(void)
Get driver state.
Definition: driver_state.c:35