RichString.c 5.25 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
3
4
5
6
/*
htop - RichString.c
(C) 2004,2011 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
Hisham Muhammad's avatar
Hisham Muhammad committed
7
8

#include "RichString.h"
Hisham's avatar
Hisham committed
9
#include "XAlloc.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
10
11
12

#include <stdlib.h>
#include <string.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
13

Hisham Muhammad's avatar
Hisham Muhammad committed
14
#define RICHSTRING_MAXLEN 350
Hisham Muhammad's avatar
Hisham Muhammad committed
15
16
17

/*{
#include "config.h"
18
#include <ctype.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
19
20

#include <assert.h>
21
#ifdef HAVE_NCURSESW_CURSES_H
22
23
24
#include <ncursesw/curses.h>
#elif HAVE_NCURSES_NCURSES_H
#include <ncurses/ncurses.h>
25
26
#elif HAVE_NCURSES_CURSES_H
#include <ncurses/curses.h>
27
28
29
30
#elif HAVE_NCURSES_H
#include <ncurses.h>
#elif HAVE_CURSES_H
#include <curses.h>
31
#endif
Hisham Muhammad's avatar
Hisham Muhammad committed
32

33
34
35
36
#ifdef HAVE_LIBNCURSESW
#include <wctype.h>
#endif

37
38
39
#define RichString_size(this) ((this)->chlen)
#define RichString_sizeVal(this) ((this).chlen)

40
41
#define RichString_begin(this) RichString (this); memset(&this, 0, sizeof(RichString)); (this).chptr = (this).chstr;
#define RichString_beginAllocated(this) memset(&this, 0, sizeof(RichString)); (this).chptr = (this).chstr;
42
#define RichString_end(this) RichString_prune(&(this));
43

44
#ifdef HAVE_LIBNCURSESW
45
46
47
#define RichString_printVal(this, y, x) mvadd_wchstr(y, x, (this).chptr)
#define RichString_printoffnVal(this, y, x, off, n) mvadd_wchnstr(y, x, (this).chptr + off, n)
#define RichString_getCharVal(this, i) ((this).chptr[i].chars[0] & 255)
48
#define RichString_setChar(this, at, ch) do{ (this)->chptr[(at)] = (CharType) { .chars = { ch, 0 } }; } while(0)
49
#define CharType cchar_t
50
#else
51
52
53
54
55
#define RichString_printVal(this, y, x) mvaddchstr(y, x, (this).chptr)
#define RichString_printoffnVal(this, y, x, off, n) mvaddchnstr(y, x, (this).chptr + off, n)
#define RichString_getCharVal(this, i) ((this).chptr[i])
#define RichString_setChar(this, at, ch) do{ (this)->chptr[(at)] = ch; } while(0)
#define CharType chtype
56
57
#endif

Hisham Muhammad's avatar
Hisham Muhammad committed
58
typedef struct RichString_ {
59
60
   int chlen;
   CharType* chptr;
Hisham Muhammad's avatar
Hisham Muhammad committed
61
   CharType chstr[RICHSTRING_MAXLEN+1];
Hisham Muhammad's avatar
Hisham Muhammad committed
62
63
64
65
} RichString;

}*/

66
67
68
69
#ifndef CLAMP
#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x)))
#endif

70
71
#define charBytes(n) (sizeof(CharType) * (n)) 

