Files
usaco/lib/cmd/io.sh
T

30 lines
1.1 KiB
Bash

# usaco io <name> [io-name]
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
old="$(detect_io "$dir" || true)"
if [[ $# -lt 2 ]]; then
stdio="$(grep -o 'STDIO("[^"]*")' "$dir/main.cpp" 2>/dev/null | sed 's/STDIO("\(.*\)")/\1/' | head -1 || true)"
echo "files: ${old:-none}"
echo "STDIO: ${stdio:-none}"
exit 0
fi
io="$2"
# rename data files
if [[ -n "$old" && "$old" != "$io" ]]; then
for ext in in out; do
[[ -e "$dir/$old.$ext" ]] && mv "$dir/$old.$ext" "$dir/$io.$ext" && echo "Renamed $old.$ext -> $io.$ext"
done
fi
[[ -e "$dir/$io.in" ]] || { touch "$dir/$io.in"; echo "Created $io.in"; }
# rewire STDIO in main.cpp
if grep -q 'STDIO("' "$dir/main.cpp" 2>/dev/null; then
sed -i "s/STDIO(\"[^\"]*\")/STDIO(\"$io\")/" "$dir/main.cpp"
echo "Updated STDIO(\"$io\") in main.cpp"
elif grep -q '^int main() {$' "$dir/main.cpp" 2>/dev/null; then
sed -i "s/^int main() {$/int main() {\n STDIO(\"$io\");/" "$dir/main.cpp"
echo "Inserted STDIO(\"$io\") in main.cpp"
else
echo "Warning: couldn't find STDIO(...) or 'int main() {' in main.cpp — wire it up manually" >&2
fi
echo "io name for $(basename "$dir") is now '$io'"