Index: vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp
===================================================================
--- vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp	(revision 411)
+++ vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp	(revision 419)
@@ -23,5 +23,5 @@
  * Missions are loaded from an XML file, connected with services provided by components,
  * and run.  See the documentation for the "PerformActiveMission" below for important 
- * info.  
+ * info.
  */
 
@@ -66,7 +66,7 @@
 
 struct services_s {
-    char filename[64];
-    char tablename[64];
-    char command[2048];
+    string filename;
+    string tablename;
+    string command;
     sqlite3 *db; 
     unsigned int num_columns;
@@ -74,7 +74,7 @@
 
 struct data_s {
-    char filename[64];
-    char tablename[64];
-    char command[2048];
+    string filename;
+    string tablename;
+    string command;
     sqlite3 *db; 
     unsigned int num_columns;
@@ -113,22 +113,22 @@
 {
     char *errorMsg;
-    strcpy(_services_DB->command, "drop table ");
-    strcat(_services_DB->command, _services_DB->tablename);
-    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg);
+    _services_DB->command="drop table ";
+    _services_DB->command.append(_services_DB->tablename);
+    int rc = sqlite3_exec(_services_DB->db, _services_DB->command.c_str(), callback, 0, &errorMsg);
     if( rc!=SQLITE_OK && rc!=101 )
         fprintf(stderr, "ServiceManagementLayer::Destructor services 'drop table' error: %s\n", errorMsg);
-    strcpy(_services_DB->command, "vacuum");
-    rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg);
+    _services_DB->command="vacuum";
+    rc = sqlite3_exec(_services_DB->db, _services_DB->command.c_str(), callback, 0, &errorMsg);
     if( rc!=SQLITE_OK && rc!=101 )
         fprintf(stderr, "ServiceManagementLayer::Destructor services 'vacuum' error: %s\n", errorMsg);
     free(_services_DB);
 
-    strcpy(_data_DB->command, "drop table ");
-    strcat(_data_DB->command, _data_DB->tablename);
-    rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg);
+    _data_DB->command="drop table ";
+    _data_DB->command.append(_data_DB->tablename);
+    rc = sqlite3_exec(_data_DB->db, _data_DB->command.c_str(), callback, 0, &errorMsg);
     if( rc!=SQLITE_OK && rc!=101 )
         fprintf(stderr, "ServiceManagementLayer::Destructor data 'drop table' error: %s\n", errorMsg);
-    strcpy(_data_DB->command, "vacuum");
-    rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg);
+    _data_DB->command="vacuum";
+    rc = sqlite3_exec(_data_DB->db, _data_DB->command.c_str(), callback, 0, &errorMsg);
     if( rc!=SQLITE_OK && rc!=101 )
         fprintf(stderr, "ServiceManagementLayer::Destructor data 'vacuum' error: %s\n", errorMsg);
@@ -169,33 +169,26 @@
 ServiceManagementLayer::CreateServicesDB()
 {
-
-
-  sqlite3_stmt *ppStmt;  /* OUT: Statement handle */
-  const char *pzTail;     /* OUT: Pointer to unused portion of zSql */
-
-
-    _services_DB = (services_DB) malloc(sizeof(struct services_s));
-   // char *errorMsg;
-
-    // create database
-
+    sqlite3_stmt *ppStmt;  /* OUT: Statement handle */
+    const char *pzTail;     /* OUT: Pointer to unused portion of zSql */
+
+    //_services_DB = (services_DB) malloc(sizeof(struct services_s));
+    _services_DB = new services_s;
     // copy filename
-    strcpy(_services_DB->filename, "Services_Table");
-
+    _services_DB->filename="Services_Table";
     // execute create database command
     // database handle
     //_services_DB->db = NULL;
-    sqlite3_open(_services_DB->filename, &(_services_DB->db));
+    sqlite3_open(_services_DB->filename.c_str(), &(_services_DB->db));
     char* cols[] = {(char *)"ID_Num", (char *)"Service_Name"};
 
 
     // copy tablename
-    strcpy(_services_DB->tablename, "Services");
+    _services_DB->tablename="Services";
 
     //If program execution ends in anything other than a ordered shutdown, DB's will still be there for next run
     //Need to get rid of it so that old data isn't inadvertantly used in the next execution cycle
-    sprintf(_services_DB->command, "DROP TABLE IF EXISTS Services;");     
-
-    int rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command, 128, &ppStmt, &pzTail);
+    _services_DB->command = "DROP TABLE IF EXISTS Services;";     
+
+    int rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command.c_str(), 128, &ppStmt, &pzTail);
     if( rc!=SQLITE_OK && rc!=101 )
         printf("ServiceManagementLayer::CreateServicesDB 'prepare_stmt' error %d\n", rc);
@@ -208,16 +201,15 @@
 
     // generate command
-    strcpy(_services_DB->command, "CREATE TABLE ");
-    strcat(_services_DB->command, _services_DB->tablename);
-    strcat(_services_DB->command, "(");
-    strcat(_services_DB->command, cols[0]);
-    strcat(_services_DB->command, " INT, ");
-    strcat(_services_DB->command, cols[1]);
-    strcat(_services_DB->command, " TEXT");
-    strcat(_services_DB->command, ");");
+    _services_DB->command="CREATE TABLE ";
+    _services_DB->command.append(_services_DB->tablename);
+    _services_DB->command.append("(");
+    _services_DB->command.append(cols[0]);
+    _services_DB->command.append(" INT, ");
+    _services_DB->command.append(cols[1]);
+    _services_DB->command.append(" TEXT);");
 
     // execute create table command
 
-    rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command, 128, &ppStmt, &pzTail);
+    rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command.c_str(), 128, &ppStmt, &pzTail);
     if( rc!=SQLITE_OK && rc!=101 )
         printf("ServiceManagementLayer::CreateServicesDB 'prepare_stmt' error %d\n", rc);
