OSDN Git Service

2019/04/13(Sat) 07:01
[drdeamon64/drdeamon64.git] / libedittext / drd64_libedittext_lineedit.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_LINEEDIT
38 #include"drd64_libedittext.h"
39
40 #define LOCATION(n)     DRD64_ERR_LOCATION( \
41                                                 DRD64_ERROR_MODULE_LIBEDITTEXT, DRD64_ERROR_ARCH_NODEPEND, \
42                                                 DRD64_SRC_LIBEDITTEXT_LINEEDIT_SRCID, (n))
43
44
45 /*----------------------------------------------------------------------
46 ----------------------------------------------------------------------*/
47 #define FUNCID_LibEditText_LineEdit_ClearLine           0x01
48 LIBEDITTEXT_LINEEDIT_EXTERN
49 int
50         LibEditText_LineEdit_ClearLine(
51                 LibEditText_TextInfo    *p_tinfo,
52                 LibEditText_LineInfo    *p_line,
53                 DWord                                   dw_line )
54 {
55         int             i_result;
56         DWord   dw_len;
57
58         assert( NULL != p_tinfo );
59         assert( NULL != p_line );
60
61         if( INVALID_LINE == p_line->dw_next )
62                 { dw_len        = p_line->dw_strlen; }
63         else    {
64                 if( 1 == p_line->dw_strlen )    { return 0x00; }
65                 dw_len  = p_line->dw_strlen - 1;
66         }
67
68         if( 0 == dw_len )       { return 0x00; }
69
70         // Set UndoInfo ----
71         i_result        = LibEditText_UndoInfo_SetUndoInfo(
72                                         p_tinfo, dw_line, 0, LIBEDITTEXT_UNDOCMD_DELETESTRING,
73                                         0x00, (p_tinfo->pb_text + p_line->dw_start), dw_len );
74         if( 0x00 != i_result )  {
75                 LibBrownie_Error_SetErrorInfo(
76                                 DRD64_ERROR( DRD64_ERROR_PTN_CALLFUNC, 0x01, 0x01 ),
77                                 LOCATION( FUNCID_LibEditText_LineEdit_ClearLine ),
78                                 errno, (QWord)i_result, (QWord)dw_len, __LINE__ ); 
79
80                 return -0x01;
81         }
82
83         // Clear Line-String Data ---
84         memset( (p_tinfo->pb_text + p_line->dw_start), 0x00, dw_len );
85         p_line->dw_strlen       -= dw_len;
86
87         // Adjust CursorPosition ---
88         i_result        = LibEditText_CursorCtrl_AdjustCursorPosition_DeleteString(
89                                                                 p_tinfo, p_line, 0, dw_len );
90         if( 0x00 != i_result )  {
91                 LibBrownie_Error_SetErrorInfo(
92                                 DRD64_ERROR( DRD64_ERROR_PTN_CALLFUNC, 0x01, 0x02 ),
93                                 LOCATION( FUNCID_LibEditText_LineEdit_ClearLine ),
94                                 errno, (QWord)i_result, (QWord)dw_len, __LINE__ ); 
95
96                 return -0x02;
97         }
98
99         return 0x00;
100 }
101
102
103 /*----------------------------------------------------------------------
104 ----------------------------------------------------------------------*/
105 #define FUNCID_LibEditText_LineEdit_DeleteString        0x02
106 LIBEDITTEXT_LINEEDIT_EXTERN
107 int
108         LibEditText_LineEdit_DeleteString(
109                 LibEditText_TextInfo    *p_tinfo,
110                 LibEditText_LineInfo    *p_line,
111                 DWord   dw_line,
112                 DWord   dw_pos,
113                 DWord   dw_len,
114                 Byte    b_undoflag )
115 {
116         int                     i_result        = 0x00;
117         Byte            *pb_data;
118
119         assert( NULL != p_tinfo );
120         assert( NULL != p_line );
121         assert( p_line->dw_strlen >= (dw_pos + dw_len) );
122
123         if( 0 == dw_len )       { return 0x00; }
124
125         if(( 0 == dw_pos ) && ( dw_len == p_line->dw_strlen )
126                                                 && ( dw_line + 1 < p_tinfo->dw_maxline))        {
127                 // Delete Line ---
128                 i_result        = LibEditText_LineCtrl_DeleteLine( p_tinfo, p_line, dw_line, 0x00 );
129                 goto    goto_LibEditText_LineEdit_DeleteString_post;
130         }
131
132         // Set UndoInfo ---
133         if( 0x00 == b_undoflag )        {
134                 if( 1 == dw_len )       {
135                         i_result        = LibEditText_UndoInfo_SetUndoInfo( p_tinfo, dw_line, dw_pos,
136                                                                 LIBEDITTEXT_UNDOCMD_DELETECHAR,
137                                                                 *(p_tinfo->pb_text + p_line->dw_start + dw_pos), NULL, 1 );
138                 }
139                 else {
140                         i_result        = LibEditText_UndoInfo_SetUndoInfo( p_tinfo, dw_line, dw_pos,
141                                                                 LIBEDITTEXT_UNDOCMD_DELETESTRING, 0x00,
142                                                                 (p_tinfo->pb_text + p_line->dw_start + dw_pos), dw_len );
143                 }
144                 if( 0x00 != i_result )  {
145                         LibBrownie_Error_SetErrorInfo(
146                                         DRD64_ERROR( DRD64_ERROR_PTN_CALLFUNC, 0x01, 0x02 ),
147                                         LOCATION( FUNCID_LibEditText_LineEdit_DeleteString ),
148                                         errno, (QWord)i_result, (QWord)dw_pos, __LINE__ ); 
149         
150                         return -0x02;
151                 }
152         }
153
154         // Move StringData ---
155         pb_data = p_tinfo->pb_text + p_line->dw_start;
156         memmove( (pb_data + dw_pos), (pb_data + dw_pos + dw_len),
157                                                         (p_line->dw_strlen - dw_pos - dw_len));
158         memset( (pb_data + p_line->dw_strlen - dw_len), 0x00, dw_len );
159         p_line->dw_strlen       -= dw_len;
160
161         i_result        = LibEditText_CursorCtrl_AdjustCursorPosition_DeleteString(
162                                                                 p_tinfo, p_line, dw_pos, dw_len );
163         if( 0x00 != i_result )  {
164                 LibBrownie_Error_SetErrorInfo(
165                                 DRD64_ERROR( DRD64_ERROR_PTN_CALLFUNC, 0x01, 0x02 ),
166                                 LOCATION( FUNCID_LibEditText_LineEdit_DeleteString ),
167                                 errno, (QWord)i_result, (QWord)dw_pos, __LINE__ ); 
168
169                 return -0x02;
170         }
171
172         if( p_tinfo->dw_line_end == p_line->dw_id )
173                 { goto  goto_LibEditText_LineEdit_DeleteString_post; }
174         if( '\n' == *(pb_data + p_line->dw_strlen - 1) )
175                 { goto  goto_LibEditText_LineEdit_DeleteString_post; }
176
177         // Merge line --- 
178         i_result        = LibEditText_LineCtrl_JoinLine( p_tinfo, p_line, dw_line );
179
180 goto_LibEditText_LineEdit_DeleteString_post:
181         return i_result;
182 }
183
184
185 /*----------------------------------------------------------------------
186 ----------------------------------------------------------------------*/
187 #define FUNCID_LibEditText_LineEdit_InsertString        0x03
188 LIBEDITTEXT_LINEEDIT_EXTERN
189 int
190         LibEditText_LineEdit_InsertString(
191                 LibEditText_TextInfo    *p_tinfo,
192                 LibEditText_LineInfo    *p_line,
193                 DWord   dw_line, 
194                 DWord   dw_pos,
195                 Byte    *pb_src,
196                 DWord   dw_srclen,
197                 Byte    b_flagundo )
198 {
199         int                                             i_result;
200         Byte                                    *pb_pos;
201         DWord                                   dw_length;
202
203         assert( NULL != p_tinfo );
204         assert( NULL != p_line );
205
206         if( 0 == dw_srclen )    {
207                 LibBrownie_Error_SetErrorInfo(
208                                 DRD64_WARNING( DRD64_ERROR_PTN_FAILED_ARG, 0x00, 0x01 ),
209                                 LOCATION( FUNCID_LibEditText_LineEdit_InsertString ),
210                                 errno, (QWord)0x00, (QWord)dw_srclen, __LINE__ ); 
211
212                 return 0x01;
213         }
214
215         /* Check Line Remain-Length & Swap line-buffer ---*/
216         if( p_line->dw_strlen + dw_srclen + 2 > p_line->dw_linelen )    {
217                 i_result        = LibEditText_LineInfo_ExpandLineBuffer(
218                                                 p_tinfo, p_line,
219                                                 dw_srclen + DRD64_LIBEDITTEXT_DEFAULT_RESERVEBUF_INLINE);
220                 if( 0x00 != i_result )  {
221                         LibBrownie_Error_SetErrorInfo(
222                                         DRD64_ERROR( DRD64_ERROR_PTN_CALLFUNC, 0x01, 0x02 ),
223                                         LOCATION( FUNCID_LibEditText_LineEdit_InsertString ),
224                                         errno, (QWord)i_result, (QWord)dw_srclen, __LINE__ ); 
225
226                         return -0x02;
227                 }
228         }
229
230         if( p_line->dw_strlen < dw_pos )
231                 { dw_pos        = p_line->dw_strlen; }
232
233         pb_pos  = p_tinfo->pb_text + p_line->dw_start + dw_pos;
234         assert( NULL != pb_pos );
235
236         if(( '\n' == *(pb_src + dw_srclen - 1)) && ( 0 < p_line->dw_strlen ))   {
237                 if( '\n' == *(pb_pos + p_line->dw_strlen - 1))  {
238                         dw_srclen--;
239                 }
240         }
241
242         if( p_line->dw_strlen > dw_pos )        {
243                 dw_length       = p_line->dw_strlen - dw_pos + 1;
244                 memmove( pb_pos + dw_srclen, pb_pos, dw_length ); 
245         }
246
247         memcpy( pb_pos, pb_src, dw_srclen );
248         p_line->dw_strlen       += dw_srclen;
249
250         *(p_tinfo->pb_text + p_line->dw_start + p_line->dw_strlen) = '\0';
251
252         i_result        = LibEditText_CursorCtrl_AdjustCursorPosition_InsertString(
253                                                                 p_tinfo, p_line, dw_pos, dw_srclen );
254         if( 0x00 != i_result )  {
255                 LibBrownie_Error_SetErrorInfo(
256                                 DRD64_ERROR( DRD64_ERROR_PTN_CALLFUNC, 0x01, 0x03 ),
257                                 LOCATION( FUNCID_LibEditText_LineEdit_InsertString ),
258                                 errno, (QWord)i_result, (QWord)dw_pos, __LINE__ ); 
259
260                 return -0x03;
261         }
262
263         // Set UndoInfo ---
264         if( 0x00 != b_flagundo )
265                 { goto  goto_LibEditText_LineEdit_InsertString_post; }
266
267         if( 1 == dw_srclen )    {
268                 i_result        = LibEditText_UndoInfo_SetUndoInfo( p_tinfo, dw_line, dw_pos,
269                                                         LIBEDITTEXT_UNDOCMD_INSERTCHAR, *pb_src, NULL, 1 );
270         }
271         else    {
272                 i_result        = LibEditText_UndoInfo_SetUndoInfo( p_tinfo, dw_line, dw_pos,
273                                                         LIBEDITTEXT_UNDOCMD_INSERTSTRING, 0x00, pb_src, dw_srclen );
274         }
275         if( 0x00 != i_result )  {
276                 LibBrownie_Error_SetErrorInfo(
277                                 DRD64_ERROR( DRD64_ERROR_PTN_CALLFUNC, 0x01, 0x04 ),
278                                 LOCATION( FUNCID_LibEditText_LineEdit_InsertString ),
279                                 errno, (QWord)i_result, (QWord)dw_pos, __LINE__ ); 
280
281                 return -0x04;
282         }
283
284 goto_LibEditText_LineEdit_InsertString_post:
285         return 0x00;
286 }
287
288
289 /*----------------------------------------------------------------------
290 ----------------------------------------------------------------------*/
291 #define FUNCID_LibEditText_LineEdit_CopyString  0x04
292 LIBEDITTEXT_LINEEDIT_EXTERN
293 int
294         LibEditText_LineEdit_CopyString(
295                 LibEditText_TextInfo    *p_tinfo,
296                 LibEditText_LineInfo    *p_line,
297                 DWord   dw_line, 
298                 DWord   dw_pos,
299                 Byte    *pb_src,
300                 DWord   dw_srclen )
301 {
302         int                                             i_result;
303         Byte                                    *pb_pos;
304         DWord                                   dw_length;
305         DWord                                   dw_src;
306
307         assert( NULL != p_tinfo );
308         assert( NULL != p_line );
309
310         if( 0 == dw_srclen )    {
311                 LibBrownie_Error_SetErrorInfo(
312                                 DRD64_WARNING( DRD64_ERROR_PTN_FAILED_ARG, 0x00, 0x01 ),
313                                 LOCATION( FUNCID_LibEditText_LineEdit_CopyString ),
314                                 errno, (QWord)0x00, (QWord)dw_srclen, __LINE__ ); 
315
316                 return 0x01;
317         }
318
319         dw_src  = (DWord)(pb_src - p_tinfo->pb_text);
320
321         /* Check Line Remain-Length & Swap line-buffer ---*/
322         if( p_line->dw_strlen + dw_srclen + 1 > p_line->dw_linelen )    {
323                 i_result        = LibEditText_LineInfo_ExpandLineBuffer(
324                                                 p_tinfo, p_line,
325                                                 dw_srclen + DRD64_LIBEDITTEXT_DEFAULT_RESERVEBUF_INLINE);
326                 if( 0x00 != i_result )  {
327                         LibBrownie_Error_SetErrorInfo(
328                                         DRD64_ERROR( DRD64_ERROR_PTN_CALLFUNC, 0x01, 0x02 ),
329                                         LOCATION( FUNCID_LibEditText_LineEdit_CopyString ),
330                                         errno, (QWord)i_result, (QWord)dw_srclen, __LINE__ ); 
331
332                         return -0x02;
333                 }
334
335                 pb_src  = p_tinfo->pb_text + dw_src;
336         }
337
338         if( p_line->dw_strlen < dw_pos )
339                 { dw_pos        = p_line->dw_strlen; }
340
341         pb_pos  = p_tinfo->pb_text + p_line->dw_start + dw_pos;
342         assert( NULL != pb_pos );
343
344         if(( '\n' == *(pb_src + dw_srclen - 1)) && ( 0 < p_line->dw_strlen ))   {
345                 if( '\n' == *(pb_pos + p_line->dw_strlen - 1))  {
346                         dw_srclen--;
347                 }
348         }
349
350         if( p_line->dw_strlen > dw_pos )        {
351                 dw_length       = p_line->dw_strlen - dw_pos + 1;
352                 memmove( pb_pos + dw_srclen, pb_pos, dw_length ); 
353         }
354
355         memmove( pb_pos, pb_src, dw_srclen );
356         p_line->dw_strlen       += dw_srclen;
357         *(p_tinfo->pb_text + p_line->dw_start + p_line->dw_strlen) = '\0';
358
359         i_result        = LibEditText_CursorCtrl_AdjustCursorPosition_InsertString(
360                                                                 p_tinfo, p_line, dw_pos, dw_srclen );
361         if( 0x00 != i_result )  {
362                 LibBrownie_Error_SetErrorInfo(
363                                 DRD64_ERROR( DRD64_ERROR_PTN_CALLFUNC, 0x01, 0x03 ),
364                                 LOCATION( FUNCID_LibEditText_LineEdit_CopyString ),
365                                 errno, (QWord)i_result, (QWord)dw_pos, __LINE__ ); 
366
367                 return -0x03;
368         }
369
370         // Set UndoInfo ---
371         if( 1 == dw_srclen )    {
372                 i_result        = LibEditText_UndoInfo_SetUndoInfo( p_tinfo, dw_line, dw_pos,
373                                                         LIBEDITTEXT_UNDOCMD_INSERTCHAR, *pb_src, NULL, 1 );
374         }
375         else    {
376                 i_result        = LibEditText_UndoInfo_SetUndoInfo( p_tinfo, dw_line, dw_pos,
377                                                         LIBEDITTEXT_UNDOCMD_INSERTSTRING, 0x00, pb_src, dw_srclen );
378         }
379         if( 0x00 != i_result )  {
380                 LibBrownie_Error_SetErrorInfo(
381                                 DRD64_ERROR( DRD64_ERROR_PTN_CALLFUNC, 0x01, 0x04 ),
382                                 LOCATION( FUNCID_LibEditText_LineEdit_CopyString ),
383                                 errno, (QWord)i_result, (QWord)dw_pos, __LINE__ ); 
384
385                 return -0x04;
386         }
387
388         return 0x00;
389 }
390
391
392 /* EOF of drd64_.c ----------------------------------- */