gu_util
Macros | Functions
gu_util.cpp File Reference
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
#include <unistd.h>
#include <libgen.h>
#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <stdio.h>
#include "gu_util.h"
#include <sstream>
#include <map>
#include <fstream>
Include dependency graph for gu_util.cpp:

Go to the source code of this file.

Macros

#define _POSIX_SOURCE   200112L
 
#define _XOPEN_SOURCE
 
#define __block   _xblock
 
#define __block   __attribute__((__blocks__(byref)))
 
#define WHITESPACE   " \t\v\r\n"
 

Functions

bool file_exists (const char *fileName)
 Return whether a file exists. More...
 
char * new_string_from_file (const char *fileName)
 Load the contents of a given file into a string. More...
 
int int_from_file (const char *fileName)
 Load the contents of a given file into an int. More...
 
double double_from_file (const char *fileName)
 Load the contents of a given file into a double. More...
 
char * gu_strdup (const char *s)
 Duplicate a string. More...
 
char * concatenate_path (const char *head, const char *tail)
 Create a new string by concatenating two path components. More...
 
long long get_utime (void)
 Get the current time of day in microseconds. More...
 
void protected_usleep (long long us)
 Protected usleep() – guaranteed to sleep for the given time! param us number of microseconds to sleep. More...
 
char * gu_strtrim (const char *s)
 Return a trimmed copy of the current string by removing all leading and trailing whitespace characters (needs to be free()d) More...
 
int mipal_err_file (const char *filename)
 Set the log file for the current module. More...
 
void mipal_warn (const char *fmt,...)
 Print a warning to the file mipal_err_file() has been set for (or stderr if none has been set) More...
 
const char * gu_getprogname ()
 get the basename of the current program More...
 
int getplayernumber ()
 get player number More...
 
size_t gu_strlcpy (char *dst, const char *src, size_t size)
 
size_t gu_strlcat (char *dst, const char *src, size_t size)
 
string string_from_file (const char *fileName)
 Load the contents of a given file into a string. More...
 
string string_by_concatenating_path_components (const string &h, const string &t)
 Concatenate two path components into a new string. More...
 
string & gu_trim (string &s)
 Trim whitespace characters from both ends of a string. More...
 
string gu_ltos (long val)
 Convert a long value to a string. More...
 
string gu_ultos (unsigned long val)
 Convert an unsigned long value to a string. More...
 
string gu_dtos (double val)
 Convert a double value to a string. More...
 
vector< string > components_of_string_separated (const string &str, char sep, bool trim)
 Split a string based on the token. More...
 
map< string, string > read_configuration (const string &filename)
 Read simple key = value pairs into a c++ map. More...
 
int inc (void *num)
 Increment a number Used to increment an enum value without warnings. More...
 
int dec (void *num)
 Decrement a number Used to decrement a enum value without warnings. More...
 

Macro Definition Documentation

◆ __block [1/2]

#define __block   _xblock

Definition at line 42 of file gu_util.cpp.

◆ __block [2/2]

#define __block   __attribute__((__blocks__(byref)))

Definition at line 42 of file gu_util.cpp.

◆ _POSIX_SOURCE

#define _POSIX_SOURCE   200112L

Definition at line 20 of file gu_util.cpp.

◆ _XOPEN_SOURCE

#define _XOPEN_SOURCE

Definition at line 23 of file gu_util.cpp.

◆ WHITESPACE

#define WHITESPACE   " \t\v\r\n"

Definition at line 303 of file gu_util.cpp.

Function Documentation

◆ components_of_string_separated()

vector< string > components_of_string_separated ( const std::string &  str,
char  sep = '\n',
bool  trim = false 
)

Split a string based on the token.

Parameters
strThe string to split
sepToken separator character
trimBoolean to specify whether the result should be trimmed
Returns
array of strings split by token

Definition at line 348 of file gu_util.cpp.

References gu_trim().

Referenced by read_configuration().

◆ concatenate_path()

char * concatenate_path ( const char *  head,
const char *  tail 
)

Create a new string by concatenating two path components.

Parameters
headparent directory
tailchild path component
Returns
char * with the concatenated path (needs to be free()d)

Definition at line 125 of file gu_util.cpp.

References gu_strdup(), and string_by_concatenating_path_components().

◆ dec()

int dec ( void *  num)

Decrement a number Used to decrement a enum value without warnings.

Parameters
numA pointer to the number to decrement
Returns
the new value of the number

Definition at line 395 of file gu_util.cpp.

◆ double_from_file()

double double_from_file ( const char *  fileName)

Load the contents of a given file into a double.

Parameters
fileNameName of the file to load
Returns
double with the contents of the file

Definition at line 98 of file gu_util.cpp.

References new_string_from_file().

◆ file_exists()

bool file_exists ( const char *  fileName)

Return whether a file exists.

Parameters
fileNameName of the file to test
Returns
true if the file exists, false otherwise

Definition at line 54 of file gu_util.cpp.

