8c01f4fc6a3d9cc6f41b65746dc709b63697e498
[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 <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "../pp/sl_pp_public.h"
32
33
34 int
35 main(int argc,
36 char *argv[])
37 {
38 FILE *in;
39 long size;
40 char *inbuf;
41 struct sl_pp_purify_options options;
42 char *outbuf;
43 char errmsg[100] = "";
44 unsigned int errline = 0;
45 FILE *out;
46
47 if (argc != 3) {
48 return 1;
49 }
50
51 in = fopen(argv[1], "rb");
52 if (!in) {
53 return 1;
54 }
55
56 fseek(in, 0, SEEK_END);
57 size = ftell(in);
58 fseek(in, 0, SEEK_SET);
59
60 out = fopen(argv[2], "wb");
61 if (!out) {
62 fclose(in);
63 return 1;
64 }
65
66 inbuf = malloc(size + 1);
67 if (!inbuf) {
68 fprintf(out, "$OOMERROR\n");
69
70 fclose(out);
71 fclose(in);
72 return 1;
73 }
74
75 if (fread(inbuf, 1, size, in) != size) {
76 fprintf(out, "$READERROR\n");
77
78 free(inbuf);
79 fclose(out);
80 fclose(in);
81 return 1;
82 }
83 inbuf[size] = '\0';
84
85 fclose(in);
86
87 memset(&options, 0, sizeof(options));
88
89 if (sl_pp_purify(inbuf, &options, &outbuf, errmsg, sizeof(errmsg), &errline)) {
90 fprintf(out, "$PURIFYERROR %u: %s\n", errline, errmsg);
91
92 free(inbuf);
93 fclose(out);
94 return 1;
95 }
96
97 free(inbuf);
98
99 fwrite(outbuf, 1, strlen(outbuf), out);
100
101 free(outbuf);
102 fclose(out);
103
104 return 0;
105 }