diff options
author | pescuma <pescuma@c086bb3d-8645-0410-b8da-73a8550f86e7> | 2008-11-17 00:15:02 +0000 |
---|---|---|
committer | pescuma <pescuma@c086bb3d-8645-0410-b8da-73a8550f86e7> | 2008-11-17 00:15:02 +0000 |
commit | 974495437f862c1f26fc70760bb72c2db6fcdd41 (patch) | |
tree | 324f8edcc09b21d40f65a840bf0819c71762b419 | |
parent | 6b5a5851340510255cde717b74697fa693679732 (diff) |
utils: Added scope<> and made Buffer more user friendly
git-svn-id: http://pescuma.googlecode.com/svn/trunk/Miranda@115 c086bb3d-8645-0410-b8da-73a8550f86e7
-rw-r--r-- | Plugins/utils/mir_buffer.h | 74 | ||||
-rw-r--r-- | Plugins/utils/mir_scope.h | 35 |
2 files changed, 102 insertions, 7 deletions
diff --git a/Plugins/utils/mir_buffer.h b/Plugins/utils/mir_buffer.h index f783c65..1bcdab3 100644 --- a/Plugins/utils/mir_buffer.h +++ b/Plugins/utils/mir_buffer.h @@ -120,12 +120,24 @@ class Buffer size_t len;
T *str;
- Buffer() : len(0), str(NULL), size(0) {}
+ Buffer() : str(NULL), size(0), len(0)
+ {
+ alloc(1);
+ pack();
+ }
- Buffer(T in)
+ Buffer(T in) : str(NULL), size(0), len(0)
{
- str = in;
- size = len = __blen(in);
+ if (in == NUL)
+ {
+ alloc(1);
+ pack();
+ }
+ else
+ {
+ str = in;
+ size = len = __blen(str);
+ }
}
~Buffer()
@@ -145,9 +157,9 @@ class Buffer {
size = total + 256 - total % 256;
if (str == NULL)
- str = (T *) mir_alloc(size * sizeof(T));
+ str = (T *) malloc(size * sizeof(T));
else
- str = (T *) mir_realloc(str, size * sizeof(T));
+ str = (T *) realloc(str, size * sizeof(T));
}
}
@@ -155,7 +167,7 @@ class Buffer {
if (str != NULL)
{
- mir_free(str);
+ ::free(str);
str = NULL;
len = size = 0;
}
@@ -164,6 +176,7 @@ class Buffer void clear()
{
len = 0;
+ pack();
}
void append(T app)
@@ -172,6 +185,7 @@ class Buffer str[len] = app;
len++;
+ pack();
}
void appendn(size_t n, T app)
@@ -183,10 +197,13 @@ class Buffer str[len] = app;
len++;
}
+ pack();
}
void append(const char *app, size_t appLen = -1)
{
+ if (app == NULL)
+ return;
if (appLen == -1)
appLen = __blen(app);
@@ -195,10 +212,13 @@ class Buffer __bcopy(&str[len], app, appLen);
len += appLen;
+ pack();
}
void append(const WCHAR *app, size_t appLen = -1)
{
+ if (app == NULL)
+ return;
if (appLen == -1)
appLen = __blen(app);
@@ -207,10 +227,13 @@ class Buffer __bcopy(&str[len], app, appLen);
len += appLen;
+ pack();
}
void append(const Buffer<char> &app)
{
+ if (app.str == NULL)
+ return;
size_t appLen = app.len;
size_t total = len + appLen + 1;
@@ -218,6 +241,7 @@ class Buffer __bcopy(&str[len], app.str, appLen);
len += appLen;
+ pack();
}
void append(const Buffer<WCHAR> &app)
@@ -229,6 +253,7 @@ class Buffer __bcopy(&str[len], app.str , appLen);
len += appLen;
+ pack();
}
void appendPrintf(const T *app, ...)
@@ -242,6 +267,7 @@ class Buffer if (total < 0)
total = size - len - 1;
len += total;
+ pack();
}
void insert(size_t pos, T *app, size_t appLen = -1)
@@ -261,6 +287,7 @@ class Buffer memmove(&str[pos], app, appLen * sizeof(T));
len += appLen;
+ pack();
}
void replace(size_t start, size_t end, T *app, size_t appLen = -1)
@@ -287,6 +314,7 @@ class Buffer memmove(&str[start], app, appLen * sizeof(T));
len += appLen - oldLen;
+ pack();
}
void replaceAll(T find, T replace)
@@ -294,6 +322,7 @@ class Buffer for(size_t i = 0; i < len; i++)
if (str[len] == find)
str[len] = replace;
+ pack();
}
void translate()
@@ -306,6 +335,7 @@ class Buffer len = __blen(tmp);
alloc(len + 1);
memmove(str, tmp, len * sizeof(T));
+ pack();
}
void reverse()
@@ -316,6 +346,7 @@ class Buffer str[i] = str[len-i-1];
str[len-i-1] = tmp;
}
+ pack();
}
T *appender(size_t appLen)
@@ -356,6 +387,7 @@ class Buffer || str[e] == (T)'\r'
|| str[e] == (T)'\n'); e--) ;
len = e+1;
+ pack();
}
void trimLeft()
@@ -373,6 +405,7 @@ class Buffer memmove(str, &str[s], (len - s) * sizeof(T));
len -= s;
}
+ pack();
}
void trim()
@@ -393,6 +426,33 @@ class Buffer return *this;
}
+ Buffer<T>& operator+=(const Buffer<T> &txt)
+ {
+ append(txt);
+ return *this;
+ }
+
+ Buffer<T>& operator=(const char *txt)
+ {
+ clear();
+ append(txt);
+ return *this;
+ }
+
+ Buffer<T>& operator=(const WCHAR *txt)
+ {
+ clear();
+ append(txt);
+ return *this;
+ }
+
+ Buffer<T>& operator=(const Buffer<T> &txt)
+ {
+ clear();
+ append(txt);
+ return *this;
+ }
+
private:
size_t size;
diff --git a/Plugins/utils/mir_scope.h b/Plugins/utils/mir_scope.h new file mode 100644 index 0000000..a18bf55 --- /dev/null +++ b/Plugins/utils/mir_scope.h @@ -0,0 +1,35 @@ +#ifndef __PTR_H__
+# define __PTR_H__
+
+
+template<class T>
+class scope
+{
+public:
+ scope(T t) : p(t) {}
+ ~scope() { free(); }
+
+ void free()
+ {
+ if (p != NULL)
+ mir_free(p);
+ p = NULL;
+ }
+
+// T operator->() const { return p; }
+ operator T() const { return p; }
+
+ T detach()
+ {
+ T ret = p;
+ p = NULL;
+ return ret;
+ }
+
+private:
+ T p;
+};
+
+
+
+#endif // __PTR_H__
|