test_bound.c: result_data: use isl_val
[barvinok.git] / isl_obj_str.c
blob7880fec44d921060859502d6b046db03c35ac2e5
1 #include <string.h>
2 #include <isl_obj_str.h>
4 __isl_give isl_str *isl_str_alloc(struct isl_ctx *ctx)
6 isl_str *str;
8 str = isl_alloc_type(ctx, struct isl_str);
9 if (!str)
10 return NULL;
12 str->ctx = ctx;
13 isl_ctx_ref(str->ctx);
14 str->ref = 1;
16 str->s = NULL;
18 return str;
21 __isl_give isl_str *isl_str_from_string(isl_ctx *ctx, __isl_take char *s)
23 isl_str *str;
25 if (!s)
26 return NULL;
28 str = isl_str_alloc(ctx);
29 if (!str)
30 goto error;
32 str->s = s;
34 return str;
35 error:
36 free(s);
37 return NULL;
40 __isl_give isl_str *isl_str_copy(__isl_keep isl_str *str)
42 if (!str)
43 return NULL;
45 str->ref++;
46 return str;
49 void isl_str_free(__isl_take isl_str *str)
51 int i;
53 if (!str)
54 return;
56 if (--str->ref > 0)
57 return;
59 free(str->s);
60 isl_ctx_deref(str->ctx);
61 free(str);
64 static void *isl_obj_str_copy(void *v)
66 return isl_str_copy((isl_str *)v);
69 static void isl_obj_str_free(void *v)
71 isl_str_free((isl_str *)v);
74 static __isl_give isl_printer *isl_obj_str_print(__isl_take isl_printer *p,
75 void *v)
77 isl_str *str = (isl_str *)v;
79 p = isl_printer_print_str(p, "\"");
80 p = isl_printer_print_str(p, str->s);
81 p = isl_printer_print_str(p, "\"");
83 return p;
86 __isl_give isl_str *isl_str_concat(__isl_take isl_str *str1,
87 __isl_take isl_str *str2)
89 int i;
90 isl_str *str = NULL;
91 size_t len1, len2;
93 if (!str1 || !str2)
94 goto error;
96 str = isl_str_alloc(str1->ctx);
97 if (!str)
98 goto error;
100 len1 = strlen(str1->s);
101 len2 = strlen(str2->s);
102 str->s = isl_alloc_array(str1->ctx, char, len1 + len2 + 1);
103 if (!str->s)
104 goto error;
106 memcpy(str->s, str1->s, len1);
107 memcpy(str->s + len1, str2->s, len2);
108 str->s[len1 + len2] = '\0';
110 isl_str_free(str1);
111 isl_str_free(str2);
113 return str;
114 error:
115 isl_str_free(str1);
116 isl_str_free(str2);
117 isl_str_free(str);
118 return NULL;
121 static void *isl_obj_str_add(void *v1, void *v2)
123 isl_str *str1 = (isl_str *)v1;
124 isl_str *str2 = (isl_str *)v2;
126 return isl_str_concat(str1, str2);
129 struct isl_obj_vtable isl_obj_str_vtable = {
130 isl_obj_str_copy,
131 isl_obj_str_add,
132 isl_obj_str_print,
133 isl_obj_str_free