src/include/common/base/util/utils.h

00001 /*
00002  * Copyright (C) 2003-2007 Funambol
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
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
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 #ifndef INCL_BASE_UTILS
00020 #define INCL_BASE_UTILS
00021 
00023 #include "base/fscapi.h"
00024 #include "base/util/ArrayList.h"
00025 #include "base/md5.h"
00026 #include "base/base64.h"
00027 
00028 #include <stdio.h>
00029 
00030 // Default len for stringdup (means: use source string len)
00031 #define STRINGDUP_NOLEN 0xFFFFFFFF
00032 
00033 #define B64_ENCODING        "b64"
00034 #define TEXT_PLAIN_ENCODING "text/plain"
00035 
00036 /*
00037  * Deletes the given char* [] buffer if it is not NULL
00038  * and sets the pointer to NULL
00039  *
00040  */
00041 void safeDelete(char*  p[]) EXTRA_SECTION_00;
00042 
00043 void safeDel(char** p) EXTRA_SECTION_00;
00044 
00051 void timestampToAnchor(unsigned long timestamp, char anchor[21]) EXTRA_SECTION_00;
00052 
00056 unsigned long anchorToTimestamp(const char* anchor) EXTRA_SECTION_00;
00057 
00058 char* stringdup(const char* s, size_t len = STRINGDUP_NOLEN) EXTRA_SECTION_00 ;
00059 WCHAR* wstrdup(const WCHAR* s, size_t len = STRINGDUP_NOLEN) EXTRA_SECTION_00;
00060 
00061 char*  strtolower(const char *s) EXTRA_SECTION_00;
00062 
00063 char*  wcstoupper(const char *s) EXTRA_SECTION_00;
00064 
00068 const char *brfind(const char *s1, const char *s2, size_t len=STRINGDUP_NOLEN) EXTRA_SECTION_00 ;
00069 
00073 inline BOOL isEmpty(const char*  s) {
00074     return ((s == NULL) || (strlen(s) == 0));
00075 }
00076 
00080 inline BOOL isNotEmpty(const char*  s) {
00081     return (s && (strlen(s) > 0));
00082 }
00083 
00084 
00085 /*
00086 * compare two char array ignoring the case of the char
00087 */
00088 bool wcscmpIgnoreCase(const char*  p, const char*  q);
00089 
00090 /*
00091 * Converts a integer into a char* 
00092 */
00093 char*  itow(int i);
00094 
00095 /*
00096 * Converts a integer into a char* 
00097 */
00098 char*  ltow(long i);
00099 
00100 /*
00101 * Method to create the cred data given the username, password and nonce
00102 * It uses the calculateMD5 to calculate the MD5 using the alghoritm.
00103 */
00104 char*  MD5CredentialData(char*  userName, char*  password, char*  nonce);
00105 
00106 /*
00107 * Calculates the digest given the token and its lenght
00108 */
00109 char* calculateMD5(const void* token, int len, char* wdigest);
00110 
00111 /*
00112  * Return a filename composed by the system temp dir and the name given
00113  * in input. If the file exists, try to add a digit 0-9.
00114  * If this fails too, return NULL (there's must be something wrong in
00115  * the calling program)
00116  *
00117  * @param name - a file name, without path
00118  * @return - a full pathname, allocated with new[], or NULL on error
00119  */
00120 char *mkTempFileName(const char *name);
00121 
00122 /*
00123  * Write len bytes from buffer to the file 'filename'.
00124  *
00125  * @param name - the file name
00126  * @param buffer - pointer to the buffer to write
00127  * @param len - the number of bytes to write
00128  * @param binary - if true the file will be opened in binary mode
00129  *
00130  * @return - true if file is successfully saved
00131  */
00132 bool saveFile(const char *filename, const char *buffer, size_t len,
00133               bool binary = false );
00134 
00135 /*
00136  * Get the size of the file, in bytes
00137  *
00138  * @param f - the file to evaluate
00139  * @return - the length of the file
00140  */
00141 size_t fgetsize(FILE *f); 
00142 
00143 /*
00144  * Read the content
00145  *
00146  * @param name - the file name
00147  * @param message (out) - new allocated buffer with the file content
00148  * @param len - length of the read content
00149  * @param binary - if true the file has to be opened in binary mode
00150  *
00151  * @return - true if file is succesfully read
00152  */
00153 bool readFile(const char* name, char **message, size_t *len, bool binary = false );
00154 
00155 /*
00156  * Read the content of a directory
00157  *
00158  * @param name - the dir name
00159  * @param count (out) - number of files in dir
00160  * @param onlyCount - optional, if true only set the nuber of files (count)
00161  *
00162  * @return - new allocated array of fileNames (NULL if errors)
00163  */
00164 char** readDir(char* name, int *count, bool onlyCount = false);
00165 
00171 unsigned long getFileModTime(const char* name);
00172 
00173 
00174 long int getLenEncoding(const char*  s, const char* encoding);
00175 char *toMultibyte(const WCHAR *wc, const char *encoding = 0 );
00176 WCHAR *toWideChar(const char *mb, const char *encoding = 0 );
00177 
00178 
00179 // Wide Char Convert: inline function used to convert a wchar
00180 // in char, using a static buffer to store the converted string.
00181 //
00182 // BEWARE: the string is deleted and re-used at each call.
00183 inline const char *_wcc(const WCHAR *wc, const char *enc=0) {
00184     static char* encodeBuf = 0;
00185 
00186     if (encodeBuf){
00187         delete [] encodeBuf;
00188         encodeBuf = 0;
00189     }
00190     if (wc) {
00191         encodeBuf = toMultibyte(wc, enc);
00192     }
00193     return encodeBuf;
00194 }
00195 
00196 
00198 #endif

Generated on Fri Apr 27 12:29:18 2007 for Funambol C++ Client Library by  doxygen 1.5.2