This repository has been archived by the owner on Mar 16, 2023. It is now read-only.
forked from danielsulik/hidapi-tcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swig-replace.tcl
86 lines (79 loc) · 2.47 KB
/
swig-replace.tcl
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
#!/usr/bin/tclsh
#exec tclsh "$0" "$@"
proc Usage {} {
puts "USAGE\n[info script] <infile.c>"
puts " Find functions listed in replace.txt in <infile.c>"
puts " and replace them with user's code functions of the same "
puts " name defined in corresponding c files."
puts " Example replace.txt:"
puts " function1"
puts " function2"
puts " function3"
puts " Files function1.c, function2.c, function3.c defining"
puts " corresponding functions must exist."
}
set rfile "replace.txt"
if { [catch { open $rfile "r" } rf ] } {
puts "ERROR: Could not open $rfile!"
Usage
exit -1
}
set funclist {}
while { [gets $rf line] >= 0 } {
lappend funclist $line
}
close $rf
if { $::argc == 1 } {
puts "Running [info script]"
set infile [lindex $::argv 0]
if { [catch { open $infile "r" } inf ] } {
puts "ERROR: Could not open $infile!"
Usage
}
if { [catch {open $infile\_tmp "w" } outf ] } {
puts "ERROR: Could not open temporary file for writing!"
Usage
}
set opbr 0
set state "SEARCH"
while { [gets $inf line] >= 0 } {
if { $state == "SEARCH" } {
foreach f $funclist {
set re \[\\\ |\\t\]*_wrap_$f\[\\\ |\\t\]*\\(ClientData.*\\)
if { [regexp "$re" $line] } {
puts $line
set opbr [regexp -all {\{} $line ]
set state "IGNORE"
set funcfile $f.c
puts "INFO:Replacing function $f"
break
}
}
if { $state != "IGNORE" } { puts $outf $line }
} elseif { $state == "IGNORE" } {
puts $line
# brackets balancing
set opbr [expr $opbr + [regexp -all {\{} $line ] - [regexp -all {\}} $line ]]
if { $opbr == 0 } {
set state "SEARCH"
#copy new function to the file
if { [catch {open $funcfile "r" } ff ] } {
puts "ERROR: Could not open $funcfile."
Usage
exit -1
}
while { [gets $ff line] >= 0 } {
puts $outf $line
}
close $ff
}
}
}
close $inf
close $outf
file copy -force $infile\_tmp $infile
file delete -force $infile\_tmp
} else {
puts "ERROR: No arguments passed!"
Usage
}