rtcBase.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009
00003  * Robert Bosch LLC
00004  * Research and Technology Center North America
00005  * Palo Alto, California
00006  *
00007  * All rights reserved.
00008  *
00009  *------------------------------------------------------------------------------
00010  * project ....: Autonomous Technologies
00011  * file .......: rtcBase.cpp
00012  * authors ....: Benjamin Pitzer, Soeren Kammel
00013  * organization: Robert Bosch LLC
00014  * creation ...: 01/21/2009
00015  * modified ...: $Date: 2009-01-21 18:19:16 -0800 (Wed, 21 Jan 2009) $
00016  * changed by .: $Author: benjaminpitzer $
00017  * revision ...: $Revision: 14 $
00018  */
00019 #include "rtc/rtcBase.h"
00020 #include <sys/types.h>
00021 #include <sys/stat.h>
00022 #include <fcntl.h>
00023 
00024 #ifdef _MSC_VER
00025 #else
00026 #include <termios.h>
00027 #include <unistd.h>
00028 #endif
00029 
00030 //== NAMESPACES ================================================================
00031 namespace rtc {
00032 
00033 static FILE *rtc_carp_output = NULL;
00034 static int rtc_carp_verbose = 1;
00035 
00036 void
00037 rtc_perror(const char* fmt, ...) {
00038   va_list args;
00039 
00040   if (rtc_carp_output == NULL)
00041     rtc_carp_output = stderr;
00042 
00043   va_start(args, fmt);
00044   vfprintf(rtc_carp_output, fmt, args);
00045   va_end(args);
00046   fprintf(rtc_carp_output, "\n");
00047   fprintf(rtc_carp_output, " : %s\n", strerror(errno));
00048   fflush(rtc_carp_output);
00049 }
00050 
00051 void
00052 rtc_die(const char* fmt, ...) {
00053   va_list args;
00054 
00055   if (rtc_carp_output == NULL)
00056     rtc_carp_output = stderr;
00057 
00058   va_start(args, fmt);
00059   vfprintf(rtc_carp_output, fmt, args);
00060   va_end(args);
00061   fprintf(rtc_carp_output, "\n");
00062   fflush(rtc_carp_output);
00063   exit(-1);
00064 }
00065 
00066 void
00067 rtc_die_syserror(const char* fmt, ...) {
00068   va_list args;
00069 
00070   if (rtc_carp_output == NULL)
00071     rtc_carp_output = stderr;
00072 
00073   va_start(args, fmt);
00074   vfprintf(rtc_carp_output, fmt, args);
00075   va_end(args);
00076   fprintf(rtc_carp_output, "\n");
00077   fprintf(rtc_carp_output, " : %s\n", strerror(errno));
00078   fflush(rtc_carp_output);
00079 
00080   exit(-1);
00081 }
00082 
00083 void
00084 rtc_warn(const char* fmt, ...) {
00085   va_list args;
00086 
00087   if (rtc_carp_output == NULL)
00088     rtc_carp_output = stderr;
00089 
00090   va_start(args, fmt);
00091   vfprintf(rtc_carp_output, fmt, args);
00092   va_end(args);
00093   fprintf(rtc_carp_output, "\n");
00094   fflush(rtc_carp_output);
00095 }
00096 
00097 void
00098 rtc_verbose(const char *fmt, ...) {
00099   va_list args;
00100 
00101   if (!rtc_carp_verbose)
00102     return;
00103 
00104   if (rtc_carp_output == NULL)
00105     rtc_carp_output = stderr;
00106 
00107   va_start(args, fmt);
00108   vfprintf(rtc_carp_output, fmt, args);
00109   va_end(args);
00110   fprintf(rtc_carp_output, "\n");
00111   fflush(rtc_carp_output);
00112 }
00113 
00114 void
00115 rtc_carp_set_output(FILE *output) {
00116   rtc_carp_output = output;
00117 }
00118 
00119 void
00120 rtc_carp_set_verbose(int verbosity) {
00121   rtc_carp_verbose = verbosity;
00122 }
00123 
00124 int
00125 rtc_carp_get_verbose(void) {
00126   return rtc_carp_verbose;
00127 }
00128 
00129 bool rtc_find_commandline_parameter(int argc, char **argv, const char* param, char* param_value) {
00130   int i;
00131 
00132   if (argc < 2)
00133     return false;
00134 
00135   for (i = 1; i < argc-1; i++) {
00136     if (argv[i][0] == '-') {
00137       if (!strcmp(argv[i]+1, param)) {
00138         strcpy(param_value, argv[i+1]);
00139         return true;
00140       }
00141     }
00142   }
00143 
00144   return false;
00145 }
00146 
00147 bool rtc_find_commandline_parameter(int argc, char **argv, const char* param) {
00148   int i;
00149 
00150   if (argc < 2)
00151     return false;
00152 
00153   for (i = 1; i < argc; i++) {
00154     if (argv[i][0] == '-') {
00155       if (!strcmp(argv[i]+1, param)) {
00156         return true;
00157       }
00158     }
00159   }
00160 
00161   return false;
00162 }
00163 
00164 /* This function displays a progress bar
00165    It uses a 1:2 scales. */
00166 void rtc_progress_bar_print(double current, double total)
00167 {
00168   int i;
00169   int per_tot, per_cur, per_tot_scl, per_cur_scl;
00170 
00171   per_tot = 100;
00172   /* total percentage */
00173   if (total==0.0) total=1;
00174   per_cur = (per_tot * current) / total;
00175   /* current percentage */
00176 
00177   /* 1:2 scale */
00178   per_tot_scl = per_tot / 2;
00179   per_cur_scl = per_cur / 2;
00180 
00181   /* Refresh ther bar indicator */
00182   printf("\r [");
00183 
00184   /* Display the bar indicator:
00185      - =   : indicates a part the bar
00186      - >   : indicates the end of the bar
00187              (if it's finish, convert '>' to '='*/
00188   for (i = 0; i < per_cur_scl; i++)
00189     if ((i + 1) == per_cur_scl)
00190       if (per_cur_scl == 50)
00191         putchar('=');
00192       else
00193         putchar('>');
00194     else
00195       putchar('=');
00196 
00197   for (i = per_cur_scl; i < per_tot_scl; i++)
00198     putchar(' ');
00199 
00200   printf("] %3d%% | ", per_cur);
00201   fflush(stdout);
00202 }
00203 
00204 //==============================================================================
00205 } // namespace rtc
00206 //==============================================================================


rtc
Author(s): Benjamin Pitzer
autogenerated on Thu Jan 2 2014 11:04:53