cphot 0.1
A C++ tool for computing photometry from spectra.
helpers.hpp File Reference

Some random tools to ease the project. More...

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <dirent.h>
#include <iostream>
#include <regex>
#include <sstream>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <vector>

Go to the source code of this file.

Namespaces

 utils
 

Functions

template<typename T >
std::ostream & operator<< (std::ostream &os, const std::vector< T > &v)
 Display an std::vector object. More...
 
bool contains (const std::vector< std::string > &v, const std::string &other)
 Check if a vector contains an item. More...
 
bool utils::file_exists (const std::string &name)
 Check if a file exists regardless of types. More...
 
bool utils::folder_exists (std::string foldername)
 Checks if a folder exists. More...
 
int utils::_mkdir (const char *path)
 Portable wrapper for mkdir. More...
 
int utils::mkdir (const char *path)
 Recursive, portable wrapper for mkdir. More...
 
std::vector< std::string > list_dir (const std::string &path, const std::string &pattern=".*")
 Return the list of files matching a pattern from a given directory. More...
 
std::vector< std::string > split_string (const std::string &text, const std::string &delimiter)
 Split string into bits according to a given delimiter. More...
 
std::string toupper (std::string s)
 Returns the upper case of a string. More...
 
std::string tolower (std::string s)
 Returns the lower case of a string. More...
 
template<typename... Args>
std::string string_sprintf (const char *format, Args... args)
 Helper to make a string with formatting options. More...
 
template<typename T >
parseString (const char *txt)
 Parse a char* into a numeric value. More...
 
template<typename T >
parseString (const std::string &txt)
 Parse a std::string into a numeric value. More...
 

Detailed Description

Some random tools to ease the project.

Version
0.1
Date
2021-12-01

Definition in file helpers.hpp.

Function Documentation

◆ contains()

bool contains ( const std::vector< std::string > &  v,
const std::string &  other 
)

Check if a vector contains an item.

Definition at line 45 of file helpers.hpp.

◆ list_dir()

std::vector<std::string> list_dir ( const std::string &  path,
const std::string &  pattern = ".*" 
)

Return the list of files matching a pattern from a given directory.

Parameters
pathwhere to look for files
patternwhich pattern to match (regex needed)

Definition at line 132 of file helpers.hpp.

◆ operator<<()

template<typename T >
std::ostream& operator<< ( std::ostream &  os,
const std::vector< T > &  v 
)

Display an std::vector object.

Definition at line 32 of file helpers.hpp.

◆ parseString() [1/2]

template<typename T >
T parseString ( const char *  txt)

Parse a char* into a numeric value.

Template Parameters
Tthe output type
Parameters
txtthe string to parse
Returns
T the value as a T type

Definition at line 227 of file helpers.hpp.

◆ parseString() [2/2]

template<typename T >
T parseString ( const std::string &  txt)

Parse a std::string into a numeric value.

Template Parameters
Tthe output type
Parameters
txtthe string to parse
Returns
T the value as a T type

Definition at line 240 of file helpers.hpp.

◆ split_string()

std::vector<std::string> split_string ( const std::string &  text,
const std::string &  delimiter 
)

Split string into bits according to a given delimiter.

Parameters
textoriginal string
delimiterstring to cut on
Returns
a vector of strings

Definition at line 161 of file helpers.hpp.

◆ string_sprintf()

template<typename... Args>
std::string string_sprintf ( const char *  format,
Args...  args 
)

Helper to make a string with formatting options.

Note that c++20 has std::format in the <format> library

Parameters
formatformatting string similar to printf
argsformatting arguments
Returns
the formatted string

Definition at line 207 of file helpers.hpp.

◆ tolower()

std::string tolower ( std::string  s)

Returns the lower case of a string.

Definition at line 190 of file helpers.hpp.

◆ toupper()

std::string toupper ( std::string  s)

Returns the upper case of a string.

Definition at line 180 of file helpers.hpp.