From 845c14b1972e448408109b30a9a9422cac092f16 Mon Sep 17 00:00:00 2001 From: nyamatongwe Date: Sun, 29 Feb 2004 08:51:54 +0000 Subject: Patch from Bruce to return required allocation size when a NULL pointer is used as a stringresult argument. --- doc/ScintillaDoc.html | 44 +++++++++++++++++++++++++------------------- include/Scintilla.iface | 5 +++-- src/Editor.cxx | 33 ++++++++++++++++++++++++--------- 3 files changed, 52 insertions(+), 30 deletions(-) diff --git a/doc/ScintillaDoc.html b/doc/ScintillaDoc.html index 02aae7c9b..73fcaad4c 100644 --- a/doc/ScintillaDoc.html +++ b/doc/ScintillaDoc.html @@ -38,7 +38,7 @@

Scintilla Documentation

-

Last edited 16/May/2003 NH

+

Last edited 29/February/2004 NH

There is an overview of the internal design of Scintilla.
@@ -96,7 +96,7 @@ - const char* + const char * Arguments point at text that is being passed to Scintilla but not modified. The text may be zero terminated or another argument may specify the character count, the @@ -104,11 +104,12 @@ - char* + char * Arguments point at text buffers that Scintilla will fill with text. In some cases, another argument will tell Scintilla the buffer size. In others, you must make sure that - the buffer is big enough to hold the requested text. + the buffer is big enough to hold the requested text. If a NULL pointer (0) is passed + then, for SCI_* calls, the length that should be allocated is returned. @@ -293,10 +294,10 @@ SCI_GETTEXTRANGE(<unused>, TextRange *tr)
SCI_ALLOCATE(int bytes, <unused>)
- SCI_ADDTEXT(int length, char *s)
+ SCI_ADDTEXT(int length, const char *s)
SCI_ADDSTYLEDTEXT(int length, cell *s)
SCI_APPENDTEXT(int length, const char *s)
- SCI_INSERTTEXT(int pos, char *text)
+ SCI_INSERTTEXT(int pos, const char *text)
SCI_CLEARALL
SCI_CLEARDOCUMENTSTYLE
SCI_GETCHARAT(int position)
@@ -312,7 +313,9 @@ terminating 0 character. To collect all the text in a document, use SCI_GETLENGTH to get the number of characters in the document (nLen), allocate a character buffer of length nLen+1 bytes, then call SCI_GETTEXT(nLen+1, char - *text). If you then save the text, you should use SCI_SETSAVEPOINT to mark + *text). If the text argument is 0 then the length that should be allocated to store the + entire document is returned. + If you then save the text, you should use SCI_SETSAVEPOINT to mark the text as unmodified.

See also: SCI_GETSELTEXT, SCI_LINELENGTH(int line). The returned value is the number of characters copied to the buffer. The returned text includes any end of line characters. If you ask for a line number outside the range of lines in the document, 0 - characters are copied.

+ characters are copied. If the text argument is 0 then the length that should be allocated + to store the entire line is returned.

See also: SCI_GETCURLINE, SCI_GETSELTEXT,
SCI_POSITIONBEFORE(int position)
SCI_POSITIONAFTER(int position)
- SCI_TEXTWIDTH(int styleNumber, char *text)
+ SCI_TEXTWIDTH(int styleNumber, const char *text)
SCI_TEXTHEIGHT(int line)
SCI_CHOOSECARETX
@@ -1062,7 +1066,8 @@ struct TextToFind { This copies the currently selected text and a terminating 0 byte to the text buffer. The buffer must be at least SCI_GETSELECTIONEND()-SCI_GETSELECTIONSTART()+1 bytes long.
-
+ If the text argument is 0 then the length that should be allocated + to store the entire selection is returned.

See also: SCI_GETCURLINE, SCI_GETLINE, char* text pointing at a buffer large enough to hold the text you wish to retrieve and a terminating 0 character. Set textLen to the - length of the buffer.

+ length of the buffer. If the text argument is 0 then the length that should be allocated + to store the entire current line is returned.

See also: SCI_GETSELTEXT, SCI_GETLINE, -

SCI_TEXTWIDTH(int styleNumber, char *text)
+

SCI_TEXTWIDTH(int styleNumber, const char *text)
This returns the pixel width of a string drawn in the given styleNumber which can be used, for example, to decide how wide to make the line number margin in order to display a given number of numerals.

@@ -1927,7 +1933,7 @@ struct TextToFind { 2. Use SCI_STYLECLEARALL to copy this to all styles.
3. Set the style attributes that make your lexical styles different.

-

SCI_STYLESETFONT(int styleNumber, char *fontName)
+

SCI_STYLESETFONT(int styleNumber, const char *fontName)
SCI_STYLESETSIZE(int styleNumber, int sizeInPoints)
SCI_STYLESETBOLD(int styleNumber, bool bold)
SCI_STYLESETITALIC(int styleNumber, bool italic)
@@ -4080,8 +4086,8 @@ sptr_t CallScintilla(unsigned int iMessage, uptr_t wParam, sptr_t lParam){
SCI_LOADLEXERLIBRARY(<unused>, char *path)
SCI_COLOURISE(int start, int end)
- SCI_SETPROPERTY(char *key, char *value)
- SCI_SETKEYWORDS(int keyWordSet, char + SCI_SETPROPERTY(const char *key, const char *value)
+ SCI_SETKEYWORDS(int keyWordSet, const char *keyWordList)
@@ -4095,7 +4101,7 @@ sptr_t CallScintilla(unsigned int iMessage, uptr_t wParam, sptr_t lParam){ SCLEX_AUTOMATIC value; this identifies additional external lexers that Scintilla assigns unused lexer numbers to.

-

SCI_SETLEXERLANGUAGE(<unused>, char *name)
+

SCI_SETLEXERLANGUAGE(<unused>, const char *name)
This message lets you select a lexer by name, and is the only method if you are using an external lexer or if you have written a lexer module for a language of your own and do not wish to assign it an explicit lexer number. To select an existing lexer, set name to @@ -4108,7 +4114,7 @@ sptr_t CallScintilla(unsigned int iMessage, uptr_t wParam, sptr_t lParam){ href="#SCI_GETLEXER">SCI_GETLEXER before and after setting the new lexer to see if the lexer number changed.

-

SCI_LOADLEXERLIBRARY(<unused>, char *path)
+

SCI_LOADLEXERLIBRARY(<unused>, const char *path)
Load a lexer implemented in a shared library. This is a .so file on GTK+/Linux or a .DLL file on Windows.

@@ -4764,9 +4770,9 @@ for line = lineStart to lineEnd do SCI_ENSUREVISIBLE(line) next replace them with the Scintilla equivalent.

 WM_GETTEXT(int length, char *text)
-WM_SETTEXT(<unused>, char *text)
+WM_SETTEXT(<unused>, const char *text)
 EM_GETLINE(int line, char *text)
-EM_REPLACESEL(<unused>, char *text)
+EM_REPLACESEL(<unused>, const char *text)
 EM_SETREADONLY
 EM_GETTEXTRANGE(<unused>, TEXTRANGE *tr)
 WM_CUT
diff --git a/include/Scintilla.iface b/include/Scintilla.iface
index aa3022173..e6ca585ef 100644
--- a/include/Scintilla.iface
+++ b/include/Scintilla.iface
@@ -53,7 +53,7 @@
 ##     position -> integer position in a document
 ##     colour -> colour integer containing red, green and blue bytes.
 ##     string -> pointer to const character
-##     stringresult -> pointer to character
+##     stringresult -> pointer to character, NULL-> return size of result
 ##     cells -> pointer to array of cells, each cell containing a style byte and character byte
 ##     textrange -> range of a min and a max position with an output string
 ##     findtext -> searchrange, text -> foundposition
@@ -74,7 +74,8 @@
 ## argument and retrieve NUL characters. All retrieved strings except for those retrieved
 ## by GetLine also have a NUL appended but client code should calculate the size that
 ## will be returned rather than relying upon the NUL whenever possible. Allow for the
-## extra NUL character when allocating buffers.
+## extra NUL character when allocating buffers. The size to allocate for a stringresult
+## can be determined by calling with a NULL (0) pointer.
 
 cat Basics
 
diff --git a/src/Editor.cxx b/src/Editor.cxx
index ba35bc5d9..3f30ba3bf 100644
--- a/src/Editor.cxx
+++ b/src/Editor.cxx
@@ -5407,7 +5407,7 @@ sptr_t Editor::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
 
 	case SCI_GETTEXT: {
 			if (lParam == 0)
-				return 0;
+				return pdoc->Length() + 1;
 			if (wParam == 0)
 				return 0;
 			char *ptr = CharPtrFromSPtr(lParam);
@@ -5475,11 +5475,11 @@ sptr_t Editor::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
 		return topLine;
 
 	case SCI_GETLINE: {	// Risk of overwriting the end of the buffer
-			if (lParam == 0) {
-				return 0;
-			}
 			int lineStart = pdoc->LineStart(wParam);
 			int lineEnd = pdoc->LineStart(wParam + 1);
+			if (lParam == 0) {
+				return lineEnd - lineStart;
+			}
 			char *ptr = CharPtrFromSPtr(lParam);
 			int iPlace = 0;
 			for (int iChar = lineStart; iChar < lineEnd; iChar++) {
@@ -5511,8 +5511,23 @@ sptr_t Editor::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
 		break;
 
 	case SCI_GETSELTEXT: {
-			if (lParam == 0)
-				return 0;
+			if (lParam == 0) {
+				if (selType == selStream) {
+					return 1 + SelectionEnd() - SelectionStart();
+				} else {
+					// TODO: why is selLines handled the slow way?
+					int size = 0;
+					int extraCharsPerLine = 0;
+					if (selType != selLines)
+						extraCharsPerLine = (pdoc->eolMode == SC_EOL_CRLF) ? 2 : 1;
+					SelectionLineIterator lineIterator(this);
+					while (lineIterator.Iterate()) {
+						size += lineIterator.endPos + extraCharsPerLine - lineIterator.startPos;
+					}
+
+					return 1 + size;
+				}
+			}
 			SelectionText selectedText;
 			CopySelectionRange(&selectedText);
 			char *ptr = CharPtrFromSPtr(lParam);
@@ -5913,12 +5928,12 @@ sptr_t Editor::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
 		break;
 
 	case SCI_GETCURLINE: {
-			if (lParam == 0) {
-				return 0;
-			}
 			int lineCurrentPos = pdoc->LineFromPosition(currentPos);
 			int lineStart = pdoc->LineStart(lineCurrentPos);
 			unsigned int lineEnd = pdoc->LineStart(lineCurrentPos + 1);
+			if (lParam == 0) {
+				return 1 + lineEnd - lineStart;
+			}
 			char *ptr = CharPtrFromSPtr(lParam);
 			unsigned int iPlace = 0;
 			for (unsigned int iChar = lineStart; iChar < lineEnd && iPlace < wParam - 1; iChar++) {
-- 
cgit v1.2.3