1 | /***************************************
2 | $Revision: 1.9 $
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(void *data, void *ptr)
37 | {
38 | Attribute_t *attr = data;
39 |
40 | free(attr->value);
41 | free(attr);
42 | }
43 |
44 |
45 | Attribute_t *attribute_upd(Attribute_t *attr, int newtype, char *newvalue)
46 | {
47 | attr->type=newtype;
48 | free(attr->value);
49 | attr->value=g_strdup(newvalue);
50 | return(attr);
51 | }
52 |
53 | Attribute_t *attribute_new1(int type, const char *value)
54 | {
55 | char *n;
56 | Attribute_t *attr = NULL;
57 |
58 | attr = (Attribute_t *)calloc(1, sizeof(Attribute_t)+1);
59 | attr->type = type;
60 |
61 | /* check for end-of-line comments */
62 | n = index(value, '#');
63 | /* if there is no comment check for trailing \n */
64 | if(n == NULL) n = index(value, '\n');
65 | /* now copy the clean value into the attribute */
66 | if(n == NULL) attr->value = g_strdup(value);
67 | else attr->value = g_strndup(value, (n - value));
68 | /* Strip the whitespace */
69 | g_strstrip(attr->value);
70 | return(attr);
71 |
72 | }
73 |
74 | Attribute_t *attribute_new(const char *line) {
75 | Attribute_t *attr = NULL;
76 | int type;
77 | char *colon;
78 | gchar *token;
79 |
80 |
81 | colon = index(line, ':');
82 | if (colon != NULL) {
83 | if (line[0] =='*') {
84 | token = g_strndup(line+1, 2);
85 | type = DF_attribute_code2type(token);
86 | }
87 | else {
88 | token = g_strndup(line, (colon - line));
89 | type = DF_attribute_name2type(token);
90 | }
91 | if(token)free(token);
92 |
93 | colon+=2;
94 | if (type >= 0) {
95 | attr=attribute_new1(type, colon);
96 | }
97 | else {
98 | /* fprintf(stderr, "ERROR: Bad attribute: %s\n", token);*/
99 | }
100 | }
101 | else {
102 | /* fprintf(stderr, "ERROR: Not an attribute: %s\n", line);*/
103 | }
104 | return attr;
105 | } /* attribute_new() */
106 |
107 |
108 | void object_free(Object_t *obj) {
109 | if (obj) {
110 | g_slist_foreach(obj->attributes, attribute_free, NULL);
111 | g_slist_free(obj->attributes);
112 | g_string_free(obj->object, TRUE);
113 | free(obj);
114 | }
115 | } /* object_free() */
116 |
117 | Object_t *object_new(const char *line) {
118 | Object_t *obj = NULL;
119 |
120 | int type;
121 | char *colon;
122 | gchar *token;
123 |
124 | colon = index(line, ':');
125 | if (colon != NULL) {
126 | if (line[0] =='*') {
127 | token = g_strndup(line+1, 2);
128 | type = DF_class_code2type(token);
129 | }
130 | else {
131 | token = g_strndup(line, (colon - line));
132 | type = DF_class_name2type(token);
133 | }
134 | if(token)free(token);
135 |
136 | if (type >= 0) {
137 | obj = (Object_t *)calloc(1, sizeof(Object_t)+1);
138 | obj->attributes = NULL;
139 | obj->object = g_string_sized_new(STR_XXXL);
140 | obj->type = type;
141 | }
142 | else {
143 | /* fprintf(stderr, "ERROR: Object has an invalid class: %s\n");*/
144 | }
145 | }
146 | else {
147 | /* fprintf(stderr, "ERROR: No colon found in line: %s\n", line);*/
148 | }
149 |
150 | return obj;
151 | } /* object_new() */
152 |
153 |
154 |
155 |
156 | /************************************************************
157 | *void transaction_free(Transaction_t *tr)
158 | *
159 | * **********************************************************/
160 | void transaction_free(Transaction_t *tr) {
161 | if(tr) {
162 | g_string_free(tr->error_script, TRUE);
163 | /* free nic_handle_t structure used for NHR stuff */
164 | if(tr->nh)free_nh(tr->nh);
165 | if(tr->save)free(tr->save);
166 | free(tr);
167 | }
168 | } /* transaction_free() */
169 |
170 | /************************************************************
171 | *Transaction_t *transaction_new(SQ_connection_t *sql_connection, C_Type_t class_type)
172 | *
173 | * **********************************************************/
174 | Transaction_t *transaction_new(SQ_connection_t *sql_connection, C_Type_t class_type) {
175 | Transaction_t *tr = (Transaction_t *)calloc(1, sizeof(Transaction_t));
176 |
177 | if (tr != NULL) {
178 | tr->sql_connection = sql_connection;
179 | tr->class_type = class_type;
180 | tr->thread_upd=TR_UPDATE;
181 | tr->thread_ins=TR_INSERT;
182 | tr->object_id = 0; /* Just in case*/
183 | tr->succeeded = 1;
184 | tr->error=0;
185 | tr->error_script = g_string_sized_new(STR_XL);
186 | tr->dummy = 0; /* By default do not create dummies except for sets*/
187 | tr->ndummy=0;
188 | tr->action=100;
189 | tr->load_pass=0; /* by default*/
190 | tr->sequence_id=1; /* we start from 1*/
191 | tr->save=NULL;
192 | tr->nh=NULL;
193 | tr->packptr=NULL;
194 | }
195 | return tr;
196 | } /* transaction_new() */