use test scripts for performing tests
[barvinok.git] / isl_obj_str.c
blobc23209a4b057e4b29b01a0639a7fee499be7aa7c
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 if (!str)
52 return;
54 if (--str->ref > 0)
55 return;
57 free(str->s);
58 isl_ctx_deref(str->ctx);
59 free(str);
62 static void *isl_obj_str_copy(void *v)
64 return isl_str_copy((isl_str *)v);
67 static void isl_obj_str_free(void *v)
69 isl_str_free((isl_str *)v);
72 static __isl_give isl_printer *isl_obj_str_print(__isl_take isl_printer *p,
73 void *v)
75 isl_str *str = (isl_str *)v;
77 p = isl_printer_print_str(p, "\"");
78 p = isl_printer_print_str(p, str->s);
79 p = isl_printer_print_str(p, "\"");
81 return p;
84 __isl_give isl_str *isl_str_concat(__isl_take isl_str *str1,
85 __isl_take isl_str *str2)
87 isl_str *str = NULL;
88 size_t len1, len2;
90 if (!str1 || !str2)
91 goto error;
93 str = isl_str_alloc(str1->ctx);
94 if (!str)
95 goto error;
97 len1 = strlen(str1->s);
98 len2 = strlen(str2->s);
99 str->s = isl_alloc_array(str1->ctx, char, len1 + len2 + 1);
100 if (!str->s)
101 goto error;
103 memcpy(str->s, str1->s, len1);
104 memcpy(str->s + len1, str2->s, len2);
105 str->s[len1 + len2] = '\0';
107 isl_str_free(str1);
108 isl_str_free(str2);
110 return str;
111 error:
112 isl_str_free(str1);
113 isl_str_free(str2);
114 isl_str_free(str);
115 return NULL;
118 static void *isl_obj_str_add(void *v1, void *v2)
120 isl_str *str1 = (isl_str *)v1;
121 isl_str *str2 = (isl_str *)v2;
123 return isl_str_concat(str1, str2);
126 struct isl_obj_vtable isl_obj_str_vtable = {
127 isl_obj_str_copy,
128 isl_obj_str_add,
129 isl_obj_str_print,
130 isl_obj_str_free