@@ -236,17 +228,17 @@
 ServiceManagementLayer::CreateDataDB()
 {
-    _data_DB = (data_DB) malloc(sizeof(struct data_s));
-    //char *errorMsg;
-  sqlite3_stmt *ppStmt;  /* OUT: Statement handle */
-  const char *pzTail;     /* OUT: Pointer to unused portion of zSql */
+    sqlite3_stmt *ppStmt;  /* OUT: Statement handle */
+    const char *pzTail;     /* OUT: Pointer to unused portion of zSql */
+
+    _data_DB = new data_s;//(data_DB) malloc(sizeof(struct data_s));
 
     // create database
 
     // copy filename
-    strcpy(_data_DB->filename, "Data_Table");
+    _data_DB->filename="Data_Table";
     // execute create database command
     // database handle
     //_services_DB->db = NULL;
-    sqlite3_open(_data_DB->filename, &(_data_DB->db));
+    sqlite3_open(_data_DB->filename.c_str(), &(_data_DB->db));
     char* cols[] = {(char *)"Tag", (char *)"Data"};
 
@@ -254,8 +246,8 @@
 
     // copy tablename
-    strcpy(_data_DB->tablename, "Data");
-    sprintf(_data_DB->command, "DROP TABLE IF EXISTS Data;");     
-
-    int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, 128, &ppStmt, &pzTail);
+    _data_DB->tablename="Data";
+    _data_DB->command="DROP TABLE IF EXISTS Data;";     
+
+    int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), 128, &ppStmt, &pzTail);
     if( rc!=SQLITE_OK && rc!=101 )
         printf("ServiceManagementLayer::CreateServicesDB 'prepare_stmt' error %d\n", rc);
@@ -269,18 +261,17 @@
 
     // generate command
-    strcpy(_data_DB->command, "CREATE TABLE ");
-    strcat(_data_DB->command, _data_DB->tablename);
-    strcat(_data_DB->command, "(");
-    strcat(_data_DB->command, cols[0]);
+    _data_DB->command="CREATE TABLE ";
+    _data_DB->command.append(_data_DB->tablename);
+    _data_DB->command.append("(");
+    _data_DB->command.append(cols[0]);
     //First column is the name of the data (coresponding to the name of the output/input pair)
     //It is the primary key so any subsequent data with the same name will replace the row
-    strcat(_data_DB->command, " TEXT PRIMARY KEY ON CONFLICT REPLACE, ");
-    strcat(_data_DB->command, cols[1]);
-    strcat(_data_DB->command, " TEXT");
-    strcat(_data_DB->command, ");");
+    _data_DB->command.append(" TEXT PRIMARY KEY ON CONFLICT REPLACE, ");
+    _data_DB->command.append(cols[1]);
+    _data_DB->command.append(" TEXT);");
 
     // execute create table command
 
-    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, 128, &ppStmt, &pzTail);
+    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), 128, &ppStmt, &pzTail);
     if( rc!=SQLITE_OK && rc!=101 )
         printf("ServiceManagementLayer::CreateDataDB 'prepare_stmt' error %d\n", rc);
@@ -559,21 +550,24 @@
     //printf("RS_buffer = %s\n", buffer);
     // generate command
-    strcpy(_services_DB->command, "insert into ");
-    strcat(_services_DB->command, _services_DB->tablename); 
-    strcat(_services_DB->command, " (");
-    strcat(_services_DB->command, cols[0]);
-    strcat(_services_DB->command, ", ");
-    strcat(_services_DB->command, cols[1]);
-    strcat(_services_DB->command, ") ");
-    strcat(_services_DB->command, " values(");
-    sprintf(_services_DB->command, "%s%d", _services_DB->command, ID);
-    strcat(_services_DB->command, ", '");
-    strcat(_services_DB->command, buffer);
-    strcat(_services_DB->command, "');");
+    _services_DB->command="insert into ";
+    _services_DB->command.append(_services_DB->tablename); 
+    _services_DB->command.append(" (");
+    _services_DB->command.append(cols[0]);
+    _services_DB->command.append(", ");
+    _services_DB->command.append(cols[1]);
+    _services_DB->command.append(") ");
+    _services_DB->command.append(" values(");
+    char temp[3];
+    memset(temp,0,3);
+    sprintf(temp, "%d", ID);
+    _services_DB->command.append(temp);
+    _services_DB->command.append(", '");
+    _services_DB->command.append(buffer);
+    _services_DB->command.append("');");
     
     //printf("search command: %s\n", _services_DB->command);
     // execute add command
     char *errorMsg;
-    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg);
+    int rc = sqlite3_exec(_services_DB->db, _services_DB->command.c_str(), callback, 0, &errorMsg);
     if( rc!=SQLITE_OK && rc!=101 )
         fprintf(stderr, "ServiceManagementLayer::RecieveServices DB Error %s\n", errorMsg);
