11#pragma clang diagnostic push
12#pragma clang diagnostic ignored "-Wunused-macros"
13#pragma clang diagnostic ignored "-Wreserved-id-macro"
14#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
15#pragma clang diagnostic ignored "-Wold-style-cast"
16#pragma clang diagnostic ignored "-Wdeprecated"
17#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
20#define _POSIX_SOURCE 200112L
26#ifndef _DARWIN_C_SOURCE
27#define _DARWIN_C_SOURCE 200112L
28#ifndef __DARWIN_C_LEVEL
29#define __DARWIN_C_LEVEL 200112L
39#define __block _xblock
42#define __block __attribute__((__blocks__(byref)))
57 return stat(fileName, &buf) >= 0;
63 int file = open(fileName, O_RDONLY);
64 if (file < 0)
return NULL;
66 size_t len = (size_t) lseek(file, 0, SEEK_END);
67 if ((content = (
char *) malloc(len+1)))
69 lseek(file, 0, SEEK_SET);
70 if (read(file, content, len) == (
int)len)
115 if (s) n = unsigned(strlen(s))+1;
117 char *dest =
static_cast<char *
>(calloc(1, n));
119 if (s && dest) strcpy(dest, s);
133 if (gettimeofday(&tv, NULL) == -1)
return -1LL;
135 long long t = (
long long) tv.tv_usec +
136 (
long long) tv.tv_sec * 1000000LL;
145 usleep(
unsigned(us));
156 static FILE *warn_file;
160 if (warn_file) fclose(warn_file);
162 if (strchr(filename,
'/'))
do
172 char *dir = dirname(f_copy);
179 warn_file = fopen(filename,
"a");
181 return warn_file ? 0 : -1;
184#pragma clang diagnostic push
185#pragma clang diagnostic ignored "-Wformat-nonliteral"
190 FILE *file = warn_file ? warn_file : stderr;
193 time_t t = time(NULL);
194 const struct tm *btm = localtime(&t);
197 strftime(buf,
sizeof(buf),
"%Y-%m-%d %H:%M:%S", btm);
199 vfprintf(file, fmt, ap);
205#pragma clang diagnostic pop
208 static char *prog_name;
213 return getprogname();
221 prog_name = (
char *) calloc(len, 1);
222 snprintf(prog_name, len,
"pid %d", getpid());
224 return basename(prog_name);
230 char path[30] =
"/home/nao/data/player";
232 int fd = open(path, O_RDONLY);
238 ssize_t t = read(fd, &num, 1);
239 if (t <= 0)
return 0;
240 return atoi(&num[0]);
244#if !defined(BSD) || (BSD < 199506)
247 strncpy(dst, src, size--);
254 strncat(dst, src, size--);
282 const char *head = h.c_str();
283 const char *tail = t.c_str();
284 std::string path(head);
285 size_t len = path.length();
289 if (head[len-1] ==
'/')
291 if (tail[0] ==
'/') tail++;
295 if (tail[0] !=
'/') path +=
"/";
303#define WHITESPACE " \t\v\r\n"
307 string::size_type pos = s.find_last_not_of(
WHITESPACE);
308 if (pos != string::npos)
312 if (pos != string::npos)
315 else s.erase(s.begin(), s.end());
324 snprintf(buf,
sizeof(buf),
"%ld", val);
333 snprintf(buf,
sizeof(buf),
"%lu", val);
342 snprintf(buf,
sizeof(buf),
"%lg", val);
350 vector<string> array;
351 istringstream iss(str);
353 while (getline(iss, token, sep))
356 array.push_back(token);
365 map<string,string> cfg;
366 ifstream ifs(filename.c_str(), ifstream::in);
368 mipal_warn(
"Configuration file not found: %s\n", filename.c_str());
373 vector<string> tokens;
374 while (getline(ifs, line)) {
378 if (tokens.size()==2 && tokens[0].size()>0 && tokens[1].size()>0) {
380 if (!(tokens[0][0]==
'#')) {
381 cfg.insert(pair<string, string>(tokens[0], tokens[1]));
400#pragma clang diagnostic pop
char * gu_strdup(const char *s)
Duplicate a string.
const char * gu_getprogname()
get the basename of the current program
string gu_ltos(long val)
Convert a long value to a string.
long long get_utime(void)
Get the current time of day in microseconds.
string & gu_trim(string &s)
Trim whitespace characters from both ends of a string.
int int_from_file(const char *fileName)
Load the contents of a given file into an int.
double double_from_file(const char *fileName)
Load the contents of a given file into a double.
int dec(void *num)
Decrement a number Used to decrement a enum value without warnings.
string string_from_file(const char *fileName)
Load the contents of a given file into a string.
char * new_string_from_file(const char *fileName)
Load the contents of a given file into a string.
string gu_ultos(unsigned long val)
Convert an unsigned long value to a string.
size_t gu_strlcat(char *dst, const char *src, size_t size)
char * gu_strtrim(const char *s)
Return a trimmed copy of the current string by removing all leading and trailing whitespace character...
char * concatenate_path(const char *head, const char *tail)
Create a new string by concatenating two path components.
bool file_exists(const char *fileName)
Return whether a file exists.
int mipal_err_file(const char *filename)
Set the log file for the current module.
void protected_usleep(long long us)
Protected usleep() – guaranteed to sleep for the given time! param us number of microseconds to sleep...
vector< string > components_of_string_separated(const string &str, char sep, bool trim)
Split a string based on the token.
string string_by_concatenating_path_components(const string &h, const string &t)
Concatenate two path components into a new string.
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)
string gu_dtos(double val)
Convert a double value to a string.
size_t gu_strlcpy(char *dst, const char *src, size_t size)
map< string, string > read_configuration(const string &filename)
Read simple key = value pairs into a c++ map.
int getplayernumber()
get player number
int inc(void *num)
Increment a number Used to increment an enum value without warnings.