summaryrefslogtreecommitdiff
path: root/Source/Library/Common/ByteArray.class.cpp
diff options
context:
space:
mode:
authorAlexis211 <alexis211@gmail.com>2009-10-18 15:27:34 +0200
committerAlexis211 <alexis211@gmail.com>2009-10-18 15:27:34 +0200
commit323e12f1f9ab33df15dcfed210e807561d98fa8c (patch)
tree7d76e6932d4a979a1f2bfafc94478b66b1479bbc /Source/Library/Common/ByteArray.class.cpp
parentbc2eccdd11c27029096fb3e891073503eb269e27 (diff)
downloadMelon-323e12f1f9ab33df15dcfed210e807561d98fa8c.tar.gz
Melon-323e12f1f9ab33df15dcfed210e807561d98fa8c.zip
Re-organized everything
Diffstat (limited to 'Source/Library/Common/ByteArray.class.cpp')
-rw-r--r--Source/Library/Common/ByteArray.class.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/Source/Library/Common/ByteArray.class.cpp b/Source/Library/Common/ByteArray.class.cpp
new file mode 100644
index 0000000..2a42702
--- /dev/null
+++ b/Source/Library/Common/ByteArray.class.cpp
@@ -0,0 +1,59 @@
+#include "ByteArray.class.h"
+
+//Define size of a uchar_repr_t
+#define CHARSZ(x) (x.c[0] == 0 ? 0 : (x.c[1] == 0 ? 1 : (x.c[2] == 0 ? 2 : (x.c[3] == 0 ? 3 : 4))))
+
+using namespace CMem;
+
+ByteArray::ByteArray(const char* c) : BasicString<u8int>() {
+ m_length = strlen(c);
+ memcpy(m_string, (u8int*)c, m_length);
+}
+
+void ByteArray::affect(const String &string, u8int encoding) {
+ m_length = 0;
+ for (u32int i = 0; i < string.size(); i++) {
+ uchar_repr_t a = string[i].encode(encoding);
+ m_length += CHARSZ(a);
+ }
+ if (m_string != 0) delete m_string;
+ if (m_length == 0) {
+ m_string = 0;
+ return;
+ }
+ m_string = new u8int[m_length];
+ u32int x = 0;
+ for (u32int i = 0; i < string.size(); i++) {
+ uchar_repr_t a = string[i].encode(encoding);
+ memcpy(m_string + x, (u8int*)a.c, CHARSZ(a));
+ x += CHARSZ(a);
+ }
+}
+
+void ByteArray::resize(u32int size) {
+ if (size == m_length) return;
+ if (size == 0) {
+ delete m_string;
+ m_length = 0;
+ m_string = 0;
+ }
+ u8int *nd = new u8int[size];
+ if (size < m_length) {
+ memcpy(nd, m_string, size);
+ } else {
+ memcpy(nd, m_string, m_length);
+ memset(nd + m_length, 0, size - m_length);
+ }
+ delete m_string;
+ m_string = nd;
+ m_length = size;
+}
+
+String ByteArray::toString (u8int encoding) {
+ char* c = new char[m_length + 1];
+ memcpy((u8int*)c, m_string, m_length);
+ c[m_length] = 0; //Add NULL terminator
+ String r(c, encoding);
+ delete c;
+ return r;
+}