OSDN Git Service

* Implement: LibEditText_BackFindString() API and surround functions. (Non-Tested.)
authorKoine Yuusuke(koinec) <koinec@users.sourceforge.jp>
Mon, 10 Aug 2015 00:21:34 +0000 (09:21 +0900)
committerKoine Yuusuke(koinec) <koinec@users.sourceforge.jp>
Mon, 10 Aug 2015 00:21:34 +0000 (09:21 +0900)
include/libedittext.h
libedittext/drd64_libedittext_cursorapi_find.c
libedittext/drd64_libedittext_cursorfind.c
libedittext/drd64_libedittext_cursorfind.h

index 31caf16..db26e16 100644 (file)
@@ -187,6 +187,9 @@ LIBEDITTEXT_API_CURSORAPI_FIND
                        int i_tinfoid, int i_curid, char *pstr_regex, DWord dw_regexlen, DWord *pdw_len );
 LIBEDITTEXT_API_CURSORAPI_FIND
        int LibEditText_FindNextString( int i_tinfoid, int i_curid, DWord *pdw_len );
+LIBEDITTEXT_API_CURSORAPI_FIND
+       int LibEditText_BackFindString(
+                       int i_tinfoid, int i_curid, char *pstr_regex, DWord dw_regexlen, DWord *pdw_len );
 
 
 #ifdef DRD64_SRC_LIBEDITTEXT_UNDOEDIT
index 8c9ad4f..0ec1511 100644 (file)
@@ -118,4 +118,42 @@ int
 }
 
 
+/***********************************************************************
+***********************************************************************/
+LIBEDITTEXT_API_CURSORAPI_FIND
+int
+       LibEditText_BackFindString(
+                       int             i_tinfoid,
+                       int             i_curid,
+                       char    *pstr_regex,
+                       DWord   dw_regexlen,
+                       DWord   *pdw_len )
+{
+       int             i_result;
+       LibEditText_TextInfo    *p_tinfo;
+       LibEditText_Cursor              *p_cursor;
+
+       p_tinfo = LibEditText_System_GetTextInfo( i_tinfoid );
+       if( NULL == p_tinfo )   {
+               return -0x01;
+       }
+
+       p_cursor        = LibEditText_CursorInfo_GetCursor( p_tinfo, i_curid );
+       if( NULL == p_cursor )  {
+               return -0x02;
+       }
+
+       i_result        = LibEditText_CursorFind_BackFindString(
+                                               p_tinfo, p_cursor, pstr_regex, dw_regexlen, LIBEDITTEXT_FLAGFIND_NORMAL );
+       if( 0x00 > i_result )   {
+               return i_result;
+       }
+
+       if(( LIBEDITTEXT_RET_FINDNOMATCH != i_result ) && ( NULL != pdw_len ))
+               { *pdw_len      = p_cursor->dw_find_prevpos_len; }
+
+       return  i_result;
+}
+
+
 /* EOF of drd64_.c ----------------------------------- */
index 6237e10..95b7da3 100644 (file)
@@ -117,7 +117,6 @@ goto_LibEditText_CursorFind_GetFindingPostion_post:
 
 /*----------------------------------------------------------------------
 ----------------------------------------------------------------------*/
-LIBEDITTEXT_CURSORFIND_EXTERN
 int
        LibEditText_CursorFind_ExecFindString(
                LibEditText_TextInfo    *p_tinfo, 
@@ -336,6 +335,208 @@ int
 
 /*----------------------------------------------------------------------
 ----------------------------------------------------------------------*/
+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(
index 837b5c4..02510f1 100644 (file)
@@ -56,6 +56,10 @@ LIBEDITTEXT_CURSORFIND_EXTERN
        int LibEditText_CursorFind_FindNextString(
                                LibEditText_TextInfo *p_tinfo, LibEditText_Cursor *p_cursor );
 LIBEDITTEXT_CURSORFIND_EXTERN
+       int LibEditText_CursorFind_BackFindString(
+                               LibEditText_TextInfo *p_tinfo, LibEditText_Cursor *p_cursor,
+                               char *pstr_regex, DWord dw_regexlen, Byte b_flagfind );
+LIBEDITTEXT_CURSORFIND_EXTERN
        int LibEditText_CursorFind_FreeFindData( LibEditText_Cursor *p_cursor );