/home/dko/projects/mobilec/trunk/src/agent_datastate.c

Go to the documentation of this file.
00001 /*[
00002  * Copyright (c) 2007 Integration Engineering Laboratory
00003                       University of California, Davis
00004  *
00005  * Permission to use, copy, and distribute this software and its
00006  * documentation for any purpose with or without fee is hereby granted,
00007  * provided that the above copyright notice appear in all copies and
00008  * that both that copyright notice and this permission notice appear
00009  * in supporting documentation.
00010  *
00011  * Permission to modify the software is granted, but not the right to
00012  * distribute the complete modified source code.  Modifications are to
00013  * be distributed as patches to the released version.  Permission to
00014  * distribute binaries produced by compiling modified sources is granted,
00015  * provided you
00016  *   1. distribute the corresponding source modifications from the
00017  *    released version in the form of a patch file along with the binaries,
00018  *   2. add special version identification to distinguish your version
00019  *    in addition to the base release version number,
00020  *   3. provide your name and address as the primary contact for the
00021  *    support of your modified version, and
00022  *   4. retain our contact information in regard to use of the base
00023  *    software.
00024  * Permission to distribute the released version of the source code along
00025  * with corresponding source modifications in the form of a patch file is
00026  * granted with same provisions 2 through 4 for binary distributions.
00027  *
00028  * This software is provided "as is" without express or implied warranty
00029  * to the extent permitted by applicable law.
00030 ]*/
00031 
00032 #include <mxml.h>
00033 #include "include/agent_datastate.h"
00034 #include "include/macros.h"
00035 #include "include/mc_error.h"
00036 
00037 agent_datastate_p
00038 agent_datastate_Copy(const agent_datastate_p datastate)
00039 {
00040   char** code_id_itr;
00041   char** code_itr;
00042   int num_agent_codes = 0;
00043   int i;
00044   agent_datastate_p cp_data;
00045   cp_data = agent_datastate_New();
00046   /* Copy agent_codes */
00047   code_itr = datastate->agent_codes;
00048   /* First, count how many of them there are. */
00049   while (*code_itr != NULL) {
00050     num_agent_codes++;
00051     code_itr++;
00052   }
00053   cp_data->agent_code_ids = (char**) malloc
00054     (
00055      num_agent_codes + 1
00056     );
00057   cp_data->agent_codes = (char**)malloc
00058     (
00059      num_agent_codes + 1
00060     );
00061   code_id_itr = datastate->agent_code_ids;
00062   code_itr = datastate->agent_codes;
00063   i = 0;
00064   while (*code_id_itr != NULL && *code_itr != NULL) {
00065     cp_data->agent_code_ids[i] = malloc
00066       (
00067        sizeof(char) * 
00068        (strlen(*code_id_itr) + 1)
00069       );
00070     strcpy(cp_data->agent_code_ids[i], *code_id_itr);
00071 
00072     cp_data->agent_codes[i] = malloc
00073       (
00074        sizeof(char) * 
00075        (strlen(*code_itr) + 1 )
00076       );
00077     strcpy(cp_data->agent_codes[i], *code_itr);
00078 
00079     i++;
00080     code_id_itr++;
00081     code_itr++;
00082   }
00083   cp_data->agent_code_ids[i] = NULL;
00084   cp_data->agent_codes[i] = NULL;
00085   cp_data->agent_code = cp_data->agent_codes[0];
00086 
00087   cp_data->task_progress = datastate->task_progress;
00088   cp_data->return_data = datastate->return_data;
00089   cp_data->number_of_tasks = datastate->number_of_tasks;
00090   cp_data->persistent = datastate->persistent;
00091   cp_data->init_agent_status = datastate->init_agent_status;
00092 
00093   /* Copy the tasks */
00094   cp_data->tasks = (agent_task_t**)malloc
00095     (
00096      sizeof(agent_task_t*) * cp_data->number_of_tasks
00097     );
00098   for (i = 0; i < cp_data->number_of_tasks; i++) {
00099     cp_data->tasks[i] = agent_task_Copy(datastate->tasks[i]);
00100   }
00101 
00102   return cp_data;
00103 }
00104   
00105 agent_datastate_p
00106 agent_datastate_New( void )
00107 {
00108   agent_datastate_p agent_datastate;
00109   agent_datastate = (agent_datastate_p)malloc(sizeof(agent_datastate_t));
00110   CHECK_NULL(agent_datastate, exit(0););
00111   
00112   agent_datastate->agent_code = NULL;
00113   agent_datastate->tasks = NULL;
00114   agent_datastate->xml_agent_root = NULL;
00115   agent_datastate->xml_root = NULL;
00116   agent_datastate->task_progress = 0;
00117   agent_datastate->return_data = 0;
00118   agent_datastate->number_of_tasks = 0;
00119   agent_datastate->persistent = 0;
00120   agent_datastate->init_agent_status = 0;
00121 
00122   return agent_datastate;
00123 }
00124 
00125 int
00126 agent_datastate_Destroy( agent_datastate_p agent_datastate )
00127 {
00128   int i;
00129   if (agent_datastate->agent_codes != NULL) {
00130     i = 0;
00131     while (agent_datastate->agent_codes[i] != NULL) {
00132       free(agent_datastate->agent_codes[i]);
00133       i++;
00134     }
00135     free(agent_datastate->agent_codes);
00136   }
00137   if (agent_datastate->agent_code_ids != NULL) {
00138     i = 0;
00139     while(agent_datastate->agent_code_ids[i] != NULL) {
00140       free(agent_datastate->agent_code_ids[i]);
00141       i++;
00142     }
00143     free(agent_datastate->agent_code_ids);
00144   }
00145   for 
00146     ( 
00147      i = 0;
00148      i < agent_datastate->number_of_tasks;
00149      i++
00150     )
00151   {
00152     agent_task_Destroy(agent_datastate->tasks[i]);
00153   }
00154   free(agent_datastate->tasks);
00155 
00156   if(agent_datastate->xml_root != NULL) {
00157     mxmlDelete(agent_datastate->xml_root);
00158   }
00159   free(agent_datastate);
00160   return MC_SUCCESS;
00161 }
00162 

Generated on Fri May 16 14:49:54 2008 for Mobile-C by  doxygen 1.5.4