-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpara.cpp
149 lines (128 loc) · 2.05 KB
/
para.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "para.h"
#include <stdio.h>
Link *
Link::findLink(int offset)
{
Link *ptr = this;
while (ptr) {
if (ptr->sidx<=offset && ptr->eidx>offset) {
break;
}
ptr = ptr->next;
}
return ptr;
}
ParaData::ParaData(int len)
{
m_ref = 1;
m_len = len;
m_ind = 0;
m_link = NULL;
m_attr = new Attr[len];
m_lines = 0;
memset(m_attr, 0, len*sizeof(Attr));
}
ParaData::~ParaData()
{
delete m_attr;
delete m_link;
}
static QString empty_str = "";
static Attr empty_attr[1];
Paragraph::Paragraph()
{
m_para = NULL;
m_attr = empty_attr;
m_str = &empty_str;
}
Paragraph::Paragraph(const Paragraph&p)
{
if (p.m_para) {
m_para = p.m_para;
m_str = &p.m_para->m_str;
m_attr = p.m_para->m_attr;
this->grab();
} else {
m_para = NULL;
m_str = &empty_str;
m_attr = empty_attr;
}
}
Paragraph::Paragraph(int len)
{
if (len<1) {
m_para = NULL;
m_attr = NULL;
m_str = &empty_str;
return;
}
m_para = new ParaData(len);
m_str = &m_para->m_str;
m_attr = m_para->m_attr;
}
/**
* increase reference
*/
void
Paragraph::grab()
{
if (m_para) m_para->m_ref++;
}
void
Paragraph::release()
{
if (m_para){
m_para->m_ref--;
if (m_para->m_ref==0)
delete m_para;
m_para = NULL;
m_str = &empty_str;
m_attr = empty_attr;
}
}
/**
* Decrease reference.
* it will automatically clean up the memory if it has no reference.
*/
Paragraph::~Paragraph()
{
release();
}
void
Paragraph::addLink(int s, int e, QString st)
{
if (m_para) {
Link *n = new Link(s, e, st);
n->next = m_para->m_link;
m_para->m_link = n;
}
}
Link *
Paragraph::findLink(int s)
{
if (m_para && m_para->m_link)
return m_para->m_link->findLink(s);
return NULL;
}
bool
Paragraph::empty()
{
return m_para==NULL;
}
bool
Paragraph::operator ()()
{
return !empty();
}
Paragraph &
Paragraph::operator = (const Paragraph &p)
{
this->release();
if (p.m_para) {
m_para = p.m_para;
m_str = &m_para->m_str;
m_attr = m_para->m_attr;
this->grab();
}
return *this;
}