librostlab
1.0.20
Toggle main menu visibility
Loading...
Searching...
No Matches
rostlab
rostlab_stdlib.h
Go to the documentation of this file.
1
/*
2
Copyright (C) 2011 Laszlo Kajan, Technical University of Munich, Germany
3
4
This file is part of librostlab.
5
6
librostlab is free software: you can redistribute it and/or modify
7
it under the terms of the GNU Lesser General Public License as published by
8
the Free Software Foundation, either version 3 of the License, or
9
(at your option) any later version.
10
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU Lesser General Public License for more details.
15
16
You should have received a copy of the GNU Lesser General Public License
17
along with this program. If not, see <http://www.gnu.org/licenses/>.
18
*/
19
#ifndef ROSTLAB_STDLIB
20
#define ROSTLAB_STDLIB 1
21
22
#include <errno.h>
23
#include <stdexcept>
24
#include <stdarg.h>
25
#include <stdlib.h>
26
#include <string>
27
#include <sstream>
28
#include <string.h>
29
#include <sys/stat.h>
30
#include <sys/types.h>
31
#include <sys/wait.h>
32
#include <unistd.h>
33
#include <vector>
34
35
namespace
rostlab
{
36
37
typedef
std::vector<std::string>
38
argvec_type
;
39
40
inline
argvec_type
41
mkargvec
(
const
char
* __path, ... );
42
inline
bool
file_exists
(
const
std::string& __path );
43
inline
int
system
(
const
char
* __path, ... );
44
inline
int
system
(
const
std::vector<std::string>& __args );
45
inline
std::string
tolower
( std::string __str );
46
47
48
inline
bool
file_exists
(
const
std::string& __path )
49
{
50
struct
stat buf;
51
if
( stat( __path.c_str(), &buf ) )
return
false
;
52
return
true
;
53
}
54
55
56
inline
argvec_type
57
mkargvec
(
const
char
* __path, ... )
58
{
59
std::vector<std::string> argvec;
60
argvec.push_back( __path );
61
62
va_list listPointer;
63
va_start( listPointer, __path );
64
65
char
* arg;
66
while
( ( arg = va_arg( listPointer,
char
* ) ) != NULL )
67
{
68
argvec.push_back( arg );
69
}
70
va_end( listPointer );
71
72
return
argvec;
73
}
74
75
76
inline
int
system
(
const
char
* __path, ... )
77
{
78
std::vector<std::string> argvec;
79
argvec.push_back( __path );
80
81
va_list listPointer;
82
va_start( listPointer, __path );
83
84
char
* arg;
85
while
( ( arg = va_arg( listPointer,
char
* ) ) != NULL )
86
{
87
argvec.push_back( arg );
88
}
89
va_end( listPointer );
90
91
return
system
( argvec );
92
}
93
94
inline
int
system
(
const
std::vector<std::string>& __args )
95
{
96
pid_t pid = fork();
97
if
( pid == -1 )
throw
runtime_error
( strerror( errno ) );
98
if
( !pid )
99
{
100
char
* argv[ __args.size()+1 ];
101
for
(
size_t
i = 0; i < __args.size(); ++i ) argv[i] =
const_cast<
char
*
>
( __args[i].c_str() );
102
argv[__args.size()] = NULL;
103
104
if
( execvp( argv[0], argv ) )
throw
runtime_error
( strerror( errno ) );
105
exit(0);
106
}
107
int
status;
108
if
( !waitpid( pid, &status, 0 ) )
throw
runtime_error
( strerror( errno ) );
109
if
( !WIFEXITED(status) )
throw
runtime_error
(
"child exited abnormally"
);
110
return
status;
111
}
112
113
114
inline
std::string
tolower
( std::string __str )
115
{
116
for
( std::string::iterator s_i = __str.begin(); s_i != __str.end(); ++s_i ) *s_i =
::tolower
( *s_i );
117
return
__str;
118
}
119
120
}
// namespace rostlab
121
122
#endif
// ROSTLAB_STDLIB
123
// vim:et:ts=4:ai:
rostlab::runtime_error
Definition
rostlab_stdexcept.h:69
rostlab
Definition
aux_functions.h:27
rostlab::tolower
std::string tolower(std::string __str)
Definition
rostlab_stdlib.h:114
rostlab::mkargvec
argvec_type mkargvec(const char *__path,...)
Definition
rostlab_stdlib.h:57
rostlab::file_exists
bool file_exists(const std::string &__path)
Definition
rostlab_stdlib.h:48
rostlab::system
int system(const char *__path,...)
Definition
rostlab_stdlib.h:76
rostlab::argvec_type
std::vector< std::string > argvec_type
Definition
rostlab_stdlib.h:38
Generated on
for librostlab by
1.17.0