-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.cc
39 lines (32 loc) · 845 Bytes
/
utils.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <sstream>
#include <stdexcept>
#include <cassert>
// Select GNU basename() on Linux
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <limits.h>
#include <libgen.h>
#include <string.h>
#include <stdlib.h>
#include "utils.h"
std::string extract_basename(const std::string& path)
{
char* const temp_s = strdup(path.c_str());
std::string result(basename(temp_s));
free(temp_s);
return result;
}
void raise_from_system_error_code(const std::string& user_message, int err)
{
std::ostringstream sts;
if (user_message.size() > 0) {
sts << user_message << ' ';
}
assert(0 != err);
throw std::system_error(std::error_code(err, std::system_category()), sts.str().c_str());
}
void raise_from_errno(const std::string& user_message)
{
raise_from_system_error_code(user_message, errno);
}