@@ -613,13 +607,14 @@
 	   miss[activeMission].services[i].name.compare("shell")){
 	    //If it is a service, search the database of registered services to find the ID of the component that registered it
-    	    strcpy(_services_DB->command, "select ");
-   	    strcat(_services_DB->command, _services_DB->tablename);
-    	    strcat(_services_DB->command, ".* from ");
-    	    strcat(_services_DB->command, _services_DB->tablename);
-    	    strcat(_services_DB->command, " where Service_Name==");
-	    sprintf(_services_DB->command, "%s'%s';", _services_DB->command, miss[activeMission].services[i].name.c_str());
+    	    _services_DB->command="select ";
+   	    _services_DB->command.append(_services_DB->tablename);
+    	    _services_DB->command.append(".* from ");
+    	    _services_DB->command.append( _services_DB->tablename);
+    	    _services_DB->command.append(" where Service_Name=='");
+	    _services_DB->command.append(miss[activeMission].services[i].name);
+	    _services_DB->command.append("';");
 	
             sqlite3_stmt * pStatement;
-            int rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command, -1, &pStatement, NULL);
+            int rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command.c_str(), -1, &pStatement, NULL);
             if (rc == SQLITE_OK){ 
                 if (sqlite3_step(pStatement) == SQLITE_ROW)
@@ -630,5 +625,5 @@
 	        }
              } else {
-	        printf("services_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_services_DB->command);
+	        printf("services_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_services_DB->command.c_str());
             }
 		//printf("s_name=%s\n",miss[activeMission].services[i].name.c_str());
@@ -677,22 +672,23 @@
 	for(int t = 0; t < k; t++){
 	    memset(buffer, 0 , 256);
-            strcpy(_data_DB->command, "select ");
-   	    strcat(_data_DB->command, _data_DB->tablename);
-    	    strcat(_data_DB->command, ".* from ");
-    	    strcat(_data_DB->command, _data_DB->tablename);
-    	    strcat(_data_DB->command, " where Tag==");
-	    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, miss[activeMission].input[t].c_str());
+            _data_DB->command="select ";
+   	    _data_DB->command.append(_data_DB->tablename);
+    	    _data_DB->command.append(".* from ");
+    	    _data_DB->command.append(_data_DB->tablename);
+    	    _data_DB->command.append(" where Tag=='");
+	    _data_DB->command.append(miss[activeMission].input[t]);
+	    _data_DB->command.append("';");
             sqlite3_stmt * pStatement;
-            int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
+            int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
             if (rc == SQLITE_OK){ 
                 if (sqlite3_step(pStatement) == SQLITE_ROW)
 	            data=((const char*) sqlite3_column_text(pStatement, 1)); 
                 else {
-	            printf("data_DB:: Data not yet in DB.\n");
+	            printf("3data_DB:: Data not yet in DB., %s\n", _data_DB->command.c_str());
 		    rc=31337;
 	        }
             } 
 	    else {
-	        printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+	        printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
             }
             sqlite3_finalize(pStatement);
@@ -722,22 +718,23 @@
     //Load and transmit the input data
     while(i < 10 && !miss[activeMission].services[sourceID].input[i].empty()){
-        strcpy(_data_DB->command, "select ");
-   	strcat(_data_DB->command, _data_DB->tablename);
-    	strcat(_data_DB->command, ".* from ");
-    	strcat(_data_DB->command, _data_DB->tablename);
-    	strcat(_data_DB->command, " where Tag==");
-	sprintf(_data_DB->command, "%s'%s';", _data_DB->command, miss[activeMission].services[sourceID].input[i].c_str());
+        _data_DB->command="select ";
+   	_data_DB->command.append(_data_DB->tablename);
+    	_data_DB->command.append(".* from ");
+    	_data_DB->command.append(_data_DB->tablename);
+    	_data_DB->command.append(" where Tag=='");
+	_data_DB->command.append(miss[activeMission].services[sourceID].input[i]);
+    	_data_DB->command.append("';");
         sqlite3_stmt * pStatement;
-        int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
+        int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
         if (rc == SQLITE_OK){ 
             if (sqlite3_step(pStatement) == SQLITE_ROW)
 	         data.append((const char*) sqlite3_column_text(pStatement, 1)); 
             else {
-	            printf("data_DB:: Data not yet in DB.\n");
+	            printf("2data_DB:: Data not yet in DB.\n");
 		    rc=31337;
 	    }
         } 
 	else {
-	    printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+	    printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
         }
         sqlite3_finalize(pStatement);
@@ -774,18 +771,18 @@
 	}
 
-	strcpy(_data_DB->command, "insert or replace into ");
-    	strcat(_data_DB->command, _data_DB->tablename); 
-      	strcat(_data_DB->command, " (");
-    	strcat(_data_DB->command, cols[0]);
-    	strcat(_data_DB->command, ", ");
-	strcat(_data_DB->command, cols[1]);
-	strcat(_data_DB->command, ") ");
-	strcat(_data_DB->command, " values('");	
-	strcat(_data_DB->command, miss[activeMission].services[sourceID].output[j].c_str());
-	strcat(_data_DB->command, "', '");
-	strcat(_data_DB->command, data.c_str());
-	strcat(_data_DB->command, "');");
+	_data_DB->command="insert or replace into ";
+    	_data_DB->command.append(_data_DB->tablename); 
+      	_data_DB->command.append(" (");
+    	_data_DB->command.append(cols[0]);
+    	_data_DB->command.append(", ");
+	_data_DB->command.append(cols[1]);
+	_data_DB->command.append(") ");
+	_data_DB->command.append(" values('");	
+	_data_DB->command.append(miss[activeMission].services[sourceID].output[j]);
+	_data_DB->command.append("', '");
+	_data_DB->command.append(data);
+	_data_DB->command.append("');");
     	char *errorMsg;
-   	rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg);
+   	rc = sqlite3_exec(_data_DB->db, _data_DB->command.c_str(), callback, 0, &errorMsg);
    	if( rc!=SQLITE_OK && rc!=101 )
    	    fprintf(stderr, "SQL error: %s\n", errorMsg);
@@ -847,5 +844,5 @@
     char buffer[256];
     char buffer1[256];
-    char *token, *token2;
+    std::string token, token2;
     std::string data2;
     int rc;
@@ -866,18 +863,23 @@
     t=atoi(buffer);
     for(int m = 0; m < t; m++) {
-	//printf("data=%s\n", data_obsv.c_str());
         memset(buffer1, 0, 256);
         ReadMessage(shellSocketFD, buffer1);
-	strcpy(_data_DB->command, "insert into ");
-    	strcat(_data_DB->command, _data_DB->tablename); 
-      	strcat(_data_DB->command, " (");
-    	strcat(_data_DB->command, cols[0]);
-    	strcat(_data_DB->command, ", ");
-	strcat(_data_DB->command, cols[1]);
-	strcat(_data_DB->command, ") ");
+	_data_DB->command="insert into ";
+    	_data_DB->command.append(_data_DB->tablename); 
+      	_data_DB->command.append(" (");
+    	_data_DB->command.append(cols[0]);
+    	_data_DB->command.append(", ");
+	_data_DB->command.append(cols[1]);
+	_data_DB->command.append(") ");
         memset(buffer, 0, 256);
         ReadMessage(shellSocketFD, buffer);
-	sprintf(_data_DB->command, "%s values('%s', '1@%s@%s');", _data_DB->command, buffer1, buffer1, buffer);
-   	rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg);
+	_data_DB->command.append(" values('");
+	_data_DB->command.append(buffer1);
+	_data_DB->command.append("', '1@");
+	_data_DB->command.append(buffer1);
+	_data_DB->command.append("@");
+	_data_DB->command.append(buffer);
+	_data_DB->command.append("');");
+   	rc = sqlite3_exec(_data_DB->db, _data_DB->command.c_str(), callback, 0, &errorMsg);
    	if( rc!=SQLITE_OK && rc!=101 )
    	    fprintf(stderr, "SQL error: %s\n", errorMsg);
@@ -913,14 +915,14 @@
 	    for(t = 0; t < 10; t++){
 		if(!miss[activeMission].services[i].output[t].empty()){
-		    //printf("i-numstmts-1 = %d\n", i-numstatements[0]-1);
 		    input=miss[activeMission].services[i-numstatements[0]-1].output[t];
-		    strcpy(_data_DB->command, "SELECT ");
-		    strcat(_data_DB->command, _data_DB->tablename);
-		    strcat(_data_DB->command, ".* from ");
-		    strcat(_data_DB->command, _data_DB->tablename);
-		    strcat(_data_DB->command, " where Tag==");
-		    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str());
+		    _data_DB->command="SELECT ";
+		    _data_DB->command.append(_data_DB->tablename);
+		    _data_DB->command.append(".* from ");
+		    _data_DB->command.append(_data_DB->tablename);
+		    _data_DB->command.append(" where Tag=='");
+		    _data_DB->command.append(input);
+		    _data_DB->command.append("';");
 		    sqlite3_stmt * pStatement;
-		    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
+		    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
 		    if (rc == SQLITE_OK){ 
 			if (sqlite3_step(pStatement) == SQLITE_ROW)
@@ -931,30 +933,29 @@
 			}
 		    } else {
-			printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+			printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
 		    }
 	    	    sqlite3_finalize(pStatement);
 		    //printf("data=%s\n", data.c_str());
-		    token = strtok((char *)data.c_str(), "@");
-		    token = strtok(NULL, "@");
-		    token = strtok(NULL, "@");
-		    //printf("data=%s\n", token);
+		    int pos = data.find_last_of("@", data.length()-2);
+		    token = data.substr(pos+1);
+		    token.erase(token.length()-1);
+		    //printf("token=%s, %d\n", token.c_str(), pos);
 		data.clear();
 		    break;
 		}
 	    }
