File: extractEventHandlerAndResource.cs

package info (click to toggle)
cadencii 3.3.9%2Bsvn20110818.r1732-6
  • links: PTS
  • area: main
  • in suites: buster
  • size: 35,916 kB
  • sloc: cs: 160,836; java: 42,449; cpp: 7,605; ansic: 1,728; perl: 1,087; makefile: 236; php: 142; xml: 117; sh: 21
file content (132 lines) | stat: -rw-r--r-- 4,971 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.IO;

class ExtractEventHandlerAndResource{
    static string in_file = "";
    static string event_out_file = "";
    static string res_out_file = "";
    static string edited_out_file = "";

    public static void Main( string[] args ){
        string current = "";
        foreach( string s in args ){
            if( s.StartsWith( "-" ) ){
                current = s;
            }else{
                if( current == "" ){
                    in_file = s;
                }else if( current == "-e" ){
                    event_out_file = s;
                    current = "";
                }else if( current == "-r" ){
                    res_out_file = s;
                    current = "";
                }else if( current == "-o" ){
                    edited_out_file = s;
                    current = "";
                }
            }
        }
        
        if( in_file == "" || 
            (in_file != "" && event_out_file == "" && res_out_file == "") ||
            (in_file != "" && edited_out_file == "") ){
            Console.WriteLine( "extractEventHandlerAndResource:" );
            Console.WriteLine( "    [NONE]  input file" );
            Console.WriteLine( "    -o      output file for \"edited\" input file" );
            Console.WriteLine( "    -e      output file for event handlers" );
            Console.WriteLine( "    -r      output file for resources" );
            return;
        }
        
        string infile = args[0];
        Mode mode = Mode.DEFAULT;
        StreamWriter event_out = null;
        if( event_out_file != "" ){
            event_out = new StreamWriter( event_out_file );
            event_out.WriteLine( "private void registerEventHandlers(){" );
        }
        StreamWriter res_out = null;
        if( res_out_file != "" ){
            res_out = new StreamWriter( res_out_file );
            res_out.WriteLine( "private void setResources(){" );
        }
        StreamWriter edited_out = null;
        if( edited_out_file != "" ){
            edited_out = new StreamWriter( edited_out_file );
        }
        using( StreamReader sr = new StreamReader( infile ) ){
            string line = "";
            int bra_count = 0; // ʂ̏o񐔁B"{"o++, "}"o--
            bool first_bra_found = false; //InitializeComponent͂ލŏ̒ʂoǂ
            while( (line = sr.ReadLine()) != null ){
                if( mode == Mode.DEFAULT ){
                    if( line.IndexOf( " void InitializeComponent" ) >= 0 ){
                        int i = countChar( line, '{' );
                        if( i > 0 ){
                            first_bra_found = true;
                        }
                        bra_count += i;
                        mode = Mode.READ_METHOD;
                    }
                    if( edited_out != null ){
                        edited_out.WriteLine( line );
                    }
                }else if( mode == Mode.READ_METHOD ){
                    //Console.WriteLine( "mode=" + mode + "; bra_count=" + bra_count );
                    int i = countChar( line, '{' );
                    if( !first_bra_found && i > 0 ){
                        first_bra_found = true;
                    }
                    bra_count += i;
                    bra_count -= countChar( line, '}' );
                    if( line.IndexOf( "EventHandler" ) >= 0 && line.IndexOf( "+=" ) >= 0 && line.IndexOf( "new" ) >= 0 ){
                        if( event_out != null ){
                            event_out.WriteLine( line );
                        }
                    }else if( line.IndexOf( "Resources." ) >= 0 ){
                        if( res_out != null ){
                            res_out.WriteLine( line );
                        }
                    }else{
                        if( edited_out != null ){
                            edited_out.WriteLine( line );
                        }
                    }
                    if( first_bra_found && bra_count == 0 ){
                        mode = Mode.DEFAULT;
                    }
                }
            }
        }
        
        if( event_out != null ){
            event_out.WriteLine( "}" );
            event_out.Close();
        }
        if( res_out != null ){
            res_out.WriteLine( "}" );
            res_out.Close();
        }
        if( edited_out != null ){
            edited_out.Close();
        }
    }
    
    private static int countChar( string s, char search ){
        int ret = 0;
        int index = 0;
        int found = s.IndexOf( search, index );
        while( found >= 0 ){
            ret++;
            index = found + 1;
            found = s.IndexOf( search, index );
        }
        return ret;
    }
}

enum Mode{
    DEFAULT,
    READ_METHOD,
}