OSDN Git Service

* WorkBackup: 2015/05/15(Fri) AM 05:45 (Implementing DeleteLine & DeleteString)
[drdeamon64/drdeamon64.git] / libedittext / drd64_libedittext_linectrl.c
1 /*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
2
3                          D r . D e a m o n  6 4
4                         for INTEL64(R), AMD64(R)
5         
6    Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11  1. Redistributions of source code must retain the above copyright notice,
12     this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY Koine Yuusuke(koinec) ``AS IS'' AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL Koine Yuusuke(koinec) OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
30
31 /* File Info -----------------------------------------------------------
32 File: drd64_.c
33 Function: 
34 Comment: 
35 ----------------------------------------------------------------------*/
36
37 #define DRD64_SRC_LIBEDITTEXT_LINECTRL
38 #include"drd64_libedittext.h"
39
40
41 /*----------------------------------------------------------------------
42 ----------------------------------------------------------------------*/
43 LIBEDITTEXT_LINECTRL_EXTERN
44 int
45         LibEditText_LineCtrl_DeleteLine(
46                 LibEditText_TextInfo    *p_tinfo,
47                 LibEditText_LineInfo    *p_line,
48                 DWord   dw_line )
49 {
50         int             i_result;
51         Byte    *pb_data;
52         LibEditText_LineInfo    *p_lbefore;
53         LibEditText_LineInfo    *p_lnext;
54
55         assert( NULL != p_tinfo );
56         assert( NULL != p_line );
57
58         if( 1 == p_tinfo->dw_maxline )  { return 0x00; }
59
60         // Data Clear ---
61         pb_data = p_tinfo->pb_text + p_line->dw_start;
62         memset( pb_data, 0x00, p_line->dw_linelen );
63         p_line->dw_strlen       = 0;
64
65         // Remove Link LineInfo ---
66         p_lbefore       = LINFO(p_tinfo, p_line->dw_before);
67         p_lnext         = LINFO(p_tinfo, p_line->dw_next);
68
69         if( NULL != p_lbefore )
70                 { p_lbefore->dw_next    = p_line->dw_next; }
71         if( NULL != p_lnext )
72                 { p_lnext->dw_before    = p_line->dw_before; }
73         if( p_tinfo->dw_line_start == p_line->dw_id )
74                 { p_tinfo->dw_line_start        = p_line->dw_next; }
75         if( p_tinfo->dw_line_end == p_line->dw_id )
76                 { p_tinfo->dw_line_end  = p_line->dw_before; }
77
78         p_line->dw_before       = INVALID_LINE;
79         p_line->dw_next         = INVALID_LINE;
80
81         // Remove SortChain ---
82         i_result        = LibEditText_LineInfo_RemoveSortChain( p_tinfo, p_line );
83         if( 0x00 != i_result )  { return i_result; }
84
85         // Add EmptyChain ---
86         i_result        = LibEditText_LineInfo_SetEmptyLineChain( p_tinfo, p_line );
87         if( 0x00 != i_result )  { return i_result; }
88
89         // Check & Remove Section ---
90
91         p_tinfo->dw_maxline--;
92
93         return 0x00;
94 }
95
96
97 /*----------------------------------------------------------------------
98 ----------------------------------------------------------------------*/
99 LIBEDITTEXT_API_LINECTRL
100 int
101         LibEditText_DeleteLine_toLine(
102                 int             i_tinfoid,
103                 DWord   dw_line )
104 {
105         int                                             i_result;
106         LibEditText_TextInfo    *p_tinfo;
107
108         if( 0 > i_tinfoid )             { return -0x01; }
109         p_tinfo = LibEditText_System_GetTextInfo( i_tinfoid );
110         if( NULL == p_tinfo )   { return -0x02; }
111
112         
113         return i_result;
114 }
115
116
117 /*----------------------------------------------------------------------
118 ----------------------------------------------------------------------*/
119 LIBEDITTEXT_LINECTRL_EXTERN
120 int
121         LibEditText_LineCtrl_InsertLine(
122                 LibEditText_TextInfo    *p_tinfo,
123                 DWord   dw_line,
124                 Byte    *pb_text,
125                 DWord   dw_length )
126 {
127         int                                             i_result;
128         DWord                                   dw_buflength;
129         DWord                                   dw_strlen;
130         LibEditText_LineInfo    *p_lbefore;
131         LibEditText_LineInfo    *p_line;
132         LibEditText_LineInfo    *p_lnext;
133         
134         assert( NULL != p_tinfo );
135
136         p_lnext = LibEditText_Section_SearchLine( p_tinfo, dw_line );
137         assert( NULL != p_lnext );
138
139         // PreProccess
140         p_lbefore       = LINFO(p_tinfo, p_lnext->dw_before);
141         
142         dw_strlen       = dw_length;
143         if(( 0 == dw_length ) || ( '\n' != *(pb_text + dw_length - 1) ))
144                 { dw_strlen++; }
145
146         // Get LineInfo struct
147         dw_buflength    = dw_strlen + DRD64_LIBEDITTEXT_DEFAULT_RESERVEBUF_INLINE;
148         p_line  = LibEditText_LineInfo_GetEmptyLineInfo( p_tinfo, dw_buflength );
149         if( NULL == p_line )    { return -0x11; }
150         
151         // Link Before & After LineInfo
152         if( NULL != p_lbefore )         {
153                 p_lbefore->dw_next      = p_line->dw_id;
154                 p_line->dw_before       = p_lbefore->dw_id;
155         }
156         else
157                 { p_tinfo->dw_line_start        = p_line->dw_id; }
158
159         p_lnext->dw_before      = p_line->dw_id;
160         p_line->dw_next         = p_lnext->dw_id;
161
162         // Set LineInfo to SortChain
163         i_result    = LibEditText_LineInfo_AddSortChain( p_tinfo, p_line );
164         if( 0x00 != i_result )  { return -0x12; }
165
166         // Set Init. TextData
167         p_line->dw_strlen       = dw_strlen;
168         memcpy( p_tinfo->pb_text + p_line->dw_start, pb_text, dw_length );
169         if( dw_length != dw_strlen )
170                 { *(p_tinfo->pb_text + p_line->dw_start + dw_length) = '\n'; }  
171
172         // Adjust LineSection
173         i_result        = LibEditText_Section_InsertLine( p_tinfo, dw_line );
174         if( 0x00 != i_result )  { return -0x13; }
175
176         p_tinfo->dw_maxline++;
177
178         return 0x00;
179 }
180
181
182 /*----------------------------------------------------------------------
183 ----------------------------------------------------------------------*/
184 LIBEDITTEXT_API_LINECTRL
185 int
186         LibEditText_InsertLine_toLine(
187                 int             i_tinfoid,
188                 DWord   dw_line,
189                 char    *pstr_text,
190                 DWord   dw_length )
191 {
192         int                                             i_result;
193         LibEditText_TextInfo    *p_tinfo;
194
195         if( 0 > i_tinfoid )             { return -0x01; }
196         p_tinfo = LibEditText_System_GetTextInfo( i_tinfoid );
197         if( NULL == p_tinfo )   { return -0x02; }
198
199         if( NULL == pstr_text ) { return -0x03; }
200
201         if( p_tinfo->dw_maxline < (dw_line + 1) )       {
202                 i_result        = LibEditText_LineCtrl_AppendLine(
203                                                 p_tinfo, (Byte *)pstr_text, dw_length, 0x00 );
204         }
205         else    {
206                 i_result        = LibEditText_LineCtrl_InsertLine(
207                                                 p_tinfo, dw_line, (Byte *)pstr_text, dw_length );
208         }
209         
210         return i_result;
211 }
212
213
214 /*----------------------------------------------------------------------
215 ----------------------------------------------------------------------*/
216 LIBEDITTEXT_LINECTRL_EXTERN
217 int
218         LibEditText_LineCtrl_AppendLine(
219                 LibEditText_TextInfo            *p_tinfo,
220                 Byte    *pstr_text,
221                 DWord   dw_length,
222                 Byte    b_mode )
223 {
224         int                                             i_result;
225         DWord                                   dw_buflength;
226         Byte                                            str_lf[2]       = { '\n', '\0'};
227         Byte                                            *pb_tmp;
228         LibEditText_LineInfo            *p_line;
229         LibEditText_LineInfo            *p_before;
230
231         assert( NULL != p_tinfo );
232         p_before        = LINFO(p_tinfo, p_tinfo->dw_line_end);
233
234         if( NULL == p_before )
235                 { goto goto_LibEditText_LineCtrl_AppendLine_prechecked; }
236
237         if( 0 < p_before->dw_strlen )   {
238                 pb_tmp  = p_tinfo->pb_text + ((p_before->dw_start) + (p_before->dw_strlen) - 1);
239                 if( '\n' == *pb_tmp )
240                         { goto goto_LibEditText_LineCtrl_AppendLine_prechecked; }
241         }
242
243         i_result        = LibEditText_LineEdit_InsertString(
244                                                         p_tinfo, p_before, p_before->dw_strlen, str_lf, 1);
245
246 goto_LibEditText_LineCtrl_AppendLine_prechecked:
247
248         dw_buflength    = dw_length + DRD64_LIBEDITTEXT_DEFAULT_RESERVEBUF_INLINE;
249         p_line  = LibEditText_LineInfo_GetEmptyLineInfo( p_tinfo, dw_buflength );
250         if( NULL == p_line )    { return -0x11; }
251
252         /* ReSet p_before for if expand LineInfo memory in GetEmptyLineInfo() func.. */
253         p_before        = LINFO(p_tinfo, p_tinfo->dw_line_end);
254         
255         /* p_line->dw_start      */
256         if( NULL != p_before )
257                 { p_before->dw_next     = p_line->dw_id; }
258         else
259                 { p_tinfo->dw_line_start        = p_line->dw_id; }
260
261         p_line->dw_before       = (NULL==p_before ? INVALID_LINE : p_before->dw_id);
262         p_line->dw_strlen       = dw_length;
263
264         i_result    = LibEditText_LineInfo_AddSortChain( p_tinfo, p_line );
265         if( 0x00 != i_result )  {
266                 return -0x12;
267         }
268
269         memcpy( p_tinfo->pb_text + p_line->dw_start, pstr_text, dw_length );
270
271         p_tinfo->dw_line_end    = p_line->dw_id;
272
273         /* Normal Append Mode is b_mode=0x00. */
274         if( 0x00 == b_mode )    {
275                 i_result        = LibEditText_Section_AddSection( p_tinfo );
276                 if( 0x00 > i_result )           { return i_result; }
277                 else if( 0x00 < i_result )      { i_result      = 0x00; }
278                 p_tinfo->dw_maxline++;
279         }
280
281         return 0x00;
282 }
283
284
285 /*----------------------------------------------------------------------
286 ----------------------------------------------------------------------*/
287 LIBEDITTEXT_API_LINECTRL
288 int
289         LibEditText_AppendLine(
290                 int             i_tinfoid,
291                 char    *pstr_text,
292                 DWord   dw_length )
293 {
294         int                                             i_result;
295         LibEditText_TextInfo    *p_tinfo;
296
297         if( 0 > i_tinfoid )             { return -0x01; }
298         p_tinfo = LibEditText_System_GetTextInfo( i_tinfoid );
299         if( NULL == p_tinfo )   { return -0x02; }
300
301         if( NULL == pstr_text ) { return -0x03; }
302         
303         i_result        = LibEditText_LineCtrl_AppendLine( p_tinfo, (Byte *)pstr_text, dw_length, 0x00 );
304         
305         return i_result;
306 }
307
308
309 /* EOF of drd64_.c ----------------------------------- */