BASIS  version 1.2.3 (revision 2104)
stdaux.h
Go to the documentation of this file.
00001 /**
00002  * @file  stdaux.h
00003  * @brief Standard auxiliary functions.
00004  *
00005  * @note The file stdaux.h is automatically generated by BASIS from the
00006  *       file stdaux.h.in which is part of BASIS.
00007  *
00008  * Copyright (c) 2011 University of Pennsylvania. All rights reserved.<br />
00009  * See https://www.cbica.upenn.edu/sbia/software/license.html or COPYING file.
00010  *
00011  * Contact: SBIA Group <sbia-software at uphs.upenn.edu>
00012  */
00013 
00014 #pragma once
00015 #ifndef _SBIA_BASIS_STDAUX_H
00016 #define _SBIA_BASIS_STDAUX_H
00017 
00018 
00019 #include <string>
00020 #include <vector>
00021 #include <iostream>
00022 
00023 
00024 namespace sbia
00025 {
00026 
00027 namespace basis
00028 {
00029 
00030 
00031 /// @addtogroup BasisCxxUtilities
00032 /// @{
00033 
00034 
00035 // ===========================================================================
00036 // executable information
00037 // ===========================================================================
00038 
00039 /**
00040  * @brief Print version information.
00041  *
00042  * @param [in] name      Program name.
00043  * @param [in] copyright Copyright notice. Defaults to University of Pennsylvania
00044  *                       without year if NULL. If an empty string is given,
00045  *                       i.e., copyright = "", no copyright notice is written.
00046  * @param [in] license   License notice. Defaults to official software license
00047  *                       used at SBIA if NULL. If an empty string is given,
00048  *                       i.e., license = "", no license notice is written.
00049  */
00050 void print_version(const char* name, const char* copyright = NULL, const char* license = NULL);
00051 
00052 /**
00053  * @brief Print contact information.
00054  *
00055  * @param [in] contact Name of the contact. Defaults to
00056  *                     "SBIA Group <sbia-software at uphs.upenn.edu>".
00057  */
00058 void print_contact(const char* contact = NULL);
00059 
00060 // ===========================================================================
00061 // installation directories
00062 // ===========================================================================
00063 
00064 /**
00065  * @brief Whether the executable was executed from within the build tree.
00066  *
00067  * @return Whether the executable was executed from within the build tree.
00068  */
00069 bool executing_in_build_tree();
00070 
00071 /**
00072  * @brief Get absolute path of installation directory.
00073  *
00074  * This function returns the absolute path of the installation prefix path.
00075  * If the installation was not moved after a "make install", the returned
00076  * directory corresponds to the value of the INSTALL_PREFIX CMake variable
00077  * as specified during the build of the executable file. Note, however,
00078  * that even when the installation tree was moved after the configuration
00079  * and build of the software, the correct path is returned as long as the
00080  * relative directory structure of the installation tree is maintained.
00081  * This is because the path is determined relative to the directory of the
00082  * executable itself, knowning in which path this executable is located
00083  * relative to the INSTALL_PREFIX.
00084  *
00085  * @note If the executable is executed from within the build tree, the
00086  *       returned path will not be correct. Therefore, only use this
00087  *       function when executing_in_build_tree() returns false. Otherwise,
00088  *       the configured absolute paths which are valid for the build tree
00089  *       have to be used. Note that the build tree is not supposed to be
00090  *       relocatable in any case as it is only a temporary directory tree
00091  *       and CMake requires it to be not moved anywhere else.
00092  *
00093  * @return Absolute path of top directory of installation tree.
00094  */
00095 std::string get_installation_prefix();
00096 
00097 /**
00098  * @brief Get absolute path of directory containing runtime executables.
00099  *
00100  * @return Absolute path of directory containing runtime executables.
00101  */
00102 std::string get_runtime_directory();
00103 
00104 /**
00105  * @brief Get absolute path of directory containing auxiliary executables.
00106  *
00107  * @return Absolute path of directory containing auxiliary executables.
00108  */
00109 std::string get_libexec_directory();
00110 
00111 /**
00112  * @brief Get absolute path of directory containing libraries.
00113  *
00114  * @return Absolute path of directory containing libraries.
00115  */
00116 std::string get_library_directory();
00117 
00118 /**
00119  * @brief Get absolute path of directory containing auxiliary data.
00120  *
00121  * @return Absolute path of directory containing auxiliary data.
00122  */
00123 std::string get_data_directory();
00124 
00125 // ===========================================================================
00126 // executable file path of other build targets
00127 // ===========================================================================
00128 
00129 /**
00130  * @brief Get canonical path of executable file.
00131  *
00132  * This function uses the static instance of the class ExecutableTargetInfo
00133  * in order to obtain the required information to be able to determine the
00134  * absolute path of the executable file which was built and/or installed as
00135  * the specified build target.
00136  *
00137  * @note The BASIS path module provides a function also named get_executable_path()
00138  *       which can be used to get the canonical path of the calling executable.
00139  *
00140  * @sa ExecutableTargetInfo
00141  *
00142  * @param [in] target Name/UID of build target.
00143  *
00144  * @return Absolute path of executable file or an empty string if the build
00145  *         target is not known.
00146  */
00147 std::string get_executable_path(const std::string& target);
00148 
00149 // ===========================================================================
00150 // command execution
00151 // ===========================================================================
00152 
00153 /**
00154  * @brief Execute command as subprocess.
00155  *
00156  * This function is a replacement for system() on Unix and is furthermore
00157  * less platform dependent. The first argument of the given command-line string
00158  * is mapped to an absolute executable file using get_executable_path() if the
00159  * given first argument is a know build target name. Otherwise, the command-line
00160  * is used unmodified.
00161  *
00162  * @param [in] cmd         Command-line given as double quoted string. Arguments
00163  *                         containing whitespaces have to be quoted using double
00164  *                         quotes. Use a backslash (\) to escape double quotes
00165  *                         inside an argument as well as to escape a backslash
00166  *                         itself (required if backslash at end of double quoted
00167  *                         argument, e.g., "this argument \\").
00168  * @param [in]  quiet      Turns off output of stdout of child process to stdout
00169  *                         of parent process.
00170  * @param [out] out        Output stream where command output is written to.
00171  * @param [in]  allow_fail If true, no exception is thrown if the exit code
00172  *                         of the child process is non-zero. Otherwise,
00173  *                         a SubprocessException object is thrown in that case.
00174  * @param [in]  verbose    Verbosity of output messages. Does not affect
00175  *                         verbosity of executed command.
00176  * @param [in]  simulate   Whether to simulate command execution only.
00177  *
00178  * @returns Exit code of command or -1 if subprocess creation failed.
00179  *
00180  * @throws SubprocessException If subprocess creation failed or command returned
00181  *                             a non-zero exit code while @p allow_fail is false.
00182  */
00183 int execute_process(const std::string& cmd,
00184                     bool               quiet      = false,
00185                     // attention: stdout is a macro defined by windows.h
00186                     std::ostream*      out        = NULL,
00187                     bool               allow_fail = false,
00188                     int                verbose    = 0,
00189                     bool               simulate   = false);
00190 
00191 /**
00192  * @brief Execute command as subprocess.
00193  *
00194  * This function is a replacement for system() on Unix and is furthermore
00195  * less platform dependent. The first argument of the given command-line string
00196  * is mapped to an absolute executable file using get_executable_path() if the
00197  * given first argument is a know build target name. Otherwise, the command-line
00198  * is used unmodified.
00199  *
00200  * @param [in,out] args       Command-line given as argument vector. The first
00201  *                            argument has to be either a build target name or the
00202  *                            name/path of the command to execute. Note that as a
00203  *                            side effect, the first argument of the input vector
00204  *                            is replaced by the absolute path of the actual
00205  *                            executable file if applicable.
00206  * @param [in]     quiet      Turns off output of stdout of child process to
00207  *                            stdout of parent process.
00208  * @param [out]    out        Output stream where command output is written to.
00209  * @param [in]     allow_fail If true, no exception is thrown if the exit code
00210  *                            of the child process is non-zero. Otherwise,
00211  *                            a SubprocessException object is thrown in that case.
00212  * @param [in]     verbose    Verbosity of output messages. Does not affect
00213  *                            verbosity of executed command.
00214  * @param [in]     simulate   Whether to simulate command execution only.
00215  *
00216  * @returns Exit code of command or -1 if subprocess creation failed.
00217  *
00218  * @throws SubprocessException If subprocess creation failed or command returned
00219  *                             a non-zero exit code while @p allow_fail is false.
00220  */
00221 int execute_process(std::vector<std::string>& args,
00222                     bool                      quiet      = false,
00223                     // attention: stdout is a macro defined by windows.h
00224                     std::ostream*             out        = NULL,
00225                     bool                      allow_fail = false,
00226                     int                       verbose    = 0,
00227                     bool                      simulate   = false);
00228 
00229 
00230 /// @}
00231 // end of Doxygen group
00232 
00233 
00234 } // namespace basis
00235 
00236 } // namespace sbia
00237 
00238 
00239 #endif // _SBIA_BASIS_STDAUX_H
00240