-	    //printf("L0:--- %s  %s---\n", miss[activeMission].services[i].output[t].c_str(), token);
+	    //printf("Level 0:--- %s  %s---\n", miss[activeMission].services[i].output[t].c_str(), token);
 	    bool doit = false;
 	    if(miss[activeMission].services[i].output[t].find(">") != string::npos){
 		    std::string data2;
-		    strcpy(_data_DB->command, "SELECT ");
-		    strcat(_data_DB->command, _data_DB->tablename);
-		    strcat(_data_DB->command, ".* from ");
-		    strcat(_data_DB->command, _data_DB->tablename);
-	        strcat(_data_DB->command, " where Tag==");
-		    miss[activeMission].services[i].output[t].erase(0, 1);
-	    //printf("here! %s\n", miss[activeMission].services[i].output[t].c_str());
-		    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, miss[activeMission].services[i].output[t].c_str());
+		    _data_DB->command="SELECT ";
+		    _data_DB->command.append(_data_DB->tablename);
+		    _data_DB->command.append(".* from ");
+		    _data_DB->command.append(_data_DB->tablename);
+		    _data_DB->command.append(" where Tag=='");
+		    _data_DB->command.append(miss[activeMission].services[i].output[t].erase(0, 1));
+		    _data_DB->command.append("';");
 		    sqlite3_stmt * pStatement;
-		    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
+		    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
 		    if (rc == SQLITE_OK){ 
 			if (sqlite3_step(pStatement) == SQLITE_ROW)
@@ -965,38 +966,37 @@
 			}
 		    } else {
-			printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+			printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
 		    }
-		   	    sqlite3_finalize(pStatement);
-		    char* token2 = strtok((char *)data2.c_str(), "@");
-		    token2 = strtok(NULL, "@");
-		    token2 = strtok(NULL, "@");
-		    if(atof(token) > atof(token2))
+		    sqlite3_finalize(pStatement);
+
+		    int pos = data2.find_last_of("@", data2.length()-2);
+		    token2 = data2.substr(pos+1);
+		    token2.erase(token2.length()-1);
+		    //printf("token2=%s, %d\n", token2.c_str(), pos);
+		    if(atof(token.c_str()) > atof(token2.c_str()))
 			doit=true;
 		    //printf("%s %s\n", buffer, token);
 	    }
