common/utils.cc

Go to the documentation of this file.
00001 /* utils.cc: Various useful utilities
00002  *
00003  * Copyright 1999,2000,2001 BrightStation PLC
00004  * Copyright 2002 Ananova Ltd
00005  * Copyright 2003,2004,2005,2007 Olly Betts
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as
00009  * published by the Free Software Foundation; either version 2 of the
00010  * License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00020  * USA
00021  */
00022 
00023 #include <config.h>
00024 
00025 #include "utils.h"
00026 
00027 #include <stdio.h>
00028 #include <cfloat>
00029 #include <cmath>
00030 
00031 using namespace std;
00032 
00033 // This ought to be enough for any of the conversions below.
00034 #define BUFSIZE 100
00035 
00036 #ifdef SNPRINTF
00037 #define CONVERT_TO_STRING(FMT) \
00038     char buf[BUFSIZE];\
00039     int len = SNPRINTF(buf, BUFSIZE, (FMT), val);\
00040     if (len == -1 || len > BUFSIZE) return string(buf, BUFSIZE);\
00041     return string(buf, len);
00042 #else
00043 #define CONVERT_TO_STRING(FMT) \
00044     char buf[BUFSIZE];\
00045     buf[BUFSIZE - 1] = '\0';\
00046     sprintf(buf, (FMT), val);\
00047     if (buf[BUFSIZE - 1]) abort(); /* Uh-oh, buffer overrun */ \
00048     return string(buf);
00049 #endif
00050 
00051 // Convert a number to a string
00052 string
00053 om_tostring(int val)
00054 {
00055     CONVERT_TO_STRING("%d")
00056 }
00057 
00058 string
00059 om_tostring(unsigned int val)
00060 {
00061     CONVERT_TO_STRING("%u")
00062 }
00063 
00064 string
00065 om_tostring(long int val)
00066 {
00067     CONVERT_TO_STRING("%ld")
00068 }
00069 
00070 string
00071 om_tostring(unsigned long int val)
00072 {
00073     CONVERT_TO_STRING("%lu")
00074 }
00075 
00076 #ifdef __WIN32__
00077 string
00078 om_tostring(__int64 val)
00079 {
00080     // Avoid a format string warning from GCC - mingw uses the MS C runtime DLL
00081     // which does understand "%I64d", but GCC doesn't know that.
00082     static const char fmt[] = { '%', 'I', '6', '4', 'd', 0 };
00083     CONVERT_TO_STRING(fmt)
00084 }
00085 #endif
00086 
00087 string
00088 om_tostring(double val)
00089 {
00090     CONVERT_TO_STRING("%.20g")
00091 }
00092 
00093 string
00094 om_tostring(const void * val)
00095 {
00096     CONVERT_TO_STRING("%p")
00097 }
00098 
00099 string
00100 om_tostring(bool val)
00101 {
00102     return val ? "1" : "0";
00103 }
00104 
00107 bool
00108 file_exists(const string &fname)
00109 {
00110     struct stat sbuf;
00111     // exists && is a regular file
00112     return stat(fname, &sbuf) == 0 && S_ISREG(sbuf.st_mode);
00113 }
00114 
00117 bool
00118 dir_exists(const string &fname)
00119 {
00120     struct stat sbuf;
00121     // exists && is a directory
00122     return stat(fname, &sbuf) == 0 && S_ISDIR(sbuf.st_mode);
00123 }
00124 
00125 namespace Xapian {
00126 namespace Internal {
00127 
00128 bool within_DBL_EPSILON(double a, double b) {
00129     return fabs(a - b) >= DBL_EPSILON;
00130 }
00131 
00132 }
00133 }

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.