◆ get_utime()

long long get_utime ( void  )

Get the current time of day in microseconds.

Returns
long long with the current time in us (-1LL in case of error)

Definition at line 130 of file gu_util.cpp.

Referenced by protected_usleep().

◆ getplayernumber()

int getplayernumber ( void  )

get player number

Returns
int player number

Definition at line 228 of file gu_util.cpp.

◆ gu_dtos()

string gu_dtos ( double  val)

Convert a double value to a string.

Parameters
valValue to convert
Returns
string with contens value

Definition at line 339 of file gu_util.cpp.

◆ gu_getprogname()

const char * gu_getprogname ( void  )

get the basename of the current program

Returns
const char * program name

Definition at line 210 of file gu_util.cpp.

◆ gu_ltos()

string gu_ltos ( long  val)

Convert a long value to a string.

Parameters
valValue to convert
Returns
string with contens value

Definition at line 321 of file gu_util.cpp.

◆ gu_strdup()

char * gu_strdup ( const char *  str)

Duplicate a string.

Parameters
stroriginal string
Returns
char * duplicated string (needs to be free()d)

Definition at line 111 of file gu_util.cpp.

Referenced by concatenate_path(), and gu_strtrim().

◆ gu_strlcat()

size_t gu_strlcat ( char *  dst,
const char *  src,
size_t  size 
)

Definition at line 252 of file gu_util.cpp.

◆ gu_strlcpy()

size_t gu_strlcpy ( char *  dst,
const char *  src,
size_t  size 
)

Definition at line 245 of file gu_util.cpp.

◆ gu_strtrim()

char * gu_strtrim ( const char *  s)

Return a trimmed copy of the current string by removing all leading and trailing whitespace characters (needs to be free()d)

Parameters
sthe string to trim
Returns
new string void of leading

Definition at line 150 of file gu_util.cpp.

References gu_strdup(), and gu_trim().

◆ gu_trim()

string & gu_trim ( std::string &  s)

Trim whitespace characters from both ends of a string.

This operation is in-place, so the original string gets modified!

Parameters
sthe string to trim (in place)
Returns
reference to s

Definition at line 305 of file gu_util.cpp.

References WHITESPACE.

Referenced by components_of_string_separated(), and gu_strtrim().

◆ gu_ultos()

string gu_ultos ( unsigned long  val)

Convert an unsigned long value to a string.

Parameters
valValue to convert
Returns
string with contens value

Definition at line 330 of file gu_util.cpp.

◆ inc()

int inc ( void *  num)

Increment a number Used to increment an enum value without warnings.

Parameters
numA pointer to the number to increment
Returns
the new value of the number

Definition at line 389 of file gu_util.cpp.

◆ int_from_file()

int int_from_file ( const char *  fileName)

Load the contents of a given file into an int.

Parameters
fileNameName of the file to load
Returns
int with the contents of the file

Definition at line 84 of file gu_util.cpp.

References new_string_from_file().

◆ mipal_err_file()

int mipal_err_file ( const char *  filename)

Set the log file for the current module.

Parameters
filenamename of the file to open. If the file contains a slash '/', it will create the directory the file should go in with mode 01777 if non-existent
Returns
-1 in case of errors, 0 in case of success

Definition at line 158 of file gu_util.cpp.

◆ mipal_warn()

void mipal_warn ( const char *  fmt,
  ... 
)

Print a warning to the file mipal_err_file() has been set for (or stderr if none has been set)

Parameters
fmtprintf() style format string

Definition at line 187 of file gu_util.cpp.

Referenced by read_configuration().

◆ new_string_from_file()

char * new_string_from_file ( const char *  fileName)

Load the contents of a given file into a string.

Parameters
fileNameName of the file to load
Returns
string with contens of file

Definition at line 60 of file gu_util.cpp.

Referenced by double_from_file(), int_from_file(), and string_from_file().

◆ protected_usleep()

void protected_usleep ( long long  us)

Protected usleep() – guaranteed to sleep for the given time! param us number of microseconds to sleep.

Definition at line 140 of file gu_util.cpp.

References get_utime().

◆ read_configuration()

map< string, string > read_configuration ( const std::string &  filename)

Read simple key = value pairs into a c++ map.

Parameters
filenamePath to configuration file
Returns
map<string, string> of key/value string pairs

Definition at line 363 of file gu_util.cpp.

References components_of_string_separated(), and mipal_warn().

◆ string_by_concatenating_path_components()

string string_by_concatenating_path_components ( const std::string &  head,
const std::string &  tail 
)

Concatenate two path components into a new string.

Parameters
headparent directory
tailchild path component
Returns
string with the concatenated path

Definition at line 280 of file gu_util.cpp.

Referenced by concatenate_path().

◆ string_from_file()

string string_from_file ( const char *  fileName)

Load the contents of a given file into a string.

Parameters
fileNameName of the file to load
Returns
string with contens of file

Definition at line 267 of file gu_util.cpp.

References new_string_from_file().