-	    else if (strstr(miss[activeMission].services[i].output[t].c_str(), token))
+	    else if (miss[activeMission].services[i].output[t].find(token) != string::npos)
     	        doit=true;
 	    if(doit){
-	    //if(strstr(miss[activeMission].services[i].output[t].c_str(), token)){
-		//printf("L0:if taken\n");
+		//printf("Level 0:if taken\n");
 		for(uint16_t k = i+1; k <= i+miss[activeMission].services[i].num_conds; k++){ 
-		        //printf("transacting data for k=%d\n", k);
-		    //printf("%s---%d\n", miss[activeMission].services[k].name.c_str(), k);
 		    if(miss[activeMission].services[k].name.compare("if")==0){
-			//printf("L1:if detected\n");
+			//printf("Level 1:if detected\n");
 			    input.clear();
 			    check.clear();
 			    for(t = 0; t < 10; t++){
 				if(!miss[activeMission].services[k].output[t].empty()){
-				    //printf("i-numstmts = %d\n", i-numstatements-1);
 				    input=miss[activeMission].services[k-numstatements[1]-1].output[t];
-				    strcpy(_data_DB->command, "SELECT ");
-				    strcat(_data_DB->command, _data_DB->tablename);
-				    strcat(_data_DB->command, ".* from ");
-				    strcat(_data_DB->command, _data_DB->tablename);
-				    strcat(_data_DB->command, " where Tag==");
-				    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str());
+		    		    _data_DB->command="SELECT ";
+		    		    _data_DB->command.append(_data_DB->tablename);
+		   		    _data_DB->command.append(".* from ");
+		   		    _data_DB->command.append(_data_DB->tablename);
+		  		    _data_DB->command.append(" where Tag=='");
+		 		    _data_DB->command.append(input);
+				    _data_DB->command.append("';");
 				    sqlite3_stmt * pStatement;
-				    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
+				    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
 				    if (rc == SQLITE_OK){ 
 					if (sqlite3_step(pStatement) == SQLITE_ROW)
@@ -1007,29 +1007,27 @@
 					}
 				    } else {
-					printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+					printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
 				    }
 			    	    sqlite3_finalize(pStatement);
-				    //printf("data=%s\n", data.c_str());
-				    token = strtok((char *)data.c_str(), "@");
-				    token = strtok(NULL, "@");
-				    token = strtok(NULL, "@");
-				    //printf("data=%s\n", token);
+				    //printf("Level 1:data=%s\n", data.c_str());
+				    int pos = data.find_last_of("@", data.length()-2);
+				    token = data.substr(pos+1);
+				    token.erase(token.length()-1);
+				    //printf("Level 1:token=%s\n", token);
 				    break;
 				}
 			    }
-			    //printf("L1:--- %s  %s---\n", miss[activeMission].services[k].output[t].c_str(), token);
 			    bool doit = false;
-				//printf("here! %s\n", miss[activeMission].services[k].output[t].c_str());
 			    if(miss[activeMission].services[k].output[t].find(">") != string::npos){
 				    std::string data2;
-				    strcpy(_data_DB->command, "SELECT ");
-				    strcat(_data_DB->command, _data_DB->tablename);
-				    strcat(_data_DB->command, ".* from ");
-				    strcat(_data_DB->command, _data_DB->tablename);
-				    strcat(_data_DB->command, " where Tag==");
-				    miss[activeMission].services[k].output[t].erase(0, 1);
-				    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, miss[activeMission].services[k].output[t].c_str());
+		    		    _data_DB->command="SELECT ";
+				    _data_DB->command.append(_data_DB->tablename);
+				    _data_DB->command.append(".* from ");
+				    _data_DB->command.append(_data_DB->tablename);
+				    _data_DB->command.append(" where Tag=='");
+				    _data_DB->command.append(miss[activeMission].services[k].output[t].erase(0, 1));
+				    _data_DB->command.append("';");
 				    sqlite3_stmt * pStatement;
-				    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
+				    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
 				    if (rc == SQLITE_OK){ 
 					if (sqlite3_step(pStatement) == SQLITE_ROW)
@@ -1040,38 +1038,35 @@
 					}
 				    } else {
-					printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+					printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
 				    }
 			    	    sqlite3_finalize(pStatement);
-		    		//printf("data=%s token=%s\n", data2.c_str(), token);
-				    token2 = strtok((char *)data2.c_str(), "@");
-				    token2 = strtok(NULL, "@");
-				    token2 = strtok(NULL, "@");
-		    		//printf("token2 %s token %s\n", token2, token);
-			        if(atof(token) > atof(token2))
-				    doit=true;
+				    int pos = data2.find_last_of("@", data2.length()-2);
+				    token2 = data2.substr(pos+1);
+				    token2.erase(token2.length()-1);
+		    		    //printf("token=%s token2=%s\n", token.c_str(), token2.c_str());
+		    		    if(atof(token.c_str()) > atof(token2.c_str()))
+				    	doit=true;
 			    }
