summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/GMPString.cc27
-rw-r--r--lib/Makefile.am8
-rw-r--r--lib/String.cc111
3 files changed, 89 insertions, 57 deletions
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;
}