summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpixel <pixel>2003-02-13 22:45:44 +0000
committerpixel <pixel>2003-02-13 22:45:44 +0000
commit9683c7b1f67c75670714397c46338c5b2f9b4dc8 (patch)
tree7b257dc9f65e0eb17c74b98c123bdf63a715bcc0
parent383a1ae79222f971bc9245d9c6f35b5db3e28c8b (diff)
Supporting GMP
-rw-r--r--configure.ac35
-rw-r--r--include/BString.h7
-rw-r--r--include/GMPString.h18
-rw-r--r--include/Makefile.am2
-rw-r--r--lib/GMPString.cc27
-rw-r--r--lib/Makefile.am8
-rw-r--r--lib/String.cc111
7 files changed, 143 insertions, 65 deletions
diff --git a/configure.ac b/configure.ac
index 5035d7e..7b220ac 100644
--- a/configure.ac
+++ b/configure.ac
@@ -56,7 +56,7 @@ AC_TYPE_SIGNAL
AC_FUNC_STRFTIME
AC_FUNC_VPRINTF
AC_FUNC_STAT
-AC_CHECK_FUNCS([__argz_count __argz_next __argz_stringify getcwd gethostbyname memmove mempcpy memset munmap nl_langinfo regcomp setlocale socket stpcpy strcasecmp strchr strcspn strdup strerror strrchr strstr strtoul pipe sleep fcntl vsnprintf gettimeofday])
+AC_CHECK_FUNCS([__argz_count __argz_next __argz_stringify getcwd gethostbyname memmove mempcpy memset munmap nl_langinfo regcomp setlocale socket stpcpy strcasecmp strchr strcspn strdup strerror strrchr strstr strtoul pipe sleep fcntl vsnprintf gettimeofday asprintf vasprintf])
AC_SEARCH_LIBS(socket, socket)
AC_SEARCH_LIBS(gethostbyname, nsl)
@@ -80,8 +80,6 @@ else
AC_MSG_ERROR([no zlib found in the system.])
fi
-AC_DEFINE([HOOK_STDS], 1, [Hook the standard input/outputs])
-
if test x$ac_cv_wd_gzwrite != xyes ; then
AC_MSG_WARN([You have a broken zconf.h.]
[You should patch this using]
@@ -89,6 +87,37 @@ if test x$ac_cv_wd_gzwrite != xyes ; then
[Enabling workaround.])
fi
+GMP_LIBS="-lm"
+if test "$with_gmp" != "no"; then
+ AC_ARG_WITH(gmp-prefix, [ --with-gmp-prefix=DIR path to GMP],
+ [if test "$withval" != "no"; then
+ CPPFLAGS="${CPPFLAGS} -I$withval/include"
+ LDFLAGS="${LDFLAGS} -L$withval/lib"
+ fi],
+ dnl debian brain-damage
+ [if test -d /usr/include/gmp2; then
+ CPPFLAGS="${CPPFLAGS} -I/usr/include/gmp2"
+ fi])
+
+ found_gmp=no
+ AC_CHECK_HEADER(gmp.h,
+ [AC_CHECK_LIB(gmp, mpz_init,
+ [GMP_LIBS="-lgmp -lm"; found_gmp=yes],
+ [AC_CHECK_LIB(gmp, __gmpz_init,
+ [GMP_LIBS="-lgmp -lm"; found_gmp=yes])])])
+
+ if test "$found_gmp" = "yes"; then
+ AC_DEFINE([HAVE_GMP], 1, [Have the GMP library])
+ LIBS="$LIBS $GMP_LIBS"
+ AC_CHECK_FUNC(__gmp_randinit, AC_DEFINE([HAVE_GMP_RANDINIT], 1, [Have the RandInit function]))
+ elif test "$with_gmp" != "no"; then
+ AC_MSG_ERROR([Can't find GMP (--without-gmp for cut-down non-GMP build)])
+ fi
+fi
+AC_SUBST(GMP_LIBS)
+
+AC_DEFINE([HOOK_STDS], 1, [Hook the standard input/outputs])
+
AC_CHECK_FUNCS(gethostbyname)
AC_CHECK_FUNCS(regcomp)
AC_CHECK_FUNCS(socket)
diff --git a/include/BString.h b/include/BString.h
index 8dd71c5..ab4f5e5 100644
--- a/include/BString.h
+++ b/include/BString.h
@@ -2,6 +2,7 @@
#define __STRING_H__
#include <string.h>
+#include <stdarg.h>
#include <iostream>
#include <string>
#include <Exceptions.h>
@@ -15,9 +16,6 @@ class String : public Base {
public:
String(const String &);
String(const char * = "");
-#if 0
- String(const char * = "", ...);
-#endif
String(char);
String(int);
String(unsigned int);
@@ -25,6 +23,7 @@ class String : public Base {
String(uint64);
String(double);
~String();
+ const char * set(const char *, va_list);
const char * set(const char *, ...);
const char * set(const ugly_string &, ...);
int scanf(const char *, ...) const;
@@ -65,7 +64,7 @@ class String : public Base {
private:
String(int hs, char *);
static char t[];
- char * str, * ostr;
+ char * str;
size_t siz;
};
diff --git a/include/GMPString.h b/include/GMPString.h
new file mode 100644
index 0000000..77364bf
--- /dev/null
+++ b/include/GMPString.h
@@ -0,0 +1,18 @@
+#ifndef __GMPSTRING_H__
+#define __GMPSTRING_H__
+#include <gmpxx.h>
+#include "BString.h"
+
+class GMPString : public Base {
+ public:
+ GMPString(const GMPString &);
+ GMPString(const mpz_class &);
+ GMPString(const mpq_class &);
+ GMPString(const mpf_class &);
+ ~GMPString();
+ operator String() const;
+ private:
+ char * str;
+};
+
+#endif
diff --git a/include/Makefile.am b/include/Makefile.am
index c8c5036..583a97d 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -2,6 +2,6 @@ pkginclude_HEADERS = \
Exceptions.h Handle.h BString.h Output.h Socket.h HttpServ.h Variables.h Menu.h \
Action.h Message.h Form.h Confirm.h Table.h IRC.h Task.h Buffer.h generic.h \
CopyJob.h ReadJob.h Regex.h TaskMan.h InPipe.h OutPipe.h Input.h Image.h \
-Main.h Color.h
+Main.h Color.h GMPString.h
noinst_HEADERS = gettext.h
diff --git a/lib/GMPString.cc b/lib/GMPString.cc
new file mode 100644
index 0000000..5b2d1c6
--- /dev/null
+++ b/lib/GMPString.cc
@@ -0,0 +1,27 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#ifdef HAVE_GMP
+#include <gmpxx.h>
+#include "GMPString.h"
+
+GMPString::GMPString(const GMPString & s) : str(s.str) {
+}
+
+GMPString::GMPString(const mpz_class & z) {
+ gmp_asprintf(&str, "%Zd", z.get_mpz_t());
+}
+
+GMPString::GMPString(const mpq_class & q) {
+ gmp_asprintf(&str, "%Qd", q.get_mpq_t());
+}
+
+GMPString::GMPString(const mpf_class & f) {
+ gmp_asprintf(&str, "%Ff", f.get_mpf_t());
+}
+
+GMPString::operator String() const {
+ return String(str);
+}
+
+#endif
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 3e921e6..6719ad7 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -6,7 +6,7 @@ INCLUDES = -I.. -I../include -I$(includedir)
lib_LTLIBRARIES = libBaltisot.la
libBaltisot_la_SOURCES = Action.cc Buffer.cc checkargs.c Confirm.cc CopyJob.cc \
-datecalc.c Exceptions.cc Form.cc Handle.cc HttpServ.cc Image.cc InPipe.cc \
-Input.cc IRC.cc Menu.cc Message.cc OutPipe.cc Output.cc ReadJob.cc Regex.cc \
-Socket.cc String.cc Table.cc Task.cc TaskMan.cc Variables.cc generic.cc \
-fileutils.cc Main.cc
+datecalc.c Exceptions.cc Form.cc GMPString.cc Handle.cc HttpServ.cc Image.cc \
+InPipe.cc Input.cc IRC.cc Main.cc Menu.cc Message.cc OutPipe.cc Output.cc \
+ReadJob.cc Regex.cc Socket.cc String.cc Table.cc Task.cc TaskMan.cc \
+Variables.cc generic.cc fileutils.cc
diff --git a/lib/String.cc b/lib/String.cc
index c4fbaf9..ec78c22 100644
--- a/lib/String.cc
+++ b/lib/String.cc
@@ -5,6 +5,9 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
+#ifdef HAVE_GMP
+#include <gmpxx.h>
+#endif
#include "BString.h"
#include "Exceptions.h"
@@ -21,18 +24,19 @@ String::String(const String & s) : str(Base::strdup(s.str)), siz(s.siz) {
#ifdef DEBUG
fprintf(stderr, "Duplicating string `%s', from %p to %p, from this %p to this %p\n", str, s.str, str, s, this);
#endif
- ostr = Base::strdup(str);
}
String::String(char c) : siz(1) {
- char * t = (char *) malloc(2);
-
#ifdef DEBUG
fprintf(stderr, "Creating a string with `%c' at %p, this = %p\n", c, str, this);
#endif
+#ifndef HAVE_ASPRINTF
+ char * t = (char *) malloc(2);
sprintf(t, "%c", c);
str = t;
- ostr = Base::strdup(str);
+#else
+ asprintf(&str, "%c", c);
+#endif
}
String::String(const char * s) : str(Base::strdup(s)), siz(::strlen(str)) {
@@ -42,121 +46,114 @@ String::String(const char * s) : str(Base::strdup(s)), siz(::strlen(str)) {
#ifdef DEBUG
fprintf(stderr, "Creating a string with `%s' at %p from %p, this = %p\n", str, str, s, this);
#endif
- ostr = Base::strdup(str);
}
-#if 0
-String::String(const char * s, ...) {
- va_list ap;
-
-#ifdef DEBUG
- fprintf(stderr, "Creating a String with s = '%s'\n", s);
-#endif
-
- va_start(ap, s);
-#ifdef HAVE_VSNPRINTF
- vsnprintf(t, BUFSIZ, s, ap);
-#else
- vsprintf(t, s, ap);
-#endif
- str = Base::strdup(t);
- va_end(ap);
- siz = ::strlen(str);
-}
-#endif
-
String::String(int hs, char * s) : str(Base::strdup(s ? s : "")), siz(hs) {
#ifdef DEBUG
fprintf(stderr, "Fast-Creating a string with `%s' at %p from %p, this = %p\n", str, str, s, this);
#endif
- ostr = Base::strdup(str);
}
String::String(int i) {
+#ifndef HAVE_ASPRINTF
char t[20];
-
sprintf(t, "%i", i);
str = Base::strdup(t);
+#else
+ asprintf(&str, "%i", i);
+#endif
siz = ::strlen(str);
}
String::String(unsigned int i) {
+#ifndef HAVE_ASPRINTF
char t[20];
-
sprintf(t, "%u", i);
str = Base::strdup(t);
+#else
+ asprintf(&str, "%u", i);
+#endif
siz = ::strlen(str);
}
String::String(int64 l) {
+#ifndef HAVE_ASPRINTF
char t[40];
-
sprintf(t, "%lld", l);
str = Base::strdup(t);
+#else
+ asprintf(&str, "%lld", l);
+#endif
siz = ::strlen(str);
}
String::String(uint64 l) {
+#ifndef HAVE_ASPRINTF
char t[40];
-
sprintf(t, "%llu", l);
str = Base::strdup(t);
+#else
+ asprintf(&str, "%llu", l);
+#endif
siz = ::strlen(str);
}
String::String(double d) {
+#ifndef HAVE_ASPRINTF
char t[30];
-
sprintf(t, "%g", d);
str = Base::strdup(t);
+#else
+ asprintf(&str, "%g", d);
+#endif
siz = ::strlen(str);
}
String::~String() {
#ifdef DEBUG
fprintf(stderr, "Destroying string @ %p, freeing %p.\n", this, str);
- fprintf(stderr, "Destroying string `%s'\n", ostr);
#endif
free(str);
}
-const char * String::set(const char * s, ...) {
- va_list ap;
-
+const char * String::set(const char * s, va_list ap) {
+ const char * r;
+ free(str);
if (!s) {
- free(str);
str = Base::strdup("");
- t[0] = 0;
- return t;
+ return str;
}
- va_start(ap, s);
+#ifdef HAVE_VASPRINTF
+ vasprintf(&str, s, ap);
+ r = str;
#ifdef HAVE_VSNPRINTF
vsnprintf(t, BUFSIZ, s, ap);
#else
vsprintf(t, s, ap);
#endif
- free(str);
- str = Base::strdup(t);
- va_end(ap);
+ str = Base::strdup(r = t);
+#endif
siz = ::strlen(str);
- return t;
+ return r;
}
-const char * String::set(const ugly_string & s, ...) {
+const char * String::set(const char * s, ...) {
+ const char * r;
va_list ap;
+ va_start(ap, s);
+ r = set(s, ap);
+ va_end(ap);
+ return r;
+}
+const char * String::set(const ugly_string & s, ...) {
+ const char * r;
+ va_list ap;
va_start(ap, s);
-#ifdef HAVE_VSNPRINTF
- vsnprintf(t, BUFSIZ, s.p, ap);
-#else
- vsprintf(t, s.p, ap);
-#endif
- free(str);
- str = Base::strdup(t);
+ r = set(s.p, ap);
va_end(ap);
- siz = ::strlen(str);
- return t;
+ return r;
}
int String::scanf(const char * s, ...) const {
@@ -164,7 +161,11 @@ int String::scanf(const char * s, ...) const {
int t;
va_start(ap, s);
+#ifdef HAVE_GMP
+ t = gmp_vsscanf(str, s, ap);
+#else
t = vsscanf(str, s, ap);
+#endif
va_end(ap);
return t;
}
@@ -174,7 +175,11 @@ int String::scanf(const ugly_string & s, ...) const {
int t;
va_start(ap, s);
+#ifdef HAVE_GMP
+ t = gmp_vsscanf(str, s.p, ap);
+#else
t = vsscanf(str, s.p, ap);
+#endif
va_end(ap);
return t;
}