-			    else if (strstr(miss[activeMission].services[k].output[t].c_str(), token))
+			    else if (miss[activeMission].services[k].output[t].find(token) != string::npos)
 				doit=true;
 			    if(doit){
-				//printf("L1:if taken\n");
+				//printf("Level 1:if taken\n");
 				for(uint16_t j = k+1; j <= k+miss[activeMission].services[k].num_conds; j++){ 
-				    //printf("transacting data for k=%d\n", k);
-				    //printf("%s---%d\n", miss[activeMission].services[j].name.c_str(), j);
 				    if(miss[activeMission].services[j].name.compare("if")==0){
-					//printf("L2:if detected\n");
+					//printf("Level 2:if detected\n");
 					    input.clear();
 					    check.clear();
 					    for(t = 0; t < 10; t++){
 						if(!miss[activeMission].services[j].output[t].empty()){
-						    //printf("i-numstmts = %d\n", i-numstatements-1);
 						    input=miss[activeMission].services[j-numstatements[2]-1].output[t];
-						    strcpy(_data_DB->command, "SELECT ");
-						    strcat(_data_DB->command, _data_DB->tablename);
-						    strcat(_data_DB->command, ".* from ");
-						    strcat(_data_DB->command, _data_DB->tablename);
-						    strcat(_data_DB->command, " where Tag==");
-						    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str());
+		    		   		    _data_DB->command="SELECT ";
+		    				    _data_DB->command.append(_data_DB->tablename);
+		   				    _data_DB->command.append(".* from ");
+		   				    _data_DB->command.append(_data_DB->tablename);
+		  				    _data_DB->command.append(" where Tag=='");
+		 				    _data_DB->command.append(input);
+						    _data_DB->command.append("';");
 						    sqlite3_stmt * pStatement;
-						    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
+						    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
 						    if (rc == SQLITE_OK){ 
 							if (sqlite3_step(pStatement) == SQLITE_ROW)
@@ -1082,29 +1077,27 @@
 							}
 						    } else {
-							printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+							printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
 						    }
 					    	    sqlite3_finalize(pStatement);
 						    //printf("data=%s\n", data.c_str());
-						    token = strtok((char *)data.c_str(), "@");
-						    token = strtok(NULL, "@");
-						    token = strtok(NULL, "@");
-						    //printf("data=%s\n", token);
+						    int pos = data.find_last_of("@", data.length()-2);
+						    token = data.substr(pos+1);
+						    token.erase(token.length()-1);
+						    //printf("token=%s\n", token.c_str());
 						    data.clear();
 						    break;
 						}
 					    }
-					    //printf("L2:---%s||%s---\n", miss[activeMission].services[j].output[t].c_str(), token);
 					    bool doit = false;
-						//printf("here! %s\n", miss[activeMission].services[j].output[t].c_str());
 					    if(miss[activeMission].services[j].output[t].find(">") != string::npos){
-						    strcpy(_data_DB->command, "SELECT ");
-						    strcat(_data_DB->command, _data_DB->tablename);
-						    strcat(_data_DB->command, ".* from ");
-						    strcat(_data_DB->command, _data_DB->tablename);
-						    strcat(_data_DB->command, " where Tag==");
-						    miss[activeMission].services[k].output[t].erase(0, 1);
-					sprintf(_data_DB->command, "%s'%s';", _data_DB->command, miss[activeMission].services[j].output[t].c_str());
+					        _data_DB->command="SELECT ";
+		    				_data_DB->command.append(_data_DB->tablename);
+		    				_data_DB->command.append(".* from ");
+		   				_data_DB->command.append(_data_DB->tablename);
+		    				_data_DB->command.append(" where Tag=='");
+		    				_data_DB->command.append(miss[activeMission].services[j].output[t].erase(0, 1));
+		    				_data_DB->command.append("';");
 						    sqlite3_stmt * pStatement;
-						    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
+						    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
 						    if (rc == SQLITE_OK){ 
 							if (sqlite3_step(pStatement) == SQLITE_ROW)
@@ -1115,39 +1108,37 @@
 							}
 						    } else {
-							printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+							printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
 						    }
 					    	    sqlite3_finalize(pStatement);
-						    token2 = strtok((char *)data2.c_str(), "@");
-						    token2 = strtok(NULL, "@");
-						    token2 = strtok(NULL, "@");
-						if(atof(token) > atof(token2))
+
+						int pos = data2.find_last_of("@", data2.length()-2);
+						token2 = data2.substr(pos+1);
+						token2.erase(token2.length()-1);
+		    				if(atof(token.c_str()) > atof(token2.c_str()))
 						    doit=true;
 						data.clear();
 					    }
-					    else if (strstr(miss[activeMission].services[j].output[t].c_str(), token))
+					    else if (miss[activeMission].services[j].output[t].find(token) != string::npos)
 						doit=true;
 					    if(doit){
-						//printf("L1:if taken\n");
+						//printf("Level 1:if taken\n");
 						for(uint16_t l = j+1; l <= j+miss[activeMission].services[j].num_conds; l++){ 
-						    //printf("transacting data for k=%d\n", k);
 						    TransactData(l);
 						}
 					    }
 					    else
-						//printf("L2: if not taken\n");
+						//printf("Level 2:if not taken\n");
 						numstatements[2] +=miss[activeMission].services[j].num_conds+1;
 						j+=miss[activeMission].services[j].num_conds;
-					    //printf("doneif %d, %d, %d\n", numstatements, miss[activeMission].services[i].num_conds, i);
+					    //printf("Level 2:doneif %d, %d, %d\n", numstatements, miss[activeMission].services[i].num_conds, i);
 					}else if(miss[activeMission].services[j].name.compare("dowhile")==0){
 					    numstatements[0]=0;
-					    //printf("L2 while detected\n");
+					    //printf("Level 2:while detected\n");
 					    while(true){
 						uint16_t l;
 						    for(l = j+1; l <= j+miss[activeMission].services[j].num_conds; l++){ 
 							TransactData(l);
-							//printf("l=%d\n", l);
 						    }
 						    data.clear();
-						    //printf("L2:while detected %d, %d\n", k, miss[activeMission].services[i].num_conds);
 						    input.clear();
 						    check.clear();
@@ -1156,12 +1147,13 @@
 							if(!miss[activeMission].services[j].output[t].empty()){
 							    input=miss[activeMission].services[l-1].output[t];
-							    strcpy(_data_DB->command, "SELECT ");
-							    strcat(_data_DB->command, _data_DB->tablename);
-							    strcat(_data_DB->command, ".* from ");
-							    strcat(_data_DB->command, _data_DB->tablename);
-							    strcat(_data_DB->command, " where Tag==");
-							    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str());
+							    _data_DB->command="SELECT ";
+							    _data_DB->command.append(_data_DB->tablename);
+							    _data_DB->command.append(".* from ");
+							    _data_DB->command.append(_data_DB->tablename);
+							    _data_DB->command.append(" where Tag=='");
+							    _data_DB->command.append(input);
+							    _data_DB->command.append("';");
 							    sqlite3_stmt * pStatement;
-							    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
+							    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
 							    if (rc == SQLITE_OK){ 
 								if (sqlite3_step(pStatement) == SQLITE_ROW)
@@ -1172,28 +1164,27 @@
 								}
 							    } else {
-								printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+							  	printf("data_DB:: SQL statement error. rc = %i\n%s\n",rc,_data_DB->command.c_str());
 							    }
 						    	    sqlite3_finalize(pStatement);
-							    //printf("data=%s\n", data.c_str());
-							    token = strtok((char *)data.c_str(), "@");
-							    token = strtok(NULL, "@");
-							    token = strtok(NULL, "@");
-							    //printf("data=%s\n", token);
+							    //printf("Level 2:data=%s\n", data.c_str());
+						    	    int pos = data.find_last_of("@", data.length()-2);
+						            token = data.substr(pos+1);
+						            token.erase(token.length()-1);
+							    //printf("Level 2:token=%s\n", token);
 							    break;
 							}
 						    }
