Last active
May 6, 2023 23:53
-
-
Save PeteBlackerThe3rd/8ab24aa87620625d3b43f58965baf24a to your computer and use it in GitHub Desktop.
drop in non-blocking replacement for std::getline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
bool getline_async(std::istream& is, std::string& str, char delim = '\n') { | |
static std::string lineSoFar; | |
char inChar; | |
int charsRead = 0; | |
bool lineRead = false; | |
str = ""; | |
do { | |
charsRead = is.readsome(&inChar, 1); | |
if (charsRead == 1) { | |
// if the delimiter is read then return the string so far | |
if (inChar == delim) { | |
str = lineSoFar; | |
lineSoFar = ""; | |
lineRead = true; | |
} else { // otherwise add it to the string so far | |
lineSoFar.append(1, inChar); | |
} | |
} | |
} while (charsRead != 0 && !lineRead); | |
return lineRead; | |
} |
doesnt work
Can you describe the issue you're having. I've successfully used this function many times.
dont remember but i was using it on a embedded system and it was returning garbage.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
doesnt work