FBCALL FBSTRING *fb_ConReadLine( int soft_cursor ) { FBSTRING result = { 0 }; int current_x, current_y; int cols, rows; size_t pos, len, tmp_buffer_len = 0; int cursor_visible; int k; char ch, tmp_buffer[12]; fb_GetSize(&cols, &rows); cursor_visible = (fb_Locate( 0, 0, -1, 0, 0 ) & 0x10000) != 0; fb_Locate( 0, 0, FALSE, 0, 0 ); pos = len = 0; fb_PrintBufferEx( NULL, 0, 0 ); /* Ensure that the cursor is visible during INPUT */ fb_Locate( 0, 0, (soft_cursor == FALSE), 0, 0 ); do { size_t delete_char_count = 0, add_char = FALSE; FBSTRING *sTmp; fb_GetXY(¤t_x, ¤t_y); if( soft_cursor ) { fb_PrintFixString( 0, "\377", 0 ); fb_Locate( current_y, current_x, FALSE, 0, 0 ); } while( fb_KeyHit( ) == 0 ) { fb_Delay( 25 ); /* release time slice */ } sTmp = fb_Inkey( ); if( sTmp->data != NULL ) { if( FB_STRSIZE(sTmp) == 2 ) { k = FB_MAKE_EXT_KEY(sTmp->data[1]); ch = 0; } else { k = FB_MAKE_KEY(ch = sTmp->data[0]); } fb_hStrDelTemp( sTmp ); } else { k = 0; continue; } if( soft_cursor ) { char mask[2] = { ((result.data != NULL) && (pos < len)? result.data[pos]: ' '), '\0' }; fb_PrintFixString( 0, mask, 0 ); fb_Locate( current_y, current_x, FALSE, 0, 0 ); } switch (k) { case 8: /* DEL */ if (pos!=0) { DoMove( ¤t_x, ¤t_y, -1, 0, cols, rows ); --pos; delete_char_count = 1; } break; case 9: /* TAB */ tmp_buffer_len = ((pos + 8) / 8 * 8) - pos; memset(tmp_buffer, 32, tmp_buffer_len); add_char = TRUE; break; case 27: /* ESC */ DoMove( ¤t_x, ¤t_y, -pos, 0, cols, rows ); pos = 0; delete_char_count = len; break; case FB_MAKE_EXT_KEY(0x53): /* CLeaR */ if( len!=pos ) { delete_char_count = 1; } else { fb_Beep(); } break; case FB_MAKE_EXT_KEY(0x4B): /* Cursor LEFT */ if( pos != 0 ) { DoMove( ¤t_x, ¤t_y, -1, 0, cols, rows ); --pos; } break; case FB_MAKE_EXT_KEY(0x4D): /* Cursor RIGHT */ if( pos != len ) { DoMove( ¤t_x, ¤t_y, 1, 0, cols, rows ); ++pos; } break; case FB_MAKE_EXT_KEY(0x47): /* HOME */ DoMove( ¤t_x, ¤t_y, -pos, 0, cols, rows ); pos = 0; break; case FB_MAKE_EXT_KEY(0x4F): /* END */ DoMove( ¤t_x, ¤t_y, len-pos, 0, cols, rows ); pos = len; break; case FB_MAKE_EXT_KEY(0x48): /* Cursor UP */ if( pos >= cols) { DoMove( ¤t_x, ¤t_y, -cols, 0, cols, rows ); pos -= cols; } break; case FB_MAKE_EXT_KEY(0x50): /* Cursor DOWN */ if( ( pos + cols ) <= len ) { DoMove( ¤t_x, ¤t_y, cols, 0, cols, rows ); pos += cols; } break; default: if( k>=32 && k<=255 ) { tmp_buffer[0] = (char) k; tmp_buffer_len = 1; add_char = TRUE; /* DoMove( ¤t_x, ¤t_y, 1, 0, cols ); */ } break; } if( delete_char_count!=0 || add_char ) { /* Turn off the cursor during output (speed-up) */ fb_Locate( 0, 0, FALSE, 0, 0 ); } if( delete_char_count ) { FBSTRING *str_fill; FBSTRING *str_left = fb_StrMid( &result, 1, pos ); FBSTRING *str_right = fb_StrMid( &result, pos + 1 + delete_char_count, len - pos - delete_char_count); fb_StrAssign( &result, -1, str_left, -1, FALSE ); fb_StrConcatAssign( &result, -1, str_right, -1, FALSE ); len -= delete_char_count; FB_LOCK(); fb_PrintBufferEx( result.data + pos, len - pos, 0 ); /* Overwrite all deleted characters with SPC's */ str_fill = fb_StrFill1 ( delete_char_count, ' ' ); fb_PrintBufferEx( str_fill->data, delete_char_count, 0 ); fb_hStrDelTemp( str_fill ); fb_Locate( current_y, current_x, -1, 0, 0 ); FB_UNLOCK(); } if( add_char ) { tmp_buffer[tmp_buffer_len] = 0; } if( add_char ) { int old_x = current_x, old_y = current_y; FBSTRING *str_add = fb_StrAllocTempDescF( tmp_buffer, tmp_buffer_len + 1 ); FBSTRING *str_left = fb_StrMid( &result, 1, pos ); FBSTRING *str_right = fb_StrMid( &result, pos + 1, len - pos); fb_StrAssign( &result, -1, str_left, -1, FALSE ); fb_StrConcatAssign( &result, -1, str_add, -1, FALSE ); fb_StrConcatAssign( &result, -1, str_right, -1, FALSE ); len += tmp_buffer_len; FB_LOCK(); fb_PrintBufferEx( result.data + pos, len - pos, 0 ); fb_GetXY(¤t_x, ¤t_y); if( pos==(len-tmp_buffer_len) ) { current_x = old_x; current_y = old_y; DoMove( ¤t_x, ¤t_y, tmp_buffer_len, 0, cols, rows ); } else { int tmp_x_2 = old_x, tmp_y_2 = old_y; DoAdjust( &tmp_x_2, &tmp_y_2, len - pos, 0, cols, rows ); if( tmp_y_2 > (rows+1) || (tmp_y_2==(rows+1) && tmp_x_2>1) ) { DoMove( ¤t_x, ¤t_y, -(len - pos - tmp_buffer_len), 0, cols, rows ); } else { current_x = old_x; current_y = old_y; DoMove( ¤t_x, ¤t_y, tmp_buffer_len, 0, cols, rows ); } } pos += tmp_buffer_len; FB_UNLOCK(); } fb_Locate( 0, 0, (soft_cursor == FALSE), 0, 0 ); } while (k!='\r' && k!='\n'); FB_LOCK(); /* set cursor to end of line */ fb_GetXY(¤t_x, ¤t_y); DoMove( ¤t_x, ¤t_y, len - pos, 0, cols, rows ); /* Restore old cursor visibility */ fb_Locate( 0, 0, cursor_visible, 0, 0 ); FB_UNLOCK(); return fb_StrAllocTempResult( &result ); }
static int fb_hFileLineInputEx ( FB_FILE *handle, void *dst, ssize_t dst_len, int fillrem ) { ssize_t len, readlen; char buffer[BUFFER_LEN]; eInputMode mode = eIM_Invalid; if( !FB_HANDLE_USED(handle) ) return fb_ErrorSetNum( FB_RTERROR_ILLEGALFUNCTIONCALL ); FB_LOCK(); if( handle->hooks->pfnReadLine != NULL ) { mode = eIM_ReadLine; } else if( handle->hooks->pfnRead != NULL && handle->hooks->pfnEof != NULL ) { mode = eIM_Read; } if( mode==eIM_Invalid ) { FB_UNLOCK(); return fb_ErrorSetNum( FB_RTERROR_ILLEGALFUNCTIONCALL ); } switch( mode ) { case eIM_Read: /* This is the VFS-compatible way to read a line ... but it's slow */ len = readlen = 0; while (!handle->hooks->pfnEof(handle)) { int do_add = FALSE, do_break = FALSE; size_t read_len; int res = fb_FileGetDataEx( handle, 0, buffer+len, 1, &read_len, FALSE, FALSE ); if( res==FB_RTERROR_OK && read_len==1) { char ch = buffer[len]; if( ch==13 ) { res = fb_FileGetDataEx( handle, 0, &ch, 1, &read_len, FALSE, FALSE ); if( res==FB_RTERROR_OK && ch!=10 && read_len==1) { fb_FilePutBackEx( handle, &ch, 1 ); } ch = 10; } if( ch==10 ) { do_add = do_break = TRUE; } else { do_add = len==(sizeof(buffer)-1); } } else { do_add = len!=0; } if( do_add || handle->hooks->pfnEof( handle ) ) { /* create temporary string to ensure that NUL's are preserved ... * this function wants the length WITH the NUL character!!! */ buffer[len] = 0; FBSTRING *src = fb_StrAllocTempDescF( buffer, len + 1); if( readlen==0 ) { fb_StrAssign( dst, dst_len, src, -1, fillrem ); } else { fb_StrConcatAssign ( dst, dst_len, src, -1, fillrem ); } readlen += len; len = 0; } else { ++len; } if( res!=FB_RTERROR_OK || do_break ) break; } if( readlen == 0 ) { /* del destine string */ if( dst_len == -1 ) fb_StrDelete( (FBSTRING *)dst ); else *(char *)dst = '\0'; } break; case eIM_ReadLine: /* The read line mode is the most comfortable ... but IMHO it's * only useful for special devices (like SCRN:) */ { /* destine is a var-len string? read directly */ if( dst_len == -1 ) { handle->hooks->pfnReadLine( handle, dst ); } /* fixed-len or unknown size (ie: pointers)? use a temp var-len */ else { FBSTRING str_result = { 0, 0, 0 }; /* read complete line (may include NULs) */ handle->hooks->pfnReadLine( handle, &str_result ); /* add contents of tempporary string to result buffer */ fb_StrAssign( dst, dst_len, (void *)&str_result, -1, fillrem ); /* delete result */ fb_StrDelete( &str_result ); } } break; case eIM_Invalid: /* the "invalid" mode was already handled above ... so we don't * need to do anything here ... */ break; } FB_UNLOCK(); return fb_ErrorSetNum( FB_RTERROR_OK ); }
int fb_DevFileReadLineDumb ( FILE *fp, FBSTRING *dst, fb_FnDevReadString pfnReadString ) { int res = fb_ErrorSetNum( FB_RTERROR_OK ); size_t buffer_len; int found, first_run; char buffer[512]; FBSTRING src = { &buffer[0], 0, 0 }; DBG_ASSERT( dst!=NULL ); buffer_len = sizeof(buffer); first_run = TRUE; FB_LOCK(); if( pfnReadString == NULL ) pfnReadString = hWrapper; found = FALSE; while (!found) { memset( buffer, 0, buffer_len ); if( pfnReadString( buffer, sizeof( buffer ), fp ) == NULL ) { /* EOF reached ... this is not an error !!! */ res = FB_RTERROR_ENDOFFILE; /* but we have to notify the caller */ if( first_run ) fb_StrDelete( dst ); break; } /* the last character always is NUL */ buffer_len = sizeof(buffer) - 1; /* now let's find the end of the buffer */ while (buffer_len--) { char ch = buffer[buffer_len]; if (ch==13 || ch==10) { /* accept both CR and LF */ found = TRUE; break; } else if( ch!=0 ) { /* a character other than CR/LF found ... i.e. buffer full */ break; } } int tmp_buf_len; if( !found ) { /* remember the real length */ tmp_buf_len = buffer_len += 1; /* not found ... so simply add this to the result string */ } else { /* remember the real length */ tmp_buf_len = buffer_len + 1; /* filter a (possibly valid) CR/LF sequence */ if( buffer[buffer_len]==10 && buffer_len!=0 ) { if( buffer[buffer_len-1]==13 ) { --buffer_len; } } /* set the CR or LF to NUL */ buffer[buffer_len] = 0; } src.size = src.len = buffer_len; /* assign or concatenate */ if( first_run ) fb_StrAssign( dst, -1, &src, -1, FALSE ); else fb_StrConcatAssign( dst, -1, &src, -1, FALSE ); first_run = FALSE; buffer_len = tmp_buf_len; } FB_UNLOCK(); return res; }