PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // 00007 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 00037 00038 00039 /* ******************************************************* 00040 * $Id: general.cc 8641 2008-03-07 16:51:22Z nouiz $ 00041 * This file is part of the PLearn library. 00042 ******************************************************* */ 00043 00044 #include "general.h" 00045 #include <sys/stat.h> 00046 #include <nspr/prsystem.h> 00047 #include <nspr/prenv.h> 00048 #ifdef _MSC_VER 00049 #include <io.h> 00050 #endif 00051 00052 //#include "stringutils.h" 00053 00054 namespace PLearn { 00055 using namespace std; 00056 00057 static PLearnInit _plearn_init_; 00058 00059 #if defined(WIN32) && !defined(__CYGWIN__) 00060 #include <io.h> 00061 // norman: potentially dangerous if there is a function called with the same name in this 00062 // file. Beware! 00063 #define umask _umask 00064 #endif 00065 00066 PLearnInit::PLearnInit() 00067 { 00068 umask(002); 00069 } 00070 00071 PLearnInit::~PLearnInit(){} 00072 00073 char* strcopy(char* s) 00074 { 00075 if (!s) return 0; 00076 char* ss=new char[strlen(s)+1]; 00077 strcpy(ss,s); 00078 return ss; 00079 } 00080 00081 // print a number without unnecessary trailing zero's, into buffer 00082 void pretty_print_number(char* buffer, real number) 00083 { 00084 char* t; 00085 char* s; 00086 double dnum = double(number); 00087 sprintf(buffer,"%.15g",dnum); 00088 for (s=buffer; *s!='\0'; s++) 00089 if (*s == '.') break; 00090 if (*s == '.') 00091 { 00092 for (t = s + 1; isdigit(*t); t++) 00093 if (*t != '0') 00094 s = t + 1; 00095 for (;*t != '\0';) *s++ = *t++; 00096 *s = '\0'; 00097 } 00098 } 00099 00100 bool isMapKeysAreInt(map<real,int>& m) 00101 { 00102 map<real,int>::iterator it; 00103 for (it = m.begin(); it!= m.end(); ++it) 00104 { 00105 real key_rvalue = it->first; 00106 int key_ivalue = int(key_rvalue); 00107 if (!fast_exact_is_equal(key_rvalue, key_ivalue)) 00108 return false; 00109 } 00110 return true; 00111 } 00112 00113 00114 string hostname() 00115 { 00116 char tmp[1024]; 00117 if(PR_GetSystemInfo(PR_SI_HOSTNAME,tmp,500)==PR_SUCCESS) 00118 return string(tmp); 00119 else{ 00120 const char* h = PR_GetEnv("HOSTNAME"); 00121 if (!h) 00122 h = PR_GetEnv("HOST"); 00123 if (!h) 00124 PLERROR("hostname: could not find the host name from NSPR" 00125 " or from the variable $HOSTNAME or $HOST in environment!"); 00126 return h; 00127 } 00128 } 00129 00130 string prgname(const string& setname) 00131 { 00132 static string prgname_ = "plearn"; 00133 if(setname!="") 00134 prgname_ = setname; 00135 return prgname_; 00136 } 00137 00138 #ifdef WIN32 00139 #undef umask 00140 #endif 00141 } // end of namespace PLearn 00142 00143 00144 /* 00145 Local Variables: 00146 mode:c++ 00147 c-basic-offset:4 00148 c-file-style:"stroustrup" 00149 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00150 indent-tabs-mode:nil 00151 fill-column:79 00152 End: 00153 */ 00154 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :