misc.cc

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021    Author: Eric Viara <viara@sysra.com>
00022 */
00023 
00024 
00025 #include <stdlib.h>
00026 #include <strings.h>
00027 #include <stdarg.h>
00028 #include <unistd.h>
00029 
00030 #include "eyedb/eyedb.h"
00031 
00032 #include "eyedb/base_p.h"
00033 #include "eyedb/internals/ObjectHeader.h"
00034 #include "misc.h"
00035 
00036 namespace eyedb {
00037 
00038   const char NullString[] = "NULL";
00039   const char NilString[]  = "nil";
00040 
00041   Bool isOidValid(const eyedbsm::Oid *oid)
00042   {
00043     return (oid && oid->getNX() && oid->getUnique()) ? True : False;
00044   }
00045 
00046   eyedbsm::Oid *oidInvalidate(eyedbsm::Oid *oid)
00047   {
00048     memset(oid, 0, sizeof(eyedbsm::Oid));
00049     return oid;
00050   }
00051 
00052   const eyedbsm::Oid *getInvalidOid()
00053   {
00054     static eyedbsm::Oid oid;
00055     memset(&oid, 0, sizeof(eyedbsm::Oid)); /* be secure */
00056     return &oid;
00057   }
00058 
00059   Bool OidCompare(const eyedbsm::Oid *o1, const eyedbsm::Oid *o2)
00060   {
00061     return (o1->getNX() == o2->getNX() && o1->getUnique() == o2->getUnique()) ? True: False;
00062   }
00063 
00064   Bool ObjectHeadCompare(const ObjectHeader *h1,
00065                          const ObjectHeader *h2)
00066   {
00067     /*
00068       return (h1->magic == h2->magic &&
00069       h1->type == h2->type &&
00070       h1->size == h2->size &&
00071       OidCompare(&h1->oid_cl, &h2->oid_cl)) ? True : False;
00072     */
00073     Bool f = (h1->magic == h2->magic &&
00074               h1->type == h2->type &&
00075               h1->size == h2->size &&
00076               (OidCompare(&h1->oid_cl, &h2->oid_cl) ||
00077                !isOidValid(&h1->oid_cl))) ? True : False;
00078     if (!f)
00079       printf("OBJH CMP: %x %x, %d %d, %s %s\n",
00080              h1->type, h2->type,
00081              h1->size, h2->size,
00082              OidGetString(&h1->oid_cl), OidGetString(&h2->oid_cl));
00083 
00084     return f;
00085   }
00086 
00087   eyedbsm::Oid stringGetOid(const char *buf)
00088   {
00089     eyedbsm::Oid oid;
00090     eyedbsm::Oid::NX nx;
00091     eyedbsm::Oid::Unique unique;
00092     eyedbsm::Oid::DbID dbid;
00093 
00094     static eyedbsm::Oid null_oid;
00095     int len = strlen(buf);
00096     char c = *buf;
00097 
00098     if (c < '0' || c > '9' || len < 9)
00099       return null_oid;
00100 
00101     if (strcmp(&buf[len-4], ":oid"))
00102       return null_oid;
00103   
00104     if (sscanf(buf, "%u.%u.%u:oid", &nx, &dbid, &unique) == 3 ||
00105         sscanf(buf, "%u:%u:%u:oid", &nx, &dbid, &unique) == 3) {
00106       oid.setNX(nx);
00107       eyedbsm::dbidSet(&oid, dbid);
00108       oid.setUnique(unique);
00109       return oid;
00110     }
00111     return null_oid;
00112   }
00113 
00114   const char *OidGetString(const eyedbsm::Oid *oid)
00115   {
00116 #define NBUFS 8
00117     static char bufs[NBUFS][32];
00118     static int which;
00119 
00120     if (which == NBUFS)
00121       which = 0;
00122 
00123     if (isOidValid(oid))
00124       sprintf(bufs[which], eyedbsm::getOidString(oid));
00125     else
00126       strcpy(bufs[which], NullString);
00127 
00128     return bufs[which++];
00129   }
00130 
00131 #define INDENT_INC 8
00132 
00133   char *make_indent(int indent)
00134   {
00135     char *indent_str = (char *)malloc(indent < 0 ? 1 : indent+1);
00136     char *p;
00137     int i;
00138 
00139     if (indent < 0)
00140       indent = 0;
00141 
00142     for (p = indent_str, i = 0; i < indent; i++, p++)
00143       *p = ' ';
00144 
00145     *p = 0;
00146     return indent_str;
00147   }
00148 
00149   void delete_indent(char *indent_str)
00150   {
00151     free(indent_str);
00152   }
00153 
00154   void dump_data(Data data, Size size)
00155   {
00156     unsigned char *p = (unsigned char *)data;
00157     static const char sep[] =
00158       "------------------------------------------------------------";
00159 
00160     printf("%s\n", sep);
00161     printf("Dumping data 0x%x [%d]\n", data, size);
00162 
00163     while (size--)
00164       printf("0x%x ", *p++);
00165 
00166     printf("\n%s\n", sep);
00167   }
00168 
00169   const char *makeName(const char *name, const char *prefix)
00170   {
00171     if (prefix)
00172       {
00173         static char bufname[256];
00174 #if 0
00175         const char *p = strrchr(prefix, ':');
00176         p = (!p ? prefix : p+1);
00177 #else
00178         const char *p = prefix;
00179 #endif
00180         sprintf(bufname, "%s%s", p, name);
00181         return bufname;
00182       }
00183 
00184     return name;
00185   }
00186 
00187   static int trace = 0;
00188 
00189   void
00190   set_trace(int tr)
00191   {
00192     trace = tr;
00193   }
00194 
00195   void tr(const char *fname, const char *fmt, ...)
00196   {
00197     if (trace)
00198       {
00199         va_list ap;
00200         char s[512];
00201 
00202         va_start(ap, fmt);
00203 
00204         vsprintf(s, fmt, ap);
00205 
00206         printf("%s: %s\n", fname, s);
00207         va_end(ap);
00208       }
00209   }
00210 
00211   /*#define IDB_PROBE*/
00212 
00213   void idbPROBE(const char *fmt, ...)
00214   {
00215 #ifdef IDB_PROBE
00216     va_list ap;
00217     static struct timeval tp_s;
00218     struct timeval tp;
00219     int ms;
00220 
00221     gettimeofday(&tp, NULL);
00222 
00223     if (!tp_s.tv_sec)
00224       tp_s = tp;
00225   
00226     ms = ((tp.tv_sec - tp_s.tv_sec) * 1000) + ((tp.tv_usec - tp_s.tv_usec)/1000);
00227 
00228     va_start(ap, fmt);
00229 
00230     vfprintf(stdout, fmt, ap);
00231     fprintf(stdout, ": %d ms\n", ms);
00232     va_end(ap);
00233 #endif
00234   }
00235 
00236   static void
00237   clean(const char *tmpfile_1, const char *tmpfile_2)
00238   {
00239     unlink(tmpfile_1);
00240     if (*tmpfile_2)
00241       unlink(tmpfile_2);
00242   }
00243 
00244   FILE *
00245   run_cpp(FILE *fd, const char *cpp_cmd, const char *cpp_flags,
00246           const char *file)
00247   {
00248     if (!cpp_cmd)
00249       cpp_cmd = eyedb::ClientConfig::getCValue("cpp_cmd");
00250 
00251     if (!cpp_flags || !*cpp_flags)
00252       cpp_flags = eyedb::ClientConfig::getCValue("cpp_flags");
00253 
00254     if (!cpp_flags)
00255       cpp_flags = "";
00256 
00257     if (cpp_cmd) {
00258       fclose(fd);
00259 
00260       char *tmpfile_1, *tmpfile_2;
00261 
00262       char templ1[] =  "/tmp/eyedb-cpp.XXXXXX";
00263       tmpfile_1 = mktemp(templ1);
00264 
00265       char cmd[512];
00266 
00267       // 12/01/99 all those commands are rather fragile!!!
00268       
00269       // first pass: because the C preprocessor does not
00270       // deal well with C++ the comments '//'
00271       sprintf(cmd, "sed -e 's|//.*||' %s | %s %s - > %s", file, cpp_cmd,
00272               cpp_flags, tmpfile_1);
00273 
00274       if (system(cmd)) {
00275         fprintf(stderr, "command '%s' failed. Perharps the C preprocessor command '%s%s%s' is not correct\n", cmd, cpp_cmd,
00276                 (cpp_flags && *cpp_flags ? " " : ""), cpp_flags); 
00277         clean(tmpfile_1, "");
00278         return 0;
00279       }
00280 
00281       // second pass: to substitute the "<stdin>" file directive
00282       // to the true file directive
00283       char templ2[] = "/tmp/eyedb-cpp.out.XXXXXX";
00284       tmpfile_2 = mktemp(templ2);
00285 
00286       sprintf(cmd, "sed -e 's|<stdin>|%s|g' %s > %s",
00287               file, tmpfile_1, tmpfile_2);
00288 
00289       if (system(cmd)) {
00290         clean(tmpfile_1, tmpfile_2);
00291         return 0;
00292       }
00293 
00294       // third pass: to deal well with the ## preprocessor directive
00295       // and to transform ': :' to '::'
00296       sprintf(cmd, "sed -e 's/ ## //g' -e 's/## //g' -e 's/ ##//g' "
00297               "-e 's/# \\([a-zA-Z_][a-zA-Z_0-9]*\\)/\"\\1\"/g' "
00298               "-e 's/^\\\\#/#/' -e 's/##//g' -e 's/: :/::/g' %s | "
00299               "grep -v \"^#ident\" | grep -v \"^#pragma\" > %s",
00300               tmpfile_2, tmpfile_1);
00301               
00302       if (system(cmd)) {
00303         clean(tmpfile_1, tmpfile_2);
00304         return 0;
00305       }
00306               
00307       FILE *odlin = fopen(tmpfile_1, "r");
00308 
00309       clean(tmpfile_1, tmpfile_2);
00310       if (!odlin) {
00311         fprintf(stderr,
00312                 "eyedbodl: cannot open file '%s' for reading\n",
00313                 tmpfile);
00314         return 0;
00315       }
00316 
00317       return odlin;
00318     }
00319 
00320     return fd;
00321   }
00322 }

Generated on Mon Dec 22 18:15:58 2008 for eyedb by  doxygen 1.5.3