1 | /*************************************** 2 | $Revision: 1.11 $ 3 | 4 | Miscellaneous functions to support UD 5 | 6 | Status: NOT REVUED, NOT TESTED 7 | 8 | Author(s): Chris Ottrey, Andrei Robachevsky 9 | 10 | ******************/ /****************** 11 | Modification History: 12 | andrei (17/01/2000) Created. 13 | ******************/ /****************** 14 | Copyright (c) 2000 RIPE NCC 15 | 16 | All Rights Reserved 17 | 18 | Permission to use, copy, modify, and distribute this software and its 19 | documentation for any purpose and without fee is hereby granted, 20 | provided that the above copyright notice appear in all copies and that 21 | both that copyright notice and this permission notice appear in 22 | supporting documentation, and that the name of the author not be 23 | used in advertising or publicity pertaining to distribution of the 24 | software without specific, written prior permission. 25 | 26 | THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 27 | ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL 28 | AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY 29 | DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 30 | AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 31 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 32 | ***************************************/ 33 | #include "ud.h" 34 | #include "ud_int.h" 35 | /************************************************************ 36 | * void attribute_free() * 37 | * * 38 | * Frees memory allocated for an attribute * 39 | * * 40 | ************************************************************/ 41 | void attribute_free(void *data, void *ptr) 42 | { 43 | Attribute_t *attr = data; 44 | 45 | free(attr->value); 46 | free(attr); 47 | } 48 | 49 | /************************************************************ 50 | * Attribute_t *attribute_upd() * 51 | * * 52 | * Changes the value of an attribute * 53 | * * 54 | ************************************************************/ 55 | Attribute_t *attribute_upd(Attribute_t *attr, int newtype, char *newvalue) 56 | { 57 | attr->type=newtype; 58 | free(attr->value); 59 | attr->value=g_strdup(newvalue); 60 | return(attr); 61 | } 62 | 63 | /************************************************************ 64 | * Attribute_t *attribute_new1() * 65 | * * 66 | * Creates an attribute and fills the type and value * 67 | * * 68 | ************************************************************/ 69 | Attribute_t *attribute_new1(int type, const char *value) 70 | { 71 | char *n; 72 | Attribute_t *attr = NULL; 73 | 74 | attr = (Attribute_t *)calloc(1, sizeof(Attribute_t)+1); 75 | attr->type = type; 76 | 77 | /* check for end-of-line comments */ 78 | n = index(value, '#'); 79 | /* if there is no comment check for trailing \n */ 80 | if(n == NULL) n = index(value, '\n'); 81 | /* now copy the clean value into the attribute */ 82 | if(n == NULL) attr->value = g_strdup(value); 83 | else attr->value = g_strndup(value, (n - value)); 84 | /* Strip the whitespace */ 85 | g_strstrip(attr->value); 86 | return(attr); 87 | 88 | } 89 | /************************************************************ 90 | * Attribute_t *attribute_new() * 91 | * * 92 | * Determines the type and value of an attribute and * 93 | * creates it by calling attribute_new1() * 94 | * * 95 | ************************************************************/ 96 | Attribute_t *attribute_new(const char *line) { 97 | Attribute_t *attr = NULL; 98 | int type; 99 | char *colon; 100 | gchar *token; 101 | 102 | 103 | colon = index(line, ':'); 104 | if (colon != NULL) { 105 | if (line[0] =='*') { 106 | token = g_strndup(line+1, 2); 107 | type = DF_attribute_code2type(token); 108 | } 109 | else { 110 | token = g_strndup(line, (colon - line)); 111 | type = DF_attribute_name2type(token); 112 | } 113 | if(token)free(token); 114 | 115 | colon+=2; 116 | if (type >= 0) { 117 | attr=attribute_new1(type, colon); 118 | } 119 | } 120 | return attr; 121 | } /* attribute_new() */ 122 | 123 | /************************************************************ 124 | * void object_free() * 125 | * * 126 | * Frees memory allocated for an object * 127 | * * 128 | ************************************************************/ 129 | void object_free(Object_t *obj) { 130 | if (obj) { 131 | g_slist_foreach(obj->attributes, attribute_free, NULL); 132 | g_slist_free(obj->attributes); 133 | g_string_free(obj->object, TRUE); 134 | free(obj); 135 | } 136 | } /* object_free() */ 137 | 138 | /************************************************************ 139 | * Object_t *object_new() * 140 | * * 141 | * Determines the class type and creates an object * 142 | * * 143 | ************************************************************/ 144 | Object_t *object_new(const char *line) { 145 | Object_t *obj = NULL; 146 | 147 | int type; 148 | char *colon; 149 | gchar *token=NULL; 150 | 151 | colon = index(line, ':'); 152 | if (colon != NULL) { 153 | if (line[0] =='*') { 154 | token = g_strndup(line+1, 2); 155 | type = DF_class_code2type(token); 156 | } 157 | else { 158 | token = g_strndup(line, (colon - line)); 159 | type = DF_class_name2type(token); 160 | } 161 | if(token)free(token); 162 | 163 | if (type >= 0) { 164 | obj = (Object_t *)calloc(1, sizeof(Object_t)+1); 165 | obj->attributes = NULL; 166 | obj->object = g_string_sized_new(STR_XXXL); 167 | obj->type = type; 168 | } 169 | } 170 | return obj; 171 | } /* object_new() */ 172 | 173 | 174 | /************************************************************ 175 | * void transaction_free() * 176 | * * 177 | * Frees memory allocated for a transaction * 178 | * * 179 | ************************************************************/ 180 | 181 | void transaction_free(Transaction_t *tr) { 182 | if(tr) { 183 | g_string_free(tr->error_script, TRUE); 184 | /* free nic_handle_t structure used for NHR stuff */ 185 | if(tr->nh)free_nh(tr->nh); 186 | if(tr->save)free(tr->save); 187 | if(tr->packptr)free(tr->packptr); 188 | free(tr); 189 | } 190 | } /* transaction_free() */ 191 | 192 | /************************************************************ 193 | * Transaction_t *transaction_new() * 194 | * * 195 | * Creates a transaction * 196 | * * 197 | ************************************************************/ 198 | Transaction_t *transaction_new(SQ_connection_t *sql_connection, C_Type_t class_type) { 199 | Transaction_t *tr = (Transaction_t *)calloc(1, sizeof(Transaction_t)); 200 | 201 | if (tr != NULL) { 202 | tr->sql_connection = sql_connection; 203 | tr->class_type = class_type; 204 | tr->thread_upd=TR_UPDATE; 205 | tr->thread_ins=TR_INSERT; 206 | tr->object_id = 0; /* Just in case*/ 207 | tr->succeeded = 1; 208 | tr->error=0; 209 | tr->error_script = g_string_sized_new(STR_XL); 210 | tr->dummy = 0; /* By default do not create dummies except for sets*/ 211 | tr->ndummy=0; 212 | tr->action=100; 213 | tr->load_pass=0; /* by default*/ 214 | tr->sequence_id=1; /* we start from 1*/ 215 | tr->save=NULL; 216 | tr->nh=NULL; 217 | tr->packptr=calloc(1, sizeof(rp_upd_pack_t)); 218 | 219 | } 220 | return tr; 221 | } /* transaction_new() */