Hisham Muhammad's avatar
Hisham Muhammad committed
72
static void RichString_extendLen(RichString* this, int len) {
73
74
   if (this->chlen <= RICHSTRING_MAXLEN) {
      if (len > RICHSTRING_MAXLEN) {
Hisham's avatar
Hisham committed
75
         this->chptr = xMalloc(charBytes(len + 1));
76
         memcpy(this->chptr, this->chstr, charBytes(this->chlen));
77
78
79
      }
   } else {
      if (len <= RICHSTRING_MAXLEN) {
80
         memcpy(this->chstr, this->chptr, charBytes(len));
81
82
83
         free(this->chptr);
         this->chptr = this->chstr;
      } else {
Hisham's avatar
Hisham committed
84
         this->chptr = xRealloc(this->chptr, charBytes(len + 1));
85
86
      }
   }
87

88
89
90
91
   RichString_setChar(this, len, 0);
   this->chlen = len;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
92
#define RichString_setLen(this, len) do{ if(len < RICHSTRING_MAXLEN && this->chlen < RICHSTRING_MAXLEN) { RichString_setChar(this,len,0); this->chlen=len; } else RichString_extendLen(this,len); }while(0)
Hisham Muhammad's avatar
Hisham Muhammad committed
93

94
95
#ifdef HAVE_LIBNCURSESW

96
static inline void RichString_writeFrom(RichString* this, int attrs, const char* data_c, int from, int len) {
97
98
   wchar_t data[len+1];
   len = mbstowcs(data, data_c, len);
99
   if (len < 0)
100
      return;
101
   int newLen = from + len;
102
   RichString_setLen(this, newLen);
103
   for (int i = from, j = 0; i < newLen; i++, j++) {
104
      this->chptr[i] = (CharType) { .attr = attrs & 0xffffff, .chars = { (iswprint(data[j]) ? data[j] : '?') } };
105
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
106
107
}

108
109
inline void RichString_setAttrn(RichString* this, int attrs, int start, int finish) {
   cchar_t* ch = this->chptr + start;
110
   finish = CLAMP(finish, 0, this->chlen - 1);
111
112
113
114
   for (int i = start; i <= finish; i++) {
      ch->attr = attrs;
      ch++;
   }
115
116
}

117
int RichString_findChar(RichString* this, char c, int start) {
118
   wchar_t wc = btowc(c);
119
120
   cchar_t* ch = this->chptr + start;
   for (int i = start; i < this->chlen; i++) {
121
122
123
124
125
      if (ch->chars[0] == wc)
         return i;
      ch++;
   }
   return -1;
126
127
}

128
129
#else

130
131
static inline void RichString_writeFrom(RichString* this, int attrs, const char* data_c, int from, int len) {
   int newLen = from + len;
132
   RichString_setLen(this, newLen);
133
   for (int i = from, j = 0; i < newLen; i++, j++)
134
      this->chptr[i] = (data_c[j] >= 32 ? data_c[j] : '?') | attrs;
135
   this->chptr[newLen] = 0;
136
137
}

138
139
void RichString_setAttrn(RichString* this, int attrs, int start, int finish) {
   chtype* ch = this->chptr + start;
140
   finish = CLAMP(finish, 0, this->chlen - 1);
141
   for (int i = start; i <= finish; i++) {
142
143
      *ch = (*ch & 0xff) | attrs;
      ch++;
Hisham Muhammad's avatar
Hisham Muhammad committed
144
145
146
   }
}

147
148
149
int RichString_findChar(RichString* this, char c, int start) {
   chtype* ch = this->chptr + start;
   for (int i = start; i < this->chlen; i++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
150
      if ((*ch & 0xff) == (chtype) c)
151
         return i;
152
      ch++;
Hisham Muhammad's avatar
Hisham Muhammad committed
153
   }
154
155
156
157
158
   return -1;
}

#endif

Hisham Muhammad's avatar
Hisham Muhammad committed
159
void RichString_prune(RichString* this) {
160
161
   if (this->chlen > RICHSTRING_MAXLEN)
      free(this->chptr);
162
   memset(this, 0, sizeof(RichString));
163
   this->chptr = this->chstr;
Hisham Muhammad's avatar
Hisham Muhammad committed
164
165
}

166
167
void RichString_setAttr(RichString* this, int attrs) {
   RichString_setAttrn(this, attrs, 0, this->chlen - 1);
168
169
}

170
171
172
173
174
175
void RichString_append(RichString* this, int attrs, const char* data) {
   RichString_writeFrom(this, attrs, data, this->chlen, strlen(data));
}

void RichString_appendn(RichString* this, int attrs, const char* data, int len) {
   RichString_writeFrom(this, attrs, data, this->chlen, len);
176
177
}

Hisham Muhammad's avatar
Hisham Muhammad committed
178
void RichString_write(RichString* this, int attrs, const char* data) {
179
   RichString_writeFrom(this, attrs, data, 0, strlen(data));
180
}