summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/context/ruby/base/file.rb
blob: 39bb7d467310452895355fac9f96c7a705fc6ccf (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# module    : base/file
# copyright : PRAGMA Advanced Document Engineering
# version   : 2002-2005
# author    : Hans Hagen
#
# project   : ConTeXt / eXaMpLe
# concept   : Hans Hagen
# info      : j.hagen@xs4all.nl
# www       : www.pragma-ade.com

require 'ftools'

class File

    def File.suffixed(name,sufa,sufb=nil)
        if sufb then
            if sufa.empty? then
                unsuffixed(name) + ".#{sufb}"
            else
                unsuffixed(name) + "-#{sufa}.#{sufb}"
            end
        else
            unsuffixed(name) + ".#{sufa}"
        end
    end

    def File.unsuffixed(name)
        name.sub(/\.[^\.]*?$/o, '')
    end

    def File.suffix(name,default='')
        if name =~ /\.([^\.]*?)$/o then
            $1
        else
            default
        end
    end

    def File.splitname(name,suffix='')
        if name =~ /^(.*)\.([^\.]*?)$/o then
            [$1, $2]
        else
            [name, suffix]
        end
    end

end

class File

    def File.silentopen(name,method='r')
        begin
            f = File.open(name,method)
        rescue
            return nil
        else
            return f
        end
    end

    def File.silentread(name)
        begin
            data = IO.read(name)
        rescue
            return nil
        else
            return data
        end
    end

    def File.atleast?(name,n=0)
        begin
            size = FileTest.size(name)
        rescue
            return false
        else
            return size > n
        end
    end

    def File.appended(name,str='')
        if FileTest.file?(name) then
            begin
                if f = File.open(name,'a') then
                    f << str
                    f.close
                    return true
                end
            rescue
            end
        end
        return false
    end

    def File.written(name,str='')
        begin
            if f = File.open(name,'w') then
                f << str
                f.close
                return true
            end
        rescue
        end
        return false
    end

    def File.silentdelete(filename)
        File.delete(filename) rescue false
    end

    def File.silentcopy(oldname,newname)
        return if File.expand_path(oldname) == File.expand_path(newname)
        File.makedirs(File.dirname(newname)) rescue false
        File.copy(oldname,newname) rescue false
    end

    def File.silentrename(oldname,newname)
        # in case of troubles, we just copy the file; we
        # maybe working over multiple file systems or
        # apps may have mildly locked files (like gs does)
        return if File.expand_path(oldname) == File.expand_path(newname)
        File.delete(newname) rescue false
        begin
            File.rename(oldname,newname)
        rescue
            File.makedirs(File.dirname(newname)) rescue false
            File.copy(oldname,newname) rescue false
        end
    end

end

class File

    # handles "c:\tmp\test.tex" as well as "/${TEMP}/test.tex")

    def File.unixfied(filename)
        begin
            str = filename.gsub(/\$\{*([a-z0-9\_]+)\}*/oi) do
                if ENV.key?($1) then ENV[$1] else $1 end
            end
            str.gsub(/[\/\\]+/o, '/')
        rescue
            filename
        end
    end

end