数据通信源代码(牧云水务信息大数据存储与分析系统).docx_第1页
数据通信源代码(牧云水务信息大数据存储与分析系统).docx_第2页
数据通信源代码(牧云水务信息大数据存储与分析系统).docx_第3页
数据通信源代码(牧云水务信息大数据存储与分析系统).docx_第4页
数据通信源代码(牧云水务信息大数据存储与分析系统).docx_第5页
已阅读5页,还剩55页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/* linenoise.c - guerrilla line editing library against the idea that a * * Copyright (c) 2010, Salvatore Sanfilippo * Copyright (c) 2010, Pieter Noordhuis * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Redis nor the names of its contributors may be used * to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * line editing lib needs to be 20,000 lines of C code. * * You can find the latest source code at: * * /antirez/linenoise * * Does a number of crazy assumptions that happen to be true in 99.9999% of * the 2010 UNIX computers around. * * References: * - /xterm/ctlseqs/ctlseqs.html * - /nw/WWW/products/wizcon/vt220.html * * Todo list: * - Switch to gets() if $TERM is something we cant support. * - Filter bogus Ctrl+ combinations. * - Win32 support * * Bloat: * - Completion? * - History search like Ctrl+r in readline? * * List of escape sequences used by this program, we do everything just * with three sequences. In order to be so cheap we may have some * flickering effect with some slow terminal, but the lesser sequences * the more compatible. * * CHA (Cursor Horizontal Absolute) * Sequence: ESC n G * Effect: moves cursor to column n (1 based) * * EL (Erase Line) * Sequence: ESC n K * Effect: if n is 0 or missing, clear from cursor to end of line * Effect: if n is 1, clear from beginning of line to cursor * Effect: if n is 2, clear entire line * * CUF (Cursor Forward) * Sequence: ESC n C * Effect: moves cursor forward of n chars * * The following are used to clear the screen: ESC H ESC 2 J * This is actually composed of two sequences: * * cursorhome * Sequence: ESC H * Effect: moves the cursor to upper left corner * * ED2 (Clear entire screen) * Sequence: ESC 2 J * Effect: clear the whole screen * */#ifdef _WIN32#include #include #include #define snprintf _snprintf / Microsoft headers use underscores in some names#define strcasecmp _stricmp#define strdup _strdup#define isatty _isatty#define write _write#define STDIN_FILENO 0#else /* _WIN32 */#include #include #include #include #include #include #include #include #include #endif /* _WIN32 */#include #include #include #include linenoise.h#include linenoise_utf8.h#include mk_wcwidth.h#include #include #include using std:string;using std:vector;using boost:scoped_array;using linenoise_utf8:UChar8;using linenoise_utf8:UChar32;using linenoise_utf8:copyString8to32;using linenoise_utf8:copyString32;using linenoise_utf8:copyString32to8;using linenoise_utf8:strlen32;using linenoise_utf8:strncmp32;using linenoise_utf8:write32;using linenoise_utf8:Utf8String;using linenoise_utf8:Utf32String;struct linenoiseCompletions vector completionStrings;#define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100#define LINENOISE_MAX_LINE 4096/ make control-characters more readable#define ctrlChar( upperCaseASCII ) ( upperCaseASCII - 0x40 )/* * Recompute widths of all characters in a UChar32 buffer * param text input buffer of Unicode characters * param widths output buffer of character widths * param charCount number of characters in buffer */static void recomputeCharacterWidths( const UChar32* text, char* widths, int charCount ) for ( int i = 0; i 0 ) int charsThisRow = ( x + charsRemaining screenColumns ) ? charsRemaining : screenColumns - x; xOut = x + charsThisRow; yOut = y; charsRemaining -= charsThisRow; x = 0; +y; if ( xOut = screenColumns ) / we have to special-case line wrap xOut = 0; +yOut; /* * Calculate a column width using mk_wcswidth() * param buf32 text to calculate * param len length of text to calculate */static int calculateColumnPosition( UChar32* buf32, int len) int width = mk_wcswidth( reinterpret_cast( buf32 ), len ); if ( width = -1 ) return len; else return width;static bool isControlChar( UChar32 testChar ) return ( testChar = 0x7F & testChar = 0x9F ); / DEL and C1 controlsstruct PromptBase / a convenience struct for grouping prompt info Utf32String promptText; / our copy of the prompt text, edited char* promptCharWidths; / character widths from mk_wcwidth() int promptChars; / chars in promptText int promptExtraLines; / extra lines (beyond 1) occupied by prompt int promptIndentation; / column offset to end of prompt int promptLastLinePosition; / index into promptText where last line begins int promptPreviousInputLen; / promptChars of previous input line, for clearing int promptCursorRowOffset; / where the cursor is relative to the start of the prompt int promptScreenColumns; / width of screen in columns int promptPreviousLen; / help erasing int promptErrorCode; / error code (invalid UTF-8) or zero PromptBase() : promptPreviousInputLen( 0 ) ;struct PromptInfo : public PromptBase PromptInfo( const UChar8* textPtr, int columns ) promptExtraLines = 0; promptLastLinePosition = 0; promptPreviousLen = 0; promptScreenColumns = columns; Utf32String tempUnicode( textPtr ); / strip control characters from the prompt - we do allow newline UChar32* pIn = tempUnicode.get(); UChar32* pOut = pIn; while ( *pIn ) UChar32 c = *pIn; if ( n = c | !isControlChar( c ) ) *pOut = c; +pOut; +pIn; *pOut = 0; promptChars = pOut - tempUnicode.get(); promptText = tempUnicode; int x = 0; for ( int i = 0; i = promptScreenColumns ) x = 0; +promptExtraLines; promptLastLinePosition = i + 1; promptIndentation = promptChars - promptLastLinePosition; promptCursorRowOffset = promptExtraLines; ;/ Used with DynamicPrompt (history search)/static const Utf32String forwardSearchBasePrompt( reinterpret_cast( (i-search) ) );static const Utf32String reverseSearchBasePrompt( reinterpret_cast( (reverse-i-search) ) );static const Utf32String endSearchBasePrompt( reinterpret_cast( : ) );static Utf32String previousSearchText; / remembered across invocations of linenoise()/ changing prompt for (reverse-i-search)text: etc./struct DynamicPrompt : public PromptBase Utf32String searchText; / text we are searching for char* searchCharWidths; / character widths from mk_wcwidth() int searchTextLen; / chars in searchText int direction; / current search direction, 1=forward, -1=reverse DynamicPrompt( PromptBase& pi, int initialDirection ) : searchTextLen( 0 ), direction( initialDirection ) promptScreenColumns = mptScreenColumns; promptCursorRowOffset = 0; Utf32String emptyString( 1 ); searchText = emptyString; const Utf32String* basePrompt = ( direction 0 ) ? &forwardSearchBasePrompt : &reverseSearchBasePrompt; size_t promptStartLength = basePrompt-length(); promptChars = promptStartLength + endSearchBasePrompt.length(); promptLastLinePosition = promptChars; / TODO fix this, we are asssuming that the history prompt wont wrap (!) promptPreviousLen = promptChars; Utf32String tempUnicode( promptChars + 1 ); memcpy( tempUnicode.get(), basePrompt-get(), sizeof( UChar32 ) * promptStartLength ); memcpy( &tempUnicodepromptStartLength, endSearchBasePrompt.get(), sizeof( UChar32 ) * ( endSearchBasePrompt.length() + 1 ) ); tempUnicode.initFromBuffer(); promptText = tempUnicode; calculateScreenPosition( 0, 0, mptScreenColumns, promptChars, promptIndentation, promptExtraLines ); void updateSearchPrompt( void ) const Utf32String* basePrompt = ( direction 0 ) ? &forwardSearchBasePrompt : &reverseSearchBasePrompt; size_t promptStartLength = basePrompt-length(); promptChars = promptStartLength + searchTextLen + endSearchBasePrompt.length(); Utf32String tempUnicode( promptChars + 1 ); memcpy( tempUnicode.get(), basePrompt-get(), sizeof( UChar32 ) * promptStartLength ); memcpy( &tempUnicodepromptStartLength, searchText.get(), sizeof( UChar32 ) * searchTextLen ); size_t endIndex = promptStartLength + searchTextLen; memcpy( &tempUnicodeendIndex, endSearchBasePrompt.get(), sizeof( UChar32 ) * ( endSearchBasePrompt.length() + 1 ) ); tempUnicode.initFromBuffer(); promptText = tempUnicode; void updateSearchText( const UChar32* textPtr ) Utf32String tempUnicode( textPtr ); searchTextLen = tempUnicode.chars(); searchText = tempUnicode; updateSearchPrompt(); ;class KillRing static const int capacity = 10; int size; int index; char indexToSlot10; vector theRing;public: enum action actionOther, actionKill, actionYank ; action lastAction; size_t lastYankSize; KillRing() : size( 0 ), index( 0 ), lastAction( actionOther ) theRing.reserve( capacity ); void kill( const UChar32* text, int textLen, bool forward ) if ( textLen = 0 ) return; Utf32String killedText( text, textLen ); if ( lastAction = actionKill & size 0 ) int slot = indexToSlot0; int currentLen = theRingslot.length(); int resultLen = currentLen + textLen; Utf32String temp( resultLen + 1 ); if ( forward ) memcpy( temp.get(), theRingslot.get(), currentLen * sizeof( UChar32 ) ); memcpy( &tempcurrentLen, killedText.get(), textLen * sizeof( UChar32 ) ); else memcpy( temp.get(), killedText.get(), textLen * sizeof( UChar32 ) ); memcpy( &temptextLen, theRingslot.get(), currentLen * sizeof( UChar32 ) ); tempresultLen = 0; temp.initFromBuffer(); theRingslot = temp; else if ( size 0 ) memmove( &indexToSlot1, &indexToSlot0, size ); indexToSlot0 = size; size+; theRing.push_back( killedText ); else int slot = indexToSlotcapacity - 1; theRingslot = killedText; memmove( &indexToSlot1, &indexToSlot0, capacity - 1 ); indexToSlot0 = slot; index = 0; Utf32String* yank() return ( size 0 ) ? &theRingindexToSlotindex : 0; Utf32String* yankPop() if ( size = 0 ) return 0; +index; if ( index = size ) index = 0; return &theRingindexToSlotindex; ;class InputBuffer UChar32* buf32; / input buffer char* charWidths; / character widths from mk_wcwidth() int buflen; / buffer size in characters int len; / length of text in input buffer int pos; / character position in buffer ( 0 = pos = len ) void clearScreen( PromptBase& pi ); int incrementalHistorySearch( PromptBase& pi, int startChar ); int completeLine( PromptBase& pi ); void refreshLine( PromptBase& pi );public: InputBuffer( UChar32* buffer, char* widthArray, int bufferLen ) : buf32( buffer ), charWidths( widthArray ), buflen( bufferLen - 1 ), len( 0 ), pos( 0 ) buf320 = 0; void preloadBuffer( const UChar8* preloadText ) size_t ucharCount; int errorCode; copyString8to32( buf32, preloadText, buflen + 1, ucharCount, errorCode ); recomputeCharacterWidths( buf32, charWidths, ucharCount ); len = ucharCount; pos = ucharCount; int getInputLine( PromptBase& pi ); int length( void ) const return len; ;/ Special codes for keyboard input:/ Between Windows and the various Linux terminal programs, there is some/ pretty diverse behavior in the scan codes and escape sequences we are/ presented with. So . well translate them all into our own pidgin/ pseudocode, trying to stay out of the way of UTF-8 and international/ characters. Heres the general plan./ User input keystrokes (key chords, whatever) will be encoded as a single value./ The low 21 bits are reserved for Unicode characters. Popular function-type keys/ get their own codes in the range 0x10200000 to (if needed) 0x1FE00000, currently/ just arrow keys, Home, End and Delete. Keypresses with Ctrl get ORed with/ 0x20000000, with Alt get ORed with 0x40000000. So, Ctrl+Alt+Home is encoded/ as 0x20000000 + 0x40000000 + 0x10A00000 = 0x70A00000. To keep things complicated,/ the Alt key is equivalent to prefixing the keystroke with ESC, so ESC followed by/ D is treated the same as Alt + D . well just use Emacs terminology and call/ this Meta. So, we will encode both ESC followed by D and Alt held down while D/ is pressed the same, as Meta-D, encoded as 0x40000064./ Here are the definitions of our component constants:/ Maximum unsigned 32-bit value = 0xFFFFFFFF; / For reference, max 32-bit value/ Highest allocated Unicode char = 0x001FFFFF; / For reference, max Unicode valuestatic const int META = 0x40000000; / Meta key combinationstatic const int CTRL = 0x20000000; / Ctrl key combinationstatic const int SPECIAL_KEY = 0x10000000; / Common bit for all special keysstatic const int UP_ARROW_KEY = 0x10200000; / Special keysstatic const int DOWN_ARROW_KEY = 0x10400000;static const int RIGHT_ARROW_KEY = 0x10600000;static const int LEFT_ARROW_KEY = 0x10800000;static const int HOME_KEY = 0x10A00000;static const int END_KEY = 0x10C00000;static const int DELETE_KEY = 0x10E00000;static const int PAGE_UP_KEY = 0x11000000;static const int PAGE_DOWN_

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论