-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun
More file actions
executable file
·106 lines (85 loc) · 2.72 KB
/
run
File metadata and controls
executable file
·106 lines (85 loc) · 2.72 KB
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
#!/usr/bin/env bash
set -Eeuo pipefail
trap cleanup SIGTERM ERR EXIT
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
usage() {
cat << EOF
Usage: $(basename "${BASH_SOURCE[0]}") <file.cpp>
Arguments:
<file.cpp> C++ source file to compile and run (case-sensitive preferred, fallback to case-insensitive)
Example:
$(basename "${BASH_SOURCE[0]}") main.cpp
$(basename "${BASH_SOURCE[0]}") MAIN.cpp
EOF
exit
}
cleanup() {
status=$?
trap - SIGTERM ERR EXIT
# no cleanup needed
}
setup_colors() {
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m'
else
NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
fi
}
msg() {
echo >&2 -e "${1-}"
}
die() {
local msg=$1
local code=${2-1}
msg "$msg"
exit "$code"
}
parse_params() {
if [[ $# -ne 1 ]]; then
usage
fi
input_file="$1"
if [[ "$input_file" != *.* ]]; then
input_file="${input_file}.cpp"
fi
dir_name="$(dirname "$input_file")"
base_name="$(basename "$input_file")"
exact_match="$dir_name/$base_name"
if [[ -f "$exact_match" ]]; then
# Case-sensitive match found
src_file="$exact_match"
else
# Try case-insensitive search
msg "${ORANGE}File not found (case-sensitive): $input_file. Trying case-insensitive search...${NOFORMAT}"
matches=($(find "$dir_name" -maxdepth 1 -type f -iname "$base_name"))
if [[ ${#matches[@]} -eq 0 ]]; then
die "${RED}File not found (case-insensitive): $input_file${NOFORMAT}"
elif [[ ${#matches[@]} -gt 1 ]]; then
die "${RED}Multiple files match (case-insensitive): ${matches[*]}${NOFORMAT}"
else
src_file="${matches[0]}"
fi
fi
out_file="${src_file%.cpp}.out"
}
compile_and_run() {
msg "${ORANGE}Compiling $src_file → $out_file...${NOFORMAT}"
g++ -std=c++17 -O2 -Wall "$src_file" -o "$out_file" || die "${RED}Compilation failed${NOFORMAT}"
msg "${GREEN}Compilation successful.${NOFORMAT}"
msg "${ORANGE}Running $out_file...${NOFORMAT}"
if [[ "$(uname)" == "Darwin" ]]; then
# macOS
TIME_CMD="gtime"
else
# Linux (assume GNU time is available as /usr/bin/time)
TIME_CMD="/usr/bin/time"
fi
if [[ -f "input.txt" ]]; then
$TIME_CMD -f "Elapsed: %e s Execution: %U s System: %S s Memory: %M KB" "./$out_file" < input.txt 2>&1 | while read line; do echo -e "${CYAN}${line}${NOFORMAT}"; done
else
$TIME_CMD -f "Elapsed: %e s Execution: %U s System: %S s Memory: %M KB" "./$out_file" 2>&1 | while read line; do echo -e "${CYAN}${line}${NOFORMAT}"; done
fi
}
setup_colors
parse_params "$@"
compile_and_run