123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892 |
- #include "stdafx.h"
- #include "BasicExcelVC6.h"
- namespace YCompoundFiles
- {
- Block::Block() :
- blockSize_(512), fileSize_(0), indexEnd_(0),
- filename_(0) {}
- bool Block::Create(const wchar_t* filename)
- {
-
- size_t filenameLength = wcslen(filename);
- char* name = new char[filenameLength+1];
- wcstombs(name, filename, filenameLength);
- name[filenameLength] = 0;
- file_.open(name, ios_base::out | ios_base::trunc);
- file_.close();
- file_.clear();
-
- bool ret = this->Open(filename);
- delete[] name;
- return ret;
- }
- bool Block::Open(const wchar_t* filename, ios_base::openmode mode)
- {
-
- size_t filenameLength = wcslen(filename);
- filename_.resize(filenameLength+1, 0);
- wcstombs(&*(filename_.begin()), filename, filenameLength);
- file_.open(&*(filename_.begin()), mode | ios_base::binary);
- if (!file_.is_open()) return false;
- mode_ = mode;
-
-
- if (mode & ios_base::in)
- {
- file_.seekg(0, ios_base::end);
- fileSize_ = file_.tellg();
- }
- else if (mode & ios_base::out)
- {
- file_.seekp(0, ios_base::end);
- fileSize_ = file_.tellp();
- }
- else
- {
- this->Close();
- return false;
- }
-
-
- indexEnd_ = fileSize_/blockSize_ + (fileSize_ % blockSize_ ? 1 : 0);
- return true;
- }
- bool Block::Close()
- {
- file_.close();
- file_.clear();
- filename_.clear();
- fileSize_ = 0;
- indexEnd_ = 0;
- blockSize_ = 512;
- return !file_.is_open();
- }
- bool Block::IsOpen()
- {
- return file_.is_open();
- }
- bool Block::Read(size_t index, char* block)
- {
- if (!(mode_ & ios_base::in)) return false;
- if (index < indexEnd_)
- {
- file_.seekg(index * blockSize_);
- file_.read(block, blockSize_);
- return !file_.fail();
- }
- else return false;
- }
- bool Block::Write(size_t index, const char* block)
- {
- if (!(mode_ & ios_base::out)) return false;
- file_.seekp(index * blockSize_);
- file_.write(block, blockSize_);
- if (indexEnd_ <= index)
- {
- indexEnd_ = index + 1;
- fileSize_ += blockSize_;
- }
- file_.close();
- file_.clear();
- file_.open(&*(filename_.begin()), mode_ | ios_base::binary);
- return file_.is_open();
- }
- bool Block::Swap(size_t index1, size_t index2)
- {
- if (!(mode_ & ios_base::out)) return false;
- if (index1 < indexEnd_ && index2 < indexEnd_)
- {
- if (index1 == index2) return true;
-
- char* block1 = new char[blockSize_];
- if (!this->Read(index1, block1)) return false;
-
- char* block2 = new char[blockSize_];
- if (!this->Read(index2, block2)) return false;
-
- if (!this->Write(index1, block2)) return false;
- if (!this->Write(index2, block1)) return false;
-
- delete[] block1;
- delete[] block2;
- return true;
- }
- else return false;
- }
- bool Block::Move(size_t from, size_t to)
- {
- if (!(mode_ & ios_base::out)) return false;
- if (from < indexEnd_ && to < indexEnd_)
- {
- if (to > from)
- {
- for (size_t i=from; i!=to; ++i)
- {
- if (!this->Swap(i, i+1)) return false;
- }
- }
- else
- {
- for (size_t i=from; i!=to; --i)
- {
- if (!this->Swap(i, i-1)) return false;
- }
- }
- return true;
- }
- else return false;
- }
- bool Block::Insert(size_t index, const char* block)
- {
- if (!(mode_ & ios_base::out)) return false;
- if (index <= indexEnd_)
- {
-
- if (!this->Write(indexEnd_, block)) return false;
-
- if (index < indexEnd_-1) return this->Move(indexEnd_-1, index);
- else return true;
- }
- else
- {
-
- return this->Write(index, block);
- }
- }
- bool Block::Erase(size_t index)
- {
- if (!(mode_ & ios_base::out)) return false;
- if (index < indexEnd_)
- {
- fileSize_ -= blockSize_;
- indexEnd_ -= 1;
-
-
- char* buffer = new char[fileSize_];
- for (size_t i=0, j=0; i!=indexEnd_+1; ++i)
- {
- file_.seekg(i*blockSize_);
- if (i != index)
- {
- file_.read(buffer+j*blockSize_, blockSize_);
- ++j;
- }
- }
- file_.close();
- file_.open(&*(filename_.begin()), ios_base::out | ios_base::trunc | ios_base::binary);
- file_.write(buffer, fileSize_);
- file_.close();
- file_.open(&*(filename_.begin()), mode_ | ios_base::binary);
- delete[] buffer;
- return true;
- }
- else return false;
- }
- bool Block::Erase(vector<size_t>& indices)
- {
- if (!(mode_ & ios_base::out)) return false;
-
- size_t maxIndices = indices.size();
- fileSize_ -= maxIndices*blockSize_;
- char* buffer = new char[fileSize_];
- for (size_t i=0, k=0; i!=indexEnd_; ++i)
- {
- file_.seekg(i*blockSize_);
- bool toDelete = false;
- for (size_t j=0; j<maxIndices; ++j)
- {
- if (i == indices[j])
- {
- toDelete = true;
- break;
- }
- }
- if (!toDelete)
- {
- file_.read(buffer+k*blockSize_, blockSize_);
- ++k;
- }
- }
- indexEnd_ -= maxIndices;
-
- file_.close();
- file_.open(&*(filename_.begin()), ios_base::out | ios_base::trunc | ios_base::binary);
- file_.write(buffer, fileSize_);
- file_.close();
- file_.open(&*(filename_.begin()), mode_ | ios_base::binary);
- delete[] buffer;
- return true;
- }
- CompoundFile::Header::Header() :
- fileType_(0xE11AB1A1E011CFD0),
- uk1_(0), uk2_(0), uk3_(0), uk4_(0), uk5_(0x003B), uk6_(0x0003), uk7_(-2),
- log2BigBlockSize_(9), log2SmallBlockSize_(6),
- uk8_(0), uk9_(0), uk10_(0), uk11_(0x00001000),
- SBATStart_(-2), SBATCount_(0),
- XBATStart_(-2), XBATCount_(0),
- BATCount_(1), propertiesStart_(1)
- {
- BATArray_[0] = 0;
- fill (BATArray_+1, BATArray_+109, -1);
- Initialize();
- }
- void CompoundFile::Header::Write(char* block)
- {
- LittleEndian::Write(block, fileType_, 0x0000, 8);
- LittleEndian::Write(block, uk1_, 0x0008, 4);
- LittleEndian::Write(block, uk2_, 0x000C, 4);
- LittleEndian::Write(block, uk3_, 0x0010, 4);
- LittleEndian::Write(block, uk4_, 0x0014, 4);
- LittleEndian::Write(block, uk5_, 0x0018, 2);
- LittleEndian::Write(block, uk6_, 0x001A, 2);
- LittleEndian::Write(block, uk7_, 0x001C, 2);
- LittleEndian::Write(block, log2BigBlockSize_, 0x001E, 2);
- LittleEndian::Write(block, log2SmallBlockSize_, 0x0020, 4);
- LittleEndian::Write(block, uk8_, 0x0024, 4);
- LittleEndian::Write(block, uk9_, 0x0028, 4);
- LittleEndian::Write(block, BATCount_, 0x002C, 4);
- LittleEndian::Write(block, propertiesStart_, 0x0030, 4);
- LittleEndian::Write(block, uk10_, 0x0034, 4);
- LittleEndian::Write(block, uk11_, 0x0038, 4);
- LittleEndian::Write(block, SBATStart_, 0x003C, 4);
- LittleEndian::Write(block, SBATCount_, 0x0040, 4);
- LittleEndian::Write(block, XBATStart_, 0x0044, 4);
- LittleEndian::Write(block, XBATCount_, 0x0048, 4);
- for (size_t i=0; i<109; ++i) LittleEndian::Write(block, BATArray_[i], 0x004C+i*4, 4);
- }
- void CompoundFile::Header::Read(char* block)
- {
- LittleEndian::Read(block, fileType_, 0x0000, 8);
- LittleEndian::Read(block, uk1_, 0x0008, 4);
- LittleEndian::Read(block, uk2_, 0x000C, 4);
- LittleEndian::Read(block, uk3_, 0x0010, 4);
- LittleEndian::Read(block, uk4_, 0x0014, 4);
- LittleEndian::Read(block, uk5_, 0x0018, 2);
- LittleEndian::Read(block, uk6_, 0x001A, 2);
- LittleEndian::Read(block, uk7_, 0x001C, 2);
- LittleEndian::Read(block, log2BigBlockSize_, 0x001E, 2);
- LittleEndian::Read(block, log2SmallBlockSize_, 0x0020, 4);
- LittleEndian::Read(block, uk8_, 0x0024, 4);
- LittleEndian::Read(block, uk9_, 0x0028, 4);
- LittleEndian::Read(block, BATCount_, 0x002C, 4);
- LittleEndian::Read(block, propertiesStart_, 0x0030, 4);
- LittleEndian::Read(block, uk10_, 0x0034, 4);
- LittleEndian::Read(block, uk11_, 0x0038, 4);
- LittleEndian::Read(block, SBATStart_, 0x003C, 4);
- LittleEndian::Read(block, SBATCount_, 0x0040, 4);
- LittleEndian::Read(block, XBATStart_, 0x0044, 4);
- LittleEndian::Read(block, XBATCount_, 0x0048, 4);
- for (size_t i=0; i<109; ++i) LittleEndian::Read(block, BATArray_[i], 0x004C+i*4, 4);
- Initialize();
- }
- void CompoundFile::Header::Initialize()
- {
- bigBlockSize_ = (size_t)pow(2.0, log2BigBlockSize_);
- smallBlockSize_ = (size_t)pow(2.0, log2SmallBlockSize_);
- }
- CompoundFile::Property::Property() :
- nameSize_(0),
- propertyType_(1), nodeColor_(1),
- previousProp_(-1), nextProp_(-1), childProp_(-1),
- uk1_(0), uk2_(0), uk3_(0), uk4_(0), uk5_(0),
- seconds1_(0), days1_(0), seconds2_(0), days2_(0),
- startBlock_(-2), size_(0)
- {
- fill (name_, name_+32, 0);
- }
- void CompoundFile::Property::Write(char* block)
- {
- LittleEndian::WriteString(block, name_, 0x00, 32);
- LittleEndian::Write(block, nameSize_, 0x40, 2);
- LittleEndian::Write(block, propertyType_, 0x42, 1);
- LittleEndian::Write(block, nodeColor_, 0x43, 1);
- LittleEndian::Write(block, previousProp_, 0x44, 4);
- LittleEndian::Write(block, nextProp_, 0x48, 4);
- LittleEndian::Write(block, childProp_, 0x4C, 4);
- LittleEndian::Write(block, uk1_, 0x50, 4);
- LittleEndian::Write(block, uk2_, 0x54, 4);
- LittleEndian::Write(block, uk3_, 0x58, 4);
- LittleEndian::Write(block, uk4_, 0x5C, 4);
- LittleEndian::Write(block, uk5_, 0x60, 4);
- LittleEndian::Write(block, seconds1_, 0x64, 4);
- LittleEndian::Write(block, days1_, 0x68, 4);
- LittleEndian::Write(block, seconds2_, 0x6C, 4);
- LittleEndian::Write(block, days2_, 0x70, 4);
- LittleEndian::Write(block, startBlock_, 0x74, 4);
- LittleEndian::Write(block, size_, 0x78, 4);
- }
- void CompoundFile::Property::Read(char* block)
- {
- LittleEndian::ReadString(block, name_, 0x00, 32);
- LittleEndian::Read(block, nameSize_, 0x40, 2);
- LittleEndian::Read(block, propertyType_, 0x42, 1);
- LittleEndian::Read(block, nodeColor_, 0x43, 1);
- LittleEndian::Read(block, previousProp_, 0x44, 4);
- LittleEndian::Read(block, nextProp_, 0x48, 4);
- LittleEndian::Read(block, childProp_, 0x4C, 4);
- LittleEndian::Read(block, uk1_, 0x50, 4);
- LittleEndian::Read(block, uk2_, 0x54, 4);
- LittleEndian::Read(block, uk3_, 0x58, 4);
- LittleEndian::Read(block, uk4_, 0x5C, 4);
- LittleEndian::Read(block, uk5_, 0x60, 4);
- LittleEndian::Read(block, seconds1_, 0x64, 4);
- LittleEndian::Read(block, days1_, 0x68, 4);
- LittleEndian::Read(block, seconds2_, 0x6C, 4);
- LittleEndian::Read(block, days2_, 0x70, 4);
- LittleEndian::Read(block, startBlock_, 0x74, 4);
- LittleEndian::Read(block, size_, 0x78, 4);
- }
- CompoundFile::PropertyTree::PropertyTree() {};
- CompoundFile::PropertyTree::~PropertyTree()
- {
- size_t maxChildren = children_.size();
- for (size_t i=0; i<maxChildren; ++i) delete children_[i];
- }
- CompoundFile::CompoundFile() :
- block_(512), properties_(0), propertyTrees_(0),
- blocksIndices_(0), sblocksIndices_(0) {};
- CompoundFile::~CompoundFile() {this->Close();}
- bool CompoundFile::Create(const wchar_t* filename)
- {
- Close();
- file_.Create(filename);
-
- header_ = Header();
- SaveHeader();
-
- blocksIndices_.clear();
- blocksIndices_.resize(128, -1);
- blocksIndices_[0] = -3;
- blocksIndices_[1] = -2;
- SaveBAT();
-
- Property* root = new Property;
- wcscpy(root->name_, L"Root Entry");
- root->propertyType_ = 5;
- properties_.push_back(root);
- SaveProperties();
-
- propertyTrees_ = new PropertyTree;
- propertyTrees_->parent_ = 0;
- propertyTrees_->self_ = properties_[0];
- propertyTrees_->index_ = 0;
- currentDirectory_ = propertyTrees_;
- return true;
- }
- bool CompoundFile::Open(const wchar_t* filename, ios_base::openmode mode)
- {
- Close();
- if (!file_.Open(filename, mode)) return false;
-
-
- if (!LoadHeader()) return false;
-
- LoadBAT();
-
- propertyTrees_ = new PropertyTree;
- LoadProperties();
- currentDirectory_ = propertyTrees_;
- return true;
- }
- bool CompoundFile::Close()
- {
- blocksIndices_.clear();
- sblocksIndices_.clear();
- size_t maxProperties = properties_.size();
- for (size_t i=0; i<maxProperties; ++i)
- {
- if (properties_[i]) delete properties_[i];
- }
- properties_.clear();
- if (propertyTrees_)
- {
- delete propertyTrees_;
- propertyTrees_ = 0;
- }
- previousDirectories_.clear();
- currentDirectory_ = 0;
- return file_.Close();
- }
- bool CompoundFile::IsOpen()
- {
- return file_.IsOpen();
- }
- int CompoundFile::ChangeDirectory(const wchar_t* path)
- {
- previousDirectories_.push_back(currentDirectory_);
-
- if (wcscmp(path, L".") == 0)
- {
-
- previousDirectories_.pop_back();
- return SUCCESS;
- }
- if (wcscmp(path, L"..") == 0)
- {
-
- if (currentDirectory_->parent_ != 0)
- {
- currentDirectory_ = currentDirectory_->parent_;
- }
- previousDirectories_.pop_back();
- return SUCCESS;
- }
- if (wcscmp(path, L"\\") == 0)
- {
-
- currentDirectory_ = propertyTrees_;
- previousDirectories_.pop_back();
- return SUCCESS;
- }
-
- size_t ipos = 0;
- size_t npos = 0;
- size_t pathLength = wcslen(path);
- if (pathLength > 0 && path[0] == L'\\')
- {
-
- currentDirectory_ = propertyTrees_;
- ++ipos;
- ++npos;
- }
- do
- {
- for (; npos<pathLength; ++npos)
- {
- if (path[npos] == L'\\') break;
- }
- wchar_t* directory = new wchar_t[npos-ipos+1];
- copy (path+ipos, path+npos, directory);
- directory[npos-ipos] = 0;
- currentDirectory_ = FindProperty(currentDirectory_, directory);
- delete[] directory;
- ipos = npos + 1;
- npos = ipos;
- if (currentDirectory_ == 0)
- {
-
- currentDirectory_ = previousDirectories_.back();
- previousDirectories_.pop_back();
- return DIRECTORY_NOT_FOUND;
- }
- } while (npos < pathLength);
- previousDirectories_.pop_back();
- return SUCCESS;
- }
- int CompoundFile::MakeDirectory(const wchar_t* path)
- {
- previousDirectories_.push_back(currentDirectory_);
- Property* property = new Property;
- property->propertyType_ = 1;
- int ret = MakeProperty(path, property);
- currentDirectory_ = previousDirectories_.back();
- previousDirectories_.pop_back();
- SaveHeader();
- SaveBAT();
- SaveProperties();
- return ret;
- }
- int CompoundFile::PresentWorkingDirectory(wchar_t* path)
- {
- previousDirectories_.push_back(currentDirectory_);
- vector<wchar_t> fullpath;
- do
- {
- size_t directoryLength = wcslen(currentDirectory_->self_->name_);
- vector<wchar_t> directory(directoryLength+1);
- directory[0] = L'\\';
- copy (currentDirectory_->self_->name_,
- currentDirectory_->self_->name_+directoryLength,
- directory.begin()+1);
- fullpath.insert(fullpath.begin(), directory.begin(), directory.end());
- } while (currentDirectory_ = currentDirectory_->parent_);
- fullpath.erase(fullpath.begin(), fullpath.begin()+11);
- if (fullpath.empty()) fullpath.push_back(L'\\');
- copy (fullpath.begin(), fullpath.end(), path);
- path[fullpath.size()] = 0;
- currentDirectory_ = previousDirectories_.back();
- previousDirectories_.pop_back();
- return SUCCESS;
- }
- int CompoundFile::PresentWorkingDirectory(vector<wchar_t>& path)
- {
- previousDirectories_.push_back(currentDirectory_);
- path.clear();
- do
- {
- size_t directoryLength = wcslen(currentDirectory_->self_->name_);
- vector<wchar_t> directory(directoryLength+1);
- directory[0] = L'\\';
- copy (currentDirectory_->self_->name_,
- currentDirectory_->self_->name_+directoryLength,
- directory.begin()+1);
- path.insert(path.begin(), directory.begin(), directory.end());
- } while (currentDirectory_ = currentDirectory_->parent_);
- path.erase(path.begin(), path.begin()+11);
- if (path.empty()) path.push_back(L'\\');
- currentDirectory_ = previousDirectories_.back();
- previousDirectories_.pop_back();
- return SUCCESS;
- }
- int CompoundFile::RemoveDirectory(const wchar_t* path)
- {
- PropertyTree* directory = FindProperty(path);
- if (directory == 0) return DIRECTORY_NOT_FOUND;
- if (directory->self_->childProp_ != -1) return DIRECTORY_NOT_EMPTY;
- DeletePropertyTree(directory);
- SaveHeader();
- SaveBAT();
- SaveProperties();
- return SUCCESS;
- }
- int CompoundFile::DelTree(const wchar_t* path)
- {
- previousDirectories_.push_back(currentDirectory_);
- PropertyTree* directory = FindProperty(path);
- if (directory == 0) return DIRECTORY_NOT_FOUND;
- if (directory->self_->childProp_ != -1)
- {
- size_t maxChildren = directory->children_.size();
- wchar_t* curpath = new wchar_t[65535];
- for (size_t i=0; i<maxChildren; ++i)
- {
- currentDirectory_ = directory->children_[i];
- PresentWorkingDirectory(curpath);
- if (directory->children_[i]->self_->propertyType_ == 1)
- {
-
- DelTree(curpath);
- }
- else if (directory->children_[i]->self_->propertyType_ == 2)
- {
-
- RemoveFile(curpath);
- }
- }
- directory->self_->childProp_ = -1;
- delete[] curpath;
- }
- if (directory->self_->propertyType_ == 1)
- {
-
- RemoveDirectory(path);
- }
- else if (directory->self_->propertyType_ == 2)
- {
-
- RemoveFile(path);
- }
- currentDirectory_ = previousDirectories_.back();
- previousDirectories_.pop_back();
- return SUCCESS;
- }
- int CompoundFile::DirectoryList(vector<vector<wchar_t> >& list, const wchar_t* path)
- {
- previousDirectories_.push_back(currentDirectory_);
- if (path != 0)
- {
- int ret = ChangeDirectory(path);
- if (ret != SUCCESS) return ret;
- }
- list.clear();
- size_t maxChildren = currentDirectory_->children_.size();
- vector<wchar_t> name(32);
- for (size_t i=0; i<maxChildren; ++i)
- {
- wcscpy(&*(name.begin()), currentDirectory_->children_[i]->self_->name_);
- list.push_back(name);
- }
- currentDirectory_ = previousDirectories_.back();
- previousDirectories_.pop_back();
- return SUCCESS;
- }
- int CompoundFile::MakeFile(const wchar_t* path)
- {
- previousDirectories_.push_back(currentDirectory_);
- Property* property = new Property;
- property->propertyType_ = 2;
- int ret = MakeProperty(path, property);
- currentDirectory_ = previousDirectories_.back();
- previousDirectories_.pop_back();
- SaveHeader();
- SaveBAT();
- SaveProperties();
- return ret;
- }
- int CompoundFile::RemoveFile(const wchar_t* path)
- {
- int ret = WriteFile(path, 0, 0);
- if (ret == SUCCESS)
- {
- DeletePropertyTree(FindProperty(path));
- SaveHeader();
- SaveBAT();
- SaveProperties();
- return SUCCESS;
- }
- else return ret;
- }
- int CompoundFile::FileSize(const wchar_t* path, size_t& size)
- {
-
- if (wcscmp(path, L"\\") == 0)
- {
- size = propertyTrees_->self_->size_;
- return SUCCESS;
- }
-
-
- PropertyTree* property = FindProperty(path);
- if (property == 0) return FILE_NOT_FOUND;
- else
- {
- size = property->self_->size_;
- return SUCCESS;
- }
- }
- int CompoundFile::ReadFile(const wchar_t* path, char* data)
- {
-
- char* buffer;
- if (wcscmp(path, L"\\") == 0)
- {
- buffer = new char[DataSize(propertyTrees_->self_->startBlock_, true)];
- ReadData(propertyTrees_->self_->startBlock_, buffer, true);
- copy (buffer, buffer+propertyTrees_->self_->size_, data);
- delete[] buffer;
- return SUCCESS;
- }
-
- PropertyTree* property = FindProperty(path);
- if (property == 0) return FILE_NOT_FOUND;
- if (property->self_->size_ >= 4096)
- {
-
- buffer = new char[DataSize(property->self_->startBlock_, true)];
- ReadData(property->self_->startBlock_, buffer, true);
- }
- else
- {
-
- buffer = new char[DataSize(property->self_->startBlock_, false)];
- ReadData(property->self_->startBlock_, buffer, false);
- }
-
- copy (buffer, buffer+property->self_->size_, data);
- delete[] buffer;
- return SUCCESS;
- }
- int CompoundFile::ReadFile(const wchar_t* path, vector<char>& data)
- {
- data.clear();
- size_t dataSize;
- int ret = FileSize(path, dataSize);
- if (ret != SUCCESS) return ret;
- data.resize(dataSize);
- return ReadFile(path, &*(data.begin()));
- }
- int CompoundFile::WriteFile(const wchar_t* path, const char* data, size_t size)
- {
- PropertyTree* property = FindProperty(path);
- if (property == 0) return FILE_NOT_FOUND;
-
- if (property->self_->size_ >= 4096)
- {
- if (size >= 4096) property->self_->startBlock_ = WriteData(data, size, property->self_->startBlock_, true);
- else
- {
- property->self_->startBlock_ = WriteData(0, 0, property->self_->startBlock_, true);
- property->self_->startBlock_ = WriteData(data, size, property->self_->startBlock_, false);
- }
- }
- else
- {
- if (size < 4096) property->self_->startBlock_ = WriteData(data, size, property->self_->startBlock_, false);
- else
- {
- property->self_->startBlock_ = WriteData(0, 0, property->self_->startBlock_, false);
- property->self_->startBlock_ = WriteData(data, size, property->self_->startBlock_, true);
- }
- }
- property->self_->size_ = size;
- SaveHeader();
- SaveBAT();
- SaveProperties();
- return SUCCESS;
- }
- int CompoundFile::WriteFile(const wchar_t* path, const vector<char>& data, size_t size)
- {
- return WriteFile(path, &*(data.begin()), size);
- }
- bool CompoundFile::Create(const char* filename)
- {
- size_t filenameLength = strlen(filename);
- wchar_t* wname = new wchar_t[filenameLength+1];
- mbstowcs(wname, filename, filenameLength);
- wname[filenameLength] = 0;
- bool ret = Create(wname);
- delete[] wname;
- return ret;
- }
- bool CompoundFile::Open(const char* filename, ios_base::openmode mode)
- {
- size_t filenameLength = strlen(filename);
- wchar_t* wname = new wchar_t[filenameLength+1];
- mbstowcs(wname, filename, filenameLength);
- wname[filenameLength] = 0;
- bool ret = Open(wname, mode);
- delete[] wname;
- return ret;
- }
- int CompoundFile::ChangeDirectory(const char* path)
- {
- size_t pathLength = strlen(path);
- wchar_t* wpath = new wchar_t[pathLength+1];
- mbstowcs(wpath, path, pathLength);
- wpath[pathLength] = 0;
- int ret = ChangeDirectory(wpath);
- delete[] wpath;
- return ret;
- }
- int CompoundFile::MakeDirectory(const char* path)
- {
- size_t pathLength = strlen(path);
- wchar_t* wpath = new wchar_t[pathLength+1];
- mbstowcs(wpath, path, pathLength);
- wpath[pathLength] = 0;
- int ret = MakeDirectory(wpath);
- delete[] wpath;
- return ret;
- }
- int CompoundFile::PresentWorkingDirectory(char* path)
- {
- size_t pathLength = strlen(path);
- wchar_t* wpath = new wchar_t[pathLength+1];
- int ret = PresentWorkingDirectory(wpath);
- if (ret == SUCCESS)
- {
- pathLength = wcslen(wpath);
- wcstombs(path, wpath, pathLength);
- path[pathLength] = 0;
- }
- delete[] wpath;
- return ret;
- }
- int CompoundFile::PresentWorkingDirectory(vector<char>& path)
- {
- vector<wchar_t> wpath;
- int ret = PresentWorkingDirectory(wpath);
- if (ret == SUCCESS)
- {
- size_t pathLength = wpath.size();
- path.resize(pathLength);
- wcstombs(&*(path.begin()), &*(wpath.begin()), pathLength);
- path[pathLength] = 0;
- }
- return ret;
- }
- int CompoundFile::RemoveDirectory(const char* path)
- {
- size_t pathLength = strlen(path);
- wchar_t* wpath = new wchar_t[pathLength+1];
- mbstowcs(wpath, path, pathLength);
- wpath[pathLength] = 0;
- int ret = RemoveDirectory(wpath);
- delete[] wpath;
- return ret;
- }
- int CompoundFile::DelTree(const char* path)
- {
- size_t pathLength = strlen(path);
- wchar_t* wpath = new wchar_t[pathLength+1];
- mbstowcs(wpath, path, pathLength);
- wpath[pathLength] = 0;
- int ret = DelTree(wpath);
- delete[] wpath;
- return ret;
- }
- int CompoundFile::MakeFile(const char* path)
- {
- size_t pathLength = strlen(path);
- wchar_t* wpath = new wchar_t[pathLength+1];
- mbstowcs(wpath, path, pathLength);
- wpath[pathLength] = 0;
- int ret = MakeFile(wpath);
- delete[] wpath;
- return ret;
- }
- int CompoundFile::RemoveFile(const char* path)
- {
- size_t pathLength = strlen(path);
- wchar_t* wpath = new wchar_t[pathLength+1];
- mbstowcs(wpath, path, pathLength);
- wpath[pathLength] = 0;
- int ret = RemoveFile(wpath);
- delete[] wpath;
- return ret;
- }
- int CompoundFile::FileSize(const char* path, size_t& size)
- {
- size_t pathLength = strlen(path);
- wchar_t* wpath = new wchar_t[pathLength+1];
- mbstowcs(wpath, path, pathLength);
- wpath[pathLength] = 0;
- int ret = FileSize(wpath, size);
- delete[] wpath;
- return ret;
- }
- int CompoundFile::ReadFile(const char* path, char* data)
- {
- size_t pathLength = strlen(path);
- wchar_t* wpath = new wchar_t[pathLength+1];
- mbstowcs(wpath, path, pathLength);
- wpath[pathLength] = 0;
- int ret = ReadFile(wpath, data);
- delete[] wpath;
- return ret;
- }
- int CompoundFile::ReadFile(const char* path, vector<char>& data)
- {
- size_t pathLength = strlen(path);
- wchar_t* wpath = new wchar_t[pathLength+1];
- mbstowcs(wpath, path, pathLength);
- wpath[pathLength] = 0;
- int ret = ReadFile(wpath, data);
- delete[] wpath;
- return ret;
- }
- int CompoundFile::WriteFile(const char* path, char* data, size_t size)
- {
- size_t pathLength = strlen(path);
- wchar_t* wpath = new wchar_t[pathLength+1];
- mbstowcs(wpath, path, pathLength);
- wpath[pathLength] = 0;
- int ret = WriteFile(wpath, data, size);
- delete[] wpath;
- return ret;
- }
- int CompoundFile::WriteFile(const char* path, vector<char>& data, size_t size)
- {
- size_t pathLength = strlen(path);
- wchar_t* wpath = new wchar_t[pathLength+1];
- mbstowcs(wpath, path, pathLength);
- wpath[pathLength] = 0;
- int ret = WriteFile(wpath, data, size);
- delete[] wpath;
- return ret;
- }
- void CompoundFile::IncreaseLocationReferences(vector<size_t> indices)
- {
- size_t maxIndices = indices.size();
-
- {for (size_t i=0; i<109 && header_.BATArray_[i]!=-1; ++i)
- {
- size_t count = 0;
- for (size_t j=0; j<maxIndices; ++j)
- {
- if (header_.BATArray_[i] >= indices[j] &&
- header_.BATArray_[i] != -1) ++count;
- }
- header_.BATArray_[i] += count;
- }}
-
- if (header_.XBATCount_)
- {
- size_t count = 0;
- for (size_t j=0; j<maxIndices; ++j)
- {
- if (header_.XBATStart_ >= indices[j] &&
- header_.XBATStart_ != -2) ++count;
- }
- header_.XBATStart_ += count;
- }
-
- if (header_.SBATCount_)
- {
- size_t count = 0;
- for (size_t j=0; j<maxIndices; ++j)
- {
- if (header_.SBATStart_ >= indices[j] &&
- header_.SBATStart_ != -2) ++count;
- }
- header_.SBATStart_ += count;
- }
-
- size_t maxBATindices = blocksIndices_.size();
- {for (size_t i=0; i<maxBATindices && blocksIndices_[i]!=-1; ++i)
- {
- size_t count = 0;
- for (size_t j=0; j<maxIndices; ++j)
- {
- if (blocksIndices_[i] > indices[j] &&
- blocksIndices_[i] != -2 &&
- blocksIndices_[i] != -3) ++count;
- }
- blocksIndices_[i] += count;
- }}
-
- size_t count = 0;
- {for (size_t i=0; i<maxIndices; ++i)
- {
- if (header_.propertiesStart_ >= indices[i] &&
- header_.propertiesStart_ != -2) ++count;
- }}
- header_.propertiesStart_ += count;
-
- size_t maxProperties = properties_.size();
- if (!properties_.empty())
- {
- size_t count = 0;
- for (size_t j=0; j<maxIndices; ++j)
- {
- if (properties_[0]->startBlock_ >= indices[j] &&
- properties_[0]->startBlock_ != -2) ++count;
- }
- properties_[0]->startBlock_ += count;
- }
- {for (size_t i=1; i<maxProperties; ++i)
- {
- if (properties_[i]->size_ >= 4096)
- {
- size_t count = 0;
- for (size_t j=0; j<maxIndices; ++j)
- {
- if (properties_[i]->startBlock_ >= indices[j] &&
- properties_[i]->startBlock_ != -2) ++count;
- }
- properties_[i]->startBlock_ += count;
- }
- }}
- }
- void CompoundFile::DecreaseLocationReferences(vector<size_t> indices)
- {
- size_t maxIndices = indices.size();
-
- {for (size_t i=0; i<109 && header_.BATArray_[i]!=-1; ++i)
- {
- size_t count = 0;
- for (size_t j=0; j<maxIndices; ++j)
- {
- if (header_.BATArray_[i] > indices[j] &&
- header_.BATArray_[i] != -1) ++count;
- }
- header_.BATArray_[i] -= count;
- }}
-
- if (header_.XBATCount_)
- {
- size_t count = 0;
- for (size_t j=0; j<maxIndices; ++j)
- {
- if (header_.XBATStart_ > indices[j] &&
- header_.XBATStart_ != -2) ++count;
- }
- header_.XBATStart_ -= count;
- }
-
- if (header_.SBATCount_)
- {
- size_t count = 0;
- for (size_t j=0; j<maxIndices; ++j)
- {
- if (header_.SBATStart_ > indices[j] &&
- header_.SBATStart_ != -2) ++count;
- }
- header_.SBATStart_ -= count;
- }
-
-
-
-
- size_t maxBATindices = blocksIndices_.size();
- {for (size_t i=0; i<maxBATindices && blocksIndices_[i]!=-1; ++i)
- {
- bool end;
- do
- {
- end = true;
- for (size_t j=0; j<maxIndices; ++j)
- {
- if (blocksIndices_[i] == indices[j])
- {
- blocksIndices_[i] = blocksIndices_[indices[j]];
- end = false;
- break;
- }
- }
- } while (!end);
- }}
-
- sort (indices.begin(), indices.end(), greater<size_t>());
- {for (size_t i=0; i<maxIndices; ++i)
- {
- blocksIndices_.erase(blocksIndices_.begin()+indices[i]);
- blocksIndices_.push_back(-1);
- }}
-
- {for (size_t i=0; i<maxBATindices && blocksIndices_[i]!=-1; ++i)
- {
- size_t count = 0;
- for (size_t j=0; j<maxIndices; ++j)
- {
- if (blocksIndices_[i] > indices[j] &&
- blocksIndices_[i] != -2 &&
- blocksIndices_[i] != -3) ++count;
- }
- blocksIndices_[i] -= count;
- }}
-
- size_t count = 0;
- {for (size_t i=0; i<maxIndices; ++i)
- {
- if (header_.propertiesStart_ > indices[i] &&
- header_.propertiesStart_ != -2) ++count;
- }}
- header_.propertiesStart_ -= count;
- size_t maxProperties = properties_.size();
-
- if (!properties_.empty())
- {
- size_t count = 0;
- for (size_t j=0; j<maxIndices; ++j)
- {
- if (properties_[0]->startBlock_ > indices[j] &&
- properties_[0]->startBlock_ != -2) ++count;
- }
- properties_[0]->startBlock_ -= count;
- }
- {for (size_t i=1; i<maxProperties; ++i)
- {
- if (properties_[i]->size_ >= 4096)
- {
-
- size_t count = 0;
- for (size_t j=0; j<maxIndices; ++j)
- {
- if (properties_[i]->startBlock_ > indices[j] &&
- properties_[i]->startBlock_ != -2) ++count;
- }
- properties_[i]->startBlock_ -= count;
- }
- }}
- }
- void CompoundFile::SplitPath(const wchar_t* path,
- wchar_t*& parentpath,
- wchar_t*& propertyname)
- {
- size_t pathLength = wcslen(path);
- int npos;
- for (npos=pathLength-1; npos>0; --npos)
- {
- if (path[npos] == L'\\') break;
- }
- if (npos != 0)
- {
-
- parentpath = new wchar_t[npos+1];
- copy (path, path+npos, parentpath);
- parentpath[npos] = 0;
- ++npos;
- }
-
- if (npos==0 && pathLength > 0 && path[0] == L'\\') ++npos;
- propertyname = new wchar_t[pathLength-npos+1];
- copy (path+npos, path+pathLength, propertyname);
- propertyname[pathLength-npos] = 0;
- }
- bool CompoundFile::LoadHeader()
- {
- file_.Read(0, &*(block_.begin()));
- header_.Read(&*(block_.begin()));
-
- if (header_.fileType_ != 0xE11AB1A1E011CFD0) return false;
- block_.resize(header_.bigBlockSize_);
- file_.SetBlockSize(header_.bigBlockSize_);
- return true;
- }
- void CompoundFile::SaveHeader()
- {
- header_.Write(&*(block_.begin()));
- file_.Write(0, &*(block_.begin()));
- }
- void CompoundFile::LoadBAT()
- {
-
- {for (size_t i=0; i<header_.BATCount_; ++i)
- {
-
- blocksIndices_.resize(blocksIndices_.size()+128, -1);
- file_.Read(header_.BATArray_[i]+1, &*(block_.begin()));
- for (size_t j=0; j<128; ++j)
- {
- LittleEndian::Read(&*(block_.begin()), blocksIndices_[j+i*128], j*4, 4);
- }
- }}
-
- {for (size_t i=0; i<header_.XBATCount_; ++i)
- {
- blocksIndices_.resize(blocksIndices_.size()+128, -1);
- file_.Read(header_.XBATStart_+i+1, &*(block_.begin()));
- for (size_t j=0; j<128; ++j)
- {
- LittleEndian::Read(&*(block_.begin()), blocksIndices_[j+((i+109)*128)], j*4, 4);
- }
- }}
-
- {for (size_t i=0; i<header_.SBATCount_; ++i)
- {
- sblocksIndices_.resize(sblocksIndices_.size()+128, -1);
- file_.Read(header_.SBATStart_+i+1, &*(block_.begin()));
- for (size_t j=0; j<128; ++j)
- {
- LittleEndian::Read(&*(block_.begin()), sblocksIndices_[j+i*128], j*4, 4);
- }
- }}
- }
- void CompoundFile::SaveBAT()
- {
-
- {for (size_t i=0; i<header_.BATCount_; ++i)
- {
- for (size_t j=0; j<128; ++j)
- {
- LittleEndian::Write(&*(block_.begin()), blocksIndices_[j+i*128], j*4, 4);
- }
- file_.Write(header_.BATArray_[i]+1, &*(block_.begin()));
- }}
-
- {for (size_t i=0; i<header_.XBATCount_; ++i)
- {
- for (size_t j=0; j<128; ++j)
- {
- LittleEndian::Write(&*(block_.begin()), blocksIndices_[j+((i+109)*128)], j*4, 4);
- }
- file_.Write(header_.XBATStart_+i+1, &*(block_.begin()));
- }}
-
- {for (size_t i=0; i<header_.SBATCount_; ++i)
- {
- for (size_t j=0; j<128; ++j)
- {
- LittleEndian::Write(&*(block_.begin()), sblocksIndices_[j+i*128], j*4, 4);
- }
- file_.Write(header_.SBATStart_+i+1, &*(block_.begin()));
- }}
- }
- size_t CompoundFile::DataSize(size_t startIndex, bool isBig)
- {
- vector<size_t> indices;
- if (isBig)
- {
- GetBlockIndices(startIndex, indices, true);
- return indices.size()*header_.bigBlockSize_;
- }
- else
- {
- GetBlockIndices(startIndex, indices, false);
- return indices.size()*header_.smallBlockSize_;
- }
- }
- size_t CompoundFile::ReadData(size_t startIndex, char* data, bool isBig)
- {
- vector<size_t> indices;
- if (isBig)
- {
- GetBlockIndices(startIndex, indices, true);
- size_t maxIndices = indices.size();
- for (size_t i=0; i<maxIndices; ++i)
- {
- file_.Read(indices[i]+1, data+i*header_.bigBlockSize_);
- }
- return maxIndices*header_.bigBlockSize_;
- }
- else
- {
- GetBlockIndices(startIndex, indices, false);
- size_t minIndex = *min_element(indices.begin(), indices.end());
- size_t maxIndex = *max_element(indices.begin(), indices.end());
- size_t smallBlocksPerBigBlock = header_.bigBlockSize_ / header_.smallBlockSize_;
- size_t minBlock = minIndex / smallBlocksPerBigBlock;
- size_t maxBlock = maxIndex / smallBlocksPerBigBlock +
- (maxIndex % smallBlocksPerBigBlock ? 1 : 0);
- size_t totalBlocks = maxBlock - minBlock;
- char* buffer = new char[DataSize(properties_[0]->startBlock_, true)];
- ReadData(properties_[0]->startBlock_, buffer, true);
- size_t maxIndices = indices.size();
- for (size_t i=0; i<maxIndices; ++i)
- {
- size_t start = (indices[i] - minBlock*smallBlocksPerBigBlock)*header_.smallBlockSize_;
- copy (buffer+start,
- buffer+start+header_.smallBlockSize_,
- data+i*header_.smallBlockSize_);
- }
- delete[] buffer;
- return maxIndices*header_.smallBlockSize_;
- }
- }
- size_t CompoundFile::WriteData(const char* data, size_t size, int startIndex, bool isBig)
- {
- if (isBig)
- {
- if (size==0 && startIndex==-2) return startIndex;
-
- vector<size_t> indices;
- GetBlockIndices(startIndex, indices, true);
- size_t maxPresentBlocks = indices.size();
-
- size_t extraSize = size % header_.bigBlockSize_;
- size_t maxNewBlocks = size / header_.bigBlockSize_ + (extraSize ? 1 : 0);
-
- int extraBlocks = maxPresentBlocks - maxNewBlocks;
- if (extraBlocks > 0)
- {
-
- if (maxNewBlocks != 0) blocksIndices_[indices[maxNewBlocks]-1] = -2;
- else startIndex = -2;
-
- vector<size_t> indicesToRemove(extraBlocks);
- copy (indices.begin()+maxNewBlocks, indices.end(), indicesToRemove.begin());
- indices.erase(indices.begin()+maxNewBlocks, indices.end());
-
- FreeBlocks(indicesToRemove, true);
- }
-
- size_t remainingFullBlocks = size / header_.bigBlockSize_;
- size_t curIndex=0;
- if (maxPresentBlocks != 0)
- {
- for (; remainingFullBlocks && curIndex<maxPresentBlocks;
- --remainingFullBlocks, ++curIndex)
- {
- file_.Write(indices[curIndex]+1, data+curIndex*header_.bigBlockSize_);
- }
- }
-
-
- size_t index;
- if (indices.empty()) index = 0;
- else if (curIndex == 0) index = indices[0];
- else index = (startIndex != -2) ? indices[curIndex-1] : 0;
- if (remainingFullBlocks != 0)
- {
-
- do
- {
- size_t newIndex = GetFreeBlockIndex(true);
- if (startIndex == -2) startIndex = newIndex;
- else LinkBlocks(index, newIndex, true);
- file_.Write(newIndex+1, data+curIndex*header_.bigBlockSize_);
- ++curIndex;
- index = newIndex;
- } while (--remainingFullBlocks);
- }
-
- if (extraSize != 0)
- {
- size_t newIndex;
- if (curIndex >= maxPresentBlocks)
- {
-
- newIndex = GetFreeBlockIndex(true);
- if (startIndex == -2) startIndex = newIndex;
- else LinkBlocks(index, newIndex,true);
- }
- else newIndex = indices[curIndex];
-
- vector<char> tempdata(header_.bigBlockSize_, 0);
- copy (data+curIndex*header_.bigBlockSize_, data+curIndex*header_.bigBlockSize_+extraSize, tempdata.begin());
- file_.Write(newIndex+1, &*(tempdata.begin()));
- }
- return startIndex;
- }
- else
- {
- if (size==0 && startIndex==-2) return startIndex;
- if (size != 0 && properties_[0]->startBlock_ == -2)
- {
- size_t newIndex = GetFreeBlockIndex(true);
- fill (block_.begin(), block_.end(), 0);
- file_.Insert(newIndex, &*(block_.begin()));
- IncreaseLocationReferences(vector<size_t>(1, newIndex));
- properties_[0]->startBlock_ = newIndex;
- properties_[0]->size_ = header_.bigBlockSize_;
- }
-
- vector<size_t> indices;
- GetBlockIndices(startIndex, indices, false);
- size_t maxPresentBlocks = indices.size();
-
- size_t extraSize = size % header_.smallBlockSize_;
- size_t maxNewBlocks = size / header_.smallBlockSize_ + (extraSize ? 1 : 0);
- vector<char> smallBlocksData;
- int extraBlocks = maxPresentBlocks - maxNewBlocks;
- if (extraBlocks > 0)
- {
-
-
- if (maxNewBlocks != 0) sblocksIndices_[indices[maxNewBlocks]-1] = -2;
- else startIndex = -2;
-
- vector<size_t> indicesToRemove(extraBlocks);
- copy (indices.begin()+maxNewBlocks, indices.end(), indicesToRemove.begin());
- indices.erase(indices.begin()+maxNewBlocks, indices.end());
-
- FreeBlocks(indicesToRemove, false);
- }
- else if (extraBlocks < 0)
- {
- size_t maxBlocks = properties_[0]->size_ / header_.bigBlockSize_ +
- (properties_[0]->size_ % header_.bigBlockSize_ ? 1 : 0);
- size_t actualSize = maxBlocks * header_.bigBlockSize_;
- smallBlocksData.resize(actualSize);
- ReadData(properties_[0]->startBlock_, &*(smallBlocksData.begin()), true);
- smallBlocksData.resize(properties_[0]->size_);
-
- size_t newBlocksNeeded = -extraBlocks;
- size_t index = maxPresentBlocks - 1;
- for (size_t i=0; i<newBlocksNeeded; ++i)
- {
- size_t newIndex = GetFreeBlockIndex(false);
- if (startIndex == -2) startIndex = newIndex;
- else LinkBlocks(index, newIndex, false);
- smallBlocksData.insert(smallBlocksData.begin()+newIndex,
- header_.smallBlockSize_, 0);
- index = newIndex;
- }
- properties_[0]->size_ = newBlocksNeeded * header_.smallBlockSize_;
- }
- if (smallBlocksData.empty())
- {
- size_t maxBlocks = properties_[0]->size_ / header_.bigBlockSize_ +
- (properties_[0]->size_ % header_.bigBlockSize_ ? 1 : 0);
- size_t actualSize = maxBlocks * header_.bigBlockSize_;
- smallBlocksData.resize(actualSize);
- ReadData(properties_[0]->startBlock_, &*(smallBlocksData.begin()), true);
- smallBlocksData.resize(properties_[0]->size_);
- }
-
- GetBlockIndices(startIndex, indices, false);
- size_t fullBlocks = size / header_.smallBlockSize_;
- for (size_t i=0; i<fullBlocks; ++i)
- {
- copy (data+i*header_.smallBlockSize_,
- data+i*header_.smallBlockSize_+header_.smallBlockSize_,
- smallBlocksData.begin()+indices[i]*header_.smallBlockSize_);
- }
- if (extraSize != 0)
- {
- copy (data+fullBlocks*header_.smallBlockSize_,
- data+fullBlocks*header_.smallBlockSize_+extraSize,
- smallBlocksData.begin()+indices[fullBlocks]*header_.smallBlockSize_);
- }
- WriteData(&*(smallBlocksData.begin()), properties_[0]->size_,
- properties_[0]->startBlock_, true);
- return startIndex;
- }
- }
- void CompoundFile::GetBlockIndices(size_t startIndex, vector<size_t>& indices, bool isBig)
- {
- indices.clear();
- if (isBig)
- {
- for (size_t i=startIndex; i!=-2; i=blocksIndices_[i]) indices.push_back(i);
- }
- else
- {
- for (size_t i=startIndex; i!=-2; i=sblocksIndices_[i]) indices.push_back(i);
- }
- }
- size_t CompoundFile::GetFreeBlockIndex(bool isBig)
- {
- size_t index;
- if (isBig)
- {
-
- index = distance(blocksIndices_.begin(),
- find(blocksIndices_.begin(),
- blocksIndices_.end(), -1));
- if (index == blocksIndices_.size())
- {
- ExpandBATArray(true);
- index = distance(blocksIndices_.begin(),
- find(blocksIndices_.begin(),
- blocksIndices_.end(), -1));
- }
- blocksIndices_[index] = -2;
- }
- else
- {
-
- index = distance(sblocksIndices_.begin(),
- find(sblocksIndices_.begin(),
- sblocksIndices_.end(), -1));
- if (index == sblocksIndices_.size())
- {
- ExpandBATArray(false);
- index = distance(sblocksIndices_.begin(),
- find(sblocksIndices_.begin(),
- sblocksIndices_.end(), -1));
- }
- sblocksIndices_[index] = -2;
- }
- return index;
- }
- void CompoundFile::ExpandBATArray(bool isBig)
- {
- size_t newIndex;
- fill (block_.begin(), block_.end(), -1);
- if (isBig)
- {
- size_t BATindex = distance(&header_.BATArray_[0],
- find(header_.BATArray_,
- header_.BATArray_+109, -1));
- if (BATindex < 109)
- {
-
- newIndex = blocksIndices_.size();
- file_.Insert(newIndex+1, &*(block_.begin()));
- IncreaseLocationReferences(vector<size_t>(1, newIndex));
-
- header_.BATArray_[BATindex] = newIndex;
- ++header_.BATCount_;
- }
- else
- {
-
-
- if (header_.XBATCount_ != 0)
- {
- newIndex = header_.XBATStart_ + header_.XBATCount_;
- file_.Insert(newIndex, &*(block_.begin()));
- IncreaseLocationReferences(vector<size_t>(1, newIndex));
- }
- else
- {
- newIndex = blocksIndices_.size();
- file_.Insert(newIndex, &*(block_.begin()));
- IncreaseLocationReferences(vector<size_t>(1, newIndex));
- header_.XBATStart_ = newIndex;
- }
- ++header_.XBATCount_;
- }
- blocksIndices_.insert(blocksIndices_.begin()+newIndex, -3);
- blocksIndices_.resize(blocksIndices_.size()+127, -1);
- }
- else
- {
-
- if (header_.SBATCount_ != 0)
- {
- newIndex = header_.SBATStart_ + header_.SBATCount_;
- file_.Insert(newIndex, &*(block_.begin()));
- IncreaseLocationReferences(vector<size_t>(1, newIndex));
- }
- else
- {
- newIndex = GetFreeBlockIndex(true);
- file_.Insert(newIndex, &*(block_.begin()));
- IncreaseLocationReferences(vector<size_t>(1, newIndex));
- header_.SBATStart_ = newIndex;
- }
- ++header_.SBATCount_;
- sblocksIndices_.resize(sblocksIndices_.size()+128, -1);
- }
- }
- void CompoundFile::LinkBlocks(size_t from, size_t to, bool isBig)
- {
- if (isBig) blocksIndices_[from] = to;
- else sblocksIndices_[from] = to;
- }
- void CompoundFile::FreeBlocks(vector<size_t>& indices, bool isBig)
- {
- if (isBig)
- {
-
- DecreaseLocationReferences(indices);
- size_t maxIndices = indices.size();
- {for (size_t i=0; i<maxIndices; ++i) ++indices[i];}
- file_.Erase(indices);
-
- vector<size_t> indicesToRemove;
- while (distance(find(blocksIndices_.begin(),
- blocksIndices_.end(),-1),
- blocksIndices_.end()) >= 128)
- {
- blocksIndices_.resize(blocksIndices_.size()-128);
- if (header_.XBATCount_ != 0)
- {
-
- --header_.XBATCount_;
- indicesToRemove.push_back(header_.XBATStart_+header_.XBATCount_+1);
- if (header_.XBATCount_ == 0) header_.XBATStart_ = -2;
- }
- else
- {
-
- size_t BATindex = distance(&header_.BATArray_[0],
- find(header_.BATArray_,
- header_.BATArray_+109, -1));
- if (BATindex != 109)
- {
- --header_.BATCount_;
- indicesToRemove.push_back(header_.BATArray_[BATindex-1]+1);
- header_.BATArray_[BATindex-1] = -1;
- }
- }
- }
-
- if (!indicesToRemove.empty()) file_.Erase(indicesToRemove);
- }
- else
- {
-
- size_t maxIndices = indices.size();
- size_t maxBlocks = properties_[0]->size_ / header_.bigBlockSize_ +
- (properties_[0]->size_ % header_.bigBlockSize_ ? 1 : 0);
- size_t size = maxBlocks * header_.bigBlockSize_;
- char* data = new char[size];
- ReadData(properties_[0]->startBlock_, data, true);
- size_t maxSmallBlocks = properties_[0]->size_ / header_.smallBlockSize_;
- char* newdata = new char[properties_[0]->size_-maxIndices*header_.smallBlockSize_];
- {for (size_t i=0, j=0; i<maxSmallBlocks; ++i)
- {
- if (find(indices.begin(), indices.end(), i) == indices.end())
- {
- copy (data+i*header_.smallBlockSize_,
- data+i*header_.smallBlockSize_+header_.smallBlockSize_,
- newdata+j*header_.smallBlockSize_);
- ++j;
- }
- }}
- properties_[0]->startBlock_ = WriteData(newdata, properties_[0]->size_-maxIndices*header_.smallBlockSize_,
- properties_[0]->startBlock_, true);
- properties_[0]->size_ -= maxIndices*header_.smallBlockSize_;
- delete[] data;
- delete[] newdata;
-
- size_t maxSBATindices = sblocksIndices_.size();
- {for (size_t i=0; i<maxIndices; ++i)
- {
- for (size_t j=0; j<maxSBATindices; ++j)
- {
- if (j == indices[i]) continue;
- if (sblocksIndices_[j] == indices[i]) sblocksIndices_[j] = sblocksIndices_[indices[i]];
- if (sblocksIndices_[j] > indices[i] &&
- sblocksIndices_[j] != -1 &&
- sblocksIndices_[j] != -2) --sblocksIndices_[j];
- }
- }}
- sort (indices.begin(), indices.end(), greater<size_t>());
- {for (size_t i=0; i<maxIndices; ++i)
- {
- sblocksIndices_.erase(sblocksIndices_.begin()+indices[i]);
- sblocksIndices_.push_back(-1);
- }}
- vector<size_t> indicesToRemove;
- while (distance(find(sblocksIndices_.begin(),
- sblocksIndices_.end(),-1),
- sblocksIndices_.end()) >= 128)
- {
-
- sblocksIndices_.resize(sblocksIndices_.size()-128);
- --header_.SBATCount_;
- indicesToRemove.push_back(header_.SBATStart_+header_.SBATCount_);
- if (header_.SBATCount_ == 0) header_.SBATStart_ = -2;
- }
- FreeBlocks(indicesToRemove, true);
- }
- }
- void CompoundFile::LoadProperties()
- {
-
- size_t propertiesSize = DataSize(header_.propertiesStart_, true);
- char* buffer = new char[propertiesSize];
- ReadData(header_.propertiesStart_, buffer, true);
-
- size_t maxPropertiesBlock = propertiesSize / header_.bigBlockSize_;
- size_t propertiesPerBlock = header_.bigBlockSize_ / 128;
- size_t maxProperties = maxPropertiesBlock * propertiesPerBlock;
- size_t maxBlocks = maxProperties / propertiesPerBlock +
- (maxProperties % propertiesPerBlock ? 1 : 0);
- for (size_t i=0; i<maxBlocks; ++i)
- {
- for (size_t j=0; j<4; ++j)
- {
-
- Property* property = new Property;
- property->Read(buffer+i*512+j*128);
- if (wcslen(property->name_) == 0)
- {
- delete property;
- break;
- }
- properties_.push_back(property);
- }
- }
- delete[] buffer;
-
- propertyTrees_->parent_ = 0;
- propertyTrees_->self_ = properties_[0];
- propertyTrees_->index_ = 0;
- InsertPropertyTree(propertyTrees_,
- properties_[properties_[0]->childProp_],
- properties_[0]->childProp_);
- }
- void CompoundFile::SaveProperties()
- {
-
- size_t maxProperties = properties_.size();
- size_t propertiesPerBlock = header_.bigBlockSize_ / 128;
- size_t maxBlocks = maxProperties / propertiesPerBlock +
- (maxProperties % propertiesPerBlock ? 1 : 0);
- size_t propertiesSize = maxBlocks*header_.bigBlockSize_;
- char* buffer = new char[propertiesSize];
- {for (size_t i=0; i<propertiesSize; ++i) buffer[i] = 0;}
- {for (size_t i=0; i<maxProperties; ++i)
- {
-
- properties_[i]->Write(buffer+i*128);
- }}
-
- WriteData(buffer, propertiesSize, header_.propertiesStart_, true);
- delete[] buffer;
- }
- int CompoundFile::MakeProperty(const wchar_t* path, CompoundFile::Property* property)
- {
- wchar_t* parentpath = 0;
- wchar_t* propertyname = 0;
-
-
-
- if (wcslen(path) != 0)
- {
- if (path[0] == L'\\') currentDirectory_ = propertyTrees_;
- }
- SplitPath(path, parentpath, propertyname);
- if (propertyname != 0)
- {
- if (parentpath != 0)
- {
- if (ChangeDirectory(parentpath) != SUCCESS)
- {
- int ret = MakeDirectory(parentpath);
- if (ret != SUCCESS)
- {
- delete[] parentpath;
- delete[] propertyname;
- return ret;
- }
- else ChangeDirectory(parentpath);
- }
- delete[] parentpath;
- }
-
- size_t propertynameLength = wcslen(propertyname);
- if (propertynameLength >= 32)
- {
- delete[] propertyname;
- return NAME_TOO_LONG;
- }
- wcscpy(property->name_, propertyname);
- delete[] propertyname;
- property->nameSize_ = propertynameLength*2+2;
- if (FindProperty(currentDirectory_, property->name_) == 0)
- {
-
- size_t maxProperties = properties_.size();
- size_t index;
- for (index=1; index<maxProperties; ++index)
- {
- if (*(properties_[index]) > *property) break;
- }
- if (index != maxProperties)
- {
-
- IncreasePropertyReferences(propertyTrees_, index);
- }
- properties_.insert(properties_.begin()+index, property);
- InsertPropertyTree(currentDirectory_, property, index);
- return SUCCESS;
- }
- else return DUPLICATE_PROPERTY;
- }
- else
- {
- if (parentpath != 0) delete[] parentpath;
- return INVALID_PATH;
- }
- }
- CompoundFile::PropertyTree* CompoundFile::FindProperty(size_t index)
- {
- if (previousDirectories_.empty()) previousDirectories_.push_back(propertyTrees_);
- PropertyTree* currentTree = previousDirectories_.back();
- if (currentTree->index_ != index)
- {
- size_t maxChildren = currentTree->children_.size();
- for (size_t i=0; i<maxChildren; ++i)
- {
- previousDirectories_.push_back(currentTree->children_[i]);
- PropertyTree* child = FindProperty(index);
- if (child != 0)
- {
- previousDirectories_.pop_back();
- return child;
- }
- }
- }
- else
- {
- previousDirectories_.pop_back();
- return currentTree;
- }
- previousDirectories_.pop_back();
- return 0;
- }
- CompoundFile::PropertyTree* CompoundFile::FindProperty(const wchar_t* path)
- {
- previousDirectories_.push_back(currentDirectory_);
-
- wchar_t* parentpath = 0;
- wchar_t* filename = 0;
- if (wcslen(path) != 0)
- {
- if (path[0] == L'\\') currentDirectory_ = propertyTrees_;
- }
- SplitPath(path, parentpath, filename);
- if (parentpath != 0)
- {
- int ret = ChangeDirectory(parentpath);
- delete[] parentpath;
- if (ret != SUCCESS)
- {
-
- if (filename != 0) delete[] filename;
- currentDirectory_ = previousDirectories_.back();
- previousDirectories_.pop_back();
- PropertyTree* property = 0;
- return property;
- }
- }
-
- PropertyTree* property = 0;
- if (filename != 0)
- {
- property = FindProperty(currentDirectory_, filename);
- delete[] filename;
- }
- currentDirectory_ = previousDirectories_.back();
- previousDirectories_.pop_back();
- return property;
- }
- CompoundFile::PropertyTree*
- CompoundFile::FindProperty(CompoundFile::PropertyTree* parentTree,
- wchar_t* name)
- {
- if (parentTree->self_->childProp_ != -1)
- {
- size_t maxChildren = parentTree->children_.size();
- for (size_t i=0; i<maxChildren; ++i)
- {
- if (wcscmp(parentTree->children_[i]->self_->name_, name) == 0)
- {
- return parentTree->children_[i];
- }
- }
- }
- return 0;
- }
- void CompoundFile::InsertPropertyTree(CompoundFile::PropertyTree* parentTree,
- CompoundFile::Property* property,
- size_t index)
- {
- PropertyTree* tree = new PropertyTree;
- tree->parent_ = parentTree;
- tree->self_ = property;
- tree->index_ = index;
- if (property->previousProp_ != -1)
- {
- InsertPropertyTree(parentTree,
- properties_[property->previousProp_],
- property->previousProp_);
- }
- if (property->nextProp_ != -1)
- {
- InsertPropertyTree(parentTree,
- properties_[property->nextProp_],
- property->nextProp_);
- }
- if (property->childProp_ != -1)
- {
- InsertPropertyTree(tree,
- properties_[property->childProp_],
- property->childProp_);
- }
-
- size_t maxChildren = parentTree->children_.size();
- size_t i;
- for (i=0; i<maxChildren; ++i)
- {
- if (index < parentTree->children_[i]->index_) break;
- }
- parentTree->children_.insert(parentTree->children_.begin()+i, tree);
-
- UpdateChildrenIndices(parentTree);
- }
- void CompoundFile::DeletePropertyTree(CompoundFile::PropertyTree* tree)
- {
-
- DecreasePropertyReferences(propertyTrees_, tree->index_);
-
- if (properties_[tree->index_]) delete properties_[tree->index_];
- properties_.erase(properties_.begin()+tree->index_);
-
- size_t maxChildren = tree->parent_->children_.size();
- size_t i;
- for (i=0; i<maxChildren; ++i)
- {
- if (tree->parent_->children_[i]->index_ == tree->index_) break;
- }
- tree->parent_->children_.erase(tree->parent_->children_.begin()+i);
-
- UpdateChildrenIndices(tree->parent_);
- }
- void CompoundFile::UpdateChildrenIndices(CompoundFile::PropertyTree* parentTree)
- {
-
- size_t maxChildren = parentTree->children_.size();
- if (maxChildren != 0)
- {
- vector<PropertyTree*>& children = parentTree->children_;
- size_t prevChild = 0;
- children[0]->self_->previousProp_ = -1;
- children[0]->self_->nextProp_ = -1;
- size_t curChild;
- for (curChild=1; curChild<=maxChildren/2; ++curChild)
- {
- children[curChild]->self_->previousProp_ = children[prevChild]->index_;
- children[curChild]->self_->nextProp_ = -1;
- prevChild = curChild;
- }
-
- --curChild;
- children[curChild]->parent_->self_->childProp_ = children[curChild]->index_;
-
- size_t nextChild = curChild + 1;
- if (nextChild < maxChildren)
- {
- children[curChild]->self_->nextProp_ = children[nextChild]->index_;
- for (++curChild, ++nextChild;
- nextChild<maxChildren;
- ++curChild, ++nextChild)
- {
- children[curChild]->self_->previousProp_ = -1;
- children[curChild]->self_->nextProp_ = children[nextChild]->index_;
-
- }
- children[curChild]->self_->previousProp_ = -1;
- children[curChild]->self_->nextProp_ = -1;
- }
- }
- else
- {
- parentTree->self_->childProp_ = -1;
- }
- }
- void CompoundFile::IncreasePropertyReferences(CompoundFile::PropertyTree* parentTree,
- size_t index)
- {
- if (parentTree->index_ >= index) ++parentTree->index_;
- if (parentTree->self_->previousProp_ != -1)
- {
- if (parentTree->self_->previousProp_ >= index)
- {
- ++parentTree->self_->previousProp_;
- }
- }
- if (parentTree->self_->nextProp_!= -1)
- {
- if (parentTree->self_->nextProp_ >= index)
- {
- ++parentTree->self_->nextProp_;
- }
- }
- if (parentTree->self_->childProp_ != -1)
- {
- if (parentTree->self_->childProp_ >= index)
- {
- ++parentTree->self_->childProp_;
- }
- }
- size_t maxChildren = parentTree->children_.size();
- for (size_t i=0; i<maxChildren; ++i)
- {
- IncreasePropertyReferences(parentTree->children_[i], index);
- }
- }
- void CompoundFile::DecreasePropertyReferences(CompoundFile::PropertyTree* parentTree, size_t index)
- {
- if (parentTree->index_ > index) --parentTree->index_;
- if (parentTree->self_->previousProp_ != -1)
- {
- if (parentTree->self_->previousProp_ > index)
- {
- --parentTree->self_->previousProp_;
- }
- }
- if (parentTree->self_->nextProp_!= -1)
- {
- if (parentTree->self_->nextProp_ > index)
- {
- --parentTree->self_->nextProp_;
- }
- }
- if (parentTree->self_->childProp_ != -1)
- {
- if (parentTree->self_->childProp_ > index)
- {
- --parentTree->self_->childProp_;
- }
- }
- size_t maxChildren = parentTree->children_.size();
- for (size_t i=0; i<maxChildren; ++i)
- {
- DecreasePropertyReferences(parentTree->children_[i], index);
- }
- }
- }
- namespace YExcel
- {
- using namespace YCompoundFiles;
- Record::Record() : dataSize_(0), recordSize_(4) {};
- Record::~Record() {};
- size_t Record::Read(const char* data)
- {
- LittleEndian::Read(data, code_, 0, 2);
- LittleEndian::Read(data, dataSize_, 2, 2);
- data_.assign(data+4, data+4+dataSize_);
- recordSize_ = 4 + dataSize_;
-
- continueIndices_.clear();
- short code;
- LittleEndian::Read(data, code, dataSize_+4, 2);
- while (code == CODE::CONTINUE)
- {
- continueIndices_.push_back(dataSize_);
- size_t size;
- LittleEndian::Read(data, size, recordSize_+2, 2);
- data_.insert(data_.end(), data+recordSize_+4, data+recordSize_+4+size);
- dataSize_ += size;
- recordSize_ += 4 + size;
- LittleEndian::Read(data, code, recordSize_, 2);
- };
- return recordSize_;
- }
- size_t Record::Write(char* data)
- {
- LittleEndian::Write(data, code_, 0, 2);
- size_t npos = 2;
- if (continueIndices_.empty())
- {
- size_t size = dataSize_;
- size_t i=0;
- while (size > 8224)
- {
- LittleEndian::Write(data, 8224, npos, 2);
- npos += 2;
- size -= 8224;
- copy (data_.begin()+i*8224, data_.begin()+(i+1)*8224, data+npos);
- npos += 8224;
- if (size != 0)
- {
- ++i;
- LittleEndian::Write(data, 0x3C, npos, 2);
- npos += 2;
- }
- }
- LittleEndian::Write(data, size, npos, 2);
- npos += 2;
- copy (data_.begin()+i*8224, data_.begin()+i*8224+size, data+npos);
- npos += size;
- }
- else
- {
- size_t maxContinue = continueIndices_.size();
- size_t size = continueIndices_[0];
- LittleEndian::Write(data, size, npos, 2);
- npos += 2;
- copy (data_.begin(), data_.begin()+size, data+npos);
- npos += size;
- size_t c=0;
- for (c=1; c<maxContinue; ++c)
- {
- LittleEndian::Write(data, 0x3C, npos, 2);
- npos += 2;
- size = continueIndices_[c] - continueIndices_[c-1];
- LittleEndian::Write(data, size, npos, 2);
- npos += 2;
- copy (data_.begin()+continueIndices_[c-1],
- data_.begin()+continueIndices_[c],
- data+npos);
- npos += size;
- }
- LittleEndian::Write(data, 0x3C, npos, 2);
- npos += 2;
- size = data_.size() - continueIndices_[c-1];
- LittleEndian::Write(data, size, npos, 2);
- npos += 2;
- copy (data_.begin()+continueIndices_[c-1],
- data_.end(),
- data+npos);
- npos += size;
- }
- return npos;
- }
- size_t Record::DataSize() {return dataSize_;}
- size_t Record::RecordSize() {return recordSize_;}
- BOF::BOF() : Record() {code_ = CODE::BOF; dataSize_ = 16; recordSize_ = 20;}
- size_t BOF::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, version_, 0, 2);
- LittleEndian::Read(data_, type_, 2, 2);
- LittleEndian::Read(data_, buildIdentifier_, 4, 2);
- LittleEndian::Read(data_, buildYear_, 6, 2);
- LittleEndian::Read(data_, fileHistoryFlags_, 8, 4);
- LittleEndian::Read(data_, lowestExcelVersion_, 12, 4);
- return RecordSize();
- }
- size_t BOF::Write(char* data)
- {
- data_.resize(dataSize_);
- LittleEndian::Write(data_, version_, 0, 2);
- LittleEndian::Write(data_, type_, 2, 2);
- LittleEndian::Write(data_, buildIdentifier_, 4, 2);
- LittleEndian::Write(data_, buildYear_, 6, 2);
- LittleEndian::Write(data_, fileHistoryFlags_, 8, 4);
- LittleEndian::Write(data_, lowestExcelVersion_, 12, 4);
- return Record::Write(data);
- }
- YEOF::YEOF() : Record() {code_ = CODE::YEOF; dataSize_ = 0; recordSize_ = 4;}
- SmallString::SmallString() : name_(0), wname_(0) {};
- SmallString::~SmallString() {Reset();}
- SmallString::SmallString(const SmallString& s) :
- name_(0), wname_(0), unicode_(s.unicode_)
- {
- if (s.name_)
- {
- size_t len = strlen(s.name_);
- name_ = new char[len+1];
- strcpy(name_, s.name_);
- }
- if (s.wname_)
- {
- size_t len = wcslen(s.wname_);
- wname_ = new wchar_t[len+1];
- wcscpy(wname_, s.wname_);
- }
- }
- SmallString& SmallString::operator=(const SmallString& s)
- {
- Reset();
- unicode_ = s.unicode_;
- if (s.name_)
- {
- size_t len = strlen(s.name_);
- name_ = new char[len+1];
- strcpy(name_, s.name_);
- }
- if (s.wname_)
- {
- size_t len = wcslen(s.wname_);
- wname_ = new wchar_t[len+1];
- wcscpy(wname_, s.wname_);
- }
- return *this;
- }
- const SmallString& SmallString::operator=(const char* str)
- {
- unicode_ = 0;
- Reset();
- size_t len = strlen(str);
- name_ = new char[len+1];
- strcpy(name_, str);
- return *this;
- }
- const SmallString& SmallString::operator=(const wchar_t* str)
- {
- unicode_ = 1;
- Reset();
- size_t len = wcslen(str);
- wname_ = new wchar_t[len+1];
- wcscpy(wname_, str);
- return *this;
- }
- void SmallString::Reset()
- {
- if (name_) {delete[] name_; name_ = 0;}
- if (wname_) {delete[] wname_; wname_ = 0;}
- }
- size_t SmallString::Read(const char* data)
- {
- Reset();
- char stringSize;
- LittleEndian::Read(data, stringSize, 0, 1);
- LittleEndian::Read(data, unicode_, 1, 1);
- size_t bytesRead = 2;
- if (unicode_ == 0)
- {
-
- name_ = new char[stringSize+1];
- LittleEndian::ReadString(data, name_, 2, stringSize);
- name_[stringSize] = 0;
- bytesRead += stringSize;
- }
- else
- {
-
- wname_ = new wchar_t[stringSize+1];
- LittleEndian::ReadString(data, wname_, 2, stringSize);
- wname_[stringSize] = 0;
- bytesRead += stringSize*2;
- }
- return bytesRead;
- }
- size_t SmallString::Write(char* data)
- {
- size_t stringSize = 0;
- size_t bytesWrite = 0;
- if (unicode_ == 0)
- {
-
- if (name_)
- {
- stringSize = strlen(name_);
- LittleEndian::Write(data, stringSize, 0, 1);
- LittleEndian::Write(data, unicode_, 1, 1);
- LittleEndian::WriteString(data, name_, 2, stringSize);
- bytesWrite = 2 + stringSize;
- }
- else
- {
- LittleEndian::Write(data, stringSize, 0, 1);
- LittleEndian::Write(data, unicode_, 1, 1);
- bytesWrite = 2;
- }
- }
- else
- {
-
- if (wname_)
- {
- stringSize = wcslen(wname_);
- LittleEndian::Write(data, stringSize, 0, 1);
- LittleEndian::Write(data, unicode_, 1, 1);
- LittleEndian::WriteString(data, wname_, 2, stringSize);
- bytesWrite = 2 + stringSize*2;
- }
- else
- {
- LittleEndian::Write(data, stringSize, 0, 1);
- LittleEndian::Write(data, unicode_, 1, 1);
- bytesWrite = 2;
- }
- }
- return bytesWrite;
- }
- size_t SmallString::DataSize() {return (unicode_ == 0) ? StringSize()+2 : StringSize()*2+2;}
- size_t SmallString::RecordSize() {return DataSize();}
- size_t SmallString::StringSize()
- {
- if (unicode_ == 0)
- {
- if (name_) return strlen(name_);
- }
- else
- {
- if (wname_) return wcslen(wname_);
- }
- return 0;
- }
- LargeString::LargeString() : unicode_(-1), richtext_(0), phonetic_(0) {};
- LargeString::~LargeString() {};
- LargeString::LargeString(const LargeString& s) :
- name_(s.name_), wname_(s.wname_),
- unicode_(s.unicode_), richtext_(s.richtext_), phonetic_(s.phonetic_) {};
- LargeString& LargeString::operator=(const LargeString& s)
- {
- unicode_ = s.unicode_;
- richtext_ = s.richtext_;
- phonetic_ = s.phonetic_;
- name_ = s.name_;
- wname_ = s.wname_;
- return *this;
- }
- const LargeString& LargeString::operator=(const char* str)
- {
- unicode_ = 0;
- richtext_ = 0;
- phonetic_ = 0;
- wname_.clear();
- size_t len = strlen(str);
- name_.resize(len+1);
- strcpy(&*(name_.begin()), str);
- return *this;
- }
- const LargeString& LargeString::operator=(const wchar_t* str)
- {
- unicode_ = 1;
- richtext_ = 0;
- phonetic_ = 0;
- name_.clear();
- size_t len = wcslen(str);
- wname_.resize(len+1);
- wcscpy(&*(wname_.begin()), str);
- return *this;
- }
- size_t LargeString::Read(const char* data)
- {
- size_t stringSize;
- LittleEndian::Read(data, stringSize, 0, 2);
- LittleEndian::Read(data, unicode_, 2, 1);
- size_t npos = 3;
- if (unicode_ & 8)
- {
- LittleEndian::Read(data, richtext_, npos, 2);
- npos += 2;
- }
- if (unicode_ & 4) LittleEndian::Read(data, phonetic_, npos, 4);
- name_.clear();
- wname_.clear();
- size_t bytesRead = 2;
- if (stringSize>0) bytesRead += ContinueRead(data+2, stringSize);
- else bytesRead = 3;
- return bytesRead;
- }
- size_t LargeString::ContinueRead(const char* data, size_t size)
- {
- if (size == 0) return 0;
- char unicode;
- LittleEndian::Read(data, unicode, 0, 1);
- if (unicode_ == -1) unicode_ = unicode;
- if (unicode_ & 1)
- {
-
- size_t npos = 1;
- if (richtext_) npos += 2;
- if (phonetic_) npos += 4;
- size_t strpos = wname_.size();
- wname_.resize(strpos+size, 0);
- if (unicode & 1)
- {
- LittleEndian::ReadString(data, &*(wname_.begin())+strpos, npos, size);
- npos += size * SIZEOFWCHAR_T;
- }
- else
- {
-
- vector<char> name(size);
- LittleEndian::ReadString(data, &*(name.begin()), npos, size);
- mbstowcs(&*(wname_.begin())+strpos, &*(name.begin()), size);
- npos += size;
- }
- if (richtext_) npos += 4*richtext_;
- if (phonetic_) npos += phonetic_;
- return npos;
- }
- else
- {
-
- size_t npos = 1;
- if (richtext_) npos += 2;
- if (phonetic_) npos += 4;
- size_t strpos = name_.size();
- name_.resize(strpos+size, 0);
- if (unicode & 1)
- {
-
- vector<wchar_t> name(size);
- LittleEndian::ReadString(data, &*(name.begin()), npos, size);
- wcstombs(&*(name_.begin())+strpos, &*(name.begin()), size);
- npos += size * SIZEOFWCHAR_T;
- }
- else
- {
- LittleEndian::ReadString(data, &*(name_.begin())+strpos, npos, size);
- npos += size;
- }
- if (richtext_) npos += 4*richtext_;
- if (phonetic_) npos += phonetic_;
- return npos;
- }
- }
- size_t LargeString::Write(char* data)
- {
- size_t stringSize = 0;
- size_t bytesWrite = 0;
- if (unicode_ & 1)
- {
-
- unicode_ = 1;
- if (!wname_.empty())
- {
- stringSize = wname_.size();
- LittleEndian::Write(data, stringSize, 0, 2);
- LittleEndian::Write(data, unicode_, 2, 1);
- LittleEndian::WriteString(data, &*(wname_.begin()), 3, stringSize);
- bytesWrite = 3 + stringSize * SIZEOFWCHAR_T;
- }
- else
- {
- LittleEndian::Write(data, stringSize, 0, 2);
- LittleEndian::Write(data, unicode_, 2, 1);
- bytesWrite = 3;
- }
- }
- else
- {
-
- unicode_ = 0;
- if (!name_.empty())
- {
- stringSize = name_.size();
- LittleEndian::Write(data, stringSize, 0, 2);
- LittleEndian::Write(data, unicode_, 2, 1);
- LittleEndian::WriteString(data, &*(name_.begin()), 3, stringSize);
- bytesWrite = 3 + stringSize;
- }
- else
- {
- LittleEndian::Write(data, stringSize, 0, 2);
- LittleEndian::Write(data, unicode_, 2, 1);
- bytesWrite = 3;
- }
- }
- return bytesWrite;
- }
- size_t LargeString::DataSize()
- {
- size_t dataSize = StringSize() + 3;
- if (richtext_) dataSize += 2 + 4*richtext_;
- if (phonetic_) dataSize += 4 + phonetic_;
- return dataSize;
- }
- size_t LargeString::RecordSize() {return DataSize();}
- size_t LargeString::StringSize()
- {
- if (unicode_ & 1) return wname_.size() * SIZEOFWCHAR_T;
- else return name_.size();
- }
- Workbook::Workbook()
- {
- bof_.version_ = 1536;
- bof_.type_ = 5;
- bof_.buildIdentifier_ = 6560;
- bof_.buildYear_ = 1997;
- bof_.fileHistoryFlags_ = 49353;
- bof_.lowestExcelVersion_ = 774;
- }
- size_t Workbook::Read(const char* data)
- {
- size_t bytesRead = 0;
- short code;
- LittleEndian::Read(data, code, 0, 2);
- while (code != CODE::YEOF)
- {
- switch (code)
- {
- case CODE::BOF:
- bytesRead += bof_.Read(data+bytesRead);
- break;
-
- case CODE::WINDOW1:
- bytesRead += window1_.Read(data+bytesRead);
- break;
-
- case CODE::FONT:
- fonts_.push_back(Font());
- bytesRead += fonts_.back().Read(data+bytesRead);
- break;
-
- case CODE::XF:
- XFs_.push_back(XF());
- bytesRead += XFs_.back().Read(data+bytesRead);
- break;
-
- case CODE::STYLE:
- styles_.push_back(Style());
- bytesRead += styles_.back().Read(data+bytesRead);
- break;
-
- case CODE::BOUNDSHEET:
- boundSheets_.push_back(BoundSheet());
- bytesRead += boundSheets_.back().Read(data+bytesRead);
- break;
-
- case CODE::SST:
- bytesRead += sst_.Read(data+bytesRead);
- break;
- default:
- Record rec;
- bytesRead += rec.Read(data+bytesRead);
- }
- LittleEndian::Read(data, code, bytesRead, 2);
- }
- bytesRead += eof_.RecordSize();
- return bytesRead;
- }
- size_t Workbook::Write(char* data)
- {
- size_t bytesWritten = 0;
- bytesWritten += bof_.Write(data+bytesWritten);
-
- bytesWritten += window1_.Write(data+bytesWritten);
-
- size_t maxFonts = fonts_.size();
- {for (size_t i=0; i<maxFonts; ++i) {bytesWritten += fonts_[i].Write(data+bytesWritten);}}
-
- size_t maxXFs = XFs_.size();
- {for (size_t i=0; i<maxXFs; ++i) {bytesWritten += XFs_[i].Write(data+bytesWritten);}}
- size_t maxStyles = styles_.size();
- {for (size_t i=0; i<maxStyles; ++i) {bytesWritten += styles_[i].Write(data+bytesWritten);}}
- size_t maxBoundSheets = boundSheets_.size();
- {for (size_t i=0; i<maxBoundSheets; ++i) {bytesWritten += boundSheets_[i].Write(data+bytesWritten);}}
- bytesWritten += sst_.Write(data+bytesWritten);
-
- bytesWritten += eof_.Write(data+bytesWritten);
- return bytesWritten;
- }
- size_t Workbook::DataSize()
- {
- size_t size = 0;
- size += bof_.RecordSize();
- size += window1_.RecordSize();
- size_t maxFonts = fonts_.size();
- {for (size_t i=0; i<maxFonts; ++i) {size += fonts_[i].RecordSize();}}
-
- size_t maxXFs = XFs_.size();
- {for (size_t i=0; i<maxXFs; ++i) {size += XFs_[i].RecordSize();}}
- size_t maxStyles = styles_.size();
- {for (size_t i=0; i<maxStyles; ++i) {size += styles_[i].RecordSize();}}
- size_t maxBoundSheets = boundSheets_.size();
- {for (size_t i=0; i<maxBoundSheets; ++i) {size += boundSheets_[i].RecordSize();}}
- size += sst_.RecordSize();
- size += eof_.RecordSize();
- return size;
- }
- size_t Workbook::RecordSize() {return DataSize();}
- Workbook::Window1::Window1() : Record(),
- horizontalPos_(0x78), verticalPos_(0x78), width_(0x3B1F), height_(0x2454),
- options_(0x38), activeWorksheetIndex_(0), firstVisibleTabIndex_(0), selectedWorksheetNo_(1),
- worksheetTabBarWidth_(0x258) {code_ = CODE::WINDOW1; dataSize_ = 18; recordSize_ = 22;}
- size_t Workbook::Window1::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, horizontalPos_, 0, 2);
- LittleEndian::Read(data_, verticalPos_, 2, 2);
- LittleEndian::Read(data_, width_, 4, 2);
- LittleEndian::Read(data_, height_, 6, 2);
- LittleEndian::Read(data_, options_, 8, 2);
- LittleEndian::Read(data_, activeWorksheetIndex_, 10, 2);
- LittleEndian::Read(data_, firstVisibleTabIndex_, 12, 2);
- LittleEndian::Read(data_, selectedWorksheetNo_, 14, 2);
- LittleEndian::Read(data_, worksheetTabBarWidth_, 16, 2);
- return RecordSize();
- }
- size_t Workbook::Window1::Write(char* data)
- {
- data_.resize(dataSize_);
- LittleEndian::Write(data_, horizontalPos_, 0, 2);
- LittleEndian::Write(data_, verticalPos_, 2, 2);
- LittleEndian::Write(data_, width_, 4, 2);
- LittleEndian::Write(data_, height_, 6, 2);
- LittleEndian::Write(data_, options_, 8, 2);
- LittleEndian::Write(data_, activeWorksheetIndex_, 10, 2);
- LittleEndian::Write(data_, firstVisibleTabIndex_, 12, 2);
- LittleEndian::Write(data_, selectedWorksheetNo_, 14, 2);
- LittleEndian::Write(data_, worksheetTabBarWidth_, 16, 2);
- return Record::Write(data);
- }
- Workbook::Font::Font() : Record(),
- height_(200), options_(0), colourIndex_(0x7FFF), weight_(400), escapementType_(0),
- underlineType_(0), family_(0), characterSet_(0), unused_(0)
- {
- code_ = CODE::FONT;
- dataSize_ = 14;
- recordSize_ = 18;
- name_ = L"Arial";
- name_.unicode_ = 1;
- }
- size_t Workbook::Font::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, height_, 0, 2);
- LittleEndian::Read(data_, options_, 2, 2);
- LittleEndian::Read(data_, colourIndex_, 4, 2);
- LittleEndian::Read(data_, weight_, 6, 2);
- LittleEndian::Read(data_, escapementType_, 8, 2);
- LittleEndian::Read(data_, underlineType_, 10, 1);
- LittleEndian::Read(data_, family_, 11, 1);
- LittleEndian::Read(data_, characterSet_, 12, 1);
- LittleEndian::Read(data_, unused_, 13, 1);
- name_.Read(&*(data_.begin())+14);
- return RecordSize();
- }
- size_t Workbook::Font::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, height_, 0, 2);
- LittleEndian::Write(data_, options_, 2, 2);
- LittleEndian::Write(data_, colourIndex_, 4, 2);
- LittleEndian::Write(data_, weight_, 6, 2);
- LittleEndian::Write(data_, escapementType_, 8, 2);
- LittleEndian::Write(data_, underlineType_, 10, 1);
- LittleEndian::Write(data_, family_, 11, 1);
- LittleEndian::Write(data_, characterSet_, 12, 1);
- LittleEndian::Write(data_, unused_, 13, 1);
- name_.Write(&*(data_.begin())+14);
- return Record::Write(data);
- }
- size_t Workbook::Font::DataSize() {return (dataSize_ = 14 + name_.RecordSize());}
- size_t Workbook::Font::RecordSize() {return (recordSize_ = DataSize()+4);}
- Workbook::XF::XF() : Record(),
- fontRecordIndex_(0), formatRecordIndex_(0), protectionType_(0xFFF5), alignment_(0x20), rotation_(0x00),
- textProperties_(0x00), usedAttributes_(0x00), borderLines_(0x0000), colour1_(0x0000), colour2_(0x20C0)
- {code_ = CODE::XF; dataSize_ = 20; recordSize_ = 24;}
- size_t Workbook::XF::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, fontRecordIndex_, 0, 2);
- LittleEndian::Read(data_, formatRecordIndex_, 2, 2);
- LittleEndian::Read(data_, protectionType_, 4, 2);
- LittleEndian::Read(data_, alignment_, 6, 1);
- LittleEndian::Read(data_, rotation_, 7, 1);
- LittleEndian::Read(data_, textProperties_, 8, 1);
- LittleEndian::Read(data_, usedAttributes_, 9, 1);
- LittleEndian::Read(data_, borderLines_, 10, 4);
- LittleEndian::Read(data_, colour1_, 14, 4);
- LittleEndian::Read(data_, colour2_, 18, 2);
- return RecordSize();
- }
- size_t Workbook::XF::Write(char* data)
- {
- data_.resize(dataSize_);
- LittleEndian::Write(data_, fontRecordIndex_, 0, 2);
- LittleEndian::Write(data_, formatRecordIndex_, 2, 2);
- LittleEndian::Write(data_, protectionType_, 4, 2);
- LittleEndian::Write(data_, alignment_, 6, 1);
- LittleEndian::Write(data_, rotation_, 7, 1);
- LittleEndian::Write(data_, textProperties_, 8, 1);
- LittleEndian::Write(data_, usedAttributes_, 9, 1);
- LittleEndian::Write(data_, borderLines_, 10, 4);
- LittleEndian::Write(data_, colour1_, 14, 4);
- LittleEndian::Write(data_, colour2_, 18, 2);
- return Record::Write(data);
- }
- Workbook::Style::Style() : Record(),
- XFRecordIndex_(0x8000), identifier_(0), level_(0xFF)
- {code_ = CODE::STYLE; dataSize_ = 2; recordSize_ = 6;}
- size_t Workbook::Style::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, XFRecordIndex_, 0, 2);
- if (XFRecordIndex_ & 0x8000)
- {
-
- LittleEndian::Read(data_, identifier_, 2, 1);
- LittleEndian::Read(data_, level_, 3, 1);
- }
- else
- {
-
- name_.Read(&*(data_.begin())+2);
- }
- return RecordSize();
- }
- size_t Workbook::Style::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, XFRecordIndex_, 0, 2);
- if (XFRecordIndex_ & 0x8000)
- {
-
- LittleEndian::Write(data_, identifier_, 2, 1);
- LittleEndian::Write(data_, level_, 3, 1);
- }
- else
- {
-
- name_.Write(&*(data_.begin())+2);
- }
- return Record::Write(data);
- }
- size_t Workbook::Style::DataSize() {return (dataSize_ = (XFRecordIndex_ & 0x8000) ? 4 : 2+name_.RecordSize());}
- size_t Workbook::Style::RecordSize() {return (recordSize_ = DataSize()+4);}
- Workbook::BoundSheet::BoundSheet() : Record(),
- BOFpos_(0x0000), visibility_(0), type_(0)
- {
- code_ = CODE::BOUNDSHEET;
- dataSize_ = 6;
- dataSize_ = 10;
- name_ = "Sheet1";
- name_.unicode_ = false;
- }
- size_t Workbook::BoundSheet::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, BOFpos_, 0, 4);
- LittleEndian::Read(data_, visibility_, 4, 1);
- LittleEndian::Read(data_, type_, 5, 1);
- name_.Read(&*(data_.begin())+6);
- return RecordSize();
- }
- size_t Workbook::BoundSheet::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, BOFpos_, 0, 4);
- LittleEndian::Write(data_, visibility_, 4, 1);
- LittleEndian::Write(data_, type_, 5, 1);
- name_.Write(&*(data_.begin())+6);
- return Record::Write(data);
- }
- size_t Workbook::BoundSheet::DataSize() {return (dataSize_ = 6+name_.RecordSize());}
- size_t Workbook::BoundSheet::RecordSize() {return (recordSize_ = DataSize()+4);}
- Workbook::SharedStringTable::SharedStringTable() : Record(),
- stringsTotal_(0), uniqueStringsTotal_(0) {code_ = CODE::SST; dataSize_ = 8; recordSize_ = 12;}
- size_t Workbook::SharedStringTable::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, stringsTotal_, 0, 4);
- LittleEndian::Read(data_, uniqueStringsTotal_, 4, 4);
- strings_.clear();
- strings_.resize(uniqueStringsTotal_);
-
- size_t npos = 8;
- if (continueIndices_.empty())
- {
- for (size_t i=0; i<uniqueStringsTotal_; ++i)
- {
- npos += strings_[i].Read(&*(data_.begin())+npos);
- }
- }
- else
- {
-
- size_t maxContinue = continueIndices_.size();
- for (size_t i=0, c=0; i<uniqueStringsTotal_; ++i)
- {
- char unicode;
- size_t stringSize;
- LittleEndian::Read(data_, stringSize, npos, 2);
- LittleEndian::Read(data_, unicode, npos+2, 1);
- size_t multiplier = unicode & 1 ? 2 : 1;
- if (c >= maxContinue || npos+stringSize*multiplier+3 <= continueIndices_[c])
- {
-
- npos += strings_[i].Read(&*(data_.begin())+npos);
- }
- else
- {
-
- int bytesRead = 2;
- int size = continueIndices_[c] - npos - 1 - bytesRead;
- ++c;
- if (size > 0)
- {
- size /= multiplier;
- bytesRead += strings_[i].ContinueRead(&*(data_.begin())+npos+bytesRead, size);
- stringSize -= size;
- size = 0;
- }
- while (c<maxContinue && npos+stringSize+1>continueIndices_[c])
- {
- size_t dataSize = (continueIndices_[c] - continueIndices_[c-1] - 1) / multiplier;
- bytesRead += strings_[i].ContinueRead(&*(data_.begin())+npos+bytesRead, dataSize);
- stringSize -= dataSize + 1;
- ++c;
- };
- if (stringSize>0)
- {
- bytesRead += strings_[i].ContinueRead(&*(data_.begin())+npos+bytesRead, stringSize);
- }
- npos += bytesRead;
- }
- }
- }
- return npos + 4*(npos/8224 + 1);
- }
- size_t Workbook::SharedStringTable::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, stringsTotal_, 0, 4);
- LittleEndian::Write(data_, uniqueStringsTotal_, 4, 4);
- size_t maxContinue = continueIndices_.size();
- for (size_t i=0, c=0, npos=8; i<uniqueStringsTotal_; ++i)
- {
- npos += strings_[i].Write(&*(data_.begin())+npos);
- if (c<maxContinue && npos==continueIndices_[c]) ++c;
- else if (c<maxContinue && npos > continueIndices_[c])
- {
-
- data_.insert(data_.begin()+continueIndices_[c], strings_[i].unicode_);
- data_.pop_back();
- ++c;
- ++npos;
- }
- }
- return Record::Write(data);
- }
- size_t Workbook::SharedStringTable::DataSize()
- {
- dataSize_ = 8;
- continueIndices_.clear();
- size_t curMax = 8224;
- for (size_t i=0; i<uniqueStringsTotal_; ++i)
- {
- size_t stringSize = strings_[i].StringSize();
- if (dataSize_+stringSize+3 <= curMax)
- {
- dataSize_ += stringSize + 3;
- }
- else
- {
-
-
- bool unicode = strings_[i].unicode_ & 1;
- if (curMax - dataSize_ >= 12)
- {
- if (unicode && !((curMax-dataSize_)%2)) --curMax;
- continueIndices_.push_back(curMax);
- stringSize -= (curMax - dataSize_ - 3);
- dataSize_ = curMax;
- curMax += 8224;
- size_t additionalContinueRecords = unicode ? stringSize/8222 : stringSize/8223;
- for (size_t j=0; j<additionalContinueRecords; ++j)
- {
- if (unicode)
- {
- --curMax;
- continueIndices_.push_back(curMax);
- curMax += 8223;
- dataSize_ += 8223;
- stringSize -= 8222;
- }
- else
- {
- continueIndices_.push_back(curMax);
- curMax += 8224;
- dataSize_ += 8224;
- stringSize -= 8223;
- }
- }
- dataSize_ += stringSize + 1;
- }
- else
- {
- continueIndices_.push_back(dataSize_);
- curMax = dataSize_ + 8224;
- if (dataSize_+stringSize+3 < curMax)
- {
- dataSize_ += stringSize + 3;
- }
- else
- {
-
-
- if (curMax - dataSize_ >= 12)
- {
- if (unicode && !((curMax-dataSize_)%2)) --curMax;
- continueIndices_.push_back(curMax);
- stringSize -= (curMax - dataSize_ - 3);
- dataSize_ = curMax;
- curMax += 8224;
- size_t additionalContinueRecords = unicode ? stringSize/8222 : stringSize/8223;
- for (size_t j=0; j<additionalContinueRecords; ++j)
- {
- if (unicode)
- {
- --curMax;
- continueIndices_.push_back(curMax);
- curMax += 8223;
- dataSize_ += 8223;
- stringSize -= 8222;
- }
- else
- {
- continueIndices_.push_back(curMax);
- curMax += 8224;
- dataSize_ += 8224;
- stringSize -= 8223;
- }
- }
- dataSize_ += stringSize + 1;
- }
- }
- }
- }
- }
- return dataSize_;
- }
- size_t Workbook::SharedStringTable::RecordSize()
- {
- size_t dataSize = DataSize();
- return (recordSize_ = dataSize + 4*(dataSize/8224 + 1));
- }
- Workbook::ExtSST::ExtSST() : Record(),
- stringsTotal_(0), streamPos_(0), firstStringPos_(0), unused_(0)
- {
- code_ = CODE::EXTSST;
- dataSize_ = 2;
- recordSize_ = 6;
- }
- size_t Workbook::ExtSST::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, stringsTotal_, 0, 2);
- size_t maxPortions = (dataSize_-2) / 8;
- streamPos_.clear();
- streamPos_.resize(maxPortions);
- firstStringPos_.clear();
- firstStringPos_.resize(maxPortions);
- unused_.clear();
- unused_.resize(maxPortions);
- for (size_t i=0, npos=2; i<maxPortions; ++i)
- {
- LittleEndian::Read(data_, streamPos_[i], npos, 4);
- LittleEndian::Read(data_, firstStringPos_[i], npos+4, 2);
- LittleEndian::Read(data_, unused_[i], npos+6, 2);
- npos += 8;
- }
- return RecordSize();
- }
- size_t Workbook::ExtSST::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, stringsTotal_, 0, 2);
- size_t maxPortions = streamPos_.size();
- for (size_t i=0, npos=2; i<maxPortions; ++i)
- {
- LittleEndian::Write(data_, streamPos_[i], npos, 4);
- LittleEndian::Write(data_, firstStringPos_[i], npos+4, 2);
- LittleEndian::Write(data_, unused_[i], npos+6, 2);
- npos += 8;
- }
- return Record::Write(data);
- }
- size_t Workbook::ExtSST::DataSize()
- {
- dataSize_ = 2 + streamPos_.size()*8;
- dataSize_ += (int)(dataSize_/8224)*4;
- return dataSize_;
- }
- size_t Workbook::ExtSST::RecordSize() {return (recordSize_ = DataSize()+(int)((2+streamPos_.size()*8)/8224)*4)+4;}
- Worksheet::Worksheet()
- {
- bof_.version_ = 1536;
- bof_.type_ = 16;
- bof_.buildIdentifier_ = 6560;
- bof_.buildYear_ = 1997;
- bof_.fileHistoryFlags_ = 49353;
- bof_.lowestExcelVersion_ = 774;
- }
- size_t Worksheet::Read(const char* data)
- {
- size_t bytesRead = 0;
- short code;
- LittleEndian::Read(data, code, 0, 2);
- while (code != CODE::YEOF)
- {
- switch (code)
- {
- case CODE::BOF:
- bytesRead += bof_.Read(data+bytesRead);
- break;
-
- case CODE::INDEX:
- bytesRead += index_.Read(data+bytesRead);
- break;
-
- case CODE::DIMENSIONS:
- bytesRead += dimensions_.Read(data+bytesRead);
- break;
-
- case CODE::ROW:
- bytesRead += cellTable_.Read(data+bytesRead);
- break;
-
- case CODE::WINDOW2:
- bytesRead += window2_.Read(data+bytesRead);
- break;
- default:
- Record rec;
- bytesRead += rec.Read(data+bytesRead);
- }
- LittleEndian::Read(data, code, bytesRead, 2);
- }
- bytesRead += eof_.RecordSize();
- return bytesRead;
- }
- size_t Worksheet::Write(char* data)
- {
- size_t bytesWritten = 0;
- bytesWritten += bof_.Write(data+bytesWritten);
- bytesWritten += index_.Write(data+bytesWritten);
-
- bytesWritten += dimensions_.Write(data+bytesWritten);
-
- bytesWritten += cellTable_.Write(data+bytesWritten);
- bytesWritten += window2_.Write(data+bytesWritten);
-
- bytesWritten += eof_.Write(data+bytesWritten);
- return bytesWritten;
- }
- size_t Worksheet::DataSize()
- {
- size_t dataSize = 0;
- dataSize += bof_.RecordSize();
- dataSize += index_.RecordSize();
- dataSize += dimensions_.RecordSize();
- dataSize += cellTable_.RecordSize();
- dataSize += window2_.RecordSize();
- dataSize += eof_.RecordSize();
- return dataSize;
- }
- size_t Worksheet::RecordSize() {return DataSize();}
- Worksheet::Index::Index() : Record(),
- unused1_(0), firstUsedRowIndex_(0), firstUnusedRowIndex_(0), unused2_(0)
- {code_ = CODE::INDEX; dataSize_ = 16; recordSize_ = 20; DBCellPos_.resize(1);}
- size_t Worksheet::Index::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, unused1_, 0, 4);
- LittleEndian::Read(data_, firstUsedRowIndex_, 4, 4);
- LittleEndian::Read(data_, firstUnusedRowIndex_, 8, 4);
- LittleEndian::Read(data_, unused2_, 12, 4);
- size_t nm = int(firstUnusedRowIndex_ - firstUsedRowIndex_ - 1) / 32 + 1;
- DBCellPos_.clear();
- DBCellPos_.resize(nm);
- if (dataSize_>16)
- {
- for (size_t i=0; i<nm; ++i)
- {
- LittleEndian::Read(data_, DBCellPos_[i], 16+i*4, 4);
- }
- }
- return RecordSize();
- }
- size_t Worksheet::Index::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, unused1_, 0, 4);
- LittleEndian::Write(data_, firstUsedRowIndex_, 4, 4);
- LittleEndian::Write(data_, firstUnusedRowIndex_, 8, 4);
- LittleEndian::Write(data_, unused2_, 12, 4);
- size_t nm = DBCellPos_.size();
- for (size_t i=0; i<nm; ++i)
- {
- LittleEndian::Write(data_, DBCellPos_[i], 16+i*4, 4);
- }
- return Record::Write(data);
- }
- size_t Worksheet::Index::DataSize() {return (dataSize_ = 16 + DBCellPos_.size()*4);}
- size_t Worksheet::Index::RecordSize()
- {
- size_t dataSize = DataSize();
- return (recordSize_ = dataSize + 4*(dataSize/8224 + 1));
- }
- Worksheet::Dimensions::Dimensions() : Record(),
- firstUsedRowIndex_(0), lastUsedRowIndexPlusOne_(0),
- firstUsedColIndex_(0), lastUsedColIndexPlusOne_(0),
- unused_(0) {code_ = CODE::DIMENSIONS; dataSize_ = 14; recordSize_ = 18;}
- size_t Worksheet::Dimensions::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, firstUsedRowIndex_, 0, 4);
- LittleEndian::Read(data_, lastUsedRowIndexPlusOne_, 4, 4);
- LittleEndian::Read(data_, firstUsedColIndex_, 8, 2);
- LittleEndian::Read(data_, lastUsedColIndexPlusOne_, 10, 2);
- LittleEndian::Read(data_, unused_, 12, 2);
- return RecordSize();
- }
- size_t Worksheet::Dimensions::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, firstUsedRowIndex_, 0, 4);
- LittleEndian::Write(data_, lastUsedRowIndexPlusOne_, 4, 4);
- LittleEndian::Write(data_, firstUsedColIndex_, 8, 2);
- LittleEndian::Write(data_, lastUsedColIndexPlusOne_, 10, 2);
- LittleEndian::Write(data_, unused_, 12, 2);
- return Record::Write(data);
- }
- Worksheet::CellTable::RowBlock::CellBlock::Blank::Blank() : Record(),
- rowIndex_(0), colIndex_(0), XFRecordIndex_(0) {code_ = CODE::BLANK; dataSize_ = 6; recordSize_ = 10;}
- size_t Worksheet::CellTable::RowBlock::CellBlock::Blank::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, rowIndex_, 0, 2);
- LittleEndian::Read(data_, colIndex_, 2, 2);
- LittleEndian::Read(data_, XFRecordIndex_, 4, 2);
- return RecordSize();
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::Blank::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, rowIndex_, 0, 2);
- LittleEndian::Write(data_, colIndex_, 2, 2);
- LittleEndian::Write(data_, XFRecordIndex_, 4, 2);
- return Record::Write(data);
- }
- Worksheet::CellTable::RowBlock::CellBlock::BoolErr::BoolErr() : Record(),
- rowIndex_(0), colIndex_(0), XFRecordIndex_(0), value_(0), error_(0)
- {code_ = CODE::BOOLERR; dataSize_ = 8; recordSize_ = 12;}
- size_t Worksheet::CellTable::RowBlock::CellBlock::BoolErr::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, rowIndex_, 0, 2);
- LittleEndian::Read(data_, colIndex_, 2, 2);
- LittleEndian::Read(data_, XFRecordIndex_, 4, 2);
- LittleEndian::Read(data_, value_, 6, 1);
- LittleEndian::Read(data_, error_, 7, 1);
- return RecordSize();
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::BoolErr::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, rowIndex_, 0, 2);
- LittleEndian::Write(data_, colIndex_, 2, 2);
- LittleEndian::Write(data_, XFRecordIndex_, 4, 2);
- LittleEndian::Write(data_, value_, 6, 1);
- LittleEndian::Write(data_, error_, 7, 1);
- return Record::Write(data);
- }
- Worksheet::CellTable::RowBlock::CellBlock::LabelSST::LabelSST() : Record(),
- rowIndex_(0), colIndex_(0), XFRecordIndex_(0), SSTRecordIndex_(0)
- {code_ = CODE::LABELSST; dataSize_ = 10; recordSize_ = 14;}
- size_t Worksheet::CellTable::RowBlock::CellBlock::LabelSST::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, rowIndex_, 0, 2);
- LittleEndian::Read(data_, colIndex_, 2, 2);
- LittleEndian::Read(data_, XFRecordIndex_, 4, 2);
- LittleEndian::Read(data_, SSTRecordIndex_, 6, 4);
- return RecordSize();
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::LabelSST::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, rowIndex_, 0, 2);
- LittleEndian::Write(data_, colIndex_, 2, 2);
- LittleEndian::Write(data_, XFRecordIndex_, 4, 2);
- LittleEndian::Write(data_, SSTRecordIndex_, 6, 4);
- return Record::Write(data);
- }
- Worksheet::CellTable::RowBlock::CellBlock::MulBlank::MulBlank() : Record(),
- rowIndex_(0), firstColIndex_(0), lastColIndex_(0)
- {code_ = CODE::MULBLANK; dataSize_ = 10; recordSize_ = 14;}
- size_t Worksheet::CellTable::RowBlock::CellBlock::MulBlank::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, rowIndex_, 0, 2);
- LittleEndian::Read(data_, firstColIndex_, 2, 2);
- LittleEndian::Read(data_, lastColIndex_, dataSize_-2, 2);
- size_t nc = lastColIndex_ - firstColIndex_ + 1;
- XFRecordIndices_.clear();
- XFRecordIndices_.resize(nc);
- for (size_t i=0; i<nc; ++i)
- {
- LittleEndian::Read(data_, XFRecordIndices_[i], 4+i*2, 2);
- }
- return RecordSize();
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::MulBlank::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, rowIndex_, 0, 2);
- LittleEndian::Write(data_, firstColIndex_, 2, 2);
- LittleEndian::Write(data_, lastColIndex_, dataSize_-2, 2);
- size_t nc = XFRecordIndices_.size();
- for (size_t i=0; i<nc; ++i)
- {
- LittleEndian::Write(data_, XFRecordIndices_[i], 4+i*2, 2);
- }
- return Record::Write(data);
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::MulBlank::DataSize() {return (dataSize_ = 6 + XFRecordIndices_.size()*2);}
- size_t Worksheet::CellTable::RowBlock::CellBlock::MulBlank::RecordSize()
- {
- size_t dataSize = DataSize();
- return (recordSize_ = dataSize + 4*(dataSize/8224 + 1));
- }
- Worksheet::CellTable::RowBlock::CellBlock::MulRK::XFRK::XFRK() :
- XFRecordIndex_(0), RKValue_(0) {};
- void Worksheet::CellTable::RowBlock::CellBlock::MulRK::XFRK::Read(const char* data)
- {
- LittleEndian::Read(data, XFRecordIndex_, 0, 2);
- LittleEndian::Read(data, RKValue_, 2, 4);
- }
- void Worksheet::CellTable::RowBlock::CellBlock::MulRK::XFRK::Write(char* data)
- {
- LittleEndian::Write(data, XFRecordIndex_, 0, 2);
- LittleEndian::Write(data, RKValue_, 2, 4);
- }
- Worksheet::CellTable::RowBlock::CellBlock::MulRK::MulRK() : Record(),
- rowIndex_(0), firstColIndex_(0), lastColIndex_(0) {code_ = CODE::MULRK; dataSize_ = 10; recordSize_ = 14;}
- size_t Worksheet::CellTable::RowBlock::CellBlock::MulRK::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, rowIndex_, 0, 2);
- LittleEndian::Read(data_, firstColIndex_, 2, 2);
- LittleEndian::Read(data_, lastColIndex_, dataSize_-2, 2);
- size_t nc = lastColIndex_ - firstColIndex_ + 1;
- XFRK_.clear();
- XFRK_.resize(nc);
- for (size_t i=0; i<nc; ++i)
- {
- XFRK_[i].Read(&*(data_.begin())+4+i*6);
- }
- return RecordSize();
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::MulRK::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, rowIndex_, 0, 2);
- LittleEndian::Write(data_, firstColIndex_, 2, 2);
- LittleEndian::Write(data_, lastColIndex_, dataSize_-2, 2);
- size_t nc = XFRK_.size();
- for (size_t i=0; i<nc; ++i)
- {
- XFRK_[i].Write(&*(data_.begin())+4+i*6);
- }
- return Record::Write(data);
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::MulRK::DataSize() {return (dataSize_ = 6 + XFRK_.size()*6);}
- size_t Worksheet::CellTable::RowBlock::CellBlock::MulRK::RecordSize()
- {
- size_t dataSize = DataSize();
- return (recordSize_ = dataSize + 4*(dataSize/8224 + 1));
- }
- Worksheet::CellTable::RowBlock::CellBlock::Number::Number() : Record(),
- rowIndex_(0), colIndex_(0), XFRecordIndex_(0), value_(0) {code_ = CODE::NUMBER; dataSize_ = 14; recordSize_ = 18;}
- size_t Worksheet::CellTable::RowBlock::CellBlock::Number::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, rowIndex_, 0, 2);
- LittleEndian::Read(data_, colIndex_, 2, 2);
- LittleEndian::Read(data_, XFRecordIndex_, 4, 2);
- __int64 value;
- LittleEndian::Read(data_, value, 6, 8);
- intdouble_.intvalue_ = value;
- value_ = intdouble_.doublevalue_;
- return RecordSize();
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::Number::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, rowIndex_, 0, 2);
- LittleEndian::Write(data_, colIndex_, 2, 2);
- LittleEndian::Write(data_, XFRecordIndex_, 4, 2);
- intdouble_.doublevalue_ = value_;
- __int64 value = intdouble_.intvalue_;
- LittleEndian::Write(data_, value, 6, 8);
- return Record::Write(data);
- }
- Worksheet::CellTable::RowBlock::CellBlock::RK::RK() : Record(),
- rowIndex_(0), colIndex_(0), XFRecordIndex_(0), value_(0) {code_ = CODE::RK; dataSize_ = 10; recordSize_ = 14;}
- size_t Worksheet::CellTable::RowBlock::CellBlock::RK::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, rowIndex_, 0, 2);
- LittleEndian::Read(data_, colIndex_, 2, 2);
- LittleEndian::Read(data_, XFRecordIndex_, 4, 2);
- LittleEndian::Read(data_, value_, 6, 4);
- return RecordSize();
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::RK::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, rowIndex_, 0, 2);
- LittleEndian::Write(data_, colIndex_, 2, 2);
- LittleEndian::Write(data_, XFRecordIndex_, 4, 2);
- LittleEndian::Write(data_, value_, 6, 4);
- return Record::Write(data);
- }
- Worksheet::CellTable::RowBlock::CellBlock::Formula::Formula() : Record(),
- rowIndex_(0), colIndex_(0), XFRecordIndex_(0), options_(0), unused_(0), type_(-1)
- {code_ = CODE::FORMULA; dataSize_ = 18; recordSize_ = 22;}
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, rowIndex_, 0, 2);
- LittleEndian::Read(data_, colIndex_, 2, 2);
- LittleEndian::Read(data_, XFRecordIndex_, 4, 2);
- LittleEndian::ReadString(data_, result_, 6, 8);
- LittleEndian::Read(data_, options_, 14, 2);
- LittleEndian::Read(data_, unused_, 16, 2);
- RPNtoken_.clear();
- RPNtoken_.resize(dataSize_-18);
- LittleEndian::ReadString(data_, &*(RPNtoken_.begin()), 18, dataSize_-18);
- size_t offset = dataSize_ + 4;
- short code;
- LittleEndian::Read(data, code, offset, 2);
- switch (code)
- {
- case CODE::ARRAY:
- type_ = code;
- array_.Read(data+offset);
- offset += array_.RecordSize();
- break;
-
- case CODE::SHRFMLA:
- type_ = code;
- shrfmla_.Read(data+offset);
- offset += shrfmla_.RecordSize();
- break;
-
- case CODE::SHRFMLA1:
- type_ = code;
- shrfmla1_.Read(data+offset);
- offset += shrfmla1_.RecordSize();
- break;
- case CODE::TABLE:
- type_ = code;
- table_.Read(data+offset);
- offset += table_.RecordSize();
- break;
- }
- LittleEndian::Read(data, code, offset, 2);
- if (code == CODE::STRING) string_.Read(data+offset);
- return RecordSize();
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, rowIndex_, 0, 2);
- LittleEndian::Write(data_, colIndex_, 2, 2);
- LittleEndian::Write(data_, XFRecordIndex_, 4, 2);
- LittleEndian::WriteString(data_, result_, 6, 8);
- LittleEndian::Write(data_, options_, 14, 2);
- LittleEndian::Write(data_, unused_, 16, 2);
- LittleEndian::WriteString(data_, &*(RPNtoken_.begin()), 18, RPNtoken_.size());
- Record::Write(data);
- size_t offset = dataSize_ + 4;
- switch (type_)
- {
- case CODE::ARRAY:
- array_.Write(data+offset);
- offset += array_.RecordSize();
- break;
-
- case CODE::SHRFMLA:
- shrfmla_.Write(data+offset);
- offset += shrfmla_.RecordSize();
- break;
-
- case CODE::SHRFMLA1:
- shrfmla1_.Write(data+offset);
- offset += shrfmla1_.RecordSize();
- break;
- case CODE::TABLE:
- table_.Write(data+offset);
- offset += table_.RecordSize();
- break;
- }
- if (string_.DataSize() != 0) string_.Write(data+offset);
- return RecordSize();
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::DataSize() {return (dataSize_ = 18 + RPNtoken_.size());}
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::RecordSize()
- {
- size_t dataSize = DataSize();
- recordSize_ = dataSize + 4*(dataSize/8224 + 1);
-
- switch (type_)
- {
- case CODE::ARRAY:
- recordSize_ += array_.RecordSize();
- break;
-
- case CODE::SHRFMLA:
- recordSize_ += shrfmla_.RecordSize();
- break;
-
- case CODE::SHRFMLA1:
- recordSize_ += shrfmla1_.RecordSize();
- break;
- case CODE::TABLE:
- recordSize_ += table_.RecordSize();
- break;
- }
- if (string_.DataSize() != 0) recordSize_ += string_.RecordSize();
- return (recordSize_);
- }
- Worksheet::CellTable::RowBlock::CellBlock::Formula::Array::Array() : Record(),
- firstRowIndex_(0), lastRowIndex_(0), firstColIndex_(0), lastColIndex_(0),
- options_(0), unused_(0)
- {code_ = CODE::ARRAY; dataSize_ = 12; recordSize_ = 16;}
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::Array::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, firstRowIndex_, 0, 2);
- LittleEndian::Read(data_, lastRowIndex_, 2, 2);
- LittleEndian::Read(data_, firstColIndex_, 4, 1);
- LittleEndian::Read(data_, lastColIndex_, 5, 1);
- LittleEndian::Read(data_, options_, 6, 2);
- LittleEndian::Read(data_, unused_, 8, 4);
- formula_.clear();
- formula_.resize(dataSize_-12);
- LittleEndian::ReadString(data_, &*(formula_.begin()), 12, dataSize_-12);
- return RecordSize();
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::Array::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, firstRowIndex_, 0, 2);
- LittleEndian::Write(data_, lastRowIndex_, 2, 2);
- LittleEndian::Write(data_, firstColIndex_, 4, 1);
- LittleEndian::Write(data_, lastColIndex_, 5, 1);
- LittleEndian::Write(data_, options_, 6, 2);
- LittleEndian::Write(data_, unused_, 8, 4);
- LittleEndian::WriteString(data_, &*(formula_.begin()), 12, formula_.size());
- return Record::Write(data);
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::Array::DataSize() {return (dataSize_ = 12 + formula_.size());}
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::Array::RecordSize()
- {
- size_t dataSize = DataSize();
- return (recordSize_ = dataSize + 4*(dataSize/8224 + 1));
- }
- Worksheet::CellTable::RowBlock::CellBlock::Formula::ShrFmla::ShrFmla() : Record(),
- firstRowIndex_(0), lastRowIndex_(0), firstColIndex_(0), lastColIndex_(0),
- unused_(0)
- {code_ = CODE::SHRFMLA; dataSize_ = 8; recordSize_ = 12;}
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::ShrFmla::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, firstRowIndex_, 0, 2);
- LittleEndian::Read(data_, lastRowIndex_, 2, 2);
- LittleEndian::Read(data_, firstColIndex_, 4, 1);
- LittleEndian::Read(data_, lastColIndex_, 5, 1);
- LittleEndian::Read(data_, unused_, 6, 2);
- formula_.clear();
- formula_.resize(dataSize_-8);
- LittleEndian::ReadString(data_, &*(formula_.begin()), 8, dataSize_-8);
- return RecordSize();
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::ShrFmla::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, firstRowIndex_, 0, 2);
- LittleEndian::Write(data_, lastRowIndex_, 2, 2);
- LittleEndian::Write(data_, firstColIndex_, 4, 1);
- LittleEndian::Write(data_, lastColIndex_, 5, 1);
- LittleEndian::Write(data_, unused_, 6, 2);
- LittleEndian::WriteString(data_, &*(formula_.begin()), 8, formula_.size());
- return Record::Write(data);
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::ShrFmla::DataSize() {return (dataSize_ = 8 + formula_.size());}
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::ShrFmla::RecordSize()
- {
- size_t dataSize = DataSize();
- return (recordSize_ = dataSize + 4*(dataSize/8224 + 1));
- }
- Worksheet::CellTable::RowBlock::CellBlock::Formula::ShrFmla1::ShrFmla1() : Record(),
- firstRowIndex_(0), lastRowIndex_(0), firstColIndex_(0), lastColIndex_(0),
- unused_(0)
- {code_ = CODE::SHRFMLA1; dataSize_ = 8; recordSize_ = 12;}
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::ShrFmla1::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, firstRowIndex_, 0, 2);
- LittleEndian::Read(data_, lastRowIndex_, 2, 2);
- LittleEndian::Read(data_, firstColIndex_, 4, 1);
- LittleEndian::Read(data_, lastColIndex_, 5, 1);
- LittleEndian::Read(data_, unused_, 6, 2);
- formula_.clear();
- formula_.resize(dataSize_-8);
- LittleEndian::ReadString(data_, &*(formula_.begin()), 8, dataSize_-8);
- return RecordSize();
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::ShrFmla1::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, firstRowIndex_, 0, 2);
- LittleEndian::Write(data_, lastRowIndex_, 2, 2);
- LittleEndian::Write(data_, firstColIndex_, 4, 1);
- LittleEndian::Write(data_, lastColIndex_, 5, 1);
- LittleEndian::Write(data_, unused_, 6, 2);
- LittleEndian::WriteString(data_, &*(formula_.begin()), 8, formula_.size());
- return Record::Write(data);
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::ShrFmla1::DataSize() {return (dataSize_ = 8 + formula_.size());}
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::ShrFmla1::RecordSize()
- {
- size_t dataSize = DataSize();
- return (recordSize_ = dataSize + 4*(dataSize/8224 + 1));
- }
- Worksheet::CellTable::RowBlock::CellBlock::Formula::Table::Table() : Record(),
- firstRowIndex_(0), lastRowIndex_(0), firstColIndex_(0), lastColIndex_(0), options_(0),
- inputCellRowIndex_(0), inputCellColIndex_(0),
- inputCellColumnInputRowIndex_(0), inputCellColumnInputColIndex_(0)
- {code_ = CODE::TABLE; dataSize_ = 16; recordSize_ = 20;}
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::Table::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, firstRowIndex_, 0, 2);
- LittleEndian::Read(data_, lastRowIndex_, 2, 2);
- LittleEndian::Read(data_, firstColIndex_, 4, 1);
- LittleEndian::Read(data_, lastColIndex_, 5, 1);
- LittleEndian::Read(data_, options_, 6, 2);
- LittleEndian::Read(data_, inputCellRowIndex_, 8, 2);
- LittleEndian::Read(data_, inputCellColIndex_, 10, 2);
- LittleEndian::Read(data_, inputCellColumnInputRowIndex_, 12, 2);
- LittleEndian::Read(data_, inputCellColumnInputColIndex_, 14, 2);
- return RecordSize();
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::Table::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, firstRowIndex_, 0, 2);
- LittleEndian::Write(data_, lastRowIndex_, 2, 2);
- LittleEndian::Write(data_, firstColIndex_, 4, 1);
- LittleEndian::Write(data_, lastColIndex_, 5, 1);
- LittleEndian::Write(data_, options_, 6, 2);
- LittleEndian::Write(data_, inputCellRowIndex_, 8, 2);
- LittleEndian::Write(data_, inputCellColIndex_, 10, 2);
- LittleEndian::Write(data_, inputCellColumnInputRowIndex_, 12, 2);
- LittleEndian::Write(data_, inputCellColumnInputColIndex_, 14, 2);
- return Record::Write(data);
- }
- Worksheet::CellTable::RowBlock::CellBlock::Formula::String::String() : Record()
- {code_ = CODE::STRING; dataSize_ = 0; recordSize_ = 4;}
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::String::Read(const char* data)
- {
- Record::Read(data);
- string_.clear();
- string_.resize(dataSize_);
- LittleEndian::ReadString(data_, &*(string_.begin()), 0, dataSize_);
- return RecordSize();
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::String::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::WriteString(data_, &*(string_.begin()), 0, string_.size());
- return Record::Write(data);
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::String::DataSize() {return (dataSize_ = string_.size());}
- size_t Worksheet::CellTable::RowBlock::CellBlock::Formula::String::RecordSize()
- {
- size_t dataSize = DataSize();
- return (recordSize_ = dataSize + 4*(dataSize/8224 + 1));
- }
- Worksheet::CellTable::RowBlock::Row::Row() : Record(),
- rowIndex_(0), firstCellColIndex_(0), lastCellColIndexPlusOne_(0), height_(255),
- unused1_(0), unused2_(0), options_(0) {code_ = CODE::ROW; dataSize_ = 16; recordSize_ = 20;}
- size_t Worksheet::CellTable::RowBlock::Row::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, rowIndex_, 0, 2);
- LittleEndian::Read(data_, firstCellColIndex_, 2, 2);
- LittleEndian::Read(data_, lastCellColIndexPlusOne_, 4, 2);
- LittleEndian::Read(data_, height_, 6, 2);
- LittleEndian::Read(data_, unused1_, 8, 2);
- LittleEndian::Read(data_, unused2_, 10, 2);
- LittleEndian::Read(data_, options_, 12, 4);
- return RecordSize();
- }
- size_t Worksheet::CellTable::RowBlock::Row::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, rowIndex_, 0, 2);
- LittleEndian::Write(data_, firstCellColIndex_, 2, 2);
- LittleEndian::Write(data_, lastCellColIndexPlusOne_, 4, 2);
- LittleEndian::Write(data_, height_, 6, 2);
- LittleEndian::Write(data_, unused1_, 8, 2);
- LittleEndian::Write(data_, unused2_, 10, 2);
- LittleEndian::Write(data_, options_, 12, 4);
- return Record::Write(data);
- }
- Worksheet::CellTable::RowBlock::CellBlock::CellBlock() :
- type_(-1), normalType_(true) {};
- Worksheet::CellTable::RowBlock::CellBlock::~CellBlock() {};
- size_t Worksheet::CellTable::RowBlock::CellBlock::Read(const char* data)
- {
- size_t bytesRead = 0;
- LittleEndian::Read(data, type_, 0, 2);
- switch (type_)
- {
- case CODE::BLANK:
- bytesRead += blank_.Read(data);
- break;
-
- case CODE::BOOLERR:
- bytesRead += boolerr_.Read(data);
- break;
-
- case CODE::LABELSST:
- bytesRead += labelsst_.Read(data);
- break;
-
- case CODE::MULBLANK:
- bytesRead += mulblank_.Read(data);
- break;
-
- case CODE::MULRK:
- bytesRead += mulrk_.Read(data);
- break;
-
- case CODE::NUMBER:
- bytesRead += number_.Read(data);
- break;
-
- case CODE::RK:
- bytesRead += rk_.Read(data);
- break;
- case CODE::FORMULA:
- bytesRead += formula_.Read(data);
- break;
- }
- return bytesRead;
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::Write(char* data)
- {
- size_t bytesWritten = 0;
- switch (type_)
- {
- case CODE::BLANK:
- bytesWritten += blank_.Write(data);
- break;
-
- case CODE::BOOLERR:
- bytesWritten += boolerr_.Write(data);
- break;
-
- case CODE::LABELSST:
- bytesWritten += labelsst_.Write(data);
- break;
-
- case CODE::MULBLANK:
- bytesWritten += mulblank_.Write(data);
- break;
-
- case CODE::MULRK:
- bytesWritten += mulrk_.Write(data);
- break;
-
- case CODE::NUMBER:
- bytesWritten += number_.Write(data);
- break;
-
- case CODE::RK:
- bytesWritten += rk_.Write(data);
- break;
- case CODE::FORMULA:
- bytesWritten += formula_.Write(data);
- break;
- }
- return bytesWritten;
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::DataSize()
- {
- switch (type_)
- {
- case CODE::BLANK:
- return blank_.DataSize();
-
- case CODE::BOOLERR:
- return boolerr_.DataSize();
-
- case CODE::LABELSST:
- return labelsst_.DataSize();
-
- case CODE::MULBLANK:
- return mulblank_.DataSize();
-
- case CODE::MULRK:
- return mulrk_.DataSize();
-
- case CODE::NUMBER:
- return number_.DataSize();
-
- case CODE::RK:
- return rk_.DataSize();
- case CODE::FORMULA:
- return formula_.DataSize();
- }
- abort();
- }
- size_t Worksheet::CellTable::RowBlock::CellBlock::RecordSize()
- {
- switch (type_)
- {
- case CODE::BLANK:
- return blank_.RecordSize();
-
- case CODE::BOOLERR:
- return boolerr_.RecordSize();
-
- case CODE::LABELSST:
- return labelsst_.RecordSize();
-
- case CODE::MULBLANK:
- return mulblank_.RecordSize();
-
- case CODE::MULRK:
- return mulrk_.RecordSize();
-
- case CODE::NUMBER:
- return number_.RecordSize();
-
- case CODE::RK:
- return rk_.RecordSize();
- case CODE::FORMULA:
- return formula_.RecordSize();
- }
- abort();
- }
- short Worksheet::CellTable::RowBlock::CellBlock::RowIndex()
- {
- switch (type_)
- {
- case CODE::BLANK:
- return blank_.rowIndex_;
-
- case CODE::BOOLERR:
- return boolerr_.rowIndex_;
-
- case CODE::LABELSST:
- return labelsst_.rowIndex_;
-
- case CODE::MULBLANK:
- return mulblank_.rowIndex_;
-
- case CODE::MULRK:
- return mulrk_.rowIndex_;
-
- case CODE::NUMBER:
- return number_.rowIndex_;
-
- case CODE::RK:
- return rk_.rowIndex_;
- case CODE::FORMULA:
- return formula_.rowIndex_;
- }
- abort();
- }
- short Worksheet::CellTable::RowBlock::CellBlock::ColIndex()
- {
- switch (type_)
- {
- case CODE::BLANK:
- return blank_.colIndex_;
-
- case CODE::BOOLERR:
- return boolerr_.colIndex_;
-
- case CODE::LABELSST:
- return labelsst_.colIndex_;
-
- case CODE::MULBLANK:
- return mulblank_.firstColIndex_;
-
- case CODE::MULRK:
- return mulrk_.firstColIndex_;
-
- case CODE::NUMBER:
- return number_.colIndex_;
-
- case CODE::RK:
- return rk_.colIndex_;
- case CODE::FORMULA:
- return formula_.colIndex_;
- }
- abort();
- }
- Worksheet::CellTable::RowBlock::DBCell::DBCell() : Record(),
- firstRowOffset_(0) {code_ = CODE::DBCELL; dataSize_ = 4; recordSize_ = 8;}
- size_t Worksheet::CellTable::RowBlock::DBCell::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, firstRowOffset_, 0, 4);
- size_t nm = (dataSize_-4) / 2;
- offsets_.clear();
- offsets_.resize(nm);
- for (size_t i=0; i<nm; ++i)
- {
- LittleEndian::Read(data_, offsets_[i], 4+i*2, 2);
- }
- return RecordSize();
- }
- size_t Worksheet::CellTable::RowBlock::DBCell::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, firstRowOffset_, 0, 4);
- size_t nm = offsets_.size();
- for (size_t i=0; i<nm; ++i)
- {
- LittleEndian::Write(data_, offsets_[i], 4+i*2, 2);
- }
- return Record::Write(data);
- }
- size_t Worksheet::CellTable::RowBlock::DBCell::DataSize() {return (dataSize_ = 4+offsets_.size()*2);}
- size_t Worksheet::CellTable::RowBlock::DBCell::RecordSize()
- {
- size_t dataSize = DataSize();
- return (recordSize_ = dataSize + 4*(dataSize/8224 + 1));
- }
- size_t Worksheet::CellTable::RowBlock::Read(const char* data)
- {
- size_t bytesRead = 0;
- short code;
- LittleEndian::Read(data, code, 0, 2);
- Row row;
- CellBlock cellBlock;
- cellBlocks_.reserve(1000);
- while (code != CODE::DBCELL)
- {
- switch (code)
- {
- case CODE::ROW:
- rows_.push_back(row);
- bytesRead += rows_.back().Read(data+bytesRead);
- break;
-
- case CODE::BLANK:
- case CODE::BOOLERR:
- case CODE::LABELSST:
- case CODE::MULBLANK:
- case CODE::MULRK:
- case CODE::NUMBER:
- case CODE::RK:
- case CODE::FORMULA:
- cellBlocks_.push_back(cellBlock);
- if (cellBlocks_.size()%1000==0) cellBlocks_.reserve(cellBlocks_.size()+1000);
- bytesRead += cellBlocks_[cellBlocks_.size()-1].Read(data+bytesRead);
- break;
- default:
- Record rec;
- bytesRead += rec.Read(data+bytesRead);
- }
- LittleEndian::Read(data, code, bytesRead, 2);
- }
- bytesRead += dbcell_.Read(data+bytesRead);
- return bytesRead;
- }
- size_t Worksheet::CellTable::RowBlock::Write(char* data)
- {
- size_t bytesWritten = 0;
- size_t maxRows = rows_.size();
- {for (size_t i=0; i<maxRows; ++i)
- {
- bytesWritten += rows_[i].Write(data+bytesWritten);
- }}
- size_t maxCellBlocks = cellBlocks_.size();
- {for (size_t i=0; i<maxCellBlocks; ++i)
- {
- bytesWritten += cellBlocks_[i].Write(data+bytesWritten);
- }}
-
- bytesWritten += dbcell_.Write(data+bytesWritten);
- return bytesWritten;
- }
- size_t Worksheet::CellTable::RowBlock::DataSize()
- {
- size_t dataSize = 0;
- size_t maxRows = rows_.size();
- {for (size_t i=0; i<maxRows; ++i) dataSize += rows_[i].RecordSize();}
-
- size_t maxCellBlocks = cellBlocks_.size();
- {for (size_t i=0; i<maxCellBlocks; ++i) dataSize += cellBlocks_[i].RecordSize();}
-
- dataSize += dbcell_.RecordSize();
- return dataSize;
- }
- size_t Worksheet::CellTable::RowBlock::RecordSize() {return DataSize();}
- size_t Worksheet::CellTable::Read(const char* data)
- {
- size_t bytesRead = 0;
-
- short code;
- LittleEndian::Read(data, code, 0, 2);
- RowBlock rowBlock;
- rowBlocks_.reserve(1000);
- while (code == CODE::ROW)
- {
- rowBlocks_.push_back(rowBlock);
- bytesRead += rowBlocks_.back().Read(data+bytesRead);
- LittleEndian::Read(data, code, bytesRead, 2);
- }
- return bytesRead;
- }
- size_t Worksheet::CellTable::Write(char* data)
- {
- size_t bytesWritten = 0;
- size_t maxRowBlocks_ = rowBlocks_.size();
- for (size_t i=0; i<maxRowBlocks_; ++i)
- {
- bytesWritten += rowBlocks_[i].Write(data+bytesWritten);
- }
- return bytesWritten;
- }
- size_t Worksheet::CellTable::DataSize()
- {
- size_t dataSize = 0;
- size_t maxRowBlocks_ = rowBlocks_.size();
- for (size_t i=0; i<maxRowBlocks_; ++i) dataSize += rowBlocks_[i].RecordSize();
- return dataSize;
- }
- size_t Worksheet::CellTable::RecordSize() {return DataSize();}
- Worksheet::Window2::Window2() : Record(),
- options_(1718), firstVisibleRowIndex_(0), firstVisibleColIndex_(0), gridLineColourIndex_(64),
- unused1_(0), magnificationFactorPageBreakPreview_(0), magnificationFactorNormalView_(0), unused2_(0)
- {code_ = CODE::WINDOW2; dataSize_ = 18; recordSize_ = 22;}
- size_t Worksheet::Window2::Read(const char* data)
- {
- Record::Read(data);
- LittleEndian::Read(data_, options_, 0, 2);
- LittleEndian::Read(data_, firstVisibleRowIndex_, 2, 2);
- LittleEndian::Read(data_, firstVisibleColIndex_, 4, 2);
- LittleEndian::Read(data_, gridLineColourIndex_, 6, 2);
- LittleEndian::Read(data_, unused1_, 8, 2);
- LittleEndian::Read(data_, magnificationFactorPageBreakPreview_, 10, 2);
- LittleEndian::Read(data_, magnificationFactorNormalView_, 12, 2);
- LittleEndian::Read(data_, unused2_, 14, 4);
- return RecordSize();
- }
- size_t Worksheet::Window2::Write(char* data)
- {
- data_.resize(DataSize());
- LittleEndian::Write(data_, options_, 0, 2);
- LittleEndian::Write(data_, firstVisibleRowIndex_, 2, 2);
- LittleEndian::Write(data_, firstVisibleColIndex_, 4, 2);
- LittleEndian::Write(data_, gridLineColourIndex_, 6, 2);
- LittleEndian::Write(data_, unused1_, 8, 2);
- LittleEndian::Write(data_, magnificationFactorPageBreakPreview_, 10, 2);
- LittleEndian::Write(data_, magnificationFactorNormalView_, 12, 2);
- LittleEndian::Write(data_, unused2_, 14, 4);
- return Record::Write(data);
- }
- bool IsRKValueAnInteger(int rkValue)
- {
- return (rkValue & 2);
- }
- bool IsRKValueADouble(int rkValue)
- {
- return !(rkValue & 2);
- }
- double GetDoubleFromRKValue(int rkValue)
- {
- union
- {
- __int64 intvalue_;
- double doublevalue_;
- } intdouble;
- bool isMultiplied = rkValue & 1;
- rkValue >>= 2;
- intdouble.intvalue_ = rkValue;
- intdouble.intvalue_ <<= 34;
- if (isMultiplied) intdouble.doublevalue_ *= 0.01;
- return intdouble.doublevalue_;
- }
- int GetIntegerFromRKValue(int rkValue)
- {
- bool isMultiplied = rkValue & 1;
- rkValue >>= 2;
- if (isMultiplied) rkValue *= 0.01;
- return rkValue;
- }
- int GetRKValueFromDouble(double value)
- {
- union
- {
- __int64 intvalue_;
- double doublevalue_;
- } intdouble;
- bool isMultiplied = false;
- int testVal1 = value;
- testVal1 *= 100;
- int testVal2 = value * 100;
- if (testVal1!=testVal2)
- {
- isMultiplied = true;
- value *= 100;
- }
- intdouble.doublevalue_ = value;
- intdouble.intvalue_ >>= 34;
- int rkValue = intdouble.intvalue_;
- rkValue <<= 2;
- rkValue |= isMultiplied;
- return rkValue;
- }
- int GetRKValueFromInteger(int value)
- {
- value <<= 2;
- value |= 2;
- return value;
- }
- bool CanStoreAsRKValue(double value)
- {
- int testVal1 = value * 100;
- testVal1 *= 100;
- int testVal2 = value * 10000;
- if (testVal1!=testVal2) return false;
- else return true;
- }
- BasicExcel::BasicExcel() {};
- BasicExcel::BasicExcel(const char* filename)
- {
- Load(filename);
- }
- BasicExcel::~BasicExcel()
- {
- if (file_.IsOpen()) file_.Close();
- }
- void BasicExcel::New(int sheets)
- {
- workbook_ = Workbook();
- worksheets_.clear();
- workbook_.fonts_.resize(4);
- workbook_.XFs_.resize(21);
- workbook_.styles_.resize(6);
- workbook_.boundSheets_.resize(1);
- worksheets_.resize(1);
- UpdateYExcelWorksheet();
- for (int i=0; i<sheets-1; ++i) AddWorksheet();
- }
- bool BasicExcel::Load(const char* filename)
- {
- if (file_.IsOpen()) file_.Close();
- if (file_.Open(filename))
- {
- workbook_ = Workbook();
- worksheets_.clear();
- vector<char> data;
- file_.ReadFile("Workbook", data);
- Read(&*(data.begin()), data.size());
- UpdateYExcelWorksheet();
- return true;
- }
- else return false;
- }
- bool BasicExcel::Save()
- {
- if (file_.IsOpen())
- {
-
- UpdateWorksheets();
- AdjustStreamPositions();
-
- size_t minBytes = workbook_.RecordSize();
- size_t maxWorkSheets = worksheets_.size();
- for (size_t i=0; i<maxWorkSheets; ++i)
- {
- minBytes += worksheets_[i].RecordSize();
- }
-
-
- vector<char> data(minBytes,0);
- Write(&*(data).begin());
- if (file_.WriteFile("Workbook", data, data.size())!=CompoundFile::SUCCESS) return false;
- return true;
- }
- else return false;
- }
- bool BasicExcel::SaveAs(const char* filename)
- {
- if (file_.IsOpen()) file_.Close();
- if (!file_.Create(filename)) return false;
- if (file_.MakeFile("Workbook")!=CompoundFile::SUCCESS) return false;
- return Save();
- }
- size_t BasicExcel::GetTotalWorkSheets()
- {
- return worksheets_.size();
- }
- BasicExcelWorksheet* BasicExcel::GetWorksheet(size_t sheetIndex)
- {
- return &(yesheets_[sheetIndex]);
- }
- BasicExcelWorksheet* BasicExcel::GetWorksheet(const char* name)
- {
- size_t maxWorksheets = yesheets_.size();
- for (size_t i=0; i<maxWorksheets; ++i)
- {
- if (workbook_.boundSheets_[i].name_.unicode_ & 1) continue;
- if (strcmp(name, workbook_.boundSheets_[i].name_.name_) == 0) return &(yesheets_[i]);
- }
- return 0;
- }
- BasicExcelWorksheet* BasicExcel::GetWorksheet(const wchar_t* name)
- {
- size_t maxWorksheets = yesheets_.size();
- for (size_t i=0; i<maxWorksheets; ++i)
- {
- if (!(workbook_.boundSheets_[i].name_.unicode_ & 1)) continue;
- if (wcscmp(name, workbook_.boundSheets_[i].name_.wname_) == 0) return &(yesheets_[i]);
- }
- return 0;
- }
- BasicExcelWorksheet* BasicExcel::AddWorksheet(int sheetIndex)
- {
- size_t sheetNo = yesheets_.size() + 1;
- BasicExcelWorksheet* yesheet = 0;
- do
- {
- char sname[50];
- sprintf(sname, "Sheet%d", sheetNo++);
- yesheet = AddWorksheet(sname, sheetIndex);
- } while (!yesheet);
- return yesheet;
- }
- BasicExcelWorksheet* BasicExcel::AddWorksheet(const char* name, int sheetIndex)
- {
- size_t maxWorksheets = yesheets_.size();
- for (size_t i=0; i<maxWorksheets; ++i)
- {
- if (workbook_.boundSheets_[i].name_.unicode_ & 1) continue;
- if (strcmp(name, workbook_.boundSheets_[i].name_.name_) == 0) return 0;
- }
- Workbook::BoundSheet* boundSheet;
- Worksheet* worksheet;
- BasicExcelWorksheet* yesheet;
- if (sheetIndex == -1)
- {
- workbook_.boundSheets_.push_back(Workbook::BoundSheet());
- worksheets_.push_back(Worksheet());
- yesheets_.push_back(BasicExcelWorksheet(this, worksheets_.size()-1));
- boundSheet = &(workbook_.boundSheets_.back());
- worksheet = &(worksheets_.back());
- yesheet = &(yesheets_.back());
- }
- else
- {
- boundSheet = &*(workbook_.boundSheets_.insert(workbook_.boundSheets_.begin()+sheetIndex, Workbook::BoundSheet()));
- worksheet = &*(worksheets_.insert(worksheets_.begin()+sheetIndex, Worksheet()));
- yesheet = &*(yesheets_.insert(yesheets_.begin()+sheetIndex, BasicExcelWorksheet(this, sheetIndex)));
- size_t maxSheets = worksheets_.size();
- for (size_t i=sheetIndex+1; i<maxSheets; ++i)
- {
- yesheets_[i].sheetIndex_ = i;
- }
- }
- boundSheet->name_ = name;
- worksheet->window2_.options_ &= ~0x200;
- return yesheet;
- }
- BasicExcelWorksheet* BasicExcel::AddWorksheet(const wchar_t* name, int sheetIndex)
- {
- size_t maxWorksheets = yesheets_.size();
- for (size_t i=0; i<maxWorksheets; ++i)
- {
- if (!(workbook_.boundSheets_[i].name_.unicode_ & 1)) continue;
- if (wcscmp(name, workbook_.boundSheets_[i].name_.wname_) == 0) return 0;
- }
- Workbook::BoundSheet* boundSheet;
- Worksheet* worksheet;
- BasicExcelWorksheet* yesheet;
- if (sheetIndex == -1)
- {
- workbook_.boundSheets_.push_back(Workbook::BoundSheet());
- worksheets_.push_back(Worksheet());
- yesheets_.push_back(BasicExcelWorksheet(this, worksheets_.size()-1));
- boundSheet = &(workbook_.boundSheets_.back());
- worksheet = &(worksheets_.back());
- yesheet = &(yesheets_.back());
- }
- else
- {
- boundSheet = &*(workbook_.boundSheets_.insert(workbook_.boundSheets_.begin()+sheetIndex, Workbook::BoundSheet()));
- worksheet = &*(worksheets_.insert(worksheets_.begin()+sheetIndex, Worksheet()));
- yesheet = &*(yesheets_.insert(yesheets_.begin()+sheetIndex, BasicExcelWorksheet(this, sheetIndex)));
- size_t maxSheets = worksheets_.size();
- for (size_t i=sheetIndex+1; i<maxSheets; ++i)
- {
- yesheets_[i].sheetIndex_ = i;
- }
- }
- boundSheet->name_ = name;
- worksheet->window2_.options_ &= ~0x200;
- return yesheet;
- }
- bool BasicExcel::DeleteWorksheet(size_t sheetIndex)
- {
- if (sheetIndex<workbook_.boundSheets_.size())
- {
- workbook_.boundSheets_.erase(workbook_.boundSheets_.begin()+sheetIndex);
- worksheets_.erase(worksheets_.begin()+sheetIndex);
- yesheets_.erase(yesheets_.begin()+sheetIndex);
- return true;
- }
- else return false;
- }
- bool BasicExcel::DeleteWorksheet(const char* name)
- {
- size_t maxWorksheets = yesheets_.size();
- for (size_t i=0; i<maxWorksheets; ++i)
- {
- if (workbook_.boundSheets_[i].name_.unicode_ & 1) continue;
- if (strcmp(name, workbook_.boundSheets_[i].name_.name_) == 0) return DeleteWorksheet(i);
- }
- return false;
- }
- bool BasicExcel::DeleteWorksheet(const wchar_t* name)
- {
- size_t maxWorksheets = worksheets_.size();
- for (size_t i=0; i<maxWorksheets; ++i)
- {
- if (!(workbook_.boundSheets_[i].name_.unicode_ & 1)) continue;
- if (wcscmp(name, workbook_.boundSheets_[i].name_.wname_) == 0) return DeleteWorksheet(i);
- }
- return false;
- }
- char* BasicExcel::GetAnsiSheetName(size_t sheetIndex)
- {
- if (!(workbook_.boundSheets_[sheetIndex].name_.unicode_ & 1))
- {
- return workbook_.boundSheets_[sheetIndex].name_.name_;
- }
- else return 0;
- }
- wchar_t* BasicExcel::GetUnicodeSheetName(size_t sheetIndex)
- {
- if (workbook_.boundSheets_[sheetIndex].name_.unicode_ & 1)
- {
- return workbook_.boundSheets_[sheetIndex].name_.wname_;
- }
- else return 0;
- }
- bool BasicExcel::GetSheetName(size_t sheetIndex, char* name)
- {
- if (!(workbook_.boundSheets_[sheetIndex].name_.unicode_ & 1))
- {
- strcpy(name, workbook_.boundSheets_[sheetIndex].name_.name_);
- return true;
- }
- else return false;
- }
- bool BasicExcel::GetSheetName(size_t sheetIndex, wchar_t* name)
- {
- if (workbook_.boundSheets_[sheetIndex].name_.unicode_ & 1)
- {
- wcscpy(name, workbook_.boundSheets_[sheetIndex].name_.wname_);
- return true;
- }
- else return false;
- }
- bool BasicExcel::RenameWorksheet(size_t sheetIndex, const char* to)
- {
- size_t maxWorksheets = yesheets_.size();
- if (sheetIndex < maxWorksheets)
- {
- for (size_t i=0; i<maxWorksheets; ++i)
- {
- if (workbook_.boundSheets_[i].name_.unicode_ & 1) continue;
- if (strcmp(to, workbook_.boundSheets_[i].name_.name_) == 0) return false;
- }
- workbook_.boundSheets_[sheetIndex].name_ = to;
- return true;
- }
- else return false;
- }
- bool BasicExcel::RenameWorksheet(size_t sheetIndex, const wchar_t* to)
- {
- size_t maxWorksheets = yesheets_.size();
- if (sheetIndex < maxWorksheets)
- {
- for (size_t i=0; i<maxWorksheets; ++i)
- {
- if (!(workbook_.boundSheets_[i].name_.unicode_ & 1)) continue;
- if (wcscmp(to, workbook_.boundSheets_[i].name_.wname_) == 0) return false;
- }
- workbook_.boundSheets_[sheetIndex].name_ = to;
- return true;
- }
- else return false;
- }
- bool BasicExcel::RenameWorksheet(const char* from, const char* to)
- {
- size_t maxWorksheets = yesheets_.size();
- for (size_t i=0; i<maxWorksheets; ++i)
- {
- if (workbook_.boundSheets_[i].name_.unicode_ & 1) continue;
- if (strcmp(from, workbook_.boundSheets_[i].name_.name_) == 0)
- {
- for (size_t j=0; j<maxWorksheets; ++j)
- {
- if (workbook_.boundSheets_[j].name_.unicode_ & 1) continue;
- if (strcmp(to, workbook_.boundSheets_[j].name_.name_) == 0) return false;
- }
- workbook_.boundSheets_[i].name_ = to;
- return true;
- }
- }
- return false;
- }
- bool BasicExcel::RenameWorksheet(const wchar_t* from, const wchar_t* to)
- {
- size_t maxWorksheets = worksheets_.size();
- for (size_t i=0; i<maxWorksheets; ++i)
- {
- if (!(workbook_.boundSheets_[i].name_.unicode_ & 1)) continue;
- if (wcscmp(from, workbook_.boundSheets_[i].name_.wname_) == 0)
- {
- for (size_t j=0; j<maxWorksheets; ++j)
- {
- if (!(workbook_.boundSheets_[j].name_.unicode_ & 1)) continue;
- if (wcscmp(to, workbook_.boundSheets_[j].name_.wname_) == 0) return false;
- }
- workbook_.boundSheets_[i].name_ = to;
- return true;
- }
- }
- return false;
- }
- size_t BasicExcel::Read(const char* data, size_t dataSize)
- {
- size_t bytesRead = 0;
- BOF bof;
- short code;
- LittleEndian::Read(data, code, 0, 2);
- Record rec;
- while (code == CODE::BOF)
- {
- bof.Read(data+bytesRead);
- switch (bof.type_)
- {
- case 0x0005:
- bytesRead += workbook_.Read(data+bytesRead);
- break;
-
- case 0x0006:
- bytesRead += rec.Read(data+bytesRead);
- break;
-
- case 0x0010:
- worksheets_.push_back(Worksheet());
- bytesRead += worksheets_.back().Read(data+bytesRead);
- break;
-
- case 0x0020:
- bytesRead += rec.Read(data+bytesRead);
- break;
- default:
- bytesRead += rec.Read(data+bytesRead);
- break;
- }
- if (bytesRead < dataSize) LittleEndian::Read(data, code, bytesRead, 2);
- else break;
- }
- return bytesRead;
- }
- size_t BasicExcel::Write(char* data)
- {
- size_t bytesWritten = 0;
- bytesWritten += workbook_.Write(data+bytesWritten);
-
- size_t maxWorkSheets = worksheets_.size();
- for (size_t i=0; i<maxWorkSheets; ++i)
- {
- bytesWritten += worksheets_[i].Write(data+bytesWritten);
- }
- return bytesWritten;
- }
- void BasicExcel::AdjustStreamPositions()
- {
- AdjustBoundSheetBOFPositions();
- AdjustDBCellPositions();
- }
- void BasicExcel::AdjustBoundSheetBOFPositions()
- {
- size_t offset = workbook_.RecordSize();
- size_t maxBoundSheets = workbook_.boundSheets_.size();
- for (size_t i=0; i<maxBoundSheets; ++i)
- {
- workbook_.boundSheets_[i].BOFpos_ = offset;
- offset += worksheets_[i].RecordSize();
- }
- }
- void BasicExcel::AdjustDBCellPositions()
- {
- size_t offset = workbook_.RecordSize();
- size_t maxSheets = worksheets_.size();
- for (size_t i=0; i<maxSheets; ++i)
- {
- offset += worksheets_[i].bof_.RecordSize();
- offset += worksheets_[i].index_.RecordSize();
- offset += worksheets_[i].dimensions_.RecordSize();
-
- size_t maxRowBlocks_ = worksheets_[i].cellTable_.rowBlocks_.size();
- for (size_t j=0; j<maxRowBlocks_; ++j)
- {
- size_t firstRowOffset = 0;
- size_t maxRows = worksheets_[i].cellTable_.rowBlocks_[j].rows_.size();
- {for (size_t k=0; k<maxRows; ++k)
- {
- offset += worksheets_[i].cellTable_.rowBlocks_[j].rows_[k].RecordSize();
- firstRowOffset += worksheets_[i].cellTable_.rowBlocks_[j].rows_[k].RecordSize();
- }}
- size_t cellOffset = firstRowOffset - 20;
- size_t maxCellBlocks = worksheets_[i].cellTable_.rowBlocks_[j].cellBlocks_.size();
- {for (size_t k=0; k<maxCellBlocks; ++k)
- {
- offset += worksheets_[i].cellTable_.rowBlocks_[j].cellBlocks_[k].RecordSize();
- firstRowOffset += worksheets_[i].cellTable_.rowBlocks_[j].cellBlocks_[k].RecordSize();
- }}
-
- worksheets_[i].index_.DBCellPos_[j] = offset;
-
- offset += worksheets_[i].cellTable_.rowBlocks_[j].dbcell_.RecordSize();
-
- worksheets_[i].cellTable_.rowBlocks_[j].dbcell_.firstRowOffset_ = firstRowOffset;
-
- size_t l=0;
- {for (size_t k=0; k<maxRows; ++k)
- {
- for (; l<maxCellBlocks; ++l)
- {
- if (worksheets_[i].cellTable_.rowBlocks_[j].rows_[k].rowIndex_ <=
- worksheets_[i].cellTable_.rowBlocks_[j].cellBlocks_[l].RowIndex())
- {
- worksheets_[i].cellTable_.rowBlocks_[j].dbcell_.offsets_[k] = cellOffset;
- break;
- }
- cellOffset += worksheets_[i].cellTable_.rowBlocks_[j].cellBlocks_[l].RecordSize();
- }
- cellOffset = 0;
- }}
- }
-
- offset += worksheets_[i].cellTable_.RecordSize();
- offset += worksheets_[i].window2_.RecordSize();
- offset += worksheets_[i].eof_.RecordSize();
- }
- }
- void BasicExcel::AdjustExtSSTPositions()
- {
- size_t offset = workbook_.bof_.RecordSize();
- offset += workbook_.bof_.RecordSize();
- offset += workbook_.window1_.RecordSize();
- size_t maxFonts = workbook_.fonts_.size();
- {for (size_t i=0; i<maxFonts; ++i) {offset += workbook_.fonts_[i].RecordSize();}}
-
- size_t maxXFs = workbook_.XFs_.size();
- {for (size_t i=0; i<maxXFs; ++i) {offset += workbook_.XFs_[i].RecordSize();}}
- size_t maxStyles = workbook_.styles_.size();
- {for (size_t i=0; i<maxStyles; ++i) {offset += workbook_.styles_[i].RecordSize();}}
- size_t maxBoundSheets = workbook_.boundSheets_.size();
- {for (size_t i=0; i<maxBoundSheets; ++i) {offset += workbook_.boundSheets_[i].RecordSize();}}
- workbook_.extSST_.stringsTotal_ = 10;
- size_t maxPortions = workbook_.sst_.uniqueStringsTotal_ / workbook_.extSST_.stringsTotal_ +
- (workbook_.sst_.uniqueStringsTotal_%workbook_.extSST_.stringsTotal_ ? 1 : 0);
- workbook_.extSST_.streamPos_.resize(maxPortions);
- workbook_.extSST_.firstStringPos_.resize(maxPortions);
- workbook_.extSST_.unused_.resize(maxPortions);
- size_t relativeOffset = 8;
- for (size_t i=0; i<maxPortions; ++i)
- {
- workbook_.extSST_.streamPos_[i] = offset + 4 + relativeOffset;
- workbook_.extSST_.firstStringPos_[i] = 4 + relativeOffset;
- workbook_.extSST_.unused_[i] = 0;
- for (size_t j=0; j<workbook_.extSST_.stringsTotal_; ++j)
- {
- if (i*workbook_.extSST_.stringsTotal_+j >= workbook_.sst_.strings_.size()) break;
- size_t stringSize = workbook_.sst_.strings_[i*workbook_.extSST_.stringsTotal_+j].StringSize();
- if (relativeOffset+stringSize+3 < 8224)
- {
- relativeOffset += stringSize + 3;
- }
- else
- {
-
-
- if (8224 - relativeOffset >= 12)
- {
- stringSize -= (8224 - relativeOffset - 3);
- offset += 12 + relativeOffset;
- relativeOffset = 0;
- size_t additionalContinueRecords = stringSize / 8223;
- for (size_t k=0; k<additionalContinueRecords; ++k)
- {
- stringSize -= 8223;
- }
- relativeOffset += stringSize + 1;
- }
- else
- {
- if (relativeOffset+stringSize+3 < 8224)
- {
- relativeOffset += stringSize + 3;
- }
- else
- {
-
-
- if (8224 - relativeOffset >= 12)
- {
- stringSize -= (8224 - relativeOffset - 3);
- offset += 12 + relativeOffset;
- relativeOffset = 0;
- size_t additionalContinueRecords = stringSize / 8223;
- for (size_t k=0; k<additionalContinueRecords; ++k)
- {
- stringSize -= 8223;
- }
- relativeOffset += stringSize + 1;
- }
- }
- }
- }
- }
- }
- }
- void BasicExcel::UpdateYExcelWorksheet()
- {
- size_t maxWorksheets = worksheets_.size();
- yesheets_.clear();
- yesheets_.reserve(maxWorksheets);
- for (size_t i=0; i<maxWorksheets; ++i)
- {
- yesheets_.push_back(BasicExcelWorksheet(this,i));
- }
- }
- void BasicExcel::UpdateWorksheets()
- {
-
- const size_t maxWorksheets = yesheets_.size();
- Worksheet::CellTable::RowBlock rowBlock;
- Worksheet::CellTable::RowBlock::CellBlock cellBlock;
- Worksheet::CellTable::RowBlock::Row row;
- Worksheet::CellTable::RowBlock::CellBlock::MulRK::XFRK xfrk;
- LargeString largeString;
- map<vector<char>, size_t> stringMap;
- map<vector<char>, size_t>::iterator stringMapIt;
- map<vector<wchar_t>, size_t> wstringMap;
- map<vector<wchar_t>, size_t>::iterator wstringMapIt;
-
- worksheets_.clear();
- worksheets_.resize(maxWorksheets);
-
- workbook_.sst_.stringsTotal_ = 0;
- workbook_.sst_.uniqueStringsTotal_ = 0;
- workbook_.sst_.strings_.clear();
- for (size_t s=0; s<maxWorksheets; ++s)
- {
- size_t maxRows = yesheets_[s].GetTotalRows();
- size_t maxCols = yesheets_[s].GetTotalCols();
-
- worksheets_[s].index_.firstUsedRowIndex_ = 100000;
- worksheets_[s].index_.firstUnusedRowIndex_ = maxRows;
-
- worksheets_[s].dimensions_.firstUsedRowIndex_ = 100000;
- worksheets_[s].dimensions_.firstUsedColIndex_ = 1000;
- worksheets_[s].dimensions_.lastUsedRowIndexPlusOne_ = maxRows;
- worksheets_[s].dimensions_.lastUsedColIndexPlusOne_ = maxCols;
-
- if (s > 0) worksheets_[s].window2_.options_ &= ~0x200;
-
- vector<Worksheet::CellTable::RowBlock>& rRowBlocks = worksheets_[s].cellTable_.rowBlocks_;
- vector<Worksheet::CellTable::RowBlock::CellBlock>* pCellBlocks;
- Worksheet::CellTable::RowBlock::CellBlock* pCell;
- rRowBlocks.resize(maxRows/32 + (maxRows%32 ? 1 : 0));
- for (size_t r=0, curRowBlock=0; r<maxRows; ++r)
- {
- if (r%32==0)
- {
-
- pCellBlocks = &(rRowBlocks[curRowBlock++].cellBlocks_);
- }
- bool newRow = true;
- pCellBlocks->reserve(1000);
- for (size_t c=0; c<maxCols; ++c)
- {
- BasicExcelCell* cell = yesheets_[s].Cell(r,c);
- int cellType = cell->Type();
- if (cellType != BasicExcelCell::UNDEFINED)
- {
- if (worksheets_[s].index_.firstUsedRowIndex_ == 100000)
- {
-
- worksheets_[s].index_.firstUsedRowIndex_ = r;
- worksheets_[s].dimensions_.firstUsedRowIndex_ = r;
-
- size_t nm = int(worksheets_[s].index_.firstUnusedRowIndex_ - worksheets_[s].index_.firstUsedRowIndex_ - 1) / 32 + 1;
- worksheets_[s].index_.DBCellPos_.resize(nm);
- }
- if (worksheets_[s].dimensions_.firstUsedColIndex_ == 1000)
- {
-
- worksheets_[s].dimensions_.firstUsedColIndex_ = c;
- }
- if (newRow)
- {
-
- Worksheet::CellTable::RowBlock& rRowBlock = rRowBlocks[curRowBlock-1];
- rRowBlock.rows_.push_back(row);
- rRowBlock.rows_.back().rowIndex_ = r;
- rRowBlock.rows_.back().lastCellColIndexPlusOne_ = maxCols;
- rRowBlock.dbcell_.offsets_.push_back(0);
- newRow = false;
- }
-
- pCellBlocks->push_back(cellBlock);
- if (pCellBlocks->size()%1000==0) pCellBlocks->reserve(pCellBlocks->size()+1000);
- pCell = &(pCellBlocks->back());
-
- switch(cellType)
- {
- case BasicExcelCell::INT:
- {
-
- size_t cl = c + 1;
- for (; cl<maxCols; ++cl)
- {
- BasicExcelCell* cellNext = yesheets_[s].Cell(r,cl);
- if (cellNext->Type()==BasicExcelCell::UNDEFINED ||
- cellNext->Type()!=cell->Type()) break;
- }
- if (cl > c+1)
- {
-
- pCell->type_ = CODE::MULRK;
- pCell->normalType_ = true;
- pCell->mulrk_.rowIndex_ = r;
- pCell->mulrk_.firstColIndex_ = c;
- pCell->mulrk_.lastColIndex_ = cl - 1;
- pCell->mulrk_.XFRK_.resize(cl-c);
- for (size_t i=0; c<cl; ++c, ++i)
- {
- cell = yesheets_[s].Cell(r,c);
- pCell->mulrk_.XFRK_[i].RKValue_ = GetRKValueFromInteger(cell->GetInteger());
- }
- --c;
- }
- else
- {
-
- pCell->normalType_ = true;
- pCell->type_ = CODE::RK;
- pCell->rk_.rowIndex_ = r;
- pCell->rk_.colIndex_ = c;
- pCell->rk_.value_ = GetRKValueFromInteger(cell->GetInteger());
- }
- break;
- }
- case BasicExcelCell::DOUBLE:
- {
-
-
- bool canStoreAsRKValue = CanStoreAsRKValue(cell->GetDouble());
- size_t cl = c + 1;
- for (; cl<maxCols; ++cl)
- {
- BasicExcelCell* cellNext = yesheets_[s].Cell(r,cl);
- if (cellNext->Type()==BasicExcelCell::UNDEFINED ||
- cellNext->Type()!=cell->Type() ||
- canStoreAsRKValue!=CanStoreAsRKValue(cellNext->GetDouble())) break;
- }
- if (cl > c+1 && canStoreAsRKValue)
- {
-
- pCell->type_ = CODE::MULRK;
- pCell->normalType_ = true;
- pCell->mulrk_.rowIndex_ = r;
- pCell->mulrk_.firstColIndex_ = c;
- pCell->mulrk_.lastColIndex_ = cl - 1;
- pCell->mulrk_.XFRK_.resize(cl-c);
- for (size_t i=0; c<cl; ++c, ++i)
- {
- cell = yesheets_[s].Cell(r,c);
- pCell->mulrk_.XFRK_[i].RKValue_ = GetRKValueFromDouble(cell->GetDouble());
- }
- --c;
- }
- else
- {
-
- pCell->normalType_ = true;
- if (canStoreAsRKValue)
- {
- pCell->type_ = CODE::RK;
- pCell->rk_.rowIndex_ = r;
- pCell->rk_.colIndex_ = c;
- pCell->rk_.value_ = GetRKValueFromDouble(cell->GetDouble());
- }
- else
- {
- pCell->type_ = CODE::NUMBER;
- pCell->number_.rowIndex_ = r;
- pCell->number_.colIndex_ = c;
- pCell->number_.value_ = cell->GetDouble();
- }
- }
- break;
- }
- case BasicExcelCell::STRING:
- {
-
- pCell->type_ = CODE::LABELSST;
- pCell->normalType_ = true;
- pCell->labelsst_.rowIndex_ = r;
- pCell->labelsst_.colIndex_ = c;
-
-
- vector<char> str(cell->GetStringLength()+1);
- cell->Get(&*(str.begin()));
- str.pop_back();
-
- ++workbook_.sst_.stringsTotal_;
- size_t maxUniqueStrings = workbook_.sst_.uniqueStringsTotal_;
- size_t strIndex = 0;
- stringMapIt = stringMap.find(str);
- if (stringMapIt != stringMap.end()) strIndex = stringMapIt->second;
- else strIndex = maxUniqueStrings;
- if (strIndex < maxUniqueStrings)
- {
-
- pCell->labelsst_.SSTRecordIndex_ = strIndex;
- }
- else
- {
-
- stringMap[str] = maxUniqueStrings;
- workbook_.sst_.strings_.push_back(largeString);
- workbook_.sst_.strings_[maxUniqueStrings].name_ = str;
- workbook_.sst_.strings_[maxUniqueStrings].unicode_ = 0;
- pCell->labelsst_.SSTRecordIndex_ = maxUniqueStrings;
- ++workbook_.sst_.uniqueStringsTotal_;
- }
- break;
- }
- case BasicExcelCell::WSTRING:
- {
-
- pCell->type_ = CODE::LABELSST;
- pCell->normalType_ = true;
- pCell->labelsst_.rowIndex_ = r;
- pCell->labelsst_.colIndex_ = c;
-
- vector<wchar_t> str(cell->GetStringLength()+1);
- cell->Get(&*(str.begin()));
- str.pop_back();
-
- ++workbook_.sst_.stringsTotal_;
- size_t maxUniqueStrings = workbook_.sst_.strings_.size();
- size_t strIndex = 0;
- wstringMapIt = wstringMap.find(str);
- if (wstringMapIt != wstringMap.end()) strIndex = wstringMapIt->second;
- else strIndex = maxUniqueStrings;
- if (strIndex < maxUniqueStrings)
- {
-
- pCell->labelsst_.SSTRecordIndex_ = strIndex;
- }
- else
- {
-
- wstringMap[str] = maxUniqueStrings;
- workbook_.sst_.strings_.push_back(largeString);
- workbook_.sst_.strings_[maxUniqueStrings].wname_ = str;
- workbook_.sst_.strings_[maxUniqueStrings].unicode_ = 1;
- pCell->labelsst_.SSTRecordIndex_ = maxUniqueStrings;
- ++workbook_.sst_.uniqueStringsTotal_;
- }
- break;
- }
- }
- }
- }
- }
-
- if (worksheets_[s].index_.firstUsedRowIndex_ == 100000)
- {
-
- worksheets_[s].index_.firstUsedRowIndex_ = 0;
- worksheets_[s].dimensions_.firstUsedRowIndex_ = 0;
-
- size_t nm = int(worksheets_[s].index_.firstUnusedRowIndex_ - worksheets_[s].index_.firstUsedRowIndex_ - 1) / 32 + 1;
- worksheets_[s].index_.DBCellPos_.resize(nm);
- }
- if (worksheets_[s].dimensions_.firstUsedColIndex_ == 1000)
- {
-
- worksheets_[s].dimensions_.firstUsedColIndex_ = 0;
- }
- }
- }
- BasicExcelWorksheet::BasicExcelWorksheet(BasicExcel* excel, size_t sheetIndex) :
- excel_(excel), sheetIndex_(sheetIndex)
- {
- UpdateCells();
- }
- char* BasicExcelWorksheet::GetAnsiSheetName()
- {
- if (!(excel_->workbook_.boundSheets_[sheetIndex_].name_.unicode_ & 1))
- {
- return excel_->workbook_.boundSheets_[sheetIndex_].name_.name_;
- }
- else return 0;
- }
- wchar_t* BasicExcelWorksheet::GetUnicodeSheetName()
- {
- if (excel_->workbook_.boundSheets_[sheetIndex_].name_.unicode_ & 1)
- {
- return excel_->workbook_.boundSheets_[sheetIndex_].name_.wname_;
- }
- else return 0;
- }
- bool BasicExcelWorksheet::GetSheetName(char* name)
- {
- if (!(excel_->workbook_.boundSheets_[sheetIndex_].name_.unicode_ & 1))
- {
- strcpy(name, excel_->workbook_.boundSheets_[sheetIndex_].name_.name_);
- return true;
- }
- else return false;
- }
- bool BasicExcelWorksheet::GetSheetName(wchar_t* name)
- {
- if (excel_->workbook_.boundSheets_[sheetIndex_].name_.unicode_ & 1)
- {
- wcscpy(name, excel_->workbook_.boundSheets_[sheetIndex_].name_.wname_);
- return true;
- }
- else return false;
- }
- bool BasicExcelWorksheet::Rename(const char* to)
- {
- size_t maxWorksheets = excel_->workbook_.boundSheets_.size();
- for (size_t i=0; i<maxWorksheets; ++i)
- {
- if (excel_->workbook_.boundSheets_[i].name_.unicode_ & 1) continue;
- if (strcmp(to, excel_->workbook_.boundSheets_[i].name_.name_) == 0) return false;
- }
- excel_->workbook_.boundSheets_[sheetIndex_].name_ = to;
- return true;
- }
- bool BasicExcelWorksheet::Rename(const wchar_t* to)
- {
- size_t maxWorksheets = excel_->workbook_.boundSheets_.size();
- for (size_t i=0; i<maxWorksheets; ++i)
- {
- if (!(excel_->workbook_.boundSheets_[i].name_.unicode_ & 1)) continue;
- if (wcscmp(to, excel_->workbook_.boundSheets_[i].name_.wname_) == 0) return false;
- }
- excel_->workbook_.boundSheets_[sheetIndex_].name_ = to;
- return true;
- }
- void BasicExcelWorksheet::Print(ostream& os, char delimiter, char textQualifier)
- {
- for (size_t r=0; r<maxRows_; ++r)
- {
- for (size_t c=0; c<maxCols_; ++c)
- {
- BasicExcelCell* cell = Cell(r,c);
- switch (cell->Type())
- {
- case BasicExcelCell::UNDEFINED:
- break;
- case BasicExcelCell::INT:
- os << cell->GetInteger();
- break;
- case BasicExcelCell::DOUBLE:
- os << setprecision(15) << cell->GetDouble();
- break;
- case BasicExcelCell::STRING:
- {
- if (textQualifier != '\0')
- {
-
- size_t maxLength = cell->GetStringLength();
- vector<char> cellString(maxLength+1);
- cell->Get(&*(cellString.begin()));
-
- vector<char>::iterator it;
- size_t npos = 0;
- while ((it=find(cellString.begin()+npos, cellString.end(), textQualifier)) != cellString.end())
- {
- npos = distance(cellString.begin(), cellString.insert(it, textQualifier)) + 2;
- }
-
- os << textQualifier << &*(cellString.begin()) << textQualifier;
- }
- else os << cell->GetString();
- break;
- }
- case BasicExcelCell::WSTRING:
- {
-
-
- break;
- }
- }
- if (c < maxCols_-1) os << delimiter;
- }
- os << endl;
- }
- }
- size_t BasicExcelWorksheet::GetTotalRows()
- {
- return maxRows_;
- }
- size_t BasicExcelWorksheet::GetTotalCols()
- {
- return maxCols_;
- }
- BasicExcelCell* BasicExcelWorksheet::Cell(size_t row, size_t col)
- {
-
- if (row>65535 || col>255) return 0;
-
- if (col>=maxCols_)
- {
-
- maxCols_ = col + 1;
- for (size_t i=0; i<maxRows_; ++i) cells_[i].resize(maxCols_);
- }
- if (row>=maxRows_)
- {
-
- maxRows_ = row + 1;
- cells_.resize(maxRows_, vector<BasicExcelCell>(maxCols_));
- }
- return &(cells_[row][col]);
- }
- bool BasicExcelWorksheet::EraseCell(size_t row, size_t col)
- {
- if (row<maxRows_ && col<maxCols_)
- {
- cells_[row][col].EraseContents();
- return true;
- }
- else return false;
- }
- void BasicExcelWorksheet::UpdateCells()
- {
-
- Worksheet::Dimensions& dimension = excel_->worksheets_[sheetIndex_].dimensions_;
- vector<Worksheet::CellTable::RowBlock>& rRowBlocks = excel_->worksheets_[sheetIndex_].cellTable_.rowBlocks_;
- vector<wchar_t> wstr;
- vector<char> str;
- maxRows_ = dimension.lastUsedRowIndexPlusOne_;
- maxCols_ = dimension.lastUsedColIndexPlusOne_;
-
- vector<BasicExcelCell> cellCol(maxCols_);
- cells_.resize(maxRows_, cellCol);
- size_t maxRowBlocks = rRowBlocks.size();
- for (size_t i=0; i<maxRowBlocks; ++i)
- {
- vector<Worksheet::CellTable::RowBlock::CellBlock>& rCellBlocks = rRowBlocks[i].cellBlocks_;
- size_t maxCells = rCellBlocks.size();
- for (size_t j=0; j<maxCells; ++j)
- {
- size_t row = rCellBlocks[j].RowIndex();
- size_t col = rCellBlocks[j].ColIndex();
- switch (rCellBlocks[j].type_)
- {
- case CODE::BLANK:
- break;
-
- case CODE::BOOLERR:
- if (rCellBlocks[j].boolerr_.error_ == 0)
- {
- cells_[row][col].Set(rCellBlocks[j].boolerr_.code_);
- }
- break;
-
- case CODE::LABELSST:
- {
- vector<LargeString>& ss = excel_->workbook_.sst_.strings_;
- if (ss[rCellBlocks[j].labelsst_.SSTRecordIndex_].unicode_ & 1)
- {
- wstr = ss[rCellBlocks[j].labelsst_.SSTRecordIndex_].wname_;
- wstr.resize(wstr.size()+1);
- wstr.back() = L'\0';
- cells_[row][col].Set(&*(wstr.begin()));
- }
- else
- {
- str = ss[rCellBlocks[j].labelsst_.SSTRecordIndex_].name_;
- str.resize(str.size()+1);
- str.back() = '\0';
- cells_[row][col].Set(&*(str.begin()));
- }
- break;
- }
- case CODE::MULBLANK:
- break;
-
- case CODE::MULRK:
- {
- size_t maxCols = rCellBlocks[j].mulrk_.lastColIndex_ - rCellBlocks[j].mulrk_.firstColIndex_ + 1;
- for (size_t k=0; k<maxCols; ++k, ++col)
- {
-
- int rkValue = rCellBlocks[j].mulrk_.XFRK_[k].RKValue_;
- if (IsRKValueAnInteger(rkValue))
- {
- cells_[row][col].Set(GetIntegerFromRKValue(rkValue));
- }
- else
- {
- cells_[row][col].Set(GetDoubleFromRKValue(rkValue));
- }
- }
- break;
- }
- case CODE::NUMBER:
- cells_[row][col].Set(rCellBlocks[j].number_.value_);
- break;
- case CODE::RK:
- {
- int rkValue = rCellBlocks[j].rk_.value_;
- if (IsRKValueAnInteger(rkValue))
- {
- cells_[row][col].Set(GetIntegerFromRKValue(rkValue));
- }
- else
- {
- cells_[row][col].Set(GetDoubleFromRKValue(rkValue));
- }
- break;
- }
- case CODE::FORMULA:
- break;
- }
- }
- }
- }
- BasicExcelCell::BasicExcelCell() : type_(UNDEFINED) {};
- int BasicExcelCell::Type() const {return type_;}
- bool BasicExcelCell::Get(int& val) const
- {
- if (type_ == INT)
- {
- val = ival_;
- return true;
- }
- else if (type_ == DOUBLE)
- {
- val = (int)dval_;
- return true;
- }
- else return false;
- }
- bool BasicExcelCell::Get(double& val) const
- {
- if (type_ == DOUBLE)
- {
- val = dval_;
- return true;
- }
- else if (type_ == INT)
- {
- val = (double)ival_;
- return true;
- }
- else return false;
- }
- bool BasicExcelCell::Get(char* str) const
- {
- if (type_ == STRING)
- {
- if (str_.empty()) *str = '\0';
- else strcpy(str, &*(str_.begin()));
- return true;
- }
- else return false;
- }
- bool BasicExcelCell::Get(wchar_t* str) const
- {
- if (type_ == WSTRING)
- {
- if (wstr_.empty()) *str = L'\0';
- else wcscpy(str, &*(wstr_.begin()));
- return true;
- }
- else return false;
- }
- size_t BasicExcelCell::GetStringLength() const
- {
- if (type_ == STRING) return str_.size() - 1;
- else return wstr_.size() - 1;
- }
- int BasicExcelCell::GetInteger() const
- {
- int val;
- if (Get(val)) return val;
- else return 0;
- }
- double BasicExcelCell::GetDouble() const
- {
- double val;
- if (Get(val)) return val;
- else return 0.0;
- }
- const char* BasicExcelCell::GetString() const
- {
- vector<char> str(str_.size());
- if (!str.empty() && Get(&*(str.begin()))) return &*(str_.begin());
- else return 0;
- }
- const wchar_t* BasicExcelCell::GetWString() const
- {
- vector<wchar_t> wstr(wstr_.size());
- if (!wstr.empty() && Get(&*(wstr.begin()))) return &*(wstr_.begin());
- else return 0;
- }
- void BasicExcelCell::Set(int val)
- {
- SetInteger(val);
- }
- void BasicExcelCell::Set(double val)
- {
- SetDouble(val);
- }
- void BasicExcelCell::Set(const char* str)
- {
- SetString(str);
- }
- void BasicExcelCell::Set(const wchar_t* str)
- {
- SetWString(str);
- }
- void BasicExcelCell::SetInteger(int val)
- {
- type_ = INT;
- ival_ = val;
- }
- void BasicExcelCell::SetDouble(double val)
- {
- type_ = DOUBLE;
- dval_ = val;
- }
- void BasicExcelCell::SetString(const char* str)
- {
- size_t length = strlen(str);
- if (length > 0)
- {
- type_ = STRING;
- str_ = vector<char>(length+1);
- strcpy(&*(str_.begin()), str);
- wstr_.clear();
- }
- else EraseContents();
- }
- void BasicExcelCell::SetWString(const wchar_t* str)
- {
- size_t length = wcslen(str);
- if (length > 0)
- {
- type_ = WSTRING;
- wstr_ = vector<wchar_t>(length+1);
- wcscpy(&*(wstr_.begin()), str);
- str_.clear();
- }
- else EraseContents();
- }
- void BasicExcelCell::EraseContents()
- {
- type_ = UNDEFINED;
- str_.clear();
- wstr_.clear();
- }
- ostream& operator<<(ostream& os, const BasicExcelCell& cell)
- {
- switch (cell.Type())
- {
- case BasicExcelCell::UNDEFINED:
- os << '\0';
- break;
- case BasicExcelCell::INT:
- os << cell.GetInteger();
- break;
- case BasicExcelCell::DOUBLE:
- os << cell.GetDouble();
- break;
- case BasicExcelCell::STRING:
- os << cell.GetString();
- break;
- case BasicExcelCell::WSTRING:
- os << cell.GetWString();
- break;
- }
- return os;
- }
- }
|