00001 /* 00002 * Phusion Passenger - http://www.modrails.com/ 00003 * Copyright (C) 2008 Phusion 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; version 2 of the License. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License along 00015 * with this program; if not, write to the Free Software Foundation, Inc., 00016 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00017 */ 00018 #ifndef _PASSENGER_UTILS_H_ 00019 #define _PASSENGER_UTILS_H_ 00020 00021 #include <boost/shared_ptr.hpp> 00022 #include <string> 00023 #include <vector> 00024 #include <sstream> 00025 00026 namespace Passenger { 00027 00028 using namespace std; 00029 using namespace boost; 00030 00031 /** 00032 * Convenience shortcut for creating a <tt>shared_ptr</tt>. 00033 * Instead of: 00034 * @code 00035 * shared_ptr<Foo> foo; 00036 * ... 00037 * foo = shared_ptr<Foo>(new Foo()); 00038 * @endcode 00039 * one can write: 00040 * @code 00041 * shared_ptr<Foo> foo; 00042 * ... 00043 * foo = ptr(new Foo()); 00044 * @endcode 00045 * 00046 * @param pointer The item to put in the shared_ptr object. 00047 * @ingroup Support 00048 */ 00049 template<typename T> shared_ptr<T> 00050 ptr(T *pointer) { 00051 return shared_ptr<T>(pointer); 00052 } 00053 00054 /** 00055 * Used internally by toString(). Do not use directly. 00056 */ 00057 template<typename T> 00058 struct AnythingToString { 00059 string operator()(T something) { 00060 stringstream s; 00061 s << something; 00062 return s.str(); 00063 } 00064 }; 00065 00066 /** 00067 * Used internally by toString(). Do not use directly. 00068 */ 00069 template<> 00070 struct AnythingToString< vector<string> > { 00071 string operator()(const vector<string> &v) { 00072 string result("["); 00073 vector<string>::const_iterator it; 00074 unsigned int i; 00075 for (it = v.begin(), i = 0; it != v.end(); it++, i++) { 00076 result.append("'"); 00077 result.append(*it); 00078 if (i == v.size() - 1) { 00079 result.append("'"); 00080 } else { 00081 result.append("', "); 00082 } 00083 } 00084 result.append("]"); 00085 return result; 00086 } 00087 }; 00088 00089 /** 00090 * Convert anything to a string. 00091 * 00092 * @param something The thing to convert. 00093 * @ingroup Support 00094 */ 00095 template<typename T> string 00096 toString(T something) { 00097 return AnythingToString<T>()(something); 00098 } 00099 00100 /** 00101 * Converts the given string to an integer. 00102 * @ingroup Support 00103 */ 00104 int atoi(const string &s); 00105 00106 /** 00107 * Split the given string using the given separator. 00108 * 00109 * @param str The string to split. 00110 * @param sep The separator to use. 00111 * @param output The vector to write the output to. 00112 * @ingroup Support 00113 */ 00114 void split(const string &str, char sep, vector<string> &output); 00115 00116 /** 00117 * Check whether the specified file exists. 00118 * 00119 * @param filename The filename to check. 00120 * @return Whether the file exists. 00121 * @ingroup Support 00122 */ 00123 bool fileExists(const char *filename); 00124 00125 /** 00126 * Find the location of the Passenger spawn server script. 00127 * This is done by scanning $PATH. For security reasons, only 00128 * absolute paths are scanned. 00129 * 00130 * @return An absolute path to the spawn server script, or 00131 * an empty string on error. 00132 * @ingroup Support 00133 */ 00134 string findSpawnServer(); 00135 00136 /** 00137 * Returns a canonical version of the specified path. All symbolic links 00138 * and relative path elements are resolved. 00139 * Returns an empty string if something went wrong. 00140 * 00141 * @ingroup Support 00142 */ 00143 string canonicalizePath(const string &path); 00144 00145 /** 00146 * Check whether the specified directory is a valid Ruby on Rails 00147 * 'public' directory. 00148 * 00149 * @ingroup Support 00150 */ 00151 bool verifyRailsDir(const string &dir); 00152 00153 } // namespace Passenger 00154 00155 #endif /* _PASSENGER_UTILS_H_ */ 00156