RichString.c 4.47 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
28
29
30
#define RichString_size(this) ((this)->chlen)
#define RichString_sizeVal(this) ((this).chlen)

#define RichString_begin(this) RichString (this); (this).chlen = 0; (this).chptr = (this).chstr;
#define RichString_beginAllocated(this) (this).chlen = 0; (this).chptr = (this).chstr;
#define RichString_end(this) RichString_prune(&(this));
31

32
#ifdef HAVE_LIBNCURSESW
33
34
35
36
37
#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)
#define RichString_setChar(this, at, ch) do{ (this)->chptr[(at)].chars[0] = ch; } while(0)
#define CharType cchar_t
38
#else
39
40
41
42
43
#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
44
45
#endif

Hisham Muhammad's avatar
Hisham Muhammad committed
46
typedef struct RichString_ {
47
48
49
   int chlen;
   CharType chstr[RICHSTRING_MAXLEN+1];
   CharType* chptr;
Hisham Muhammad's avatar
Hisham Muhammad committed
50
51
52
53
54
55
56
57
} RichString;

}*/

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

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#define charBytes(n) (sizeof(CharType) * (n)) 

static inline void RichString_setLen(RichString* this, int len) {
   if (this->chlen <= RICHSTRING_MAXLEN) {
      if (len > RICHSTRING_MAXLEN) {
         this->chptr = malloc(charBytes(len+1));
         memcpy(this->chptr, this->chstr, charBytes(this->chlen+1));
      }
   } else {
      if (len <= RICHSTRING_MAXLEN) {
         memcpy(this->chstr, this->chptr, charBytes(this->chlen));
         free(this->chptr);
         this->chptr = this->chstr;
      } else {
         this->chptr = realloc(this->chptr, charBytes(len+1));
      }
   }
   RichString_setChar(this, len, 0);
   this->chlen = len;
}

79
80
#ifdef HAVE_LIBNCURSESW

Hisham Muhammad's avatar
Hisham Muhammad committed
81
inline void RichString_appendn(RichString* this, int attrs, const char* data_c, int len) {
82
83
   wchar_t data[len+1];
   len = mbstowcs(data, data_c, len);
84
85
   if (len<0)
      return;
86
87
88
89
90
91
92
   int oldLen = this->chlen;
   int newLen = len + oldLen;
   RichString_setLen(this, newLen);
   for (int i = oldLen, j = 0; i < newLen; i++, j++) {
      memset(&this->chptr[i], 0, sizeof(this->chptr[i]));
      this->chptr[i].chars[0] = data[j];
      this->chptr[i].attr = attrs;
93
   }
94
   this->chptr[newLen].chars[0] = 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
95
96
}

97
98
inline void RichString_setAttrn(RichString* this, int attrs, int start, int finish) {
   cchar_t* ch = this->chptr + start;
99
100
101
102
   for (int i = start; i <= finish; i++) {
      ch->attr = attrs;
      ch++;
   }
103
104
}

105
int RichString_findChar(RichString* this, char c, int start) {
106
   wchar_t wc = btowc(c);
107
108
   cchar_t* ch = this->chptr + start;
   for (int i = start; i < this->chlen; i++) {
109
110
111
112
113
      if (ch->chars[0] == wc)
         return i;
      ch++;
   }
   return -1;
114
115
}

116
117
#else

Hisham Muhammad's avatar
Hisham Muhammad committed
118
inline void RichString_appendn(RichString* this, int attrs, const char* data_c, int len) {
119
120
121
122
123
124
   int oldLen = this->chlen;
   int newLen = len + oldLen;
   RichString_setLen(this, newLen);
   for (int i = oldLen, j = 0; i < newLen; i++, j++)
      this->chptr[i] = (isprint(data_c[j]) ? data_c[j] : '?') | attrs;
   this->chptr[newLen] = 0;
125
126
}

127
128
void RichString_setAttrn(RichString* this, int attrs, int start, int finish) {
   chtype* ch = this->chptr + start;
129
   for (int i = start; i <= finish; i++) {
130
131
      *ch = (*ch & 0xff) | attrs;
      ch++;
Hisham Muhammad's avatar
Hisham Muhammad committed
132
133
134
   }
}

135
136
137
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
138
      if ((*ch & 0xff) == (chtype) c)
139
         return i;
140
      ch++;
Hisham Muhammad's avatar
Hisham Muhammad committed
141
   }
142
143
144
145
146
   return -1;
}

#endif

Hisham Muhammad's avatar
Hisham Muhammad committed
147
void RichString_prune(RichString* this) {
148
149
150
151
   if (this->chlen > RICHSTRING_MAXLEN)
      free(this->chptr);
   this->chptr = this->chstr;
   this->chlen = 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
152
153
}

154
155
void RichString_setAttr(RichString* this, int attrs) {
   RichString_setAttrn(this, attrs, 0, this->chlen - 1);
156
157
}

Hisham Muhammad's avatar
Hisham Muhammad committed
158
inline void RichString_append(RichString* this, int attrs, const char* data) {
159
160
161
   RichString_appendn(this, attrs, data, strlen(data));
}

Hisham Muhammad's avatar
Hisham Muhammad committed
162
void RichString_write(RichString* this, int attrs, const char* data) {
163
164
   RichString_setLen(this, 0);
   RichString_appendn(this, attrs, data, strlen(data));
Hisham Muhammad's avatar
Hisham Muhammad committed
165
}