File: rostlab_stdlib.h

package info (click to toggle)
librostlab 1.0.20-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,616 kB
  • sloc: sh: 10,131; cpp: 826; makefile: 109
file content (123 lines) | stat: -rw-r--r-- 3,193 bytes parent folder | download | duplicates (7)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
    Copyright (C) 2011 Laszlo Kajan, Technical University of Munich, Germany

    This file is part of librostlab.

    librostlab is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ROSTLAB_STDLIB
#define ROSTLAB_STDLIB 1

#include <errno.h>
#include <stdexcept>
#include <stdarg.h>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <vector>

namespace rostlab {

typedef std::vector<std::string>
                  argvec_type;

inline argvec_type
                  mkargvec( const char* __path, ... );
inline bool       file_exists( const std::string& __path );
inline int        system( const char* __path, ... );
inline int        system( const std::vector<std::string>& __args );
inline std::string     tolower( std::string __str );


inline bool       file_exists( const std::string& __path )
{
  struct stat buf;
  if( stat( __path.c_str(), &buf ) ) return false;
  return true;
}


inline argvec_type
                  mkargvec( const char* __path, ... )
{
  std::vector<std::string>  argvec;
  argvec.push_back( __path );

  va_list listPointer;
  va_start( listPointer, __path );

  char* arg;
  while( ( arg = va_arg( listPointer, char* ) ) != NULL )
  {
    argvec.push_back( arg );
  }
  va_end( listPointer );

  return argvec;
}


inline int        system( const char* __path, ... )
{
  std::vector<std::string>  argvec;
  argvec.push_back( __path );

  va_list listPointer;
  va_start( listPointer, __path );

  char* arg;
  while( ( arg = va_arg( listPointer, char* ) ) != NULL )
  {
    argvec.push_back( arg );
  }
  va_end( listPointer );

  return system( argvec );
}

inline int        system( const std::vector<std::string>& __args )
{
  pid_t pid = fork();
  if( pid == -1 ) throw runtime_error( strerror( errno ) );
  if( !pid )
  {
    char* argv[ __args.size()+1 ];
    for( size_t i = 0; i < __args.size(); ++i ) argv[i] = const_cast<char*>( __args[i].c_str() );
    argv[__args.size()] = NULL;

    if( execvp( argv[0], argv ) ) throw runtime_error( strerror( errno ) );
    exit(0);
  }
  int status;
  if( !waitpid( pid, &status, 0 ) ) throw runtime_error( strerror( errno ) );
  if( !WIFEXITED(status) ) throw runtime_error( "child exited abnormally" );
  return status;
}


inline std::string     tolower( std::string __str )
{
  for( std::string::iterator s_i = __str.begin(); s_i != __str.end(); ++s_i ) *s_i = ::tolower( *s_i );
  return __str;
}

} // namespace rostlab

#endif // ROSTLAB_STDLIB
// vim:et:ts=4:ai: