lcpex rewrite - alpha
This commit is contained in:
114
src/misc/helpers.cpp
Normal file
114
src/misc/helpers.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
#include "helpers.h"
|
||||
|
||||
/**
|
||||
* @brief Determines if a file or directory exists
|
||||
*
|
||||
* This function takes a file or directory name as input and uses the `stat` function to determine if the
|
||||
* specified file or directory exists.
|
||||
*
|
||||
* @param name The name of the file or directory to check
|
||||
*
|
||||
* @return `true` if the file or directory exists, `false` otherwise
|
||||
*/
|
||||
bool exists(const std::string& name)
|
||||
{
|
||||
struct stat buffer;
|
||||
return (stat (name.c_str(), &buffer) == 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Returns the current working directory as a string
|
||||
*
|
||||
* This function uses the `getcwd` function to obtain the current working directory. If the `getcwd`
|
||||
* function fails, an empty string is returned.
|
||||
*
|
||||
* @return The current working directory as a string
|
||||
*/
|
||||
std::string get_working_path()
|
||||
{
|
||||
char temp[MAXPATHLEN];
|
||||
if (!getcwd(temp, MAXPATHLEN)) {
|
||||
return "";
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Determines if a given file path is a regular file
|
||||
*
|
||||
* This function takes a file path as input and uses the `stat` function to obtain information about the
|
||||
* file or directory. The `st_mode` member of the `stat` structure is then checked to determine if the
|
||||
* file is a regular file or not.
|
||||
*
|
||||
* @param path The file path to be checked
|
||||
*
|
||||
* @return `true` if the file path is a regular file, `false` otherwise
|
||||
*/
|
||||
bool is_file( std::string path)
|
||||
{
|
||||
struct stat buf;
|
||||
stat( path.c_str(), &buf );
|
||||
return S_ISREG(buf.st_mode);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Determines if a given file path is a directory
|
||||
*
|
||||
* This function takes a file path as input and uses the `stat` function to obtain information about the
|
||||
* file or directory. The `st_mode` member of the `stat` structure is then checked to determine if the
|
||||
* file is a directory or not.
|
||||
*
|
||||
* @param path The file path to be checked
|
||||
*
|
||||
* @return `true` if the file path is a directory, `false` otherwise
|
||||
*/
|
||||
bool is_dir( std::string path )
|
||||
{
|
||||
struct stat buf;
|
||||
stat( path.c_str(), &buf );
|
||||
return S_ISDIR(buf.st_mode);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Returns a string representation of the current date and time in the format "YYYY-MM-DD_HH:MM:SS"
|
||||
*
|
||||
* This function uses the `std::chrono` library to obtain the current system time and convert it to a
|
||||
* time_t value. It then uses the `strftime` function to format the date and time into a string with the
|
||||
* format "YYYY-MM-DD_HH:MM:SS". The resulting string is returned as the function result.
|
||||
*
|
||||
* @return A string representation of the current date and time
|
||||
*/
|
||||
std::string get_8601()
|
||||
{
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto itt = std::chrono::system_clock::to_time_t(now);
|
||||
char buf[20];
|
||||
strftime(buf, sizeof(buf), "%Y-%m-%d_%H:%M:%S", localtime(&itt));
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Interpolates the environment variables in the input text
|
||||
*
|
||||
* This function takes a string reference as input and replaces all occurrences of
|
||||
* environment variables in the format `${VAR_NAME}` with their corresponding values.
|
||||
* If an environment variable is not set, it is replaced with an empty string.
|
||||
*
|
||||
* @param text The input text to be processed
|
||||
*/
|
||||
void interpolate( std::string & text )
|
||||
{
|
||||
static std::regex env( "\\$\\{([^}]+)\\}" );
|
||||
std::smatch match;
|
||||
while ( std::regex_search( text, match, env ) )
|
||||
{
|
||||
const char * s = getenv( match[1].str().c_str() );
|
||||
const std::string var( s == NULL ? "" : s );
|
||||
text.replace( match[0].first, match[0].second, var );
|
||||
}
|
||||
}
|
||||
54
src/misc/helpers.h
Normal file
54
src/misc/helpers.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
Rex - A configuration management and workflow automation tool that
|
||||
compiles and runs in minimal environments.
|
||||
|
||||
© SILO GROUP and Chris Punches, 2020.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef REX_HELPERS_H
|
||||
#define REX_HELPERS_H
|
||||
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/param.h>
|
||||
#include <unistd.h>
|
||||
#include <chrono>
|
||||
#include <sstream>
|
||||
#include <syslog.h>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <regex>
|
||||
|
||||
|
||||
bool exists (const std::string& name);
|
||||
|
||||
std::string get_working_path();
|
||||
|
||||
bool is_file( std::string );
|
||||
|
||||
bool is_dir( std::string );
|
||||
|
||||
// expand environment variables in string
|
||||
void interpolate( std::string & text);
|
||||
|
||||
std::string get_8601();
|
||||
|
||||
const char * command2args( std::string input_string );
|
||||
|
||||
#endif //REX_HELPERS_H
|
||||
Reference in New Issue
Block a user