remove polyhedron_range
[barvinok.git] / isl_obj_str.c
blob9d61c7ff52f996bdcb8f03db78de1bddd674b4a5
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, str->s);
82 __isl_give isl_str *isl_str_concat(__isl_take isl_str *str1,
83 __isl_take isl_str *str2)
85 int i;
86 isl_str *str = NULL;
87 size_t len1, len2;
89 if (!str1 || !str2)
90 goto error;
92 str = isl_str_alloc(str1->ctx);
93 if (!str)
94 goto error;
96 len1 = strlen(str1->s);
97 len2 = strlen(str2->s);
98 str->s = isl_alloc_array(str1->ctx, char, len1 + len2 + 1);
99 if (!str->s)
100 goto error;
102 memcpy(str->s, str1->s, len1);
103 memcpy(str->s + len1, str2->s, len2);
104 str->s[len1 + len2] = '\0';
106 isl_str_free(str1);
107 isl_str_free(str2);
109 return str;
110 error:
111 isl_str_free(str1);
112 isl_str_free(str2);
113 isl_str_free(str);
114 return NULL;
117 static void *isl_obj_str_add(void *v1, void *v2)
119 isl_str *str1 = (isl_str *)v1;
120 isl_str *str2 = (isl_str *)v2;
122 return isl_str_concat(str1, str2);
125 struct isl_obj_vtable isl_obj_str_vtable = {
126 isl_obj_str_copy,
127 isl_obj_str_add,
128 isl_obj_str_print,
129 isl_obj_str_free