Initialize Tizen 2.3
[framework/graphics/cairo.git] / util / cairo-script / csi-bind.c
1 #include <cairo.h>
2 #include <cairo-script-interpreter.h>
3
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 static cairo_status_t
8 write_func (void *closure,
9             const unsigned char *data,
10             unsigned int length)
11 {
12     if (fwrite (data, length, 1, closure) != 1)
13         return CAIRO_STATUS_WRITE_ERROR;
14
15     return CAIRO_STATUS_SUCCESS;
16 }
17
18 int
19 main (int argc, char **argv)
20 {
21     FILE *in = stdin, *out = stdout;
22     cairo_status_t status;
23     int i;
24
25     if (argc >= 3) {
26         if (strcmp (argv[argc-1], "-")) {
27             out = fopen (argv[argc-1], "w");
28             if (out == NULL) {
29                 fprintf (stderr, "Failed to open output '%s'\n", argv[argc-1]);
30                 return 1;
31             }
32         }
33     }
34
35     if (argc > 2) {
36         for (i = 1; i < argc - 1; i++) {
37             in = fopen (argv[i], "r");
38             if (in == NULL) {
39                 fprintf (stderr, "Failed to open input '%s'\n", argv[i]);
40                 return 1;
41             }
42
43             status = cairo_script_interpreter_translate_stream (in, write_func, out);
44             fclose (in);
45
46             if (status)
47                 break;
48         }
49     } else {
50         if (argc > 1) {
51             if (strcmp (argv[1], "-")) {
52                 in = fopen (argv[1], "r");
53                 if (in == NULL) {
54                     fprintf (stderr, "Failed to open input '%s'\n", argv[1]);
55                     return 1;
56                 }
57             }
58         }
59
60         status = cairo_script_interpreter_translate_stream (in, write_func, out);
61
62         if (in != stdin)
63             fclose (in);
64     }
65
66     if (out != stdout)
67         fclose (out);
68
69     if (status) {
70         fprintf (stderr, "Translation failed: %s\n",
71                 cairo_status_to_string (status));
72         return status;
73     }
74
75     return status;
76 }