OSDN Git Service

* Test Completed: API InsertLine_toLine() & API DeleteLine_toLine() (Intermidiate...
[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         // Check & Remove Section ---
61         LibEditText_Section_DeleteLine( p_tinfo, dw_line );
62
63         // Data Clear ---
64         pb_data = p_tinfo->pb_text + p_line->dw_start;
65         memset( pb_data, 0x00, p_line->dw_linelen );
66         p_line->dw_strlen       = 0;
67
68         // Remove Link LineInfo ---
69         p_lbefore       = LINFO(p_tinfo, p_line->dw_before);
70         p_lnext         = LINFO(p_tinfo, p_line->dw_next);
71
72         if( NULL != p_lbefore )
73                 { p_lbefore->dw_next    = p_line->dw_next; }
74         if( NULL != p_lnext )
75                 { p_lnext->dw_before    = p_line->dw_before; }
76         if( p_tinfo->dw_line_start == p_line->dw_id )
77                 { p_tinfo->dw_line_start        = p_line->dw_next; }
78         if( p_tinfo->dw_line_end == p_line->dw_id )
79                 { p_tinfo->dw_line_end  = p_line->dw_before; }
80
81         p_line->dw_before       = INVALID_LINE;
82         p_line->dw_next         = INVALID_LINE;
83
84         // Remove SortChain ---
85         i_result        = LibEditText_LineInfo_RemoveSortChain( p_tinfo, p_line );
86         if( 0x00 != i_result )  { return i_result; }
87
88         // Add EmptyChain ---
89         i_result        = LibEditText_LineInfo_SetEmptyLineChain( p_tinfo, p_line );
90         if( 0x00 != i_result )  { return i_result; }
91
92         p_tinfo->dw_maxline--;
93
94         return 0x00;
95 }
96
97
98 /*----------------------------------------------------------------------
99 ----------------------------------------------------------------------*/
100 LIBEDITTEXT_API_LINECTRL
101 int
102         LibEditText_DeleteLine_toLine(
103                 int             i_tinfoid,
104                 DWord   dw_line )
105 {
106         int                                             i_result        = 0x00;
107         LibEditText_TextInfo    *p_tinfo;
108         LibEditText_LineInfo    *p_line;
109
110         if( 0 > i_tinfoid )             { return -0x01; }
111         p_tinfo = LibEditText_System_GetTextInfo( i_tinfoid );
112         if( NULL == p_tinfo )   { return -0x02; }
113
114         p_line  = LibEditText_Section_SearchLine( p_tinfo, dw_line );
115         assert( NULL != p_line );
116
117         i_result        = LibEditText_LineCtrl_DeleteLine( p_tinfo, p_line, dw_line );
118         
119         return i_result;
120 }
121
122
123 /*----------------------------------------------------------------------
124 ----------------------------------------------------------------------*/
125 LIBEDITTEXT_LINECTRL_EXTERN
126 int
127         LibEditText_LineCtrl_JoinLine(
128                 LibEditText_TextInfo    *p_tinfo,
129                 LibEditText_LineInfo    *p_ldest,
130                 DWord   dw_line )
131 {
132         int             i_result;
133         Byte    *pb_dest;
134         Byte    *pb_src;
135         DWord   dw_linelen;
136         LibEditText_LineInfo    *p_lsrc;
137
138         // Check 
139         assert( NULL != p_tinfo );
140         assert( NULL != p_ldest );
141
142         if( p_tinfo->dw_line_end == p_ldest->dw_id )
143                 { return 0x01; }
144
145         p_lsrc  = LINFO(p_tinfo, p_ldest->dw_next);
146         assert( NULL != p_lsrc );
147
148         // Merge Memory data
149         if((p_ldest->dw_strlen + p_lsrc->dw_strlen + 5) > p_ldest->dw_linelen )         {
150                 dw_linelen      = p_ldest->dw_strlen + p_lsrc->dw_strlen
151                                                 + DRD64_LIBEDITTEXT_DEFAULT_RESERVEBUF_INLINE;
152                 i_result        = LibEditText_LineInfo_ExpandLineBuffer(
153                                                                 p_tinfo, p_ldest, dw_linelen );
154                 if( 0x00 != i_result )  { return -0x02; }
155         }
156
157         pb_dest = p_tinfo->pb_text + p_ldest->dw_start + p_ldest->dw_strlen - 1;
158         pb_src  = p_tinfo->pb_text + p_lsrc->dw_start;
159
160         assert( '\n' == *pb_dest );
161
162         memcpy( pb_dest, pb_src, p_lsrc->dw_strlen );
163         *(pb_dest + p_lsrc->dw_strlen) = '\0';
164
165         p_ldest->dw_strlen      = p_ldest->dw_strlen - 1 + p_lsrc->dw_strlen;
166
167         // delete line
168         i_result        = LibEditText_LineCtrl_DeleteLine( p_tinfo, p_lsrc, dw_line + 1 );
169         if( 0x00 != i_result )  { return -0x02; }
170         
171         return 0x00;
172 }
173
174
175 /*----------------------------------------------------------------------
176 ----------------------------------------------------------------------*/
177 LIBEDITTEXT_API_LINECTRL
178 int
179         LibEditText_JoinLine_toLine(
180                 int             i_tinfoid,
181                 DWord   dw_line )
182 {
183         int                                             i_result;
184         LibEditText_TextInfo    *p_tinfo;
185         LibEditText_LineInfo    *p_line;
186         
187         if( 0 > i_tinfoid )             { return -0x01; }
188         p_tinfo = LibEditText_System_GetTextInfo( i_tinfoid );
189         if( NULL == p_tinfo )   { return -0x02; }
190
191         p_line  = LibEditText_Section_SearchLine( p_tinfo, dw_line );
192         assert( NULL != p_line );
193         
194         i_result        = LibEditText_LineCtrl_JoinLine( p_tinfo, p_line, dw_line );
195
196         return i_result;
197 }
198
199
200 /*----------------------------------------------------------------------
201 ----------------------------------------------------------------------*/
202 LIBEDITTEXT_LINECTRL_EXTERN
203 int
204         LibEditText_LineCtrl_InsertLine(
205                 LibEditText_TextInfo    *p_tinfo,
206                 DWord   dw_line,
207                 Byte    *pb_text,
208                 DWord   dw_length )
209 {
210         int                                             i_result;
211         DWord                                   dw_buflength;
212         DWord                                   dw_strlen;
213         LibEditText_LineInfo    *p_lbefore;
214         LibEditText_LineInfo    *p_line;
215         LibEditText_LineInfo    *p_lnext;
216         
217         assert( NULL != p_tinfo );
218
219         // PreProccess
220         dw_strlen       = dw_length;
221         if(( 0 == dw_length ) || ( '\n' != *(pb_text + dw_length - 1) ))
222                 { dw_strlen++; }
223
224         // Get LineInfo struct
225         dw_buflength    = dw_strlen + DRD64_LIBEDITTEXT_DEFAULT_RESERVEBUF_INLINE;
226         p_line  = LibEditText_LineInfo_GetEmptyLineInfo( p_tinfo, dw_buflength );
227         if( NULL == p_line )    { return -0x11; }
228
229         p_lnext = LibEditText_Section_SearchLine( p_tinfo, dw_line );
230         assert( NULL != p_lnext );
231
232         p_lbefore       = LINFO(p_tinfo, p_lnext->dw_before);
233         
234         // Link Before & After LineInfo
235         if( NULL != p_lbefore )         {
236                 p_lbefore->dw_next      = p_line->dw_id;
237                 p_line->dw_before       = p_lbefore->dw_id;
238         }
239         else
240                 { p_tinfo->dw_line_start        = p_line->dw_id; }
241
242         p_lnext->dw_before      = p_line->dw_id;
243         p_line->dw_next         = p_lnext->dw_id;
244
245         // Set LineInfo to SortChain
246         i_result    = LibEditText_LineInfo_AddSortChain( p_tinfo, p_line );
247         if( 0x00 != i_result )  { return -0x12; }
248
249         // Set Init. TextData
250         p_line->dw_strlen       = dw_strlen;
251         memcpy( p_tinfo->pb_text + p_line->dw_start, pb_text, dw_length );
252         if( dw_length != dw_strlen )
253                 { *(p_tinfo->pb_text + p_line->dw_start + dw_length) = '\n'; }  
254
255         // Adjust LineSection
256         i_result        = LibEditText_Section_InsertLine( p_tinfo, dw_line );
257         if( 0x00 != i_result )  { return -0x13; }
258
259         p_tinfo->dw_maxline++;
260
261         return 0x00;
262 }
263
264
265 /*----------------------------------------------------------------------
266 ----------------------------------------------------------------------*/
267 LIBEDITTEXT_API_LINECTRL
268 int
269         LibEditText_InsertLine_toLine(
270                 int             i_tinfoid,
271                 DWord   dw_line,
272                 char    *pstr_text,
273                 DWord   dw_length )
274 {
275         int                                             i_result;
276         LibEditText_TextInfo    *p_tinfo;
277
278         if( 0 > i_tinfoid )             { return -0x01; }
279         p_tinfo = LibEditText_System_GetTextInfo( i_tinfoid );
280         if( NULL == p_tinfo )   { return -0x02; }
281
282         if( NULL == pstr_text ) { return -0x03; }
283
284         if( p_tinfo->dw_maxline < (dw_line + 1) )       {
285                 i_result        = LibEditText_LineCtrl_AppendLine(
286                                                 p_tinfo, (Byte *)pstr_text, dw_length, 0x00 );
287         }
288         else    {
289                 i_result        = LibEditText_LineCtrl_InsertLine(
290                                                 p_tinfo, dw_line, (Byte *)pstr_text, dw_length );
291         }
292         
293         return i_result;
294 }
295
296
297 /*----------------------------------------------------------------------
298 ----------------------------------------------------------------------*/
299 LIBEDITTEXT_LINECTRL_EXTERN
300 int
301         LibEditText_LineCtrl_AppendLine(
302                 LibEditText_TextInfo            *p_tinfo,
303                 Byte    *pstr_text,
304                 DWord   dw_length,
305                 Byte    b_mode )
306 {
307         int                                             i_result;
308         DWord                                   dw_buflength;
309         Byte                                            str_lf[2]       = { '\n', '\0'};
310         Byte                                            *pb_tmp;
311         LibEditText_LineInfo            *p_line;
312         LibEditText_LineInfo            *p_before;
313
314         assert( NULL != p_tinfo );
315         p_before        = LINFO(p_tinfo, p_tinfo->dw_line_end);
316
317         if( NULL == p_before )
318                 { goto goto_LibEditText_LineCtrl_AppendLine_prechecked; }
319
320         if( 0 < p_before->dw_strlen )   {
321                 pb_tmp  = p_tinfo->pb_text + ((p_before->dw_start) + (p_before->dw_strlen) - 1);
322                 if( '\n' == *pb_tmp )
323                         { goto goto_LibEditText_LineCtrl_AppendLine_prechecked; }
324         }
325
326         i_result        = LibEditText_LineEdit_InsertString(
327                                                         p_tinfo, p_before, p_before->dw_strlen, str_lf, 1);
328
329 goto_LibEditText_LineCtrl_AppendLine_prechecked:
330
331         dw_buflength    = dw_length + DRD64_LIBEDITTEXT_DEFAULT_RESERVEBUF_INLINE;
332         p_line  = LibEditText_LineInfo_GetEmptyLineInfo( p_tinfo, dw_buflength );
333         if( NULL == p_line )    { return -0x11; }
334
335         /* ReSet p_before for if expand LineInfo memory in GetEmptyLineInfo() func.. */
336         p_before        = LINFO(p_tinfo, p_tinfo->dw_line_end);
337         
338         /* p_line->dw_start      */
339         if( NULL != p_before )
340                 { p_before->dw_next     = p_line->dw_id; }
341         else
342                 { p_tinfo->dw_line_start        = p_line->dw_id; }
343
344         p_line->dw_before       = (NULL==p_before ? INVALID_LINE : p_before->dw_id);
345         p_line->dw_strlen       = dw_length;
346
347         i_result    = LibEditText_LineInfo_AddSortChain( p_tinfo, p_line );
348         if( 0x00 != i_result )  {
349                 return -0x12;
350         }
351
352         memcpy( p_tinfo->pb_text + p_line->dw_start, pstr_text, dw_length );
353
354         p_tinfo->dw_line_end    = p_line->dw_id;
355
356         /* Normal Append Mode is b_mode=0x00. */
357         if( 0x00 == b_mode )    {
358                 i_result        = LibEditText_Section_AddSection( p_tinfo );
359                 if( 0x00 > i_result )           { return i_result; }
360                 else if( 0x00 < i_result )      { i_result      = 0x00; }
361
362                 p_tinfo->dw_maxline++;
363         }
364
365         return 0x00;
366 }
367
368
369 /*----------------------------------------------------------------------
370 ----------------------------------------------------------------------*/
371 LIBEDITTEXT_API_LINECTRL
372 int
373         LibEditText_AppendLine(
374                 int             i_tinfoid,
375                 char    *pstr_text,
376                 DWord   dw_length )
377 {
378         int                                             i_result;
379         LibEditText_TextInfo    *p_tinfo;
380
381         if( 0 > i_tinfoid )             { return -0x01; }
382         p_tinfo = LibEditText_System_GetTextInfo( i_tinfoid );
383         if( NULL == p_tinfo )   { return -0x02; }
384
385         if( NULL == pstr_text ) { return -0x03; }
386         
387         i_result        = LibEditText_LineCtrl_AppendLine( p_tinfo, (Byte *)pstr_text, dw_length, 0x00 );
388         
389         return i_result;
390 }
391
392
393 /* EOF of drd64_.c ----------------------------------- */