forked from BOINC/boinc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupper_case.C
executable file
·300 lines (246 loc) · 7.83 KB
/
upper_case.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// The contents of this file are subject to the BOINC Public License
// Version 1.0 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://boinc.berkeley.edu/license_1.0.txt
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations
// under the License.
//
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
//
// The Initial Developer of the Original Code is the SETI@home project.
// Portions created by the SETI@home project are Copyright (C) 2002
// University of California at Berkeley. All Rights Reserved.
//
// Contributor(s):
//
// read "in", convert to UC, write to "out"
// command line options:
// -run_slow: sleep 1 second after each character, useful for debugging
// -cpu_time: chew up some CPU cycles after each character,
// -signal: raise SIGHUP signal (for testing signal handler)
// -exit: exit with status -10 (for testing exit handler)
//
#ifdef _WIN32
#include "stdafx.h"
#endif
#ifndef _WIN32
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#endif
#ifdef BOINC_APP_GRAPHICS
#ifdef __APPLE_CC__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include "mac_carbon_gl.h"
#elif _WIN32
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The Glaux Library
#elif HAVE_GL_LIB
#include <GL/glx.h>
#include <GL/gl.h>
#include <GL/glu.h>
#endif
#include "graphics_api.h"
#endif
#include "diagnostics.h"
#include "util.h"
#include "filesys.h"
#include "boinc_api.h"
#include "mfile.h"
#define CHECKPOINT_FILE "upper_case_state"
#ifdef BOINC_APP_GRAPHICS
char display_buf[10];
double xPos=0, yPos=0;
double xDelta=0.03, yDelta=0.07;
#endif
bool run_slow=false, raise_signal=false, random_exit=false;
bool cpu_time=false;
time_t my_start_time;
APP_INIT_DATA uc_aid;
int do_checkpoint(MFILE& mf, int nchars) {
int retval;
string resolved_name;
FILE* f = fopen("temp", "w");
if (!f) return 1;
fprintf(f, "%d", nchars);
fclose(f);
fprintf(stderr, "APP: upper_case checkpointing\n");
retval = mf.flush();
if (retval) return retval;
boinc_resolve_filename_s(CHECKPOINT_FILE, resolved_name);
retval = boinc_rename("temp", resolved_name.c_str());
if (retval) return retval;
return 0;
}
#ifdef _WIN32
extern int main(int argc, char** argv);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode) {
LPSTR command_line;
char* argv[100];
int argc;
command_line = GetCommandLine();
argc = parse_command_line( command_line, argv );
return main(argc, argv);
}
#endif
int main(int argc, char **argv) {
int c, nchars = 0, retval, i, n;
double j, fsize;
char resolved_name[512];
MFILE out;
FILE* state, *in;
bool standalone = false;
int flags = 0;
my_start_time = time(0);
flags =
BOINC_DIAG_DUMPCALLSTACKENABLED |
BOINC_DIAG_HEAPCHECKENABLED |
BOINC_DIAG_REDIRECTSTDERR;
if (standalone)
flags |= BOINC_DIAG_TRACETOSTDERR;
boinc_init_diagnostics(flags);
// NOTE: if you change output here, remember to change the output that
// test_uc.py pattern-matches against.
for (i=0; i<argc; i++) {
fprintf(stderr, "APP: upper_case: argv[%d] is %s\n", i, argv[i]);
if (!strcmp(argv[i], "-run_slow")) run_slow = true;
if (!strcmp(argv[i], "-cpu_time")) cpu_time = 1;
if (!strcmp(argv[i], "-signal")) raise_signal = true;
if (!strcmp(argv[i], "-exit")) random_exit = true;
if (!strcmp(argv[i], "-standalone")) standalone = true;
}
retval = boinc_init(standalone);
if (retval) exit(retval);
// can't write to stderr until after boinc_init()
//
for (i=0; i<argc; i++) {
fprintf(stderr, "APP: upper_case: argv[%d] is %s\n", i, argv[i]);
}
#ifdef BOINC_APP_GRAPHICS
strcpy(display_buf, "(none)\0");
retval = boinc_init_opengl();
if (retval) exit(retval);
#endif
boinc_get_init_data(uc_aid);
// fprintf(stderr,
// "<app prefs>\n%s\n</app_prefs>\n", uc_aid.app_preferences
// );
fprintf(stderr, "APP: upper_case: starting, argc %d\n", argc);
boinc_resolve_filename("in", resolved_name, sizeof(resolved_name));
in = fopen(resolved_name, "r");
if (in == NULL) {
fprintf(
stderr,
"Couldn't find input file, resolved name %s.\n",
resolved_name
);
exit(-1);
}
file_size(resolved_name, fsize);
boinc_resolve_filename(CHECKPOINT_FILE, resolved_name, sizeof(resolved_name));
state = fopen(resolved_name, "r");
if (state) {
fscanf(state, "%d", &nchars);
printf("nchars %d\n", nchars);
fseek(in, nchars, SEEK_SET);
boinc_resolve_filename("out", resolved_name, sizeof(resolved_name));
retval = out.open(resolved_name, "a");
} else {
boinc_resolve_filename("out", resolved_name, sizeof(resolved_name));
retval = out.open(resolved_name, "w");
}
if (retval) {
fprintf(stderr, "APP: upper_case output open failed:\n");
fprintf(stderr, "resolved name %s, retval %d\n", resolved_name, retval);
perror("open");
exit(1);
}
while (1) {
c = fgetc(in);
if (c == EOF) break;
#ifdef BOINC_APP_GRAPHICS
sprintf(display_buf, "%c -> %c", c, toupper(c));
#endif
c = toupper(c);
out._putchar(c);
nchars++;
if (cpu_time) {
n = 0;
j = 3.14159;
for(i=0; i<20000000; i++) {
n++;
j *= n+j-3.14159;
j /= (float)n;
}
if (n==j) n = 0;
}
if (run_slow) {
boinc_sleep(1.);
}
#ifdef HAVE_SIGNAL_H
if (raise_signal) {
raise(SIGHUP);
}
#endif
if (random_exit) {
if (drand() < 0.05) {
exit(-10);
}
}
bool flag = boinc_time_to_checkpoint();
if (flag) {
retval = do_checkpoint(out, nchars);
if (retval) {
fprintf(stderr, "APP: upper_case checkpoint failed %d\n", retval);
exit(1);
}
boinc_checkpoint_completed();
}
boinc_fraction_done(nchars/fsize);
}
retval = out.flush();
if (retval) {
fprintf(stderr, "APP: upper_case flush failed %d\n", retval);
exit(1);
}
if (random_exit) exit(-10);
fprintf(stderr, "APP: upper_case ending, wrote %d chars\n", nchars);
double cur_cpu;
boinc_wu_cpu_time(cur_cpu);
#ifdef BOINC_APP_GRAPHICS
boinc_finish_opengl();
#endif
boinc_finish(0);
return 0;
}
#ifdef BOINC_APP_GRAPHICS
extern GLuint main_font;
void app_init_gl() {}
bool app_render(int xs, int ys, double time_of_day) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glColor3f(1,1,1);
glRasterPos2f(xPos, yPos);
glPrint(main_font, display_buf);
xPos += xDelta;
yPos += yDelta;
if (xPos < -1 || xPos > 1) xDelta *= -1;
if (yPos < -1 || yPos > 1) yDelta *= -1;
glRasterPos2f(-0.9, 0.9);
glPrint(main_font, "User: %s", uc_aid.user_name);
glRasterPos2f(-0.9, 0.8);
glPrint(main_font, "Team: %s", uc_aid.team_name);
glRasterPos2f(-0.9, 0.7);
glPrint(main_font, "CPU Time: %f", uc_aid.wu_cpu_time);
return true; // Everything Went OK
}
#endif