From: fliep Date: Sun, 13 Jul 2008 16:23:38 +0000 (+0000) Subject: Toevoegen van de sources die ik vorige keer vergeten was, en nog wat X-Git-Tag: v0.9~461 X-Git-Url: https://git.stderr.nl/gitweb?a=commitdiff_plain;h=08b46abcb806c15a053b238e0eaee87a442c940c;p=projects%2Fchimara%2Fchimara.git Toevoegen van de sources die ik vorige keer vergeten was, en nog wat triviale functies geimplementeerd, zoals echo streams van windows. Ik heb ook de andere glk_put_ functies geschreven; de functie met de echte code is nu glk_put_string_stream(), en de anderen formatteren hun input en geven die door aan glk_put_string_stream(). Library functies geimplementeerd: glk_window_get_type() glk_window_get_parent() glk_window_get_sibling() glk_window_get_root() glk_window_clear() glk_window_set_echo_stream() glk_window_get_echo_stream() glk_stream_get_current() glk_put_string_stream() glk_put_char() glk_put_char_stream() glk_put_buffer() glk_put_buffer_stream() --- diff --git a/src/case.c b/src/case.c new file mode 100644 index 0000000..ecb7862 --- /dev/null +++ b/src/case.c @@ -0,0 +1,133 @@ +#include +#include "glk.h" + +/** + * glk_char_to_lower: + * @ch: A Latin-1 character. + * + * If @ch is an uppercase character in the Latin-1 character set, converts it + * to lowercase. Otherwise, leaves it unchanged. + * + * Returns: A lowercase or non-letter Latin-1 character. + */ +unsigned char +glk_char_to_lower(unsigned char ch) +{ + if( (ch >= 0x41 && ch <= 0x5A) || (ch >= 0xC0 && ch <= 0xD6) || (ch >= 0xD8 && ch <= 0xDE) ) + return ch + 0x20; + return ch; +} + +/** + * glk_char_to_upper: + * @ch: A Latin-1 character. + * + * If @ch is a lowercase character in the Latin-1 character set, converts it to + * uppercase. Otherwise, leaves it unchanged. + * + * Returns: An uppercase or non-letter Latin-1 character. + */ +unsigned char +glk_char_to_upper(unsigned char ch) +{ + if( (ch >= 0x61 && ch <= 0x7A) || (ch >= 0xE0 && ch <= 0xF6) || (ch >= 0xF8 && ch <= 0xFE) ) + return ch - 0x20; + return ch; +} + +#ifdef GLK_MODULE_UNICODE + +/** + * glk_buffer_to_lower_case_uni: + * @buf: A character array in UCS-4. + * @len: Available length of @buf. + * @numchars: Number of characters in @buf. + * + * Converts the first @numchars characters of @buf to their lowercase + * equivalents, if there is such a thing. These functions provide two length + * arguments because a string of Unicode characters may expand when its case + * changes. The @len argument is the available length of the buffer; @numchars + * is the number of characters in the buffer initially. (So @numchars must be + * less than or equal to @len. The contents of the buffer after @numchars do + * not affect the operation.) + * + * Returns: The number of characters after conversion. If this is greater than + * @len, the characters in the array will be safely truncated at len, but the + * true count will be returned. (The contents of the buffer after the returned + * count are undefined.) + */ +glui32 +glk_buffer_to_lower_case_uni(glui32 *buf, glui32 len, glui32 numchars) +{ + g_return_val_if_fail(buf != NULL && (len > 0 || numchars > 0), 0); + g_return_val_if_fail(numchars <= len, 0); + + /* GLib has a function that converts _one_ UCS-4 character to _one_ + lowercase UCS-4 character; so apparently we don't have to worry about the + string length changing... */ + glui32 *ptr; + for(ptr = buf; ptr < buf + numchars; ptr++) + *ptr = g_unichar_tolower(*ptr); + + return numchars; +} + +/** + * glk_buffer_to_upper_case_uni: + * @buf: A character array in UCS-4. + * @len: Available length of @buf. + * @numchars: Number of characters in @buf. + * + * Converts the first @numchars characters of @buf to their uppercase + * equivalents, if there is such a thing. See glk_buffer_to_lower_case_uni(). + * + * Returns: The number of characters after conversion. + */ +glui32 +glk_buffer_to_upper_case_uni(glui32 *buf, glui32 len, glui32 numchars) +{ + g_return_val_if_fail(buf != NULL && (len > 0 || numchars > 0), 0); + g_return_val_if_fail(numchars <= len, 0); + + /* GLib has a function that converts _one_ UCS-4 character to _one_ + uppercase UCS-4 character; so apparently we don't have to worry about the + string length changing... */ + glui32 *ptr; + for(ptr = buf; ptr < buf + numchars; ptr++) + *ptr = g_unichar_toupper(*ptr); + + return numchars; +} + +/** + * glk_buffer_to_title_case_uni: + * @buf: A character array in UCS-4. + * @len: Available length of @buf. + * @numchars: Number of characters in @buf. + * @lowerrest: #TRUE if the rest of @buf should be lowercased, #FALSE + * otherwise. + * + * Converts the first character of @buf to uppercase, if there is such a thing. + * See glk_buffer_to_lower_case_uni(). If @lowerrest is #TRUE, then the + * remainder of @buf is lowercased. + * + * Returns: The number of characters after conversion. + */ +glui32 +glk_buffer_to_title_case_uni(glui32 *buf, glui32 len, glui32 numchars, + glui32 lowerrest) +{ + g_return_val_if_fail(buf != NULL && (len > 0 || numchars > 0), 0); + g_return_val_if_fail(numchars <= len, 0); + + /* GLib has a function that converts _one_ UCS-4 character to _one_ + uppercase UCS-4 character; so apparently we don't have to worry about the + string length changing... */ + *buf = g_unichar_totitle(*buf); + /* Call lowercase on the rest of the string */ + if(lowerrest) + return glk_buffer_to_lower_case_uni(buf + 1, len - 1, numchars - 1) +1; + return numchars; +} + +#endif /* GLK_MODULE_UNICODE */ diff --git a/src/fileref.c b/src/fileref.c new file mode 100644 index 0000000..678a9be --- /dev/null +++ b/src/fileref.c @@ -0,0 +1,54 @@ +#include "fileref.h" + +/* List of streams currently in existence */ +static GList *fileref_list = NULL; + +/** + * glk_fileref_iterate: + * @fref: A file reference, or #NULL. + * @rockptr: Return location for the next window's rock, or #NULL. + * + * Iterates over the list of file references; if @fref is #NULL, it returns the + * first file reference, otherwise the next file reference after @fref. If + * there are no more, it returns #NULL. The file reference's rock is stored in + * @rockptr. If you don't want the rocks to be returned, you may set @rockptr + * to #NULL. + * + * The order in which file references are returned is arbitrary. The order may + * change every time you create or destroy a file reference, invalidating the + * iteration. + * + * Returns: the next file reference, or #NULL if there are no more. + */ +frefid_t +glk_fileref_iterate(frefid_t fref, glui32 *rockptr) +{ + GList *retnode; + + if(fref == NULL) + retnode = fileref_list; + else + retnode = fref->fileref_list->next; + frefid_t retval = retnode? (frefid_t)retnode->data : NULL; + + /* Store the fileref's rock in rockptr */ + if(retval && rockptr) + *rockptr = glk_fileref_get_rock(retval); + + return retval; +} + +/** + * glk_fileref_get_rock: + * @fref: A file reference. + * + * Returns the file reference @fref's rock value. + * + * Returns: A rock value. + */ +glui32 +glk_fileref_get_rock(frefid_t fref) +{ + g_return_val_if_fail(fref != NULL, 0); + return fref->rock; +} diff --git a/src/fileref.h b/src/fileref.h new file mode 100644 index 0000000..fb52ee5 --- /dev/null +++ b/src/fileref.h @@ -0,0 +1,19 @@ +#ifndef FILEREF_H +#define FILEREF_H + +#include +#include "glk.h" + +struct glk_fileref_struct +{ + glui32 rock; + /* Pointer to the list node in the global fileref list that contains this + fileref */ + GList* fileref_list; + /* Fileref parameters */ + gchar *filename; + glui32 filemode; + glui32 usage; +}; + +#endif diff --git a/src/gestalt.c b/src/gestalt.c new file mode 100644 index 0000000..1709e12 --- /dev/null +++ b/src/gestalt.c @@ -0,0 +1,63 @@ +#include "glk.h" + +/* Version of the Glk specification implemented by this library */ +#define MAJOR_VERSION 0 +#define MINOR_VERSION 7 +#define SUB_VERSION 0 + +/** + * glk_gestalt: + * @sel: A selector, representing which capability to request information + * about. + * @val: Extra information, depending on the value of @sel. + * + * Calls the gestalt system to request information about selector @sel, without + * passing an array to store extra information in (see glk_gestalt_ext()). + * + * Returns: an integer, depending on what selector was called. + */ +glui32 +glk_gestalt(glui32 sel, glui32 val) +{ + return glk_gestalt_ext(sel, val, NULL, 0); +} + +/** + * glk_gestalt_ext: + * @sel: A selector, representing which capability to request information + * about. + * @val: Extra information, depending on the value of @sel. + * @arr: Location of an array to store extra information in, or #NULL. + * @arrlen: Length of @arr, or 0 if @arr is #NULL. + * + * Calls the gestalt system to request information about selector @sel, + * possibly returning information in @arr. + * + * Returns: an integer, depending on what selector was called. + */ +glui32 +glk_gestalt_ext(glui32 sel, glui32 val, glui32 *arr, glui32 arrlen) +{ + switch(sel) + { + /* Version number */ + case gestalt_Version: + return MAJOR_VERSION << 16 + MINOR_VERSION << 8 + SUB_VERSION; + + /* Which characters can we print? */ + case gestalt_CharOutput: + /* All characters are printed as one character, in any case */ + if(arr && arrlen > 0) + *arr = 1; + /* Cannot print control chars except \n, or chars > 255 */ + if( (val < 32 && val != 10) || (val >= 127 && val <= 159) || (val > 255) ) + return gestalt_CharOutput_CannotPrint; + /* Can print all other Latin-1 characters */ + return gestalt_CharOutput_ExactPrint; + + /* Selector not supported */ + default: + return 0; + } +} + diff --git a/src/stream.c b/src/stream.c index 020443b..583b9a4 100644 --- a/src/stream.c +++ b/src/stream.c @@ -6,6 +6,22 @@ static strid_t current_stream = NULL; /* List of streams currently in existence */ static GList *stream_list = NULL; +/* Internal function: create a window stream to go with window. */ +strid_t +window_stream_new(winid_t window) +{ + /* Create stream and connect it to window */ + strid_t s = g_new0(struct glk_stream_struct, 1); + s->file_mode = filemode_Write; + s->stream_type = STREAM_TYPE_WINDOW; + s->window = window; + /* Add it to the global stream list */ + stream_list = g_list_prepend(stream_list, s); + s->stream_list = stream_list; + + return s; +} + /** * glk_stream_iterate: * @str: A stream, or #NULL. @@ -54,22 +70,6 @@ glk_stream_get_rock(strid_t str) return str->rock; } -/* Internal function: create a window stream to go with window. */ -strid_t -window_stream_new(winid_t window) -{ - /* Create stream and connect it to window */ - strid_t s = g_new0(struct glk_stream_struct, 1); - s->file_mode = filemode_Write; - s->stream_type = STREAM_TYPE_WINDOW; - s->window = window; - /* Add it to the global stream list */ - stream_list = g_list_prepend(stream_list, s); - s->stream_list = stream_list; - - return s; -} - /** * glk_stream_set_current: * @str: An output stream, or NULL. @@ -89,6 +89,82 @@ glk_stream_set_current(strid_t str) current_stream = str; } +/** + * glk_stream_get_current: + * + * Returns the current stream, or #NULL if there is none. + * + * Returns: A stream. + */ +strid_t +glk_stream_get_current() +{ + return current_stream; +} + +/** + * glk_put_char: + * @ch: A character in Latin-1 encoding. + * + * Prints one character @ch to the current stream. + */ +void +glk_put_char(unsigned char ch) +{ + /* Illegal to print to the current stream if it is NULL */ + g_return_if_fail(current_stream != NULL); + glk_put_char_stream(current_stream, ch); +} + +/** + * glk_put_string: + * @s: A null-terminated string in Latin-1 encoding. + * + * Prints @s to the current stream. + */ +void +glk_put_string(char *s) +{ + /* Illegal to print to the current stream if it is NULL */ + g_return_if_fail(current_stream != NULL); + glk_put_string_stream(current_stream, s); +} + +/** + * glk_put_buffer: + * @buf: An array of characters in Latin-1 encoding. + * @len: Length of @buf. + * + * Prints @buf to the current stream. + */ +void +glk_put_buffer(char *buf, glui32 len) +{ + /* Illegal to print to the current stream if it is NULL */ + g_return_if_fail(current_stream != NULL); + glk_put_buffer_stream(current_stream, buf, len); +} + +/** + * glk_put_char_stream: + * @str: An output stream. + * @ch: A character in Latin-1 encoding. + * + * Prints one character @ch to the stream @str. It is illegal for @str to be + * #NULL, or an input-only stream. + */ +void +glk_put_char_stream(strid_t str, unsigned char ch) +{ + g_return_if_fail(str != NULL); + g_return_if_fail(str->file_mode != filemode_Read); + + /* Convert ch to a null-terminated string, call glk_put_string_stream() */ + gchar *s = g_strndup(&ch, 1); + glk_put_string_stream(str, s); + g_free(s); +} + /* Internal function: change illegal (control) characters in a string to a placeholder character. Must free returned string afterwards. */ static gchar * @@ -104,40 +180,57 @@ remove_latin1_control_characters(gchar *s) return retval; } +/* Internal function: convert a Latin-1 string to a UTF-8 string, replacing +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) +{ + GError *error = NULL; + gchar *canonical = remove_latin1_control_characters(s); + utf8 = g_convert(canonical, -1, "UTF-8", "ISO-8859-1", NULL, NULL, &error); + g_free(canonical); + + if(utf8 == NULL) + { + error_dialog(NULL, error, "Error during latin1->utf8 conversion: "); + return NULL; + } + + return utf8; +} + +/* Internal function: write a UTF-8 string to a window's text buffer. */ +static void +write_utf8_to_window(winid_t win, gchar *s) +{ + GtkTextBuffer *buffer = + gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) ); + + GtkTextIter iter; + gtk_text_buffer_get_end_iter(buffer, &iter); + gtk_text_buffer_insert(buffer, &iter, utf8, -1); +} + /** - * glk_put_string: + * glk_put_string_stream: + * @str: An output stream. * @s: A null-terminated string in Latin-1 encoding. * - * Prints @s to the current stream. + * Prints @s to the stream @str. It is illegal for @str to be #NULL, or an + * input-only stream. */ void -glk_put_string(char *s) +glk_put_string_stream(strid_t str, char *s) { - /* Illegal to print to the current stream if it is NULL */ - g_return_if_fail(current_stream != NULL); - - GError *error = NULL; - gchar *canonical, *utf8; + g_return_if_fail(str != NULL); + g_return_if_fail(str->file_mode != filemode_Read); - switch(current_stream->stream_type) + switch(str->stream_type) { case STREAM_TYPE_WINDOW: - canonical = remove_latin1_control_characters(s); - utf8 = g_convert(canonical, -1, "UTF-8", "ISO-8859-1", NULL, NULL, - &error); - g_free(canonical); - - if(utf8 == NULL) - { - g_warning("glk_put_string: " - "Error during latin1->utf8 conversion: %s", - error->message); - g_error_free(error); - return; - } - /* Each window type has a different way of printing to it */ - switch(current_stream->window->window_type) + switch(str->window->window_type) { /* Printing to a these windows' streams does nothing */ case wintype_Blank: @@ -148,25 +241,46 @@ glk_put_string(char *s) /* Text buffer window */ case wintype_TextBuffer: { - GtkTextBuffer *buffer = gtk_text_view_get_buffer( - GTK_TEXT_VIEW(current_stream->window->widget) ); - - GtkTextIter iter; - gtk_text_buffer_get_end_iter(buffer, &iter); - - gtk_text_buffer_insert(buffer, &iter, utf8, -1); - } - current_stream->write_count += strlen(s); + gchar *utf8 = convert_latin1_to_utf8(s); + write_utf8_to_window(str->window, utf8); + g_free(utf8); + } + str->write_count += strlen(s); break; default: g_warning("glk_put_string: " "Writing to this kind of window unsupported."); } - g_free(utf8); + /* Now write the same buffer to the window's echo stream */ + if(str->window->echo_stream != NULL) + glk_put_string_stream(str->window->echo_stream, s); + break; default: g_warning("glk_put_string: " "Writing to this kind of stream unsupported."); } } + +/** + * glk_put_buffer_stream: + * @str: An output stream. + * @buf: An array of characters in Latin-1 encoding. + * @len: Length of @buf. + * + * Prints @buf to the stream @str. It is illegal for @str to be #NULL, or an + * input-only stream. + */ +void +glk_put_buffer_stream(strid_t str, char *buf, glui32 len) +{ + g_return_if_fail(str != NULL); + g_return_if_fail(str->file_mode != filemode_Read); + + /* Convert buf to a null-terminated string, call glk_put_string_stream() */ + gchar *s = g_strndup(buf, len); + glk_put_string_stream(str, s); + g_free(s); +} + diff --git a/src/window.c b/src/window.c index be7c20d..22cb2e3 100644 --- a/src/window.c +++ b/src/window.c @@ -65,6 +65,76 @@ glk_window_get_rock(winid_t win) return win->rock; } +/** + * glk_window_get_type: + * @win: A window. + * + * Returns the window @win's type, one of #wintype_Blank, #wintype_Pair, + * #wintype_TextBuffer, #wintype_TextGrid, or #wintype_Graphics. + * + * Returns: The window's type. + */ +glui32 +glk_window_get_type(winid_t win) +{ + g_return_val_if_fail(win != NULL, 0); + return win->wintype; +} + +/** + * glk_window_get_parent: + * @win: A window. + * + * Returns the window @win's parent window. If @win is the root window, this + * returns #NULL, since the root window has no parent. Remember that the parent + * of every window is a pair window; other window types are always childless. + * + * Returns: A window. + */ +winid_t +glk_window_get_parent(winid_t win) +{ + g_return_val_if_fail(win != NULL, NULL); + /* Value will also be NULL if win is the root window */ + return (winid_t)win->window_node->parent->data; +} + +/** + * glk_window_get_sibling: + * @win: A window. + * + * Returns the other child of the window @win's parent. If @win is the + * root window, this returns #NULL. + * + * Returns: A window, or NULL. + */ +winid_t +glk_window_get_sibling(winid_t win) +{ + g_return_val_if_fail(win != NULL, NULL); + + if(G_NODE_IS_ROOT(win->window_node)) + return NULL; + if(win->window_node->next) + return (winid_t)win->window_node->next; + return (winid_t)win->window_node->prev; +} + +/** + * glk_window_get_root: + * + * Returns the root window. If there are no windows, this returns #NULL. + * + * Returns: A window, or #NULL. + */ +winid_t +glk_window_get_root() +{ + if(root_window == NULL) + return NULL; + return (winid_t)root_window->data; +} + /** * glk_window_open: * @split: The window to split to create the new window. Must be 0 if there @@ -133,7 +203,8 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype, new_window->window_stream = window_stream_new(new_window); new_window->echo_stream = NULL; } - break; + break; + case wintype_TextBuffer: { GtkWidget *scroll_window = gtk_scrolled_window_new(NULL, NULL); @@ -150,6 +221,7 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype, new_window->line_input_buffer_unicode = NULL; } break; + default: g_warning("glk_window_open: unsupported window type"); g_free(new_window); @@ -161,6 +233,39 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype, return new_window; } +/** + * glk_window_clear: + * @win: A window. + * + * Erases the window @win. + */ +void +glk_window_clear(winid_t win) +{ + g_return_if_fail(win != NULL); + + switch(win->wintype) + { + case wintype_Blank: + /* do nothing */ + break; + + case wintype_TextBuffer: + /* delete all text in the window */ + { + GtkTextBuffer *buffer = gtk_text_view_get_buffer( + GTK_TEXT_VIEW(current_stream->window->widget) ); + GtkTextIter start, end; + gtk_text_buffer_get_bounds(buffer, &start, &end); + gtk_text_buffer_delete(buffer, &start, &end); + } + break; + + default: + g_warning("glk_window_clear: unsupported window type"); + } +} + /** * glk_set_window: * @win: A window. @@ -183,6 +288,68 @@ glk_set_window(winid_t win) */ strid_t glk_window_get_stream(winid_t win) { + g_return_val_if_fail(win != NULL, NULL); return win->window_stream; } +/** + * glk_window_set_echo_stream: + * @win: A window. + * @str: A stream to attach to the window, or #NULL. + * + * Attaches the stream @str to @win as a second stream. Any text printed to the + * window is also echoed to this second stream, which is called the window's + * "echo stream." + * + * Effectively, any call to glk_put_char() (or the other output commands) which + * is directed to @win's window stream, is replicated to @win's echo stream. + * This also goes for the style commands such as glk_set_style(). + * + * Note that the echoing is one-way. You can still print text directly to the + * echo stream, and it will go wherever the stream is bound, but it does not + * back up and appear in the window. + * + * It is illegal to set a window's echo stream to be its own window stream, + * which would create an infinite loop. It is similarly illegal to create a + * longer loop (two or more windows echoing to each other.) + * + * You can reset a window to stop echoing by setting @str to #NULL. + */ +void +glk_window_set_echo_stream(winid_t win, strid_t str) +{ + g_return_if_fail(win != NULL); + + /* Test for an infinite loop */ + strid_t next_str; + for(next_str = str; + next_str != NULL && next_str->stream_type == STREAM_TYPE_WINDOW; + next_str = next_str->window->echo_stream) + { + if(next_str == win->window_stream) + { + g_warning("glk_window_set_echo_stream: Infinite loop detected"); + win->echo_stream = NULL; + return; + } + } + + win->echo_stream = str; +} + +/** + * glk_window_get_echo_stream: + * @win: A window. + * + * Returns the echo stream of window @win. If the window has no echo stream (as + * is initially the case) then this returns #NULL. + * + * Returns: A stream, or #NULL. + */ +strid_t +glk_window_get_echo_stream(winid_t win) +{ + g_return_val_if_fail(win != NULL, NULL); + return win->echo_stream; +} +