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