9905800e34f63da2fd9bd1be4e04bf54a5b91682
[polyglot.git] / gui.cpp
1 // gui.cpp
2
3 // includes
4
5 #include <cstdarg>
6
7 #include "gui.h"
8 #include "main.h"
9
10 // constants
11
12 static const int StringSize = 4096;
13
14 // variables
15
16 gui_t GUI[1];
17
18 // functions
19
20
21 // gui_init()
22
23 void gui_init(gui_t *gui){
24     #ifdef _WIN32
25    (gui->pipeStdin).Open();
26 #else
27    
28    gui->io->in_fd = STDIN_FILENO;
29    gui->io->out_fd = STDOUT_FILENO;
30    gui->io->name = "GUI";
31
32    io_init(gui->io);
33 #endif
34 }
35
36
37 // gui_get_non_blocking()
38
39 // this is only non_blocking on windows!
40
41 bool gui_get_non_blocking(gui_t * gui, char string[], int size) {
42
43    ASSERT(gui!=NULL);
44    ASSERT(string!=NULL);
45    ASSERT(size>=256);
46 #ifndef _WIN32
47    if (!io_get_line(gui->io,string,size)) { // EOF
48       my_log("POLYGLOT *** EOF from GUI ***\n");
49       quit();
50    }
51    return true;
52 #else
53    if ((gui->pipeStdin).LineInput(string)) {
54        my_log("GUI->Adapter: %s\n", string);
55        return true;
56    } else {
57         string[0]='\0';
58         return false;
59    }
60    
61 #endif
62 }
63
64 // gui_get()
65
66 void gui_get(gui_t * gui, char string[], int size) {
67     bool data_available;
68     while(true){
69         data_available=gui_get_non_blocking(gui, string, size);
70         if(!data_available){
71             Idle();
72         }else{
73             break;
74         }
75     }
76 }
77
78
79 // gui_send()
80
81 void gui_send(gui_t * gui, const char format[], ...) {
82
83    va_list arg_list;
84    char string[StringSize];
85
86    ASSERT(gui!=NULL);
87    ASSERT(format!=NULL);
88
89    // format
90
91    va_start(arg_list,format);
92    vsprintf(string,format,arg_list);
93    va_end(arg_list);
94
95    // send
96
97 #ifndef _WIN32
98    io_send(gui->io,"%s",string);
99 #else
100    printf("%s\n",string);
101    fflush(stdout);
102    my_log("Adapter->GUI: %s\n",string);
103 #endif
104 }
105