gu_util
|
#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>
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... | |
#define __block _xblock |
Definition at line 42 of file gu_util.cpp.
#define __block __attribute__((__blocks__(byref))) |
Definition at line 42 of file gu_util.cpp.
#define _POSIX_SOURCE 200112L |
Definition at line 20 of file gu_util.cpp.
#define _XOPEN_SOURCE |
Definition at line 23 of file gu_util.cpp.
#define WHITESPACE " \t\v\r\n" |
Definition at line 303 of file gu_util.cpp.
vector< string > components_of_string_separated | ( | const std::string & | str, |
char | sep = '\n' , |
||
bool | trim = false |
||
) |
Split a string based on the token.
str | The string to split |
sep | Token separator character |
trim | Boolean to specify whether the result should be trimmed |
Definition at line 348 of file gu_util.cpp.
References gu_trim().
Referenced by read_configuration().
char * concatenate_path | ( | const char * | head, |
const char * | tail | ||
) |
Create a new string by concatenating two path components.
head | parent directory |
tail | child path component |
Definition at line 125 of file gu_util.cpp.
References gu_strdup(), and string_by_concatenating_path_components().
int dec | ( | void * | num | ) |
Decrement a number Used to decrement a enum value without warnings.
num | A pointer to the number to decrement |
Definition at line 395 of file gu_util.cpp.
double double_from_file | ( | const char * | fileName | ) |
Load the contents of a given file into a double.
fileName | Name of the file to load |
Definition at line 98 of file gu_util.cpp.
References new_string_from_file().
bool file_exists | ( | const char * | fileName | ) |
Return whether a file exists.
fileName | Name of the file to test |
Definition at line 54 of file gu_util.cpp.
long long get_utime | ( | void | ) |
Get the current time of day in microseconds.
Definition at line 130 of file gu_util.cpp.
Referenced by protected_usleep().
int getplayernumber | ( | void | ) |
string gu_dtos | ( | double | val | ) |
Convert a double value to a string.
val | Value to convert |
Definition at line 339 of file gu_util.cpp.
const char * gu_getprogname | ( | void | ) |
get the basename of the current program
Definition at line 210 of file gu_util.cpp.
string gu_ltos | ( | long | val | ) |
Convert a long value to a string.
val | Value to convert |
Definition at line 321 of file gu_util.cpp.
char * gu_strdup | ( | const char * | str | ) |
Duplicate a string.
str | original string |
Definition at line 111 of file gu_util.cpp.
Referenced by concatenate_path(), and gu_strtrim().
size_t gu_strlcat | ( | char * | dst, |
const char * | src, | ||
size_t | size | ||
) |
Definition at line 252 of file gu_util.cpp.
size_t gu_strlcpy | ( | char * | dst, |
const char * | src, | ||
size_t | size | ||
) |
Definition at line 245 of file gu_util.cpp.
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)
s | the string to trim |
Definition at line 150 of file gu_util.cpp.
References gu_strdup(), and 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!
s | the string to trim (in place) |
Definition at line 305 of file gu_util.cpp.
References WHITESPACE.
Referenced by components_of_string_separated(), and gu_strtrim().
string gu_ultos | ( | unsigned long | val | ) |
Convert an unsigned long value to a string.
val | Value to convert |
Definition at line 330 of file gu_util.cpp.
int inc | ( | void * | num | ) |
Increment a number Used to increment an enum value without warnings.
num | A pointer to the number to increment |
Definition at line 389 of file gu_util.cpp.
int int_from_file | ( | const char * | fileName | ) |
Load the contents of a given file into an int.
fileName | Name of the file to load |
Definition at line 84 of file gu_util.cpp.
References new_string_from_file().
int mipal_err_file | ( | const char * | filename | ) |
Set the log file for the current module.
filename | name 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 |
Definition at line 158 of file gu_util.cpp.
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)
fmt | printf() style format string |
Definition at line 187 of file gu_util.cpp.
Referenced by read_configuration().
char * new_string_from_file | ( | const char * | fileName | ) |
Load the contents of a given file into a string.
fileName | Name of the file to load |
Definition at line 60 of file gu_util.cpp.
Referenced by double_from_file(), int_from_file(), and string_from_file().
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().
map< string, string > read_configuration | ( | const std::string & | filename | ) |
Read simple key = value
pairs into a c++ map.
filename | Path to configuration file |
Definition at line 363 of file gu_util.cpp.
References components_of_string_separated(), and mipal_warn().
string string_by_concatenating_path_components | ( | const std::string & | head, |
const std::string & | tail | ||
) |
Concatenate two path components into a new string.
head | parent directory |
tail | child path component |
Definition at line 280 of file gu_util.cpp.
Referenced by concatenate_path().
string string_from_file | ( | const char * | fileName | ) |
Load the contents of a given file into a string.
fileName | Name of the file to load |
Definition at line 267 of file gu_util.cpp.
References new_string_from_file().