summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/BString.h8
-rw-r--r--include/Exceptions.h4
-rw-r--r--lib/String.cc14
-rw-r--r--lib/generic.cc20
4 files changed, 37 insertions, 9 deletions
diff --git a/include/BString.h b/include/BString.h
index 948534e..a0aa10b 100644
--- a/include/BString.h
+++ b/include/BString.h
@@ -3,9 +3,14 @@
#include <string.h>
#include <iostream>
+#include <string>
#include <Exceptions.h>
#include <generic.h>
+struct ugly_string {
+ char * p;
+};
+
class String : public Base {
public:
String(const String &);
@@ -21,7 +26,7 @@ class String : public Base {
String(double);
~String();
const char * set(const char *, ...);
- const char * set(const String &, ...);
+ const char * set(const ugly_string &, ...);
const char * to_charp(size_t = 0, ssize_t = -1) const;
String extract(size_t = 0, ssize_t = -1) const;
char * strdup(size_t = 0, ssize_t = -1) const;
@@ -51,6 +56,7 @@ class String : public Base {
bool operator<(const String &) const;
bool operator>(const String &) const;
char operator[](size_t i) const;
+ operator ugly_string() const;
String & toupper();
String & tolower();
diff --git a/include/Exceptions.h b/include/Exceptions.h
index c66f7d5..b7885cc 100644
--- a/include/Exceptions.h
+++ b/include/Exceptions.h
@@ -12,6 +12,7 @@ typedef int pid_t;
#endif
class String;
+struct ugly_string;
class Base {
public:
@@ -28,7 +29,8 @@ class Base {
static int pipe(int * p, int flag = 0);
static pid_t fork();
static void exit(int);
- static void printm(int level, String fmt, ...);
+ static void printm(int level, const ugly_string &, ...);
+ static void printm(int level, const char *, ...);
};
class String;
diff --git a/lib/String.cc b/lib/String.cc
index 4d7b0ff..a2c8829 100644
--- a/lib/String.cc
+++ b/lib/String.cc
@@ -130,8 +130,6 @@ const char * String::set(const char * s, ...) {
return t;
}
-/* This causes a warning: cannot pass objects of type `const String' through `...'
- but it is not really a problem. */
va_start(ap, s);
#ifdef HAVE_VSNPRINTF
vsnprintf(t, BUFSIZ, s, ap);
@@ -145,14 +143,14 @@ const char * String::set(const char * s, ...) {
return t;
}
-const char * String::set(const String & s, ...) {
+const char * String::set(const ugly_string & s, ...) {
va_list ap;
va_start(ap, s);
#ifdef HAVE_VSNPRINTF
- vsnprintf(t, BUFSIZ, s.str, ap);
+ vsnprintf(t, BUFSIZ, s.p, ap);
#else
- vsprintf(t, s.str, ap);
+ vsprintf(t, s.p, ap);
#endif
free(str);
str = Base::strdup(t);
@@ -416,6 +414,12 @@ String operator+(const char * a, const String & b) {
return String(a) + b;
}
+String::operator ugly_string() const {
+ ugly_string r;
+ r.p = str;
+ return r;
+}
+
String & String::toupper() {
for (int i = 0; i < strlen(); i++) {
str[i] = ::toupper(str[i]);
diff --git a/lib/generic.cc b/lib/generic.cc
index 1f6b61d..6e6afb9 100644
--- a/lib/generic.cc
+++ b/lib/generic.cc
@@ -28,7 +28,7 @@ char verbosity = 0;
char * heads[] = {"EE", "--", "WW", "II"};
-void Base::printm(int level, String m, ...) {
+void Base::printm(int level, const ugly_string & m, ...) {
va_list ap;
if (verbosity < level) {
@@ -40,7 +40,23 @@ void Base::printm(int level, String m, ...) {
}
va_start(ap, m);
- vfprintf(stderr, m.to_charp(), ap);
+ vfprintf(stderr, m.p, ap);
+ va_end(ap);
+}
+
+void Base::printm(int level, const char * m, ...) {
+ va_list ap;
+
+ if (verbosity < level) {
+ return;
+ }
+
+ if (level >= 0) {
+ fprintf(stderr, "(%s) ", heads[level]);
+ }
+
+ va_start(ap, m);
+ vfprintf(stderr, m, ap);
va_end(ap);
}