ivutils
Loading...
Searching...
No Matches
string_utils.h File Reference

Some usefull functions for work with strings. More...

#include <string.h>
#include <string>
#include <vector>
#include <iterator>
#include "logexc.h"
Include dependency graph for string_utils.h:
This graph shows which files directly or indirectly include this file:

Classes

struct  av_comm_t
 struct for use in average_files More...
 

Functions

std::string my_substr (const std::string &str, size_t pos, size_t n)
 calculate substring of string str similar to string::substr used because at some architectures string::substr is unstable
 
void trim (std::string &str)
 delete spaces and tabulation at the beginning and the end of the string
 
const char * val2str (int val)
 returns string corresponding to value
 
void str2val (const char *s, double &a)
 extract integer number from string
 
void str2val (const char *s, float &a)
 extract double number from string
 
void str2val (const char *s, std::string &a)
 extract double number from string
 
std::vector< std::string > separate_list (std::string s, const std::string d=",", int single_char=0)
 just copy string
 
template<class T1 , class T2 , class T3 , class T4 , class T5 , class T6 , class T7 , class T8 , class T9 >
void extract_list (std::string s, T1 *a1, T2 *a2, T3 *a3, T4 *a4, T5 *a5, T6 *a6, T7 *a7, T8 *a8, T9 *a9)
 separates list of strings s to individual strings, which are estimated using function ato and saved in variables a1, a2, ...
 
std::string size_in_bytes (long long bytes)
 returns string with used amount of bytes (if bytes<1024), Kb (if bytes<1024*1024) Mb (if bytes<1024^3) or Gb (otherwise)
 
std::string stime (int sec)
 returns string in format hours:minutes:seconds
 
char rot4 (int rot)
 returns rotating stroke |, /, -, \ corresponding to value rot%4
 
void correct_directory_name (std::string &dir)
 adds / (UNIX) or \ (Windows) to the directory path
 
bool check_directory (const std::string &dir)
 checks if directory exists
 
bool abs_path (const std::string &path)
 checks if path is absolute
 
FILE * open_file (const char *fname, const char *mode, const std::string &directory="")
 Same as fopen() but prepends directory name (if not NULL) to the file name before opening.
 
template<class T >
int read_table (const char *fname, T **ptr, int &N1, int &N2, int column_raw=1, int skip_first=0)
 reads table from file to array *ptr records number of raws to N1 and number of columns to N2.
 
template<class T >
int write_table (const char *fname, T *ptr, int N1, int N2, int column_raw=1, const char *first=NULL, int gnu_space=-1)
 records array ptr to the file first is first text raw
 
template<class T >
get_table_value (T arg, T *ptr, int N1, int N2, int j_arg, int j_val, int column_raw=1, int ord=1)
 get interpolated value from table arg is argument, data corresponding to arrument is in j_arg column j_arg is column with values
 
template<class T >
int table_grid (T *ptr, int N1, int N2, int arg_num, const int *arg, T *p1, T *p2, int *sz, int *ord, int column_raw=1)
 ptr is table in format x - y - z - some-variables (less than 3 coordinates is possible, for example just x - y) arg corresponds to columns numbers for coordinates x,y,z function calculates grid parameters p1 (left lower vertex), p2 (right top point), size sz and order ord for coordinates, from fastest to slowest
 
template<class filename_it >
int average_files (filename_it beg, filename_it end, const char *output)
 avrerages the contents of space-separated text files containing numbers into the output maximal number of columns: 100 maxinal line length: 10000 symbols '#' symbols are treated as comments and transferred to output from the first valid file in sequence

 
template<class arg_it , class func_it >
int write_ascii (const char *fname, arg_it abeg, arg_it aend, func_it fbeg, func_it fend, const char *header="", const char *arg_format="%g", const char *func_format="%g", const char *tab=" ", const char *mode="wt", const char *tailer="")
 Writes a set of functions given by [fbeg, fend) into a multicolumn ASCII file evaluated over a set of arguments [abeg, aend).
 
std::vector< std::string > split (const std::string &text, const std::string &delims)
 Spliting a string text delimited by any char of delims into separate substrings.
 

Detailed Description

Some usefull functions for work with strings.

Function Documentation

◆ read_table()

template<class T >
int read_table ( const char *  fname,
T **  ptr,
int &  N1,
int &  N2,
int  column_raw = 1,
int  skip_first = 0 
)

reads table from file to array *ptr records number of raws to N1 and number of columns to N2.

if column_raw, numbers are recorded to ptr corresponding their order in file (fastest index corresponds to raws), otherwise fastest index corresponds to columns if skip_first then first ras is ignored (it can be used if this is text raw)

◆ separate_list()

std::vector< std::string > separate_list ( std::string  s,
const std::string  d = ",",
int  single_char = 0 
)

just copy string

separate list of strings s to individual strings which will be put into the vector if single_char=0 then d is separator used in list of strings s if single_char=1 then d is string that contains all possible separators, and each separator can be only one character f. e. separate_list("one,two",",",0) returns ("one","two") f. e. separate_list("one,two","e,t",0) returns ("on","wo") f. e. separate_list("one,two.tree",".,",0) returns () f. e. separate_list("one,two.tree",".,",1) returns ("one","two,"three")

◆ split()

std::vector< std::string > split ( const std::string &  text,
const std::string &  delims 
)

Spliting a string text delimited by any char of delims into separate substrings.

Returns
a vector of substings

◆ write_ascii()

template<class arg_it , class func_it >
int write_ascii ( const char *  fname,
arg_it  abeg,
arg_it  aend,
func_it  fbeg,
func_it  fend,
const char *  header = "",
const char *  arg_format = "%g",
const char *  func_format = "%g",
const char *  tab = " ",
const char *  mode = "wt",
const char *  tailer = "" 
)

Writes a set of functions given by [fbeg, fend) into a multicolumn ASCII file evaluated over a set of arguments [abeg, aend).

Arguments and functions must be compatible with the printf formats supplied in arg_format and func_format respectively. Arguments are printed in the first column, f(arg) values in the next columns. Header is printed at the beginning, tailer at end of file (with no extra
after them). Mode mode is supplied to fopen second argument (can be "wt" or "at").

Returns
>0 if OK.