RichString.c 4.59 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_CURSES_H
16
#include <curses.h>
17
18
19
20
21
22
#elif HAVE_NCURSES_H
#include <ncurses.h>
#elif HAVE_NCURSESW_CURSES_H
#include <ncursesw/curses.h>
#elif HAVE_NCURSES_NCURSES_H
#include <ncurses/ncurses.h>
23
#endif
Hisham Muhammad's avatar
Hisham Muhammad committed
24
25
26
27
28

#define RICHSTRING_MAXLEN 300

/*{

29
30
31
32
33
34
#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));
35

36
#ifdef HAVE_LIBNCURSESW
37
38
39
40
41
#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
42
#else
43
44
45
46
47
#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
48
49
#endif

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

}*/

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

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#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;
}

83
84
#ifdef HAVE_LIBNCURSESW

Hisham Muhammad's avatar
Hisham Muhammad committed
85
inline void RichString_appendn(RichString* this, int attrs, const char* data_c, int len) {
86
87
   wchar_t data[len+1];
   len = mbstowcs(data, data_c, len);
88
89
   if (len<0)
      return;
90
91
92
93
94
95
96
   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;
97
   }
98
   this->chptr[newLen].chars[0] = 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
99
100
}

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

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

120
121
#else

Hisham Muhammad's avatar
Hisham Muhammad committed
122
inline void RichString_appendn(RichString* this, int attrs, const char* data_c, int len) {
123
124
125
126
127
128
   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;
129
130
}

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

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

#endif

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

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

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

Hisham Muhammad's avatar
Hisham Muhammad committed
166
void RichString_write(RichString* this, int attrs, const char* data) {
167
168
   RichString_setLen(this, 0);
   RichString_appendn(this, attrs, data, strlen(data));
Hisham Muhammad's avatar
Hisham Muhammad committed
169
}