Modified:
trunk/c/include/toweldb.h
trunk/c/lib/database.c
trunk/c/lib/parser.c
trunk/c/lib/record.c
trunk/c/lib/record.h
trunk/c/tests/test_db.c
Log:
Encapsulate the database struct.
Modified: trunk/c/include/toweldb.h
==============================================================================
--- trunk/c/include/toweldb.h (original)
+++ trunk/c/include/toweldb.h Tue Jul 22 18:23:11 2008
@@ -62,45 +62,49 @@
} toweldb_field_node;
/* Database */
-typedef struct
-{
- DIR* db_dir;
- char* path;
- char mode;
- toweldb_err error;
- char max_key_len;
-} toweldb_db;
+/*! An opaque data structure that defines a database handle. */
+typedef struct _toweldb_db *toweldb_db;
/* Record */
-/*! An opaque data structure that defines a database record */
+/*! An opaque data structure that defines a database record. */
typedef struct _toweldb_rec *toweldb_rec;
/* Database stuff */
-/*! Open a database located at path. If mode is 'c', create it if
needed */
-toweldb_db* toweldb_open( const char* path, const char mode );
+ /*! Open a database located at path. If mode is 'c', create it if needed.*/
+toweldb_db toweldb_open( const char* path, const char mode );
+ /*! Get the path to the database root. */
+char* toweldb_get_path( toweldb_db db );
+ /*! Get the POSIX data handle on the database root directory. This
is a
+ * temperary hack and will be removed ASAP. */
+DIR** toweldb_get_db_dir( toweldb_db db );
+ /*! Get the maximum length of a filename for the filesystem that the
+ * database is on. */
+char toweldb_get_max_key_len( toweldb_db db );
/*! Delete the database. WARNING: BROKEN */
-toweldb_err toweldb_drop( toweldb_db* db );
+toweldb_err toweldb_drop( toweldb_db db );
/*! Close a database and free the memory storing it. */
-void toweldb_close( toweldb_db* db );
+void toweldb_close( toweldb_db db );
/* Record functions */
-/*! Get the number of records in the database. */
-unsigned int toweldb_get_num_recs( toweldb_db* db );
- /*! Get the modification time of a record */
+ /*! Get the number of records in the database. */
+unsigned int toweldb_get_num_recs( toweldb_db db );
+ /*! Get the modification time of a record. */
time_t toweldb_record_get_time( toweldb_rec rec );
+ /*! Get the database that owns a record. */
+toweldb_db* toweldb_get_record_parent( toweldb_rec rec );
/*! Get the next key in the database. This is a wrapper around the POSIX
* readdir that skips entries that the programmer doesn't need. It will
* return NULL and rewind if it hits the last item in the directory. */
-char* toweldb_get_next_key( toweldb_db* db );
+char* toweldb_get_next_key( toweldb_db db );
/*! Create a new record within the database with the given key. */
-toweldb_err toweldb_create_rec( toweldb_db* db, const char* key );
- /*! Remove the record with name key from the database */
-toweldb_err toweldb_remove_rec( toweldb_db* db, const char* key );
+toweldb_err toweldb_create_rec( toweldb_db db, const char* key );
+ /*! Remove the record with name key from the database. */
+toweldb_err toweldb_remove_rec( toweldb_db db, const char* key );
/* Record parsing functions */
-/*! Read the record specified by key */
-toweldb_rec toweldb_read_rec( toweldb_db* db, const char* key );
- /*! Free the record */
+/*! Read the record specified by key. */
+toweldb_rec toweldb_read_rec( toweldb_db db, const char* key );
+ /*! Free the record. */
void toweldb_free_rec( toweldb_rec rec );
#endif
Modified: trunk/c/lib/database.c
==============================================================================
--- trunk/c/lib/database.c (original)
+++ trunk/c/lib/database.c Tue Jul 22 18:23:11 2008
@@ -16,8 +16,9 @@
*/
#include "toweldb.h"
+#include "database.h"
-toweldb_db*
+toweldb_db
toweldb_open( const char* path, const char mode )
{
/* Information about the filesystem */
@@ -25,7 +26,7 @@
struct statvfs fs_info;
/* The actual database pointer */
- toweldb_db* db = NULL;
+ toweldb_db db = NULL;
/* The path string */
char* path_final = NULL;
@@ -89,8 +90,23 @@
return db;
}
+DIR** toweldb_get_db_dir( toweldb_db db )
+{
+ return &db->db_dir;
+}
+
+char* toweldb_get_path( toweldb_db db )
+{
+ return db->path;
+}
+
+char toweldb_get_max_key_len( toweldb_db db )
+{
+ return db->max_key_len;
+}
+
toweldb_err
-toweldb_drop( toweldb_db* db )
+toweldb_drop( toweldb_db db )
{
if( rmdir( db->path ))
{
@@ -103,7 +119,7 @@
}
void
-toweldb_close( toweldb_db* db )
+toweldb_close( toweldb_db db )
{
closedir( db->db_dir );
free( db->path );
Modified: trunk/c/lib/parser.c
==============================================================================
--- trunk/c/lib/parser.c (original)
+++ trunk/c/lib/parser.c Tue Jul 22 18:23:11 2008
@@ -27,7 +27,7 @@
#define TOWELDB_PHASE_FINISH 3
toweldb_rec
-toweldb_read_rec( toweldb_db* db, const char* key )
+toweldb_read_rec( toweldb_db db, const char* key )
{
char* path = NULL;
FILE* rec_file = NULL;
@@ -60,7 +60,7 @@
cur_node->next = NULL;
/* Open the record */
- path = toweldb_get_path( db, key );
+ path = toweldb_get_record_path( db, key );
if(( rec_file = fopen( path, "r" )) == NULL )
{
/* Something's wrong. Let's skip town, shall we? */
Modified: trunk/c/lib/record.c
==============================================================================
--- trunk/c/lib/record.c (original)
+++ trunk/c/lib/record.c Tue Jul 22 18:23:11 2008
@@ -18,29 +18,29 @@
#include "record.h"
#include "toweldb.h"
-bool toweldb_is_record_real( toweldb_db* db, const char* key );
+bool toweldb_is_record_real( toweldb_db db, const char* key );
/* Check to see if a key is worth returning as a record key or not */
char*
-toweldb_get_path( toweldb_db* db, const char* key )
+toweldb_get_record_path( toweldb_db db, const char* key )
{
char* path = NULL;
path = malloc( sizeof( char ) * \
- ( strlen( db->path ) + strlen( key ) + 1 ));
- strcpy( path, db->path );
+ ( strlen( toweldb_get_path( db )) + strlen( key ) + 1 ));
+ strcpy( path, toweldb_get_path( db ));
strcat( path, key );
return path;
}
bool
-toweldb_is_record_real( toweldb_db* db, const char* key )
+toweldb_is_record_real( toweldb_db db, const char* key )
{
struct stat file_info;
char* path = NULL;
- path = toweldb_get_path( db, key );
+ path = toweldb_get_record_path( db, key );
/* First let's try getting the file information. I can't imagine why it
* would fail, but if it does, we don't want it to be considered
valid */
@@ -67,14 +67,14 @@
}
unsigned int
-toweldb_get_num_recs( toweldb_db* db )
+toweldb_get_num_recs( toweldb_db db )
{
DIR* db_dir = NULL;
struct dirent* dir_entry = NULL;
unsigned int n_recs = 0;
/* Open the database directory stream */
- db_dir = opendir( db->path );
+ db_dir = opendir( toweldb_get_path( db ));
/* Now we just loop through the database keys until we hit NULL */
while(( dir_entry = readdir( db_dir )) != NULL )
@@ -91,7 +91,7 @@
}
char*
-toweldb_get_next_key( toweldb_db* db )
+toweldb_get_next_key( toweldb_db db )
{
struct dirent* dir_entry = NULL;
char found = 0;
@@ -99,13 +99,13 @@
while( !found )
{
/* Read the next file/directory on the stack */
- dir_entry = readdir( db->db_dir );
+ dir_entry = readdir( *toweldb_get_db_dir( db ));
/* First things first; is there anything there? If not, we need to
* rewind */
if( dir_entry == NULL )
{
- rewinddir( db->db_dir );
+ rewinddir( *toweldb_get_db_dir( db ) );
return NULL;
}
@@ -122,12 +122,12 @@
}
toweldb_err
-toweldb_create_rec( toweldb_db* db, const char* key )
+toweldb_create_rec( toweldb_db db, const char* key )
{
FILE* rec = NULL;
/* Let's first check if the key is too long */
- if( strlen( key ) > db->max_key_len )
+ if( strlen( key ) > toweldb_get_max_key_len( db ))
{
return toweldb_err_key_too_long;
}
@@ -145,7 +145,7 @@
}
toweldb_err
-toweldb_remove_rec( toweldb_db* db, const char* key )
+toweldb_remove_rec( toweldb_db db, const char* key )
{
if( remove( key ))
{
@@ -160,7 +160,7 @@
time_t toweldb_record_get_time( toweldb_rec rec )
{
struct stat rec_info;
- stat( toweldb_get_path( rec->parent, rec->key ), &rec_info );
+ stat( toweldb_get_record_path( rec->parent, rec->key ), &rec_info );
return rec_info.st_mtime;
}
Modified: trunk/c/lib/record.h
==============================================================================
--- trunk/c/lib/record.h (original)
+++ trunk/c/lib/record.h Tue Jul 22 18:23:11 2008
@@ -22,18 +22,14 @@
struct _toweldb_rec
{
- toweldb_db* parent;
+ toweldb_db parent;
char* key;
toweldb_field_node* contents_start;
};
/* Record functions */
-char* toweldb_get_path( toweldb_db* db, const char* key );
+char* toweldb_get_record_path( toweldb_db db, const char* key );
/* Get the path to the record specified jointly by the database and the
* key */
-
-/* Data structure accessor "methods" */
-toweldb_db* toweldb_record_get_parent( toweldb_rec rec );
- /* Get the parent database that a record is a member of */
#endif
Modified: trunk/c/tests/test_db.c
==============================================================================
--- trunk/c/tests/test_db.c (original)
+++ trunk/c/tests/test_db.c Tue Jul 22 18:23:11 2008
@@ -19,7 +19,7 @@
int main( int argc, char* argv[] )
{
- toweldb_db* db = NULL;
+ toweldb_db db = NULL;
db = toweldb_open( "./testdb", 'c' );
if( db == NULL )