Index: vtcross/trunk/src/include/vtcross/cbr.h
===================================================================
--- vtcross/trunk/src/include/vtcross/cbr.h	(revision 471)
+++ vtcross/trunk/src/include/vtcross/cbr.h	(revision 472)
@@ -70,7 +70,7 @@
          * don't fail (i.e. we cannot rely on the compiler-provided constructor. */
         CBR(){};
-        CBR(char *_filename, char *_tablename, char *_cols[], uint32_t _len);
-        CBR(char *_filename, char *_tablename, char *_cols[], \
-                char *_primcols[], uint32_t _len, uint32_t _primlen);
+        CBR(string _filename, string _tablename, string _cols[], uint32_t _len);
+        CBR(string _filename, string _tablename, string _cols[], \
+                string _primcols[], uint32_t _len, uint32_t _primlen);
 
         /* Destructor for the CBR class. Note that this destructor will be
@@ -95,21 +95,22 @@
         /* Search the VTCROSS database for specific fields and store the results
          * in the passed retvals argument. */
-        virtual int32_t Search(char *_names[], int32_t *_ops, float *_vals, \
+        virtual int32_t Search(string _names[], int32_t *_ops, float *_vals, \
                 uint32_t _n, float *_retvals);
-        virtual int32_t SearchSum(char *_name, float *_retvals);
-        virtual int32_t SearchRand(char *_names[], int *_ops, float *_vals, uint32_t _n, \
+        virtual int32_t SearchSum(string _name, float *_retvals);
+        virtual int32_t SearchRand(string _names[], int32_t *_ops, float *_vals, uint32_t _n, \
             float *_retvals);
 
         /* Update a row in the VTCROSS sqlite3 database. */
-        virtual int32_t Update(char *_where[], char *_set[], float *_wherevals, \
+        virtual int32_t Update(string _where[], string _set[], float *_wherevals, \
                 float *_setvals, uint32_t _wherelen, uint32_t _setlen);
 
         /* Add a row to the VTCROSS sqlite3 database. */
-        virtual int32_t AddRow(char *_cols[], float *_vals, uint32_t _len);
+        virtual int32_t AddRow(string _cols[], float *_vals, uint32_t _len);
 
     protected:
-        char filename[64];
-        char tablename[64];
-        char command[2048];
+        string filename;
+        string tablename;
+        string command;
+
         sqlite3 *db; 
         uint32_t numColumns;
@@ -117,108 +118,87 @@
 
 
-CBR::CBR(char *_filename, char *_tablename, char *_cols[], uint32_t _len)
-{
-    // copy filename
-    unsigned int i=0;
-    strcpy(filename, _filename);
-
-    // execute create database command
-    // database handle
-    //db = NULL;
+CBR::CBR(string _filename, string _tablename, string _cols[], uint32_t _len)
+{
+    /* Store database properties. */
+    filename = _filename;
+    tablename = _tablename;
+    numColumns = _len;
+
+    /* Create the database (or open it if it already exists). */
     OpenDatabase();
 
-    // create table
-
-    // copy tablename
-    strcpy(tablename, _tablename);
-
-    // number of columns in the table
+    /* Generate the command that will create the initial table within the
+     * VTCROSS database. */
+    command = "CREATE TABLE " + tablename + "(";
+    for(size_t i = 0; i < numColumns; i++) {
+        command += _cols[i];
+        command += " FLOAT";
+
+        /* If this column is not the last entry, add a comma to the command in
+         * preperation for the next entry. */
+        if(i != numColumns - 1)
+            command += ", ";
+    }
+    command += ");";
+
+    /* Execute the generated command. At this point, the database is ready for
+     * use. */
+    ExecuteCommand();
+}
+
+
+CBR::CBR(string _filename, string _tablename, string _cols[], \
+        string _primcols[], uint32_t _len, uint32_t _primlen)
+{
+    /* Store database properties. */
+    filename = _filename;
+    tablename = _tablename;
     numColumns = _len;
 
-    // generate command
-    strcpy(command, "CREATE TABLE ");
-    strcat(command, tablename);
-    strcat(command, "(");
-    for (i=0; i<numColumns; i++) {
-        strcat(command, _cols[i]);
-        strcat(command, " FLOAT");
-        if (i != numColumns-1) // not last entry
-            strcat(command, ", ");
-    }
-    strcat(command, ");");
-
-    // execute create table command
+    /* Create the database (or open it if it already exists). */
+    OpenDatabase();
+
+    /* Generate the command that will create the initial table within the
+     * VTCROSS database with primary keys. */
+    command = "CREATE TABLE " + tablename + "(";
+    for(size_t i = 0; i < numColumns; i++) {
+        command += _cols[i];
+        command += " FLOAT, ";
+    }
+
+    command += "PRIMARY KEY (";
+    for(size_t j = 0; j < _primlen; j++) {
+        command += _primcols[j];
+
+        /* If this column is not the last entry, add a comma to the command in
+         * preperation for the next entry. */
+        if(j != _primlen - 1)
+            command += ", ";
+    }
+    command += "));";
+
+    /* Execute the generated command. At this point, the database is ready for
+     * use. */
     ExecuteCommand();
 }
-
-CBR::CBR(char * _filename, char * _tablename, 
-	char * _cols[], char * _primcols[], unsigned int _len,
-	unsigned int _primlen)
-{
-    // create database
-
-    // copy filename
-    unsigned int i=0;
-    strcpy(filename, _filename);
-
-    // execute create database command
-    // database handle
-    //db = NULL;
-    OpenDatabase();
-
-    // create table
-
-    // copy tablename
-    strcpy(tablename, _tablename);
-
-    // number of columns in the table
-    numColumns = _len;
-
-    // generate command
-    strcpy(command, "CREATE TABLE ");
-    strcat(command, tablename);
-    strcat(command, "(");
-    for (i=0; i<numColumns; i++) {
-        strcat(command, _cols[i]);
-        strcat(command, " FLOAT");
-        strcat(command, ", ");
-    }
-   strcat(command, "PRIMARY KEY (");
-    for (i=0; i<_primlen; i++) {
-        strcat(command, _primcols[i]);
-        if (i != _primlen-1) // not last entry
-            strcat(command, ", ");
-    }
-    strcat(command, "));");
-
-    // execute create table command
-    ExecuteCommand();
-}
-
 
 
 CBR::~CBR()
 {  
-    // generate command, remove a table with its content
-    strcpy(command, "drop table ");
-    strcat(command, tablename);
-
-    // execute delete command
+    /* Generate the sqlite command to delete a table and all of its contents,
+     * and then execute it. */
+    command = "drop table " + tablename;
     ExecuteCommand();
 
-    // clean the database
-    strcpy(command, "vacuum");
+    /* Tell sqlite to clean up the database. */
+    command = "vacuum";
     ExecuteCommand();
 }
 
 
-
-
 int32_t 
 CBR::OpenDatabase()
 {
-    int32_t rc;
-
-    rc = sqlite3_open(filename, &db);
+    int32_t rc = sqlite3_open(filename.c_str(), &db);
     if(rc) {
         WARNING("Can't open database: %s\n", sqlite3_errmsg(db));
@@ -234,10 +214,9 @@
 CBR::ExecuteCommand()
 {
-    int rc;
     char *zErrMsg = 0;
 
-    rc = sqlite3_exec(db, command, callback, 0, &zErrMsg);
-    if( rc!=SQLITE_OK){
-        fprintf(stderr, "SQL error: %s: %s\n", zErrMsg, command);
+    int32_t rc = sqlite3_exec(db, command.c_str(), callback, 0, &zErrMsg);
+    if(rc != SQLITE_OK) {
+        WARNING("SQL error: %s: %s\n", zErrMsg, command.c_str());
         sqlite3_free(zErrMsg);
     }
@@ -250,19 +229,18 @@
 CBR::ExecuteSearchCommand(float *_retvals)
 {
-    int rc;
-    unsigned int i;
-
-    sqlite3_stmt * pStatement;
-    rc = sqlite3_prepare_v2(db, command, -1, &pStatement, NULL);
-    if (rc == SQLITE_OK){ 
-        if (sqlite3_step(pStatement) == SQLITE_ROW){
-            for (i=0; i<numColumns; ++i)
+    sqlite3_stmt *pStatement;
+
+    int32_t rc = sqlite3_prepare_v2(db, command.c_str(), -1, &pStatement, NULL);
+    if(rc == SQLITE_OK) { 
+        if(sqlite3_step(pStatement) == SQLITE_ROW) {
+            for(size_t i = 0; i < numColumns; ++i) {
                 _retvals[i] = sqlite3_column_double(pStatement, i);
+            }
         } else {
-		    printf("CBR:: No matched results returning default.\n");
-			rc=31337;
+		    LOG("CBR:: No matched results returning default.\n");
+			rc = 31337;
 		}
     } else {
-		printf("CBR:: Error executing SQL statement. rc = %i\n%s\n",rc,command);
+		WARNING("CBR:: Error executing SQL statement. rc = %i\n%s\n", rc, command.c_str());
     }
 
@@ -271,212 +249,152 @@
     return rc;
 }
+
+
 void 
 CBR::Print()
 {
-    // generate commandi
-    strcpy(command, "select ");
-    strcat(command, tablename);
-    strcat(command, ".* from ");
-    strcat(command, tablename);
-    strcat(command, ";");
-
-    // execute print (select all)  command
+    /* Generate the sqlite command to print the database, which is effectively a
+     * 'select all elements' command, and then execute it. */
+    command = "select " + tablename + ".* from " + tablename + ";";
+
     ExecuteCommand();
-    printf("database %s, table %s:\n", filename, tablename);
-}
-
-int32_t 
-CBR::Search(
-    char *_names[],
-    int * _ops,
-    float *_vals,
-    unsigned int _n,
-    float *_retvals)
+    LOG("database %s, table %s:\n", filename.c_str(), tablename.c_str());
+}
+
+
+int32_t 
+CBR::Search(string _names[], int32_t *_ops, float *_vals, uint32_t _n, \
+        float *_retvals)
 {   
-    int rc;
-        const char *ops_str[] = {"==", "!=", ">", ">=", "<", "<="};
-    // generate command
-    strcpy(command, "select ");
-    strcat(command, tablename);
-    strcat(command, ".* from ");
-    strcat(command, tablename);
-    strcat(command, " where ");
-
-    unsigned int i;
+    LOG("CBR::Search - number of ops %d:\n", _n);
+
     char str_buffer[64];
-    printf("number of ops %d:\n", _n);
-    for (i=0; i<_n; i++) {
-        // ensure valid ops value
-        if (_ops[i] < 0 || _ops[i] > 5) {
-            printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]);
-            exit(1);
+    const string ops_str[] = {"==", "!=", ">", ">=", "<", "<="};
+
+    command = "select " + tablename + ".* from " + tablename + " where ";
+
+    for(size_t i = 0; i < _n; i++) {
+        /* Make sure that the passed ops value is valid. */
+        if((_ops[i] < 0) || (_ops[i] > 5)) {
+            ERROR(1, "Error: cbr_search(), invalid ops id : %d\n", _ops[i]);
         }
 
-        strcat(command, _names[i]);
-        strcat(command, ops_str[_ops[i]]);
-    printf("search command: %s\n", command);
+        command += _names[i] + ops_str[_ops[i]];;
+
+        LOG("CBR::Search - command: %s\n", command.c_str());
+
         sprintf(str_buffer, "%E", _vals[i]);
-        strcat(command, str_buffer);
-
-
-        if (i<_n-1)
-            strcat(command, " AND ");
+        command += string(str_buffer);
+
+        if(i < _n - 1)
+            command += " AND ";
         else
-            strcat(command, " order by utility desc;");
-    }
-
-
-    rc = ExecuteSearchCommand(_retvals);
+            command += " order by utility desc;";
+    }
+
+    return ExecuteSearchCommand(_retvals);
+}
+
+
+int32_t 
+CBR::SearchSum(string _name, float *_retvals)
+{   
+    command = "select SUM(" + tablename + "." + _name + ") from " + tablename + ";";
+
+    return ExecuteSearchCommand(_retvals);
+}
+
+
+int32_t 
+CBR::SearchRand(string _names[], int32_t *_ops, float *_vals, uint32_t _n, \
+        float *_retvals)
+{   
+    char str_buffer[64];
+    const char *ops_str[] = {"==", "!=", ">", ">=", "<", "<="};
+
+    command = "select " + tablename + ".* from " + tablename + " where ";
+
+    for(size_t i = 0; i < _n; i++) {
+        /* Make sure that the passed ops value is valid. */
+        if((_ops[i] < 0) || (_ops[i] > 5)) {
+            ERROR(1, "Error: cbr_search(), invalid ops id : %d\n", _ops[i]);
+        }
+
+        command += _names[i] + ops_str[_ops[i]];;
+
+        sprintf(str_buffer, "%E", _vals[i]);
+        command += str_buffer;
+
+        if(i < _n - 1)
+            command += " AND ";
+        else
+            command += " order by RAND();";
+    }
+
+    return ExecuteSearchCommand(_retvals);
+}
+
+
+int32_t 
+CBR::Update(string _where[], string _set[], float *_wherevals, float *_setvals, 
+		uint32_t _wherelen, uint32_t _setlen)
+{
+    char str_buffer[64];
+
+    /* Generate the command to update the table. */
+    command = "UPDATE " + tablename + " SET ";
+
+    for(size_t i = 0; i < _setlen; i++) {
+        command += _set[i] + " = ";
+        sprintf(str_buffer, "%f", _setvals[i]);
+        command += string(str_buffer) + "  ";
+
+        if(i != _setlen - 1)
+            command += ", ";
+    }
+    command += " WHERE ";
+
+    for(size_t j = 0; j < _wherelen; j++) {
+        command += _where[j] + " = ";
+        sprintf(str_buffer, "%f", _wherevals[j]);
+        command += string(str_buffer) + "  ";
+
+        if(j != _wherelen - 1)
+            command += "AND ";
+    }
+    command += ";";
     
-    return rc;
-}
-
-int32_t 
-CBR::SearchSum(
-    char *_name,
-    float *_retvals)
-{   
-    int rc;
-    // generate command
-    strcpy(command, "select SUM( ");
-    strcat(command, tablename);
-    strcat(command, ".");
-    strcat(command, _name);
-    strcat(command, ") from ");
-    strcat(command, tablename);
-    strcat(command, ";");
-
-    //printf("search command: %s\n", command);
-
-    rc = ExecuteSearchCommand(_retvals);
-    /*printf("search result: ");
-    for (int i=0; i<numColumns; i++)
-        printf("%f, ",_retvals[i]);
-    printf("\n");
-    */
-    return rc;
-}
-
-
-int32_t 
-CBR::SearchRand(
-    char *_names[],
-    int * _ops,
-    float *_vals,
-    unsigned int _n,
-    float *_retvals)
-{   
-        const char *ops_str[] = {"==", "!=", ">", ">=", "<", "<="};
-    int rc;
-    // generate command
-    strcpy(command, "select ");
-    strcat(command, tablename);
-    strcat(command, ".* from ");
-    strcat(command, tablename);
-    strcat(command, " where ");
-
-    unsigned int i;
+    return ExecuteCommand();
+}
+
+
+int32_t 
+CBR::AddRow(string _cols[], float *_vals, uint32_t _len)
+{
     char str_buffer[64];
-    for (i=0; i<_n; i++) {
-        // ensure valid ops value
-        if (_ops[i] < 0 || _ops[i] > 5) {
-            printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]);
-            exit(1);
-        }
-
-        strcat(command, _names[i]);
-        strcat(command, ops_str[_ops[i]]);
-        sprintf(str_buffer, "%E", _vals[i]);
-        strcat(command, str_buffer);
-
-        if (i<_n-1)
-            strcat(command, " AND ");
-        else
-            strcat(command, " order by RAND();");
-    }
-
-    //printf("search command: %s\n", command);
-
-    rc = ExecuteSearchCommand(_retvals);
-    
-    /*printf("search result: ");
-    for (i=0; i<numColumns; i++)
-        printf("%f, ",_retvals[i]);
-    printf("\n");
-*/
-    return rc;
-}
-
-
-
-
-int32_t 
-CBR::Update(char *_where[], char*_set[], float *_wherevals, float *_setvals, 
-		unsigned int _wherelen, unsigned int _setlen)
-{
-    unsigned int i;
-    
-    // generate command
-    strcpy(command, "UPDATE ");
-    strcat(command, tablename);
-    
-    strcat(command, " SET ");
-    for (i=0; i<_setlen; i++) {
-        strcat(command, _set[i]);
-        strcat(command, " = ");
-        sprintf(command, "%s%f", command, _setvals[i]);
-        strcat(command, "  ");
-        if (i != _setlen-1) // not last entry
-            strcat(command, ", ");
-    }
-    strcat(command, " WHERE ");
-
-    for (i=0; i<_wherelen; i++) {
-        strcat(command, _where[i]);
-        strcat(command, " = ");
-        sprintf(command, "%s%f", command, _wherevals[i]);
-        strcat(command, "  ");
-        if (i != _wherelen-1) // not last entry
-            strcat(command, "AND ");
-    }
-    strcat(command, ";");
-    
-    // execute add command
-    ExecuteCommand();
-
-    return 0;
-}
-
-int32_t 
-CBR::AddRow(char *_cols[], float *_vals, unsigned int _len)
-{
-    unsigned int i;
-    
-    // generate command
-    strcpy(command, "insert into ");
-    strcat(command, tablename);
-    
-    strcat(command, " (");
-    for (i=0; i<_len; i++) {
-        strcat(command, _cols[i]);
-        if (i != numColumns-1) // not last entry
-            strcat(command, ", ");
-    }
-    strcat(command, ") ");
-
-    strcat(command, " values(");
-    for (i=0; i<_len; i++) {
+
+    command = "insert into " + tablename + " (";
+
+    for(size_t i = 0; i < _len; i++) {
+        command += _cols[i];
+
+        if(i != numColumns - 1)
+            command += ", ";
+    }
+    command += ") values(";
+
+    for(size_t j = 0; j < _len; j++) {
+        // TODO I have no idea what the below question is about.
         // ???? how to fill the values if numColumns != _len
         // assume = in the following
-        sprintf(command, "%s%f", command, _vals[i]);
-        if (i != numColumns-1) // not last entry
-            strcat(command, ", ");
-    }
-    strcat(command, ");");
+        sprintf(str_buffer, "%f", _vals[j]);
+        command += str_buffer;
+
+        if(j != numColumns - 1)
+            command += ", ";
+    }
+    command += ");";
     
-    // execute add command
-    ExecuteCommand();
-
-    return 0;
+    return ExecuteCommand();
 }
 
