1// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Package util provides small utility functions.
16package util
17
18import (
19	"os"
20)
21
22// IsFile returns true if path is a file.
23func IsFile(path string) bool {
24	s, err := os.Stat(path)
25	if err != nil {
26		return false
27	}
28	return !s.IsDir()
29}
30
31// IsDir returns true if path is a directory.
32func IsDir(path string) bool {
33	s, err := os.Stat(path)
34	if err != nil {
35		return false
36	}
37	return s.IsDir()
38}
39
40// Percent returns the percentage completion of i items out of n.
41func Percent(i, n int) int {
42	return int(Percent64(int64(i), int64(n)))
43}
44
45// Percent64 returns the percentage completion of i items out of n.
46func Percent64(i, n int64) int64 {
47	if n == 0 {
48		return 0
49	}
50	return (100 * i) / n
51}
52