00001 /* SVN FILE INFO 00002 * $Revision: 517 $ : Last Committed Revision 00003 * $Date: 2010-06-11 12:06:47 -0700 (Fri, 11 Jun 2010) $ : 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 #ifndef _WIN32 00036 #include "config.h" 00037 #else 00038 #include "winconfig.h" 00039 #endif 00040 00041 #include <mxml.h> 00042 #include "include/agent_datastate.h" 00043 #include "include/macros.h" 00044 #include "include/mc_error.h" 00045 00046 agent_datastate_p 00047 agent_datastate_Copy(const agent_datastate_p datastate) 00048 { 00049 char** code_id_itr; 00050 char** code_itr; 00051 int num_agent_codes = 0; 00052 int i; 00053 agent_datastate_p cp_data; 00054 cp_data = agent_datastate_New(); 00055 /* Copy agent_codes */ 00056 code_itr = datastate->agent_codes; 00057 /* First, count how many of them there are. */ 00058 while (*code_itr != NULL) { 00059 num_agent_codes++; 00060 code_itr++; 00061 } 00062 cp_data->agent_code_ids = (char**) malloc 00063 ( 00064 num_agent_codes + 1 00065 ); 00066 cp_data->agent_codes = (char**)malloc 00067 ( 00068 num_agent_codes + 1 00069 ); 00070 code_id_itr = datastate->agent_code_ids; 00071 code_itr = datastate->agent_codes; 00072 i = 0; 00073 while (*code_id_itr != NULL && *code_itr != NULL) { 00074 cp_data->agent_code_ids[i] = (char*)malloc 00075 ( 00076 sizeof(char) * 00077 (strlen(*code_id_itr) + 1) 00078 ); 00079 strcpy(cp_data->agent_code_ids[i], *code_id_itr); 00080 00081 cp_data->agent_codes[i] = (char*)malloc 00082 ( 00083 sizeof(char) * 00084 (strlen(*code_itr) + 1 ) 00085 ); 00086 strcpy(cp_data->agent_codes[i], *code_itr); 00087 00088 i++; 00089 code_id_itr++; 00090 code_itr++; 00091 } 00092 cp_data->agent_code_ids[i] = NULL; 00093 cp_data->agent_codes[i] = NULL; 00094 cp_data->agent_code = cp_data->agent_codes[0]; 00095 00096 cp_data->task_progress = datastate->task_progress; 00097 cp_data->return_data = datastate->return_data; 00098 cp_data->number_of_tasks = datastate->number_of_tasks; 00099 cp_data->persistent = datastate->persistent; 00100 cp_data->init_agent_status = datastate->init_agent_status; 00101 00102 /* Copy the tasks */ 00103 cp_data->tasks = (agent_task_t**)malloc 00104 ( 00105 sizeof(agent_task_t*) * cp_data->number_of_tasks 00106 ); 00107 for (i = 0; i < cp_data->number_of_tasks; i++) { 00108 cp_data->tasks[i] = agent_task_Copy(datastate->tasks[i]); 00109 } 00110 00111 return cp_data; 00112 } 00113 00114 agent_datastate_p 00115 agent_datastate_New( void ) 00116 { 00117 agent_datastate_p agent_datastate; 00118 agent_datastate = (agent_datastate_p)malloc(sizeof(agent_datastate_t)); 00119 CHECK_NULL(agent_datastate, exit(0);); 00120 00121 agent_datastate->agent_code = NULL; 00122 agent_datastate->tasks = NULL; 00123 agent_datastate->xml_agent_root = NULL; 00124 agent_datastate->xml_root = NULL; 00125 agent_datastate->task_progress = 0; 00126 agent_datastate->return_data = 0; 00127 agent_datastate->number_of_tasks = 0; 00128 agent_datastate->persistent = 0; 00129 agent_datastate->init_agent_status = 0; 00130 agent_datastate->progress_modifier = 0; 00131 00132 return agent_datastate; 00133 } 00134 00135 int 00136 agent_datastate_Destroy( agent_datastate_p agent_datastate ) 00137 { 00138 int i; 00139 if(agent_datastate == NULL) {return 0;} 00140 if (agent_datastate->agent_codes != NULL) { 00141 i = 0; 00142 while (agent_datastate->agent_codes[i] != NULL) { 00143 free(agent_datastate->agent_codes[i]); 00144 i++; 00145 } 00146 free(agent_datastate->agent_codes); 00147 } 00148 if (agent_datastate->agent_code_ids != NULL) { 00149 i = 0; 00150 while(agent_datastate->agent_code_ids[i] != NULL) { 00151 free(agent_datastate->agent_code_ids[i]); 00152 i++; 00153 } 00154 free(agent_datastate->agent_code_ids); 00155 } 00156 for 00157 ( 00158 i = 0; 00159 i < agent_datastate->number_of_tasks; 00160 i++ 00161 ) 00162 { 00163 agent_task_Destroy(agent_datastate->tasks[i]); 00164 } 00165 free(agent_datastate->tasks); 00166 00167 if(agent_datastate->xml_root != NULL) { 00168 mxmlDelete(agent_datastate->xml_root); 00169 } 00170 free(agent_datastate); 00171 return MC_SUCCESS; 00172 } 00173