libidx
/home/rex/ebltrunk/core/libidx/include/utils.h
00001 /***************************************************************************
00002  *   Copyright (C) 2010 by Pierre Sermanet   *
00003  *   pierre.sermanet@gmail.com   *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source 
00014  *       Initiative (http://www.opensource.org) must display the 
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 #ifndef UTILS_H_
00034 #define UTILS_H_
00035 
00036 #ifdef __WINDOWS__
00037 #include <time.h>
00038 #else // linux & mac
00039 #include <sys/time.h>
00040 #include <unistd.h>
00041 #endif
00042 
00043 #include <stdio.h>
00044 #include "defines.h"
00045 #include "stl.h"
00046 
00047 using namespace std;
00048 
00049 namespace ebl {
00050 
00051   #define COLOR_LIST_SIZE 13
00052 
00053   extern IDXEXPORT ubyte color_list[COLOR_LIST_SIZE][3];
00054 
00055   // file IO utilities /////////////////////////////////////////////////////////
00056 
00058   EXPORT int fscan_int(FILE *fp);
00060   EXPORT float fscan_float(FILE *fp);
00063   EXPORT char *fscan_str(FILE *fp);
00064 
00065   // directory utilities ///////////////////////////////////////////////////////
00066 
00069   EXPORT bool mkdir_full(const char *dir);  
00072   EXPORT bool mkdir_full(string &dir);
00074   EXPORT bool dir_exists(const char *s);  
00076   EXPORT bool dir_exists(const std::string &s);  
00078   EXPORT bool file_exists(const char *s);
00080   EXPORT bool file_exists(const std::string &s);
00082   EXPORT uint file_size(const char *s);
00084   EXPORT uint file_size(const std::string &s);
00086   EXPORT time_t file_modified(const char *s);
00088   EXPORT time_t file_modified(const std::string &s);
00091   EXPORT int file_modified_elapsed(const char *s);
00093   EXPORT bool touch_file(const char *s);
00095   EXPORT bool touch_file(const std::string &s);
00097   EXPORT bool rm_file(const char *s);
00099   EXPORT bool mv_file(const char *src, const char *tgt);
00102   EXPORT const char *dirname(const char *fname);  
00107   EXPORT const char *basename(const char *fname, const char *suffix = NULL);    
00110   EXPORT std::string noext_name(const char *fname);
00113   EXPORT std::string ext_name(const char *fname);
00114 
00115   // timing utilities //////////////////////////////////////////////////////////
00116 
00119   EXPORT string tstamp();
00120 
00122   class EXPORT timer {
00123   public:
00124     timer();
00125     virtual ~timer();
00127     void start();
00129     void stop();
00131     void restart();
00133     void reset();
00135     double elapsed_minutes();
00137     long elapsed_seconds();
00139     long elapsed_milliseconds();
00141     long elapsed_microseconds();
00142 
00144     long accumulated_milliseconds();
00145     
00147     void pretty_elapsed();
00149     void pretty_secs(long seconds);
00151     string elapsed(long seconds);
00153     string elapsed();
00156     string elapsed_ms(long milliseconds);
00159     string elapsed_ms();
00162     string accumulated_ms();
00165     string eta(uint n, uint total);
00166     
00167   private:
00168 #ifdef __WINDOWS__
00169 #else // linux & mac
00170     struct timeval t0, t1;
00171 #endif
00172     long total_micros; 
00173     long last_micros; 
00174     bool running; 
00175   };
00176 
00178   EXPORT void millisleep(long millis);  
00180   EXPORT void secsleep(long seconds);
00181 
00182   // process utilities /////////////////////////////////////////////////////////
00183   
00185   EXPORT int pid();
00186   
00187   // TIMING MACROS ////////////////////////////////////////////////
00188 
00189 #ifdef __TIMING__
00190   
00191   extern EXPORT ebl::timer debug_timer1;
00192   extern EXPORT ebl::timer debug_timer2;
00193   extern EXPORT ebl::timer debug_timer_resizing; // timing of resizing
00194   extern EXPORT uint debug_resizing_cnt; // # of resizing
00195 
00196 #define INIT_TIMING()                                   \
00197   ebl::timer debug_timer1;                              \
00198   ebl::timer debug_timer2;                              \
00199   ebl::timer debug_timer_resizing;                      \
00200   uint debug_resizing_cnt;
00201 
00202 #define TIMING(s, t) {                                                  \
00203     std::cout << s << ": " << t.elapsed_ms() << std::endl;              \
00204     t.restart();                                                        \
00205   }
00206 #define TIMINGACC(s, t) {                                               \
00207     std::cout << s << ": " << t.accumulated_ms() << std::endl;          \
00208     t.restart();                                                        \
00209   }
00210 #define TIMING_ACCSTART(t) t.start();
00211 #define TIMING_ACCSTOP(t) t.stop();                                     
00212 #define TIMING_RESET(t) t.reset();
00213 #define LOCAL_TIMING_START() ebl::timer local_debug_timer;
00214 #define LOCAL_TIMING2_START() ebl::timer local_debug_timer2;
00215 #define LOCAL_TIMING_REPORT(s) TIMING(s, local_debug_timer)
00216 #define LOCAL_TIMING2_REPORT(s) TIMING(s, local_debug_timer2)
00217 
00218 #define TIMING1(s) TIMING(s, debug_timer1)
00219 #define TIMING2(s) TIMING(s, debug_timer2)
00220 #define TIMING_RESIZING_ACCSTART()              \
00221   TIMING_ACCSTART(debug_timer_resizing);        \
00222   debug_resizing_cnt++;
00223 #define TIMING_RESIZING_ACCSTOP() TIMING_ACCSTOP(debug_timer_resizing)
00224 #define TIMING_RESIZING_RESET()                                 \
00225   TIMING_RESET(debug_timer_resizing);                           \
00226   debug_resizing_cnt = 0;
00227 #define TIMING_RESIZING(s)                                              \
00228   TIMINGACC(s, debug_timer_resizing);                                   \
00229   std::cout << s << ": number of resizings: " << debug_resizing_cnt <<std::endl;
00230 
00231 #else
00232 #define INIT_TIMING()
00233 #define TIMING(s, t)
00234 #define TIMING_START_ACC(t)
00235 #define TIMING_STOP_ACC(t)
00236 #define TIMING_RESET(t)
00237 #define TIMING_START()
00238 #define TIMING_REPORT()
00239 #define TIMING1(s)
00240 #define TIMING2(s)
00241 #define TIMING_RESIZING_ACCSTART()
00242 #define TIMING_RESIZING_ACCSTOP()
00243 #define TIMING_RESIZING_RESET()
00244 #define TIMING_RESIZING(s)
00245 #define LOCAL_TIMING_START()
00246 #define LOCAL_TIMING2_START()
00247 #define LOCAL_TIMING_REPORT(s)
00248 #define LOCAL_TIMING2_REPORT(s)
00249 
00250 #endif
00251 
00252 } // end namespace ebl
00253 
00254 #include "utils.hpp"
00255 
00256 #endif /* UTILS_ */