summaryrefslogtreecommitdiff
path: root/src/BString.cc
diff options
context:
space:
mode:
authorPixel <pixel@nobis-crew.org>2011-10-03 14:48:05 -0700
committerPixel <pixel@nobis-crew.org>2011-10-03 14:48:05 -0700
commit342b273234405ab76dc159d2e402bfb1ddfa1d8f (patch)
treef10d6857960313d6fc3b0aaa325ed46b8ad481fb /src/BString.cc
First commit - very basic features.
Diffstat (limited to 'src/BString.cc')
-rw-r--r--src/BString.cc108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/BString.cc b/src/BString.cc
new file mode 100644
index 0000000..61abb8c
--- /dev/null
+++ b/src/BString.cc
@@ -0,0 +1,108 @@
+#include <iconv.h>
+#include <ctype.h>
+
+#include "BString.h"
+
+void Balau::String::set(const char * fmt, va_list ap) {
+ char * t;
+ unsigned int l;
+#ifdef _WIN32
+ // Microsoft is stupid.
+ char tt[65536];
+ l = _vsnprintf(tt, sizeof(tt) - 1, fmt, ap);
+ tt[65535] = 0;
+ t = ::strdup(tt);
+#else
+ l = vasprintf(&t, fmt, ap);
+#endif
+ assign(t, l);
+ free(t);
+}
+
+int Balau::String::strchrcnt(char c) const {
+ unsigned int l = length();
+ int r = 0;
+ const char * buffer = data();
+
+ for (unsigned int i = 0; i < l; i++)
+ if (buffer[i] == c)
+ r++;
+
+ return r;
+}
+
+Balau::String & Balau::String::do_ltrim() {
+ unsigned int l = length(), s = 0;
+ const char * buffer = data();
+
+ for (unsigned int i = 0; i < l; i++)
+ if (isspace(buffer[i]))
+ s++;
+ else
+ break;
+
+ erase(0, s);
+
+ return *this;
+}
+
+Balau::String & Balau::String::do_rtrim() {
+ unsigned int l = length(), p = l;
+ const char * buffer = data();
+
+ for (unsigned int i = l - 1; i >= 0; i--)
+ if (isspace(buffer[i]))
+ p--;
+ else
+ break;
+
+ erase(p);
+
+ return *this;
+}
+
+Balau::String & Balau::String::do_upper() {
+ unsigned int l = length();
+
+ for (unsigned int i = 0; i < l; i++)
+ (*this)[i] = toupper((*this)[i]);
+
+ return *this;
+}
+
+Balau::String & Balau::String::do_lower() {
+ unsigned int l = length();
+
+ for (unsigned int i = 0; i < l; i++)
+ (*this)[i] = tolower((*this)[i]);
+
+ return *this;
+}
+
+Balau::String & Balau::String::do_iconv(const char * from, const char * _to) {
+ iconv_t cd;
+ const String to = String(_to) + "//TRANSLIT";
+
+ const char * inbuf;
+ char * outbuf, * t;
+ size_t inleft, outleft;
+
+ if ((cd = iconv_open(to.c_str(), from)) == (iconv_t) (-1))
+ return *this;
+
+ inleft = length();
+ outleft = inleft * 8;
+ inbuf = c_str();
+ t = outbuf = (char *) malloc(outleft + 1);
+ memset(t, 0, outleft + 1);
+#ifdef HAVE_PROPER_ICONV
+ ::iconv(cd, &inbuf, &inleft, &outbuf, &outleft);
+#else
+ ::iconv(cd, const_cast<char **>(&inbuf), &inleft, &outbuf, &outleft);
+#endif
+
+ assign(t, outbuf - t);
+ free(t);
+
+ return *this;
+}