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

00001 /*
00002  * Copyright (C) 2003-2007 Funambol, Inc
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 version 2 as
00006  * published by the Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR
00011  * PURPOSE.  See the GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program; if not, write to the Free Software
00015  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00016  * 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[]);
00042 
00043 void safeDel(char** p);
00044 
00051 void timestampToAnchor(unsigned long timestamp, char anchor[21]);
00052 
00056 unsigned long anchorToTimestamp(const char* anchor);
00057 
00058 char* stringdup(const char* s, size_t len = STRINGDUP_NOLEN) ;
00059 WCHAR* wstrdup(const WCHAR* s, size_t len = STRINGDUP_NOLEN);
00060 
00061 char*  strtolower(const char *s);
00062 
00063 char*  wcstoupper(const char *s);
00064 
00068 const char *brfind(const char *s1, const char *s2, size_t len=STRINGDUP_NOLEN) ;
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 /*
00102 * Method to create the cred data given the username, password and nonce
00103 * It uses the calculateMD5 to calculate the MD5 using the alghoritm.
00104 */
00105 char*  MD5CredentialData(const char* userName, const char* password, const char* nonce);
00106 
00107 /*
00108 * Calculates the digest given the token and its lenght
00109 */
00110 char* calculateMD5(const void* token, int len, char* wdigest);
00111 
00112 /*
00113  * Return a filename composed by the system temp dir and the name given
00114  * in input. If the file exists, try to add a digit 0-9.
00115  * If this fails too, return NULL (there's must be something wrong in
00116  * the calling program)
00117  *
00118  * @param name - a file name, without path
00119  * @return - a full pathname, allocated with new[], or NULL on error
00120  */
00121 char *mkTempFileName(const char *name);
00122 
00123 /*
00124  * Write len bytes from buffer to the file 'filename'.
00125  *
00126  * @param name - the file name
00127  * @param buffer - pointer to the buffer to write
00128  * @param len - the number of bytes to write
00129  * @param binary - if true the file will be opened in binary mode
00130  *
00131  * @return - true if file is successfully saved
00132  */
00133 bool saveFile(const char *filename, const char *buffer, size_t len,
00134               bool binary = false );
00135 
00136 /*
00137  * Get the size of the file, in bytes
00138  *
00139  * @param f - the file to evaluate
00140  * @return - the length of the file
00141  */
00142 size_t fgetsize(FILE *f);
00143 
00144 /*
00145  * Read the content
00146  *
00147  * @param name - the file name
00148  * @param message (out) - new allocated buffer with the file content
00149  * @param len - length of the read content
00150  * @param binary - if true the file has to be opened in binary mode
00151  *
00152  * @return - true if file is succesfully read
00153  */
00154 bool readFile(const char* name, char **message, size_t *len, bool binary = false );
00155 
00156 /*
00157  * Read the content of a directory
00158  *
00159  * @param name - the dir name
00160  * @param count (out) - number of files in dir
00161  * @param onlyCount - optional, if true only set the nuber of files (count)
00162  *
00163  * @return - new allocated array of fileNames (NULL if errors)
00164  */
00165 char** readDir(char* name, int *count, bool onlyCount = false);
00166 
00172 unsigned long getFileModTime(const char* name);
00173 
00174 
00175 long int getLenEncoding(const char*  s, const char* encoding);
00176 char *toMultibyte(const WCHAR *wc, const char *encoding = 0 );
00177 WCHAR *toWideChar(const char *mb, const char *encoding = 0 );
00178 
00179 
00180 // Wide Char Convert: inline function used to convert a wchar
00181 // in char, using a static buffer to store the converted string.
00182 //
00183 // BEWARE: the string is deleted and re-used at each call.
00184 inline const char *_wcc(const WCHAR *wc, const char *enc=0) {
00185     static char* encodeBuf = 0;
00186 
00187     if (encodeBuf){
00188         delete [] encodeBuf;
00189         encodeBuf = 0;
00190     }
00191     if (wc) {
00192         encodeBuf = toMultibyte(wc, enc);
00193     }
00194     return encodeBuf;
00195 }
00196 
00197 
00199 #endif

Generated on Tue Oct 30 15:11:25 2007 for Funambol C++ Client Library by  doxygen 1.5.2