OSDN Git Service

* WorkBackup: 2015/06/23(Tue) AM 05:51 (Testing & DeBugging LibEditText_LineCtrl_Spli...
[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 {
54         DWord   dw_len;
55
56         assert( NULL != p_tinfo );
57         assert( NULL != p_line );
58
59         if(( INVALID_LINE == p_line->dw_before ) && ( INVALID_LINE == p_line->dw_next ))
60                 { dw_len        = p_line->dw_strlen; }
61         else    {
62                 if( 1 == p_line->dw_strlen )    { return 0x00; }
63                 dw_len  = p_line->dw_strlen - 1;
64         }
65
66         memset( (p_tinfo->pb_text + p_line->dw_start), 0x00, dw_len );
67         p_line->dw_strlen       -= dw_len;
68
69         return 0x00;
70 }
71
72
73 /*----------------------------------------------------------------------
74 ----------------------------------------------------------------------*/
75 #define FUNCID_LibEditText_LineEdit_DeleteString        0x02
76 LIBEDITTEXT_LINEEDIT_EXTERN
77 int
78         LibEditText_LineEdit_DeleteString(
79                 LibEditText_TextInfo    *p_tinfo,
80                 LibEditText_LineInfo    *p_line,
81                 DWord   dw_line,
82                 DWord   dw_pos,
83                 DWord   dw_len )
84 {
85         int                     i_result        = 0x00;
86         Byte            *pb_data;
87
88         assert( NULL != p_tinfo );
89         assert( NULL != p_line );
90         assert( p_line->dw_strlen >= (dw_pos + dw_len) );
91
92         if(( 0 == dw_pos ) && ( dw_len == p_line->dw_strlen ))  {
93                 // Delete Line ---
94                 i_result        = LibEditText_LineCtrl_DeleteLine( p_tinfo, p_line, dw_line );
95                 goto    goto_LibEditText_LineEdit_DeleteString_post;
96         }
97
98         // Move StringData ---
99         pb_data = p_tinfo->pb_text + p_line->dw_start;
100         memmove( (pb_data + dw_pos), (pb_data + dw_pos + dw_len),
101                                                         (p_line->dw_strlen - dw_pos - dw_len));
102         memset( (pb_data + p_line->dw_strlen - dw_len), 0x00, dw_len );
103         p_line->dw_strlen       -= dw_len;
104
105         assert( 0 < p_line->dw_strlen );
106
107         if( p_tinfo->dw_line_end == p_line->dw_id )
108                 { goto  goto_LibEditText_LineEdit_DeleteString_post; }
109         if( '\n' == *(pb_data + p_line->dw_strlen - 1) )
110                 { goto  goto_LibEditText_LineEdit_DeleteString_post; }
111
112         // Merge line --- 
113         i_result        = LibEditText_LineCtrl_JoinLine( p_tinfo, p_line, dw_line );
114
115 goto_LibEditText_LineEdit_DeleteString_post:
116         return i_result;
117 }
118
119
120 /*----------------------------------------------------------------------
121 ----------------------------------------------------------------------*/
122 #define FUNCID_LibEditText_LineEdit_InsertString        0x03
123 LIBEDITTEXT_LINEEDIT_EXTERN
124 int
125         LibEditText_LineEdit_InsertString(
126                 LibEditText_TextInfo    *p_tinfo,
127                 LibEditText_LineInfo    *p_line,
128                 DWord   dw_pos,
129                 Byte    *pb_src,
130                 DWord   dw_srclen )
131 {
132         int                                             i_result;
133         Byte                                    *pb_pos;
134         DWord                                   dw_length;
135
136         assert( NULL != p_tinfo );
137         assert( NULL != p_line );
138
139         if( 0 == dw_srclen )    {
140                 LibBrownie_Error_SetErrorInfo(
141                                 DRD64_WARNING( DRD64_ERROR_PTN_FAILED_ARG, 0x00, 0x01 ),
142                                 LOCATION( FUNCID_LibEditText_LineEdit_InsertString ),
143                                 errno, (QWord)0x00, (QWord)dw_srclen, __LINE__ ); 
144
145                 return 0x01;
146         }
147
148         /* Check Line Remain-Length & Swap line-buffer ---*/
149         if( p_line->dw_strlen + dw_srclen + 1 > p_line->dw_linelen )    {
150                 i_result        = LibEditText_LineInfo_ExpandLineBuffer(
151                                                 p_tinfo, p_line,
152                                                 dw_srclen + DRD64_LIBEDITTEXT_DEFAULT_RESERVEBUF_INLINE);
153                 if( 0x00 != i_result )  {
154                         LibBrownie_Error_SetErrorInfo(
155                                         DRD64_ERROR( DRD64_ERROR_PTN_CALLFUNC, 0x01, 0x02 ),
156                                         LOCATION( FUNCID_LibEditText_LineEdit_InsertString ),
157                                         errno, (QWord)i_result, (QWord)dw_srclen, __LINE__ ); 
158
159                         return -0x02;
160                 }
161         }
162
163         if( p_line->dw_strlen < dw_pos )
164                 { dw_pos        = p_line->dw_strlen; }
165
166         pb_pos  = p_tinfo->pb_text + p_line->dw_start + dw_pos;
167         assert( NULL != pb_pos );
168
169         if(( '\n' == *(pb_src + dw_srclen - 1)) && ( 0 < p_line->dw_strlen ))   {
170                 if( '\n' == *(pb_pos + p_line->dw_strlen - 1))  {
171                         dw_srclen--;
172                 }
173         }
174
175         if( p_line->dw_strlen > dw_pos )        {
176                 dw_length       = p_line->dw_strlen - dw_pos + 1;
177                 memmove( pb_pos + dw_srclen, pb_pos, dw_length ); 
178         }
179
180         memcpy( pb_pos, pb_src, dw_srclen );
181         p_line->dw_strlen       += dw_srclen;
182
183         return 0x00;
184 }
185
186
187 /* EOF of drd64_.c ----------------------------------- */