RichString.c 5.05 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
9
10
11

#include "RichString.h"

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

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

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

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

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

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

39
40
#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;
41
#define RichString_end(this) RichString_prune(&(this));
42

43
#ifdef HAVE_LIBNCURSESW
44
45
46
#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)
47
#define RichString_setChar(this, at, ch) do{ (this)->chptr[(at)] = (CharType) { .chars = { ch, 0 } }; } while(0)
48
#define CharType cchar_t
49
#else
50
51
52
53
54
#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
55
56
#endif

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

}*/

65
66
#define charBytes(n) (sizeof(CharType) * (n)) 

Hisham Muhammad's avatar
Hisham Muhammad committed
67
static void RichString_extendLen(RichString* this, int len) {
68
69
   if (this->chlen <= RICHSTRING_MAXLEN) {
      if (len > RICHSTRING_MAXLEN) {
70
71
         this->chptr = malloc(charBytes(len + 1));
         memcpy(this->chptr, this->chstr, charBytes(this->chlen));
72
73
74
      }
   } else {
      if (len <= RICHSTRING_MAXLEN) {
75
         memcpy(this->chstr, this->chptr, charBytes(len));
76
77
78
         free(this->chptr);
         this->chptr = this->chstr;
      } else {
79
         this->chptr = realloc(this->chptr, charBytes(len + 1));
80
81
      }
   }
82

83
84
85
86
   RichString_setChar(this, len, 0);
   this->chlen = len;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
87
#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
88

89
90
#ifdef HAVE_LIBNCURSESW

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

103
104
inline void RichString_setAttrn(RichString* this, int attrs, int start, int finish) {
   cchar_t* ch = this->chptr + start;
105
106
107
108
   for (int i = start; i <= finish; i++) {
      ch->attr = attrs;
      ch++;
   }
109
110
}

111
int RichString_findChar(RichString* this, char c, int start) {
112
   wchar_t wc = btowc(c);
113
114
   cchar_t* ch = this->chptr + start;
   for (int i = start; i < this->chlen; i++) {
115
116
117
118
119
      if (ch->chars[0] == wc)
         return i;
      ch++;
   }
   return -1;
120
121
}

122
123
#else

124
125
static inline void RichString_writeFrom(RichString* this, int attrs, const char* data_c, int from, int len) {
   int newLen = from + len;
126
   RichString_setLen(this, newLen);
127
   for (int i = from, j = 0; i < newLen; i++, j++)
128
      this->chptr[i] = (data_c[j] >= 32 ? data_c[j] : '?') | attrs;
129
   this->chptr[newLen] = 0;
130
131
}

132
133
void RichString_setAttrn(RichString* this, int attrs, int start, int finish) {
   chtype* ch = this->chptr + start;
134
   for (int i = start; i <= finish; i++) {
135
136
      *ch = (*ch & 0xff) | attrs;
      ch++;
Hisham Muhammad's avatar
Hisham Muhammad committed
137
138
139
   }
}

140
141
142
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
143
      if ((*ch & 0xff) == (chtype) c)
144
         return i;
145
      ch++;
Hisham Muhammad's avatar
Hisham Muhammad committed
146
   }
147
148
149
150
151
   return -1;
}

#endif

Hisham Muhammad's avatar
Hisham Muhammad committed
152
void RichString_prune(RichString* this) {
153
154
   if (this->chlen > RICHSTRING_MAXLEN)
      free(this->chptr);
155
   memset(this, 0, sizeof(RichString));
156
   this->chptr = this->chstr;
Hisham Muhammad's avatar
Hisham Muhammad committed
157
158
}

159
160
void RichString_setAttr(RichString* this, int attrs) {
   RichString_setAttrn(this, attrs, 0, this->chlen - 1);
161
162
}

163
164
165
166
167
168
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);
169
170
}

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