e3fca59ab450a6b06611e366103cba5e8ec21753
[mesa.git] / src / glsl / apps / purify.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <assert.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include "../pp/sl_pp_public.h"
33 #include "../pp/sl_pp_purify.h"
34
35
36 int
37 main(int argc,
38 char *argv[])
39 {
40 FILE *in;
41 long size;
42 char *inbuf;
43 struct sl_pp_purify_options options;
44 char *outbuf;
45 char errmsg[100] = "";
46 unsigned int errline = 0;
47 FILE *out;
48
49 if (argc != 3) {
50 printf("Usage: purify infile outfile\n");
51 return 1;
52 }
53
54 in = fopen(argv[1], "rb");
55 if (!in) {
56 return 1;
57 }
58
59 fseek(in, 0, SEEK_END);
60 size = ftell(in);
61 assert(size != -1);
62 if (size == -1) {
63 return 1;
64 }
65 fseek(in, 0, SEEK_SET);
66
67 out = fopen(argv[2], "wb");
68 if (!out) {
69 fclose(in);
70 return 1;
71 }
72
73 inbuf = malloc(size + 1);
74 if (!inbuf) {
75 fprintf(out, "$OOMERROR\n");
76
77 fclose(out);
78 fclose(in);
79 return 1;
80 }
81
82 if (fread(inbuf, 1, size, in) != size) {
83 fprintf(out, "$READERROR\n");
84
85 free(inbuf);
86 fclose(out);
87 fclose(in);
88 return 1;
89 }
90 inbuf[size] = '\0';
91
92 fclose(in);
93
94 memset(&options, 0, sizeof(options));
95
96 if (sl_pp_purify(inbuf, &options, &outbuf, errmsg, sizeof(errmsg), &errline)) {
97 fprintf(out, "$PURIFYERROR %u: %s\n", errline, errmsg);
98
99 free(inbuf);
100 fclose(out);
101 return 1;
102 }
103
104 free(inbuf);
105
106 fwrite(outbuf, 1, strlen(outbuf), out);
107
108 free(outbuf);
109 fclose(out);
110
111 return 0;
112 }