github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpq/set_str.c (about)

     1  /* mpq_set_str -- string to mpq conversion.
     2  
     3  Copyright 2001, 2002 Free Software Foundation, Inc.
     4  
     5  This file is part of the GNU MP Library.
     6  
     7  The GNU MP Library is free software; you can redistribute it and/or modify
     8  it under the terms of either:
     9  
    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.
    13  
    14  or
    15  
    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.
    19  
    20  or both in parallel, as here.
    21  
    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.
    26  
    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/.  */
    30  
    31  #include <stdio.h>
    32  #include <string.h>
    33  #include "gmp.h"
    34  #include "gmp-impl.h"
    35  
    36  
    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.  */
    40  
    41  int
    42  mpq_set_str (mpq_ptr q, const char *str, int base)
    43  {
    44    const char  *slash;
    45    char        *num;
    46    size_t      numlen;
    47    int         ret;
    48  
    49    slash = strchr (str, '/');
    50    if (slash == NULL)
    51      {
    52        SIZ(DEN(q)) = 1;
    53        PTR(DEN(q))[0] = 1;
    54  
    55        return mpz_set_str (mpq_numref(q), str, base);
    56      }
    57  
    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);
    64  
    65    if (ret != 0)
    66      return ret;
    67  
    68    return mpz_set_str (mpq_denref(q), slash+1, base);
    69  }