]> git.saurik.com Git - wxWidgets.git/blob - samples/console/console.cpp
Committing in .
[wxWidgets.git] / samples / console / console.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: a sample console (as opposed to GUI) progam using wxWindows
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 04.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include <stdio.h>
21
22 #include <wx/string.h>
23 #include <wx/file.h>
24 #include <wx/app.h>
25
26 // ----------------------------------------------------------------------------
27 // conditional compilation
28 // ----------------------------------------------------------------------------
29
30 // what to test?
31
32 //#define TEST_ARRAYS
33 #define TEST_LOG
34 //#define TEST_THREADS
35
36 // ============================================================================
37 // implementation
38 // ============================================================================
39
40 // ----------------------------------------------------------------------------
41 // threads
42 // ----------------------------------------------------------------------------
43
44 #ifdef TEST_THREADS
45
46 #include <wx/thread.h>
47
48 static size_t gs_counter = (size_t)-1;
49 static wxCriticalSection gs_critsect;
50
51 class MyThread : public wxThread
52 {
53 public:
54 MyThread(char ch);
55
56 // thread execution starts here
57 virtual void *Entry();
58
59 // and stops here
60 virtual void OnExit();
61
62 public:
63 char m_ch;
64 };
65
66 MyThread::MyThread(char ch)
67 {
68 m_ch = ch;
69
70 Create();
71 }
72
73 void *MyThread::Entry()
74 {
75 {
76 wxCriticalSectionLocker lock(gs_critsect);
77 if ( gs_counter == (size_t)-1 )
78 gs_counter = 1;
79 else
80 gs_counter++;
81 }
82
83 for ( size_t n = 0; n < 10; n++ )
84 {
85 if ( TestDestroy() )
86 break;
87
88 putchar(m_ch);
89 fflush(stdout);
90
91 wxThread::Sleep(100);
92 }
93
94 return NULL;
95 }
96
97 void MyThread::OnExit()
98 {
99 wxCriticalSectionLocker lock(gs_critsect);
100 gs_counter--;
101 }
102
103 #endif // TEST_THREADS
104
105 // ----------------------------------------------------------------------------
106 // arrays
107 // ----------------------------------------------------------------------------
108
109 #ifdef TEST_ARRAYS
110
111 void PrintArray(const char* name, const wxArrayString& array)
112 {
113 printf("Dump of the array '%s'\n", name);
114
115 size_t nCount = array.GetCount();
116 for ( size_t n = 0; n < nCount; n++ )
117 {
118 printf("\t%s[%u] = '%s'\n", name, n, array[n].c_str());
119 }
120 }
121
122 #endif // TEST_ARRAYS
123
124 // ----------------------------------------------------------------------------
125 // entry point
126 // ----------------------------------------------------------------------------
127
128 int main(int argc, char **argv)
129 {
130 if ( !wxInitialize() )
131 {
132 fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
133 }
134
135 #ifdef TEST_ARRAYS
136 wxArrayString a1;
137 a1.Add("tiger");
138 a1.Add("cat");
139 a1.Add("lion");
140 a1.Add("dog");
141 a1.Add("human");
142 a1.Add("ape");
143
144 puts("*** Initially:");
145
146 PrintArray("a1", a1);
147
148 wxArrayString a2(a1);
149 PrintArray("a2", a2);
150
151 wxSortedArrayString a3(a1);
152 PrintArray("a3", a3);
153
154 puts("*** After deleting a string from a1");
155 a1.Remove(2);
156
157 PrintArray("a1", a1);
158 PrintArray("a2", a2);
159 PrintArray("a3", a3);
160
161 puts("*** After reassigning a1 to a2 and a3");
162 a3 = a2 = a1;
163 PrintArray("a2", a2);
164 PrintArray("a3", a3);
165 #endif // TEST_ARRAYS
166
167 #ifdef TEST_LOG
168 wxString s;
169 for ( size_t n = 0; n < 8000; n++ )
170 {
171 s << (char)('A' + (n % 26));
172 }
173
174 wxString msg;
175 msg.Printf("A very very long message: '%s', the end!\n", s.c_str());
176
177 // this one shouldn't be truncated
178 printf(msg);
179
180 // but this one will because log functions use fixed size buffer
181 wxLogMessage("A very very long message 2: '%s', the end!\n", s.c_str());
182 #endif // TEST_LOG
183
184 #ifdef TEST_THREADS
185 static const size_t nThreads = 3;
186 MyThread *threads[nThreads];
187 size_t n;
188 for ( n = 0; n < nThreads; n++ )
189 {
190 threads[n] = new MyThread('+' + n);
191 threads[n]->Run();
192 }
193
194 // wait until all threads terminate
195 for ( ;; )
196 {
197 wxCriticalSectionLocker lock(gs_critsect);
198 if ( !gs_counter )
199 break;
200 }
201
202 puts("\nThat's all, folks!");
203
204 for ( n = 0; n < nThreads; n++ )
205 {
206 threads[n]->Delete();
207 }
208 #endif // TEST_THREADS
209
210 wxUninitialize();
211
212 return 0;
213 }