Skip to content

Latest commit

 

History

History
160 lines (139 loc) · 4 KB

preprocess.c

File metadata and controls

160 lines (139 loc) · 4 KB
 
1
2
3
4
5
6
7
8
9
10
11
/**
* MojoShader; generate shader programs from bytecode of compiled
* Direct3D shaders.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#include <stdio.h>
#include <stdlib.h>
Feb 19, 2009
Feb 19, 2009
12
#include <string.h>
13
14
#include "mojoshader.h"
Feb 12, 2009
Feb 12, 2009
15
static int preprocess(const char *fname, const char *buf, int len,
Feb 19, 2009
Feb 19, 2009
16
17
18
const char *outfile,
const MOJOSHADER_preprocessorDefine *defs,
unsigned int defcount)
Feb 19, 2009
Feb 19, 2009
20
FILE *io = outfile ? fopen(outfile, "wb") : stdout;
21
22
23
24
25
26
27
28
29
if (io == NULL)
{
printf(" ... fopen('%s') failed.\n", outfile);
return 0;
} // if
const MOJOSHADER_preprocessData *pd;
int retval = 0;
Feb 19, 2009
Feb 19, 2009
30
pd = MOJOSHADER_preprocess(fname, buf, len, defs, defcount, NULL,
31
32
33
34
35
36
37
NULL, NULL, NULL, NULL);
if (pd->error_count > 0)
{
int i;
for (i = 0; i < pd->error_count; i++)
{
Feb 12, 2009
Feb 12, 2009
38
39
printf("%s:%d: ERROR: %s\n",
pd->errors[i].filename ? pd->errors[i].filename : "???",
40
41
42
43
44
45
46
47
48
49
pd->errors[i].error_position,
pd->errors[i].error);
} // for
} // if
else
{
if (pd->output != NULL)
{
if (fwrite(pd->output, pd->output_len, 1, io) != 1)
printf(" ... fwrite('%s') failed.\n", outfile);
Feb 19, 2009
Feb 19, 2009
50
else if ((outfile != NULL) && (fclose(io) == EOF))
51
52
53
54
55
56
57
58
printf(" ... fclose('%s') failed.\n", outfile);
else
retval = 1;
} // if
} // else
MOJOSHADER_freePreprocessData(pd);
return retval;
Feb 10, 2009
Feb 10, 2009
59
} // preprocess
60
61
62
63
64
int main(int argc, char **argv)
{
int retval = 1;
Feb 19, 2009
Feb 19, 2009
65
66
67
const char *infile = NULL;
const char *outfile = NULL;
int i;
Feb 19, 2009
Feb 19, 2009
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
MOJOSHADER_preprocessorDefine *defs = NULL;
unsigned int defcount = 0;
for (i = 1; i < argc; i++)
{
const char *arg = argv[i];
if (strcmp(arg, "-o") == 0)
{
if (outfile != NULL)
{
printf("multiple output files specified.\n");
exit(1);
} // if
arg = argv[++i];
if (arg == NULL)
{
printf("no filename after '-o'\n");
exit(1);
} // if
outfile = arg;
} // if
else if (strncmp(arg, "-D", 2) == 0)
{
arg += 2;
char *ident = strdup(arg);
char *ptr = strchr(ident, '=');
const char *val = "";
if (ptr)
{
*ptr = '\0';
val = ptr+1;
} // if
defs = (MOJOSHADER_preprocessorDefine *) realloc(defs,
(defcount+1) * sizeof (MOJOSHADER_preprocessorDefine));
defs[defcount].identifier = ident;
defs[defcount].definition = val;
defcount++;
} // else if
else
{
if (infile != NULL)
{
printf("multiple input files specified.\n");
exit(1);
} // if
infile = arg;
} // else
} // for
if (infile == NULL)
{
printf("no input file specified.\n");
exit(1);
} // if
FILE *io = fopen(infile, "rb");
if (io == NULL)
printf(" ... fopen('%s') failed.\n", infile);
132
133
else
{
Feb 19, 2009
Feb 19, 2009
134
135
136
137
138
char *buf = (char *) malloc(1000000);
int rc = fread(buf, 1, 1000000, io);
fclose(io);
if (rc == EOF)
printf(" ... fread('%s') failed.\n", infile);
139
140
else
{
Feb 19, 2009
Feb 19, 2009
141
142
if (preprocess(infile, buf, rc, outfile, defs, defcount))
retval = 0;
143
144
else
{
Feb 19, 2009
Feb 19, 2009
145
if (outfile != NULL)
146
147
remove(outfile);
} // else
Feb 19, 2009
Feb 19, 2009
148
149
free(buf);
} // else
150
151
} // else
Feb 19, 2009
Feb 19, 2009
152
153
154
155
for (i = 0; i < defcount; i++)
free((void *) defs[defcount].identifier);
free(defs);
156
157
158
return retval;
} // main
Feb 10, 2009
Feb 10, 2009
159
// end of preprocess.c ...