XTA: API Reference for C language

xta_xid.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2019, Christian Ferrari <tiian@users.sourceforge.net>
3  * All rights reserved.
4  *
5  * This file is part of LIXA.
6  *
7  * LIXA is free software: you can redistribute this file and/or modify
8  * it under the terms of the GNU Lesser General Public License version 2.1 as
9  * published by the Free Software Foundation.
10  *
11  * LIXA is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with LIXA. If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "config.h"
20 
21 
22 
23 #ifdef HAVE_ERRNO_H
24 # include <errno.h>
25 #endif
26 /* system includes */
27 #ifdef HAVE_GLIB_H
28 # include <glib.h>
29 #endif
30 /* LIXA includes */
31 #include "lixa_errors.h"
32 #include "lixa_trace.h"
33 #include "lixa_xid.h"
34 /* XTA includes */
35 #include "xta_xid.h"
36 
37 
38 
39 /* set module trace flag */
40 #ifdef LIXA_TRACE_MODULE
41 # undef LIXA_TRACE_MODULE
42 #endif /* LIXA_TRACE_MODULE */
43 #define LIXA_TRACE_MODULE LIXA_TRACE_MOD_XTA
44 
45 
46 
47 xta_xid_t *xta_xid_new(const char *branch_qualifier,
48  int multiple_branches)
49 {
50  enum Exception { STRDUP_ERROR
51  , G_TRY_MALLOC_ERROR
52  , NONE } excp;
53  int ret_cod = LIXA_RC_INTERNAL_ERROR;
54  xta_xid_t *this = NULL;
55  char *tmp_branch_qualifier = NULL;
56 
57  LIXA_TRACE(("xta_xid_new: branch_qualifier=%s, multiple_branches=%d\n",
58  branch_qualifier, multiple_branches));
59  TRY {
60  uuid_t tmp_bqual;
61 
62  /* duplicate branch_qualifier */
63  if (NULL == (tmp_branch_qualifier = strdup(branch_qualifier)))
64  THROW(STRDUP_ERROR);
65  /* allocate the object */
66  if (NULL == (this = (xta_xid_t *)
67  g_try_malloc0(sizeof(xta_xid_t))))
68  THROW(G_TRY_MALLOC_ERROR);
69  /* set the first HEX digit of tmp_branch_qualifier according to
70  multiple_branches flag */
71  if (multiple_branches && !xta_xid_branch_qualifier_is_multibranch(
72  tmp_branch_qualifier))
73  xta_xid_branch_qualifier_set_multibranch(tmp_branch_qualifier);
74  else if (!multiple_branches && xta_xid_branch_qualifier_is_multibranch(
75  tmp_branch_qualifier))
76  xta_xid_branch_qualifier_unset_multibranch(tmp_branch_qualifier);
77  /* convert from ASCII HEX to binary hex */
78  lixa_xid_set_bqual(tmp_branch_qualifier, tmp_bqual);
79  /* initialize the object */
80  lixa_xid_create_new(tmp_bqual, &this->xa_xid);
81 
82  THROW(NONE);
83  } CATCH {
84  switch (excp) {
85  case STRDUP_ERROR:
86  ret_cod = LIXA_RC_STRDUP_ERROR;
87  break;
88  case G_TRY_MALLOC_ERROR:
90  break;
91  case NONE:
92  ret_cod = LIXA_RC_OK;
93  break;
94  default:
95  ret_cod = LIXA_RC_INTERNAL_ERROR;
96  } /* switch (excp) */
97  } /* TRY-CATCH */
98  /* memory recovery */
99  if (NULL != tmp_branch_qualifier) {
100  free(tmp_branch_qualifier);
101  tmp_branch_qualifier = NULL;
102  }
103  LIXA_TRACE(("xta_xid_new/excp=%d/"
104  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
105  return this;
106 }
107 
108 
109 
110 int xta_xid_branch_qualifier_is_multibranch(const char *branch_qualifier)
111 {
112  int ret_value = FALSE;
113  switch (branch_qualifier[0]) {
114  case '0':
115  case '2':
116  case '4':
117  case '6':
118  case '8':
119  case 'a':
120  case 'A':
121  case 'c':
122  case 'C':
123  case 'e':
124  case 'E':
125  ret_value = TRUE;
126  break;
127  default:
128  break;
129  } /* switch (branch_qualifier[0]) */
130  LIXA_TRACE(("xta_xid_branch_qualifier_is_multibranch: "
131  "branch_qualifier=%s, ret_value=%d\n", branch_qualifier,
132  ret_value));
133  return ret_value;
134 }
135 
136 
137 
138 int xta_xid_branch_qualifier_set_multibranch(char *branch_qualifier)
139 {
140  int ret_value = FALSE;
141  LIXA_TRACE(("xta_xid_branch_qualifier_set_multibranch: "
142  "branch_qualifier=%s\n", branch_qualifier));
143  switch (branch_qualifier[0]) {
144  case '0':
145  break;
146  case '1':
147  branch_qualifier[0] = '0';
148  ret_value = TRUE;
149  break;
150  case '2':
151  break;
152  case '3':
153  branch_qualifier[0] = '2';
154  ret_value = TRUE;
155  break;
156  case '4':
157  break;
158  case '5':
159  branch_qualifier[0] = '4';
160  ret_value = TRUE;
161  break;
162  case '6':
163  break;
164  case '7':
165  branch_qualifier[0] = '6';
166  ret_value = TRUE;
167  break;
168  case '8':
169  break;
170  case '9':
171  branch_qualifier[0] = '8';
172  ret_value = TRUE;
173  break;
174  case 'a':
175  case 'A':
176  break;
177  case 'b':
178  branch_qualifier[0] = 'a';
179  ret_value = TRUE;
180  break;
181  case 'B':
182  branch_qualifier[0] = 'A';
183  ret_value = TRUE;
184  break;
185  case 'c':
186  case 'C':
187  break;
188  case 'd':
189  branch_qualifier[0] = 'c';
190  ret_value = TRUE;
191  break;
192  case 'D':
193  branch_qualifier[0] = 'C';
194  ret_value = TRUE;
195  break;
196  case 'e':
197  case 'E':
198  break;
199  case 'f':
200  branch_qualifier[0] = 'e';
201  ret_value = TRUE;
202  break;
203  case 'F':
204  branch_qualifier[0] = 'E';
205  ret_value = TRUE;
206  break;
207  default:
208  LIXA_TRACE(("xta_xid_branch_qualifier_set_multibranch: "
209  "branch_qualifier[0]='%c' (%s), this is an INTERNAL "
210  "ERROR\n", branch_qualifier[0], branch_qualifier));
211  } /* switch (branch_qualifier[0]) */
212  LIXA_TRACE(("xta_xid_branch_qualifier_set_multibranch: "
213  "branch_qualifier=%s\n", branch_qualifier));
214  return ret_value;
215 }
216 
217 
218 
220 {
221  int ret_value = FALSE;
222  LIXA_TRACE(("xta_xid_branch_qualifier_unset_multibranch: "
223  "branch_qualifier=%s\n", branch_qualifier));
224  switch (branch_qualifier[0]) {
225  case '0':
226  branch_qualifier[0] = '1';
227  ret_value = TRUE;
228  break;
229  case '1':
230  break;
231  case '2':
232  branch_qualifier[0] = '3';
233  ret_value = TRUE;
234  break;
235  case '3':
236  break;
237  case '4':
238  branch_qualifier[0] = '5';
239  ret_value = TRUE;
240  break;
241  case '5':
242  break;
243  case '6':
244  branch_qualifier[0] = '7';
245  ret_value = TRUE;
246  break;
247  case '7':
248  break;
249  case '8':
250  branch_qualifier[0] = '9';
251  ret_value = TRUE;
252  break;
253  case '9':
254  break;
255  case 'a':
256  case 'A':
257  branch_qualifier[0] = 'B';
258  ret_value = TRUE;
259  break;
260  case 'b':
261  case 'B':
262  break;
263  case 'c':
264  case 'C':
265  branch_qualifier[0] = 'D';
266  ret_value = TRUE;
267  break;
268  case 'd':
269  case 'D':
270  break;
271  case 'e':
272  case 'E':
273  branch_qualifier[0] = 'F';
274  ret_value = TRUE;
275  break;
276  case 'f':
277  case 'F':
278  break;
279  default:
280  LIXA_TRACE(("xta_xid_branch_qualifier_unset_multibranch: "
281  "branch_qualifier[0]='%c' (%s), this is an INTERNAL "
282  "ERROR\n", branch_qualifier[0], branch_qualifier));
283  } /* switch (branch_qualifier[0]) */
284  LIXA_TRACE(("xta_xid_branch_qualifier_unset_multibranch: "
285  "branch_qualifier=%s\n", branch_qualifier));
286  return ret_value;
287 }
288 
289 
290 
291 xta_xid_t *xta_xid_new_from_string(const char *xid_string)
292 {
293  enum Exception { G_TRY_MALLOC_ERROR
294  , LIXA_XID_DESERIALIZE_ERROR
295  , NONE } excp;
296  int ret_cod = LIXA_RC_INTERNAL_ERROR;
297  xta_xid_t *this = NULL;
298 
299  LIXA_TRACE(("xta_xid_new_from_string\n"));
300  TRY {
301  /* allocate the object */
302  if (NULL == (this = (xta_xid_t *)
303  g_try_malloc0(sizeof(xta_xid_t))))
304  THROW(G_TRY_MALLOC_ERROR);
305  /* deserialize XID */
306  if (!lixa_xid_deserialize(&this->xa_xid, xid_string))
307  THROW(LIXA_XID_DESERIALIZE_ERROR);
308 
309  THROW(NONE);
310  } CATCH {
311  switch (excp) {
312  case G_TRY_MALLOC_ERROR:
313  ret_cod = LIXA_RC_G_TRY_MALLOC_ERROR;
314  break;
315  case LIXA_XID_DESERIALIZE_ERROR:
316  ret_cod = LIXA_RC_MALFORMED_XID;
317  break;
318  case NONE:
319  ret_cod = LIXA_RC_OK;
320  break;
321  default:
322  ret_cod = LIXA_RC_INTERNAL_ERROR;
323  } /* switch (excp) */
324  } /* TRY-CATCH */
325  /* clean-up memory in the event of an error */
326  if (NONE != excp) {
327  g_free(this);
328  this = NULL;
329  }
330  LIXA_TRACE(("xta_xid_new_from_string/excp=%d/"
331  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
332  return this;
333 }
334 
335 
336 
338 {
339  enum Exception { NULL_OBJECT
340  , G_TRY_MALLOC_ERROR
341  , NONE } excp;
342  int ret_cod = LIXA_RC_INTERNAL_ERROR;
343  xta_xid_t *this = NULL;
344 
345  LIXA_TRACE(("xta_xid_new_from_XID\n"));
346  TRY {
347  /* check the passed xid */
348  if (NULL == xid)
349  THROW(NULL_OBJECT);
350  /* allocate the object */
351  if (NULL == (this = (xta_xid_t *)
352  g_try_malloc0(sizeof(xta_xid_t))))
353  THROW(G_TRY_MALLOC_ERROR);
354  /* copy XID */
355  memcpy(&this->xa_xid, xid, sizeof(XID));
356 
357  THROW(NONE);
358  } CATCH {
359  switch (excp) {
360  case NULL_OBJECT:
361  ret_cod = LIXA_RC_NULL_OBJECT;
362  break;
363  case G_TRY_MALLOC_ERROR:
364  ret_cod = LIXA_RC_G_TRY_MALLOC_ERROR;
365  break;
366  case NONE:
367  ret_cod = LIXA_RC_OK;
368  break;
369  default:
370  ret_cod = LIXA_RC_INTERNAL_ERROR;
371  } /* switch (excp) */
372  /* clean-up memory in the event of an error */
373  if (NONE != excp) {
374  g_free(this);
375  this = NULL;
376  }
377  } /* TRY-CATCH */
378  LIXA_TRACE(("xta_xid_new_from_XID/excp=%d/"
379  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
380  return this;
381 }
382 
383 
384 
386 {
387  enum Exception { NULL_OBJECT
388  , G_TRY_MALLOC_ERROR
389  , NONE } excp;
390  int ret_cod = LIXA_RC_INTERNAL_ERROR;
391  xta_xid_t *this = NULL;
392 
393  LIXA_TRACE(("xta_xid_dup\n"));
394  TRY {
395  if (NULL == xid)
396  THROW(NULL_OBJECT);
397  /* allocate the object */
398  if (NULL == (this = (xta_xid_t *)
399  g_try_malloc0(sizeof(xta_xid_t))))
400  THROW(G_TRY_MALLOC_ERROR);
401  /* copy da field */
402  this->xa_xid = xid->xa_xid;
403 
404  THROW(NONE);
405  } CATCH {
406  switch (excp) {
407  case NULL_OBJECT:
408  ret_cod = LIXA_RC_NULL_OBJECT;
409  break;
410  case G_TRY_MALLOC_ERROR:
411  ret_cod = LIXA_RC_G_TRY_MALLOC_ERROR;
412  break;
413  case NONE:
414  ret_cod = LIXA_RC_OK;
415  break;
416  default:
417  ret_cod = LIXA_RC_INTERNAL_ERROR;
418  } /* switch (excp) */
419  /* clean-up memory in the event of an error */
420  if (NONE != excp) {
421  g_free(this);
422  this = NULL;
423  }
424  } /* TRY-CATCH */
425  LIXA_TRACE(("xta_xid_dup/excp=%d/"
426  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
427  return this;
428 }
429 
430 
431 
433 {
434  enum Exception { NONE } excp;
435  int ret_cod = LIXA_RC_INTERNAL_ERROR;
436 
437  LIXA_TRACE(("xta_xid_delete\n"));
438  TRY {
439  g_free(xid);
440 
441  THROW(NONE);
442  } CATCH {
443  switch (excp) {
444  case NONE:
445  ret_cod = LIXA_RC_OK;
446  break;
447  default:
448  ret_cod = LIXA_RC_INTERNAL_ERROR;
449  } /* switch (excp) */
450  } /* TRY-CATCH */
451  LIXA_TRACE(("xta_xid_delete/excp=%d/"
452  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
453  return;
454 }
455 
456 
457 
458 const XID *xta_xid_get_xa_xid(const xta_xid_t *xid)
459 {
460  enum Exception { NULL_OBJECT
461  , NONE } excp;
462  int ret_cod = LIXA_RC_INTERNAL_ERROR;
463  const XID *xa_xid = NULL;
464 
465  LIXA_TRACE(("xta_xid_get_xa_xid\n"));
466  TRY {
467  if (NULL == xid)
468  THROW(NULL_OBJECT);
469  xa_xid = &xid->xa_xid;
470 
471  THROW(NONE);
472  } CATCH {
473  switch (excp) {
474  case NULL_OBJECT:
475  ret_cod = LIXA_RC_NULL_OBJECT;
476  break;
477  case NONE:
478  ret_cod = LIXA_RC_OK;
479  break;
480  default:
481  ret_cod = LIXA_RC_INTERNAL_ERROR;
482  } /* switch (excp) */
483  } /* TRY-CATCH */
484  LIXA_TRACE(("xta_xid_get_xa_xid/excp=%d/"
485  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
486  return xa_xid;
487 }
488 
489 
490 
491 char *xta_xid_to_string(const xta_xid_t *xid)
492 {
493  enum Exception { NULL_OBJECT
494  , MALLOC_ERROR
495  , LIXA_XID_SERIALIZE_ERROR
496  , NONE } excp;
497  int ret_cod = LIXA_RC_INTERNAL_ERROR;
498  char *string = NULL;
499 
500  LIXA_TRACE(("xta_xid_to_string\n"));
501  TRY {
502  lixa_ser_xid_t lsx;
503  if (NULL == xid)
504  THROW(NULL_OBJECT);
505  /* allocate the dynamic memory */
506  if (NULL == (string = malloc(sizeof(lixa_ser_xid_t))))
507  THROW(MALLOC_ERROR);
508  if (!lixa_xid_serialize(&xid->xa_xid, lsx))
509  THROW(LIXA_XID_SERIALIZE_ERROR);
510  strncpy(string, lsx, sizeof(lixa_ser_xid_t));
511 
512  THROW(NONE);
513  } CATCH {
514  switch (excp) {
515  case NULL_OBJECT:
516  ret_cod = LIXA_RC_NULL_OBJECT;
517  break;
518  case MALLOC_ERROR:
519  ret_cod = LIXA_RC_MALLOC_ERROR;
520  break;
521  case LIXA_XID_SERIALIZE_ERROR:
522  break;
523  case NONE:
524  ret_cod = LIXA_RC_OK;
525  break;
526  default:
527  ret_cod = LIXA_RC_INTERNAL_ERROR;
528  } /* switch (excp) */
529  } /* TRY-CATCH */
530  LIXA_TRACE(("xta_xid_to_string/excp=%d/"
531  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
532  return string;
533 }
534 
535 
536 
538 {
539  enum Exception { NONE } excp;
540  int ret_cod = LIXA_RC_INTERNAL_ERROR;
541 
542  LIXA_TRACE(("xta_xid_reset\n"));
543  TRY {
544  lixa_xid_reset(&xid->xa_xid);
545 
546  THROW(NONE);
547  } CATCH {
548  switch (excp) {
549  case NONE:
550  ret_cod = LIXA_RC_OK;
551  break;
552  default:
553  ret_cod = LIXA_RC_INTERNAL_ERROR;
554  } /* switch (excp) */
555  } /* TRY-CATCH */
556  LIXA_TRACE(("xta_xid_reset/excp=%d/"
557  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
558  return;
559 }
560 
561 
562 
563 long xta_xid_get_gtrid(const xta_xid_t *xid, char *gtrid) {
564  return lixa_xid_get_gtrid(&xid->xa_xid, gtrid);
565 }
566 
567 
568 
569 long xta_xid_get_bqual(const xta_xid_t *xid, char *bqual) {
570  return lixa_xid_get_bqual(&xid->xa_xid, bqual);
571 }
572 
573 
574 
575 long xta_xid_get_formatID(const xta_xid_t *xid) {
576  return lixa_xid_get_formatID(&xid->xa_xid);
577 }
void xta_xid_delete(xta_xid_t *xid)
Definition: xta_xid.c:432
int xta_xid_branch_qualifier_set_multibranch(char *branch_qualifier)
Definition: xta_xid.c:138
long xta_xid_get_bqual(const xta_xid_t *xid, char *bqual)
Definition: xta_xid.c:569
#define LIXA_RC_STRDUP_ERROR
Definition: lixa_errors.h:281
#define LIXA_RC_NULL_OBJECT
Definition: lixa_errors.h:139
#define LIXA_RC_INTERNAL_ERROR
Definition: lixa_errors.h:122
XID xa_xid
Definition: xta_xid.h:36
int xta_xid_branch_qualifier_is_multibranch(const char *branch_qualifier)
Definition: xta_xid.c:110
#define LIXA_RC_OK
Definition: lixa_errors.h:115
xta_xid_t * xta_xid_dup(const xta_xid_t *xid)
Definition: xta_xid.c:385
xta_xid_t * xta_xid_new_from_XID(const XID *xid)
Definition: xta_xid.c:337
#define LIXA_RC_MALLOC_ERROR
Definition: lixa_errors.h:273
xta_xid_t * xta_xid_new(const char *branch_qualifier, int multiple_branches)
Definition: xta_xid.c:47
xta_xid_t * xta_xid_new_from_string(const char *xid_string)
Definition: xta_xid.c:291
const XID * xta_xid_get_xa_xid(const xta_xid_t *xid)
Definition: xta_xid.c:458
#define LIXA_RC_G_TRY_MALLOC_ERROR
Definition: lixa_errors.h:563
Definition: xa.h:28
long xta_xid_get_gtrid(const xta_xid_t *xid, char *gtrid)
Definition: xta_xid.c:563
void xta_xid_reset(xta_xid_t *xid)
Definition: xta_xid.c:537
int xta_xid_branch_qualifier_unset_multibranch(char *branch_qualifier)
Definition: xta_xid.c:219
#define LIXA_RC_MALFORMED_XID
Definition: lixa_errors.h:224
char * xta_xid_to_string(const xta_xid_t *xid)
Definition: xta_xid.c:491
long xta_xid_get_formatID(const xta_xid_t *xid)
Definition: xta_xid.c:575

Copyright © 2009-2019, Christian Ferrari tiian@users.sourceforge.net http://www.tiian.org/