ListItem.c 1.61 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
/*
htop - ListItem.c
Hisham Muhammad's avatar
Hisham Muhammad committed
3
(C) 2004-2006 Hisham H. Muhammad
Hisham Muhammad's avatar
Hisham Muhammad committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "ListItem.h"
#include "String.h"
#include "Object.h"
#include "RichString.h"
#include <string.h>

#include "debug.h"

/*{

typedef struct ListItem_ {
   Object super;
   char* value;
   int key;
} ListItem;

}*/

26
#ifdef DEBUG
Hisham Muhammad's avatar
Hisham Muhammad committed
27
char* LISTITEM_CLASS = "ListItem";
28
29
30
#else
#define LISTITEM_CLASS NULL
#endif
Hisham Muhammad's avatar
Hisham Muhammad committed
31

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
static void ListItem_delete(Object* cast) {
   ListItem* this = (ListItem*)cast;
   free(this->value);
   free(this);
}

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);
}

Hisham Muhammad's avatar
Hisham Muhammad committed
47
48
ListItem* ListItem_new(char* value, int key) {
   ListItem* this = malloc(sizeof(ListItem));
49
   Object_setClass(this, LISTITEM_CLASS);
Hisham Muhammad's avatar
Hisham Muhammad committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
   ((Object*)this)->display = ListItem_display;
   ((Object*)this)->delete = ListItem_delete;
   this->value = String_copy(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;
}

const char* ListItem_getRef(ListItem* this) {
   return this->value;
}

68
int ListItem_compare(const void* cast1, const void* cast2) {
Hisham Muhammad's avatar
Hisham Muhammad committed
69
70
71
72
73
   ListItem* obj1 = (ListItem*) cast1;
   ListItem* obj2 = (ListItem*) cast2;
   return strcmp(obj1->value, obj2->value);
}