aboutsummaryrefslogtreecommitdiffstats
path: root/ListItem.c
diff options
context:
space:
mode:
Diffstat (limited to 'ListItem.c')
-rw-r--r--ListItem.c46
1 files changed, 25 insertions, 21 deletions
diff --git a/ListItem.c b/ListItem.c
index 2568ef4..140ebdc 100644
--- a/ListItem.c
+++ b/ListItem.c
@@ -1,19 +1,22 @@
/*
htop - ListItem.c
-(C) 2004-2010 Hisham H. Muhammad
+(C) 2004-2011 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "ListItem.h"
+
+#include "CRT.h"
#include "String.h"
-#include "Object.h"
#include "RichString.h"
-#include <string.h>
-#include "debug.h"
+#include <string.h>
+#include <assert.h>
+#include <stdlib.h>
/*{
+#include "Object.h"
typedef struct ListItem_ {
Object super;
@@ -23,12 +26,6 @@ typedef struct ListItem_ {
}*/
-#ifdef DEBUG
-char* LISTITEM_CLASS = "ListItem";
-#else
-#define LISTITEM_CLASS NULL
-#endif
-
static void ListItem_delete(Object* cast) {
ListItem* this = (ListItem*)cast;
free(this->value);
@@ -38,27 +35,34 @@ static void ListItem_delete(Object* cast) {
static void ListItem_display(Object* cast, RichString* out) {
ListItem* this = (ListItem*)cast;
assert (this != NULL);
+ /*
int len = strlen(this->value)+1;
char buffer[len+1];
snprintf(buffer, len, "%s", this->value);
- RichString_write(out, CRT_colors[DEFAULT_COLOR], buffer);
+ */
+ RichString_write(out, CRT_colors[DEFAULT_COLOR], this->value/*buffer*/);
}
+ObjectClass ListItem_class = {
+ .display = ListItem_display,
+ .delete = ListItem_delete,
+ .compare = ListItem_compare
+};
+
ListItem* ListItem_new(const char* value, int key) {
- ListItem* this = malloc(sizeof(ListItem));
- Object_setClass(this, LISTITEM_CLASS);
- ((Object*)this)->display = ListItem_display;
- ((Object*)this)->delete = ListItem_delete;
- this->value = String_copy(value);
+ ListItem* this = AllocThis(ListItem);
+ this->value = strdup(value);
this->key = key;
return this;
}
-void ListItem_append(ListItem* this, char* text) {
- char* buf = malloc(strlen(this->value) + strlen(text) + 1);
- sprintf(buf, "%s%s", this->value, text);
- free(this->value);
- this->value = buf;
+void ListItem_append(ListItem* this, const char* text) {
+ int oldLen = strlen(this->value);
+ int textLen = strlen(text);
+ int newLen = strlen(this->value) + textLen;
+ this->value = realloc(this->value, newLen + 1);
+ memcpy(this->value + oldLen, text, textLen);
+ this->value[newLen] = '\0';
}
const char* ListItem_getRef(ListItem* this) {