1 /*
2 * testmain.c: A sample source to show how to use mcpp as a subroutine.
3 * 2006/11 Contributed by Juergen Mueller.
4 * Refer to mcpp-porting.html section 3.12 for compiling mcpp as a subroutine.
5 *
6 * 2007/03 Updated to enable testing of memory buffer output.
7 * 2008/04 The macro MCPP_LIB is no longer necessary.
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <time.h>
13
14 #include "mcpp_lib.h"
15
main(int argc,char * argv[])16 int main(int argc, char *argv[])
17 {
18 int i, j;
19 char ** tmp_argv;
20
21 tmp_argv = (char **) malloc(sizeof(char *) * (argc + 1));
22
23 if (tmp_argv == NULL) {
24 return(0);
25 }
26
27 /*
28 * assume strict order: <options, ...> <files, ...> and
29 * each option and its argument are specified without intervening spaces
30 * such as '-I/dir' or '-otmp.i' (not '-I /dir' nor '-o tmp.i').
31 */
32 for (i = 0; i < argc; ++i) {
33 tmp_argv[i] = argv[i]; /* copy options */
34
35 if ( (*argv[i] != '-')
36 && (*argv[i] != '/')
37 && (i > 0)) {
38 break;
39 }
40 }
41
42 /* this works only if called function does not manipulate pointer array! */
43 /*
44 * Note that mcpp no longer uses freopen() since 2008/04/20, so you can
45 * output multiple input to different files, though this sample output
46 * to stdout, which may be redirected.
47 */
48 for (j = i; i < argc; ++i) {
49 int retval;
50 char * result;
51 clock_t start, finish;
52
53 tmp_argv[j] = argv[i]; /* process each file */
54 fprintf(stderr, "\n%s\n", argv[i]);
55
56 start = clock(); /* get start time */
57 #if OUT2MEM /* Use memory buffer */
58 mcpp_use_mem_buffers( 1); /* enable memory output */
59 retval = mcpp_lib_main(j + 1, tmp_argv); /* call MCPP */
60 result = mcpp_get_mem_buffer( OUT); /* get the output */
61 if (result)
62 fputs( result, stdout);
63 result = mcpp_get_mem_buffer( ERR); /* get the diagnostics */
64 if (result)
65 fputs( result, stderr);
66 #if 0 /* debug output is the same with normal output by default */
67 result = mcpp_get_mem_buffer( DBG); /* get the debug output */
68 if (result)
69 fputs( result, stdout); /* appended to output */
70 #endif
71 #else /* Normal output to file */
72 retval = mcpp_lib_main(j + 1, tmp_argv);
73 #endif
74 finish = clock(); /* get finish time */
75
76 fprintf(stderr, "\nReturned status:%d, Elapsed time: %.3f seconds.\n",
77 retval, (double)(finish - start) / (double)CLOCKS_PER_SEC);
78 }
79
80 free(tmp_argv);
81
82 return(0);
83 }
84