return;
}
- /* Set the current output stream to print to it. */
- glk_set_window(mainwin);
-
- unsigned char buffer[256];
+ char buffer[256];
int i;
for(i = 0; i < 256; i++)
- buffer[i] = glk_char_to_upper(i);
+ buffer[i] = (char)glk_char_to_upper(i);
+
+ /*frefid_t f = glk_fileref_create_temp(fileusage_BinaryMode, 0);
+ if(f)
+ {*/
- glk_put_string("Philip en Marijn zijn vet goed.\n");
- glk_put_buffer((gchar *)buffer, 256);
+ char memorybuffer[100];
- frefid_t f =
- glk_fileref_create_by_prompt(fileusage_TextMode, filemode_Write, 0);
- if(f) {
- if( glk_fileref_does_file_exist(f) )
- glk_put_string("\n\nFile exists!\n");
- else
- glk_put_string("\n\nFile does not exist!\n");
+ strid_t s = glk_stream_open_memory(memorybuffer, 100,
+ filemode_ReadWrite, 0);
+ glk_stream_set_current(s);
+ glk_put_char('X');
+ glk_put_string("Philip en Marijn zijn vet goed.\n");
+ glk_put_buffer(buffer, 256);
+
+ glk_stream_set_position(s, 0, seekmode_Start);
+ glk_set_window(mainwin);
+ glk_put_char( glk_get_char_stream(s) );
+ glk_put_char('\n');
+ g_printerr("Line read: %d\n", glk_get_line_stream(s, buffer, 256));
+ glk_put_string(buffer);
+ int count = glk_get_buffer_stream(s, buffer, 256);
+ g_printerr("Buffer read: %d\n", count);
+ glk_put_buffer(buffer, count);
+
+ stream_result_t result;
+ glk_stream_close(s, &result);
+
+ g_printerr("Read count: %d\nWrite count: %d\n", result.readcount,
+ result.writecount);
+/*
glk_fileref_destroy(f);
- } else
- glk_put_string("\n\nCancel was clicked!\n");
+ }*/
/* Bye bye */
glk_exit();
void
glk_stream_set_current(strid_t str)
{
- if(str != NULL && str->file_mode != filemode_Write)
+ if(str != NULL && str->file_mode == filemode_Read)
{
g_warning("glk_stream_set_current: "
"Cannot set current stream to non output stream");
g_return_val_if_fail(fileref != NULL, NULL);
gchar *modestr;
- gboolean binary = fileref->usage & fileusage_BinaryMode;
+ /* Binary mode is 0x000, text mode 0x100 */
+ gboolean binary = !(fileref->usage & fileusage_TextMode);
switch(fmode)
{
case filemode_Read:
/* Internal function: change illegal (control) characters in a string to a
placeholder character. Must free returned string afterwards. */
static gchar *
-remove_latin1_control_characters(unsigned char *s, gssize len)
+remove_latin1_control_characters(unsigned char *s, gsize len)
{
+ /* If len == 0, then return an empty string, not NULL */
+ if(len == 0)
+ return g_strdup("");
+
gchar *retval = g_new0(gchar, len);
int i;
for(i = 0; i < len; i++)
Latin-1 control characters by a placeholder first. The UTF-8 string must be
freed afterwards. Returns NULL on error. */
static gchar *
-convert_latin1_to_utf8(gchar *s, gssize len)
+convert_latin1_to_utf8(gchar *s, gsize len)
{
GError *error = NULL;
gchar *utf8;
case wintype_TextBuffer:
{
gchar *utf8 = convert_latin1_to_utf8(buf, len);
- write_utf8_to_window(str->window, utf8);
- g_free(utf8);
+ if(utf8)
+ {
+ write_utf8_to_window(str->window, utf8);
+ g_free(utf8);
+ }
}
str->write_count += len;
break;
{
int foo = 0;
while(str->mark < str->buflen && foo < len)
- str->ubuffer[str->mark++] = (glui32)buf[foo++];
+ str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
}
if(!str->unicode && str->buffer)
{
- memmove(str->buffer + str->mark, buf,
- min(len, str->buflen - str->mark));
+ int copycount = min(len, str->buflen - str->mark);
+ memmove(str->buffer + str->mark, buf, copycount);
+ str->mark += copycount;
}
str->write_count += len;
if(ch == EOF)
return -1;
readbuffer[foo] = (gchar)ch;
- charresult = g_utf8_get_char_validated(readbuffer, foo);
+ charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
/* charresult is -1 if invalid, -2 if incomplete, and the unicode code
point otherwise */
}
if(str->buffer) /* if not, copycount stays 0 */
copycount = min(len, str->buflen - str->mark);
memmove(buf, str->buffer + str->mark, copycount);
+ str->mark += copycount;
}
str->read_count += copycount;
else
{
if(str->buffer) /* if not, copycount stays 0 */
- copycount = min(len, str->buflen - str->mark);
- memccpy(buf, str->buffer + str->mark, '\n', copycount);
+ copycount = min(len - 1, str->buflen - str->mark);
+ char *endptr = memccpy(buf, str->buffer + str->mark, '\n',
+ copycount);
+ if(endptr) /* newline was found */
+ copycount = endptr - buf; /* Real copy count */
+ buf[copycount] = '\0';
+ str->mark += copycount;
}
str->read_count += copycount;