isl_tab_pip.c: fix typos in comments
[platform/upstream/isl.git] / isl_input.c
index 84cde79..3fcf5e0 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include <strings.h>
-#include <isl_set.h>
-#include <isl_seq.h>
-#include <isl_div.h>
-#include "isl_stream.h"
+#include <isl/set.h>
+#include <isl/seq.h>
+#include <isl/div.h>
+#include <isl/stream.h>
 #include "isl_map_private.h"
-#include "isl_obj.h"
+#include <isl/obj.h>
 #include "isl_polynomial_private.h"
-#include <isl_union_map.h>
+#include <isl/union_map.h>
 #include <isl_mat_private.h>
 
 struct variable {
@@ -1146,6 +1146,13 @@ static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
                isl_stream_error(s, NULL, "unexpected EOF");
                return NULL;
        }
+       if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
+               isl_stream_push_token(s, tok2);
+               isl_stream_push_token(s, tok);
+               isl_stream_error(s, NULL,
+                                "expecting constraint matrix dimensions");
+               return NULL;
+       }
        n_row = isl_int_get_si(tok->u.v);
        n_col = isl_int_get_si(tok2->u.v);
        on_new_line = tok2->on_new_line;
@@ -1246,11 +1253,17 @@ static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
                return NULL;
        }
        tok2 = isl_stream_next_token_on_same_line(s);
-       if (tok2) {
+       if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
                isl_stream_push_token(s, tok2);
                isl_stream_push_token(s, tok);
                return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
        }
+       if (tok2) {
+               isl_stream_error(s, tok2, "unexpected token");
+               isl_stream_push_token(s, tok2);
+               isl_stream_push_token(s, tok);
+               return NULL;
+       }
        n = isl_int_get_si(tok->u.v);
        isl_token_free(tok);
 
@@ -1258,7 +1271,7 @@ static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
 
        map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
 
-       for (i = 1; i < n; ++i)
+       for (i = 1; map && i < n; ++i)
                map = isl_map_union(map,
                        isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
 
@@ -1330,12 +1343,16 @@ static __isl_give isl_qpolynomial *read_factor(struct isl_stream *s,
                return NULL;
        }
        if (tok->type == '(') {
+               int pow;
+
                isl_token_free(tok);
                qp = read_term(s, bmap, v);
                if (!qp)
                        return NULL;
                if (isl_stream_eat(s, ')'))
                        goto error;
+               pow = optional_power(s);
+               qp = isl_qpolynomial_pow(qp, pow);
        } else if (tok->type == ISL_TOKEN_VALUE) {
                struct isl_token *tok2;
                tok2 = isl_stream_next_token(s);
@@ -1378,7 +1395,7 @@ static __isl_give isl_qpolynomial *read_factor(struct isl_stream *s,
                }
                isl_token_free(tok);
                pow = optional_power(s);
-               qp = isl_qpolynomial_pow(isl_basic_map_get_dim(bmap), pos, pow);
+               qp = isl_qpolynomial_var_pow(isl_basic_map_get_dim(bmap), pos, pow);
        } else if (tok->type == '[') {
                isl_div *div;
                int pow;
@@ -1819,6 +1836,49 @@ error:
        return NULL;
 }
 
+__isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
+{
+       struct isl_obj obj;
+       isl_union_map *umap;
+
+       obj = obj_read(s, -1);
+       if (obj.type == isl_obj_map) {
+               obj.type = isl_obj_union_map;
+               obj.v = isl_union_map_from_map(obj.v);
+       }
+       if (obj.type == isl_obj_set) {
+               obj.type = isl_obj_union_set;
+               obj.v = isl_union_set_from_set(obj.v);
+       }
+       if (obj.v)
+               isl_assert(s->ctx, obj.type == isl_obj_union_map ||
+                                  obj.type == isl_obj_union_set, goto error);
+
+       return obj.v;
+error:
+       obj.type->free(obj.v);
+       return NULL;
+}
+
+__isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
+{
+       struct isl_obj obj;
+       isl_union_set *uset;
+
+       obj = obj_read(s, -1);
+       if (obj.type == isl_obj_set) {
+               obj.type = isl_obj_union_set;
+               obj.v = isl_union_set_from_set(obj.v);
+       }
+       if (obj.v)
+               isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
+
+       return obj.v;
+error:
+       obj.type->free(obj.v);
+       return NULL;
+}
+
 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
 {
        struct isl_obj obj;
@@ -1949,60 +2009,83 @@ error:
        return NULL;
 }
 
-static char *next_line(FILE *input, char *line, unsigned len)
+__isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
+               const char *str)
 {
-       char *p;
-
-       do {
-               if (!(p = fgets(line, len, input)))
-                       return NULL;
-               while (isspace(*p) && *p != '\n')
-                       ++p;
-       } while (*p == '#' || *p == '\n');
+       isl_union_map *umap;
+       struct isl_stream *s = isl_stream_new_str(ctx, str);
+       if (!s)
+               return NULL;
+       umap = isl_stream_read_union_map(s);
+       isl_stream_free(s);
+       return umap;
+}
 
-       return p;
+__isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
+               const char *str)
+{
+       isl_union_set *uset;
+       struct isl_stream *s = isl_stream_new_str(ctx, str);
+       if (!s)
+               return NULL;
+       uset = isl_stream_read_union_set(s);
+       isl_stream_free(s);
+       return uset;
 }
 
-static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
-               FILE *input)
+static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
 {
        struct isl_vec *vec = NULL;
-       char line[1024];
-       char val[1024];
-       char *p;
+       struct isl_token *tok;
        unsigned size;
        int j;
-       int n;
-       int offset;
 
-       isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
-       isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
+       tok = isl_stream_next_token(s);
+       if (!tok || tok->type != ISL_TOKEN_VALUE) {
+               isl_stream_error(s, tok, "expecting vector length");
+               goto error;
+       }
 
-       vec = isl_vec_alloc(ctx, size);
+       size = isl_int_get_si(tok->u.v);
+       isl_token_free(tok);
 
-       p = next_line(input, line, sizeof(line));
-       isl_assert(ctx, p, goto error);
+       vec = isl_vec_alloc(s->ctx, size);
 
        for (j = 0; j < size; ++j) {
-               n = sscanf(p, "%s%n", val, &offset);
-               isl_assert(ctx, n != 0, goto error);
-               isl_int_read(vec->el[j], val);
-               p += offset;
+               tok = isl_stream_next_token(s);
+               if (!tok || tok->type != ISL_TOKEN_VALUE) {
+                       isl_stream_error(s, tok, "expecting constant value");
+                       goto error;
+               }
+               isl_int_set(vec->el[j], tok->u.v);
+               isl_token_free(tok);
        }
 
        return vec;
 error:
+       isl_token_free(tok);
        isl_vec_free(vec);
        return NULL;
 }
 
+static __isl_give isl_vec *vec_read(struct isl_stream *s,
+       unsigned input_format)
+{
+       if (input_format == ISL_FORMAT_POLYLIB)
+               return isl_vec_read_polylib(s);
+       isl_assert(s->ctx, 0, return NULL);
+}
+
 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
                FILE *input, unsigned input_format)
 {
-       if (input_format == ISL_FORMAT_POLYLIB)
-               return isl_vec_read_from_file_polylib(ctx, input);
-       else
-               isl_assert(ctx, 0, return NULL);
+       isl_vec *v;
+       struct isl_stream *s = isl_stream_new_file(ctx, input);
+       if (!s)
+               return NULL;
+       v = vec_read(s, input_format);
+       isl_stream_free(s);
+       return v;
 }
 
 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(