• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

mrubyを超漢字で動作させる


Commit MetaInfo

Révision85bb1f92ef74a85488700c556b60e6ac7933e822 (tree)
l'heure2014-07-12 20:36:44
Auteurcremno <cremno@mail...>
CommiterYukihiro "Matz" Matsumoto

Message de Log

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:

files[i] = fopen(argv[i], "wb");
if (!ireps[i]) {

Improve error messages a bit and add missing newline to one.

Change Summary

Modification

--- a/mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c
+++ b/mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c
@@ -6,6 +6,9 @@
66 #include "mruby/dump.h"
77
88 struct strip_args {
9+ int argc_start;
10+ int argc;
11+ char **argv;
912 mrb_bool lvar;
1013 };
1114
@@ -64,14 +67,65 @@ parse_args(int argc, char **argv, struct strip_args *args)
6467 return i;
6568 }
6669
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+
67122 int
68123 main(int argc, char **argv)
69124 {
70125 struct strip_args args;
71- int args_result, i, dump_result;
72- FILE **files;
73- mrb_irep **ireps;
126+ int args_result;
74127 mrb_state *mrb;
128+ int ret;
75129
76130 if (argc <= 1) {
77131 printf("no files to strip\n");
@@ -85,15 +139,9 @@ main(int argc, char **argv)
85139 return EXIT_FAILURE;
86140 }
87141
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;
97145
98146 mrb = mrb_open();
99147 if (mrb == NULL) {
@@ -101,35 +149,8 @@ main(int argc, char **argv)
101149 return EXIT_FAILURE;
102150 }
103151
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);
132153
133154 mrb_close(mrb);
134- return EXIT_SUCCESS;
155+ return ret;
135156 }