diff --git a/wpilibc/src/main/native/cpp/Filesystem.cpp b/wpilibc/src/main/native/cpp/Filesystem.cpp new file mode 100644 index 0000000000..1546050e9b --- /dev/null +++ b/wpilibc/src/main/native/cpp/Filesystem.cpp @@ -0,0 +1,31 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2018 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "frc/Filesystem.h" + +#include +#include + +#include "frc/RobotBase.h" + +void frc::filesystem::GetLaunchDirectory(wpi::SmallVectorImpl& result) { + wpi::sys::fs::current_path(result); +} + +void frc::filesystem::GetOperatingDirectory( + wpi::SmallVectorImpl& result) { + if (RobotBase::IsReal()) { + wpi::sys::path::native("/home/lvuser", result); + } else { + frc::filesystem::GetLaunchDirectory(result); + } +} + +void frc::filesystem::GetDeployDirectory(wpi::SmallVectorImpl& result) { + frc::filesystem::GetOperatingDirectory(result); + wpi::sys::path::append(result, "deploy"); +} diff --git a/wpilibc/src/main/native/include/frc/Filesystem.h b/wpilibc/src/main/native/include/frc/Filesystem.h new file mode 100644 index 0000000000..baf8044dee --- /dev/null +++ b/wpilibc/src/main/native/include/frc/Filesystem.h @@ -0,0 +1,45 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2018 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#pragma once + +#include + +#include "frc/RobotBase.h" + +namespace frc { +namespace filesystem { + +/** + * Obtains the current working path that the program was launched with. + * This is analogous to the `pwd` command on unix. + * + * @param result The result of the current working path lookup. + */ +void GetLaunchDirectory(wpi::SmallVectorImpl& result); + +/** + * Obtains the operating directory of the program. On the roboRIO, this + * is /home/lvuser. In simulation, it is where the simulation was launched + * from (`pwd`). + * + * @param result The result of the operating directory lookup. + */ +void GetOperatingDirectory(wpi::SmallVectorImpl& result); + +/** + * Obtains the deploy directory of the program, which is the remote location + * src/main/deploy is deployed to by default. On the roboRIO, this is + * /home/lvuser/deploy. In simulation, it is where the simulation was launched + * from, in the subdirectory "deploy" (`pwd`/deploy). + * + * @param result The result of the operating directory lookup + */ +void GetDeployDirectory(wpi::SmallVectorImpl& result); + +} // namespace filesystem +} // namespace frc diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Filesystem.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Filesystem.java new file mode 100644 index 0000000000..cfefb6aa70 --- /dev/null +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Filesystem.java @@ -0,0 +1,58 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +package edu.wpi.first.wpilibj; + +import java.io.File; + +/** + * Class for interacting with the Filesystem, particularly, interacting with + * FRC-related paths on the system, such as the launch and deploy directories. + * + *

This class is primarily used for obtaining resources in src/main/deploy, and + * the RoboRIO path /home/lvuser in a simulation-compatible way.

+ */ +public final class Filesystem { + private Filesystem() { } + + /** + * Obtains the current working path that the program was launched with. + * This is analogous to the `pwd` command on unix. + * + * @return The current working directory (launch directory) + */ + public static File getLaunchDirectory() { + return new File(System.getProperty("user.dir")).getAbsoluteFile(); + } + + /** + * Obtains the operating directory of the program. On the roboRIO, this is + * /home/lvuser. In simulation, it is where the simulation was launched from + * (`pwd`). + * + * @return The operating directory + */ + public static File getOperatingDirectory() { + if (RobotBase.isReal()) { + return new File("/home/lvuser"); + } else { + return getLaunchDirectory(); + } + } + + /** + * Obtains the deploy directory of the program, which is the remote location + * src/main/deploy is deployed to by default. On the roboRIO, this is + * /home/lvuser/deploy. In simulation, it is where the simulation was launched + * from, in the subdirectory "deploy" (`pwd`/deploy). + * + * @return The deploy directory + */ + public static File getDeployDirectory() { + return new File(getOperatingDirectory(), "deploy"); + } +}