How can I read a single line of text that is longer than 1023 characters? #571
-
The |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You can keep reading (call the function again), but I guess the more specific question you'd have is knowing whether you've read all the stdin / line yet right, since there is no flag letting you know if it read a full line. For now you can create your own c function that does what you want, and link to it from Koka: Here is all the definitions from Extern imports, extern functions etc, are not limited to the std core libraries. You can use them in your code. (
static kk_std_core_exn__error kk_os_read_line_error( kk_context_t* ctx ) {
kk_string_t content;
const int err = kk_os_read_line(&content,ctx);
if (err != 0) return kk_error_from_errno(err,ctx);
else return kk_error_ok(kk_string_box(content),ctx);
}
kk_decl_export int kk_os_read_line(kk_string_t* result, kk_context_t* ctx)
{
char buf[1024];
if (fgets(buf, 1023, stdin) == NULL) return errno;
buf[1023] = 0; // ensure zero termination
const size_t len = strlen(buf);
if (len > 0 && buf[len-1] == '\n') {
buf[len-1] = 0; // remove possible ending newline character
}
*result = kk_string_alloc_from_qutf8(buf, ctx);
return 0;
} |
Beta Was this translation helpful? Give feedback.
You can keep reading (call the function again), but I guess the more specific question you'd have is knowing whether you've read all the stdin / line yet right, since there is no flag letting you know if it read a full line.
For now you can create your own c function that does what you want, and link to it from Koka: Here is all the definitions from
std/os/readline.kk
,std/os/readline-inline.c
, and the kklib c libraryos.c
where this all is implemented.Extern imports, extern functions etc, are not limited to the std core libraries. You can use them in your code. (
kklib.h
and all of the imports / types defined in your module are defined prior to yourc
file is spliced in, so you shouldn't…