ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/gcdb.cpp
(Generate patch)
# Line 1 | Line 1
1   #include "gcdb.h"
2   #include <errno.h>
3 < #include <stdio.h>
4 < #include <stdarg.h>
5 < #if defined(__WIN32__) || defined(WIN32)
6 <  #include <windows.h>
7 < /*   m m a p           ===      got from imagick sources
3 >
4 > #ifdef __WIN32__
5 > /*   m m a p           ===      from imagick sources
6   %  Method mmap emulates the Unix method of the same name.
7   %  The format of the mmap method is:
8   %    void *mmap(char *address,size_t length,int protection,
# Line 78 | Line 76
76  
77  
78   int cdbInfoSIZE=offsetof(cdbInfo, tag)+4;
79 <
79 > int IdxDataSIZE=offsetof(CIdxData, reclen)+sizeof(uint32);
80 > int IdxDataSIZE32=offsetof(CIdxData32, reclen)+sizeof(uint32);
81 > /*
82 > int IdxSeqDataSIZE=offsetof(CIdxSeqData, elen)+sizeof(byte);
83 > int IdxSeqDataSIZE32=offsetof(CIdxSeqData32, elen)+sizeof(byte);
84 > */
85  
86   //=====================================================
87   //-------------     buffer stuff    -------------------
# Line 147 | Line 150
150  
151   static unsigned int avail = SPACE; /* multiple of ALIGNMENT; 0<=avail<=SPACE */
152  
153 < offt_conv gcvt_offt;
154 < uint_conv gcvt_uint;
155 <
153 > offt_conv_func gcvt_offt;
154 > uint_conv_func gcvt_uint;
155 > int16_conv_func gcvt_int16;
156  
157   char *gcdb_alloc(unsigned int n) {
158    char *x;
# Line 342 | Line 345
345   //------------------------------------------------
346   //------------    allocation routines:
347  
345 /*
346 big/little endian check
347 */
348 int endian_test(void) {
349 unsigned short v=0x0001;
350 unsigned char* b = (unsigned char*)&v;
351 return b[1];
352 }
348   /* conversion of unsigned int offsets read from a file
349     can also be used to prepare unsigned integers to be written
350     into a file in an independent platform manner
351   */
352  
353 + union UInt32Bytes {
354 +  unsigned char b[4];
355 +  int32_t ui;
356 + };
357 +
358 + union UInt16Bytes {
359 +  unsigned char b[2];
360 +  int16_t ui;
361 + };
362 +
363 +
364   unsigned int uint32_sun(void* x86int) {
365 + UInt32Bytes ub;
366 + ub.b[3]=((unsigned char*)x86int)[0];
367 + ub.b[0]=((unsigned char*)x86int)[3];
368 + ub.b[1]=((unsigned char*)x86int)[2];
369 + ub.b[2]=((unsigned char*)x86int)[1];
370 + return ub.ui;
371 + }
372 +
373 + int16_t int16_sun(void* x86int) {
374 + UInt16Bytes ub;
375 + ub.b[1]=((unsigned char*)x86int)[0];
376 + ub.b[0]=((unsigned char*)x86int)[1];
377 + return ub.ui;
378 + }
379 +
380 + /* unsigned int uint32_sun(void* x86int) {
381   unsigned char b[4];
382   b[3]=((unsigned char*)x86int)[0];
383   b[0]=((unsigned char*)x86int)[3];
384   b[1]=((unsigned char*)x86int)[2];
385   b[2]=((unsigned char*)x86int)[1];
386   return *((unsigned int*)b);
387 < }
388 <
387 > return *ub;
388 > }*/
389  
390   unsigned int uint32_x86(void* offt) {
391   return *((unsigned int*)offt);
392   }
393  
394 < //-------- 64bit types, if that's the case:
394 > int16_t int16_x86(void* v) {
395 > return *((int16_t *)v);
396 > }
397 >
398 > //-------- 64bit types conversion :
399 > union ULongBytes {
400 > unsigned char b[8];
401 > off_t ob;
402 > };
403 >
404 > off_t offt_sun(void* offt) {
405 > //unsigned char b[8];
406 > ULongBytes ub;
407 > if (sizeof(off_t)==8) { //64 bit?
408 >  // upper words:
409 >  ub.b[3]=((unsigned char*)offt)[4];
410 >  ub.b[0]=((unsigned char*)offt)[7];
411 >  ub.b[1]=((unsigned char*)offt)[6];
412 >  ub.b[2]=((unsigned char*)offt)[5];
413 >  //--
414 >  ub.b[7]=((unsigned char*)offt)[0];
415 >  ub.b[4]=((unsigned char*)offt)[3];
416 >  ub.b[5]=((unsigned char*)offt)[2];
417 >  ub.b[6]=((unsigned char*)offt)[1];
418 >  }
419 > else {
420 >  ub.b[3]=((unsigned char*)offt)[0];
421 >  ub.b[0]=((unsigned char*)offt)[3];
422 >  ub.b[1]=((unsigned char*)offt)[2];
423 >  ub.b[2]=((unsigned char*)offt)[1];
424 >  }
425 > //return *((off_t*)b);
426 > return ub.ob;
427 > }
428  
429 + /*
430   off_t offt_sun(void* offt) {
431   unsigned char b[8];
432   if (sizeof(off_t)==8) { //64 bit?
# Line 393 | Line 449
449    }
450   return *((off_t*)b);
451   }
452 <
452 > */
453  
454  
455   off_t offt_x86(void* offt) {
# Line 457 | Line 513
513    *u = result;
514   }
515  
516 + /*
517 + big/little endian check
518 + */
519 + int endian_test(void) {
520 + unsigned short v=0x0001;
521 + unsigned char* b = (unsigned char*)&v;
522 + return b[1];
523 + }
524 +
525 + void gcvt_endian_setup() {
526 + //check endianness
527 + if (endian_test()) {
528 +       gcvt_uint  = &uint32_sun;
529 +       gcvt_offt  = &offt_sun;
530 +       gcvt_int16 = &int16_sun;
531 +       }
532 +     else {
533 +       gcvt_uint  = &uint32_x86;
534 +       gcvt_offt  = &offt_x86;
535 +       gcvt_int16 = &int16_x86;
536 +       }
537 + }
538 +
539   //=====================================================
540   //-------------     cdb index       -------------------
541   //=====================================================
542  
543   GCdbWrite::GCdbWrite(int afd) {
544    //check endianness :)
545 <  gcvt_uint=(endian_test())? &uint32_sun : &uint32_x86;
467 <  gcvt_offt=(endian_test())? &offt_sun : &offt_x86;
545 >  gcvt_endian_setup();
546    cdbuf=new GCDBuffer((opfunc)&write,(int) afd,(char*)bspace,sizeof bspace);
547    head = NULL;
548    split = 0;
# Line 479 | Line 557
557   }
558  
559   GCdbWrite::GCdbWrite(char* afname) {
560 < #if defined(__WIN32__) || defined(WIN32)
560 > #ifdef __WIN32__
561     fd = open(afname,O_WRONLY | O_TRUNC | O_BINARY | O_CREAT, S_IREAD|S_IWRITE);
562   #else
563     fd = open(afname,O_WRONLY | O_NDELAY | O_TRUNC | O_CREAT, 0664);
# Line 487 | Line 565
565    if (fd == -1)
566      GError("GCdbWrite: Error creating file '%s'\n", fname);
567  
568 < //check endianness :)
491 <  gcvt_uint=(endian_test())? &uint32_sun : &uint32_x86;
492 <  gcvt_offt=(endian_test())? &offt_sun : &offt_x86;
568 >  gcvt_endian_setup();
569  
570    cdbuf=new GCDBuffer((opfunc)&write,(int) fd,(char*)bspace,sizeof bspace);
571    head = NULL;
# Line 505 | Line 581
581  
582   GCdbWrite::~GCdbWrite() {
583    cdbuf->flush();
584 <  #if !(defined(__WIN32__) || defined(WIN32))
584 >  #ifndef __WIN32__
585     /* NFS silliness  */
586     if (fsync(fd) == -1)
587        GError("GCdbWrite: Error at fsync() for file '%s'\n",
# Line 701 | Line 777
777    struct stat st;
778    char *x;
779    map=NULL;
780 < //check endianness :)
705 <  gcvt_uint=(endian_test())? &uint32_sun : &uint32_x86;
706 <  gcvt_offt=(endian_test())? &offt_sun : &offt_x86;
780 >  gcvt_endian_setup();
781  
782    findstart();
783    fd = afd;
784 <  if (fstat(fd,&st) == 0)
784 >  if (fstat(fd,&st) == 0) {
785      if (st.st_size <= MAX_UINT) {
786        #ifndef NO_MMAP
787        x = (char *) mmap(0,st.st_size,PROT_READ,MAP_SHARED,fd,0);
# Line 724 | Line 798
798         GError("Error mapping the file (size %ld > MAX_UINT)\n",
799             st.st_size);
800         }
801 +    }
802   }
803  
804   GCdbRead::GCdbRead(char* afname) {
805    struct stat st;
806    char *x;
807    map=NULL;
808 <  //check endianness :)
734 <  gcvt_uint=(endian_test())? &uint32_sun : &uint32_x86;
735 <  gcvt_offt=(endian_test())? &offt_sun : &offt_x86;
736 <
808 >  gcvt_endian_setup();
809  
810    findstart();
811    #ifdef __WIN32__
# Line 744 | Line 816
816    if (fd == -1)
817       GError("Error: cannot open file %s\n", afname);
818    strcpy(fname, afname);
819 <  if (fstat(fd,&st) == 0)
819 >  if (fstat(fd,&st) == 0) {
820      if (st.st_size <= MAX_UINT) {
821       #ifndef NO_MMAP
822        x = (char *) mmap(0,st.st_size,PROT_READ,MAP_SHARED,fd,0);
# Line 761 | Line 833
833         GError("GCdbRead: Error mapping the file (size %ld > MAX_UINT)\n",
834             st.st_size);
835         }
836 +   }
837   }
838  
839  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines