MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / GetLine

Method GetLine

source/script.cpp:3418–3545  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3416
3417
3418size_t Script::GetLine(LineBuffer &aBuf, int aInContinuationSection, bool aInBlockComment, TextStream *ts)
3419{
3420 size_t aBuf_length = 0;
3421 for (;;)
3422 {
3423 auto aBuf_capacity = aBuf.Capacity();
3424 auto read_length = ts->ReadLine(aBuf + aBuf_length, (DWORD)(aBuf_capacity - aBuf_length));
3425 if (!read_length && !aBuf_length) // End of file was reached and there's no line text from a previous iteration.
3426 {
3427 aBuf[0] = '\0';
3428 return -1;
3429 }
3430 aBuf_length += read_length;
3431 if (aBuf[aBuf_length - 1] == '\n')
3432 --aBuf_length;
3433 if (aBuf_length < aBuf_capacity)
3434 break; // It read one complete line, either finding a line ending or reaching end of file.
3435 // This line is at least the size of aBuf, so expand aBuf to read more.
3436 if (!aBuf.Expand())
3437 return -1;
3438 }
3439 aBuf[aBuf_length] = '\0';
3440
3441 if (aInContinuationSection)
3442 {
3443 LPTSTR cp = omit_leading_whitespace(aBuf);
3444 if (aInContinuationSection == CONTINUATION_SECTION_WITHOUT_COMMENTS) // By default, continuation sections don't allow comments (lines beginning with a semicolon are treated as literal text).
3445 {
3446 // Caller relies on us to detect the end of the continuation section so that trimming
3447 // will be done on the final line of the section and so that a comment can immediately
3448 // follow the closing parenthesis (on the same line). Example:
3449 // (
3450 // Text
3451 // ) ; Same line comment.
3452 if (*cp != ')') // This isn't the last line of the continuation section, so leave the line untrimmed (caller will apply the ltrim setting on its own).
3453 return aBuf_length; // Earlier sections are responsible for keeping aBufLength up-to-date with any changes to aBuf.
3454 //else this line starts with ')', so continue on to later section that checks for a same-line comment on its right side.
3455 }
3456 else // aInContinuationSection == CONTINUATION_SECTION_WITH_COMMENTS (i.e. comments are allowed in this continuation section).
3457 {
3458 // Fix for v1.0.46.09+: The "com" option shouldn't put "ltrim" into effect.
3459 if (*cp == g_CommentChar)
3460 {
3461 *aBuf = '\0'; // Since this line is a comment, have the caller ignore it.
3462 return -2; // Callers tolerate -2 only when in a continuation section. -2 indicates, "don't include this line at all, not even as a blank line to which the JOIN string (default "\n") will apply.
3463 }
3464 if (*cp == ')') // This is the last line of the continuation section.
3465 {
3466 ltrim(aBuf); // Ltrim this line unconditionally so that caller will see that it starts with ')' without having to do extra steps.
3467 aBuf_length = _tcslen(aBuf); // ltrim() doesn't always return an accurate length, so do it this way.
3468 }
3469 }
3470 }
3471 // Since above didn't return, either:
3472 // 1) We're not in a continuation section at all, so apply ltrim() to support semicolons after tabs or
3473 // other whitespace. Seems best to rtrim also.
3474 // 2) CONTINUATION_SECTION_WITHOUT_COMMENTS but this line is the final line of the section. Apply
3475 // trim() and other logic further below because caller might rely on it.

Callers

nothing calls this directly

Calls 7

omit_leading_whitespaceFunction · 0.85
ltrimFunction · 0.85
trimFunction · 0.85
rtrim_with_nbspFunction · 0.85
ReadLineMethod · 0.80
CapacityMethod · 0.45
ExpandMethod · 0.45

Tested by

no test coverage detected