librostlab  1.0.20
rostlab/rostlab_stdlib.h
Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2011 Laszlo Kajan, Technical University of Munich, Germany
00003 
00004     This file is part of librostlab.
00005 
00006     librostlab is free software: you can redistribute it and/or modify
00007     it under the terms of the GNU Lesser General Public License as published by
00008     the Free Software Foundation, either version 3 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU Lesser General Public License for more details.
00015 
00016     You should have received a copy of the GNU Lesser General Public License
00017     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 #ifndef ROSTLAB_STDLIB
00020 #define ROSTLAB_STDLIB 1
00021 
00022 #include <errno.h>
00023 #include <stdexcept>
00024 #include <stdarg.h>
00025 #include <stdlib.h>
00026 #include <string>
00027 #include <sstream>
00028 #include <string.h>
00029 #include <sys/stat.h>
00030 #include <sys/types.h>
00031 #include <sys/wait.h>
00032 #include <unistd.h>
00033 #include <vector>
00034 
00035 namespace rostlab {
00036 
00037 typedef std::vector<std::string>
00038                   argvec_type;
00039 
00040 inline argvec_type
00041                   mkargvec( const char* __path, ... );
00042 inline bool       file_exists( const std::string& __path );
00043 inline int        system( const char* __path, ... );
00044 inline int        system( const std::vector<std::string>& __args );
00045 inline std::string     tolower( std::string __str );
00046 
00047 
00048 inline bool       file_exists( const std::string& __path )
00049 {
00050   struct stat buf;
00051   if( stat( __path.c_str(), &buf ) ) return false;
00052   return true;
00053 }
00054 
00055 
00056 inline argvec_type
00057                   mkargvec( const char* __path, ... )
00058 {
00059   std::vector<std::string>  argvec;
00060   argvec.push_back( __path );
00061 
00062   va_list listPointer;
00063   va_start( listPointer, __path );
00064 
00065   char* arg;
00066   while( ( arg = va_arg( listPointer, char* ) ) != NULL )
00067   {
00068     argvec.push_back( arg );
00069   }
00070   va_end( listPointer );
00071 
00072   return argvec;
00073 }
00074 
00075 
00076 inline int        system( const char* __path, ... )
00077 {
00078   std::vector<std::string>  argvec;
00079   argvec.push_back( __path );
00080 
00081   va_list listPointer;
00082   va_start( listPointer, __path );
00083 
00084   char* arg;
00085   while( ( arg = va_arg( listPointer, char* ) ) != NULL )
00086   {
00087     argvec.push_back( arg );
00088   }
00089   va_end( listPointer );
00090 
00091   return system( argvec );
00092 }
00093 
00094 inline int        system( const std::vector<std::string>& __args )
00095 {
00096   pid_t pid = fork();
00097   if( pid == -1 ) throw runtime_error( strerror( errno ) );
00098   if( !pid )
00099   {
00100     char* argv[ __args.size()+1 ];
00101     for( size_t i = 0; i < __args.size(); ++i ) argv[i] = const_cast<char*>( __args[i].c_str() );
00102     argv[__args.size()] = NULL;
00103 
00104     if( execvp( argv[0], argv ) ) throw runtime_error( strerror( errno ) );
00105     exit(0);
00106   }
00107   int status;
00108   if( !waitpid( pid, &status, 0 ) ) throw runtime_error( strerror( errno ) );
00109   if( !WIFEXITED(status) ) throw runtime_error( "child exited abnormally" );
00110   return status;
00111 }
00112 
00113 
00114 inline std::string     tolower( std::string __str )
00115 {
00116   for( std::string::iterator s_i = __str.begin(); s_i != __str.end(); ++s_i ) *s_i = ::tolower( *s_i );
00117   return __str;
00118 }
00119 
00120 } // namespace rostlab
00121 
00122 #endif // ROSTLAB_STDLIB
00123 // vim:et:ts=4:ai:
 All Classes Namespaces Files Functions Variables Typedefs