MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey-v1.0 / GetLine

Method GetLine

Source/script.cpp:1381–1474  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1379size_t Script::GetLine(char *aBuf, int aMaxCharsToRead, UCHAR *&aMemFile) // last param = reference to pointer
1380#else
1381size_t Script::GetLine(char *aBuf, int aMaxCharsToRead, FILE *fp)
1382#endif
1383{
1384 size_t aBuf_length = 0;
1385#ifdef AUTOHOTKEYSC
1386 if (!aBuf || !aMemFile) return -1;
1387 if (aMaxCharsToRead <= 0) return -1; // We're signaling to caller that the end of the memory file has been reached.
1388 // Otherwise, continue reading characters from the memory file until either a newline is
1389 // reached or aMaxCharsToRead have been read:
1390 // Track "i" separately from aBuf_length because we want to read beyond the bounds of the memory file.
1391 int i;
1392 for (i = 0; i < aMaxCharsToRead; ++i)
1393 {
1394 if (aMemFile[i] == '\n')
1395 {
1396 // The end of this line has been reached. Don't copy this char into the target buffer.
1397 // In addition, if the previous char was '\r', remove it from the target buffer:
1398 if (aBuf_length > 0 && aBuf[aBuf_length - 1] == '\r')
1399 aBuf[--aBuf_length] = '\0';
1400 ++i; // i.e. so that aMemFile will be adjusted to omit this newline char.
1401 break;
1402 }
1403 else
1404 aBuf[aBuf_length++] = aMemFile[i];
1405 }
1406 // We either read aMaxCharsToRead or reached the end of the line (as indicated by the newline char).
1407 // In the former case, aMemFile might now be changed to be a position outside the bounds of the
1408 // memory area, which the caller will reflect back to us during the next call as a 0 value for
1409 // aMaxCharsToRead, which we then signal to the caller (above) as the end of the file):
1410 aMemFile += i; // Update this value for use by the caller.
1411 // Terminate the buffer (the caller has already ensured that there's room for the terminator
1412 // via its value of aMaxCharsToRead):
1413 aBuf[aBuf_length] = '\0';
1414#else
1415 if (!aBuf || !fp) return -1;
1416 if (aMaxCharsToRead <= 0) return 0;
1417 if (feof(fp)) return -1; // Previous call to this function probably already read the last line.
1418 if (fgets(aBuf, aMaxCharsToRead, fp) == NULL) // end-of-file or error
1419 {
1420 *aBuf = '\0'; // Reset since on error, contents added by fgets() are indeterminate.
1421 return -1;
1422 }
1423 aBuf_length = strlen(aBuf);
1424 if (!aBuf_length)
1425 return 0;
1426 if (aBuf[aBuf_length-1] == '\n')
1427 aBuf[--aBuf_length] = '\0';
1428 if (aBuf[aBuf_length-1] == '\r') // In case there are any, e.g. a Macintosh or Unix file?
1429 aBuf[--aBuf_length] = '\0';
1430#endif
1431
1432 // ltrim to support semicolons after tab keys or other whitespace. Seems best to rtrim also:
1433 trim(aBuf);
1434 if (!strncmp(aBuf, g_CommentFlag, g_CommentFlagLength)) // Case sensitive.
1435 {
1436 *aBuf = '\0';
1437 return 0;
1438 }

Callers

nothing calls this directly

Calls 2

trimFunction · 0.85
rtrim_with_nbspFunction · 0.85

Tested by

no test coverage detected