/home/dko/projects/mobilec/tags/MobileC-v1.10.2/MobileC-v1.10.2/src/agent_datastate.c

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

Generated on Fri Jul 11 17:59:44 2008 for Mobile-C by  doxygen 1.5.4