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