1 /*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
4 for INTEL64(R), AMD64(R)
6 Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
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.
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.
29 DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
31 /* File Info -----------------------------------------------------------
35 ----------------------------------------------------------------------*/
37 #define DRD64_SRC_LIBEDITTEXT_CURSORFIND
38 #include"drd64_libedittext.h"
40 #define LOCATION(n) DRD64_ERR_LOCATION( \
41 DRD64_ERROR_MODULE_LIBEDITTEXT, DRD64_ERROR_ARCH_NODEPEND, \
42 DRD64_SRC_LIBEDITTEXT_CURSORFIND_SRCID, (n))
45 /*----------------------------------------------------------------------
50 ----------------------------------------------------------------------*/
53 /*----------------------------------------------------------------------
54 ----------------------------------------------------------------------*/
56 LibEditText_CursorFind_SetRegexString(
57 LibEditText_Cursor *p_cursor,
64 assert( NULL != p_cursor );
65 assert( NULL != pstr_regex );
67 // Check regex length & NULL terminator ---
68 if( DRD64_LIBEDITTEXT_DEFAULT_REGSTR_SIZE <= dw_regexlen ) {
72 pstr_now = (pstr_regex + dw_regexlen);
73 for( dw_cnt = dw_regexlen; dw_cnt > 0; dw_cnt-- ) {
74 if( *pstr_now == '\0' ) { break; }
82 // Set regex string to CursorInfo ---
83 strncpy( p_cursor->str_regstr, pstr_regex, (dw_cnt + 1) );
89 /*----------------------------------------------------------------------
90 ----------------------------------------------------------------------*/
92 LibEditText_CursorFind_GetFindingPostion(
93 LibEditText_Cursor *p_cursor )
97 dw_pos = p_cursor->dw_pos;
99 if( 0 < p_cursor->dw_find_prevpos_len ) {
100 if( p_cursor->dw_lineid != p_cursor->dw_find_prevlineid )
101 { goto goto_LibEditText_CursorFind_GetFindingPostion_post; }
103 if( p_cursor->dw_pos < p_cursor->dw_find_prevpos_start )
104 { goto goto_LibEditText_CursorFind_GetFindingPostion_post; }
106 if( p_cursor->dw_pos >=
107 (p_cursor->dw_find_prevpos_start + p_cursor->dw_find_prevpos_len ) )
108 { goto goto_LibEditText_CursorFind_GetFindingPostion_post; }
110 dw_pos = p_cursor->dw_find_prevpos_start + p_cursor->dw_find_prevpos_len;
113 goto_LibEditText_CursorFind_GetFindingPostion_post:
118 /*----------------------------------------------------------------------
119 ----------------------------------------------------------------------*/
121 LibEditText_CursorFind_ExecFindString(
122 LibEditText_TextInfo *p_tinfo,
123 LibEditText_Cursor *p_cursor,
138 LibEditText_LineInfo *p_line;
141 assert( NULL != p_tinfo );
142 assert( NULL != p_cursor );
145 p_line = LINFO(p_tinfo, p_cursor->dw_lineid);
146 assert( NULL != p_line );
148 dw_lidbase = p_line->dw_id;
149 dw_lnow = p_cursor->dw_line;
150 dw_pstart = *pdw_pos;
151 pb_start = p_tinfo->pb_text + p_line->dw_start + dw_pstart;
155 if( 0 < dw_pstart ) { i_flag = REG_NOTBOL; }
156 else { i_flag = 0x00; }
159 i_result = regexec( &(p_cursor->t_findreg), (const char *)pb_start,
160 1, &t_match, i_flag );
161 if( 0x00 == i_result ) { break; }
162 else if( REG_NOMATCH != i_result ) {
166 if( p_line->dw_id == dw_lidbase ) { i_brkcnt++; }
168 // Going to Next-Line ---
170 p_line = LINFO(p_tinfo, p_line->dw_next);
172 if( NULL == p_line ) {
173 p_line = LINFO(p_tinfo, p_tinfo->dw_line_start);
177 assert( NULL != p_line );
179 pb_start = p_tinfo->pb_text + p_line->dw_start;
181 }while( 2 > i_brkcnt );
183 if(( 2 == i_brkcnt ) && ( REG_NOMATCH == i_result ))
184 { return LIBEDITTEXT_RET_FINDNOMATCH; } // NO match string in Text.
186 dw_pstart += (DWord)t_match.rm_so; // Find String Pos.
187 dw_len = (DWord)(t_match.rm_eo - t_match.rm_so); // Find String Len.
188 // Find String Line is dw_lnow.
190 assert( dw_lnow < p_tinfo->dw_maxline );
191 assert( dw_pstart < p_line->dw_strlen );
193 *pdw_pos = dw_pstart;
195 *pdw_findlid = p_line->dw_id;
198 i_result = LIBEDITTEXT_RET_FINDMATCH;
199 if( 0x00 != i_round ) { i_result = LIBEDITTEXT_RET_FINDMATCH_ROUND; }
205 /*----------------------------------------------------------------------
206 ----------------------------------------------------------------------*/
207 LIBEDITTEXT_CURSORFIND_EXTERN
209 LibEditText_CursorFind_FindString(
210 LibEditText_TextInfo *p_tinfo,
211 LibEditText_Cursor *p_cursor,
223 LibEditText_LineInfo *p_line;
225 assert( NULL != p_tinfo );
226 assert( NULL != p_cursor );
227 assert( NULL != pstr_regex );
229 // Clear Before Find Data ---
230 LibEditText_CursorFind_FreeFindData( p_cursor );
232 // Check & Set regex String ---
233 i_result = LibEditText_CursorFind_SetRegexString( p_cursor, pstr_regex, dw_regexlen );
234 if( 0x00 != i_result ) {
238 // Compile regex string ---
239 i_flag = REG_EXTENDED;
240 if( LIBEDITTEXT_FLAGFIND_IGNOREICASE & b_flagfind ) { i_flag |= REG_ICASE; }
242 i_result = regcomp( &(p_cursor->t_findreg), p_cursor->str_regstr, REG_EXTENDED );
243 if( 0x00 != i_result ) {
244 LibEditText_CursorFind_FreeFindData( p_cursor );
250 dw_pstart = p_cursor->dw_pos;
251 i_retfind = LibEditText_CursorFind_ExecFindString(
252 p_tinfo, p_cursor, &dw_findlid, &dw_line, &dw_pstart, &dw_len );
253 if( 0x00 > i_retfind ) {
256 if( LIBEDITTEXT_RET_FINDNOMATCH == i_retfind ) { return i_retfind; }
258 p_line = LINFO(p_tinfo, dw_findlid);
259 assert( NULL != p_line );
261 i_result = LibEditText_CursorInfo_LocateCursor( p_tinfo, p_cursor, p_line, dw_line );
262 if( 0x00 != i_result ) {
263 LibEditText_CursorFind_FreeFindData( p_cursor );
267 i_result = LibEditText_CursorMove_SetPosition( p_tinfo, p_cursor, dw_pstart );
268 if( 0x00 != i_result ) {
269 LibEditText_CursorFind_FreeFindData( p_cursor );
273 p_cursor->dw_find_prevpos_start = dw_pstart;
274 p_cursor->dw_find_prevpos_len = dw_len;
275 p_cursor->dw_find_prevlineid = dw_findlid;
281 /*----------------------------------------------------------------------
282 ----------------------------------------------------------------------*/
283 LIBEDITTEXT_CURSORFIND_EXTERN
285 LibEditText_CursorFind_FindNextString(
286 LibEditText_TextInfo *p_tinfo,
287 LibEditText_Cursor *p_cursor )
295 LibEditText_LineInfo *p_line;
297 assert( NULL != p_tinfo );
298 assert( NULL != p_cursor );
300 // Check PrevFind Data ---
301 if(( '\0' == p_cursor->str_regstr[0] ) || ( 0 == p_cursor->dw_find_prevpos_len ))
305 dw_pstart = LibEditText_CursorFind_GetFindingPostion( p_cursor );
306 i_retfind = LibEditText_CursorFind_ExecFindString(
307 p_tinfo, p_cursor, &dw_findlid, &dw_line, &dw_pstart, &dw_len );
308 if( 0x00 > i_retfind ) {
311 if( LIBEDITTEXT_RET_FINDNOMATCH == i_retfind ) { return i_retfind; }
313 p_line = LINFO(p_tinfo, dw_findlid);
314 assert( NULL != p_line );
316 i_result = LibEditText_CursorInfo_LocateCursor( p_tinfo, p_cursor, p_line, dw_line );
317 if( 0x00 != i_result ) {
318 LibEditText_CursorFind_FreeFindData( p_cursor );
322 i_result = LibEditText_CursorMove_SetPosition( p_tinfo, p_cursor, dw_pstart );
323 if( 0x00 != i_result ) {
324 LibEditText_CursorFind_FreeFindData( p_cursor );
328 p_cursor->dw_find_prevpos_start = dw_pstart;
329 p_cursor->dw_find_prevpos_len = dw_len;
330 p_cursor->dw_find_prevlineid = dw_findlid;
336 /*----------------------------------------------------------------------
337 ----------------------------------------------------------------------*/
339 LibEditText_CursorFind_ExecBackFindString(
340 LibEditText_TextInfo *p_tinfo,
341 LibEditText_Cursor *p_cursor,
357 LibEditText_LineInfo *p_line;
359 regmatch_t t_oldmatch;
361 assert( NULL != p_tinfo );
362 assert( NULL != p_cursor );
365 p_line = LINFO(p_tinfo, p_cursor->dw_lineid);
366 assert( NULL != p_line );
368 dw_lidbase = p_line->dw_id;
369 dw_lnow = p_cursor->dw_line;
370 pb_start = p_tinfo->pb_text + p_line->dw_start;
375 if( 1 == p_tinfo->dw_maxline )
376 { return LIBEDITTEXT_RET_FINDNOMATCH; } // NO match string in Text.
378 p_line = LINFO(p_tinfo, p_line->dw_before);
379 if( NULL == p_line ) {
380 p_line = LINFO(p_tinfo, p_tinfo->dw_line_end);
381 dw_lnow = p_tinfo->dw_maxline - 1;
387 assert( NULL != p_line );
388 pb_start = p_tinfo->pb_text + p_line->dw_start;
389 dw_end = p_line->dw_strlen;
397 i_result = regexec( &(p_cursor->t_findreg),
398 (const char *)pb_start, 1, &t_match, 0x00 );
400 if( 0x00 == i_result ) {
401 t_oldmatch.rm_so = t_match.rm_so;
402 t_oldmatch.rm_eo = t_match.rm_eo;
403 dw_oldbase = dw_nowbase;
405 pb_start += t_match.rm_eo;
406 dw_nowbase += t_match.rm_eo;
408 }while(( 0x00 == i_result ) && ( dw_nowbase < dw_end ));
410 if(( 0x00 != i_result ) && ( REG_NOMATCH != i_result )) {
414 if( dw_nowbase >= dw_end ) {
415 assert( 0x00 == i_result );
416 i_result = REG_NOMATCH;
419 assert( REG_NOMATCH == i_result );
420 if( 0 < dw_nowbase ) {
421 dw_pos = dw_oldbase + t_oldmatch.rm_so;
422 dw_len = t_oldmatch.rm_eo - t_oldmatch.rm_so;
423 // Find String Line is dw_lnow.
428 // Going to Before-Line ---
429 p_line = LINFO(p_tinfo, p_line->dw_before);
430 if( NULL == p_line ) {
431 p_line = LINFO(p_tinfo, p_tinfo->dw_line_end);
432 dw_lnow = p_tinfo->dw_maxline - 1;
438 assert( NULL != p_line );
439 pb_start = p_tinfo->pb_text + p_line->dw_start;
440 dw_end = p_line->dw_strlen;
442 }while( p_line->dw_id != dw_lidbase );
444 if( REG_NOMATCH == i_result )
445 { return LIBEDITTEXT_RET_FINDNOMATCH; } // NO match string in Text.
447 assert( dw_lnow < p_tinfo->dw_maxline );
448 assert( dw_pos < p_line->dw_strlen );
452 *pdw_findlid = p_line->dw_id;
455 i_result = LIBEDITTEXT_RET_FINDMATCH;
456 if( 0x00 != i_round ) { i_result = LIBEDITTEXT_RET_FINDMATCH_ROUND; }
462 /*----------------------------------------------------------------------
463 ----------------------------------------------------------------------*/
464 LIBEDITTEXT_CURSORFIND_EXTERN
466 LibEditText_CursorFind_BackFindString(
467 LibEditText_TextInfo *p_tinfo,
468 LibEditText_Cursor *p_cursor,
480 LibEditText_LineInfo *p_line;
482 assert( NULL != p_tinfo );
483 assert( NULL != p_cursor );
484 assert( NULL != pstr_regex );
486 // Clear Before Find Data ---
487 LibEditText_CursorFind_FreeFindData( p_cursor );
489 // Check & Set regex String ---
490 i_result = LibEditText_CursorFind_SetRegexString( p_cursor, pstr_regex, dw_regexlen );
491 if( 0x00 != i_result ) {
495 // Compile regex string ---
496 i_flag = REG_EXTENDED;
497 if( LIBEDITTEXT_FLAGFIND_IGNOREICASE & b_flagfind ) { i_flag |= REG_ICASE; }
499 i_result = regcomp( &(p_cursor->t_findreg), p_cursor->str_regstr, REG_EXTENDED );
500 if( 0x00 != i_result ) {
501 LibEditText_CursorFind_FreeFindData( p_cursor );
507 dw_pstart = p_cursor->dw_pos;
508 i_retfind = LibEditText_CursorFind_ExecBackFindString(
509 p_tinfo, p_cursor, &dw_findlid, &dw_line, &dw_pstart, &dw_len );
510 if( 0x00 > i_retfind ) {
513 if( LIBEDITTEXT_RET_FINDNOMATCH == i_retfind ) { return i_retfind; }
515 p_line = LINFO(p_tinfo, dw_findlid);
516 assert( NULL != p_line );
518 i_result = LibEditText_CursorInfo_LocateCursor( p_tinfo, p_cursor, p_line, dw_line );
519 if( 0x00 != i_result ) {
520 LibEditText_CursorFind_FreeFindData( p_cursor );
524 i_result = LibEditText_CursorMove_SetPosition( p_tinfo, p_cursor, dw_pstart );
525 if( 0x00 != i_result ) {
526 LibEditText_CursorFind_FreeFindData( p_cursor );
530 p_cursor->dw_find_prevpos_start = dw_pstart;
531 p_cursor->dw_find_prevpos_len = dw_len;
532 p_cursor->dw_find_prevlineid = dw_findlid;
538 /*----------------------------------------------------------------------
539 ----------------------------------------------------------------------*/
540 LIBEDITTEXT_CURSORFIND_EXTERN
542 LibEditText_CursorFind_FreeFindData(
543 LibEditText_Cursor *p_cursor )
545 assert( NULL != p_cursor );
547 if( '\0' == p_cursor->str_regstr[0] ) { return 0x00; }
549 regfree( &(p_cursor->t_findreg) );
550 memset( &(p_cursor->t_findreg), 0x00, sizeof( regex_t ) );
551 memset( p_cursor->str_regstr, 0x00, DRD64_LIBEDITTEXT_DEFAULT_REGSTR_SIZE );
557 /* EOF of drd64_.c ----------------------------------- */