Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

general.cc

Go to the documentation of this file.
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,v 1.8 2004/02/28 17:58:51 tihocan Exp $ 00041 * This file is part of the PLearn library. 00042 ******************************************************* */ 00043 00044 #include "general.h" 00045 #include <sys/stat.h> 00046 #ifdef _MSC_VER 00047 #include <io.h> 00048 #endif 00049 00050 //#include "stringutils.h" 00051 00052 namespace PLearn { 00053 using namespace std; 00054 00055 static PLearnInit _plearn_init_; 00056 00057 #ifdef WIN32 00058 #include <io.h> 00059 // norman: potentially dangerous if there is a function called with the same name in this 00060 // file. Beware! 00061 #define umask _umask 00062 #endif 00063 00064 PLearnInit::PLearnInit() 00065 { 00066 umask(002); 00067 } 00068 00069 PLearnInit::~PLearnInit(){} 00070 00071 int file_size(const string& filename) 00072 { 00073 #ifdef _MSC_VER 00074 struct _stat buf; 00075 if (_stat(filename.c_str(),&buf)==0) 00076 #else 00077 struct stat buf; 00078 if (stat(filename.c_str(),&buf)==0) 00079 #endif 00080 00081 return buf.st_size; 00082 else 00083 return -1; 00084 } 00085 00086 00087 char* strcopy(char* s) 00088 { 00089 if (!s) return 0; 00090 char* ss=new char[strlen(s)+1]; 00091 strcpy(ss,s); 00092 return ss; 00093 } 00094 00095 // print a number without unnecessary trailing zero's, into buffer 00096 void pretty_print_number(char* buffer, real number) 00097 { 00098 char* t; 00099 char* s; 00100 double dnum = double(number); 00101 sprintf(buffer,"%.15g",dnum); 00102 for (s=buffer; *s!='\0'; s++) 00103 if (*s == '.') break; 00104 if (*s == '.') 00105 { 00106 for (t = s + 1; isdigit(*t); t++) 00107 if (*t != '0') 00108 s = t + 1; 00109 for (;*t != '\0';) *s++ = *t++; 00110 *s = '\0'; 00111 } 00112 } 00113 00114 bool file_exists(const string& filename) 00115 { 00116 FILE* fp = fopen(filename.c_str(),"r"); 00117 if (fp) 00118 { 00119 fclose(fp); 00120 return true; 00121 } 00122 return false; 00123 } 00124 00125 00126 bool isMapKeysAreInt(map<real,int>& m) 00127 { 00128 map<real,int>::iterator it; 00129 for (it = m.begin(); it!= m.end(); ++it) 00130 { 00131 real key_rvalue = it->first; 00132 int key_ivalue = int(key_rvalue); 00133 if (key_rvalue != key_ivalue) return false; 00134 } 00135 return true; 00136 } 00137 00138 00139 string hostname() 00140 { 00141 char* h = getenv("HOSTNAME"); 00142 if (!h) 00143 h = getenv("HOST"); 00144 if (!h) 00145 PLERROR("hostname: could not find $HOSTNAME nor $HOST in environment!"); 00146 return h; 00147 } 00148 00149 string prgname(const string& setname) 00150 { 00151 static string prgname_ = "plearn"; 00152 if(setname!="") 00153 prgname_ = setname; 00154 return prgname_; 00155 } 00156 00157 #ifdef WIN32 00158 #undef umask 00159 #endif 00160 } // end of namespace PLearn 00161 00162

Generated on Tue Aug 17 15:54:04 2004 for PLearn by doxygen 1.3.7