1#!/usr/bin/env omake
2#----------------------------------------------------------------------------
3# Project: Horizon
4# File:    utildefs.om
5#
6# Copyright 2007-2009 Nintendo.  All rights reserved.
7#
8# These coded instructions, statements, and computer programs contain
9# proprietary information of Nintendo of America Inc. and/or Nintendo
10# Company Ltd., and are protected by Federal copyright law.  They may
11# not be disclosed to third parties or copied or duplicated in any form,
12# in whole or in part, without the prior written consent of Nintendo.
13#
14# $Date:: 2010-12-24#$
15# $Rev: 33135 $
16# $Author: hatamoto_minoru $
17#----------------------------------------------------------------------------
18
19public.gsub(str1, pattern, str2) =
20    FS=__XYZ
21    RS=__XYZ
22    stdout = $(open-out-string)
23    fsubst($(open-in-string $(str1)))
24    case $(pattern) g
25        value $(str2)
26    result = $(out-contents $(stdout))
27    close($(stdout))
28    return $(result)
29
30public.findstring(str1, pattern) =
31    return $(grep q, $(pattern), $(open-in-string $(str1)))
32
33public.compileToRegex(filterstrings) =
34    regex_result =
35    foreach(fs, $(filterstrings))
36        stdout = $(open-out-string)
37        fsubst($(open-in-string $(fs)))
38        case $'\*(\.\*)+' g
39            value $'*'
40        case $'\.' g
41            value $'\.'
42        case $'\*' g
43            value $'.*'
44        case $'[()]' g
45            value $'\'$1
46
47        regex = $(out-contents $(stdout))
48        close($(stdout))
49        regex_result += $"\($(regex)\)"
50        export
51    return $(concat $'|', $(regex_result))
52
53public.isFilterMatch(regex, fullname) =
54    match $(fullname)
55    case $(regex)
56        return true
57    default
58        return false
59
60public.exist-dirs(dirs) =
61    return $(filter $(dirs),$(glob D,*))
62
63public.ls_(options, dir) =
64    files =
65    .SUBDIRS: $(dir)
66        files = $(ls $(options), .)
67        export
68    return $(files)
69
70public.subdirs_(options, dir) =
71    dirs =
72    .SUBDIRS: $(dir)
73        dirs = $(subdirs $(options), .)
74        export
75    return $(dirs)
76
77public.ls_relative(options, dir) =
78    dir = $(absname $(dir))
79    return $(removeprefix $(dir)$(DIRSEP), $(filter-out $(dir), $(ls_ $(options), $(dir))))
80
81public.files(options, path) =
82    result =
83    export result
84    .SUBDIRS: $(path)
85        foreach(filename, $(ls $(options), .))
86            if $(test -f $(filename))
87                result += $(string $(filename))
88    return $(result)
89
90public.filter-dirs(filenames) =
91    result =
92    export result
93    foreach(filename, $(filenames))
94        if $(test -d $(filename))
95            result += $(filename)
96    return $(result)
97
98public.filter-files(filenames) =
99    result =
100    export result
101    foreach(filename, $(filenames))
102        if $(test -f $(filename))
103            result += $(filename)
104    return $(result)
105
106public.CheckProg_(prog) =
107   WHERE = $(where $(prog))
108   if $(WHERE)
109      return true
110   else
111      return false
112
113public.readfile(filename) =
114    result =
115    try
116        channel = $(fopen $(filename), r)
117        result += $(input-line $(channel))
118        close($(channel))
119        export
120    catch RuntimeException(e)
121        return
122    return $(result)
123
124public.makeDirectory(path) =
125    section
126        $(path):
127            mkdir -p $@
128    return $(dir $(path))
129
130public.getMtimeIfLarge(filename) =
131    file_stat = $`(stat $(filename))
132    return $(if $(and $(file-exists $(filename)), $(ge $(file_stat.size), 8000000)), $(file_stat.mtime))