RichString.c 3.46 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
3

#include "RichString.h"

4
5
6
7
8
#ifndef CONFIG_H
#define CONFIG_H
#include "config.h"
#endif

Hisham Muhammad's avatar
Hisham Muhammad committed
9
10
#include <stdlib.h>
#include <string.h>
11
#include <ctype.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
12
13
14

#include "debug.h"
#include <assert.h>
15
#ifdef HAVE_LIBNCURSESW
Hisham Muhammad's avatar
Hisham Muhammad committed
16
#include <ncursesw/curses.h>
17
18
#else
#include <curses.h>
19
#endif
Hisham Muhammad's avatar
Hisham Muhammad committed
20
21
22
23
24

#define RICHSTRING_MAXLEN 300

/*{

25
26
27
#define RichString_init(this) (this)->len = 0
#define RichString_initVal(this) (this).len = 0

28
29
30
31
32
33
34
35
36
37
#ifdef HAVE_LIBNCURSESW
#define RichString_printVal(this, y, x) mvadd_wchstr(y, x, this.chstr)
#define RichString_printoffnVal(this, y, x, off, n) mvadd_wchnstr(y, x, this.chstr + off, n)
#define RichString_getCharVal(this, i) (this.chstr[i].chars[0] & 255)
#else
#define RichString_printVal(this, y, x) mvaddchstr(y, x, this.chstr)
#define RichString_printoffnVal(this, y, x, off, n) mvaddchnstr(y, x, this.chstr + off, n)
#define RichString_getCharVal(this, i) (this.chstr[i])
#endif

Hisham Muhammad's avatar
Hisham Muhammad committed
38
39
typedef struct RichString_ {
   int len;
40
41
42
#ifdef HAVE_LIBNCURSESW
   cchar_t chstr[RICHSTRING_MAXLEN+1];
#else
Hisham Muhammad's avatar
Hisham Muhammad committed
43
   chtype chstr[RICHSTRING_MAXLEN+1];
44
#endif
Hisham Muhammad's avatar
Hisham Muhammad committed
45
46
47
48
49
50
51
52
} RichString;

}*/

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

53
54
#ifdef HAVE_LIBNCURSESW

Hisham Muhammad's avatar
Hisham Muhammad committed
55
inline void RichString_appendn(RichString* this, int attrs, const char* data_c, int len) {
56
57
58
59
   wchar_t data[RICHSTRING_MAXLEN];
   len = mbstowcs(data, data_c, RICHSTRING_MAXLEN);
   if (len<0)
      return;
60
   int last = MIN(RICHSTRING_MAXLEN - 1, len + this->len);
61
62
63
64
65
66
   for (int i = this->len, j = 0; i < last; i++, j++) {
      memset(&this->chstr[i], 0, sizeof(this->chstr[i]));
      this->chstr[i].chars[0] = data[j];
      this->chstr[i].attr = attrs;
   }
   this->chstr[last].chars[0] = 0;
67
   this->len = last;
Hisham Muhammad's avatar
Hisham Muhammad committed
68
69
}

70
71
72
73
74
75
inline void RichString_setAttrn(RichString *this, int attrs, int start, int finish) {
   cchar_t* ch = this->chstr + start;
   for (int i = start; i <= finish; i++) {
      ch->attr = attrs;
      ch++;
   }
76
77
}

78
79
80
81
82
83
84
85
86
int RichString_findChar(RichString *this, char c, int start) {
   wchar_t wc = btowc(c);
   cchar_t* ch = this->chstr + start;
   for (int i = start; i < this->len; i++) {
      if (ch->chars[0] == wc)
         return i;
      ch++;
   }
   return -1;
87
88
}

89
90
#else

Hisham Muhammad's avatar
Hisham Muhammad committed
91
inline void RichString_appendn(RichString* this, int attrs, const char* data_c, int len) {
92
93
   int last = MIN(RICHSTRING_MAXLEN - 1, len + this->len);
   for (int i = this->len, j = 0; i < last; i++, j++)
94
95
      this->chstr[i] = (isprint(data_c[j]) ? data_c[j] : '?') | attrs;

96
97
98
99
100
101
102
   this->chstr[last] = 0;
   this->len = last;
}

void RichString_setAttrn(RichString *this, int attrs, int start, int finish) {
   chtype* ch = this->chstr + start;
   for (int i = start; i <= finish; i++) {
103
104
      *ch = (*ch & 0xff) | attrs;
      ch++;
Hisham Muhammad's avatar
Hisham Muhammad committed
105
106
107
   }
}

108
109
110
int RichString_findChar(RichString *this, char c, int start) {
   chtype* ch = this->chstr + start;
   for (int i = start; i < this->len; i++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
111
      if ((*ch & 0xff) == (chtype) c)
112
         return i;
113
      ch++;
Hisham Muhammad's avatar
Hisham Muhammad committed
114
   }
115
116
117
118
119
   return -1;
}

#endif

Hisham Muhammad's avatar
Hisham Muhammad committed
120
121
122
123
void RichString_prune(RichString* this) {
   this->len = 0;
}

124
125
126
127
void RichString_setAttr(RichString *this, int attrs) {
   RichString_setAttrn(this, attrs, 0, this->len - 1);
}

Hisham Muhammad's avatar
Hisham Muhammad committed
128
inline void RichString_append(RichString* this, int attrs, const char* data) {
129
130
131
   RichString_appendn(this, attrs, data, strlen(data));
}

Hisham Muhammad's avatar
Hisham Muhammad committed
132
void RichString_write(RichString* this, int attrs, const char* data) {
133
134
   RichString_init(this);
   RichString_append(this, attrs, data);
Hisham Muhammad's avatar
Hisham Muhammad committed
135
136
}

Hisham Muhammad's avatar
Hisham Muhammad committed
137
RichString RichString_quickString(int attrs, const char* data) {
138
139
   RichString str;
   RichString_initVal(str);
Hisham Muhammad's avatar
Hisham Muhammad committed
140
141
142
   RichString_write(&str, attrs, data);
   return str;
}