Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macro / txt / CharListDirectoryMacro.groovy @ 499

History | View | Annotate | Download (1.3 kB)

1
package org.txm.macro.txt;
2
// STANDARD DECLARATIONS
3

    
4
import org.kohsuke.args4j.*
5
import groovy.transform.Field
6
import org.txm.rcp.swt.widget.parameters.*
7

    
8
// BEGINNING OF PARAMETERS
9

    
10
// Declare each parameter here
11
// (available widget types: Query, File, Folder, String, Text, Boolean, Integer, Float and Date)
12

    
13
@Field @Option(name="inputFile",usage="The file to read", widget="File", required=true, def="C:/Temp/foo.txt")
14
def inputFile
15

    
16
@Field @Option(name="inputDir",usage="The file to read", widget="Folder", required=false, def="")
17
def inputDir
18

    
19

    
20
// Open the parameters input dialog box
21
if (!ParametersDialog.open(this)) return;
22

    
23
def dic = [:]
24

    
25
// END OF PARAMETERS
26
if (inputDir != null && inputDir.exists()) {
27
if (inputDir.listFiles() == null) {println "no files..."; return;}
28
        println "processing "+inputDir.listFiles().size()+" files"
29
        for (def f : inputDir.listFiles()) {
30
                if (f.isDirectory()) continue;
31
                print "."
32
                count(f, dic)
33
        }
34
        println ""
35
} else {
36
        count(inputFile, dic)
37
}
38
int total = 0
39
for (def e : dic.sort()) {
40
        println(sprintf("%c\t%d", [e.getKey(), e.getValue()]))
41
        total += e.getValue()
42
}
43
println "TOTAL\t$total"
44

    
45
def count(def f, def dic) {
46
        String s = f.getText()
47
        for (char c : s) {
48
                if (c == '\r' || c == '\n' || c == '\t' || c == ' ' || c == ' ') continue;
49
                if (!dic.containsKey(c)) { dic[c] = 0 }
50
                dic[c]++
51
        }
52

    
53
        return dic
54
}
55