00001 /* 00002 * Funambol is a mobile platform developed by Funambol, Inc. 00003 * Copyright (C) 2003 - 2007 Funambol, Inc. 00004 * 00005 * This program is free software; you can redistribute it and/or modify it under 00006 * the terms of the GNU Affero General Public License version 3 as published by 00007 * the Free Software Foundation with the addition of the following permission 00008 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED 00009 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 00010 * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. 00011 * 00012 * This program is distributed in the hope that it will be useful, but WITHOUT 00013 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00014 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 00015 * details. 00016 * 00017 * You should have received a copy of the GNU Affero General Public License 00018 * along with this program; if not, see http://www.gnu.org/licenses or write to 00019 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00020 * MA 02110-1301 USA. 00021 * 00022 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 00023 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com. 00024 * 00025 * The interactive user interfaces in modified source and object code versions 00026 * of this program must display Appropriate Legal Notices, as required under 00027 * Section 5 of the GNU Affero General Public License version 3. 00028 * 00029 * In accordance with Section 7(b) of the GNU Affero General Public License 00030 * version 3, these Appropriate Legal Notices must retain the display of the 00031 * "Powered by Funambol" logo. If the display of the logo is not reasonably 00032 * feasible for technical reasons, the Appropriate Legal Notices must display 00033 * the words "Powered by Funambol". 00034 */ 00035 00036 #ifndef INCL_BASE_UTILS 00037 #define INCL_BASE_UTILS 00038 00040 #include "base/fscapi.h" 00041 #include "base/util/ArrayList.h" 00042 #include "base/md5.h" 00043 #include "base/base64.h" 00044 00045 #include <stdio.h> 00046 00047 // Default len for stringdup (means: use source string len) 00048 #define STRINGDUP_NOLEN 0xFFFFFFFF 00049 00050 #define B64_ENCODING "b64" 00051 #define TEXT_PLAIN_ENCODING "text/plain" 00052 00053 /* 00054 * Deletes the given char* [] buffer if it is not NULL 00055 * and sets the pointer to NULL 00056 * 00057 */ 00058 void safeDelete(char* p[]); 00059 00060 void safeDel(char** p); 00061 00068 void timestampToAnchor(unsigned long timestamp, char anchor[21]); 00069 00073 unsigned long anchorToTimestamp(const char* anchor); 00074 00075 char* stringdup(const char* s, size_t len = STRINGDUP_NOLEN) ; 00076 WCHAR* wstrdup(const WCHAR* s, size_t len = STRINGDUP_NOLEN); 00077 00078 char* strtolower(const char *s); 00079 00080 char* wcstoupper(const char *s); 00081 00085 const char *brfind(const char *s1, const char *s2, size_t len=STRINGDUP_NOLEN) ; 00086 00090 inline BOOL isEmpty(const char* s) { 00091 return ((s == NULL) || (strlen(s) == 0)); 00092 } 00093 00097 inline BOOL isNotEmpty(const char* s) { 00098 return (s && (strlen(s) > 0)); 00099 } 00100 00101 00102 /* 00103 * compare two char array ignoring the case of the char 00104 */ 00105 bool wcscmpIgnoreCase(const char* p, const char* q); 00106 00107 /* 00108 * Converts a integer into a char* 00109 */ 00110 char* itow(int i); 00111 00112 /* 00113 * Converts a integer into a char* 00114 */ 00115 char* ltow(long i); 00116 00117 00118 /* 00119 * Method to create the cred data given the username, password and nonce 00120 * It uses the calculateMD5 to calculate the MD5 using the alghoritm. 00121 */ 00122 char* MD5CredentialData(const char* userName, const char* password, const char* nonce); 00123 00124 /* 00125 * Calculates the digest given the token and its lenght 00126 */ 00127 char* calculateMD5(const void* token, int len, char* wdigest); 00128 00129 /* 00130 * Return a filename composed by the system temp dir and the name given 00131 * in input. If the file exists, try to add a digit 0-9. 00132 * If this fails too, return NULL (there's must be something wrong in 00133 * the calling program) 00134 * 00135 * @param name - a file name, without path 00136 * @return - a full pathname, allocated with new[], or NULL on error 00137 */ 00138 char *mkTempFileName(const char *name); 00139 00140 /* 00141 * Write len bytes from buffer to the file 'filename'. 00142 * 00143 * @param name - the file name 00144 * @param buffer - pointer to the buffer to write 00145 * @param len - the number of bytes to write 00146 * @param binary - if true the file will be opened in binary mode 00147 * 00148 * @return - true if file is successfully saved 00149 */ 00150 bool saveFile(const char *filename, const char *buffer, size_t len, 00151 bool binary = false ); 00152 00153 /* 00154 * Get the size of the file, in bytes 00155 * 00156 * @param f - the file to evaluate 00157 * @return - the length of the file 00158 */ 00159 size_t fgetsize(FILE *f); 00160 00161 /* 00162 * Read the content 00163 * 00164 * @param name - the file name 00165 * @param message (out) - new allocated buffer with the file content 00166 * @param len - length of the read content 00167 * @param binary - if true the file has to be opened in binary mode 00168 * 00169 * @return - true if file is succesfully read 00170 */ 00171 bool readFile(const char* name, char **message, size_t *len, bool binary = false ); 00172 00173 /* 00174 * Read the content of a directory 00175 * 00176 * @param name - the dir name 00177 * @param count (out) - number of files in dir 00178 * @param onlyCount - optional, if true only set the nuber of files (count) 00179 * 00180 * @return - new allocated array of fileNames (NULL if errors) 00181 */ 00182 char** readDir(char* name, int *count, bool onlyCount = false); 00183 00189 unsigned long getFileModTime(const char* name); 00190 00191 00192 long int getLenEncoding(const char* s, const char* encoding); 00193 char *toMultibyte(const WCHAR *wc, const char *encoding = 0 ); 00194 WCHAR *toWideChar(const char *mb, const char *encoding = 0 ); 00195 00196 00197 // Wide Char Convert: inline function used to convert a wchar 00198 // in char, using a static buffer to store the converted string. 00199 // 00200 // BEWARE: the string is deleted and re-used at each call. 00201 inline const char *_wcc(const WCHAR *wc, const char *enc=0) { 00202 static char* encodeBuf = 0; 00203 00204 if (encodeBuf){ 00205 delete [] encodeBuf; 00206 encodeBuf = 0; 00207 } 00208 if (wc) { 00209 encodeBuf = toMultibyte(wc, enc); 00210 } 00211 return encodeBuf; 00212 } 00213 00214 00216 #endif