-						    //printf("L2:--- %s  %s---\n", miss[activeMission].services[j].output[t].c_str(), token);
-						    if(strstr(miss[activeMission].services[j].output[t].c_str(), token)){
-							//printf("L2:while taken again!\n");
+						    //printf("Level 2:--- %s  %s---\n", miss[activeMission].services[j].output[t].c_str(), token);
+						    if(miss[activeMission].services[j].output[t].find(token) != string::npos){
+							//printf("Level 2:while taken again!\n");
 						    }
 						    else {
-							//printf("no more while\n");
+							//printf("Level 2:no more while\n");
 							break;}
 					    }
 					    j+=miss[activeMission].services[j].num_conds;
-					    //printf("doneif\n");
 					}
 					else{
-					    //printf("NO L2 COND!\n");
+					    //printf("Level 2: no conditional\n");
 					    numstatements[2]=0;
 				    	    TransactData(j);
@@ -1202,5 +1193,5 @@
 			    }
 			    else{
-				//printf("L1: if not taken\n");
+				//printf("Level 1: if not taken\n");
 			    }
 			        numstatements[1] +=miss[activeMission].services[k].num_conds+1;
@@ -1209,5 +1200,5 @@
 			}else if(miss[activeMission].services[k].name.compare("dowhile")==0){
 			    numstatements[0]=0;
-			    //printf("L1 while detected\n");
+			    //printf("Level 1: while detected\n");
 			    while(true){
 				uint16_t j;
@@ -1216,5 +1207,4 @@
 				    }
 				    data.clear();
-				    //printf("L1:while detected %d, %d\n", k, miss[activeMission].services[i].num_conds);
 				    input.clear();
 				    check.clear();
@@ -1223,13 +1213,13 @@
 					if(!miss[activeMission].services[k].output[t].empty()){
 					    input=miss[activeMission].services[j-1].output[t];
-					    //printf("input=%s\n", input.c_str());
-					    strcpy(_data_DB->command, "SELECT ");
-					    strcat(_data_DB->command, _data_DB->tablename);
-					    strcat(_data_DB->command, ".* from ");
-					    strcat(_data_DB->command, _data_DB->tablename);
-					    strcat(_data_DB->command, " where Tag==");
-					    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str());
+					    _data_DB->command="SELECT ";
+					    _data_DB->command.append(_data_DB->tablename);
+					    _data_DB->command.append(".* from ");
+					    _data_DB->command.append(_data_DB->tablename);
+					    _data_DB->command.append(" where Tag=='");
+					    _data_DB->command.append(input);
+					    _data_DB->command.append("';");
 					    sqlite3_stmt * pStatement;
-					    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
+					    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
 					    if (rc == SQLITE_OK){ 
 						if (sqlite3_step(pStatement) == SQLITE_ROW)
@@ -1240,47 +1230,39 @@
 						}
 					    } else {
-						printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+						printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
 					    }
 				    	    sqlite3_finalize(pStatement);
-					    //printf("data=%s\n", data.c_str());
-					    token = strtok((char *)data.c_str(), "@");
-					    token = strtok(NULL, "@");
-					    token = strtok(NULL, "@");
-					    //printf("data=%s\n", token);
+					    //printf("Level 1:data=%s\n", data.c_str());
+					    int pos = data.find_last_of("@", data.length()-2);
+					    token = data.substr(pos+1);
+					    token.erase(token.length()-1);
+					    //printf("Level 1:token=%s\n", token);
 					    break;
 					}
 				    }
-				    //printf("L1:--- %s  %s---\n", miss[activeMission].services[k].output[t].c_str(), token);
-				    if(strstr(miss[activeMission].services[k].output[t].c_str(), token)){
-					//printf("L1:while taken again!\n");
+				    //printf("Level 1:--- %s  %s---\n", miss[activeMission].services[k].output[t].c_str(), token);
+				    if(miss[activeMission].services[k].output[t].find(token) != string::npos){
+					//printf("Level 1:while taken again!\n");
 				    }
 				    else {
-					//printf("no more while\n");
+					//printf("Level 1:While finished\n");
 					break;}
 			    }
 			    k+=miss[activeMission].services[k].num_conds;
-			    //printf("doneif\n");
 			}
 			else{
-			    //printf("NO L1 COND!\n");
+			    //printf("Level1:No conditional\n");
 			    numstatements[1]=0;
 		    	    TransactData(k);
 			}
-		
-            		//numstatements[0] +=miss[activeMission].services[i].num_conds+1;
-	    		//i+=miss[activeMission].services[i].num_conds;
-	    //printf("doneif %d, %d, %d\n", numstatements, miss[activeMission].services[i].num_conds, i);
 		}
 	    }
-	   // else
-		//printf("LO if not taken\n");
             numstatements[0] +=miss[activeMission].services[i].num_conds+1;
 	    i+=miss[activeMission].services[i].num_conds;
-	    //printf("doneif %d, %d, %d\n", numstatements, miss[activeMission].services[i].num_conds, i);
 	}
 	else if(miss[activeMission].services[i].name.compare("dowhile")==0)
 	{
 	    numstatements[0]=0;
-	    //printf("L0 while detected\n");
+	    //printf("Level 0: while detected\n");
 	    while(true){
 		uint16_t k;
@@ -1295,13 +1277,13 @@
 			if(!miss[activeMission].services[i].output[t].empty()){
 			    input=miss[activeMission].services[k-1].output[t];
-			    //printf("input=%s\n", input.c_str());
-			    strcpy(_data_DB->command, "SELECT ");
-			    strcat(_data_DB->command, _data_DB->tablename);
-			    strcat(_data_DB->command, ".* from ");
-			    strcat(_data_DB->command, _data_DB->tablename);
-			    strcat(_data_DB->command, " where Tag==");
-			    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str());
+			    _data_DB->command="SELECT ";
+			    _data_DB->command.append(_data_DB->tablename);
+			    _data_DB->command.append(".* from ");
+			    _data_DB->command.append(_data_DB->tablename);
+			    _data_DB->command.append(" where Tag=='");
+			    _data_DB->command.append(input);
+			    _data_DB->command.append("';");
 			    sqlite3_stmt * pStatement;
-			    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
+			    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
 			    if (rc == SQLITE_OK){ 
 				if (sqlite3_step(pStatement) == SQLITE_ROW)
@@ -1312,27 +1294,26 @@
 				}
 			    } else {
-				printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+				printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
 			    }
 		    	    sqlite3_finalize(pStatement);
-			    token = strtok((char *)data.c_str(), "@");
-			    token = strtok(NULL, "@");
-			    token = strtok(NULL, "@");
+			    int pos = data.find_last_of("@", data.length()-2);
+			    token = data.substr(pos+1);
+			    token.erase(token.length()-1);
 			    break;
 			}
 		    }
