beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpq / set_str.c
blob930fe5576c0a4630869fa3bf6003811a941eed92
1 /* mpq_set_str -- string to mpq conversion.
3 Copyright 2001, 2002 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
18 later version.
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 for more details.
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
31 #include <stdio.h>
32 #include <string.h>
33 #include "gmp.h"
34 #include "gmp-impl.h"
37 /* FIXME: Would like an mpz_set_mem (or similar) accepting a pointer and
38 length so we wouldn't have to copy the numerator just to null-terminate
39 it. */
41 int
42 mpq_set_str (mpq_ptr q, const char *str, int base)
44 const char *slash;
45 char *num;
46 size_t numlen;
47 int ret;
49 slash = strchr (str, '/');
50 if (slash == NULL)
52 SIZ(DEN(q)) = 1;
53 PTR(DEN(q))[0] = 1;
55 return mpz_set_str (mpq_numref(q), str, base);
58 numlen = slash - str;
59 num = __GMP_ALLOCATE_FUNC_TYPE (numlen+1, char);
60 memcpy (num, str, numlen);
61 num[numlen] = '\0';
62 ret = mpz_set_str (mpq_numref(q), num, base);
63 (*__gmp_free_func) (num, numlen+1);
65 if (ret != 0)
66 return ret;
68 return mpz_set_str (mpq_denref(q), slash+1, base);