- Fixed a bug that made Nitfol crash since [130]
[rodin/chimara.git] / tests / model.c
1 #include <stdio.h>
2 #include <libchimara/glk.h>
3
4 static winid_t mainwin = NULL;
5
6 void sayit(void)
7 {
8         fprintf(stderr, "I'm the interrupt handler!\n");
9 }
10
11 void glk_main(void)
12 {
13     /* Open the main window. */
14     mainwin = glk_window_open(0, 0, 0, wintype_TextBuffer, 1);
15     if (!mainwin) {
16         /* It's possible that the main window failed to open. There's
17             nothing we can do without it, so exit. */
18         return; 
19     }
20         
21     glui32 buffer[1024];
22     int i;
23     for(i = 0; i < 512; i++) {
24         buffer[i * 2] = i + 33;
25                 buffer[i * 2 + 1] = 32;
26         }
27     
28 /*    frefid_t f = glk_fileref_create_temp(fileusage_BinaryMode, 0);
29     if(f) 
30     {
31                 strid_t s = glk_stream_open_file(f, filemode_ReadWrite, 0);*/
32                 glui32 membuf[512];
33                 strid_t s = glk_stream_open_memory_uni(membuf, 512, filemode_ReadWrite, 0);
34                 glk_stream_set_current(s);
35                 
36                 glk_put_char_uni('X');
37                 glk_put_string("Philip en Marijn zijn vet goed.\n");
38                 glk_put_buffer_uni(buffer, 1024);
39
40                 glk_stream_set_position(s, 0, seekmode_Start);
41                 glk_set_window(mainwin);
42                 glk_put_char_uni( glk_get_char_stream_uni(s) );
43                 glk_put_char('\n');
44                 printf("Line read: %d\n", glk_get_line_stream_uni(s, buffer, 1024) );
45                 printf("string[5] = %X\n", buffer[5]);
46                 glk_put_string_uni(buffer);
47                 int count = glk_get_buffer_stream_uni(s, buffer, 1024);
48                 printf("Buffer read: %d\n", count);
49                 glk_put_string("\n---SOME CHARACTERS---\n");
50                 glk_put_buffer_uni(buffer, count);
51                 glk_put_string("\n---THE SAME CHARACTERS IN UPPERCASE---\n");
52                 int newcount = glk_buffer_to_upper_case_uni(buffer, 1024, 1024);
53                 glk_put_buffer_uni(buffer, newcount);
54                 
55                 stream_result_t result;
56                 glk_stream_close(s, &result);
57                 
58                 fprintf(stderr, "Read count: %d\nWrite count: %d\n", result.readcount, result.writecount);
59 /*              glk_fileref_destroy(f);
60         }*/
61
62         glk_set_interrupt_handler(&sayit);
63
64         event_t ev;
65         while(1) {
66                 glk_put_string("\nprompt> ");
67                 glk_request_line_event_uni(mainwin, buffer, 1024, 0);
68                 glk_select(&ev);
69                 switch(ev.type) {
70                         default:
71                                 printf("Received event:\n");
72                                 printf("Type: %d\n", ev.type);
73                                 printf("Win: %d\n", glk_window_get_rock(ev.win) );
74                                 printf("Var1: %d\n", ev.val1);
75                                 printf("Var2: %d\n", ev.val2);
76                 }
77         }
78         
79         /* Bye bye */
80         glk_exit();
81 }