ListItem.c 2.02 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-2011 Hisham H. Muhammad
Hisham Muhammad's avatar
Hisham Muhammad committed
4
5
6
7
8
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "ListItem.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
9
10

#include "CRT.h"
David Hunt's avatar
David Hunt committed
11
#include "StringUtils.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
12
13
#include "RichString.h"

Hisham Muhammad's avatar
Hisham Muhammad committed
14
15
16
17
#include <string.h>
#include <assert.h>
#include <stdlib.h>

Hisham Muhammad's avatar
Hisham Muhammad committed
18
/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
19
#include "Object.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
20
21
22
23
24

typedef struct ListItem_ {
   Object super;
   char* value;
   int key;
Hisham Muhammad's avatar
Hisham Muhammad committed
25
   bool moving;
Hisham Muhammad's avatar
Hisham Muhammad committed
26
27
28
29
} ListItem;

}*/

30
void ListItem_delete(Object* cast) {
31
32
33
34
35
   ListItem* this = (ListItem*)cast;
   free(this->value);
   free(this);
}

36
void ListItem_display(Object* cast, RichString* out) {
Hisham Muhammad's avatar
Hisham Muhammad committed
37
   ListItem* const this = (ListItem*)cast;
38
   assert (this != NULL);
39
   /*
40
41
   int len = strlen(this->value)+1;
   char buffer[len+1];
42
   xSnprintf(buffer, len, "%s", this->value);
43
   */
Hisham Muhammad's avatar
Hisham Muhammad committed
44
   if (this->moving) {
45
46
      RichString_write(out, CRT_colors[DEFAULT_COLOR],
#ifdef HAVE_LIBNCURSESW
47
            CRT_utf8 ? "↕ " :
48
#endif
49
            "+ ");
Hisham Muhammad's avatar
Hisham Muhammad committed
50
51
52
53
   } else {
      RichString_prune(out);
   }
   RichString_append(out, CRT_colors[DEFAULT_COLOR], this->value/*buffer*/);
54
55
}

56
57
ObjectClass ListItem_class = {
   .display = ListItem_display,
58
59
   .delete = ListItem_delete,
   .compare = ListItem_compare
60
61
};

62
void ListItem_init(ListItem* this, const char* value, int key) {
Hisham's avatar
Hisham committed
63
   this->value = xStrdup(value);
Hisham Muhammad's avatar
Hisham Muhammad committed
64
   this->key = key;
Hisham Muhammad's avatar
Hisham Muhammad committed
65
   this->moving = false;
66
67
68
69
70
}

ListItem* ListItem_new(const char* value, int key) {
   ListItem* this = AllocThis(ListItem);
   ListItem_init(this, value, key);
Hisham Muhammad's avatar
Hisham Muhammad committed
71
72
73
   return this;
}

74
75
76
77
void ListItem_append(ListItem* this, const char* text) {
   int oldLen = strlen(this->value);
   int textLen = strlen(text);
   int newLen = strlen(this->value) + textLen;
Hisham's avatar
Hisham committed
78
   this->value = xRealloc(this->value, newLen + 1);
79
80
   memcpy(this->value + oldLen, text, textLen);
   this->value[newLen] = '\0';
Hisham Muhammad's avatar
Hisham Muhammad committed
81
82
83
84
85
86
}

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

87
long ListItem_compare(const void* cast1, const void* cast2) {
Hisham Muhammad's avatar
Hisham Muhammad committed
88
89
90
91
92
   ListItem* obj1 = (ListItem*) cast1;
   ListItem* obj2 = (ListItem*) cast2;
   return strcmp(obj1->value, obj2->value);
}