update isl for change in isl_pw_aff_set_tuple_id prototype
[barvinok.git] / isl_obj_str.c
blob46b33c6b5ab22c2e5fe60f3fc0e4990c2060d155
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 isl_printer_print_str(p, "\"");
80 isl_printer_print_str(p, str->s);
81 isl_printer_print_str(p, "\"");
84 __isl_give isl_str *isl_str_concat(__isl_take isl_str *str1,
85 __isl_take isl_str *str2)
87 int i;
88 isl_str *str = NULL;
89 size_t len1, len2;
91 if (!str1 || !str2)
92 goto error;
94 str = isl_str_alloc(str1->ctx);
95 if (!str)
96 goto error;
98 len1 = strlen(str1->s);
99 len2 = strlen(str2->s);
100 str->s = isl_alloc_array(str1->ctx, char, len1 + len2 + 1);
101 if (!str->s)
102 goto error;
104 memcpy(str->s, str1->s, len1);
105 memcpy(str->s + len1, str2->s, len2);
106 str->s[len1 + len2] = '\0';
108 isl_str_free(str1);
109 isl_str_free(str2);
111 return str;
112 error:
113 isl_str_free(str1);
114 isl_str_free(str2);
115 isl_str_free(str);
116 return NULL;
119 static void *isl_obj_str_add(void *v1, void *v2)
121 isl_str *str1 = (isl_str *)v1;
122 isl_str *str2 = (isl_str *)v2;
124 return isl_str_concat(str1, str2);
127 struct isl_obj_vtable isl_obj_str_vtable = {
128 isl_obj_str_copy,
129 isl_obj_str_add,
130 isl_obj_str_print,
131 isl_obj_str_free