5 * MP manipulation stuff
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the Perl interface to Catacomb.
14 * Catacomb/Perl is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Catacomb/Perl is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Catacomb/Perl; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
31 #include "catacomb-perl.h"
33 /*----- Main code ---------------------------------------------------------*/
35 /* --- Convert Perl integers to multiprecision --- */
37 mp
*mp_fromiv(mp
*d
, IV iv
)
39 MP_FROMINT(d
, IV
, iv
);
46 MP_TOINT(x
, IV
, IV_MAX
, i
);
50 /* --- Parse Perl strings into integers --- */
52 typedef struct mptext_svctx
{
57 static int svget(void *p
)
60 if (c
->i
>= SvCUR(c
->sv
))
62 return ((unsigned char)SvPVX(c
->sv
)[c
->i
++]);
65 static void svunget(int ch
, void *p
)
68 if (ch
== EOF
|| c
->i
== 0)
73 static int svput(const char *s
, size_t sz
, void *p
)
76 sv_catpvn(c
->sv
, (char *)s
, sz
);
80 static const mptext_ops mptext_svops
= { svget
, svunget
, svput
};
82 mp
*mp_readsv(mp
*m
, SV
*sv
, STRLEN
*off
, int radix
)
91 m
= mp_read(m
, radix
, &mptext_svops
, &c
);
97 int group_writesv(group
*g
, ge
*x
, SV
*sv
)
102 sv_setpvn(sv
, "", 0);
104 rc
= G_WRITE(g
, x
, &mptext_svops
, &c
);
108 int mp_writesv(mp
*m
, SV
*sv
, int radix
)
113 sv_setpvn(sv
, "", 0);
115 rc
= mp_write(m
, radix
, &mptext_svops
, &c
);
119 /* --- Conversion to and from SVs --- */
121 mp
*mp_fromsv(SV
*sv
, const char *what
, int radix
, int keep
, ...)
125 if (sv_derived_from(sv
, "Catacomb::MP"))
126 m
= (mp
*)SvIV((SV
*)SvRV(sv
));
129 SV
*t
= sv_newmortal();
131 sv_vsetpvfn(t
, what
, strlen(what
), &ap
, 0, 0, 0);
132 croak("%s is not of type Catacomb::MP", SvPVX(t
));
138 m
= mp_fromiv(MP_NEW
, SvIV(sv
));
140 m
= mp_readsv(MP_NEW
, sv
, 0, radix
);
142 RET_MP(m
); /* Kill temporary later */
147 /*----- That's all, folks -------------------------------------------------*/