OSDN Git Service

* Implement: InsertLine_toLine API Implemented.(complete)
[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 LIBEDITTEXT_LINECTRL_EXTERN
43 int
44         LibEditText_LineCtrl_InsertLine(
45                 LibEditText_TextInfo    *p_tinfo,
46                 DWord   dw_line,
47                 Byte    *pb_text,
48                 DWord   dw_length )
49 {
50         int                                             i_result;
51         DWord                                   dw_buflength;
52         DWord                                   dw_strlen;
53         LibEditText_LineInfo    *p_lbefore;
54         LibEditText_LineInfo    *p_line;
55         LibEditText_LineInfo    *p_lnext;
56         
57         assert( NULL != p_tinfo );
58
59         p_lnext = LibEditText_Section_SearchLine( p_tinfo, dw_line );
60         assert( NULL != p_lnext );
61
62         // PreProccess
63         p_lbefore       = LINFO(p_tinfo, p_lnext->dw_before);
64         
65         dw_strlen       = dw_length;
66         if(( 0 == dw_length ) || ( '\n' != *(pb_text + dw_length - 1) ))
67                 { dw_strlen++; }
68
69         // Get LineInfo struct
70         dw_buflength    = dw_strlen + DRD64_LIBEDITTEXT_DEFAULT_RESERVEBUF_INLINE;
71         p_line  = LibEditText_LineInfo_GetEmptyLineInfo( p_tinfo, dw_buflength );
72         if( NULL == p_line )    { return -0x11; }
73         
74         // Link Before & After LineInfo
75         if( NULL != p_lbefore )         {
76                 p_lbefore->dw_next      = p_line->dw_id;
77                 p_line->dw_before       = p_lbefore->dw_id;
78         }
79         else
80                 { p_tinfo->dw_line_start        = p_line->dw_id; }
81
82         p_lnext->dw_before      = p_line->dw_id;
83         p_line->dw_next         = p_lnext->dw_id;
84
85         // Set LineInfo to SortChain
86         i_result    = LibEditText_LineInfo_AddSortChain( p_tinfo, p_line );
87         if( 0x00 != i_result )  { return -0x12; }
88
89         // Set Init. TextData
90         p_line->dw_strlen       = dw_strlen;
91         memcpy( p_tinfo->pb_text + p_line->dw_start, pb_text, dw_length );
92         if( dw_length != dw_strlen )
93                 { *(p_tinfo->pb_text + p_line->dw_start + dw_length) = '\n'; }  
94
95         // Adjust LineSection
96         i_result        = LibEditText_Section_InsertLine( p_tinfo, dw_line );
97         if( 0x00 != i_result )  { return -0x13; }
98
99         p_tinfo->dw_maxline++;
100
101         return 0x00;
102 }
103
104
105 /*----------------------------------------------------------------------
106 ----------------------------------------------------------------------*/
107 LIBEDITTEXT_API_LINECTRL
108 int
109         LibEditText_InsertLine_toLine(
110                 int             i_tinfoid,
111                 DWord   dw_line,
112                 char    *pstr_text,
113                 DWord   dw_length )
114 {
115         int                                             i_result;
116         LibEditText_TextInfo    *p_tinfo;
117
118         if( 0 > i_tinfoid )             { return -0x01; }
119         p_tinfo = LibEditText_System_GetTextInfo( i_tinfoid );
120         if( NULL == p_tinfo )   { return -0x02; }
121
122         if( NULL == pstr_text ) { return -0x03; }
123
124         if( p_tinfo->dw_maxline < (dw_line + 1) )       {
125                 i_result        = LibEditText_LineCtrl_AppendLine(
126                                                 p_tinfo, (Byte *)pstr_text, dw_length, 0x00 );
127         }
128         else    {
129                 i_result        = LibEditText_LineCtrl_InsertLine(
130                                                 p_tinfo, dw_line, (Byte *)pstr_text, dw_length );
131         }
132         
133         return i_result;
134 }
135
136
137 /*----------------------------------------------------------------------
138 ----------------------------------------------------------------------*/
139 LIBEDITTEXT_LINECTRL_EXTERN
140 int
141         LibEditText_LineCtrl_AppendLine(
142                 LibEditText_TextInfo            *p_tinfo,
143                 Byte    *pstr_text,
144                 DWord   dw_length,
145                 Byte    b_mode )
146 {
147         int                                             i_result;
148         DWord                                   dw_buflength;
149         Byte                                            str_lf[2]       = { '\n', '\0'};
150         Byte                                            *pb_tmp;
151         LibEditText_LineInfo            *p_line;
152         LibEditText_LineInfo            *p_before;
153
154         assert( NULL != p_tinfo );
155         p_before        = LINFO(p_tinfo, p_tinfo->dw_line_end);
156
157         if( NULL == p_before )
158                 { goto goto_LibEditText_LineCtrl_AppendLine_prechecked; }
159
160         if( 0 < p_before->dw_strlen )   {
161                 pb_tmp  = p_tinfo->pb_text + ((p_before->dw_start) + (p_before->dw_strlen) - 1);
162                 if( '\n' == *pb_tmp )
163                         { goto goto_LibEditText_LineCtrl_AppendLine_prechecked; }
164         }
165
166         i_result        = LibEditText_LineEdit_InsertString(
167                                                         p_tinfo, p_before, p_before->dw_strlen, str_lf, 1);
168
169 goto_LibEditText_LineCtrl_AppendLine_prechecked:
170
171         dw_buflength    = dw_length + DRD64_LIBEDITTEXT_DEFAULT_RESERVEBUF_INLINE;
172         p_line  = LibEditText_LineInfo_GetEmptyLineInfo( p_tinfo, dw_buflength );
173         if( NULL == p_line )    { return -0x11; }
174
175         /* ReSet p_before for if expand LineInfo memory in GetEmptyLineInfo() func.. */
176         p_before        = LINFO(p_tinfo, p_tinfo->dw_line_end);
177         
178         /* p_line->dw_start      */
179         if( NULL != p_before )
180                 { p_before->dw_next     = p_line->dw_id; }
181         else
182                 { p_tinfo->dw_line_start        = p_line->dw_id; }
183
184         p_line->dw_before       = (NULL==p_before ? INVALID_LINE : p_before->dw_id);
185         p_line->dw_strlen       = dw_length;
186
187         i_result    = LibEditText_LineInfo_AddSortChain( p_tinfo, p_line );
188         if( 0x00 != i_result )  {
189                 return -0x12;
190         }
191
192         memcpy( p_tinfo->pb_text + p_line->dw_start, pstr_text, dw_length );
193
194         p_tinfo->dw_line_end    = p_line->dw_id;
195
196         /* Normal Append Mode is b_mode=0x00. */
197         if( 0x00 == b_mode )    {
198                 i_result        = LibEditText_Section_AddSection( p_tinfo );
199                 if( 0x00 > i_result )           { return i_result; }
200                 else if( 0x00 < i_result )      { i_result      = 0x00; }
201                 p_tinfo->dw_maxline++;
202         }
203
204         return 0x00;
205 }
206
207
208 /*----------------------------------------------------------------------
209 ----------------------------------------------------------------------*/
210 LIBEDITTEXT_API_LINECTRL
211 int
212         LibEditText_AppendLine(
213                 int             i_tinfoid,
214                 char    *pstr_text,
215                 DWord   dw_length )
216 {
217         int                                             i_result;
218         LibEditText_TextInfo    *p_tinfo;
219
220         if( 0 > i_tinfoid )             { return -0x01; }
221         p_tinfo = LibEditText_System_GetTextInfo( i_tinfoid );
222         if( NULL == p_tinfo )   { return -0x02; }
223
224         if( NULL == pstr_text ) { return -0x03; }
225         
226         i_result        = LibEditText_LineCtrl_AppendLine( p_tinfo, (Byte *)pstr_text, dw_length, 0x00 );
227         
228         return i_result;
229 }
230
231
232 /* EOF of drd64_.c ----------------------------------- */