mrubyを超漢字で動作させる
Révision | 85bb1f92ef74a85488700c556b60e6ac7933e822 (tree) |
---|---|
l'heure | 2014-07-12 20:36:44 |
Auteur | cremno <cremno@mail...> |
Commiter | Yukihiro "Matz" Matsumoto |
rewrite stripping
Previous version ignored some errors, and didn't free memory and close files.
Now no memory will be dynamically allocated to simplify error handling.
This commit also fixes a wrong check:
Improve error messages a bit and add missing newline to one.
@@ -6,6 +6,9 @@ | ||
6 | 6 | #include "mruby/dump.h" |
7 | 7 | |
8 | 8 | struct strip_args { |
9 | + int argc_start; | |
10 | + int argc; | |
11 | + char **argv; | |
9 | 12 | mrb_bool lvar; |
10 | 13 | }; |
11 | 14 |
@@ -64,14 +67,65 @@ parse_args(int argc, char **argv, struct strip_args *args) | ||
64 | 67 | return i; |
65 | 68 | } |
66 | 69 | |
70 | +static int | |
71 | +strip(mrb_state *mrb, struct strip_args *args) | |
72 | +{ | |
73 | + int i; | |
74 | + | |
75 | + for (i = args->argc_start; i < args->argc; ++i) { | |
76 | + char *filename; | |
77 | + FILE *rfile; | |
78 | + mrb_irep *irep; | |
79 | + FILE *wfile; | |
80 | + int dump_result; | |
81 | + | |
82 | + filename = args->argv[i]; | |
83 | + rfile = fopen(filename, "rb"); | |
84 | + if (rfile == NULL) { | |
85 | + fprintf(stderr, "can't open file for reading %s\n", filename); | |
86 | + return EXIT_FAILURE; | |
87 | + } | |
88 | + | |
89 | + irep = mrb_read_irep_file(mrb, rfile); | |
90 | + fclose(rfile); | |
91 | + if (irep == NULL) { | |
92 | + fprintf(stderr, "can't read irep file %s\n", filename); | |
93 | + return EXIT_FAILURE; | |
94 | + } | |
95 | + | |
96 | + /* clear lv if --lvar is enabled */ | |
97 | + if (args->lvar) { | |
98 | + irep_remove_lv(mrb, irep); | |
99 | + } | |
100 | + | |
101 | + wfile = fopen(filename, "wb"); | |
102 | + if (wfile == NULL) { | |
103 | + fprintf(stderr, "can't open file for writing %s\n", filename); | |
104 | + mrb_irep_decref(mrb, irep); | |
105 | + return EXIT_FAILURE; | |
106 | + } | |
107 | + | |
108 | + /* debug flag must always be false */ | |
109 | + dump_result = mrb_dump_irep_binary(mrb, irep, FALSE, wfile); | |
110 | + | |
111 | + fclose(wfile); | |
112 | + mrb_irep_decref(mrb, irep); | |
113 | + | |
114 | + if (dump_result != MRB_DUMP_OK) { | |
115 | + fprintf(stderr, "error occurred during dumping %s\n", filename); | |
116 | + return EXIT_FAILURE; | |
117 | + } | |
118 | + } | |
119 | + return EXIT_SUCCESS; | |
120 | +} | |
121 | + | |
67 | 122 | int |
68 | 123 | main(int argc, char **argv) |
69 | 124 | { |
70 | 125 | struct strip_args args; |
71 | - int args_result, i, dump_result; | |
72 | - FILE **files; | |
73 | - mrb_irep **ireps; | |
126 | + int args_result; | |
74 | 127 | mrb_state *mrb; |
128 | + int ret; | |
75 | 129 | |
76 | 130 | if (argc <= 1) { |
77 | 131 | printf("no files to strip\n"); |
@@ -85,15 +139,9 @@ main(int argc, char **argv) | ||
85 | 139 | return EXIT_FAILURE; |
86 | 140 | } |
87 | 141 | |
88 | - files = (FILE**)malloc(sizeof(FILE*) * argc); | |
89 | - for (i = args_result; i < argc; ++i) { | |
90 | - files[i] = fopen(argv[i], "rb"); | |
91 | - | |
92 | - if (!files[i]) { | |
93 | - fprintf(stderr, "can't open file %s\n", argv[i]); | |
94 | - return EXIT_FAILURE; | |
95 | - } | |
96 | - } | |
142 | + args.argc_start = args_result; | |
143 | + args.argc = argc; | |
144 | + args.argv = argv; | |
97 | 145 | |
98 | 146 | mrb = mrb_open(); |
99 | 147 | if (mrb == NULL) { |
@@ -101,35 +149,8 @@ main(int argc, char **argv) | ||
101 | 149 | return EXIT_FAILURE; |
102 | 150 | } |
103 | 151 | |
104 | - ireps = (mrb_irep**)malloc(sizeof(mrb_irep*) * argc); | |
105 | - for (i = args_result; i < argc; ++i) { | |
106 | - ireps[i] = mrb_read_irep_file(mrb, files[i]); | |
107 | - if (!ireps[i]) { | |
108 | - fprintf(stderr, "can't read irep file %s\n", argv[i]); | |
109 | - return EXIT_FAILURE; | |
110 | - } | |
111 | - fclose(files[i]); | |
112 | - files[i] = fopen(argv[i], "wb"); | |
113 | - if (!ireps[i]) { | |
114 | - fprintf(stderr, "can't reopen irep file %s\n", argv[i]); | |
115 | - return EXIT_FAILURE; | |
116 | - } | |
117 | - } | |
118 | - | |
119 | - for (i = args_result; i < argc; ++i) { | |
120 | - /* clear lv if --lvar is enabled */ | |
121 | - if (args.lvar) { | |
122 | - irep_remove_lv(mrb, ireps[i]); | |
123 | - } | |
124 | - | |
125 | - /* debug flag must be alway false */ | |
126 | - dump_result = mrb_dump_irep_binary(mrb, ireps[i], FALSE, files[i]); | |
127 | - if (dump_result != MRB_DUMP_OK) { | |
128 | - fprintf(stderr, "error occur when dumping %s", argv[i]); | |
129 | - return EXIT_FAILURE; | |
130 | - } | |
131 | - } | |
152 | + ret = strip(mrb, &args); | |
132 | 153 | |
133 | 154 | mrb_close(mrb); |
134 | - return EXIT_SUCCESS; | |
155 | + return ret; | |
135 | 156 | } |