RichString.c 1.44 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include "RichString.h"

#include <stdlib.h>
#include <string.h>
#include <curses.h>

#include "debug.h"
#include <assert.h>

#define RICHSTRING_MAXLEN 300

/*{

15
16
17
#define RichString_init(this) (this)->len = 0
#define RichString_initVal(this) (this).len = 0

Hisham Muhammad's avatar
Hisham Muhammad committed
18
19
20
21
22
23
24
25
26
27
28
29
typedef struct RichString_ {
   int len;
   chtype chstr[RICHSTRING_MAXLEN+1];
} RichString;

}*/

#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif

inline void RichString_appendn(RichString* this, int attrs, char* data, int len) {
30
31
32
33
34
   int last = MIN(RICHSTRING_MAXLEN - 1, len + this->len);
   for (int i = this->len, j = 0; i < last; i++, j++)
      this->chstr[i] = data[j] | attrs;
   this->chstr[last] = 0;
   this->len = last;
Hisham Muhammad's avatar
Hisham Muhammad committed
35
36
}

37
38
39
40
41
42
43
44
45
inline void RichString_append(RichString* this, int attrs, char* data) {
   RichString_appendn(this, attrs, data, strlen(data));
}

void RichString_write(RichString* this, int attrs, char* data) {
   RichString_init(this);
   RichString_append(this, attrs, data);
}

Hisham Muhammad's avatar
Hisham Muhammad committed
46
void RichString_setAttr(RichString *this, int attrs) {
47
   chtype* ch = this->chstr;
Hisham Muhammad's avatar
Hisham Muhammad committed
48
   for (int i = 0; i < this->len; i++) {
49
50
      *ch = (*ch & 0xff) | attrs;
      ch++;
Hisham Muhammad's avatar
Hisham Muhammad committed
51
52
53
54
   }
}

void RichString_applyAttr(RichString *this, int attrs) {
55
56
57
58
   chtype* ch = this->chstr;
   for (int i = 0; i < this->len; i++) {
      *ch |= attrs;
      ch++;
Hisham Muhammad's avatar
Hisham Muhammad committed
59
60
61
62
   }
}

RichString RichString_quickString(int attrs, char* data) {
63
64
   RichString str;
   RichString_initVal(str);
Hisham Muhammad's avatar
Hisham Muhammad committed
65
66
67
   RichString_write(&str, attrs, data);
   return str;
}