1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "MacabHeader.hxx" 32*cdf0e10cSrcweir #include "MacabRecord.hxx" 33*cdf0e10cSrcweir #include "macabutilities.hxx" 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir #include <math.h> 36*cdf0e10cSrcweir #include <com/sun/star/sdbc/DataType.hpp> 37*cdf0e10cSrcweir #include <connectivity/dbconversion.hxx> 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir using namespace connectivity::macab; 40*cdf0e10cSrcweir using namespace com::sun::star::sdbc; 41*cdf0e10cSrcweir using namespace com::sun::star::util; 42*cdf0e10cSrcweir using namespace ::dbtools; 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir // ------------------------------------------------------------------------- 45*cdf0e10cSrcweir MacabHeader::MacabHeader(const sal_Int32 _size, macabfield **_fields) 46*cdf0e10cSrcweir { 47*cdf0e10cSrcweir sal_Int32 i; 48*cdf0e10cSrcweir size = _size; 49*cdf0e10cSrcweir fields = new macabfield *[size]; 50*cdf0e10cSrcweir for(i = 0; i < size; i++) 51*cdf0e10cSrcweir { 52*cdf0e10cSrcweir if(_fields[i] == NULL) 53*cdf0e10cSrcweir { 54*cdf0e10cSrcweir fields[i] = NULL; 55*cdf0e10cSrcweir } 56*cdf0e10cSrcweir else 57*cdf0e10cSrcweir { 58*cdf0e10cSrcweir /* The constructor duplicates the macabfields it gets because they 59*cdf0e10cSrcweir * are either deleted later or used for other purposes. 60*cdf0e10cSrcweir */ 61*cdf0e10cSrcweir fields[i] = new macabfield; 62*cdf0e10cSrcweir fields[i]->type = _fields[i]->type; 63*cdf0e10cSrcweir fields[i]->value = _fields[i]->value; 64*cdf0e10cSrcweir if (fields[i]->value) 65*cdf0e10cSrcweir CFRetain(fields[i]->value); 66*cdf0e10cSrcweir } 67*cdf0e10cSrcweir } 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir // ------------------------------------------------------------------------- 72*cdf0e10cSrcweir MacabHeader::MacabHeader() 73*cdf0e10cSrcweir { 74*cdf0e10cSrcweir size = 0; 75*cdf0e10cSrcweir fields = NULL; 76*cdf0e10cSrcweir } 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir // ------------------------------------------------------------------------- 79*cdf0e10cSrcweir MacabHeader::~MacabHeader() 80*cdf0e10cSrcweir { 81*cdf0e10cSrcweir } 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir // ------------------------------------------------------------------------- 84*cdf0e10cSrcweir void MacabHeader::operator+= (const MacabHeader *r) 85*cdf0e10cSrcweir { 86*cdf0e10cSrcweir /* Add one MacabHeader to another. Anything not already in the header is 87*cdf0e10cSrcweir * added to the end of it. 88*cdf0e10cSrcweir */ 89*cdf0e10cSrcweir sal_Int32 rSize = r->getSize(); 90*cdf0e10cSrcweir if(rSize != 0) // If the new header does actually have fields 91*cdf0e10cSrcweir { 92*cdf0e10cSrcweir /* If our header is currently empty, just copy all of the fields from 93*cdf0e10cSrcweir * the new header to this one. 94*cdf0e10cSrcweir */ 95*cdf0e10cSrcweir if(size == 0) 96*cdf0e10cSrcweir { 97*cdf0e10cSrcweir sal_Int32 i; 98*cdf0e10cSrcweir size = rSize; 99*cdf0e10cSrcweir fields = new macabfield *[size]; 100*cdf0e10cSrcweir for(i = 0; i < size; i++) 101*cdf0e10cSrcweir { 102*cdf0e10cSrcweir fields[i] = r->copy(i); 103*cdf0e10cSrcweir } 104*cdf0e10cSrcweir } 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir /* Otherwise, only add the duplicates. We do this with a two-pass 107*cdf0e10cSrcweir * approach. First, find out how many fields to add, then reallocate 108*cdf0e10cSrcweir * the size of the fields array and add the old ones at the end. 109*cdf0e10cSrcweir * (More precisely, we create a _new_ fields array with the new length 110*cdf0e10cSrcweir * allocated to it, then get all of the fields from the current 111*cdf0e10cSrcweir * fields array to it, then copy the non-duplicates from the new 112*cdf0e10cSrcweir * header to the end.) 113*cdf0e10cSrcweir */ 114*cdf0e10cSrcweir else 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir sal_Int32 i; 117*cdf0e10cSrcweir sal_Int32 numToAdd = 0, numAdded = 0; 118*cdf0e10cSrcweir macabfield **newFields; 119*cdf0e10cSrcweir for( i = 0; i < rSize; i++) 120*cdf0e10cSrcweir { 121*cdf0e10cSrcweir if(!contains(r->get(i))) 122*cdf0e10cSrcweir { 123*cdf0e10cSrcweir numToAdd++; 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir } 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir newFields = new macabfield *[size+numToAdd]; 128*cdf0e10cSrcweir for(i = 0; i < size; i++) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir newFields[i] = copy(i); 131*cdf0e10cSrcweir } 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir for( i = 0; i < rSize; i++) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir if(!contains(r->get(i))) 136*cdf0e10cSrcweir { 137*cdf0e10cSrcweir newFields[size+numAdded] = r->copy(i); 138*cdf0e10cSrcweir numAdded++; 139*cdf0e10cSrcweir if(numAdded == numToAdd) 140*cdf0e10cSrcweir break; 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir } 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir releaseFields(); 145*cdf0e10cSrcweir delete [] fields; 146*cdf0e10cSrcweir size += numAdded; 147*cdf0e10cSrcweir fields = newFields; 148*cdf0e10cSrcweir } 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir } 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir // ------------------------------------------------------------------------- 153*cdf0e10cSrcweir ::rtl::OUString MacabHeader::getString(const sal_Int32 i) const 154*cdf0e10cSrcweir { 155*cdf0e10cSrcweir ::rtl::OUString nRet; 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir if(i < size) 158*cdf0e10cSrcweir { 159*cdf0e10cSrcweir if(fields[i] == NULL || fields[i]->value == NULL || CFGetTypeID(fields[i]->value) != CFStringGetTypeID()) 160*cdf0e10cSrcweir return ::rtl::OUString(); 161*cdf0e10cSrcweir try 162*cdf0e10cSrcweir { 163*cdf0e10cSrcweir nRet = CFStringToOUString( (CFStringRef) fields[i]->value); 164*cdf0e10cSrcweir } 165*cdf0e10cSrcweir catch(...){ } 166*cdf0e10cSrcweir } 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir return nRet; 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir // ------------------------------------------------------------------------- 172*cdf0e10cSrcweir void MacabHeader::sortRecord() 173*cdf0e10cSrcweir { 174*cdf0e10cSrcweir sortRecord(0,size); 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir // ------------------------------------------------------------------------- 178*cdf0e10cSrcweir macabfield **MacabHeader::sortRecord(const sal_Int32 _start, const sal_Int32 _length) 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir /* Sort using mergesort. Because it uses mergesort, it is recursive and 181*cdf0e10cSrcweir * not in place (so it creates a new array at every step of the 182*cdf0e10cSrcweir * recursion), so if you prefer to use a different sort, please feel 183*cdf0e10cSrcweir * free to implement it. 184*cdf0e10cSrcweir */ 185*cdf0e10cSrcweir macabfield** sorted = new macabfield *[_length]; 186*cdf0e10cSrcweir if(_length <= 2) 187*cdf0e10cSrcweir { 188*cdf0e10cSrcweir if(_length == 2) 189*cdf0e10cSrcweir { 190*cdf0e10cSrcweir if(compareFields(fields[_start], fields[_start+1]) > 0) 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir sorted[0] = get(_start+1); 193*cdf0e10cSrcweir sorted[1] = get(_start); 194*cdf0e10cSrcweir } 195*cdf0e10cSrcweir else 196*cdf0e10cSrcweir { 197*cdf0e10cSrcweir sorted[0] = get(_start); 198*cdf0e10cSrcweir sorted[1] = get(_start+1); 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir } 201*cdf0e10cSrcweir else if(_length == 1) 202*cdf0e10cSrcweir { 203*cdf0e10cSrcweir sorted[0] = get(_start); 204*cdf0e10cSrcweir } 205*cdf0e10cSrcweir } 206*cdf0e10cSrcweir else 207*cdf0e10cSrcweir { 208*cdf0e10cSrcweir sal_Int32 halfLength = floor(_length/2); 209*cdf0e10cSrcweir sal_Int32 fp = 0, lp = 0; 210*cdf0e10cSrcweir sal_Int32 i; 211*cdf0e10cSrcweir macabfield **firstHalf = new macabfield *[halfLength]; 212*cdf0e10cSrcweir macabfield **lastHalf = new macabfield *[_length - halfLength]; 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir firstHalf = sortRecord(_start, halfLength); 215*cdf0e10cSrcweir lastHalf = sortRecord(_start+halfLength, _length-halfLength); 216*cdf0e10cSrcweir for(i = 0; i < _length; i++) 217*cdf0e10cSrcweir { 218*cdf0e10cSrcweir if(compareFields(firstHalf[fp],lastHalf[lp]) < 0) 219*cdf0e10cSrcweir { 220*cdf0e10cSrcweir sorted[i] = firstHalf[fp++]; 221*cdf0e10cSrcweir if(fp == halfLength) 222*cdf0e10cSrcweir { 223*cdf0e10cSrcweir for( i++; i < _length; i++) 224*cdf0e10cSrcweir { 225*cdf0e10cSrcweir sorted[i] = lastHalf[lp++]; 226*cdf0e10cSrcweir } 227*cdf0e10cSrcweir break; 228*cdf0e10cSrcweir } 229*cdf0e10cSrcweir } 230*cdf0e10cSrcweir else 231*cdf0e10cSrcweir { 232*cdf0e10cSrcweir sorted[i] = lastHalf[lp++]; 233*cdf0e10cSrcweir if(lp == _length - halfLength) 234*cdf0e10cSrcweir { 235*cdf0e10cSrcweir for( i++; i < _length; i++) 236*cdf0e10cSrcweir { 237*cdf0e10cSrcweir sorted[i] = firstHalf[fp++]; 238*cdf0e10cSrcweir } 239*cdf0e10cSrcweir break; 240*cdf0e10cSrcweir } 241*cdf0e10cSrcweir } 242*cdf0e10cSrcweir } 243*cdf0e10cSrcweir if(_length == size) 244*cdf0e10cSrcweir { 245*cdf0e10cSrcweir fields = sorted; 246*cdf0e10cSrcweir } 247*cdf0e10cSrcweir } 248*cdf0e10cSrcweir return sorted; 249*cdf0e10cSrcweir } 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir sal_Int32 MacabHeader::compareFields(const macabfield *_field1, const macabfield *_field2) 252*cdf0e10cSrcweir { 253*cdf0e10cSrcweir /* Comparing two fields in a MacabHeader is different than comparing two 254*cdf0e10cSrcweir * fields in a MacabRecord. It starts in the same way (if one of the two 255*cdf0e10cSrcweir * fields is NULL, it belongs after the other, so it is considered 256*cdf0e10cSrcweir * "greater"). But, then, all headers are CFStrings, no matter what 257*cdf0e10cSrcweir * type they claim to be (since they actually hold the expected type for 258*cdf0e10cSrcweir * the records with that header). That being said, all we have to do is 259*cdf0e10cSrcweir * the built-in CFStringCompare. 260*cdf0e10cSrcweir */ 261*cdf0e10cSrcweir if(_field1 == _field2) 262*cdf0e10cSrcweir return 0; 263*cdf0e10cSrcweir if(_field1 == NULL) 264*cdf0e10cSrcweir return 1; 265*cdf0e10cSrcweir if(_field2 == NULL) 266*cdf0e10cSrcweir return -1; 267*cdf0e10cSrcweir 268*cdf0e10cSrcweir CFComparisonResult result = CFStringCompare( 269*cdf0e10cSrcweir (CFStringRef) _field1->value, 270*cdf0e10cSrcweir (CFStringRef) _field2->value, 271*cdf0e10cSrcweir 0); // 0 = no options (like ignore case) 272*cdf0e10cSrcweir 273*cdf0e10cSrcweir return (sal_Int32) result; 274*cdf0e10cSrcweir } 275*cdf0e10cSrcweir 276*cdf0e10cSrcweir // ------------------------------------------------------------------------- 277*cdf0e10cSrcweir sal_Int32 MacabHeader::getColumnNumber(const ::rtl::OUString s) const 278*cdf0e10cSrcweir { 279*cdf0e10cSrcweir sal_Int32 i; 280*cdf0e10cSrcweir for(i = 0; i < size; i++) 281*cdf0e10cSrcweir { 282*cdf0e10cSrcweir if(getString(i) == s) 283*cdf0e10cSrcweir break; 284*cdf0e10cSrcweir } 285*cdf0e10cSrcweir 286*cdf0e10cSrcweir if(i == size) 287*cdf0e10cSrcweir i = -1; 288*cdf0e10cSrcweir 289*cdf0e10cSrcweir return i; 290*cdf0e10cSrcweir } 291*cdf0e10cSrcweir 292*cdf0e10cSrcweir // ------------------------------------------------------------------------- 293*cdf0e10cSrcweir MacabHeader *MacabHeader::begin() 294*cdf0e10cSrcweir { 295*cdf0e10cSrcweir return this; 296*cdf0e10cSrcweir } 297*cdf0e10cSrcweir 298*cdf0e10cSrcweir // ------------------------------------------------------------------------- 299*cdf0e10cSrcweir MacabHeader::iterator::iterator () 300*cdf0e10cSrcweir { 301*cdf0e10cSrcweir } 302*cdf0e10cSrcweir 303*cdf0e10cSrcweir // ------------------------------------------------------------------------- 304*cdf0e10cSrcweir MacabHeader::iterator::~iterator () 305*cdf0e10cSrcweir { 306*cdf0e10cSrcweir } 307*cdf0e10cSrcweir 308*cdf0e10cSrcweir void MacabHeader::iterator::operator= (MacabHeader *_record) 309*cdf0e10cSrcweir { 310*cdf0e10cSrcweir id = 0; 311*cdf0e10cSrcweir record = _record; 312*cdf0e10cSrcweir } 313*cdf0e10cSrcweir 314*cdf0e10cSrcweir // ------------------------------------------------------------------------- 315*cdf0e10cSrcweir void MacabHeader::iterator::operator++ () 316*cdf0e10cSrcweir { 317*cdf0e10cSrcweir id++; 318*cdf0e10cSrcweir } 319*cdf0e10cSrcweir 320*cdf0e10cSrcweir // ------------------------------------------------------------------------- 321*cdf0e10cSrcweir sal_Bool MacabHeader::iterator::operator!= (const sal_Int32 i) const 322*cdf0e10cSrcweir { 323*cdf0e10cSrcweir return(id != i); 324*cdf0e10cSrcweir } 325*cdf0e10cSrcweir 326*cdf0e10cSrcweir // ------------------------------------------------------------------------- 327*cdf0e10cSrcweir sal_Bool MacabHeader::iterator::operator== (const sal_Int32 i) const 328*cdf0e10cSrcweir { 329*cdf0e10cSrcweir return(id == i); 330*cdf0e10cSrcweir } 331*cdf0e10cSrcweir 332*cdf0e10cSrcweir // ------------------------------------------------------------------------- 333*cdf0e10cSrcweir macabfield *MacabHeader::iterator::operator* () const 334*cdf0e10cSrcweir { 335*cdf0e10cSrcweir return record->get(id); 336*cdf0e10cSrcweir } 337*cdf0e10cSrcweir 338*cdf0e10cSrcweir // ------------------------------------------------------------------------- 339*cdf0e10cSrcweir sal_Int32 MacabHeader::end() const 340*cdf0e10cSrcweir { 341*cdf0e10cSrcweir return size; 342*cdf0e10cSrcweir } 343*cdf0e10cSrcweir 344