-		    //printf("L0:--- %s  %s---\n", miss[activeMission].services[i].output[t].c_str(), token);
-		    if(strstr(miss[activeMission].services[i].output[t].c_str(), token)){
-			//printf("L0:while taken again!\n");
+		    //printf("Level 0:--- %s  %s---\n", miss[activeMission].services[i].output[t].c_str(), token);
+		    if(miss[activeMission].services[i].output[t].find(token) != string::npos){
+			//printf("Level 0:while taken again!\n");
 		    }
 		    else {
-			//printf("no more while\n");
+			//printf("Level 0:no more while\n");
 			break;}
 	    }
 	    i+=miss[activeMission].services[i].num_conds;
-	    //printf("doneif\n");
 	}
 	else{
 	    numstatements[0]=0;
-	    //printf("L0 Neither if nor while\n");
+	    //printf("Level 0: No conditional\n");
 	    TransactData(i);}
 	i++;
@@ -1340,6 +1321,4 @@
     i=0;
     data.clear();
-
-
 
     if(!shellFound)
@@ -1352,5 +1331,4 @@
 	SendMessage(shellSocketFD, buffer);
 	for(int t = 0; t < k; t++){
-	    //printf("---%s---\n", miss[activeMission].input[t].c_str());
 	    SendMessage(shellSocketFD, miss[activeMission].input[t].c_str()); 
 	    SendMessage(shellSocketFD, "0");
@@ -1385,16 +1363,16 @@
 ServiceManagementLayer::ListServices()
 {
-    strcpy(_services_DB->command, "select ");
-    strcat(_services_DB->command, _services_DB->tablename);
-    strcat(_services_DB->command, ".* from ");
-    strcat(_services_DB->command, _services_DB->tablename);
-    strcat(_services_DB->command, ";");
+    _services_DB->command="select ";
+    _services_DB->command.append(_services_DB->tablename);
+    _services_DB->command.append(".* from ");
+    _services_DB->command.append(_services_DB->tablename);
+    _services_DB->command.append(";");
 
     // execute print (select all)  command    
     char *errorMsg;
-    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg);
+    int rc = sqlite3_exec(_services_DB->db, _services_DB->command.c_str(), callback, 0, &errorMsg);
     if( rc!=SQLITE_OK && rc!=101 )
         fprintf(stderr, "SQL error: %s\n", errorMsg);
-    printf("database %s, table %s:\n", _services_DB->filename, _services_DB->tablename);
+    printf("database %s, table %s:\n", _services_DB->filename.c_str(), _services_DB->tablename.c_str());
 }
 
@@ -1623,15 +1601,19 @@
     memset(buffer, 0, 256);
     ReadMessage(CE_List[ID].FD, buffer); 
-    strcpy(_services_DB->command, "DELETE FROM ");
-    strcat(_services_DB->command, _services_DB->tablename);
-    strcat(_services_DB->command, " WHERE ID_Num IN (SELECT");
-    sprintf(_services_DB->command, " %s %d",_services_DB->command, ID);
-    strcat(_services_DB->command, " FROM ");
-    strcat(_services_DB->command, _services_DB->tablename);
-    strcat(_services_DB->command, " WHERE Service_Name");
-    strcat(_services_DB->command, "==");
-    sprintf(_services_DB->command, "%s'%s');", _services_DB->command, buffer);
+    _services_DB->command="DELETE FROM ";
+    _services_DB->command.append(_services_DB->tablename);
+    _services_DB->command.append(" WHERE ID_Num IN (SELECT ");
+    char tmp[3];
+    memset(tmp,0,3);
+    sprintf(tmp, "%d", ID);
+    _services_DB->command.append(tmp);
+    _services_DB->command.append(" FROM ");
+    _services_DB->command.append(_services_DB->tablename);
+    _services_DB->command.append(" WHERE Service_Name");
+    _services_DB->command.append("=='");
+    _services_DB->command.append(buffer);
+    _services_DB->command.append("');");
     char *errorMsg;
-    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg);
+    int rc = sqlite3_exec(_services_DB->db, _services_DB->command.c_str(), callback, 0, &errorMsg);
     if( rc!=SQLITE_OK && rc!=101 )
         fprintf(stderr, "SQL error: %s\n", errorMsg);
@@ -1664,12 +1646,16 @@
 
     //Deregister the services
-    strcpy(_services_DB->command, "DELETE FROM ");
-    strcat(_services_DB->command, _services_DB->tablename);
-    strcat(_services_DB->command, " WHERE ");
-    strcat(_services_DB->command, "ID_Num");
-    strcat(_services_DB->command, "==");
-    sprintf(_services_DB->command, "%s%d;", _services_DB->command, ID);
+    _services_DB->command="DELETE FROM ";
+    _services_DB->command.append(_services_DB->tablename);
+    _services_DB->command.append(" WHERE ");
+    _services_DB->command.append("ID_Num");
+    _services_DB->command.append("==");
+    char tmp[3];
+    memset(tmp,0,3);
+    sprintf(tmp, "%d", ID);
+    _services_DB->command.append(tmp);
+    _services_DB->command.append(";");
     char *errorMsg;
-    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg);
+    int rc = sqlite3_exec(_services_DB->db, _services_DB->command.c_str(), callback, 0, &errorMsg);
     if( rc!=SQLITE_OK && rc!=101 )
         fprintf(stderr, "SQL error: %s\n", errorMsg);
