1 /*
2 Copyright (C) 2007, David Howden.
3 Copyright (C) 2010 William Hart
4
5 This file is part of FLINT.
6
7 FLINT is free software: you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License (LGPL) as published
9 by the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version. See <http://www.gnu.org/licenses/>.
11 */
12
13 #include <gmp.h>
14 #include <stdio.h>
15 #include "flint.h"
16 #include "fmpz_mod_poly.h"
17 #include "fmpz.h"
18
fmpz_mod_poly_fread(FILE * f,fmpz_mod_poly_t poly,fmpz_mod_ctx_t ctx)19 int fmpz_mod_poly_fread(FILE * f, fmpz_mod_poly_t poly, fmpz_mod_ctx_t ctx)
20 {
21 int success = 0;
22 slong i, length;
23 fmpz_t coeff;
24
25 fmpz_init(coeff);
26
27 poly->length = 0;
28
29 if (flint_fscanf(f, "%wd", &length) != 1)
30 goto cleanup;
31
32 if (!fmpz_fread(f, coeff))
33 goto cleanup;
34
35 if (fmpz_cmp_ui(coeff, 2) < 0)
36 goto cleanup;
37
38 fmpz_mod_ctx_set_modulus(ctx, coeff);
39
40 fmpz_mod_poly_fit_length(poly, length, ctx);
41
42 for (i = 0; i < length; i++)
43 {
44 if (!fmpz_fread(f, coeff))
45 goto cleanup;
46
47 fmpz_mod_poly_set_coeff_fmpz(poly, i, coeff, ctx);
48 }
49
50 poly->length = length;
51 _fmpz_mod_poly_normalise(poly);
52
53 success = 1;
54
55 cleanup:
56
57 fmpz_clear(coeff);
58
59 return success;
60 }
61
62