App2: hook up console for reading

This commit is contained in:
Wolfgang Thaller 2014-09-15 00:00:45 +02:00
parent f4a8005a8f
commit dea5fa2a21
2 changed files with 28 additions and 0 deletions

View File

@ -65,6 +65,22 @@ extern "C" ssize_t consolewrite(int fd, const void *buf, size_t count)
return count; return count;
} }
extern ssize_t (*__read_hook)(int fd, void*buf, size_t count);
extern "C" ssize_t consoleread(int fd, void *buf, size_t count)
{
static std::string consoleBuf;
if(consoleBuf.size() == 0)
{
consoleBuf = Console::currentInstance->ReadLine() + "\n";
}
if(count > consoleBuf.size())
count = consoleBuf.size();
memcpy(buf, consoleBuf.data(), count);
consoleBuf = consoleBuf.substr(count);
return count;
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
//GrafPort port; //GrafPort port;
@ -83,6 +99,8 @@ int main(int argc, char** argv)
new char[32]; new char[32];
Console console(win, win->portRect); Console console(win, win->portRect);
__write_hook = &consolewrite; __write_hook = &consolewrite;
__read_hook = &consoleread;
console.putch('x'); console.putch('x');
console.putch('a'); console.putch('a');
console.putch('b'); console.putch('b');
@ -109,6 +127,13 @@ int main(int argc, char** argv)
fflush(stdout); fflush(stdout);
printf("You said: %s\n", console.ReadLine().c_str()); printf("You said: %s\n", console.ReadLine().c_str());
char buffer[100];
printf("Say something else: ");
fflush(stdout);
fgets(buffer, 100, stdin);
printf("You said: %s\n", buffer);
for(int i = 0; i < 5; i++) for(int i = 0; i < 5; i++)
{ {
//std::cout << "Exception speed test: " << std::flush; //std::cout << "Exception speed test: " << std::flush;

View File

@ -40,6 +40,7 @@ void _exit(int status)
} }
ssize_t (*__write_hook)(int fd, const void*buf, size_t count) = NULL; ssize_t (*__write_hook)(int fd, const void*buf, size_t count) = NULL;
ssize_t (*__read_hook)(int fd, void*buf, size_t count) = NULL;
ssize_t write(int fd, const void *buf, size_t count) ssize_t write(int fd, const void *buf, size_t count)
{ {
@ -50,6 +51,8 @@ ssize_t write(int fd, const void *buf, size_t count)
ssize_t read(int fd, void *buf, size_t count) ssize_t read(int fd, void *buf, size_t count)
{ {
if(__read_hook)
return (*__read_hook)(fd,buf,count);
return -1; return -1;
} }