/*----------------------------------------------------------------------
----------------------------------------------------------------------*/
-LIBEDITTEXT_CURSORFIND_EXTERN
int
LibEditText_CursorFind_ExecFindString(
LibEditText_TextInfo *p_tinfo,
/*----------------------------------------------------------------------
----------------------------------------------------------------------*/
+int
+ LibEditText_CursorFind_ExecBackFindString(
+ LibEditText_TextInfo *p_tinfo,
+ LibEditText_Cursor *p_cursor,
+ DWord *pdw_findlid,
+ DWord *pdw_line,
+ DWord *pdw_pos,
+ DWord *pdw_len )
+{
+ int i_result;
+ int i_round;
+ Byte *pb_start;
+ DWord dw_end;
+ DWord dw_pos;
+ DWord dw_lnow;
+ DWord dw_len;
+ DWord dw_lidbase;
+ DWord dw_nowbase;
+ DWord dw_oldbase;
+ LibEditText_LineInfo *p_line;
+ regmatch_t t_match;
+ regmatch_t t_oldmatch;
+
+ assert( NULL != p_tinfo );
+ assert( NULL != p_cursor );
+
+ // Exec Find ---
+ p_line = LINFO(p_tinfo, p_cursor->dw_lineid);
+ assert( NULL != p_line );
+
+ dw_lidbase = p_line->dw_id;
+ dw_lnow = p_cursor->dw_line;
+ pb_start = p_tinfo->pb_text + p_line->dw_start;
+ dw_end = *pdw_pos;
+ i_round = 0x00;
+
+ if( 0 == dw_end ) {
+ if( 1 == p_tinfo->dw_maxline )
+ { return LIBEDITTEXT_RET_FINDNOMATCH; } // NO match string in Text.
+
+ p_line = LINFO(p_tinfo, p_line->dw_before);
+ if( NULL == p_line ) {
+ p_line = LINFO(p_tinfo, p_tinfo->dw_line_end);
+ dw_lnow = p_tinfo->dw_maxline - 1;
+ i_round = 0x01;
+ }
+ else {
+ dw_lnow--;
+ }
+ assert( NULL != p_line );
+ pb_start = p_tinfo->pb_text + p_line->dw_start;
+ dw_end = p_line->dw_strlen;
+ }
+
+ do {
+
+ dw_nowbase = 0;
+ dw_oldbase = 0;
+ do {
+ i_result = regexec( &(p_cursor->t_findreg),
+ (const char *)pb_start, 1, &t_match, 0x00 );
+
+ if( 0x00 == i_result ) {
+ t_oldmatch.rm_so = t_match.rm_so;
+ t_oldmatch.rm_eo = t_match.rm_eo;
+ dw_oldbase = dw_nowbase;
+
+ pb_start += t_match.rm_eo;
+ dw_nowbase += t_match.rm_eo;
+ }
+ }while(( 0x00 == i_result ) && ( dw_nowbase < dw_end ));
+
+ if(( 0x00 != i_result ) && ( REG_NOMATCH != i_result )) {
+ return -0x10;
+ }
+
+ if( dw_nowbase >= dw_end ) {
+ assert( 0x00 == i_result );
+ i_result = REG_NOMATCH;
+ }
+
+ assert( REG_NOMATCH == i_result );
+ if( 0 < dw_nowbase ) {
+ dw_pos = dw_oldbase + t_oldmatch.rm_so;
+ dw_len = t_oldmatch.rm_eo - t_oldmatch.rm_so;
+ // Find String Line is dw_lnow.
+ i_result = 0x00;
+ break;
+ }
+
+ // Going to Before-Line ---
+ p_line = LINFO(p_tinfo, p_line->dw_before);
+ if( NULL == p_line ) {
+ p_line = LINFO(p_tinfo, p_tinfo->dw_line_end);
+ dw_lnow = p_tinfo->dw_maxline - 1;
+ i_round = 0x01;
+ }
+ else {
+ dw_lnow--;
+ }
+ assert( NULL != p_line );
+ pb_start = p_tinfo->pb_text + p_line->dw_start;
+ dw_end = p_line->dw_strlen;
+
+ }while( p_line->dw_id != dw_lidbase );
+
+ if( REG_NOMATCH == i_result )
+ { return LIBEDITTEXT_RET_FINDNOMATCH; } // NO match string in Text.
+
+ assert( dw_lnow < p_tinfo->dw_maxline );
+ assert( dw_pos < p_line->dw_strlen );
+
+ *pdw_pos = dw_pos;
+ *pdw_len = dw_len;
+ *pdw_findlid = p_line->dw_id;
+ *pdw_line = dw_lnow;
+
+ i_result = LIBEDITTEXT_RET_FINDMATCH;
+ if( 0x00 != i_round ) { i_result = LIBEDITTEXT_RET_FINDMATCH_ROUND; }
+
+ return i_result;
+}
+
+
+/*----------------------------------------------------------------------
+----------------------------------------------------------------------*/
+LIBEDITTEXT_CURSORFIND_EXTERN
+int
+ LibEditText_CursorFind_BackFindString(
+ LibEditText_TextInfo *p_tinfo,
+ LibEditText_Cursor *p_cursor,
+ char *pstr_regex,
+ DWord dw_regexlen,
+ Byte b_flagfind )
+{
+ int i_retfind;
+ int i_result;
+ int i_flag;
+ DWord dw_pstart;
+ DWord dw_len;
+ DWord dw_line;
+ DWord dw_findlid;
+ LibEditText_LineInfo *p_line;
+
+ assert( NULL != p_tinfo );
+ assert( NULL != p_cursor );
+ assert( NULL != pstr_regex );
+
+ // Clear Before Find Data ---
+ LibEditText_CursorFind_FreeFindData( p_cursor );
+
+ // Check & Set regex String ---
+ i_result = LibEditText_CursorFind_SetRegexString( p_cursor, pstr_regex, dw_regexlen );
+ if( 0x00 != i_result ) {
+ return i_result;
+ }
+
+ // Compile regex string ---
+ i_flag = REG_EXTENDED;
+ if( LIBEDITTEXT_FLAGFIND_IGNOREICASE & b_flagfind ) { i_flag |= REG_ICASE; }
+
+ i_result = regcomp( &(p_cursor->t_findreg), p_cursor->str_regstr, REG_EXTENDED );
+ if( 0x00 != i_result ) {
+ LibEditText_CursorFind_FreeFindData( p_cursor );
+
+ return i_result;
+ }
+
+ // Exec Find ---
+ dw_pstart = p_cursor->dw_pos;
+ i_retfind = LibEditText_CursorFind_ExecBackFindString(
+ p_tinfo, p_cursor, &dw_findlid, &dw_line, &dw_pstart, &dw_len );
+ if( 0x00 > i_retfind ) {
+ return i_retfind;
+ }
+ if( LIBEDITTEXT_RET_FINDNOMATCH == i_retfind ) { return i_retfind; }
+
+ p_line = LINFO(p_tinfo, dw_findlid);
+ assert( NULL != p_line );
+
+ i_result = LibEditText_CursorInfo_LocateCursor( p_tinfo, p_cursor, p_line, dw_line );
+ if( 0x00 != i_result ) {
+ LibEditText_CursorFind_FreeFindData( p_cursor );
+ return -0x20;
+ }
+
+ i_result = LibEditText_CursorMove_SetPosition( p_tinfo, p_cursor, dw_pstart );
+ if( 0x00 != i_result ) {
+ LibEditText_CursorFind_FreeFindData( p_cursor );
+ return -0x21;
+ }
+
+ p_cursor->dw_find_prevpos_start = dw_pstart;
+ p_cursor->dw_find_prevpos_len = dw_len;
+ p_cursor->dw_find_prevlineid = dw_findlid;
+
+ return i_retfind;
+}
+
+
+/*----------------------------------------------------------------------
+----------------------------------------------------------------------*/
LIBEDITTEXT_CURSORFIND_EXTERN
int
LibEditText_CursorFind_FreeFindData(