OSDN Git Service

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