libeblearntools
/home/rex/ebltrunk/tools/libeblearntools/include/tools_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 TOOLS_UTILS_H_
00034 #define TOOLS_UTILS_H_
00035 
00036 #include <list>
00037 #include <string>
00038 #include <vector>
00039 #include <map>
00040 
00041 #include "defines.h"
00042 #include "idx.h"
00043 
00044 using namespace std;
00045 
00046 namespace ebl {
00047 
00049   // directory utilities
00050 
00051   typedef pair<string, string> stringpair;
00052   typedef list<stringpair> files_list;
00053 
00054   inline bool less_than(const stringpair& b1, const stringpair& b2);
00055   
00066   EXPORT files_list *find_files(const string &dir,
00067                                 const char *pattern = IMAGE_PATTERN,
00068                                 files_list *fl = NULL, bool sort = true,
00069                                 bool recursive = true, bool randomize = false);
00070   
00085   EXPORT list<string> *find_fullfiles(const string &dir,
00086                                       const char *pattern = IMAGE_PATTERN,
00087                                       list<string> *fl = NULL, 
00088                                       bool sort = true,
00089                                       bool recursive = true, 
00090                                       bool randomize = false,
00091                                       bool finddir = false,
00092                                       bool fullpattern = false);
00093   
00098   EXPORT uint count_files(const string &dir, 
00099                           const char *pattern = IMAGE_PATTERN);
00100 
00105   EXPORT string increment_filename(const char *fullname, uint stride); 
00106 
00109   EXPORT bool string_to_bool(const string &s);
00112   EXPORT int string_to_int(const string &s);
00115   EXPORT int string_to_int(const char *s);
00118   EXPORT intg string_to_intg(const string &s);
00121   EXPORT intg string_to_intg(const char *s);
00124   EXPORT uint string_to_uint(const string &s);
00127   EXPORT uint string_to_uint(const char *s);
00130   EXPORT float string_to_float(const string &s);
00133   EXPORT float string_to_float(const char *s);
00136   EXPORT double string_to_double(const string &s);
00139   EXPORT double string_to_double(const char *s);
00140 
00143   EXPORT list<uint> string_to_uintlist(const string &s);
00146   EXPORT list<uint> string_to_uintlist(const char *s);
00150   EXPORT vector<int> string_to_intvector(const string &s, char sep = ',');
00154   EXPORT vector<int> string_to_intvector(const char *s, char sep = ',');
00158   EXPORT vector<uint> string_to_uintvector(const string &s, char sep = ',');
00162   EXPORT vector<uint> string_to_uintvector(const char *s, char sep = ',');
00166   EXPORT vector<intg> string_to_intgvector(const char *s, char sep = ',');
00170   EXPORT vector<float> string_to_floatvector(const char *s, char sep = ',');
00171 
00175   template <typename T>
00176   EXPORT idx<T> string_to_idx(const char *s, char sep = ',');
00178   template <typename T>
00179   EXPORT T string_to_number(const char *s);  
00183   EXPORT idxdim string_to_idxdim(const char *s, char sep = 'x');
00187   EXPORT fidxdim string_to_fidxdim(const char *s, char sep = 'x');
00191   EXPORT idxdim string_to_idxdim(const string &s, char sep = 'x');
00196   EXPORT midxdim string_to_idxdimvector(const char *s, char vecsep = ',',
00197                                         char dimsep = 'x');
00202   EXPORT mfidxdim string_to_fidxdimvector(const char *s, char vecsep = ',',
00203                                           char dimsep = 'x');
00204 
00208   EXPORT list<string> string_to_stringlist(const string &s, char sep = ',');
00212   EXPORT list<string> string_to_stringlist(const char *s, char sep = ',');
00216   EXPORT vector<string> string_to_stringvector(const string &s, char sep = ',');
00220   EXPORT vector<string> string_to_stringvector(const char *s, char sep = ',');
00223   EXPORT vector<double> string_to_doublevector(const string &s);
00226   EXPORT vector<double> string_to_doublevector(const char *s);
00227 
00229   template <typename T1, typename T2>
00230     EXPORT string map_to_string(map<T1,T2> &m);  
00233   template <typename T1, typename T2>
00234     EXPORT string map_to_string2(map<T1,T2> &m);  
00236   EXPORT string stringlist_to_string(list<string> &l);  
00238   EXPORT vector<string> ubyteidx_to_stringvector(idx<ubyte> &u);
00240   EXPORT string string_replaceall(const string &s, const char *s1,
00241                                   const char *s2);
00242   
00244   // system functions
00245   
00249   EXPORT bool tar(const string &dir, const string &tgtdir);
00253   EXPORT bool tar_pattern(const string &dir, const string &tgtdir,
00254                           const string &tgtfilename, const char *pattern);
00255 
00257   template <typename T>
00258     void list_to_vector(list<T> &l, vector<T> &v);
00259 
00264   EXPORT string filename(const char *s);
00265     
00267   EXPORT string system_to_string(const string &cmd);
00268 
00269 } // end namespace ebl
00270 
00271 #include "tools_utils.hpp"
00272 
00273 #endif /* TOOLS_UTILS_ */