chiark / gitweb /
rand/rand-x86ish.S: Hoist argument register allocation outside.
[catacomb] / math / field-parse.c
1 /* -*-c-*-
2  *
3  * Parse field descriptions
4  *
5  * (c) 2004 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
12  * Catacomb is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * Catacomb is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with Catacomb; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "field.h"
31
32 /*----- Main code ---------------------------------------------------------*/
33
34 /* --- @field_parse@ --- *
35  *
36  * Arguments:   @qd_parse *qd@ = parser context
37  *
38  * Returns:     Field pointer if OK, or null.
39  *
40  * Use:         Parses a field description, which has the form
41  *
42  *                * `prime', `niceprime' or `binpoly'
43  *                * an optional `:'
44  *                * the field modulus
45  */
46
47 field *field_parse(qd_parse *qd)
48 {
49   field *f = 0;
50   mp *m = MP_NEW, *b = MP_NEW;
51
52   switch (qd_enum(qd, "prime,niceprime,binpoly,binnorm")) {
53     case 0:
54       qd_delim(qd, ':');
55       if ((m = qd_getmp(qd)) == 0) goto done;
56       f = field_prime(m);
57       break;
58     case 1:
59       qd_delim(qd, ':');
60       if ((m = qd_getmp(qd)) == 0) goto done;
61       f = field_niceprime(m);
62       break;
63     case 2:
64       qd_delim(qd, ':');
65       if ((m = qd_getmp(qd)) == 0) goto done;
66       f = field_binpoly(m);
67       break;
68     case 3:
69       qd_delim(qd, ':');
70       if ((m = qd_getmp(qd)) == 0) goto done;
71       qd_delim(qd, ',');
72       if ((b = qd_getmp(qd)) == 0) goto done;
73       f = field_binnorm(m, b);
74       break;
75     default:
76       goto done;
77   }
78   if (!f) qd->e = "bad field parameters";
79 done:
80   mp_drop(m);
81   mp_drop(b);
82   return (f);
83 }
84
85 /*----- That's all, folks -------------------------------------------------*/