add isl_set_align_divs
[isl.git] / isl_input.c
blob2ee8b1e734b041f6be8da27a36fa990a9386020b
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <isl_set.h>
4 #include "isl_dim.h"
5 #include "isl_map_private.h"
6 #include "isl_input_omega.h"
8 static char *next_line(FILE *input, char *line, unsigned len)
10 char *p;
12 do {
13 if (!(p = fgets(line, len, input)))
14 return NULL;
15 while (isspace(*p) && *p != '\n')
16 ++p;
17 } while (*p == '#' || *p == '\n');
19 return p;
22 static struct isl_basic_set *isl_basic_set_read_from_file_polylib(
23 struct isl_ctx *ctx, FILE *input, unsigned nparam)
25 struct isl_basic_set *bset = NULL;
26 int i, j;
27 unsigned n_row, n_col;
28 unsigned dim;
29 char line[1024];
30 char val[1024];
31 char *p;
33 isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
34 isl_assert(ctx, sscanf(line, "%u %u", &n_row, &n_col) == 2, return NULL);
35 isl_assert(ctx, n_col >= 2, return NULL);
36 dim = n_col - 2 - nparam;
37 bset = isl_basic_set_alloc(ctx, nparam, dim, 0, n_row, n_row);
38 if (!bset)
39 return NULL;
40 for (i = 0; i < n_row; ++i) {
41 int type;
42 int offset;
43 int n;
44 int k;
45 isl_int *c;
47 p = next_line(input, line, sizeof(line));
48 isl_assert(ctx, p, goto error);
49 n = sscanf(p, "%u%n", &type, &offset);
50 isl_assert(ctx, n != 0, goto error);
51 p += offset;
52 isl_assert(ctx, type == 0 || type == 1, goto error);
53 if (type == 0) {
54 k = isl_basic_set_alloc_equality(bset);
55 c = bset->eq[k];
56 } else {
57 k = isl_basic_set_alloc_inequality(bset);
58 c = bset->ineq[k];
60 isl_assert(ctx, k >= 0, goto error);
61 for (j = 0; j < dim; ++j) {
62 n = sscanf(p, "%s%n", val, &offset);
63 isl_assert(ctx, n != 0, goto error);
64 isl_int_read(c[1+nparam+j], val);
65 p += offset;
67 for (j = 0; j < nparam; ++j) {
68 n = sscanf(p, "%s%n", val, &offset);
69 isl_assert(ctx, n != 0, goto error);
70 isl_int_read(c[1+j], val);
71 p += offset;
73 n = sscanf(p, "%s%n", val, &offset);
74 isl_assert(ctx, n != 0, goto error);
75 isl_int_read(c[0], val);
77 bset = isl_basic_set_simplify(bset);
78 bset = isl_basic_set_finalize(bset);
79 return bset;
80 error:
81 isl_basic_set_free(bset);
82 return NULL;
85 static struct isl_set *isl_set_read_from_file_polylib(
86 struct isl_ctx *ctx, FILE *input, unsigned nparam)
88 struct isl_set *set = NULL;
89 char line[1024];
90 int i;
91 unsigned n;
93 isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
94 isl_assert(ctx, sscanf(line, "%u", &n) == 1, return NULL);
96 set = isl_set_alloc(ctx, nparam, 0, n, 0);
97 if (!set)
98 return NULL;
99 if (n == 0)
100 return set;
101 for (i = 0; i < n; ++i) {
102 set->p[i] = isl_basic_set_read_from_file_polylib(ctx, input,
103 nparam);
104 if (!set->p[i])
105 goto error;
107 set->n = n;
108 isl_dim_free(set->dim);
109 set->dim = isl_dim_copy(set->p[0]->dim);
110 for (i = 1; i < n; ++i)
111 isl_assert(ctx, isl_dim_equal(set->dim, set->p[i]->dim),
112 goto error);
113 return set;
114 error:
115 isl_set_free(set);
116 return NULL;
119 struct isl_basic_set *isl_basic_set_read_from_file(struct isl_ctx *ctx,
120 FILE *input, unsigned nparam, unsigned input_format)
122 if (input_format == ISL_FORMAT_POLYLIB)
123 return isl_basic_set_read_from_file_polylib(ctx, input, nparam);
124 else if (input_format == ISL_FORMAT_OMEGA) {
125 isl_assert(ctx, nparam == 0, return NULL);
126 return isl_basic_set_read_from_file_omega(ctx, input);
127 } else
128 isl_assert(ctx, 0, return NULL);
131 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
132 const char *str, unsigned nparam, unsigned input_format)
134 if (input_format == ISL_FORMAT_OMEGA) {
135 isl_assert(ctx, nparam == 0, return NULL);
136 return isl_basic_set_read_from_str_omega(ctx, str);
137 } else
138 isl_assert(ctx, 0, return NULL);
141 struct isl_basic_map *isl_basic_map_read_from_file(struct isl_ctx *ctx,
142 FILE *input, unsigned nparam, unsigned input_format)
144 if (input_format == ISL_FORMAT_OMEGA)
145 return isl_basic_map_read_from_file_omega(ctx, input);
146 else
147 isl_assert(ctx, 0, return NULL);
150 struct isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
151 FILE *input, unsigned nparam, unsigned input_format)
153 if (input_format == ISL_FORMAT_POLYLIB)
154 return isl_set_read_from_file_polylib(ctx, input, nparam);
155 else
156 isl_assert(ctx, 0, return NULL);