123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135 |
- #define IN_LIBXML
- #include "libxml.h"
- #include <string.h>
- #ifdef HAVE_SYS_TYPES_H
- #include <sys/types.h>
- #endif
- #ifdef HAVE_MATH_H
- #include <math.h>
- #endif
- #ifdef HAVE_FLOAT_H
- #include <float.h>
- #endif
- #ifdef HAVE_CTYPE_H
- #include <ctype.h>
- #endif
- #ifdef HAVE_SIGNAL_H
- #include <signal.h>
- #endif
- #include <libxml/xmlmemory.h>
- #include <libxml/tree.h>
- #include <libxml/valid.h>
- #include <libxml/xpath.h>
- #include <libxml/xpathInternals.h>
- #include <libxml/parserInternals.h>
- #include <libxml/hash.h>
- #ifdef LIBXML_XPTR_ENABLED
- #include <libxml/xpointer.h>
- #endif
- #ifdef LIBXML_DEBUG_ENABLED
- #include <libxml/debugXML.h>
- #endif
- #include <libxml/xmlerror.h>
- #include <libxml/threads.h>
- #include <libxml/globals.h>
- #ifdef LIBXML_PATTERN_ENABLED
- #include <libxml/pattern.h>
- #endif
- #ifdef LIBXML_PATTERN_ENABLED
- #define XPATH_STREAMING
- #endif
- #define TODO \
- xmlGenericError(xmlGenericErrorContext, \
- "Unimplemented block at %s:%d\n", \
- __FILE__, __LINE__);
- #define XP_OPTIMIZED_NON_ELEM_COMPARISON
- #define XP_OPTIMIZED_FILTER_FIRST
- #if defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
- #ifndef TRIO_REPLACE_STDIO
- #define TRIO_PUBLIC static
- #endif
- #include "trionan.c"
- double xmlXPathNAN = 0;
- double xmlXPathPINF = 1;
- double xmlXPathNINF = -1;
- static double xmlXPathNZERO = 0;
- static int xmlXPathInitialized = 0;
- void
- xmlXPathInit(void) {
- if (xmlXPathInitialized) return;
- xmlXPathPINF = trio_pinf();
- xmlXPathNINF = trio_ninf();
- xmlXPathNAN = trio_nan();
- xmlXPathNZERO = trio_nzero();
- xmlXPathInitialized = 1;
- }
- int
- xmlXPathIsNaN(double val) {
- return(trio_isnan(val));
- }
- int
- xmlXPathIsInf(double val) {
- return(trio_isinf(val));
- }
- #endif
- #ifdef LIBXML_XPATH_ENABLED
- static int
- xmlXPathGetSign(double val) {
- return(trio_signbit(val));
- }
- #ifdef DEBUG_XPATH_EXPRESSION
- #define DEBUG_STEP
- #define DEBUG_EXPR
- #define DEBUG_EVAL_COUNTS
- #endif
- static xmlNs xmlXPathXMLNamespaceStruct = {
- NULL,
- XML_NAMESPACE_DECL,
- XML_XML_NAMESPACE,
- BAD_CAST "xml",
- NULL,
- NULL
- };
- static xmlNsPtr xmlXPathXMLNamespace = &xmlXPathXMLNamespaceStruct;
- #ifndef LIBXML_THREAD_ENABLED
- static int xmlXPathDisableOptimizer = 0;
- #endif
- #define XP_ERRORNULL(X) \
- { xmlXPathErr(ctxt, X); return(NULL); }
- static const char *xmlXPathErrorMessages[] = {
- "Ok\n",
- "Number encoding\n",
- "Unfinished literal\n",
- "Start of literal\n",
- "Expected $ for variable reference\n",
- "Undefined variable\n",
- "Invalid predicate\n",
- "Invalid expression\n",
- "Missing closing curly brace\n",
- "Unregistered function\n",
- "Invalid operand\n",
- "Invalid type\n",
- "Invalid number of arguments\n",
- "Invalid context size\n",
- "Invalid context position\n",
- "Memory allocation error\n",
- "Syntax error\n",
- "Resource error\n",
- "Sub resource error\n",
- "Undefined namespace prefix\n",
- "Encoding error\n",
- "Char out of XML range\n",
- "Invalid or incomplete context\n",
- "?? Unknown error ??\n"
- };
- #define MAXERRNO ((int)(sizeof(xmlXPathErrorMessages) / \
- sizeof(xmlXPathErrorMessages[0])) - 1)
- static void
- xmlXPathErrMemory(xmlXPathContextPtr ctxt, const char *extra)
- {
- if (ctxt != NULL) {
- if (extra) {
- xmlChar buf[200];
- xmlStrPrintf(buf, 200,
- BAD_CAST "Memory allocation failed : %s\n",
- extra);
- ctxt->lastError.message = (char *) xmlStrdup(buf);
- } else {
- ctxt->lastError.message = (char *)
- xmlStrdup(BAD_CAST "Memory allocation failed\n");
- }
- ctxt->lastError.domain = XML_FROM_XPATH;
- ctxt->lastError.code = XML_ERR_NO_MEMORY;
- if (ctxt->error != NULL)
- ctxt->error(ctxt->userData, &ctxt->lastError);
- } else {
- if (extra)
- __xmlRaiseError(NULL, NULL, NULL,
- NULL, NULL, XML_FROM_XPATH,
- XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0,
- extra, NULL, NULL, 0, 0,
- "Memory allocation failed : %s\n", extra);
- else
- __xmlRaiseError(NULL, NULL, NULL,
- NULL, NULL, XML_FROM_XPATH,
- XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0,
- NULL, NULL, NULL, 0, 0,
- "Memory allocation failed\n");
- }
- }
- static void
- xmlXPathPErrMemory(xmlXPathParserContextPtr ctxt, const char *extra)
- {
- if (ctxt == NULL)
- xmlXPathErrMemory(NULL, extra);
- else {
- ctxt->error = XPATH_MEMORY_ERROR;
- xmlXPathErrMemory(ctxt->context, extra);
- }
- }
- void
- xmlXPathErr(xmlXPathParserContextPtr ctxt, int error)
- {
- if ((error < 0) || (error > MAXERRNO))
- error = MAXERRNO;
- if (ctxt == NULL) {
- __xmlRaiseError(NULL, NULL, NULL,
- NULL, NULL, XML_FROM_XPATH,
- error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK,
- XML_ERR_ERROR, NULL, 0,
- NULL, NULL, NULL, 0, 0,
- "%s", xmlXPathErrorMessages[error]);
- return;
- }
- ctxt->error = error;
- if (ctxt->context == NULL) {
- __xmlRaiseError(NULL, NULL, NULL,
- NULL, NULL, XML_FROM_XPATH,
- error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK,
- XML_ERR_ERROR, NULL, 0,
- (const char *) ctxt->base, NULL, NULL,
- ctxt->cur - ctxt->base, 0,
- "%s", xmlXPathErrorMessages[error]);
- return;
- }
-
- xmlResetError(&ctxt->context->lastError);
- ctxt->context->lastError.domain = XML_FROM_XPATH;
- ctxt->context->lastError.code = error + XML_XPATH_EXPRESSION_OK -
- XPATH_EXPRESSION_OK;
- ctxt->context->lastError.level = XML_ERR_ERROR;
- ctxt->context->lastError.str1 = (char *) xmlStrdup(ctxt->base);
- ctxt->context->lastError.int1 = ctxt->cur - ctxt->base;
- ctxt->context->lastError.node = ctxt->context->debugNode;
- if (ctxt->context->error != NULL) {
- ctxt->context->error(ctxt->context->userData,
- &ctxt->context->lastError);
- } else {
- __xmlRaiseError(NULL, NULL, NULL,
- NULL, ctxt->context->debugNode, XML_FROM_XPATH,
- error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK,
- XML_ERR_ERROR, NULL, 0,
- (const char *) ctxt->base, NULL, NULL,
- ctxt->cur - ctxt->base, 0,
- "%s", xmlXPathErrorMessages[error]);
- }
- }
- void
- xmlXPatherror(xmlXPathParserContextPtr ctxt, const char *file ATTRIBUTE_UNUSED,
- int line ATTRIBUTE_UNUSED, int no) {
- xmlXPathErr(ctxt, no);
- }
- typedef struct _xmlPointerList xmlPointerList;
- typedef xmlPointerList *xmlPointerListPtr;
- struct _xmlPointerList {
- void **items;
- int number;
- int size;
- };
- static int
- xmlPointerListAddSize(xmlPointerListPtr list,
- void *item,
- int initialSize)
- {
- if (list->items == NULL) {
- if (initialSize <= 0)
- initialSize = 1;
- list->items = (void **) xmlMalloc(
- initialSize * sizeof(void *));
- if (list->items == NULL) {
- xmlXPathErrMemory(NULL,
- "xmlPointerListCreate: allocating item\n");
- return(-1);
- }
- list->number = 0;
- list->size = initialSize;
- } else if (list->size <= list->number) {
- list->size *= 2;
- list->items = (void **) xmlRealloc(list->items,
- list->size * sizeof(void *));
- if (list->items == NULL) {
- xmlXPathErrMemory(NULL,
- "xmlPointerListCreate: re-allocating item\n");
- list->size = 0;
- return(-1);
- }
- }
- list->items[list->number++] = item;
- return(0);
- }
- static xmlPointerListPtr
- xmlPointerListCreate(int initialSize)
- {
- xmlPointerListPtr ret;
- ret = xmlMalloc(sizeof(xmlPointerList));
- if (ret == NULL) {
- xmlXPathErrMemory(NULL,
- "xmlPointerListCreate: allocating item\n");
- return (NULL);
- }
- memset(ret, 0, sizeof(xmlPointerList));
- if (initialSize > 0) {
- xmlPointerListAddSize(ret, NULL, initialSize);
- ret->number = 0;
- }
- return (ret);
- }
- static void
- xmlPointerListFree(xmlPointerListPtr list)
- {
- if (list == NULL)
- return;
- if (list->items != NULL)
- xmlFree(list->items);
- xmlFree(list);
- }
- typedef enum {
- XPATH_OP_END=0,
- XPATH_OP_AND,
- XPATH_OP_OR,
- XPATH_OP_EQUAL,
- XPATH_OP_CMP,
- XPATH_OP_PLUS,
- XPATH_OP_MULT,
- XPATH_OP_UNION,
- XPATH_OP_ROOT,
- XPATH_OP_NODE,
- XPATH_OP_RESET,
- XPATH_OP_COLLECT,
- XPATH_OP_VALUE,
- XPATH_OP_VARIABLE,
- XPATH_OP_FUNCTION,
- XPATH_OP_ARG,
- XPATH_OP_PREDICATE,
- XPATH_OP_FILTER,
- XPATH_OP_SORT
- #ifdef LIBXML_XPTR_ENABLED
- ,XPATH_OP_RANGETO
- #endif
- } xmlXPathOp;
- typedef enum {
- AXIS_ANCESTOR = 1,
- AXIS_ANCESTOR_OR_SELF,
- AXIS_ATTRIBUTE,
- AXIS_CHILD,
- AXIS_DESCENDANT,
- AXIS_DESCENDANT_OR_SELF,
- AXIS_FOLLOWING,
- AXIS_FOLLOWING_SIBLING,
- AXIS_NAMESPACE,
- AXIS_PARENT,
- AXIS_PRECEDING,
- AXIS_PRECEDING_SIBLING,
- AXIS_SELF
- } xmlXPathAxisVal;
- typedef enum {
- NODE_TEST_NONE = 0,
- NODE_TEST_TYPE = 1,
- NODE_TEST_PI = 2,
- NODE_TEST_ALL = 3,
- NODE_TEST_NS = 4,
- NODE_TEST_NAME = 5
- } xmlXPathTestVal;
- typedef enum {
- NODE_TYPE_NODE = 0,
- NODE_TYPE_COMMENT = XML_COMMENT_NODE,
- NODE_TYPE_TEXT = XML_TEXT_NODE,
- NODE_TYPE_PI = XML_PI_NODE
- } xmlXPathTypeVal;
- #define XP_REWRITE_DOS_CHILD_ELEM 1
- typedef struct _xmlXPathStepOp xmlXPathStepOp;
- typedef xmlXPathStepOp *xmlXPathStepOpPtr;
- struct _xmlXPathStepOp {
- xmlXPathOp op;
- int ch1;
- int ch2;
- int value;
- int value2;
- int value3;
- void *value4;
- void *value5;
- void *cache;
- void *cacheURI;
- int rewriteType;
- };
- struct _xmlXPathCompExpr {
- int nbStep;
- int maxStep;
- xmlXPathStepOp *steps;
- int last;
- xmlChar *expr;
- xmlDictPtr dict;
- #ifdef DEBUG_EVAL_COUNTS
- int nb;
- xmlChar *string;
- #endif
- #ifdef XPATH_STREAMING
- xmlPatternPtr stream;
- #endif
- };
- static void
- xmlXPathFreeValueTree(xmlNodeSetPtr obj);
- static void
- xmlXPathReleaseObject(xmlXPathContextPtr ctxt, xmlXPathObjectPtr obj);
- static int
- xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt,
- xmlXPathStepOpPtr op, xmlNodePtr *first);
- static int
- xmlXPathCompOpEvalToBoolean(xmlXPathParserContextPtr ctxt,
- xmlXPathStepOpPtr op,
- int isPredicate);
- static xmlXPathCompExprPtr
- xmlXPathNewCompExpr(void) {
- xmlXPathCompExprPtr cur;
- cur = (xmlXPathCompExprPtr) xmlMalloc(sizeof(xmlXPathCompExpr));
- if (cur == NULL) {
- xmlXPathErrMemory(NULL, "allocating component\n");
- return(NULL);
- }
- memset(cur, 0, sizeof(xmlXPathCompExpr));
- cur->maxStep = 10;
- cur->nbStep = 0;
- cur->steps = (xmlXPathStepOp *) xmlMalloc(cur->maxStep *
- sizeof(xmlXPathStepOp));
- if (cur->steps == NULL) {
- xmlXPathErrMemory(NULL, "allocating steps\n");
- xmlFree(cur);
- return(NULL);
- }
- memset(cur->steps, 0, cur->maxStep * sizeof(xmlXPathStepOp));
- cur->last = -1;
- #ifdef DEBUG_EVAL_COUNTS
- cur->nb = 0;
- #endif
- return(cur);
- }
- void
- xmlXPathFreeCompExpr(xmlXPathCompExprPtr comp)
- {
- xmlXPathStepOpPtr op;
- int i;
- if (comp == NULL)
- return;
- if (comp->dict == NULL) {
- for (i = 0; i < comp->nbStep; i++) {
- op = &comp->steps[i];
- if (op->value4 != NULL) {
- if (op->op == XPATH_OP_VALUE)
- xmlXPathFreeObject(op->value4);
- else
- xmlFree(op->value4);
- }
- if (op->value5 != NULL)
- xmlFree(op->value5);
- }
- } else {
- for (i = 0; i < comp->nbStep; i++) {
- op = &comp->steps[i];
- if (op->value4 != NULL) {
- if (op->op == XPATH_OP_VALUE)
- xmlXPathFreeObject(op->value4);
- }
- }
- xmlDictFree(comp->dict);
- }
- if (comp->steps != NULL) {
- xmlFree(comp->steps);
- }
- #ifdef DEBUG_EVAL_COUNTS
- if (comp->string != NULL) {
- xmlFree(comp->string);
- }
- #endif
- #ifdef XPATH_STREAMING
- if (comp->stream != NULL) {
- xmlFreePatternList(comp->stream);
- }
- #endif
- if (comp->expr != NULL) {
- xmlFree(comp->expr);
- }
- xmlFree(comp);
- }
- static int
- xmlXPathCompExprAdd(xmlXPathCompExprPtr comp, int ch1, int ch2,
- xmlXPathOp op, int value,
- int value2, int value3, void *value4, void *value5) {
- if (comp->nbStep >= comp->maxStep) {
- xmlXPathStepOp *real;
- comp->maxStep *= 2;
- real = (xmlXPathStepOp *) xmlRealloc(comp->steps,
- comp->maxStep * sizeof(xmlXPathStepOp));
- if (real == NULL) {
- comp->maxStep /= 2;
- xmlXPathErrMemory(NULL, "adding step\n");
- return(-1);
- }
- comp->steps = real;
- }
- comp->last = comp->nbStep;
- comp->steps[comp->nbStep].rewriteType = 0;
- comp->steps[comp->nbStep].ch1 = ch1;
- comp->steps[comp->nbStep].ch2 = ch2;
- comp->steps[comp->nbStep].op = op;
- comp->steps[comp->nbStep].value = value;
- comp->steps[comp->nbStep].value2 = value2;
- comp->steps[comp->nbStep].value3 = value3;
- if ((comp->dict != NULL) &&
- ((op == XPATH_OP_FUNCTION) || (op == XPATH_OP_VARIABLE) ||
- (op == XPATH_OP_COLLECT))) {
- if (value4 != NULL) {
- comp->steps[comp->nbStep].value4 = (xmlChar *)
- (void *)xmlDictLookup(comp->dict, value4, -1);
- xmlFree(value4);
- } else
- comp->steps[comp->nbStep].value4 = NULL;
- if (value5 != NULL) {
- comp->steps[comp->nbStep].value5 = (xmlChar *)
- (void *)xmlDictLookup(comp->dict, value5, -1);
- xmlFree(value5);
- } else
- comp->steps[comp->nbStep].value5 = NULL;
- } else {
- comp->steps[comp->nbStep].value4 = value4;
- comp->steps[comp->nbStep].value5 = value5;
- }
- comp->steps[comp->nbStep].cache = NULL;
- return(comp->nbStep++);
- }
- static void
- xmlXPathCompSwap(xmlXPathStepOpPtr op) {
- int tmp;
- #ifndef LIBXML_THREAD_ENABLED
-
- if (xmlXPathDisableOptimizer)
- return;
- #endif
- tmp = op->ch1;
- op->ch1 = op->ch2;
- op->ch2 = tmp;
- }
- #define PUSH_FULL_EXPR(op, op1, op2, val, val2, val3, val4, val5) \
- xmlXPathCompExprAdd(ctxt->comp, (op1), (op2), \
- (op), (val), (val2), (val3), (val4), (val5))
- #define PUSH_LONG_EXPR(op, val, val2, val3, val4, val5) \
- xmlXPathCompExprAdd(ctxt->comp, ctxt->comp->last, -1, \
- (op), (val), (val2), (val3), (val4), (val5))
- #define PUSH_LEAVE_EXPR(op, val, val2) \
- xmlXPathCompExprAdd(ctxt->comp, -1, -1, (op), (val), (val2), 0 ,NULL ,NULL)
- #define PUSH_UNARY_EXPR(op, ch, val, val2) \
- xmlXPathCompExprAdd(ctxt->comp, (ch), -1, (op), (val), (val2), 0 ,NULL ,NULL)
- #define PUSH_BINARY_EXPR(op, ch1, ch2, val, val2) \
- xmlXPathCompExprAdd(ctxt->comp, (ch1), (ch2), (op), \
- (val), (val2), 0 ,NULL ,NULL)
- #define XP_HAS_CACHE(c) ((c != NULL) && ((c)->cache != NULL))
- typedef struct _xmlXPathContextCache xmlXPathContextCache;
- typedef xmlXPathContextCache *xmlXPathContextCachePtr;
- struct _xmlXPathContextCache {
- xmlPointerListPtr nodesetObjs;
- xmlPointerListPtr stringObjs;
- xmlPointerListPtr booleanObjs;
- xmlPointerListPtr numberObjs;
- xmlPointerListPtr miscObjs;
- int maxNodeset;
- int maxString;
- int maxBoolean;
- int maxNumber;
- int maxMisc;
- #ifdef XP_DEBUG_OBJ_USAGE
- int dbgCachedAll;
- int dbgCachedNodeset;
- int dbgCachedString;
- int dbgCachedBool;
- int dbgCachedNumber;
- int dbgCachedPoint;
- int dbgCachedRange;
- int dbgCachedLocset;
- int dbgCachedUsers;
- int dbgCachedXSLTTree;
- int dbgCachedUndefined;
- int dbgReusedAll;
- int dbgReusedNodeset;
- int dbgReusedString;
- int dbgReusedBool;
- int dbgReusedNumber;
- int dbgReusedPoint;
- int dbgReusedRange;
- int dbgReusedLocset;
- int dbgReusedUsers;
- int dbgReusedXSLTTree;
- int dbgReusedUndefined;
- #endif
- };
- #define STRANGE \
- xmlGenericError(xmlGenericErrorContext, \
- "Internal error at %s:%d\n", \
- __FILE__, __LINE__);
- #ifdef LIBXML_DEBUG_ENABLED
- static void
- xmlXPathDebugDumpNode(FILE *output, xmlNodePtr cur, int depth) {
- int i;
- char shift[100];
- for (i = 0;((i < depth) && (i < 25));i++)
- shift[2 * i] = shift[2 * i + 1] = ' ';
- shift[2 * i] = shift[2 * i + 1] = 0;
- if (cur == NULL) {
- fprintf(output, "%s", shift);
- fprintf(output, "Node is NULL !\n");
- return;
- }
- if ((cur->type == XML_DOCUMENT_NODE) ||
- (cur->type == XML_HTML_DOCUMENT_NODE)) {
- fprintf(output, "%s", shift);
- fprintf(output, " /\n");
- } else if (cur->type == XML_ATTRIBUTE_NODE)
- xmlDebugDumpAttr(output, (xmlAttrPtr)cur, depth);
- else
- xmlDebugDumpOneNode(output, cur, depth);
- }
- static void
- xmlXPathDebugDumpNodeList(FILE *output, xmlNodePtr cur, int depth) {
- xmlNodePtr tmp;
- int i;
- char shift[100];
- for (i = 0;((i < depth) && (i < 25));i++)
- shift[2 * i] = shift[2 * i + 1] = ' ';
- shift[2 * i] = shift[2 * i + 1] = 0;
- if (cur == NULL) {
- fprintf(output, "%s", shift);
- fprintf(output, "Node is NULL !\n");
- return;
- }
- while (cur != NULL) {
- tmp = cur;
- cur = cur->next;
- xmlDebugDumpOneNode(output, tmp, depth);
- }
- }
- static void
- xmlXPathDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur, int depth) {
- int i;
- char shift[100];
- for (i = 0;((i < depth) && (i < 25));i++)
- shift[2 * i] = shift[2 * i + 1] = ' ';
- shift[2 * i] = shift[2 * i + 1] = 0;
- if (cur == NULL) {
- fprintf(output, "%s", shift);
- fprintf(output, "NodeSet is NULL !\n");
- return;
- }
- if (cur != NULL) {
- fprintf(output, "Set contains %d nodes:\n", cur->nodeNr);
- for (i = 0;i < cur->nodeNr;i++) {
- fprintf(output, "%s", shift);
- fprintf(output, "%d", i + 1);
- xmlXPathDebugDumpNode(output, cur->nodeTab[i], depth + 1);
- }
- }
- }
- static void
- xmlXPathDebugDumpValueTree(FILE *output, xmlNodeSetPtr cur, int depth) {
- int i;
- char shift[100];
- for (i = 0;((i < depth) && (i < 25));i++)
- shift[2 * i] = shift[2 * i + 1] = ' ';
- shift[2 * i] = shift[2 * i + 1] = 0;
- if ((cur == NULL) || (cur->nodeNr == 0) || (cur->nodeTab[0] == NULL)) {
- fprintf(output, "%s", shift);
- fprintf(output, "Value Tree is NULL !\n");
- return;
- }
- fprintf(output, "%s", shift);
- fprintf(output, "%d", i + 1);
- xmlXPathDebugDumpNodeList(output, cur->nodeTab[0]->children, depth + 1);
- }
- #if defined(LIBXML_XPTR_ENABLED)
- static void
- xmlXPathDebugDumpLocationSet(FILE *output, xmlLocationSetPtr cur, int depth) {
- int i;
- char shift[100];
- for (i = 0;((i < depth) && (i < 25));i++)
- shift[2 * i] = shift[2 * i + 1] = ' ';
- shift[2 * i] = shift[2 * i + 1] = 0;
- if (cur == NULL) {
- fprintf(output, "%s", shift);
- fprintf(output, "LocationSet is NULL !\n");
- return;
- }
- for (i = 0;i < cur->locNr;i++) {
- fprintf(output, "%s", shift);
- fprintf(output, "%d : ", i + 1);
- xmlXPathDebugDumpObject(output, cur->locTab[i], depth + 1);
- }
- }
- #endif
- void
- xmlXPathDebugDumpObject(FILE *output, xmlXPathObjectPtr cur, int depth) {
- int i;
- char shift[100];
- if (output == NULL) return;
- for (i = 0;((i < depth) && (i < 25));i++)
- shift[2 * i] = shift[2 * i + 1] = ' ';
- shift[2 * i] = shift[2 * i + 1] = 0;
- fprintf(output, "%s", shift);
- if (cur == NULL) {
- fprintf(output, "Object is empty (NULL)\n");
- return;
- }
- switch(cur->type) {
- case XPATH_UNDEFINED:
- fprintf(output, "Object is uninitialized\n");
- break;
- case XPATH_NODESET:
- fprintf(output, "Object is a Node Set :\n");
- xmlXPathDebugDumpNodeSet(output, cur->nodesetval, depth);
- break;
- case XPATH_XSLT_TREE:
- fprintf(output, "Object is an XSLT value tree :\n");
- xmlXPathDebugDumpValueTree(output, cur->nodesetval, depth);
- break;
- case XPATH_BOOLEAN:
- fprintf(output, "Object is a Boolean : ");
- if (cur->boolval) fprintf(output, "true\n");
- else fprintf(output, "false\n");
- break;
- case XPATH_NUMBER:
- switch (xmlXPathIsInf(cur->floatval)) {
- case 1:
- fprintf(output, "Object is a number : Infinity\n");
- break;
- case -1:
- fprintf(output, "Object is a number : -Infinity\n");
- break;
- default:
- if (xmlXPathIsNaN(cur->floatval)) {
- fprintf(output, "Object is a number : NaN\n");
- } else if (cur->floatval == 0 && xmlXPathGetSign(cur->floatval) != 0) {
- fprintf(output, "Object is a number : 0\n");
- } else {
- fprintf(output, "Object is a number : %0g\n", cur->floatval);
- }
- }
- break;
- case XPATH_STRING:
- fprintf(output, "Object is a string : ");
- xmlDebugDumpString(output, cur->stringval);
- fprintf(output, "\n");
- break;
- case XPATH_POINT:
- fprintf(output, "Object is a point : index %d in node", cur->index);
- xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, depth + 1);
- fprintf(output, "\n");
- break;
- case XPATH_RANGE:
- if ((cur->user2 == NULL) ||
- ((cur->user2 == cur->user) && (cur->index == cur->index2))) {
- fprintf(output, "Object is a collapsed range :\n");
- fprintf(output, "%s", shift);
- if (cur->index >= 0)
- fprintf(output, "index %d in ", cur->index);
- fprintf(output, "node\n");
- xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user,
- depth + 1);
- } else {
- fprintf(output, "Object is a range :\n");
- fprintf(output, "%s", shift);
- fprintf(output, "From ");
- if (cur->index >= 0)
- fprintf(output, "index %d in ", cur->index);
- fprintf(output, "node\n");
- xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user,
- depth + 1);
- fprintf(output, "%s", shift);
- fprintf(output, "To ");
- if (cur->index2 >= 0)
- fprintf(output, "index %d in ", cur->index2);
- fprintf(output, "node\n");
- xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user2,
- depth + 1);
- fprintf(output, "\n");
- }
- break;
- case XPATH_LOCATIONSET:
- #if defined(LIBXML_XPTR_ENABLED)
- fprintf(output, "Object is a Location Set:\n");
- xmlXPathDebugDumpLocationSet(output,
- (xmlLocationSetPtr) cur->user, depth);
- #endif
- break;
- case XPATH_USERS:
- fprintf(output, "Object is user defined\n");
- break;
- }
- }
- static void
- xmlXPathDebugDumpStepOp(FILE *output, xmlXPathCompExprPtr comp,
- xmlXPathStepOpPtr op, int depth) {
- int i;
- char shift[100];
- for (i = 0;((i < depth) && (i < 25));i++)
- shift[2 * i] = shift[2 * i + 1] = ' ';
- shift[2 * i] = shift[2 * i + 1] = 0;
- fprintf(output, "%s", shift);
- if (op == NULL) {
- fprintf(output, "Step is NULL\n");
- return;
- }
- switch (op->op) {
- case XPATH_OP_END:
- fprintf(output, "END"); break;
- case XPATH_OP_AND:
- fprintf(output, "AND"); break;
- case XPATH_OP_OR:
- fprintf(output, "OR"); break;
- case XPATH_OP_EQUAL:
- if (op->value)
- fprintf(output, "EQUAL =");
- else
- fprintf(output, "EQUAL !=");
- break;
- case XPATH_OP_CMP:
- if (op->value)
- fprintf(output, "CMP <");
- else
- fprintf(output, "CMP >");
- if (!op->value2)
- fprintf(output, "=");
- break;
- case XPATH_OP_PLUS:
- if (op->value == 0)
- fprintf(output, "PLUS -");
- else if (op->value == 1)
- fprintf(output, "PLUS +");
- else if (op->value == 2)
- fprintf(output, "PLUS unary -");
- else if (op->value == 3)
- fprintf(output, "PLUS unary - -");
- break;
- case XPATH_OP_MULT:
- if (op->value == 0)
- fprintf(output, "MULT *");
- else if (op->value == 1)
- fprintf(output, "MULT div");
- else
- fprintf(output, "MULT mod");
- break;
- case XPATH_OP_UNION:
- fprintf(output, "UNION"); break;
- case XPATH_OP_ROOT:
- fprintf(output, "ROOT"); break;
- case XPATH_OP_NODE:
- fprintf(output, "NODE"); break;
- case XPATH_OP_RESET:
- fprintf(output, "RESET"); break;
- case XPATH_OP_SORT:
- fprintf(output, "SORT"); break;
- case XPATH_OP_COLLECT: {
- xmlXPathAxisVal axis = (xmlXPathAxisVal)op->value;
- xmlXPathTestVal test = (xmlXPathTestVal)op->value2;
- xmlXPathTypeVal type = (xmlXPathTypeVal)op->value3;
- const xmlChar *prefix = op->value4;
- const xmlChar *name = op->value5;
- fprintf(output, "COLLECT ");
- switch (axis) {
- case AXIS_ANCESTOR:
- fprintf(output, " 'ancestors' "); break;
- case AXIS_ANCESTOR_OR_SELF:
- fprintf(output, " 'ancestors-or-self' "); break;
- case AXIS_ATTRIBUTE:
- fprintf(output, " 'attributes' "); break;
- case AXIS_CHILD:
- fprintf(output, " 'child' "); break;
- case AXIS_DESCENDANT:
- fprintf(output, " 'descendant' "); break;
- case AXIS_DESCENDANT_OR_SELF:
- fprintf(output, " 'descendant-or-self' "); break;
- case AXIS_FOLLOWING:
- fprintf(output, " 'following' "); break;
- case AXIS_FOLLOWING_SIBLING:
- fprintf(output, " 'following-siblings' "); break;
- case AXIS_NAMESPACE:
- fprintf(output, " 'namespace' "); break;
- case AXIS_PARENT:
- fprintf(output, " 'parent' "); break;
- case AXIS_PRECEDING:
- fprintf(output, " 'preceding' "); break;
- case AXIS_PRECEDING_SIBLING:
- fprintf(output, " 'preceding-sibling' "); break;
- case AXIS_SELF:
- fprintf(output, " 'self' "); break;
- }
- switch (test) {
- case NODE_TEST_NONE:
- fprintf(output, "'none' "); break;
- case NODE_TEST_TYPE:
- fprintf(output, "'type' "); break;
- case NODE_TEST_PI:
- fprintf(output, "'PI' "); break;
- case NODE_TEST_ALL:
- fprintf(output, "'all' "); break;
- case NODE_TEST_NS:
- fprintf(output, "'namespace' "); break;
- case NODE_TEST_NAME:
- fprintf(output, "'name' "); break;
- }
- switch (type) {
- case NODE_TYPE_NODE:
- fprintf(output, "'node' "); break;
- case NODE_TYPE_COMMENT:
- fprintf(output, "'comment' "); break;
- case NODE_TYPE_TEXT:
- fprintf(output, "'text' "); break;
- case NODE_TYPE_PI:
- fprintf(output, "'PI' "); break;
- }
- if (prefix != NULL)
- fprintf(output, "%s:", prefix);
- if (name != NULL)
- fprintf(output, "%s", (const char *) name);
- break;
- }
- case XPATH_OP_VALUE: {
- xmlXPathObjectPtr object = (xmlXPathObjectPtr) op->value4;
- fprintf(output, "ELEM ");
- xmlXPathDebugDumpObject(output, object, 0);
- goto finish;
- }
- case XPATH_OP_VARIABLE: {
- const xmlChar *prefix = op->value5;
- const xmlChar *name = op->value4;
- if (prefix != NULL)
- fprintf(output, "VARIABLE %s:%s", prefix, name);
- else
- fprintf(output, "VARIABLE %s", name);
- break;
- }
- case XPATH_OP_FUNCTION: {
- int nbargs = op->value;
- const xmlChar *prefix = op->value5;
- const xmlChar *name = op->value4;
- if (prefix != NULL)
- fprintf(output, "FUNCTION %s:%s(%d args)",
- prefix, name, nbargs);
- else
- fprintf(output, "FUNCTION %s(%d args)", name, nbargs);
- break;
- }
- case XPATH_OP_ARG: fprintf(output, "ARG"); break;
- case XPATH_OP_PREDICATE: fprintf(output, "PREDICATE"); break;
- case XPATH_OP_FILTER: fprintf(output, "FILTER"); break;
- #ifdef LIBXML_XPTR_ENABLED
- case XPATH_OP_RANGETO: fprintf(output, "RANGETO"); break;
- #endif
- default:
- fprintf(output, "UNKNOWN %d\n", op->op); return;
- }
- fprintf(output, "\n");
- finish:
- if (op->ch1 >= 0)
- xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch1], depth + 1);
- if (op->ch2 >= 0)
- xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch2], depth + 1);
- }
- void
- xmlXPathDebugDumpCompExpr(FILE *output, xmlXPathCompExprPtr comp,
- int depth) {
- int i;
- char shift[100];
- if ((output == NULL) || (comp == NULL)) return;
- for (i = 0;((i < depth) && (i < 25));i++)
- shift[2 * i] = shift[2 * i + 1] = ' ';
- shift[2 * i] = shift[2 * i + 1] = 0;
- fprintf(output, "%s", shift);
- fprintf(output, "Compiled Expression : %d elements\n",
- comp->nbStep);
- i = comp->last;
- xmlXPathDebugDumpStepOp(output, comp, &comp->steps[i], depth + 1);
- }
- #ifdef XP_DEBUG_OBJ_USAGE
- static int xmlXPathDebugObjCounterUndefined = 0;
- static int xmlXPathDebugObjCounterNodeset = 0;
- static int xmlXPathDebugObjCounterBool = 0;
- static int xmlXPathDebugObjCounterNumber = 0;
- static int xmlXPathDebugObjCounterString = 0;
- static int xmlXPathDebugObjCounterPoint = 0;
- static int xmlXPathDebugObjCounterRange = 0;
- static int xmlXPathDebugObjCounterLocset = 0;
- static int xmlXPathDebugObjCounterUsers = 0;
- static int xmlXPathDebugObjCounterXSLTTree = 0;
- static int xmlXPathDebugObjCounterAll = 0;
- static int xmlXPathDebugObjTotalUndefined = 0;
- static int xmlXPathDebugObjTotalNodeset = 0;
- static int xmlXPathDebugObjTotalBool = 0;
- static int xmlXPathDebugObjTotalNumber = 0;
- static int xmlXPathDebugObjTotalString = 0;
- static int xmlXPathDebugObjTotalPoint = 0;
- static int xmlXPathDebugObjTotalRange = 0;
- static int xmlXPathDebugObjTotalLocset = 0;
- static int xmlXPathDebugObjTotalUsers = 0;
- static int xmlXPathDebugObjTotalXSLTTree = 0;
- static int xmlXPathDebugObjTotalAll = 0;
- static int xmlXPathDebugObjMaxUndefined = 0;
- static int xmlXPathDebugObjMaxNodeset = 0;
- static int xmlXPathDebugObjMaxBool = 0;
- static int xmlXPathDebugObjMaxNumber = 0;
- static int xmlXPathDebugObjMaxString = 0;
- static int xmlXPathDebugObjMaxPoint = 0;
- static int xmlXPathDebugObjMaxRange = 0;
- static int xmlXPathDebugObjMaxLocset = 0;
- static int xmlXPathDebugObjMaxUsers = 0;
- static int xmlXPathDebugObjMaxXSLTTree = 0;
- static int xmlXPathDebugObjMaxAll = 0;
- static void
- xmlXPathDebugObjUsageReset(xmlXPathContextPtr ctxt)
- {
- if (ctxt != NULL) {
- if (ctxt->cache != NULL) {
- xmlXPathContextCachePtr cache =
- (xmlXPathContextCachePtr) ctxt->cache;
- cache->dbgCachedAll = 0;
- cache->dbgCachedNodeset = 0;
- cache->dbgCachedString = 0;
- cache->dbgCachedBool = 0;
- cache->dbgCachedNumber = 0;
- cache->dbgCachedPoint = 0;
- cache->dbgCachedRange = 0;
- cache->dbgCachedLocset = 0;
- cache->dbgCachedUsers = 0;
- cache->dbgCachedXSLTTree = 0;
- cache->dbgCachedUndefined = 0;
- cache->dbgReusedAll = 0;
- cache->dbgReusedNodeset = 0;
- cache->dbgReusedString = 0;
- cache->dbgReusedBool = 0;
- cache->dbgReusedNumber = 0;
- cache->dbgReusedPoint = 0;
- cache->dbgReusedRange = 0;
- cache->dbgReusedLocset = 0;
- cache->dbgReusedUsers = 0;
- cache->dbgReusedXSLTTree = 0;
- cache->dbgReusedUndefined = 0;
- }
- }
- xmlXPathDebugObjCounterUndefined = 0;
- xmlXPathDebugObjCounterNodeset = 0;
- xmlXPathDebugObjCounterBool = 0;
- xmlXPathDebugObjCounterNumber = 0;
- xmlXPathDebugObjCounterString = 0;
- xmlXPathDebugObjCounterPoint = 0;
- xmlXPathDebugObjCounterRange = 0;
- xmlXPathDebugObjCounterLocset = 0;
- xmlXPathDebugObjCounterUsers = 0;
- xmlXPathDebugObjCounterXSLTTree = 0;
- xmlXPathDebugObjCounterAll = 0;
- xmlXPathDebugObjTotalUndefined = 0;
- xmlXPathDebugObjTotalNodeset = 0;
- xmlXPathDebugObjTotalBool = 0;
- xmlXPathDebugObjTotalNumber = 0;
- xmlXPathDebugObjTotalString = 0;
- xmlXPathDebugObjTotalPoint = 0;
- xmlXPathDebugObjTotalRange = 0;
- xmlXPathDebugObjTotalLocset = 0;
- xmlXPathDebugObjTotalUsers = 0;
- xmlXPathDebugObjTotalXSLTTree = 0;
- xmlXPathDebugObjTotalAll = 0;
- xmlXPathDebugObjMaxUndefined = 0;
- xmlXPathDebugObjMaxNodeset = 0;
- xmlXPathDebugObjMaxBool = 0;
- xmlXPathDebugObjMaxNumber = 0;
- xmlXPathDebugObjMaxString = 0;
- xmlXPathDebugObjMaxPoint = 0;
- xmlXPathDebugObjMaxRange = 0;
- xmlXPathDebugObjMaxLocset = 0;
- xmlXPathDebugObjMaxUsers = 0;
- xmlXPathDebugObjMaxXSLTTree = 0;
- xmlXPathDebugObjMaxAll = 0;
- }
- static void
- xmlXPathDebugObjUsageRequested(xmlXPathContextPtr ctxt,
- xmlXPathObjectType objType)
- {
- int isCached = 0;
- if (ctxt != NULL) {
- if (ctxt->cache != NULL) {
- xmlXPathContextCachePtr cache =
- (xmlXPathContextCachePtr) ctxt->cache;
- isCached = 1;
- cache->dbgReusedAll++;
- switch (objType) {
- case XPATH_UNDEFINED:
- cache->dbgReusedUndefined++;
- break;
- case XPATH_NODESET:
- cache->dbgReusedNodeset++;
- break;
- case XPATH_BOOLEAN:
- cache->dbgReusedBool++;
- break;
- case XPATH_NUMBER:
- cache->dbgReusedNumber++;
- break;
- case XPATH_STRING:
- cache->dbgReusedString++;
- break;
- case XPATH_POINT:
- cache->dbgReusedPoint++;
- break;
- case XPATH_RANGE:
- cache->dbgReusedRange++;
- break;
- case XPATH_LOCATIONSET:
- cache->dbgReusedLocset++;
- break;
- case XPATH_USERS:
- cache->dbgReusedUsers++;
- break;
- case XPATH_XSLT_TREE:
- cache->dbgReusedXSLTTree++;
- break;
- default:
- break;
- }
- }
- }
- switch (objType) {
- case XPATH_UNDEFINED:
- if (! isCached)
- xmlXPathDebugObjTotalUndefined++;
- xmlXPathDebugObjCounterUndefined++;
- if (xmlXPathDebugObjCounterUndefined >
- xmlXPathDebugObjMaxUndefined)
- xmlXPathDebugObjMaxUndefined =
- xmlXPathDebugObjCounterUndefined;
- break;
- case XPATH_NODESET:
- if (! isCached)
- xmlXPathDebugObjTotalNodeset++;
- xmlXPathDebugObjCounterNodeset++;
- if (xmlXPathDebugObjCounterNodeset >
- xmlXPathDebugObjMaxNodeset)
- xmlXPathDebugObjMaxNodeset =
- xmlXPathDebugObjCounterNodeset;
- break;
- case XPATH_BOOLEAN:
- if (! isCached)
- xmlXPathDebugObjTotalBool++;
- xmlXPathDebugObjCounterBool++;
- if (xmlXPathDebugObjCounterBool >
- xmlXPathDebugObjMaxBool)
- xmlXPathDebugObjMaxBool =
- xmlXPathDebugObjCounterBool;
- break;
- case XPATH_NUMBER:
- if (! isCached)
- xmlXPathDebugObjTotalNumber++;
- xmlXPathDebugObjCounterNumber++;
- if (xmlXPathDebugObjCounterNumber >
- xmlXPathDebugObjMaxNumber)
- xmlXPathDebugObjMaxNumber =
- xmlXPathDebugObjCounterNumber;
- break;
- case XPATH_STRING:
- if (! isCached)
- xmlXPathDebugObjTotalString++;
- xmlXPathDebugObjCounterString++;
- if (xmlXPathDebugObjCounterString >
- xmlXPathDebugObjMaxString)
- xmlXPathDebugObjMaxString =
- xmlXPathDebugObjCounterString;
- break;
- case XPATH_POINT:
- if (! isCached)
- xmlXPathDebugObjTotalPoint++;
- xmlXPathDebugObjCounterPoint++;
- if (xmlXPathDebugObjCounterPoint >
- xmlXPathDebugObjMaxPoint)
- xmlXPathDebugObjMaxPoint =
- xmlXPathDebugObjCounterPoint;
- break;
- case XPATH_RANGE:
- if (! isCached)
- xmlXPathDebugObjTotalRange++;
- xmlXPathDebugObjCounterRange++;
- if (xmlXPathDebugObjCounterRange >
- xmlXPathDebugObjMaxRange)
- xmlXPathDebugObjMaxRange =
- xmlXPathDebugObjCounterRange;
- break;
- case XPATH_LOCATIONSET:
- if (! isCached)
- xmlXPathDebugObjTotalLocset++;
- xmlXPathDebugObjCounterLocset++;
- if (xmlXPathDebugObjCounterLocset >
- xmlXPathDebugObjMaxLocset)
- xmlXPathDebugObjMaxLocset =
- xmlXPathDebugObjCounterLocset;
- break;
- case XPATH_USERS:
- if (! isCached)
- xmlXPathDebugObjTotalUsers++;
- xmlXPathDebugObjCounterUsers++;
- if (xmlXPathDebugObjCounterUsers >
- xmlXPathDebugObjMaxUsers)
- xmlXPathDebugObjMaxUsers =
- xmlXPathDebugObjCounterUsers;
- break;
- case XPATH_XSLT_TREE:
- if (! isCached)
- xmlXPathDebugObjTotalXSLTTree++;
- xmlXPathDebugObjCounterXSLTTree++;
- if (xmlXPathDebugObjCounterXSLTTree >
- xmlXPathDebugObjMaxXSLTTree)
- xmlXPathDebugObjMaxXSLTTree =
- xmlXPathDebugObjCounterXSLTTree;
- break;
- default:
- break;
- }
- if (! isCached)
- xmlXPathDebugObjTotalAll++;
- xmlXPathDebugObjCounterAll++;
- if (xmlXPathDebugObjCounterAll >
- xmlXPathDebugObjMaxAll)
- xmlXPathDebugObjMaxAll =
- xmlXPathDebugObjCounterAll;
- }
- static void
- xmlXPathDebugObjUsageReleased(xmlXPathContextPtr ctxt,
- xmlXPathObjectType objType)
- {
- int isCached = 0;
- if (ctxt != NULL) {
- if (ctxt->cache != NULL) {
- xmlXPathContextCachePtr cache =
- (xmlXPathContextCachePtr) ctxt->cache;
- isCached = 1;
- cache->dbgCachedAll++;
- switch (objType) {
- case XPATH_UNDEFINED:
- cache->dbgCachedUndefined++;
- break;
- case XPATH_NODESET:
- cache->dbgCachedNodeset++;
- break;
- case XPATH_BOOLEAN:
- cache->dbgCachedBool++;
- break;
- case XPATH_NUMBER:
- cache->dbgCachedNumber++;
- break;
- case XPATH_STRING:
- cache->dbgCachedString++;
- break;
- case XPATH_POINT:
- cache->dbgCachedPoint++;
- break;
- case XPATH_RANGE:
- cache->dbgCachedRange++;
- break;
- case XPATH_LOCATIONSET:
- cache->dbgCachedLocset++;
- break;
- case XPATH_USERS:
- cache->dbgCachedUsers++;
- break;
- case XPATH_XSLT_TREE:
- cache->dbgCachedXSLTTree++;
- break;
- default:
- break;
- }
- }
- }
- switch (objType) {
- case XPATH_UNDEFINED:
- xmlXPathDebugObjCounterUndefined--;
- break;
- case XPATH_NODESET:
- xmlXPathDebugObjCounterNodeset--;
- break;
- case XPATH_BOOLEAN:
- xmlXPathDebugObjCounterBool--;
- break;
- case XPATH_NUMBER:
- xmlXPathDebugObjCounterNumber--;
- break;
- case XPATH_STRING:
- xmlXPathDebugObjCounterString--;
- break;
- case XPATH_POINT:
- xmlXPathDebugObjCounterPoint--;
- break;
- case XPATH_RANGE:
- xmlXPathDebugObjCounterRange--;
- break;
- case XPATH_LOCATIONSET:
- xmlXPathDebugObjCounterLocset--;
- break;
- case XPATH_USERS:
- xmlXPathDebugObjCounterUsers--;
- break;
- case XPATH_XSLT_TREE:
- xmlXPathDebugObjCounterXSLTTree--;
- break;
- default:
- break;
- }
- xmlXPathDebugObjCounterAll--;
- }
- static void
- xmlXPathDebugObjUsageDisplay(xmlXPathContextPtr ctxt)
- {
- int reqAll, reqNodeset, reqString, reqBool, reqNumber,
- reqXSLTTree, reqUndefined;
- int caAll = 0, caNodeset = 0, caString = 0, caBool = 0,
- caNumber = 0, caXSLTTree = 0, caUndefined = 0;
- int reAll = 0, reNodeset = 0, reString = 0, reBool = 0,
- reNumber = 0, reXSLTTree = 0, reUndefined = 0;
- int leftObjs = xmlXPathDebugObjCounterAll;
- reqAll = xmlXPathDebugObjTotalAll;
- reqNodeset = xmlXPathDebugObjTotalNodeset;
- reqString = xmlXPathDebugObjTotalString;
- reqBool = xmlXPathDebugObjTotalBool;
- reqNumber = xmlXPathDebugObjTotalNumber;
- reqXSLTTree = xmlXPathDebugObjTotalXSLTTree;
- reqUndefined = xmlXPathDebugObjTotalUndefined;
- printf("# XPath object usage:\n");
- if (ctxt != NULL) {
- if (ctxt->cache != NULL) {
- xmlXPathContextCachePtr cache =
- (xmlXPathContextCachePtr) ctxt->cache;
- reAll = cache->dbgReusedAll;
- reqAll += reAll;
- reNodeset = cache->dbgReusedNodeset;
- reqNodeset += reNodeset;
- reString = cache->dbgReusedString;
- reqString += reString;
- reBool = cache->dbgReusedBool;
- reqBool += reBool;
- reNumber = cache->dbgReusedNumber;
- reqNumber += reNumber;
- reXSLTTree = cache->dbgReusedXSLTTree;
- reqXSLTTree += reXSLTTree;
- reUndefined = cache->dbgReusedUndefined;
- reqUndefined += reUndefined;
- caAll = cache->dbgCachedAll;
- caBool = cache->dbgCachedBool;
- caNodeset = cache->dbgCachedNodeset;
- caString = cache->dbgCachedString;
- caNumber = cache->dbgCachedNumber;
- caXSLTTree = cache->dbgCachedXSLTTree;
- caUndefined = cache->dbgCachedUndefined;
- if (cache->nodesetObjs)
- leftObjs -= cache->nodesetObjs->number;
- if (cache->stringObjs)
- leftObjs -= cache->stringObjs->number;
- if (cache->booleanObjs)
- leftObjs -= cache->booleanObjs->number;
- if (cache->numberObjs)
- leftObjs -= cache->numberObjs->number;
- if (cache->miscObjs)
- leftObjs -= cache->miscObjs->number;
- }
- }
- printf("# all\n");
- printf("# total : %d\n", reqAll);
- printf("# left : %d\n", leftObjs);
- printf("# created: %d\n", xmlXPathDebugObjTotalAll);
- printf("# reused : %d\n", reAll);
- printf("# max : %d\n", xmlXPathDebugObjMaxAll);
- printf("# node-sets\n");
- printf("# total : %d\n", reqNodeset);
- printf("# created: %d\n", xmlXPathDebugObjTotalNodeset);
- printf("# reused : %d\n", reNodeset);
- printf("# max : %d\n", xmlXPathDebugObjMaxNodeset);
- printf("# strings\n");
- printf("# total : %d\n", reqString);
- printf("# created: %d\n", xmlXPathDebugObjTotalString);
- printf("# reused : %d\n", reString);
- printf("# max : %d\n", xmlXPathDebugObjMaxString);
- printf("# booleans\n");
- printf("# total : %d\n", reqBool);
- printf("# created: %d\n", xmlXPathDebugObjTotalBool);
- printf("# reused : %d\n", reBool);
- printf("# max : %d\n", xmlXPathDebugObjMaxBool);
- printf("# numbers\n");
- printf("# total : %d\n", reqNumber);
- printf("# created: %d\n", xmlXPathDebugObjTotalNumber);
- printf("# reused : %d\n", reNumber);
- printf("# max : %d\n", xmlXPathDebugObjMaxNumber);
- printf("# XSLT result tree fragments\n");
- printf("# total : %d\n", reqXSLTTree);
- printf("# created: %d\n", xmlXPathDebugObjTotalXSLTTree);
- printf("# reused : %d\n", reXSLTTree);
- printf("# max : %d\n", xmlXPathDebugObjMaxXSLTTree);
- printf("# undefined\n");
- printf("# total : %d\n", reqUndefined);
- printf("# created: %d\n", xmlXPathDebugObjTotalUndefined);
- printf("# reused : %d\n", reUndefined);
- printf("# max : %d\n", xmlXPathDebugObjMaxUndefined);
- }
- #endif
- #endif
- static xmlXPathContextCachePtr
- xmlXPathNewCache(void)
- {
- xmlXPathContextCachePtr ret;
- ret = (xmlXPathContextCachePtr) xmlMalloc(sizeof(xmlXPathContextCache));
- if (ret == NULL) {
- xmlXPathErrMemory(NULL, "creating object cache\n");
- return(NULL);
- }
- memset(ret, 0 , (size_t) sizeof(xmlXPathContextCache));
- ret->maxNodeset = 100;
- ret->maxString = 100;
- ret->maxBoolean = 100;
- ret->maxNumber = 100;
- ret->maxMisc = 100;
- return(ret);
- }
- static void
- xmlXPathCacheFreeObjectList(xmlPointerListPtr list)
- {
- int i;
- xmlXPathObjectPtr obj;
- if (list == NULL)
- return;
- for (i = 0; i < list->number; i++) {
- obj = list->items[i];
-
- if (obj->nodesetval != NULL) {
- if (obj->nodesetval->nodeTab != NULL)
- xmlFree(obj->nodesetval->nodeTab);
- xmlFree(obj->nodesetval);
- }
- xmlFree(obj);
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjCounterAll--;
- #endif
- }
- xmlPointerListFree(list);
- }
- static void
- xmlXPathFreeCache(xmlXPathContextCachePtr cache)
- {
- if (cache == NULL)
- return;
- if (cache->nodesetObjs)
- xmlXPathCacheFreeObjectList(cache->nodesetObjs);
- if (cache->stringObjs)
- xmlXPathCacheFreeObjectList(cache->stringObjs);
- if (cache->booleanObjs)
- xmlXPathCacheFreeObjectList(cache->booleanObjs);
- if (cache->numberObjs)
- xmlXPathCacheFreeObjectList(cache->numberObjs);
- if (cache->miscObjs)
- xmlXPathCacheFreeObjectList(cache->miscObjs);
- xmlFree(cache);
- }
- int
- xmlXPathContextSetCache(xmlXPathContextPtr ctxt,
- int active,
- int value,
- int options)
- {
- if (ctxt == NULL)
- return(-1);
- if (active) {
- xmlXPathContextCachePtr cache;
- if (ctxt->cache == NULL) {
- ctxt->cache = xmlXPathNewCache();
- if (ctxt->cache == NULL)
- return(-1);
- }
- cache = (xmlXPathContextCachePtr) ctxt->cache;
- if (options == 0) {
- if (value < 0)
- value = 100;
- cache->maxNodeset = value;
- cache->maxString = value;
- cache->maxNumber = value;
- cache->maxBoolean = value;
- cache->maxMisc = value;
- }
- } else if (ctxt->cache != NULL) {
- xmlXPathFreeCache((xmlXPathContextCachePtr) ctxt->cache);
- ctxt->cache = NULL;
- }
- return(0);
- }
- static xmlXPathObjectPtr
- xmlXPathCacheWrapNodeSet(xmlXPathContextPtr ctxt, xmlNodeSetPtr val)
- {
- if ((ctxt != NULL) && (ctxt->cache != NULL)) {
- xmlXPathContextCachePtr cache =
- (xmlXPathContextCachePtr) ctxt->cache;
- if ((cache->miscObjs != NULL) &&
- (cache->miscObjs->number != 0))
- {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr)
- cache->miscObjs->items[--cache->miscObjs->number];
- ret->type = XPATH_NODESET;
- ret->nodesetval = val;
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(ctxt, XPATH_NODESET);
- #endif
- return(ret);
- }
- }
- return(xmlXPathWrapNodeSet(val));
- }
- static xmlXPathObjectPtr
- xmlXPathCacheWrapString(xmlXPathContextPtr ctxt, xmlChar *val)
- {
- if ((ctxt != NULL) && (ctxt->cache != NULL)) {
- xmlXPathContextCachePtr cache = (xmlXPathContextCachePtr) ctxt->cache;
- if ((cache->stringObjs != NULL) &&
- (cache->stringObjs->number != 0))
- {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr)
- cache->stringObjs->items[--cache->stringObjs->number];
- ret->type = XPATH_STRING;
- ret->stringval = val;
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(ctxt, XPATH_STRING);
- #endif
- return(ret);
- } else if ((cache->miscObjs != NULL) &&
- (cache->miscObjs->number != 0))
- {
- xmlXPathObjectPtr ret;
-
- ret = (xmlXPathObjectPtr)
- cache->miscObjs->items[--cache->miscObjs->number];
- ret->type = XPATH_STRING;
- ret->stringval = val;
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(ctxt, XPATH_STRING);
- #endif
- return(ret);
- }
- }
- return(xmlXPathWrapString(val));
- }
- static xmlXPathObjectPtr
- xmlXPathCacheNewNodeSet(xmlXPathContextPtr ctxt, xmlNodePtr val)
- {
- if ((ctxt != NULL) && (ctxt->cache)) {
- xmlXPathContextCachePtr cache = (xmlXPathContextCachePtr) ctxt->cache;
- if ((cache->nodesetObjs != NULL) &&
- (cache->nodesetObjs->number != 0))
- {
- xmlXPathObjectPtr ret;
-
- ret = (xmlXPathObjectPtr)
- cache->nodesetObjs->items[--cache->nodesetObjs->number];
- ret->type = XPATH_NODESET;
- ret->boolval = 0;
- if (val) {
- if ((ret->nodesetval->nodeMax == 0) ||
- (val->type == XML_NAMESPACE_DECL))
- {
- xmlXPathNodeSetAddUnique(ret->nodesetval, val);
- } else {
- ret->nodesetval->nodeTab[0] = val;
- ret->nodesetval->nodeNr = 1;
- }
- }
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(ctxt, XPATH_NODESET);
- #endif
- return(ret);
- } else if ((cache->miscObjs != NULL) &&
- (cache->miscObjs->number != 0))
- {
- xmlXPathObjectPtr ret;
-
- ret = (xmlXPathObjectPtr)
- cache->miscObjs->items[--cache->miscObjs->number];
- ret->type = XPATH_NODESET;
- ret->boolval = 0;
- ret->nodesetval = xmlXPathNodeSetCreate(val);
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(ctxt, XPATH_NODESET);
- #endif
- return(ret);
- }
- }
- return(xmlXPathNewNodeSet(val));
- }
- static xmlXPathObjectPtr
- xmlXPathCacheNewCString(xmlXPathContextPtr ctxt, const char *val)
- {
- if ((ctxt != NULL) && (ctxt->cache)) {
- xmlXPathContextCachePtr cache = (xmlXPathContextCachePtr) ctxt->cache;
- if ((cache->stringObjs != NULL) &&
- (cache->stringObjs->number != 0))
- {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr)
- cache->stringObjs->items[--cache->stringObjs->number];
- ret->type = XPATH_STRING;
- ret->stringval = xmlStrdup(BAD_CAST val);
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(ctxt, XPATH_STRING);
- #endif
- return(ret);
- } else if ((cache->miscObjs != NULL) &&
- (cache->miscObjs->number != 0))
- {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr)
- cache->miscObjs->items[--cache->miscObjs->number];
- ret->type = XPATH_STRING;
- ret->stringval = xmlStrdup(BAD_CAST val);
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(ctxt, XPATH_STRING);
- #endif
- return(ret);
- }
- }
- return(xmlXPathNewCString(val));
- }
- static xmlXPathObjectPtr
- xmlXPathCacheNewString(xmlXPathContextPtr ctxt, const xmlChar *val)
- {
- if ((ctxt != NULL) && (ctxt->cache)) {
- xmlXPathContextCachePtr cache = (xmlXPathContextCachePtr) ctxt->cache;
- if ((cache->stringObjs != NULL) &&
- (cache->stringObjs->number != 0))
- {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr)
- cache->stringObjs->items[--cache->stringObjs->number];
- ret->type = XPATH_STRING;
- if (val != NULL)
- ret->stringval = xmlStrdup(val);
- else
- ret->stringval = xmlStrdup((const xmlChar *)"");
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(ctxt, XPATH_STRING);
- #endif
- return(ret);
- } else if ((cache->miscObjs != NULL) &&
- (cache->miscObjs->number != 0))
- {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr)
- cache->miscObjs->items[--cache->miscObjs->number];
- ret->type = XPATH_STRING;
- if (val != NULL)
- ret->stringval = xmlStrdup(val);
- else
- ret->stringval = xmlStrdup((const xmlChar *)"");
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(ctxt, XPATH_STRING);
- #endif
- return(ret);
- }
- }
- return(xmlXPathNewString(val));
- }
- static xmlXPathObjectPtr
- xmlXPathCacheNewBoolean(xmlXPathContextPtr ctxt, int val)
- {
- if ((ctxt != NULL) && (ctxt->cache)) {
- xmlXPathContextCachePtr cache = (xmlXPathContextCachePtr) ctxt->cache;
- if ((cache->booleanObjs != NULL) &&
- (cache->booleanObjs->number != 0))
- {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr)
- cache->booleanObjs->items[--cache->booleanObjs->number];
- ret->type = XPATH_BOOLEAN;
- ret->boolval = (val != 0);
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(ctxt, XPATH_BOOLEAN);
- #endif
- return(ret);
- } else if ((cache->miscObjs != NULL) &&
- (cache->miscObjs->number != 0))
- {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr)
- cache->miscObjs->items[--cache->miscObjs->number];
- ret->type = XPATH_BOOLEAN;
- ret->boolval = (val != 0);
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(ctxt, XPATH_BOOLEAN);
- #endif
- return(ret);
- }
- }
- return(xmlXPathNewBoolean(val));
- }
- static xmlXPathObjectPtr
- xmlXPathCacheNewFloat(xmlXPathContextPtr ctxt, double val)
- {
- if ((ctxt != NULL) && (ctxt->cache)) {
- xmlXPathContextCachePtr cache = (xmlXPathContextCachePtr) ctxt->cache;
- if ((cache->numberObjs != NULL) &&
- (cache->numberObjs->number != 0))
- {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr)
- cache->numberObjs->items[--cache->numberObjs->number];
- ret->type = XPATH_NUMBER;
- ret->floatval = val;
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(ctxt, XPATH_NUMBER);
- #endif
- return(ret);
- } else if ((cache->miscObjs != NULL) &&
- (cache->miscObjs->number != 0))
- {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr)
- cache->miscObjs->items[--cache->miscObjs->number];
- ret->type = XPATH_NUMBER;
- ret->floatval = val;
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(ctxt, XPATH_NUMBER);
- #endif
- return(ret);
- }
- }
- return(xmlXPathNewFloat(val));
- }
- static xmlXPathObjectPtr
- xmlXPathCacheConvertString(xmlXPathContextPtr ctxt, xmlXPathObjectPtr val) {
- xmlChar *res = NULL;
- if (val == NULL)
- return(xmlXPathCacheNewCString(ctxt, ""));
- switch (val->type) {
- case XPATH_UNDEFINED:
- #ifdef DEBUG_EXPR
- xmlGenericError(xmlGenericErrorContext, "STRING: undefined\n");
- #endif
- break;
- case XPATH_NODESET:
- case XPATH_XSLT_TREE:
- res = xmlXPathCastNodeSetToString(val->nodesetval);
- break;
- case XPATH_STRING:
- return(val);
- case XPATH_BOOLEAN:
- res = xmlXPathCastBooleanToString(val->boolval);
- break;
- case XPATH_NUMBER:
- res = xmlXPathCastNumberToString(val->floatval);
- break;
- case XPATH_USERS:
- case XPATH_POINT:
- case XPATH_RANGE:
- case XPATH_LOCATIONSET:
- TODO;
- break;
- }
- xmlXPathReleaseObject(ctxt, val);
- if (res == NULL)
- return(xmlXPathCacheNewCString(ctxt, ""));
- return(xmlXPathCacheWrapString(ctxt, res));
- }
- static xmlXPathObjectPtr
- xmlXPathCacheObjectCopy(xmlXPathContextPtr ctxt, xmlXPathObjectPtr val)
- {
- if (val == NULL)
- return(NULL);
- if (XP_HAS_CACHE(ctxt)) {
- switch (val->type) {
- case XPATH_NODESET:
- return(xmlXPathCacheWrapNodeSet(ctxt,
- xmlXPathNodeSetMerge(NULL, val->nodesetval)));
- case XPATH_STRING:
- return(xmlXPathCacheNewString(ctxt, val->stringval));
- case XPATH_BOOLEAN:
- return(xmlXPathCacheNewBoolean(ctxt, val->boolval));
- case XPATH_NUMBER:
- return(xmlXPathCacheNewFloat(ctxt, val->floatval));
- default:
- break;
- }
- }
- return(xmlXPathObjectCopy(val));
- }
- static xmlXPathObjectPtr
- xmlXPathCacheConvertBoolean(xmlXPathContextPtr ctxt, xmlXPathObjectPtr val) {
- xmlXPathObjectPtr ret;
- if (val == NULL)
- return(xmlXPathCacheNewBoolean(ctxt, 0));
- if (val->type == XPATH_BOOLEAN)
- return(val);
- ret = xmlXPathCacheNewBoolean(ctxt, xmlXPathCastToBoolean(val));
- xmlXPathReleaseObject(ctxt, val);
- return(ret);
- }
- static xmlXPathObjectPtr
- xmlXPathCacheConvertNumber(xmlXPathContextPtr ctxt, xmlXPathObjectPtr val) {
- xmlXPathObjectPtr ret;
- if (val == NULL)
- return(xmlXPathCacheNewFloat(ctxt, 0.0));
- if (val->type == XPATH_NUMBER)
- return(val);
- ret = xmlXPathCacheNewFloat(ctxt, xmlXPathCastToNumber(val));
- xmlXPathReleaseObject(ctxt, val);
- return(ret);
- }
- xmlXPathObjectPtr
- valuePop(xmlXPathParserContextPtr ctxt)
- {
- xmlXPathObjectPtr ret;
- if ((ctxt == NULL) || (ctxt->valueNr <= 0))
- return (NULL);
- ctxt->valueNr--;
- if (ctxt->valueNr > 0)
- ctxt->value = ctxt->valueTab[ctxt->valueNr - 1];
- else
- ctxt->value = NULL;
- ret = ctxt->valueTab[ctxt->valueNr];
- ctxt->valueTab[ctxt->valueNr] = NULL;
- return (ret);
- }
- int
- valuePush(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr value)
- {
- if ((ctxt == NULL) || (value == NULL)) return(-1);
- if (ctxt->valueNr >= ctxt->valueMax) {
- xmlXPathObjectPtr *tmp;
- tmp = (xmlXPathObjectPtr *) xmlRealloc(ctxt->valueTab,
- 2 * ctxt->valueMax *
- sizeof(ctxt->valueTab[0]));
- if (tmp == NULL) {
- xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
- return (0);
- }
- ctxt->valueMax *= 2;
- ctxt->valueTab = tmp;
- }
- ctxt->valueTab[ctxt->valueNr] = value;
- ctxt->value = value;
- return (ctxt->valueNr++);
- }
- int
- xmlXPathPopBoolean (xmlXPathParserContextPtr ctxt) {
- xmlXPathObjectPtr obj;
- int ret;
- obj = valuePop(ctxt);
- if (obj == NULL) {
- xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
- return(0);
- }
- if (obj->type != XPATH_BOOLEAN)
- ret = xmlXPathCastToBoolean(obj);
- else
- ret = obj->boolval;
- xmlXPathReleaseObject(ctxt->context, obj);
- return(ret);
- }
- double
- xmlXPathPopNumber (xmlXPathParserContextPtr ctxt) {
- xmlXPathObjectPtr obj;
- double ret;
- obj = valuePop(ctxt);
- if (obj == NULL) {
- xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
- return(0);
- }
- if (obj->type != XPATH_NUMBER)
- ret = xmlXPathCastToNumber(obj);
- else
- ret = obj->floatval;
- xmlXPathReleaseObject(ctxt->context, obj);
- return(ret);
- }
- xmlChar *
- xmlXPathPopString (xmlXPathParserContextPtr ctxt) {
- xmlXPathObjectPtr obj;
- xmlChar * ret;
- obj = valuePop(ctxt);
- if (obj == NULL) {
- xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
- return(NULL);
- }
- ret = xmlXPathCastToString(obj);
-
- if (obj->stringval == ret)
- obj->stringval = NULL;
- xmlXPathReleaseObject(ctxt->context, obj);
- return(ret);
- }
- xmlNodeSetPtr
- xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt) {
- xmlXPathObjectPtr obj;
- xmlNodeSetPtr ret;
- if (ctxt == NULL) return(NULL);
- if (ctxt->value == NULL) {
- xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
- return(NULL);
- }
- if (!xmlXPathStackIsNodeSet(ctxt)) {
- xmlXPathSetTypeError(ctxt);
- return(NULL);
- }
- obj = valuePop(ctxt);
- ret = obj->nodesetval;
- #if 0
-
- if (obj->boolval && obj->user != NULL)
- xmlFreeNodeList((xmlNodePtr) obj->user);
- #endif
- obj->nodesetval = NULL;
- xmlXPathReleaseObject(ctxt->context, obj);
- return(ret);
- }
- void *
- xmlXPathPopExternal (xmlXPathParserContextPtr ctxt) {
- xmlXPathObjectPtr obj;
- void * ret;
- if ((ctxt == NULL) || (ctxt->value == NULL)) {
- xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
- return(NULL);
- }
- if (ctxt->value->type != XPATH_USERS) {
- xmlXPathSetTypeError(ctxt);
- return(NULL);
- }
- obj = valuePop(ctxt);
- ret = obj->user;
- obj->user = NULL;
- xmlXPathReleaseObject(ctxt->context, obj);
- return(ret);
- }
- #define CUR (*ctxt->cur)
- #define SKIP(val) ctxt->cur += (val)
- #define NXT(val) ctxt->cur[(val)]
- #define CUR_PTR ctxt->cur
- #define CUR_CHAR(l) xmlXPathCurrentChar(ctxt, &l)
- #define COPY_BUF(l,b,i,v) \
- if (l == 1) b[i++] = (xmlChar) v; \
- else i += xmlCopyChar(l,&b[i],v)
- #define NEXTL(l) ctxt->cur += l
- #define SKIP_BLANKS \
- while (IS_BLANK_CH(*(ctxt->cur))) NEXT
- #define CURRENT (*ctxt->cur)
- #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
- #ifndef DBL_DIG
- #define DBL_DIG 16
- #endif
- #ifndef DBL_EPSILON
- #define DBL_EPSILON 1E-9
- #endif
- #define UPPER_DOUBLE 1E9
- #define LOWER_DOUBLE 1E-5
- #define LOWER_DOUBLE_EXP 5
- #define INTEGER_DIGITS DBL_DIG
- #define FRACTION_DIGITS (DBL_DIG + 1 + (LOWER_DOUBLE_EXP))
- #define EXPONENT_DIGITS (3 + 2)
- static void
- xmlXPathFormatNumber(double number, char buffer[], int buffersize)
- {
- switch (xmlXPathIsInf(number)) {
- case 1:
- if (buffersize > (int)sizeof("Infinity"))
- snprintf(buffer, buffersize, "Infinity");
- break;
- case -1:
- if (buffersize > (int)sizeof("-Infinity"))
- snprintf(buffer, buffersize, "-Infinity");
- break;
- default:
- if (xmlXPathIsNaN(number)) {
- if (buffersize > (int)sizeof("NaN"))
- snprintf(buffer, buffersize, "NaN");
- } else if (number == 0 && xmlXPathGetSign(number) != 0) {
- snprintf(buffer, buffersize, "0");
- } else if (number == ((int) number)) {
- char work[30];
- char *ptr, *cur;
- int value = (int) number;
- ptr = &buffer[0];
- if (value == 0) {
- *ptr++ = '0';
- } else {
- snprintf(work, 29, "%d", value);
- cur = &work[0];
- while ((*cur) && (ptr - buffer < buffersize)) {
- *ptr++ = *cur++;
- }
- }
- if (ptr - buffer < buffersize) {
- *ptr = 0;
- } else if (buffersize > 0) {
- ptr--;
- *ptr = 0;
- }
- } else {
-
- char work[DBL_DIG + EXPONENT_DIGITS + 3 + LOWER_DOUBLE_EXP];
- int integer_place, fraction_place;
- char *ptr;
- char *after_fraction;
- double absolute_value;
- int size;
- absolute_value = fabs(number);
-
- if ( ((absolute_value > UPPER_DOUBLE) ||
- (absolute_value < LOWER_DOUBLE)) &&
- (absolute_value != 0.0) ) {
-
- integer_place = DBL_DIG + EXPONENT_DIGITS + 1;
- fraction_place = DBL_DIG - 1;
- size = snprintf(work, sizeof(work),"%*.*e",
- integer_place, fraction_place, number);
- while ((size > 0) && (work[size] != 'e')) size--;
- }
- else {
-
- if (absolute_value > 0.0) {
- integer_place = (int)log10(absolute_value);
- if (integer_place > 0)
- fraction_place = DBL_DIG - integer_place - 1;
- else
- fraction_place = DBL_DIG - integer_place;
- } else {
- fraction_place = 1;
- }
- size = snprintf(work, sizeof(work), "%0.*f",
- fraction_place, number);
- }
-
- after_fraction = work + size;
- ptr = after_fraction;
- while (*(--ptr) == '0')
- ;
- if (*ptr != '.')
- ptr++;
- while ((*ptr++ = *after_fraction++) != 0);
-
- size = strlen(work) + 1;
- if (size > buffersize) {
- work[buffersize - 1] = 0;
- size = buffersize;
- }
- memmove(buffer, work, size);
- }
- break;
- }
- }
- long
- xmlXPathOrderDocElems(xmlDocPtr doc) {
- long count = 0;
- xmlNodePtr cur;
- if (doc == NULL)
- return(-1);
- cur = doc->children;
- while (cur != NULL) {
- if (cur->type == XML_ELEMENT_NODE) {
- cur->content = (void *) (-(++count));
- if (cur->children != NULL) {
- cur = cur->children;
- continue;
- }
- }
- if (cur->next != NULL) {
- cur = cur->next;
- continue;
- }
- do {
- cur = cur->parent;
- if (cur == NULL)
- break;
- if (cur == (xmlNodePtr) doc) {
- cur = NULL;
- break;
- }
- if (cur->next != NULL) {
- cur = cur->next;
- break;
- }
- } while (cur != NULL);
- }
- return(count);
- }
- int
- xmlXPathCmpNodes(xmlNodePtr node1, xmlNodePtr node2) {
- int depth1, depth2;
- int attr1 = 0, attr2 = 0;
- xmlNodePtr attrNode1 = NULL, attrNode2 = NULL;
- xmlNodePtr cur, root;
- if ((node1 == NULL) || (node2 == NULL))
- return(-2);
-
- if (node1 == node2)
- return(0);
- if (node1->type == XML_ATTRIBUTE_NODE) {
- attr1 = 1;
- attrNode1 = node1;
- node1 = node1->parent;
- }
- if (node2->type == XML_ATTRIBUTE_NODE) {
- attr2 = 1;
- attrNode2 = node2;
- node2 = node2->parent;
- }
- if (node1 == node2) {
- if (attr1 == attr2) {
-
- if (attr1 != 0) {
- cur = attrNode2->prev;
- while (cur != NULL) {
- if (cur == attrNode1)
- return (1);
- cur = cur->prev;
- }
- return (-1);
- }
- return(0);
- }
- if (attr2 == 1)
- return(1);
- return(-1);
- }
- if ((node1->type == XML_NAMESPACE_DECL) ||
- (node2->type == XML_NAMESPACE_DECL))
- return(1);
- if (node1 == node2->prev)
- return(1);
- if (node1 == node2->next)
- return(-1);
-
- if ((node1->type == XML_ELEMENT_NODE) &&
- (node2->type == XML_ELEMENT_NODE) &&
- (0 > (long) node1->content) &&
- (0 > (long) node2->content) &&
- (node1->doc == node2->doc)) {
- long l1, l2;
- l1 = -((long) node1->content);
- l2 = -((long) node2->content);
- if (l1 < l2)
- return(1);
- if (l1 > l2)
- return(-1);
- }
-
- for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) {
- if (cur == node1)
- return(1);
- depth2++;
- }
- root = cur;
- for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) {
- if (cur == node2)
- return(-1);
- depth1++;
- }
-
- if (root != cur) {
- return(-2);
- }
-
- while (depth1 > depth2) {
- depth1--;
- node1 = node1->parent;
- }
- while (depth2 > depth1) {
- depth2--;
- node2 = node2->parent;
- }
- while (node1->parent != node2->parent) {
- node1 = node1->parent;
- node2 = node2->parent;
-
- if ((node1 == NULL) || (node2 == NULL))
- return(-2);
- }
-
- if (node1 == node2->prev)
- return(1);
- if (node1 == node2->next)
- return(-1);
-
- if ((node1->type == XML_ELEMENT_NODE) &&
- (node2->type == XML_ELEMENT_NODE) &&
- (0 > (long) node1->content) &&
- (0 > (long) node2->content) &&
- (node1->doc == node2->doc)) {
- long l1, l2;
- l1 = -((long) node1->content);
- l2 = -((long) node2->content);
- if (l1 < l2)
- return(1);
- if (l1 > l2)
- return(-1);
- }
- for (cur = node1->next;cur != NULL;cur = cur->next)
- if (cur == node2)
- return(1);
- return(-1);
- }
- #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON
- static int
- xmlXPathCmpNodesExt(xmlNodePtr node1, xmlNodePtr node2) {
- int depth1, depth2;
- int misc = 0, precedence1 = 0, precedence2 = 0;
- xmlNodePtr miscNode1 = NULL, miscNode2 = NULL;
- xmlNodePtr cur, root;
- long l1, l2;
- if ((node1 == NULL) || (node2 == NULL))
- return(-2);
- if (node1 == node2)
- return(0);
-
- switch (node1->type) {
- case XML_ELEMENT_NODE:
- if (node2->type == XML_ELEMENT_NODE) {
- if ((0 > (long) node1->content) &&
- (0 > (long) node2->content) &&
- (node1->doc == node2->doc))
- {
- l1 = -((long) node1->content);
- l2 = -((long) node2->content);
- if (l1 < l2)
- return(1);
- if (l1 > l2)
- return(-1);
- } else
- goto turtle_comparison;
- }
- break;
- case XML_ATTRIBUTE_NODE:
- precedence1 = 1;
- miscNode1 = node1;
- node1 = node1->parent;
- misc = 1;
- break;
- case XML_TEXT_NODE:
- case XML_CDATA_SECTION_NODE:
- case XML_COMMENT_NODE:
- case XML_PI_NODE: {
- miscNode1 = node1;
-
- if (node1->prev != NULL) {
- do {
- node1 = node1->prev;
- if (node1->type == XML_ELEMENT_NODE) {
- precedence1 = 3;
- break;
- }
- if (node1->prev == NULL) {
- precedence1 = 2;
-
- node1 = node1->parent;
- break;
- }
- } while (1);
- } else {
- precedence1 = 2;
- node1 = node1->parent;
- }
- if ((node1 == NULL) || (node1->type != XML_ELEMENT_NODE) ||
- (0 <= (long) node1->content)) {
-
- node1 = miscNode1;
- precedence1 = 0;
- } else
- misc = 1;
- }
- break;
- case XML_NAMESPACE_DECL:
-
- return(1);
- default:
- break;
- }
- switch (node2->type) {
- case XML_ELEMENT_NODE:
- break;
- case XML_ATTRIBUTE_NODE:
- precedence2 = 1;
- miscNode2 = node2;
- node2 = node2->parent;
- misc = 1;
- break;
- case XML_TEXT_NODE:
- case XML_CDATA_SECTION_NODE:
- case XML_COMMENT_NODE:
- case XML_PI_NODE: {
- miscNode2 = node2;
- if (node2->prev != NULL) {
- do {
- node2 = node2->prev;
- if (node2->type == XML_ELEMENT_NODE) {
- precedence2 = 3;
- break;
- }
- if (node2->prev == NULL) {
- precedence2 = 2;
- node2 = node2->parent;
- break;
- }
- } while (1);
- } else {
- precedence2 = 2;
- node2 = node2->parent;
- }
- if ((node2 == NULL) || (node2->type != XML_ELEMENT_NODE) ||
- (0 <= (long) node1->content))
- {
- node2 = miscNode2;
- precedence2 = 0;
- } else
- misc = 1;
- }
- break;
- case XML_NAMESPACE_DECL:
- return(1);
- default:
- break;
- }
- if (misc) {
- if (node1 == node2) {
- if (precedence1 == precedence2) {
-
- cur = miscNode2->prev;
- while (cur != NULL) {
- if (cur == miscNode1)
- return(1);
- if (cur->type == XML_ELEMENT_NODE)
- return(-1);
- cur = cur->prev;
- }
- return (-1);
- } else {
-
- if (precedence1 < precedence2)
- return(1);
- else
- return(-1);
- }
- }
-
- if ((precedence2 == 3) && (precedence1 > 1)) {
- cur = node1->parent;
- while (cur) {
- if (cur == node2)
- return(1);
- cur = cur->parent;
- }
- }
- if ((precedence1 == 3) && (precedence2 > 1)) {
- cur = node2->parent;
- while (cur) {
- if (cur == node1)
- return(-1);
- cur = cur->parent;
- }
- }
- }
-
- if ((node1->type == XML_ELEMENT_NODE) &&
- (node2->type == XML_ELEMENT_NODE) &&
- (0 > (long) node1->content) &&
- (0 > (long) node2->content) &&
- (node1->doc == node2->doc)) {
- l1 = -((long) node1->content);
- l2 = -((long) node2->content);
- if (l1 < l2)
- return(1);
- if (l1 > l2)
- return(-1);
- }
- turtle_comparison:
- if (node1 == node2->prev)
- return(1);
- if (node1 == node2->next)
- return(-1);
-
- for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) {
- if (cur == node1)
- return(1);
- depth2++;
- }
- root = cur;
- for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) {
- if (cur == node2)
- return(-1);
- depth1++;
- }
-
- if (root != cur) {
- return(-2);
- }
-
- while (depth1 > depth2) {
- depth1--;
- node1 = node1->parent;
- }
- while (depth2 > depth1) {
- depth2--;
- node2 = node2->parent;
- }
- while (node1->parent != node2->parent) {
- node1 = node1->parent;
- node2 = node2->parent;
-
- if ((node1 == NULL) || (node2 == NULL))
- return(-2);
- }
-
- if (node1 == node2->prev)
- return(1);
- if (node1 == node2->next)
- return(-1);
-
- if ((node1->type == XML_ELEMENT_NODE) &&
- (node2->type == XML_ELEMENT_NODE) &&
- (0 > (long) node1->content) &&
- (0 > (long) node2->content) &&
- (node1->doc == node2->doc)) {
- l1 = -((long) node1->content);
- l2 = -((long) node2->content);
- if (l1 < l2)
- return(1);
- if (l1 > l2)
- return(-1);
- }
- for (cur = node1->next;cur != NULL;cur = cur->next)
- if (cur == node2)
- return(1);
- return(-1);
- }
- #endif
- void
- xmlXPathNodeSetSort(xmlNodeSetPtr set) {
- int i, j, incr, len;
- xmlNodePtr tmp;
- if (set == NULL)
- return;
-
- len = set->nodeNr;
- for (incr = len / 2; incr > 0; incr /= 2) {
- for (i = incr; i < len; i++) {
- j = i - incr;
- while (j >= 0) {
- #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON
- if (xmlXPathCmpNodesExt(set->nodeTab[j],
- set->nodeTab[j + incr]) == -1)
- #else
- if (xmlXPathCmpNodes(set->nodeTab[j],
- set->nodeTab[j + incr]) == -1)
- #endif
- {
- tmp = set->nodeTab[j];
- set->nodeTab[j] = set->nodeTab[j + incr];
- set->nodeTab[j + incr] = tmp;
- j -= incr;
- } else
- break;
- }
- }
- }
- }
- #define XML_NODESET_DEFAULT 10
- static xmlNodePtr
- xmlXPathNodeSetDupNs(xmlNodePtr node, xmlNsPtr ns) {
- xmlNsPtr cur;
- if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL))
- return(NULL);
- if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
- return((xmlNodePtr) ns);
-
- cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
- if (cur == NULL) {
- xmlXPathErrMemory(NULL, "duplicating namespace\n");
- return(NULL);
- }
- memset(cur, 0, sizeof(xmlNs));
- cur->type = XML_NAMESPACE_DECL;
- if (ns->href != NULL)
- cur->href = xmlStrdup(ns->href);
- if (ns->prefix != NULL)
- cur->prefix = xmlStrdup(ns->prefix);
- cur->next = (xmlNsPtr) node;
- return((xmlNodePtr) cur);
- }
- void
- xmlXPathNodeSetFreeNs(xmlNsPtr ns) {
- if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL))
- return;
- if ((ns->next != NULL) && (ns->next->type != XML_NAMESPACE_DECL)) {
- if (ns->href != NULL)
- xmlFree((xmlChar *)ns->href);
- if (ns->prefix != NULL)
- xmlFree((xmlChar *)ns->prefix);
- xmlFree(ns);
- }
- }
- xmlNodeSetPtr
- xmlXPathNodeSetCreate(xmlNodePtr val) {
- xmlNodeSetPtr ret;
- ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet));
- if (ret == NULL) {
- xmlXPathErrMemory(NULL, "creating nodeset\n");
- return(NULL);
- }
- memset(ret, 0 , (size_t) sizeof(xmlNodeSet));
- if (val != NULL) {
- ret->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
- sizeof(xmlNodePtr));
- if (ret->nodeTab == NULL) {
- xmlXPathErrMemory(NULL, "creating nodeset\n");
- xmlFree(ret);
- return(NULL);
- }
- memset(ret->nodeTab, 0 ,
- XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
- ret->nodeMax = XML_NODESET_DEFAULT;
- if (val->type == XML_NAMESPACE_DECL) {
- xmlNsPtr ns = (xmlNsPtr) val;
- ret->nodeTab[ret->nodeNr++] =
- xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
- } else
- ret->nodeTab[ret->nodeNr++] = val;
- }
- return(ret);
- }
- static xmlNodeSetPtr
- xmlXPathNodeSetCreateSize(int size) {
- xmlNodeSetPtr ret;
- ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet));
- if (ret == NULL) {
- xmlXPathErrMemory(NULL, "creating nodeset\n");
- return(NULL);
- }
- memset(ret, 0 , (size_t) sizeof(xmlNodeSet));
- if (size < XML_NODESET_DEFAULT)
- size = XML_NODESET_DEFAULT;
- ret->nodeTab = (xmlNodePtr *) xmlMalloc(size * sizeof(xmlNodePtr));
- if (ret->nodeTab == NULL) {
- xmlXPathErrMemory(NULL, "creating nodeset\n");
- xmlFree(ret);
- return(NULL);
- }
- memset(ret->nodeTab, 0 , size * (size_t) sizeof(xmlNodePtr));
- ret->nodeMax = size;
- return(ret);
- }
- int
- xmlXPathNodeSetContains (xmlNodeSetPtr cur, xmlNodePtr val) {
- int i;
- if ((cur == NULL) || (val == NULL)) return(0);
- if (val->type == XML_NAMESPACE_DECL) {
- for (i = 0; i < cur->nodeNr; i++) {
- if (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) {
- xmlNsPtr ns1, ns2;
- ns1 = (xmlNsPtr) val;
- ns2 = (xmlNsPtr) cur->nodeTab[i];
- if (ns1 == ns2)
- return(1);
- if ((ns1->next != NULL) && (ns2->next == ns1->next) &&
- (xmlStrEqual(ns1->prefix, ns2->prefix)))
- return(1);
- }
- }
- } else {
- for (i = 0; i < cur->nodeNr; i++) {
- if (cur->nodeTab[i] == val)
- return(1);
- }
- }
- return(0);
- }
- void
- xmlXPathNodeSetAddNs(xmlNodeSetPtr cur, xmlNodePtr node, xmlNsPtr ns) {
- int i;
- if ((cur == NULL) || (ns == NULL) || (node == NULL) ||
- (ns->type != XML_NAMESPACE_DECL) ||
- (node->type != XML_ELEMENT_NODE))
- return;
-
-
- for (i = 0;i < cur->nodeNr;i++) {
- if ((cur->nodeTab[i] != NULL) &&
- (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) &&
- (((xmlNsPtr)cur->nodeTab[i])->next == (xmlNsPtr) node) &&
- (xmlStrEqual(ns->prefix, ((xmlNsPtr)cur->nodeTab[i])->prefix)))
- return;
- }
-
- if (cur->nodeMax == 0) {
- cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
- sizeof(xmlNodePtr));
- if (cur->nodeTab == NULL) {
- xmlXPathErrMemory(NULL, "growing nodeset\n");
- return;
- }
- memset(cur->nodeTab, 0 ,
- XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
- cur->nodeMax = XML_NODESET_DEFAULT;
- } else if (cur->nodeNr == cur->nodeMax) {
- xmlNodePtr *temp;
- cur->nodeMax *= 2;
- temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax *
- sizeof(xmlNodePtr));
- if (temp == NULL) {
- xmlXPathErrMemory(NULL, "growing nodeset\n");
- return;
- }
- cur->nodeTab = temp;
- }
- cur->nodeTab[cur->nodeNr++] = xmlXPathNodeSetDupNs(node, ns);
- }
- void
- xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) {
- int i;
- if ((cur == NULL) || (val == NULL)) return;
- #if 0
- if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' '))
- return;
- #endif
-
-
- for (i = 0;i < cur->nodeNr;i++)
- if (cur->nodeTab[i] == val) return;
-
- if (cur->nodeMax == 0) {
- cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
- sizeof(xmlNodePtr));
- if (cur->nodeTab == NULL) {
- xmlXPathErrMemory(NULL, "growing nodeset\n");
- return;
- }
- memset(cur->nodeTab, 0 ,
- XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
- cur->nodeMax = XML_NODESET_DEFAULT;
- } else if (cur->nodeNr == cur->nodeMax) {
- xmlNodePtr *temp;
- cur->nodeMax *= 2;
- temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax *
- sizeof(xmlNodePtr));
- if (temp == NULL) {
- xmlXPathErrMemory(NULL, "growing nodeset\n");
- return;
- }
- cur->nodeTab = temp;
- }
- if (val->type == XML_NAMESPACE_DECL) {
- xmlNsPtr ns = (xmlNsPtr) val;
- cur->nodeTab[cur->nodeNr++] =
- xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
- } else
- cur->nodeTab[cur->nodeNr++] = val;
- }
- void
- xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) {
- if ((cur == NULL) || (val == NULL)) return;
- #if 0
- if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' '))
- return;
- #endif
-
-
- if (cur->nodeMax == 0) {
- cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
- sizeof(xmlNodePtr));
- if (cur->nodeTab == NULL) {
- xmlXPathErrMemory(NULL, "growing nodeset\n");
- return;
- }
- memset(cur->nodeTab, 0 ,
- XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
- cur->nodeMax = XML_NODESET_DEFAULT;
- } else if (cur->nodeNr == cur->nodeMax) {
- xmlNodePtr *temp;
- cur->nodeMax *= 2;
- temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax *
- sizeof(xmlNodePtr));
- if (temp == NULL) {
- xmlXPathErrMemory(NULL, "growing nodeset\n");
- return;
- }
- cur->nodeTab = temp;
- }
- if (val->type == XML_NAMESPACE_DECL) {
- xmlNsPtr ns = (xmlNsPtr) val;
- cur->nodeTab[cur->nodeNr++] =
- xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
- } else
- cur->nodeTab[cur->nodeNr++] = val;
- }
- xmlNodeSetPtr
- xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) {
- int i, j, initNr, skip;
- xmlNodePtr n1, n2;
- if (val2 == NULL) return(val1);
- if (val1 == NULL) {
- val1 = xmlXPathNodeSetCreate(NULL);
- if (val1 == NULL)
- return (NULL);
- #if 0
-
-
- val1 = xmlXPathNodeSetCreateSize(val2->nodeNr);
- if (val1 == NULL)
- return(NULL);
- if (val2->nodeNr != 0) {
- if (val2->nodeNr == 1)
- *(val1->nodeTab) = *(val2->nodeTab);
- else {
- memcpy(val1->nodeTab, val2->nodeTab,
- val2->nodeNr * sizeof(xmlNodePtr));
- }
- val1->nodeNr = val2->nodeNr;
- }
- return(val1);
- #endif
- }
-
- initNr = val1->nodeNr;
- for (i = 0;i < val2->nodeNr;i++) {
- n2 = val2->nodeTab[i];
-
- skip = 0;
- for (j = 0; j < initNr; j++) {
- n1 = val1->nodeTab[j];
- if (n1 == n2) {
- skip = 1;
- break;
- } else if ((n1->type == XML_NAMESPACE_DECL) &&
- (n2->type == XML_NAMESPACE_DECL)) {
- if ((((xmlNsPtr) n1)->next == ((xmlNsPtr) n2)->next) &&
- (xmlStrEqual(((xmlNsPtr) n1)->prefix,
- ((xmlNsPtr) n2)->prefix)))
- {
- skip = 1;
- break;
- }
- }
- }
- if (skip)
- continue;
-
- if (val1->nodeMax == 0) {
- val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
- sizeof(xmlNodePtr));
- if (val1->nodeTab == NULL) {
- xmlXPathErrMemory(NULL, "merging nodeset\n");
- return(NULL);
- }
- memset(val1->nodeTab, 0 ,
- XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
- val1->nodeMax = XML_NODESET_DEFAULT;
- } else if (val1->nodeNr == val1->nodeMax) {
- xmlNodePtr *temp;
- val1->nodeMax *= 2;
- temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax *
- sizeof(xmlNodePtr));
- if (temp == NULL) {
- xmlXPathErrMemory(NULL, "merging nodeset\n");
- return(NULL);
- }
- val1->nodeTab = temp;
- }
- if (n2->type == XML_NAMESPACE_DECL) {
- xmlNsPtr ns = (xmlNsPtr) n2;
- val1->nodeTab[val1->nodeNr++] =
- xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
- } else
- val1->nodeTab[val1->nodeNr++] = n2;
- }
- return(val1);
- }
- #if 0
- static xmlNodeSetPtr
- xmlXPathNodeSetMergeUnique(xmlNodeSetPtr val1, xmlNodeSetPtr val2) {
- int i;
- if (val2 == NULL) return(val1);
- if (val1 == NULL) {
- val1 = xmlXPathNodeSetCreate(NULL);
- }
- if (val1 == NULL)
- return (NULL);
-
- for (i = 0;i < val2->nodeNr;i++) {
-
- if (val1->nodeMax == 0) {
- val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
- sizeof(xmlNodePtr));
- if (val1->nodeTab == NULL) {
- xmlXPathErrMemory(NULL, "merging nodeset\n");
- return(NULL);
- }
- memset(val1->nodeTab, 0 ,
- XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
- val1->nodeMax = XML_NODESET_DEFAULT;
- } else if (val1->nodeNr == val1->nodeMax) {
- xmlNodePtr *temp;
- val1->nodeMax *= 2;
- temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax *
- sizeof(xmlNodePtr));
- if (temp == NULL) {
- xmlXPathErrMemory(NULL, "merging nodeset\n");
- return(NULL);
- }
- val1->nodeTab = temp;
- }
- if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) {
- xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i];
- val1->nodeTab[val1->nodeNr++] =
- xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
- } else
- val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i];
- }
- return(val1);
- }
- #endif
- static xmlNodeSetPtr
- xmlXPathNodeSetMergeAndClear(xmlNodeSetPtr set1, xmlNodeSetPtr set2,
- int hasNullEntries)
- {
- if ((set1 == NULL) && (hasNullEntries == 0)) {
-
- set1 = xmlXPathNodeSetCreateSize(set2->nodeNr);
- if (set1 == NULL)
- return(NULL);
- if (set2->nodeNr != 0) {
- memcpy(set1->nodeTab, set2->nodeTab,
- set2->nodeNr * sizeof(xmlNodePtr));
- set1->nodeNr = set2->nodeNr;
- }
- } else {
- int i, j, initNbSet1;
- xmlNodePtr n1, n2;
- if (set1 == NULL)
- set1 = xmlXPathNodeSetCreate(NULL);
- if (set1 == NULL)
- return (NULL);
- initNbSet1 = set1->nodeNr;
- for (i = 0;i < set2->nodeNr;i++) {
- n2 = set2->nodeTab[i];
-
- if (n2 == NULL)
- continue;
-
- for (j = 0; j < initNbSet1; j++) {
- n1 = set1->nodeTab[j];
- if (n1 == n2) {
- goto skip_node;
- } else if ((n1->type == XML_NAMESPACE_DECL) &&
- (n2->type == XML_NAMESPACE_DECL))
- {
- if ((((xmlNsPtr) n1)->next == ((xmlNsPtr) n2)->next) &&
- (xmlStrEqual(((xmlNsPtr) n1)->prefix,
- ((xmlNsPtr) n2)->prefix)))
- {
-
- set2->nodeTab[i] = NULL;
- xmlXPathNodeSetFreeNs((xmlNsPtr) n2);
- goto skip_node;
- }
- }
- }
-
- if (set1->nodeMax == 0) {
- set1->nodeTab = (xmlNodePtr *) xmlMalloc(
- XML_NODESET_DEFAULT * sizeof(xmlNodePtr));
- if (set1->nodeTab == NULL) {
- xmlXPathErrMemory(NULL, "merging nodeset\n");
- return(NULL);
- }
- memset(set1->nodeTab, 0,
- XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
- set1->nodeMax = XML_NODESET_DEFAULT;
- } else if (set1->nodeNr >= set1->nodeMax) {
- xmlNodePtr *temp;
- set1->nodeMax *= 2;
- temp = (xmlNodePtr *) xmlRealloc(
- set1->nodeTab, set1->nodeMax * sizeof(xmlNodePtr));
- if (temp == NULL) {
- xmlXPathErrMemory(NULL, "merging nodeset\n");
- return(NULL);
- }
- set1->nodeTab = temp;
- }
- if (n2->type == XML_NAMESPACE_DECL) {
- xmlNsPtr ns = (xmlNsPtr) n2;
- set1->nodeTab[set1->nodeNr++] =
- xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
- } else
- set1->nodeTab[set1->nodeNr++] = n2;
- skip_node:
- {}
- }
- }
- set2->nodeNr = 0;
- return(set1);
- }
- static xmlNodeSetPtr
- xmlXPathNodeSetMergeAndClearNoDupls(xmlNodeSetPtr set1, xmlNodeSetPtr set2,
- int hasNullEntries)
- {
- if (set2 == NULL)
- return(set1);
- if ((set1 == NULL) && (hasNullEntries == 0)) {
-
- set1 = xmlXPathNodeSetCreateSize(set2->nodeNr);
- if (set1 == NULL)
- return(NULL);
- if (set2->nodeNr != 0) {
- memcpy(set1->nodeTab, set2->nodeTab,
- set2->nodeNr * sizeof(xmlNodePtr));
- set1->nodeNr = set2->nodeNr;
- }
- } else {
- int i;
- xmlNodePtr n2;
- if (set1 == NULL)
- set1 = xmlXPathNodeSetCreate(NULL);
- if (set1 == NULL)
- return (NULL);
- for (i = 0;i < set2->nodeNr;i++) {
- n2 = set2->nodeTab[i];
-
- if (n2 == NULL)
- continue;
- if (set1->nodeMax == 0) {
- set1->nodeTab = (xmlNodePtr *) xmlMalloc(
- XML_NODESET_DEFAULT * sizeof(xmlNodePtr));
- if (set1->nodeTab == NULL) {
- xmlXPathErrMemory(NULL, "merging nodeset\n");
- return(NULL);
- }
- memset(set1->nodeTab, 0,
- XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
- set1->nodeMax = XML_NODESET_DEFAULT;
- } else if (set1->nodeNr >= set1->nodeMax) {
- xmlNodePtr *temp;
- set1->nodeMax *= 2;
- temp = (xmlNodePtr *) xmlRealloc(
- set1->nodeTab, set1->nodeMax * sizeof(xmlNodePtr));
- if (temp == NULL) {
- xmlXPathErrMemory(NULL, "merging nodeset\n");
- return(NULL);
- }
- set1->nodeTab = temp;
- }
- set1->nodeTab[set1->nodeNr++] = n2;
- }
- }
- set2->nodeNr = 0;
- return(set1);
- }
- void
- xmlXPathNodeSetDel(xmlNodeSetPtr cur, xmlNodePtr val) {
- int i;
- if (cur == NULL) return;
- if (val == NULL) return;
-
- for (i = 0;i < cur->nodeNr;i++)
- if (cur->nodeTab[i] == val) break;
- if (i >= cur->nodeNr) {
- #ifdef DEBUG
- xmlGenericError(xmlGenericErrorContext,
- "xmlXPathNodeSetDel: Node %s wasn't found in NodeList\n",
- val->name);
- #endif
- return;
- }
- if ((cur->nodeTab[i] != NULL) &&
- (cur->nodeTab[i]->type == XML_NAMESPACE_DECL))
- xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[i]);
- cur->nodeNr--;
- for (;i < cur->nodeNr;i++)
- cur->nodeTab[i] = cur->nodeTab[i + 1];
- cur->nodeTab[cur->nodeNr] = NULL;
- }
- void
- xmlXPathNodeSetRemove(xmlNodeSetPtr cur, int val) {
- if (cur == NULL) return;
- if (val >= cur->nodeNr) return;
- if ((cur->nodeTab[val] != NULL) &&
- (cur->nodeTab[val]->type == XML_NAMESPACE_DECL))
- xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[val]);
- cur->nodeNr--;
- for (;val < cur->nodeNr;val++)
- cur->nodeTab[val] = cur->nodeTab[val + 1];
- cur->nodeTab[cur->nodeNr] = NULL;
- }
- void
- xmlXPathFreeNodeSet(xmlNodeSetPtr obj) {
- if (obj == NULL) return;
- if (obj->nodeTab != NULL) {
- int i;
-
- for (i = 0;i < obj->nodeNr;i++)
- if ((obj->nodeTab[i] != NULL) &&
- (obj->nodeTab[i]->type == XML_NAMESPACE_DECL))
- xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]);
- xmlFree(obj->nodeTab);
- }
- xmlFree(obj);
- }
- static void
- xmlXPathNodeSetClear(xmlNodeSetPtr set, int hasNsNodes)
- {
- if ((set == NULL) || (set->nodeNr <= 0))
- return;
- else if (hasNsNodes) {
- int i;
- xmlNodePtr node;
- for (i = 0; i < set->nodeNr; i++) {
- node = set->nodeTab[i];
- if ((node != NULL) &&
- (node->type == XML_NAMESPACE_DECL))
- xmlXPathNodeSetFreeNs((xmlNsPtr) node);
- }
- }
- set->nodeNr = 0;
- }
- static void
- xmlXPathNodeSetClearFromPos(xmlNodeSetPtr set, int pos, int hasNsNodes)
- {
- if ((set == NULL) || (set->nodeNr <= 0) || (pos >= set->nodeNr))
- return;
- else if ((hasNsNodes)) {
- int i;
- xmlNodePtr node;
- for (i = pos; i < set->nodeNr; i++) {
- node = set->nodeTab[i];
- if ((node != NULL) &&
- (node->type == XML_NAMESPACE_DECL))
- xmlXPathNodeSetFreeNs((xmlNsPtr) node);
- }
- }
- set->nodeNr = pos;
- }
- static void
- xmlXPathFreeValueTree(xmlNodeSetPtr obj) {
- int i;
- if (obj == NULL) return;
- if (obj->nodeTab != NULL) {
- for (i = 0;i < obj->nodeNr;i++) {
- if (obj->nodeTab[i] != NULL) {
- if (obj->nodeTab[i]->type == XML_NAMESPACE_DECL) {
- xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]);
- } else {
- xmlFreeNodeList(obj->nodeTab[i]);
- }
- }
- }
- xmlFree(obj->nodeTab);
- }
- xmlFree(obj);
- }
- #if defined(DEBUG) || defined(DEBUG_STEP)
- void
- xmlGenericErrorContextNodeSet(FILE *output, xmlNodeSetPtr obj) {
- int i;
- if (output == NULL) output = xmlGenericErrorContext;
- if (obj == NULL) {
- fprintf(output, "NodeSet == NULL !\n");
- return;
- }
- if (obj->nodeNr == 0) {
- fprintf(output, "NodeSet is empty\n");
- return;
- }
- if (obj->nodeTab == NULL) {
- fprintf(output, " nodeTab == NULL !\n");
- return;
- }
- for (i = 0; i < obj->nodeNr; i++) {
- if (obj->nodeTab[i] == NULL) {
- fprintf(output, " NULL !\n");
- return;
- }
- if ((obj->nodeTab[i]->type == XML_DOCUMENT_NODE) ||
- (obj->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE))
- fprintf(output, " /");
- else if (obj->nodeTab[i]->name == NULL)
- fprintf(output, " noname!");
- else fprintf(output, " %s", obj->nodeTab[i]->name);
- }
- fprintf(output, "\n");
- }
- #endif
- xmlXPathObjectPtr
- xmlXPathNewNodeSet(xmlNodePtr val) {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
- if (ret == NULL) {
- xmlXPathErrMemory(NULL, "creating nodeset\n");
- return(NULL);
- }
- memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
- ret->type = XPATH_NODESET;
- ret->boolval = 0;
- ret->nodesetval = xmlXPathNodeSetCreate(val);
-
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(NULL, XPATH_NODESET);
- #endif
- return(ret);
- }
- xmlXPathObjectPtr
- xmlXPathNewValueTree(xmlNodePtr val) {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
- if (ret == NULL) {
- xmlXPathErrMemory(NULL, "creating result value tree\n");
- return(NULL);
- }
- memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
- ret->type = XPATH_XSLT_TREE;
- ret->boolval = 1;
- ret->user = (void *) val;
- ret->nodesetval = xmlXPathNodeSetCreate(val);
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(NULL, XPATH_XSLT_TREE);
- #endif
- return(ret);
- }
- xmlXPathObjectPtr
- xmlXPathNewNodeSetList(xmlNodeSetPtr val)
- {
- xmlXPathObjectPtr ret;
- int i;
- if (val == NULL)
- ret = NULL;
- else if (val->nodeTab == NULL)
- ret = xmlXPathNewNodeSet(NULL);
- else {
- ret = xmlXPathNewNodeSet(val->nodeTab[0]);
- if (ret)
- for (i = 1; i < val->nodeNr; ++i)
- xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]);
- }
- return (ret);
- }
- xmlXPathObjectPtr
- xmlXPathWrapNodeSet(xmlNodeSetPtr val) {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
- if (ret == NULL) {
- xmlXPathErrMemory(NULL, "creating node set object\n");
- return(NULL);
- }
- memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
- ret->type = XPATH_NODESET;
- ret->nodesetval = val;
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(NULL, XPATH_NODESET);
- #endif
- return(ret);
- }
- void
- xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj) {
- if (obj == NULL) return;
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageReleased(NULL, obj->type);
- #endif
- xmlFree(obj);
- }
- xmlNodeSetPtr
- xmlXPathDifference (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
- xmlNodeSetPtr ret;
- int i, l1;
- xmlNodePtr cur;
- if (xmlXPathNodeSetIsEmpty(nodes2))
- return(nodes1);
- ret = xmlXPathNodeSetCreate(NULL);
- if (xmlXPathNodeSetIsEmpty(nodes1))
- return(ret);
- l1 = xmlXPathNodeSetGetLength(nodes1);
- for (i = 0; i < l1; i++) {
- cur = xmlXPathNodeSetItem(nodes1, i);
- if (!xmlXPathNodeSetContains(nodes2, cur))
- xmlXPathNodeSetAddUnique(ret, cur);
- }
- return(ret);
- }
- xmlNodeSetPtr
- xmlXPathIntersection (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
- xmlNodeSetPtr ret = xmlXPathNodeSetCreate(NULL);
- int i, l1;
- xmlNodePtr cur;
- if (ret == NULL)
- return(ret);
- if (xmlXPathNodeSetIsEmpty(nodes1))
- return(ret);
- if (xmlXPathNodeSetIsEmpty(nodes2))
- return(ret);
- l1 = xmlXPathNodeSetGetLength(nodes1);
- for (i = 0; i < l1; i++) {
- cur = xmlXPathNodeSetItem(nodes1, i);
- if (xmlXPathNodeSetContains(nodes2, cur))
- xmlXPathNodeSetAddUnique(ret, cur);
- }
- return(ret);
- }
- xmlNodeSetPtr
- xmlXPathDistinctSorted (xmlNodeSetPtr nodes) {
- xmlNodeSetPtr ret;
- xmlHashTablePtr hash;
- int i, l;
- xmlChar * strval;
- xmlNodePtr cur;
- if (xmlXPathNodeSetIsEmpty(nodes))
- return(nodes);
- ret = xmlXPathNodeSetCreate(NULL);
- if (ret == NULL)
- return(ret);
- l = xmlXPathNodeSetGetLength(nodes);
- hash = xmlHashCreate (l);
- for (i = 0; i < l; i++) {
- cur = xmlXPathNodeSetItem(nodes, i);
- strval = xmlXPathCastNodeToString(cur);
- if (xmlHashLookup(hash, strval) == NULL) {
- xmlHashAddEntry(hash, strval, strval);
- xmlXPathNodeSetAddUnique(ret, cur);
- } else {
- xmlFree(strval);
- }
- }
- xmlHashFree(hash, (xmlHashDeallocator) xmlFree);
- return(ret);
- }
- xmlNodeSetPtr
- xmlXPathDistinct (xmlNodeSetPtr nodes) {
- if (xmlXPathNodeSetIsEmpty(nodes))
- return(nodes);
- xmlXPathNodeSetSort(nodes);
- return(xmlXPathDistinctSorted(nodes));
- }
- int
- xmlXPathHasSameNodes (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
- int i, l;
- xmlNodePtr cur;
- if (xmlXPathNodeSetIsEmpty(nodes1) ||
- xmlXPathNodeSetIsEmpty(nodes2))
- return(0);
- l = xmlXPathNodeSetGetLength(nodes1);
- for (i = 0; i < l; i++) {
- cur = xmlXPathNodeSetItem(nodes1, i);
- if (xmlXPathNodeSetContains(nodes2, cur))
- return(1);
- }
- return(0);
- }
- xmlNodeSetPtr
- xmlXPathNodeLeadingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) {
- int i, l;
- xmlNodePtr cur;
- xmlNodeSetPtr ret;
- if (node == NULL)
- return(nodes);
- ret = xmlXPathNodeSetCreate(NULL);
- if (ret == NULL)
- return(ret);
- if (xmlXPathNodeSetIsEmpty(nodes) ||
- (!xmlXPathNodeSetContains(nodes, node)))
- return(ret);
- l = xmlXPathNodeSetGetLength(nodes);
- for (i = 0; i < l; i++) {
- cur = xmlXPathNodeSetItem(nodes, i);
- if (cur == node)
- break;
- xmlXPathNodeSetAddUnique(ret, cur);
- }
- return(ret);
- }
- xmlNodeSetPtr
- xmlXPathNodeLeading (xmlNodeSetPtr nodes, xmlNodePtr node) {
- xmlXPathNodeSetSort(nodes);
- return(xmlXPathNodeLeadingSorted(nodes, node));
- }
- xmlNodeSetPtr
- xmlXPathLeadingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
- if (xmlXPathNodeSetIsEmpty(nodes2))
- return(nodes1);
- return(xmlXPathNodeLeadingSorted(nodes1,
- xmlXPathNodeSetItem(nodes2, 1)));
- }
- xmlNodeSetPtr
- xmlXPathLeading (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
- if (xmlXPathNodeSetIsEmpty(nodes2))
- return(nodes1);
- if (xmlXPathNodeSetIsEmpty(nodes1))
- return(xmlXPathNodeSetCreate(NULL));
- xmlXPathNodeSetSort(nodes1);
- xmlXPathNodeSetSort(nodes2);
- return(xmlXPathNodeLeadingSorted(nodes1,
- xmlXPathNodeSetItem(nodes2, 1)));
- }
- xmlNodeSetPtr
- xmlXPathNodeTrailingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) {
- int i, l;
- xmlNodePtr cur;
- xmlNodeSetPtr ret;
- if (node == NULL)
- return(nodes);
- ret = xmlXPathNodeSetCreate(NULL);
- if (ret == NULL)
- return(ret);
- if (xmlXPathNodeSetIsEmpty(nodes) ||
- (!xmlXPathNodeSetContains(nodes, node)))
- return(ret);
- l = xmlXPathNodeSetGetLength(nodes);
- for (i = l - 1; i >= 0; i--) {
- cur = xmlXPathNodeSetItem(nodes, i);
- if (cur == node)
- break;
- xmlXPathNodeSetAddUnique(ret, cur);
- }
- xmlXPathNodeSetSort(ret);
- return(ret);
- }
- xmlNodeSetPtr
- xmlXPathNodeTrailing (xmlNodeSetPtr nodes, xmlNodePtr node) {
- xmlXPathNodeSetSort(nodes);
- return(xmlXPathNodeTrailingSorted(nodes, node));
- }
- xmlNodeSetPtr
- xmlXPathTrailingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
- if (xmlXPathNodeSetIsEmpty(nodes2))
- return(nodes1);
- return(xmlXPathNodeTrailingSorted(nodes1,
- xmlXPathNodeSetItem(nodes2, 0)));
- }
- xmlNodeSetPtr
- xmlXPathTrailing (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
- if (xmlXPathNodeSetIsEmpty(nodes2))
- return(nodes1);
- if (xmlXPathNodeSetIsEmpty(nodes1))
- return(xmlXPathNodeSetCreate(NULL));
- xmlXPathNodeSetSort(nodes1);
- xmlXPathNodeSetSort(nodes2);
- return(xmlXPathNodeTrailingSorted(nodes1,
- xmlXPathNodeSetItem(nodes2, 0)));
- }
- int
- xmlXPathRegisterFunc(xmlXPathContextPtr ctxt, const xmlChar *name,
- xmlXPathFunction f) {
- return(xmlXPathRegisterFuncNS(ctxt, name, NULL, f));
- }
- int
- xmlXPathRegisterFuncNS(xmlXPathContextPtr ctxt, const xmlChar *name,
- const xmlChar *ns_uri, xmlXPathFunction f) {
- if (ctxt == NULL)
- return(-1);
- if (name == NULL)
- return(-1);
- if (ctxt->funcHash == NULL)
- ctxt->funcHash = xmlHashCreate(0);
- if (ctxt->funcHash == NULL)
- return(-1);
- if (f == NULL)
- return(xmlHashRemoveEntry2(ctxt->funcHash, name, ns_uri, NULL));
- return(xmlHashAddEntry2(ctxt->funcHash, name, ns_uri, XML_CAST_FPTR(f)));
- }
- void
- xmlXPathRegisterFuncLookup (xmlXPathContextPtr ctxt,
- xmlXPathFuncLookupFunc f,
- void *funcCtxt) {
- if (ctxt == NULL)
- return;
- ctxt->funcLookupFunc = f;
- ctxt->funcLookupData = funcCtxt;
- }
- xmlXPathFunction
- xmlXPathFunctionLookup(xmlXPathContextPtr ctxt, const xmlChar *name) {
- if (ctxt == NULL)
- return (NULL);
- if (ctxt->funcLookupFunc != NULL) {
- xmlXPathFunction ret;
- xmlXPathFuncLookupFunc f;
- f = ctxt->funcLookupFunc;
- ret = f(ctxt->funcLookupData, name, NULL);
- if (ret != NULL)
- return(ret);
- }
- return(xmlXPathFunctionLookupNS(ctxt, name, NULL));
- }
- xmlXPathFunction
- xmlXPathFunctionLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name,
- const xmlChar *ns_uri) {
- xmlXPathFunction ret;
- if (ctxt == NULL)
- return(NULL);
- if (name == NULL)
- return(NULL);
- if (ctxt->funcLookupFunc != NULL) {
- xmlXPathFuncLookupFunc f;
- f = ctxt->funcLookupFunc;
- ret = f(ctxt->funcLookupData, name, ns_uri);
- if (ret != NULL)
- return(ret);
- }
- if (ctxt->funcHash == NULL)
- return(NULL);
- XML_CAST_FPTR(ret) = xmlHashLookup2(ctxt->funcHash, name, ns_uri);
- return(ret);
- }
- void
- xmlXPathRegisteredFuncsCleanup(xmlXPathContextPtr ctxt) {
- if (ctxt == NULL)
- return;
- xmlHashFree(ctxt->funcHash, NULL);
- ctxt->funcHash = NULL;
- }
- int
- xmlXPathRegisterVariable(xmlXPathContextPtr ctxt, const xmlChar *name,
- xmlXPathObjectPtr value) {
- return(xmlXPathRegisterVariableNS(ctxt, name, NULL, value));
- }
- int
- xmlXPathRegisterVariableNS(xmlXPathContextPtr ctxt, const xmlChar *name,
- const xmlChar *ns_uri,
- xmlXPathObjectPtr value) {
- if (ctxt == NULL)
- return(-1);
- if (name == NULL)
- return(-1);
- if (ctxt->varHash == NULL)
- ctxt->varHash = xmlHashCreate(0);
- if (ctxt->varHash == NULL)
- return(-1);
- if (value == NULL)
- return(xmlHashRemoveEntry2(ctxt->varHash, name, ns_uri,
- (xmlHashDeallocator)xmlXPathFreeObject));
- return(xmlHashUpdateEntry2(ctxt->varHash, name, ns_uri,
- (void *) value,
- (xmlHashDeallocator)xmlXPathFreeObject));
- }
- void
- xmlXPathRegisterVariableLookup(xmlXPathContextPtr ctxt,
- xmlXPathVariableLookupFunc f, void *data) {
- if (ctxt == NULL)
- return;
- ctxt->varLookupFunc = f;
- ctxt->varLookupData = data;
- }
- xmlXPathObjectPtr
- xmlXPathVariableLookup(xmlXPathContextPtr ctxt, const xmlChar *name) {
- if (ctxt == NULL)
- return(NULL);
- if (ctxt->varLookupFunc != NULL) {
- xmlXPathObjectPtr ret;
- ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc)
- (ctxt->varLookupData, name, NULL);
- return(ret);
- }
- return(xmlXPathVariableLookupNS(ctxt, name, NULL));
- }
- xmlXPathObjectPtr
- xmlXPathVariableLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name,
- const xmlChar *ns_uri) {
- if (ctxt == NULL)
- return(NULL);
- if (ctxt->varLookupFunc != NULL) {
- xmlXPathObjectPtr ret;
- ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc)
- (ctxt->varLookupData, name, ns_uri);
- if (ret != NULL) return(ret);
- }
- if (ctxt->varHash == NULL)
- return(NULL);
- if (name == NULL)
- return(NULL);
- return(xmlXPathCacheObjectCopy(ctxt, (xmlXPathObjectPtr)
- xmlHashLookup2(ctxt->varHash, name, ns_uri)));
- }
- void
- xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt) {
- if (ctxt == NULL)
- return;
- xmlHashFree(ctxt->varHash, (xmlHashDeallocator)xmlXPathFreeObject);
- ctxt->varHash = NULL;
- }
- int
- xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix,
- const xmlChar *ns_uri) {
- if (ctxt == NULL)
- return(-1);
- if (prefix == NULL)
- return(-1);
- if (prefix[0] == 0)
- return(-1);
- if (ctxt->nsHash == NULL)
- ctxt->nsHash = xmlHashCreate(10);
- if (ctxt->nsHash == NULL)
- return(-1);
- if (ns_uri == NULL)
- return(xmlHashRemoveEntry(ctxt->nsHash, prefix,
- (xmlHashDeallocator)xmlFree));
- return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) xmlStrdup(ns_uri),
- (xmlHashDeallocator)xmlFree));
- }
- const xmlChar *
- xmlXPathNsLookup(xmlXPathContextPtr ctxt, const xmlChar *prefix) {
- if (ctxt == NULL)
- return(NULL);
- if (prefix == NULL)
- return(NULL);
- #ifdef XML_XML_NAMESPACE
- if (xmlStrEqual(prefix, (const xmlChar *) "xml"))
- return(XML_XML_NAMESPACE);
- #endif
- if (ctxt->namespaces != NULL) {
- int i;
- for (i = 0;i < ctxt->nsNr;i++) {
- if ((ctxt->namespaces[i] != NULL) &&
- (xmlStrEqual(ctxt->namespaces[i]->prefix, prefix)))
- return(ctxt->namespaces[i]->href);
- }
- }
- return((const xmlChar *) xmlHashLookup(ctxt->nsHash, prefix));
- }
- void
- xmlXPathRegisteredNsCleanup(xmlXPathContextPtr ctxt) {
- if (ctxt == NULL)
- return;
- xmlHashFree(ctxt->nsHash, (xmlHashDeallocator)xmlFree);
- ctxt->nsHash = NULL;
- }
- xmlXPathObjectPtr
- xmlXPathNewFloat(double val) {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
- if (ret == NULL) {
- xmlXPathErrMemory(NULL, "creating float object\n");
- return(NULL);
- }
- memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
- ret->type = XPATH_NUMBER;
- ret->floatval = val;
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(NULL, XPATH_NUMBER);
- #endif
- return(ret);
- }
- xmlXPathObjectPtr
- xmlXPathNewBoolean(int val) {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
- if (ret == NULL) {
- xmlXPathErrMemory(NULL, "creating boolean object\n");
- return(NULL);
- }
- memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
- ret->type = XPATH_BOOLEAN;
- ret->boolval = (val != 0);
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(NULL, XPATH_BOOLEAN);
- #endif
- return(ret);
- }
- xmlXPathObjectPtr
- xmlXPathNewString(const xmlChar *val) {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
- if (ret == NULL) {
- xmlXPathErrMemory(NULL, "creating string object\n");
- return(NULL);
- }
- memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
- ret->type = XPATH_STRING;
- if (val != NULL)
- ret->stringval = xmlStrdup(val);
- else
- ret->stringval = xmlStrdup((const xmlChar *)"");
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(NULL, XPATH_STRING);
- #endif
- return(ret);
- }
- xmlXPathObjectPtr
- xmlXPathWrapString (xmlChar *val) {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
- if (ret == NULL) {
- xmlXPathErrMemory(NULL, "creating string object\n");
- return(NULL);
- }
- memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
- ret->type = XPATH_STRING;
- ret->stringval = val;
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(NULL, XPATH_STRING);
- #endif
- return(ret);
- }
- xmlXPathObjectPtr
- xmlXPathNewCString(const char *val) {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
- if (ret == NULL) {
- xmlXPathErrMemory(NULL, "creating string object\n");
- return(NULL);
- }
- memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
- ret->type = XPATH_STRING;
- ret->stringval = xmlStrdup(BAD_CAST val);
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(NULL, XPATH_STRING);
- #endif
- return(ret);
- }
- xmlXPathObjectPtr
- xmlXPathWrapCString (char * val) {
- return(xmlXPathWrapString((xmlChar *)(val)));
- }
- xmlXPathObjectPtr
- xmlXPathWrapExternal (void *val) {
- xmlXPathObjectPtr ret;
- ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
- if (ret == NULL) {
- xmlXPathErrMemory(NULL, "creating user object\n");
- return(NULL);
- }
- memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
- ret->type = XPATH_USERS;
- ret->user = val;
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(NULL, XPATH_USERS);
- #endif
- return(ret);
- }
- xmlXPathObjectPtr
- xmlXPathObjectCopy(xmlXPathObjectPtr val) {
- xmlXPathObjectPtr ret;
- if (val == NULL)
- return(NULL);
- ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
- if (ret == NULL) {
- xmlXPathErrMemory(NULL, "copying object\n");
- return(NULL);
- }
- memcpy(ret, val , (size_t) sizeof(xmlXPathObject));
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageRequested(NULL, val->type);
- #endif
- switch (val->type) {
- case XPATH_BOOLEAN:
- case XPATH_NUMBER:
- case XPATH_POINT:
- case XPATH_RANGE:
- break;
- case XPATH_STRING:
- ret->stringval = xmlStrdup(val->stringval);
- break;
- case XPATH_XSLT_TREE:
- #if 0
- if ((val->nodesetval != NULL) &&
- (val->nodesetval->nodeTab != NULL)) {
- xmlNodePtr cur, tmp;
- xmlDocPtr top;
- ret->boolval = 1;
- top = xmlNewDoc(NULL);
- top->name = (char *)
- xmlStrdup(val->nodesetval->nodeTab[0]->name);
- ret->user = top;
- if (top != NULL) {
- top->doc = top;
- cur = val->nodesetval->nodeTab[0]->children;
- while (cur != NULL) {
- tmp = xmlDocCopyNode(cur, top, 1);
- xmlAddChild((xmlNodePtr) top, tmp);
- cur = cur->next;
- }
- }
- ret->nodesetval = xmlXPathNodeSetCreate((xmlNodePtr) top);
- } else
- ret->nodesetval = xmlXPathNodeSetCreate(NULL);
-
- break;
- #endif
- case XPATH_NODESET:
- ret->nodesetval = xmlXPathNodeSetMerge(NULL, val->nodesetval);
-
- ret->boolval = 0;
- break;
- case XPATH_LOCATIONSET:
- #ifdef LIBXML_XPTR_ENABLED
- {
- xmlLocationSetPtr loc = val->user;
- ret->user = (void *) xmlXPtrLocationSetMerge(NULL, loc);
- break;
- }
- #endif
- case XPATH_USERS:
- ret->user = val->user;
- break;
- case XPATH_UNDEFINED:
- xmlGenericError(xmlGenericErrorContext,
- "xmlXPathObjectCopy: unsupported type %d\n",
- val->type);
- break;
- }
- return(ret);
- }
- void
- xmlXPathFreeObject(xmlXPathObjectPtr obj) {
- if (obj == NULL) return;
- if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) {
- if (obj->boolval) {
- #if 0
- if (obj->user != NULL) {
- xmlXPathFreeNodeSet(obj->nodesetval);
- xmlFreeNodeList((xmlNodePtr) obj->user);
- } else
- #endif
- obj->type = XPATH_XSLT_TREE;
- if (obj->nodesetval != NULL)
- xmlXPathFreeValueTree(obj->nodesetval);
- } else {
- if (obj->nodesetval != NULL)
- xmlXPathFreeNodeSet(obj->nodesetval);
- }
- #ifdef LIBXML_XPTR_ENABLED
- } else if (obj->type == XPATH_LOCATIONSET) {
- if (obj->user != NULL)
- xmlXPtrFreeLocationSet(obj->user);
- #endif
- } else if (obj->type == XPATH_STRING) {
- if (obj->stringval != NULL)
- xmlFree(obj->stringval);
- }
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageReleased(NULL, obj->type);
- #endif
- xmlFree(obj);
- }
- static void
- xmlXPathReleaseObject(xmlXPathContextPtr ctxt, xmlXPathObjectPtr obj)
- {
- #define XP_CACHE_ADD(sl, o) if (sl == NULL) { \
- sl = xmlPointerListCreate(10); if (sl == NULL) goto free_obj; } \
- if (xmlPointerListAddSize(sl, obj, 0) == -1) goto free_obj;
- #define XP_CACHE_WANTS(sl, n) ((sl == NULL) || ((sl)->number < n))
- if (obj == NULL)
- return;
- if ((ctxt == NULL) || (ctxt->cache == NULL)) {
- xmlXPathFreeObject(obj);
- } else {
- xmlXPathContextCachePtr cache =
- (xmlXPathContextCachePtr) ctxt->cache;
- switch (obj->type) {
- case XPATH_NODESET:
- case XPATH_XSLT_TREE:
- if (obj->nodesetval != NULL) {
- if (obj->boolval) {
-
- obj->type = XPATH_XSLT_TREE;
- xmlXPathFreeValueTree(obj->nodesetval);
- obj->nodesetval = NULL;
- } else if ((obj->nodesetval->nodeMax <= 40) &&
- (XP_CACHE_WANTS(cache->nodesetObjs,
- cache->maxNodeset)))
- {
- XP_CACHE_ADD(cache->nodesetObjs, obj);
- goto obj_cached;
- } else {
- xmlXPathFreeNodeSet(obj->nodesetval);
- obj->nodesetval = NULL;
- }
- }
- break;
- case XPATH_STRING:
- if (obj->stringval != NULL)
- xmlFree(obj->stringval);
- if (XP_CACHE_WANTS(cache->stringObjs, cache->maxString)) {
- XP_CACHE_ADD(cache->stringObjs, obj);
- goto obj_cached;
- }
- break;
- case XPATH_BOOLEAN:
- if (XP_CACHE_WANTS(cache->booleanObjs, cache->maxBoolean)) {
- XP_CACHE_ADD(cache->booleanObjs, obj);
- goto obj_cached;
- }
- break;
- case XPATH_NUMBER:
- if (XP_CACHE_WANTS(cache->numberObjs, cache->maxNumber)) {
- XP_CACHE_ADD(cache->numberObjs, obj);
- goto obj_cached;
- }
- break;
- #ifdef LIBXML_XPTR_ENABLED
- case XPATH_LOCATIONSET:
- if (obj->user != NULL) {
- xmlXPtrFreeLocationSet(obj->user);
- }
- goto free_obj;
- #endif
- default:
- goto free_obj;
- }
-
- if (XP_CACHE_WANTS(cache->miscObjs, cache->maxMisc)) {
- XP_CACHE_ADD(cache->miscObjs, obj);
- } else
- goto free_obj;
- obj_cached:
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageReleased(ctxt, obj->type);
- #endif
- if (obj->nodesetval != NULL) {
- xmlNodeSetPtr tmpset = obj->nodesetval;
-
- if (tmpset->nodeNr > 1) {
- int i;
- xmlNodePtr node;
- for (i = 0; i < tmpset->nodeNr; i++) {
- node = tmpset->nodeTab[i];
- if ((node != NULL) &&
- (node->type == XML_NAMESPACE_DECL))
- {
- xmlXPathNodeSetFreeNs((xmlNsPtr) node);
- }
- }
- } else if (tmpset->nodeNr == 1) {
- if ((tmpset->nodeTab[0] != NULL) &&
- (tmpset->nodeTab[0]->type == XML_NAMESPACE_DECL))
- xmlXPathNodeSetFreeNs((xmlNsPtr) tmpset->nodeTab[0]);
- }
- tmpset->nodeNr = 0;
- memset(obj, 0, sizeof(xmlXPathObject));
- obj->nodesetval = tmpset;
- } else
- memset(obj, 0, sizeof(xmlXPathObject));
- return;
- free_obj:
-
- if (obj->nodesetval != NULL)
- xmlXPathFreeNodeSet(obj->nodesetval);
- #ifdef XP_DEBUG_OBJ_USAGE
- xmlXPathDebugObjUsageReleased(NULL, obj->type);
- #endif
- xmlFree(obj);
- }
- return;
- }
- xmlChar *
- xmlXPathCastBooleanToString (int val) {
- xmlChar *ret;
- if (val)
- ret = xmlStrdup((const xmlChar *) "true");
- else
- ret = xmlStrdup((const xmlChar *) "false");
- return(ret);
- }
- xmlChar *
- xmlXPathCastNumberToString (double val) {
- xmlChar *ret;
- switch (xmlXPathIsInf(val)) {
- case 1:
- ret = xmlStrdup((const xmlChar *) "Infinity");
- break;
- case -1:
- ret = xmlStrdup((const xmlChar *) "-Infinity");
- break;
- default:
- if (xmlXPathIsNaN(val)) {
- ret = xmlStrdup((const xmlChar *) "NaN");
- } else if (val == 0 && xmlXPathGetSign(val) != 0) {
- ret = xmlStrdup((const xmlChar *) "0");
- } else {
-
- char buf[100];
- xmlXPathFormatNumber(val, buf, 99);
- buf[99] = 0;
- ret = xmlStrdup((const xmlChar *) buf);
- }
- }
- return(ret);
- }
- xmlChar *
- xmlXPathCastNodeToString (xmlNodePtr node) {
- xmlChar *ret;
- if ((ret = xmlNodeGetContent(node)) == NULL)
- ret = xmlStrdup((const xmlChar *) "");
- return(ret);
- }
- xmlChar *
- xmlXPathCastNodeSetToString (xmlNodeSetPtr ns) {
- if ((ns == NULL) || (ns->nodeNr == 0) || (ns->nodeTab == NULL))
- return(xmlStrdup((const xmlChar *) ""));
- if (ns->nodeNr > 1)
- xmlXPathNodeSetSort(ns);
- return(xmlXPathCastNodeToString(ns->nodeTab[0]));
- }
- xmlChar *
- xmlXPathCastToString(xmlXPathObjectPtr val) {
- xmlChar *ret = NULL;
- if (val == NULL)
- return(xmlStrdup((const xmlChar *) ""));
- switch (val->type) {
- case XPATH_UNDEFINED:
- #ifdef DEBUG_EXPR
- xmlGenericError(xmlGenericErrorContext, "String: undefined\n");
- #endif
- ret = xmlStrdup((const xmlChar *) "");
- break;
- case XPATH_NODESET:
- case XPATH_XSLT_TREE:
- ret = xmlXPathCastNodeSetToString(val->nodesetval);
- break;
- case XPATH_STRING:
- return(xmlStrdup(val->stringval));
- case XPATH_BOOLEAN:
- ret = xmlXPathCastBooleanToString(val->boolval);
- break;
- case XPATH_NUMBER: {
- ret = xmlXPathCastNumberToString(val->floatval);
- break;
- }
- case XPATH_USERS:
- case XPATH_POINT:
- case XPATH_RANGE:
- case XPATH_LOCATIONSET:
- TODO
- ret = xmlStrdup((const xmlChar *) "");
- break;
- }
- return(ret);
- }
- xmlXPathObjectPtr
- xmlXPathConvertString(xmlXPathObjectPtr val) {
- xmlChar *res = NULL;
- if (val == NULL)
- return(xmlXPathNewCString(""));
- switch (val->type) {
- case XPATH_UNDEFINED:
- #ifdef DEBUG_EXPR
- xmlGenericError(xmlGenericErrorContext, "STRING: undefined\n");
- #endif
- break;
- case XPATH_NODESET:
- case XPATH_XSLT_TREE:
- res = xmlXPathCastNodeSetToString(val->nodesetval);
- break;
- case XPATH_STRING:
- return(val);
- case XPATH_BOOLEAN:
- res = xmlXPathCastBooleanToString(val->boolval);
- break;
- case XPATH_NUMBER:
- res = xmlXPathCastNumberToString(val->floatval);
- break;
- case XPATH_USERS:
- case XPATH_POINT:
- case XPATH_RANGE:
- case XPATH_LOCATIONSET:
- TODO;
- break;
- }
- xmlXPathFreeObject(val);
- if (res == NULL)
- return(xmlXPathNewCString(""));
- return(xmlXPathWrapString(res));
- }
- double
- xmlXPathCastBooleanToNumber(int val) {
- if (val)
- return(1.0);
- return(0.0);
- }
- double
- xmlXPathCastStringToNumber(const xmlChar * val) {
- return(xmlXPathStringEvalNumber(val));
- }
- double
- xmlXPathCastNodeToNumber (xmlNodePtr node) {
- xmlChar *strval;
- double ret;
- if (node == NULL)
- return(xmlXPathNAN);
- strval = xmlXPathCastNodeToString(node);
- if (strval == NULL)
- return(xmlXPathNAN);
- ret = xmlXPathCastStringToNumber(strval);
- xmlFree(strval);
- return(ret);
- }
- double
- xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns) {
- xmlChar *str;
- double ret;
- if (ns == NULL)
- return(xmlXPathNAN);
- str = xmlXPathCastNodeSetToString(ns);
- ret = xmlXPathCastStringToNumber(str);
- xmlFree(str);
- return(ret);
- }
- double
- xmlXPathCastToNumber(xmlXPathObjectPtr val) {
- double ret = 0.0;
- if (val == NULL)
- return(xmlXPathNAN);
- switch (val->type) {
- case XPATH_UNDEFINED:
- #ifdef DEGUB_EXPR
- xmlGenericError(xmlGenericErrorContext, "NUMBER: undefined\n");
- #endif
- ret = xmlXPathNAN;
- break;
- case XPATH_NODESET:
- case XPATH_XSLT_TREE:
- ret = xmlXPathCastNodeSetToNumber(val->nodesetval);
- break;
- case XPATH_STRING:
- ret = xmlXPathCastStringToNumber(val->stringval);
- break;
- case XPATH_NUMBER:
- ret = val->floatval;
- break;
- case XPATH_BOOLEAN:
- ret = xmlXPathCastBooleanToNumber(val->boolval);
- break;
- case XPATH_USERS:
- case XPATH_POINT:
- case XPATH_RANGE:
- case XPATH_LOCATIONSET:
- TODO;
- ret = xmlXPathNAN;
- break;
- }
- return(ret);
- }
- xmlXPathObjectPtr
- xmlXPathConvertNumber(xmlXPathObjectPtr val) {
- xmlXPathObjectPtr ret;
- if (val == NULL)
- return(xmlXPathNewFloat(0.0));
- if (val->type == XPATH_NUMBER)
- return(val);
- ret = xmlXPathNewFloat(xmlXPathCastToNumber(val));
- xmlXPathFreeObject(val);
- return(ret);
- }
- int
- xmlXPathCastNumberToBoolean (double val) {
- if (xmlXPathIsNaN(val) || (val == 0.0))
- return(0);
- return(1);
- }
- int
- xmlXPathCastStringToBoolean (const xmlChar *val) {
- if ((val == NULL) || (xmlStrlen(val) == 0))
- return(0);
- return(1);
- }
- int
- xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns) {
- if ((ns == NULL) || (ns->nodeNr == 0))
- return(0);
- return(1);
- }
- int
- xmlXPathCastToBoolean (xmlXPathObjectPtr val) {
- int ret = 0;
- if (val == NULL)
- return(0);
- switch (val->type) {
- case XPATH_UNDEFINED:
- #ifdef DEBUG_EXPR
- xmlGenericError(xmlGenericErrorContext, "BOOLEAN: undefined\n");
- #endif
- ret = 0;
- break;
- case XPATH_NODESET:
- case XPATH_XSLT_TREE:
- ret = xmlXPathCastNodeSetToBoolean(val->nodesetval);
- break;
- case XPATH_STRING:
- ret = xmlXPathCastStringToBoolean(val->stringval);
- break;
- case XPATH_NUMBER:
- ret = xmlXPathCastNumberToBoolean(val->floatval);
- break;
- case XPATH_BOOLEAN:
- ret = val->boolval;
- break;
- case XPATH_USERS:
- case XPATH_POINT:
- case XPATH_RANGE:
- case XPATH_LOCATIONSET:
- TODO;
- ret = 0;
- break;
- }
- return(ret);
- }
- xmlXPathObjectPtr
- xmlXPathConvertBoolean(xmlXPathObjectPtr val) {
- xmlXPathObjectPtr ret;
- if (val == NULL)
- return(xmlXPathNewBoolean(0));
- if (val->type == XPATH_BOOLEAN)
- return(val);
- ret = xmlXPathNewBoolean(xmlXPathCastToBoolean(val));
- xmlXPathFreeObject(val);
- return(ret);
- }
- xmlXPathContextPtr
- xmlXPathNewContext(xmlDocPtr doc) {
- xmlXPathContextPtr ret;
- ret = (xmlXPathContextPtr) xmlMalloc(sizeof(xmlXPathContext));
- if (ret == NULL) {
- xmlXPathErrMemory(NULL, "creating context\n");
- return(NULL);
- }
- memset(ret, 0 , (size_t) sizeof(xmlXPathContext));
- ret->doc = doc;
- ret->node = NULL;
- ret->varHash = NULL;
- ret->nb_types = 0;
- ret->max_types = 0;
- ret->types = NULL;
- ret->funcHash = xmlHashCreate(0);
- ret->nb_axis = 0;
- ret->max_axis = 0;
- ret->axis = NULL;
- ret->nsHash = NULL;
- ret->user = NULL;
- ret->contextSize = -1;
- ret->proximityPosition = -1;
- #ifdef XP_DEFAULT_CACHE_ON
- if (xmlXPathContextSetCache(ret, 1, -1, 0) == -1) {
- xmlXPathFreeContext(ret);
- return(NULL);
- }
- #endif
- xmlXPathRegisterAllFunctions(ret);
- return(ret);
- }
- void
- xmlXPathFreeContext(xmlXPathContextPtr ctxt) {
- if (ctxt == NULL) return;
- if (ctxt->cache != NULL)
- xmlXPathFreeCache((xmlXPathContextCachePtr) ctxt->cache);
- xmlXPathRegisteredNsCleanup(ctxt);
- xmlXPathRegisteredFuncsCleanup(ctxt);
- xmlXPathRegisteredVariablesCleanup(ctxt);
- xmlResetError(&ctxt->lastError);
- xmlFree(ctxt);
- }
- #define CHECK_CTXT(ctxt) \
- if (ctxt == NULL) { \
- __xmlRaiseError(NULL, NULL, NULL, \
- NULL, NULL, XML_FROM_XPATH, \
- XML_ERR_INTERNAL_ERROR, XML_ERR_FATAL, \
- __FILE__, __LINE__, \
- NULL, NULL, NULL, 0, 0, \
- "NULL context pointer\n"); \
- return(NULL); \
- } \
- #define CHECK_CTXT_NEG(ctxt) \
- if (ctxt == NULL) { \
- __xmlRaiseError(NULL, NULL, NULL, \
- NULL, NULL, XML_FROM_XPATH, \
- XML_ERR_INTERNAL_ERROR, XML_ERR_FATAL, \
- __FILE__, __LINE__, \
- NULL, NULL, NULL, 0, 0, \
- "NULL context pointer\n"); \
- return(-1); \
- } \
- #define CHECK_CONTEXT(ctxt) \
- if ((ctxt == NULL) || (ctxt->doc == NULL) || \
- (ctxt->doc->children == NULL)) { \
- xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_INVALID_CTXT); \
- return(NULL); \
- }
- xmlXPathParserContextPtr
- xmlXPathNewParserContext(const xmlChar *str, xmlXPathContextPtr ctxt) {
- xmlXPathParserContextPtr ret;
- ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext));
- if (ret == NULL) {
- xmlXPathErrMemory(ctxt, "creating parser context\n");
- return(NULL);
- }
- memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext));
- ret->cur = ret->base = str;
- ret->context = ctxt;
- ret->comp = xmlXPathNewCompExpr();
- if (ret->comp == NULL) {
- xmlFree(ret->valueTab);
- xmlFree(ret);
- return(NULL);
- }
- if ((ctxt != NULL) && (ctxt->dict != NULL)) {
- ret->comp->dict = ctxt->dict;
- xmlDictReference(ret->comp->dict);
- }
- return(ret);
- }
- static xmlXPathParserContextPtr
- xmlXPathCompParserContext(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctxt) {
- xmlXPathParserContextPtr ret;
- ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext));
- if (ret == NULL) {
- xmlXPathErrMemory(ctxt, "creating evaluation context\n");
- return(NULL);
- }
- memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext));
-
- ret->valueTab = (xmlXPathObjectPtr *)
- xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
- if (ret->valueTab == NULL) {
- xmlFree(ret);
- xmlXPathErrMemory(ctxt, "creating evaluation context\n");
- return(NULL);
- }
- ret->valueNr = 0;
- ret->valueMax = 10;
- ret->value = NULL;
- ret->context = ctxt;
- ret->comp = comp;
- return(ret);
- }
- void
- xmlXPathFreeParserContext(xmlXPathParserContextPtr ctxt) {
- if (ctxt->valueTab != NULL) {
- xmlFree(ctxt->valueTab);
- }
- if (ctxt->comp != NULL) {
- #ifdef XPATH_STREAMING
- if (ctxt->comp->stream != NULL) {
- xmlFreePatternList(ctxt->comp->stream);
- ctxt->comp->stream = NULL;
- }
- #endif
- xmlXPathFreeCompExpr(ctxt->comp);
- }
- xmlFree(ctxt);
- }
- static unsigned int
- xmlXPathNodeValHash(xmlNodePtr node) {
- int len = 2;
- const xmlChar * string = NULL;
- xmlNodePtr tmp = NULL;
- unsigned int ret = 0;
- if (node == NULL)
- return(0);
- if (node->type == XML_DOCUMENT_NODE) {
- tmp = xmlDocGetRootElement((xmlDocPtr) node);
- if (tmp == NULL)
- node = node->children;
- else
- node = tmp;
- if (node == NULL)
- return(0);
- }
- switch (node->type) {
- case XML_COMMENT_NODE:
- case XML_PI_NODE:
- case XML_CDATA_SECTION_NODE:
- case XML_TEXT_NODE:
- string = node->content;
- if (string == NULL)
- return(0);
- if (string[0] == 0)
- return(0);
- return(((unsigned int) string[0]) +
- (((unsigned int) string[1]) << 8));
- case XML_NAMESPACE_DECL:
- string = ((xmlNsPtr)node)->href;
- if (string == NULL)
- return(0);
- if (string[0] == 0)
- return(0);
- return(((unsigned int) string[0]) +
- (((unsigned int) string[1]) << 8));
- case XML_ATTRIBUTE_NODE:
- tmp = ((xmlAttrPtr) node)->children;
- break;
- case XML_ELEMENT_NODE:
- tmp = node->children;
- break;
- default:
- return(0);
- }
- while (tmp != NULL) {
- switch (tmp->type) {
- case XML_COMMENT_NODE:
- case XML_PI_NODE:
- case XML_CDATA_SECTION_NODE:
- case XML_TEXT_NODE:
- string = tmp->content;
- break;
- case XML_NAMESPACE_DECL:
- string = ((xmlNsPtr)tmp)->href;
- break;
- default:
- break;
- }
- if ((string != NULL) && (string[0] != 0)) {
- if (len == 1) {
- return(ret + (((unsigned int) string[0]) << 8));
- }
- if (string[1] == 0) {
- len = 1;
- ret = (unsigned int) string[0];
- } else {
- return(((unsigned int) string[0]) +
- (((unsigned int) string[1]) << 8));
- }
- }
-
- if ((tmp->children != NULL) && (tmp->type != XML_DTD_NODE)) {
- if (tmp->children->type != XML_ENTITY_DECL) {
- tmp = tmp->children;
- continue;
- }
- }
- if (tmp == node)
- break;
- if (tmp->next != NULL) {
- tmp = tmp->next;
- continue;
- }
- do {
- tmp = tmp->parent;
- if (tmp == NULL)
- break;
- if (tmp == node) {
- tmp = NULL;
- break;
- }
- if (tmp->next != NULL) {
- tmp = tmp->next;
- break;
- }
- } while (tmp != NULL);
- }
- return(ret);
- }
- static unsigned int
- xmlXPathStringHash(const xmlChar * string) {
- if (string == NULL)
- return((unsigned int) 0);
- if (string[0] == 0)
- return(0);
- return(((unsigned int) string[0]) +
- (((unsigned int) string[1]) << 8));
- }
- static int
- xmlXPathCompareNodeSetFloat(xmlXPathParserContextPtr ctxt, int inf, int strict,
- xmlXPathObjectPtr arg, xmlXPathObjectPtr f) {
- int i, ret = 0;
- xmlNodeSetPtr ns;
- xmlChar *str2;
- if ((f == NULL) || (arg == NULL) ||
- ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) {
- xmlXPathReleaseObject(ctxt->context, arg);
- xmlXPathReleaseObject(ctxt->context, f);
- return(0);
- }
- ns = arg->nodesetval;
- if (ns != NULL) {
- for (i = 0;i < ns->nodeNr;i++) {
- str2 = xmlXPathCastNodeToString(ns->nodeTab[i]);
- if (str2 != NULL) {
- valuePush(ctxt,
- xmlXPathCacheNewString(ctxt->context, str2));
- xmlFree(str2);
- xmlXPathNumberFunction(ctxt, 1);
- valuePush(ctxt, xmlXPathCacheObjectCopy(ctxt->context, f));
- ret = xmlXPathCompareValues(ctxt, inf, strict);
- if (ret)
- break;
- }
- }
- }
- xmlXPathReleaseObject(ctxt->context, arg);
- xmlXPathReleaseObject(ctxt->context, f);
- return(ret);
- }
- static int
- xmlXPathCompareNodeSetString(xmlXPathParserContextPtr ctxt, int inf, int strict,
- xmlXPathObjectPtr arg, xmlXPathObjectPtr s) {
- int i, ret = 0;
- xmlNodeSetPtr ns;
- xmlChar *str2;
- if ((s == NULL) || (arg == NULL) ||
- ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) {
- xmlXPathReleaseObject(ctxt->context, arg);
- xmlXPathReleaseObject(ctxt->context, s);
- return(0);
- }
- ns = arg->nodesetval;
- if (ns != NULL) {
- for (i = 0;i < ns->nodeNr;i++) {
- str2 = xmlXPathCastNodeToString(ns->nodeTab[i]);
- if (str2 != NULL) {
- valuePush(ctxt,
- xmlXPathCacheNewString(ctxt->context, str2));
- xmlFree(str2);
- valuePush(ctxt, xmlXPathCacheObjectCopy(ctxt->context, s));
- ret = xmlXPathCompareValues(ctxt, inf, strict);
- if (ret)
- break;
- }
- }
- }
- xmlXPathReleaseObject(ctxt->context, arg);
- xmlXPathReleaseObject(ctxt->context, s);
- return(ret);
- }
- static int
- xmlXPathCompareNodeSets(int inf, int strict,
- xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) {
- int i, j, init = 0;
- double val1;
- double *values2;
- int ret = 0;
- xmlNodeSetPtr ns1;
- xmlNodeSetPtr ns2;
- if ((arg1 == NULL) ||
- ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) {
- xmlXPathFreeObject(arg2);
- return(0);
- }
- if ((arg2 == NULL) ||
- ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) {
- xmlXPathFreeObject(arg1);
- xmlXPathFreeObject(arg2);
- return(0);
- }
- ns1 = arg1->nodesetval;
- ns2 = arg2->nodesetval;
- if ((ns1 == NULL) || (ns1->nodeNr <= 0)) {
- xmlXPathFreeObject(arg1);
- xmlXPathFreeObject(arg2);
- return(0);
- }
- if ((ns2 == NULL) || (ns2->nodeNr <= 0)) {
- xmlXPathFreeObject(arg1);
- xmlXPathFreeObject(arg2);
- return(0);
- }
- values2 = (double *) xmlMalloc(ns2->nodeNr * sizeof(double));
- if (values2 == NULL) {
- xmlXPathErrMemory(NULL, "comparing nodesets\n");
- xmlXPathFreeObject(arg1);
- xmlXPathFreeObject(arg2);
- return(0);
- }
- for (i = 0;i < ns1->nodeNr;i++) {
- val1 = xmlXPathCastNodeToNumber(ns1->nodeTab[i]);
- if (xmlXPathIsNaN(val1))
- continue;
- for (j = 0;j < ns2->nodeNr;j++) {
- if (init == 0) {
- values2[j] = xmlXPathCastNodeToNumber(ns2->nodeTab[j]);
- }
- if (xmlXPathIsNaN(values2[j]))
- continue;
- if (inf && strict)
- ret = (val1 < values2[j]);
- else if (inf && !strict)
- ret = (val1 <= values2[j]);
- else if (!inf && strict)
- ret = (val1 > values2[j]);
- else if (!inf && !strict)
- ret = (val1 >= values2[j]);
- if (ret)
- break;
- }
- if (ret)
- break;
- init = 1;
- }
- xmlFree(values2);
- xmlXPathFreeObject(arg1);
- xmlXPathFreeObject(arg2);
- return(ret);
- }
- static int
- xmlXPathCompareNodeSetValue(xmlXPathParserContextPtr ctxt, int inf, int strict,
- xmlXPathObjectPtr arg, xmlXPathObjectPtr val) {
- if ((val == NULL) || (arg == NULL) ||
- ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE)))
- return(0);
- switch(val->type) {
- case XPATH_NUMBER:
- return(xmlXPathCompareNodeSetFloat(ctxt, inf, strict, arg, val));
- case XPATH_NODESET:
- case XPATH_XSLT_TREE:
- return(xmlXPathCompareNodeSets(inf, strict, arg, val));
- case XPATH_STRING:
- return(xmlXPathCompareNodeSetString(ctxt, inf, strict, arg, val));
- case XPATH_BOOLEAN:
- valuePush(ctxt, arg);
- xmlXPathBooleanFunction(ctxt, 1);
- valuePush(ctxt, val);
- return(xmlXPathCompareValues(ctxt, inf, strict));
- default:
- TODO
- }
- return(0);
- }
- static int
- xmlXPathEqualNodeSetString(xmlXPathObjectPtr arg, const xmlChar * str, int neq)
- {
- int i;
- xmlNodeSetPtr ns;
- xmlChar *str2;
- unsigned int hash;
- if ((str == NULL) || (arg == NULL) ||
- ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE)))
- return (0);
- ns = arg->nodesetval;
-
- if ((ns == NULL) || (ns->nodeNr <= 0) )
- return (0);
- hash = xmlXPathStringHash(str);
- for (i = 0; i < ns->nodeNr; i++) {
- if (xmlXPathNodeValHash(ns->nodeTab[i]) == hash) {
- str2 = xmlNodeGetContent(ns->nodeTab[i]);
- if ((str2 != NULL) && (xmlStrEqual(str, str2))) {
- xmlFree(str2);
- if (neq)
- continue;
- return (1);
- } else if ((str2 == NULL) && (xmlStrEqual(str, BAD_CAST ""))) {
- if (neq)
- continue;
- return (1);
- } else if (neq) {
- if (str2 != NULL)
- xmlFree(str2);
- return (1);
- }
- if (str2 != NULL)
- xmlFree(str2);
- } else if (neq)
- return (1);
- }
- return (0);
- }
- static int
- xmlXPathEqualNodeSetFloat(xmlXPathParserContextPtr ctxt,
- xmlXPathObjectPtr arg, double f, int neq) {
- int i, ret=0;
- xmlNodeSetPtr ns;
- xmlChar *str2;
- xmlXPathObjectPtr val;
- double v;
- if ((arg == NULL) ||
- ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE)))
- return(0);
- ns = arg->nodesetval;
- if (ns != NULL) {
- for (i=0;i<ns->nodeNr;i++) {
- str2 = xmlXPathCastNodeToString(ns->nodeTab[i]);
- if (str2 != NULL) {
- valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, str2));
- xmlFree(str2);
- xmlXPathNumberFunction(ctxt, 1);
- val = valuePop(ctxt);
- v = val->floatval;
- xmlXPathReleaseObject(ctxt->context, val);
- if (!xmlXPathIsNaN(v)) {
- if ((!neq) && (v==f)) {
- ret = 1;
- break;
- } else if ((neq) && (v!=f)) {
- ret = 1;
- break;
- }
- } else {
- if (neq)
- ret = 1;
- }
- }
- }
- }
- return(ret);
- }
- static int
- xmlXPathEqualNodeSets(xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2, int neq) {
- int i, j;
- unsigned int *hashs1;
- unsigned int *hashs2;
- xmlChar **values1;
- xmlChar **values2;
- int ret = 0;
- xmlNodeSetPtr ns1;
- xmlNodeSetPtr ns2;
- if ((arg1 == NULL) ||
- ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)))
- return(0);
- if ((arg2 == NULL) ||
- ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE)))
- return(0);
- ns1 = arg1->nodesetval;
- ns2 = arg2->nodesetval;
- if ((ns1 == NULL) || (ns1->nodeNr <= 0))
- return(0);
- if ((ns2 == NULL) || (ns2->nodeNr <= 0))
- return(0);
-
- if (neq == 0)
- for (i = 0;i < ns1->nodeNr;i++)
- for (j = 0;j < ns2->nodeNr;j++)
- if (ns1->nodeTab[i] == ns2->nodeTab[j])
- return(1);
- values1 = (xmlChar **) xmlMalloc(ns1->nodeNr * sizeof(xmlChar *));
- if (values1 == NULL) {
- xmlXPathErrMemory(NULL, "comparing nodesets\n");
- return(0);
- }
- hashs1 = (unsigned int *) xmlMalloc(ns1->nodeNr * sizeof(unsigned int));
- if (hashs1 == NULL) {
- xmlXPathErrMemory(NULL, "comparing nodesets\n");
- xmlFree(values1);
- return(0);
- }
- memset(values1, 0, ns1->nodeNr * sizeof(xmlChar *));
- values2 = (xmlChar **) xmlMalloc(ns2->nodeNr * sizeof(xmlChar *));
- if (values2 == NULL) {
- xmlXPathErrMemory(NULL, "comparing nodesets\n");
- xmlFree(hashs1);
- xmlFree(values1);
- return(0);
- }
- hashs2 = (unsigned int *) xmlMalloc(ns2->nodeNr * sizeof(unsigned int));
- if (hashs2 == NULL) {
- xmlXPathErrMemory(NULL, "comparing nodesets\n");
- xmlFree(hashs1);
- xmlFree(values1);
- xmlFree(values2);
- return(0);
- }
- memset(values2, 0, ns2->nodeNr * sizeof(xmlChar *));
- for (i = 0;i < ns1->nodeNr;i++) {
- hashs1[i] = xmlXPathNodeValHash(ns1->nodeTab[i]);
- for (j = 0;j < ns2->nodeNr;j++) {
- if (i == 0)
- hashs2[j] = xmlXPathNodeValHash(ns2->nodeTab[j]);
- if (hashs1[i] != hashs2[j]) {
- if (neq) {
- ret = 1;
- break;
- }
- }
- else {
- if (values1[i] == NULL)
- values1[i] = xmlNodeGetContent(ns1->nodeTab[i]);
- if (values2[j] == NULL)
- values2[j] = xmlNodeGetContent(ns2->nodeTab[j]);
- ret = xmlStrEqual(values1[i], values2[j]) ^ neq;
- if (ret)
- break;
- }
- }
- if (ret)
- break;
- }
- for (i = 0;i < ns1->nodeNr;i++)
- if (values1[i] != NULL)
- xmlFree(values1[i]);
- for (j = 0;j < ns2->nodeNr;j++)
- if (values2[j] != NULL)
- xmlFree(values2[j]);
- xmlFree(values1);
- xmlFree(values2);
- xmlFree(hashs1);
- xmlFree(hashs2);
- return(ret);
- }
- static int
- xmlXPathEqualValuesCommon(xmlXPathParserContextPtr ctxt,
- xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) {
- int ret = 0;
-
- switch (arg1->type) {
- case XPATH_UNDEFINED:
- #ifdef DEBUG_EXPR
- xmlGenericError(xmlGenericErrorContext,
- "Equal: undefined\n");
- #endif
- break;
- case XPATH_BOOLEAN:
- switch (arg2->type) {
- case XPATH_UNDEFINED:
- #ifdef DEBUG_EXPR
- xmlGenericError(xmlGenericErrorContext,
- "Equal: undefined\n");
- #endif
- break;
- case XPATH_BOOLEAN:
- #ifdef DEBUG_EXPR
- xmlGenericError(xmlGenericErrorContext,
- "Equal: %d boolean %d \n",
- arg1->boolval, arg2->boolval);
- #endif
- ret = (arg1->boolval == arg2->boolval);
- break;
- case XPATH_NUMBER:
- ret = (arg1->boolval ==
- xmlXPathCastNumberToBoolean(arg2->floatval));
- break;
- case XPATH_STRING:
- if ((arg2->stringval == NULL) ||
- (arg2->stringval[0] == 0)) ret = 0;
- else
- ret = 1;
- ret = (arg1->boolval == ret);
- break;
- case XPATH_USERS:
- case XPATH_POINT:
- case XPATH_RANGE:
- case XPATH_LOCATIONSET:
- TODO
- break;
- case XPATH_NODESET:
- case XPATH_XSLT_TREE:
- break;
- }
- break;
- case XPATH_NUMBER:
- switch (arg2->type) {
- case XPATH_UNDEFINED:
- #ifdef DEBUG_EXPR
- xmlGenericError(xmlGenericErrorContext,
- "Equal: undefined\n");
- #endif
- break;
- case XPATH_BOOLEAN:
- ret = (arg2->boolval==
- xmlXPathCastNumberToBoolean(arg1->floatval));
- break;
- case XPATH_STRING:
- valuePush(ctxt, arg2);
- xmlXPathNumberFunction(ctxt, 1);
- arg2 = valuePop(ctxt);
-
- case XPATH_NUMBER:
-
- if (xmlXPathIsNaN(arg1->floatval) ||
- xmlXPathIsNaN(arg2->floatval)) {
- ret = 0;
- } else if (xmlXPathIsInf(arg1->floatval) == 1) {
- if (xmlXPathIsInf(arg2->floatval) == 1)
- ret = 1;
- else
- ret = 0;
- } else if (xmlXPathIsInf(arg1->floatval) == -1) {
- if (xmlXPathIsInf(arg2->floatval) == -1)
- ret = 1;
- else
- ret = 0;
- } else if (xmlXPathIsInf(arg2->floatval) == 1) {
- if (xmlXPathIsInf(arg1->floatval) == 1)
- ret = 1;
- else
- ret = 0;
- } else if (xmlXPathIsInf(arg2->floatval) == -1) {
- if (xmlXPathIsInf(arg1->floatval) == -1)
- ret = 1;
- else
- ret = 0;
- } else {
- ret = (arg1->floatval == arg2->floatval);
- }
- break;
- case XPATH_USERS:
- case XPATH_POINT:
- case XPATH_RANGE:
- case XPATH_LOCATIONSET:
- TODO
- break;
- case XPATH_NODESET:
- case XPATH_XSLT_TREE:
- break;
- }
- break;
- case XPATH_STRING:
- switch (arg2->type) {
- case XPATH_UNDEFINED:
- #ifdef DEBUG_EXPR
- xmlGenericError(xmlGenericErrorContext,
- "Equal: undefined\n");
- #endif
- break;
- case XPATH_BOOLEAN:
- if ((arg1->stringval == NULL) ||
- (arg1->stringval[0] == 0)) ret = 0;
- else
- ret = 1;
- ret = (arg2->boolval == ret);
- break;
- case XPATH_STRING:
- ret = xmlStrEqual(arg1->stringval, arg2->stringval);
- break;
- case XPATH_NUMBER:
- valuePush(ctxt, arg1);
- xmlXPathNumberFunction(ctxt, 1);
- arg1 = valuePop(ctxt);
-
- if (xmlXPathIsNaN(arg1->floatval) ||
- xmlXPathIsNaN(arg2->floatval)) {
- ret = 0;
- } else if (xmlXPathIsInf(arg1->floatval) == 1) {
- if (xmlXPathIsInf(arg2->floatval) == 1)
- ret = 1;
- else
- ret = 0;
- } else if (xmlXPathIsInf(arg1->floatval) == -1) {
- if (xmlXPathIsInf(arg2->floatval) == -1)
- ret = 1;
- else
- ret = 0;
- } else if (xmlXPathIsInf(arg2->floatval) == 1) {
- if (xmlXPathIsInf(arg1->floatval) == 1)
- ret = 1;
- else
- ret = 0;
- } else if (xmlXPathIsInf(arg2->floatval) == -1) {
- if (xmlXPathIsInf(arg1->floatval) == -1)
- ret = 1;
- else
- ret = 0;
- } else {
- ret = (arg1->floatval == arg2->floatval);
- }
- break;
- case XPATH_USERS:
- case XPATH_POINT:
- case XPATH_RANGE:
- case XPATH_LOCATIONSET:
- TODO
- break;
- case XPATH_NODESET:
- case XPATH_XSLT_TREE:
- break;
- }
- break;
- case XPATH_USERS:
- case XPATH_POINT:
- case XPATH_RANGE:
- case XPATH_LOCATIONSET:
- TODO
- break;
- case XPATH_NODESET:
- case XPATH_XSLT_TREE:
- break;
- }
- xmlXPathReleaseObject(ctxt->context, arg1);
- xmlXPathReleaseObject(ctxt->context, arg2);
- return(ret);
- }
- int
- xmlXPathEqualValues(xmlXPathParserContextPtr ctxt) {
- xmlXPathObjectPtr arg1, arg2, argtmp;
- int ret = 0;
- if ((ctxt == NULL) || (ctxt->context == NULL)) return(0);
- arg2 = valuePop(ctxt);
- arg1 = valuePop(ctxt);
- if ((arg1 == NULL) || (arg2 == NULL)) {
- if (arg1 != NULL)
- xmlXPathReleaseObject(ctxt->context, arg1);
- else
- xmlXPathReleaseObject(ctxt->context, arg2);
- XP_ERROR0(XPATH_INVALID_OPERAND);
- }
- if (arg1 == arg2) {
- #ifdef DEBUG_EXPR
- xmlGenericError(xmlGenericErrorContext,
- "Equal: by pointer\n");
- #endif
- xmlXPathFreeObject(arg1);
- return(1);
- }
-
- if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) ||
- (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) {
-
- if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) {
- argtmp = arg2;
- arg2 = arg1;
- arg1 = argtmp;
- }
- switch (arg2->type) {
- case XPATH_UNDEFINED:
- #ifdef DEBUG_EXPR
- xmlGenericError(xmlGenericErrorContext,
- "Equal: undefined\n");
- #endif
- break;
- case XPATH_NODESET:
- case XPATH_XSLT_TREE:
- ret = xmlXPathEqualNodeSets(arg1, arg2, 0);
- break;
- case XPATH_BOOLEAN:
- if ((arg1->nodesetval == NULL) ||
- (arg1->nodesetval->nodeNr == 0)) ret = 0;
- else
- ret = 1;
- ret = (ret == arg2->boolval);
- break;
- case XPATH_NUMBER:
- ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 0);
- break;
- case XPATH_STRING:
- ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval, 0);
- break;
- case XPATH_USERS:
- case XPATH_POINT:
- case XPATH_RANGE:
- case XPATH_LOCATIONSET:
- TODO
- break;
- }
- xmlXPathReleaseObject(ctxt->context, arg1);
- xmlXPathReleaseObject(ctxt->context, arg2);
- return(ret);
- }
- return (xmlXPathEqualValuesCommon(ctxt, arg1, arg2));
- }
- int
- xmlXPathNotEqualValues(xmlXPathParserContextPtr ctxt) {
- xmlXPathObjectPtr arg1, arg2, argtmp;
- int ret = 0;
- if ((ctxt == NULL) || (ctxt->context == NULL)) return(0);
- arg2 = valuePop(ctxt);
- arg1 = valuePop(ctxt);
- if ((arg1 == NULL) || (arg2 == NULL)) {
- if (arg1 != NULL)
- xmlXPathReleaseObject(ctxt->context, arg1);
- else
- xmlXPathReleaseObject(ctxt->context, arg2);
- XP_ERROR0(XPATH_INVALID_OPERAND);
- }
- if (arg1 == arg2) {
- #ifdef DEBUG_EXPR
- xmlGenericError(xmlGenericErrorContext,
- "NotEqual: by pointer\n");
- #endif
- xmlXPathReleaseObject(ctxt->context, arg1);
- return(0);
- }
-
- if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) ||
- (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) {
-
- if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) {
- argtmp = arg2;
- arg2 = arg1;
- arg1 = argtmp;
- }
- switch (arg2->type) {
- case XPATH_UNDEFINED:
- #ifdef DEBUG_EXPR
- xmlGenericError(xmlGenericErrorContext,
- "NotEqual: undefined\n");
- #endif
- break;
- case XPATH_NODESET:
- case XPATH_XSLT_TREE:
- ret = xmlXPathEqualNodeSets(arg1, arg2, 1);
- break;
- case XPATH_BOOLEAN:
- if ((arg1->nodesetval == NULL) ||
- (arg1->nodesetval->nodeNr == 0)) ret = 0;
- else
- ret = 1;
- ret = (ret != arg2->boolval);
- break;
- case XPATH_NUMBER:
- ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 1);
- break;
- case XPATH_STRING:
- ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval,1);
- break;
- case XPATH_USERS:
- case XPATH_POINT:
- case XPATH_RANGE:
- case XPATH_LOCATIONSET:
- TODO
- break;
- }
- xmlXPathReleaseObject(ctxt->context, arg1);
- xmlXPathReleaseObject(ctxt->context, arg2);
- return(ret);
- }
- return (!xmlXPathEqualValuesCommon(ctxt, arg1, arg2));
- }
- int
- xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict) {
- int ret = 0, arg1i = 0, arg2i = 0;
- xmlXPathObjectPtr arg1, arg2;
- if ((ctxt == NULL) || (ctxt->context == NULL)) return(0);
- arg2 = valuePop(ctxt);
- arg1 = valuePop(ctxt);
- if ((arg1 == NULL) || (arg2 == NULL)) {
- if (arg1 != NULL)
- xmlXPathReleaseObject(ctxt->context, arg1);
- else
- xmlXPathReleaseObject(ctxt->context, arg2);
- XP_ERROR0(XPATH_INVALID_OPERAND);
- }
- if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) ||
- (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) {
-
- if (((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE)) &&
- ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE))){
- ret = xmlXPathCompareNodeSets(inf, strict, arg1, arg2);
- } else {
- if ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) {
- ret = xmlXPathCompareNodeSetValue(ctxt, inf, strict,
- arg1, arg2);
- } else {
- ret = xmlXPathCompareNodeSetValue(ctxt, !inf, strict,
- arg2, arg1);
- }
- }
- return(ret);
- }
- if (arg1->type != XPATH_NUMBER) {
- valuePush(ctxt, arg1);
- xmlXPathNumberFunction(ctxt, 1);
- arg1 = valuePop(ctxt);
- }
- if (arg1->type != XPATH_NUMBER) {
- xmlXPathFreeObject(arg1);
- xmlXPathFreeObject(arg2);
- XP_ERROR0(XPATH_INVALID_OPERAND);
- }
- if (arg2->type != XPATH_NUMBER) {
- valuePush(ctxt, arg2);
- xmlXPathNumberFunction(ctxt, 1);
- arg2 = valuePop(ctxt);
- }
- if (arg2->type != XPATH_NUMBER) {
- xmlXPathReleaseObject(ctxt->context, arg1);
- xmlXPathReleaseObject(ctxt->context, arg2);
- XP_ERROR0(XPATH_INVALID_OPERAND);
- }
-
-
- if (xmlXPathIsNaN(arg1->floatval) || xmlXPathIsNaN(arg2->floatval)) {
- ret=0;
- } else {
- arg1i=xmlXPathIsInf(arg1->floatval);
- arg2i=xmlXPathIsInf(arg2->floatval);
- if (inf && strict) {
- if ((arg1i == -1 && arg2i != -1) ||
- (arg2i == 1 && arg1i != 1)) {
- ret = 1;
- } else if (arg1i == 0 && arg2i == 0) {
- ret = (arg1->floatval < arg2->floatval);
- } else {
- ret = 0;
- }
- }
- else if (inf && !strict) {
- if (arg1i == -1 || arg2i == 1) {
- ret = 1;
- } else if (arg1i == 0 && arg2i == 0) {
- ret = (arg1->floatval <= arg2->floatval);
- } else {
- ret = 0;
- }
- }
- else if (!inf && strict) {
- if ((arg1i == 1 && arg2i != 1) ||
- (arg2i == -1 && arg1i != -1)) {
- ret = 1;
- } else if (arg1i == 0 && arg2i == 0) {
- ret = (arg1->floatval > arg2->floatval);
- } else {
- ret = 0;
- }
- }
- else if (!inf && !strict) {
- if (arg1i == 1 || arg2i == -1) {
- ret = 1;
- } else if (arg1i == 0 && arg2i == 0) {
- ret = (arg1->floatval >= arg2->floatval);
- } else {
- ret = 0;
- }
- }
- }
- xmlXPathReleaseObject(ctxt->context, arg1);
- xmlXPathReleaseObject(ctxt->context, arg2);
- return(ret);
- }
- void
- xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt) {
- if ((ctxt == NULL) || (ctxt->context == NULL)) return;
- CAST_TO_NUMBER;
- CHECK_TYPE(XPATH_NUMBER);
- if (xmlXPathIsNaN(ctxt->value->floatval))
- ctxt->value->floatval=xmlXPathNAN;
- else if (xmlXPathIsInf(ctxt->value->floatval) == 1)
- ctxt->value->floatval=xmlXPathNINF;
- else if (xmlXPathIsInf(ctxt->value->floatval) == -1)
- ctxt->value->floatval=xmlXPathPINF;
- else if (ctxt->value->floatval == 0) {
- if (xmlXPathGetSign(ctxt->value->floatval) == 0)
- ctxt->value->floatval = xmlXPathNZERO;
- else
- ctxt->value->floatval = 0;
- }
- else
- ctxt->value->floatval = - ctxt->value->floatval;
- }
- void
- xmlXPathAddValues(xmlXPathParserContextPtr ctxt) {
- xmlXPathObjectPtr arg;
- double val;
- arg = valuePop(ctxt);
- if (arg == NULL)
- XP_ERROR(XPATH_INVALID_OPERAND);
- val = xmlXPathCastToNumber(arg);
- xmlXPathReleaseObject(ctxt->context, arg);
- CAST_TO_NUMBER;
- CHECK_TYPE(XPATH_NUMBER);
- ctxt->value->floatval += val;
- }
- void
- xmlXPathSubValues(xmlXPathParserContextPtr ctxt) {
- xmlXPathObjectPtr arg;
- double val;
- arg = valuePop(ctxt);
- if (arg == NULL)
- XP_ERROR(XPATH_INVALID_OPERAND);
- val = xmlXPathCastToNumber(arg);
- xmlXPathReleaseObject(ctxt->context, arg);
- CAST_TO_NUMBER;
- CHECK_TYPE(XPATH_NUMBER);
- ctxt->value->floatval -= val;
- }
- void
- xmlXPathMultValues(xmlXPathParserContextPtr ctxt) {
- xmlXPathObjectPtr arg;
- double val;
- arg = valuePop(ctxt);
- if (arg == NULL)
- XP_ERROR(XPATH_INVALID_OPERAND);
- val = xmlXPathCastToNumber(arg);
- xmlXPathReleaseObject(ctxt->context, arg);
- CAST_TO_NUMBER;
- CHECK_TYPE(XPATH_NUMBER);
- ctxt->value->floatval *= val;
- }
- void
- xmlXPathDivValues(xmlXPathParserContextPtr ctxt) {
- xmlXPathObjectPtr arg;
- double val;
- arg = valuePop(ctxt);
- if (arg == NULL)
- XP_ERROR(XPATH_INVALID_OPERAND);
- val = xmlXPathCastToNumber(arg);
- xmlXPathReleaseObject(ctxt->context, arg);
- CAST_TO_NUMBER;
- CHECK_TYPE(XPATH_NUMBER);
- if (xmlXPathIsNaN(val) || xmlXPathIsNaN(ctxt->value->floatval))
- ctxt->value->floatval = xmlXPathNAN;
- else if (val == 0 && xmlXPathGetSign(val) != 0) {
- if (ctxt->value->floatval == 0)
- ctxt->value->floatval = xmlXPathNAN;
- else if (ctxt->value->floatval > 0)
- ctxt->value->floatval = xmlXPathNINF;
- else if (ctxt->value->floatval < 0)
- ctxt->value->floatval = xmlXPathPINF;
- }
- else if (val == 0) {
- if (ctxt->value->floatval == 0)
- ctxt->value->floatval = xmlXPathNAN;
- else if (ctxt->value->floatval > 0)
- ctxt->value->floatval = xmlXPathPINF;
- else if (ctxt->value->floatval < 0)
- ctxt->value->floatval = xmlXPathNINF;
- } else
- ctxt->value->floatval /= val;
- }
- void
- xmlXPathModValues(xmlXPathParserContextPtr ctxt) {
- xmlXPathObjectPtr arg;
- double arg1, arg2;
- arg = valuePop(ctxt);
- if (arg == NULL)
- XP_ERROR(XPATH_INVALID_OPERAND);
- arg2 = xmlXPathCastToNumber(arg);
- xmlXPathReleaseObject(ctxt->context, arg);
- CAST_TO_NUMBER;
- CHECK_TYPE(XPATH_NUMBER);
- arg1 = ctxt->value->floatval;
- if (arg2 == 0)
- ctxt->value->floatval = xmlXPathNAN;
- else {
- ctxt->value->floatval = fmod(arg1, arg2);
- }
- }
- typedef xmlNodePtr (*xmlXPathTraversalFunction)
- (xmlXPathParserContextPtr ctxt, xmlNodePtr cur);
- typedef xmlNodePtr (*xmlXPathTraversalFunctionExt)
- (xmlNodePtr cur, xmlNodePtr contextNode);
- typedef xmlNodeSetPtr (*xmlXPathNodeSetMergeFunction)
- (xmlNodeSetPtr, xmlNodeSetPtr, int);
- xmlNodePtr
- xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
- if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
- if (cur == NULL)
- return(ctxt->context->node);
- return(NULL);
- }
- xmlNodePtr
- xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
- if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
- if (cur == NULL) {
- if (ctxt->context->node == NULL) return(NULL);
- switch (ctxt->context->node->type) {
- case XML_ELEMENT_NODE:
- case XML_TEXT_NODE:
- case XML_CDATA_SECTION_NODE:
- case XML_ENTITY_REF_NODE:
- case XML_ENTITY_NODE:
- case XML_PI_NODE:
- case XML_COMMENT_NODE:
- case XML_NOTATION_NODE:
- case XML_DTD_NODE:
- return(ctxt->context->node->children);
- case XML_DOCUMENT_NODE:
- case XML_DOCUMENT_TYPE_NODE:
- case XML_DOCUMENT_FRAG_NODE:
- case XML_HTML_DOCUMENT_NODE:
- #ifdef LIBXML_DOCB_ENABLED
- case XML_DOCB_DOCUMENT_NODE:
- #endif
- return(((xmlDocPtr) ctxt->context->node)->children);
- case XML_ELEMENT_DECL:
- case XML_ATTRIBUTE_DECL:
- case XML_ENTITY_DECL:
- case XML_ATTRIBUTE_NODE:
- case XML_NAMESPACE_DECL:
- case XML_XINCLUDE_START:
- case XML_XINCLUDE_END:
- return(NULL);
- }
- return(NULL);
- }
- if ((cur->type == XML_DOCUMENT_NODE) ||
- (cur->type == XML_HTML_DOCUMENT_NODE))
- return(NULL);
- return(cur->next);
- }
- static xmlNodePtr
- xmlXPathNextChildElement(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
- if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
- if (cur == NULL) {
- cur = ctxt->context->node;
- if (cur == NULL) return(NULL);
-
- switch (cur->type) {
- case XML_ELEMENT_NODE:
- case XML_DOCUMENT_FRAG_NODE:
- case XML_ENTITY_REF_NODE:
- case XML_ENTITY_NODE:
- cur = cur->children;
- if (cur != NULL) {
- if (cur->type == XML_ELEMENT_NODE)
- return(cur);
- do {
- cur = cur->next;
- } while ((cur != NULL) &&
- (cur->type != XML_ELEMENT_NODE));
- return(cur);
- }
- return(NULL);
- case XML_DOCUMENT_NODE:
- case XML_HTML_DOCUMENT_NODE:
- #ifdef LIBXML_DOCB_ENABLED
- case XML_DOCB_DOCUMENT_NODE:
- #endif
- return(xmlDocGetRootElement((xmlDocPtr) cur));
- default:
- return(NULL);
- }
- return(NULL);
- }
-
- switch (cur->type) {
- case XML_ELEMENT_NODE:
- case XML_TEXT_NODE:
- case XML_ENTITY_REF_NODE:
- case XML_ENTITY_NODE:
- case XML_CDATA_SECTION_NODE:
- case XML_PI_NODE:
- case XML_COMMENT_NODE:
- case XML_XINCLUDE_END:
- break;
-
- default:
- return(NULL);
- }
- if (cur->next != NULL) {
- if (cur->next->type == XML_ELEMENT_NODE)
- return(cur->next);
- cur = cur->next;
- do {
- cur = cur->next;
- } while ((cur != NULL) && (cur->type != XML_ELEMENT_NODE));
- return(cur);
- }
- return(NULL);
- }
- static xmlNodePtr
- xmlXPathNextDescendantOrSelfElemParent(xmlNodePtr cur,
- xmlNodePtr contextNode)
- {
- if (cur == NULL) {
- if (contextNode == NULL)
- return(NULL);
- switch (contextNode->type) {
- case XML_ELEMENT_NODE:
- case XML_XINCLUDE_START:
- case XML_DOCUMENT_FRAG_NODE:
- case XML_DOCUMENT_NODE:
- #ifdef LIBXML_DOCB_ENABLED
- case XML_DOCB_DOCUMENT_NODE:
- #endif
- case XML_HTML_DOCUMENT_NODE:
- return(contextNode);
- default:
- return(NULL);
- }
- return(NULL);
- } else {
- xmlNodePtr start = cur;
- while (cur != NULL) {
- switch (cur->type) {
- case XML_ELEMENT_NODE:
-
- case XML_XINCLUDE_START:
- case XML_DOCUMENT_FRAG_NODE:
- if (cur != start)
- return(cur);
- if (cur->children != NULL) {
- cur = cur->children;
- continue;
- }
- break;
-
- case XML_DOCUMENT_NODE:
- #ifdef LIBXML_DOCB_ENABLED
- case XML_DOCB_DOCUMENT_NODE:
- #endif
- case XML_HTML_DOCUMENT_NODE:
- if (cur != start)
- return(cur);
- return(xmlDocGetRootElement((xmlDocPtr) cur));
- default:
- break;
- }
- next_sibling:
- if ((cur == NULL) || (cur == contextNode))
- return(NULL);
- if (cur->next != NULL) {
- cur = cur->next;
- } else {
- cur = cur->parent;
- goto next_sibling;
- }
- }
- }
- return(NULL);
- }
- xmlNodePtr
- xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
- if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
- if (cur == NULL) {
- if (ctxt->context->node == NULL)
- return(NULL);
- if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
- (ctxt->context->node->type == XML_NAMESPACE_DECL))
- return(NULL);
- if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc)
- return(ctxt->context->doc->children);
- return(ctxt->context->node->children);
- }
- if (cur->children != NULL) {
-
- if (cur->children->type != XML_ENTITY_DECL) {
- cur = cur->children;
-
- if (cur->type != XML_DTD_NODE)
- return(cur);
- }
- }
- if (cur == ctxt->context->node) return(NULL);
- while (cur->next != NULL) {
- cur = cur->next;
- if ((cur->type != XML_ENTITY_DECL) &&
- (cur->type != XML_DTD_NODE))
- return(cur);
- }
- do {
- cur = cur->parent;
- if (cur == NULL) break;
- if (cur == ctxt->context->node) return(NULL);
- if (cur->next != NULL) {
- cur = cur->next;
- return(cur);
- }
- } while (cur != NULL);
- return(cur);
- }
- xmlNodePtr
- xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
- if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
- if (cur == NULL) {
- if (ctxt->context->node == NULL)
- return(NULL);
- if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
- (ctxt->context->node->type == XML_NAMESPACE_DECL))
- return(NULL);
- return(ctxt->context->node);
- }
- return(xmlXPathNextDescendant(ctxt, cur));
- }
- xmlNodePtr
- xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
- if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
-
- if (cur == NULL) {
- if (ctxt->context->node == NULL) return(NULL);
- switch (ctxt->context->node->type) {
- case XML_ELEMENT_NODE:
- case XML_TEXT_NODE:
- case XML_CDATA_SECTION_NODE:
- case XML_ENTITY_REF_NODE:
- case XML_ENTITY_NODE:
- case XML_PI_NODE:
- case XML_COMMENT_NODE:
- case XML_NOTATION_NODE:
- case XML_DTD_NODE:
- case XML_ELEMENT_DECL:
- case XML_ATTRIBUTE_DECL:
- case XML_XINCLUDE_START:
- case XML_XINCLUDE_END:
- case XML_ENTITY_DECL:
- if (ctxt->context->node->parent == NULL)
- return((xmlNodePtr) ctxt->context->doc);
- if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) &&
- ((ctxt->context->node->parent->name[0] == ' ') ||
- (xmlStrEqual(ctxt->context->node->parent->name,
- BAD_CAST "fake node libxslt"))))
- return(NULL);
- return(ctxt->context->node->parent);
- case XML_ATTRIBUTE_NODE: {
- xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node;
- return(att->parent);
- }
- case XML_DOCUMENT_NODE:
- case XML_DOCUMENT_TYPE_NODE:
- case XML_DOCUMENT_FRAG_NODE:
- case XML_HTML_DOCUMENT_NODE:
- #ifdef LIBXML_DOCB_ENABLED
- case XML_DOCB_DOCUMENT_NODE:
- #endif
- return(NULL);
- case XML_NAMESPACE_DECL: {
- xmlNsPtr ns = (xmlNsPtr) ctxt->context->node;
- if ((ns->next != NULL) &&
- (ns->next->type != XML_NAMESPACE_DECL))
- return((xmlNodePtr) ns->next);
- return(NULL);
- }
- }
- }
- return(NULL);
- }
- xmlNodePtr
- xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
- if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
-
- if (cur == NULL) {
- if (ctxt->context->node == NULL) return(NULL);
- switch (ctxt->context->node->type) {
- case XML_ELEMENT_NODE:
- case XML_TEXT_NODE:
- case XML_CDATA_SECTION_NODE:
- case XML_ENTITY_REF_NODE:
- case XML_ENTITY_NODE:
- case XML_PI_NODE:
- case XML_COMMENT_NODE:
- case XML_DTD_NODE:
- case XML_ELEMENT_DECL:
- case XML_ATTRIBUTE_DECL:
- case XML_ENTITY_DECL:
- case XML_NOTATION_NODE:
- case XML_XINCLUDE_START:
- case XML_XINCLUDE_END:
- if (ctxt->context->node->parent == NULL)
- return((xmlNodePtr) ctxt->context->doc);
- if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) &&
- ((ctxt->context->node->parent->name[0] == ' ') ||
- (xmlStrEqual(ctxt->context->node->parent->name,
- BAD_CAST "fake node libxslt"))))
- return(NULL);
- return(ctxt->context->node->parent);
- case XML_ATTRIBUTE_NODE: {
- xmlAttrPtr tmp = (xmlAttrPtr) ctxt->context->node;
- return(tmp->parent);
- }
- case XML_DOCUMENT_NODE:
- case XML_DOCUMENT_TYPE_NODE:
- case XML_DOCUMENT_FRAG_NODE:
- case XML_HTML_DOCUMENT_NODE:
- #ifdef LIBXML_DOCB_ENABLED
- case XML_DOCB_DOCUMENT_NODE:
- #endif
- return(NULL);
- case XML_NAMESPACE_DECL: {
- xmlNsPtr ns = (xmlNsPtr) ctxt->context->node;
- if ((ns->next != NULL) &&
- (ns->next->type != XML_NAMESPACE_DECL))
- return((xmlNodePtr) ns->next);
-
- return(NULL);
- }
- }
- return(NULL);
- }
- if (cur == ctxt->context->doc->children)
- return((xmlNodePtr) ctxt->context->doc);
- if (cur == (xmlNodePtr) ctxt->context->doc)
- return(NULL);
- switch (cur->type) {
- case XML_ELEMENT_NODE:
- case XML_TEXT_NODE:
- case XML_CDATA_SECTION_NODE:
- case XML_ENTITY_REF_NODE:
- case XML_ENTITY_NODE:
- case XML_PI_NODE:
- case XML_COMMENT_NODE:
- case XML_NOTATION_NODE:
- case XML_DTD_NODE:
- case XML_ELEMENT_DECL:
- case XML_ATTRIBUTE_DECL:
- case XML_ENTITY_DECL:
- case XML_XINCLUDE_START:
- case XML_XINCLUDE_END:
- if (cur->parent == NULL)
- return(NULL);
- if ((cur->parent->type == XML_ELEMENT_NODE) &&
- ((cur->parent->name[0] == ' ') ||
- (xmlStrEqual(cur->parent->name,
- BAD_CAST "fake node libxslt"))))
- return(NULL);
- return(cur->parent);
- case XML_ATTRIBUTE_NODE: {
- xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node;
- return(att->parent);
- }
- case XML_NAMESPACE_DECL: {
- xmlNsPtr ns = (xmlNsPtr) ctxt->context->node;
- if ((ns->next != NULL) &&
- (ns->next->type != XML_NAMESPACE_DECL))
- return((xmlNodePtr) ns->next);
-
- return(NULL);
- }
- case XML_DOCUMENT_NODE:
- case XML_DOCUMENT_TYPE_NODE:
- case XML_DOCUMENT_FRAG_NODE:
- case XML_HTML_DOCUMENT_NODE:
- #ifdef LIBXML_DOCB_ENABLED
- case XML_DOCB_DOCUMENT_NODE:
- #endif
- return(NULL);
- }
- return(NULL);
- }
- xmlNodePtr
- xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
- if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
- if (cur == NULL)
- return(ctxt->context->node);
- return(xmlXPathNextAncestor(ctxt, cur));
- }
- xmlNodePtr
- xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
- if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
- if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
- (ctxt->context->node->type == XML_NAMESPACE_DECL))
- return(NULL);
- if (cur == (xmlNodePtr) ctxt->context->doc)
- return(NULL);
- if (cur == NULL)
- return(ctxt->context->node->next);
- return(cur->next);
- }
- xmlNodePtr
- xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
- if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
- if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
- (ctxt->context->node->type == XML_NAMESPACE_DECL))
- return(NULL);
- if (cur == (xmlNodePtr) ctxt->context->doc)
- return(NULL);
- if (cur == NULL)
- return(ctxt->context->node->prev);
- if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) {
- cur = cur->prev;
- if (cur == NULL)
- return(ctxt->context->node->prev);
- }
- return(cur->prev);
- }
- xmlNodePtr
- xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
- if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
- if (cur != NULL && cur->children != NULL)
- return cur->children ;
- if (cur == NULL) cur = ctxt->context->node;
- if (cur == NULL) return(NULL) ;
- if (cur->next != NULL) return(cur->next) ;
- do {
- cur = cur->parent;
- if (cur == NULL) break;
- if (cur == (xmlNodePtr) ctxt->context->doc) return(NULL);
- if (cur->next != NULL) return(cur->next);
- } while (cur != NULL);
- return(cur);
- }
- static int
- xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) {
- if ((ancestor == NULL) || (node == NULL)) return(0);
-
- if (ancestor->doc != node->doc) return(0);
-
- if (ancestor == (xmlNodePtr) node->doc) return(1);
- if (node == (xmlNodePtr) ancestor->doc) return(0);
- while (node->parent != NULL) {
- if (node->parent == ancestor)
- return(1);
- node = node->parent;
- }
- return(0);
- }
- xmlNodePtr
- xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur)
- {
- if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
- if (cur == NULL)
- cur = ctxt->context->node;
- if (cur == NULL)
- return (NULL);
- if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE))
- cur = cur->prev;
- do {
- if (cur->prev != NULL) {
- for (cur = cur->prev; cur->last != NULL; cur = cur->last) ;
- return (cur);
- }
- cur = cur->parent;
- if (cur == NULL)
- return (NULL);
- if (cur == ctxt->context->doc->children)
- return (NULL);
- } while (xmlXPathIsAncestor(cur, ctxt->context->node));
- return (cur);
- }
- static xmlNodePtr
- xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt,
- xmlNodePtr cur)
- {
- if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
- if (cur == NULL) {
- cur = ctxt->context->node;
- if (cur == NULL)
- return (NULL);
- if (cur->type == XML_NAMESPACE_DECL)
- cur = (xmlNodePtr)((xmlNsPtr)cur)->next;
- ctxt->ancestor = cur->parent;
- }
- if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE))
- cur = cur->prev;
- while (cur->prev == NULL) {
- cur = cur->parent;
- if (cur == NULL)
- return (NULL);
- if (cur == ctxt->context->doc->children)
- return (NULL);
- if (cur != ctxt->ancestor)
- return (cur);
- ctxt->ancestor = cur->parent;
- }
- cur = cur->prev;
- while (cur->last != NULL)
- cur = cur->last;
- return (cur);
- }
- xmlNodePtr
- xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
- if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
- if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL);
- if (ctxt->context->tmpNsList == NULL && cur != (xmlNodePtr) xmlXPathXMLNamespace) {
- if (ctxt->context->tmpNsList != NULL)
- xmlFree(ctxt->context->tmpNsList);
- ctxt->context->tmpNsList =
- xmlGetNsList(ctxt->context->doc, ctxt->context->node);
- ctxt->context->tmpNsNr = 0;
- if (ctxt->context->tmpNsList != NULL) {
- while (ctxt->context->tmpNsList[ctxt->context->tmpNsNr] != NULL) {
- ctxt->context->tmpNsNr++;
- }
- }
- return((xmlNodePtr) xmlXPathXMLNamespace);
- }
- if (ctxt->context->tmpNsNr > 0) {
- return (xmlNodePtr)ctxt->context->tmpNsList[--ctxt->context->tmpNsNr];
- } else {
- if (ctxt->context->tmpNsList != NULL)
- xmlFree(ctxt->context->tmpNsList);
- ctxt->context->tmpNsList = NULL;
- return(NULL);
- }
- }
- xmlNodePtr
- xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
- if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
- if (ctxt->context->node == NULL)
- return(NULL);
- if (ctxt->context->node->type != XML_ELEMENT_NODE)
- return(NULL);
- if (cur == NULL) {
- if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc)
- return(NULL);
- return((xmlNodePtr)ctxt->context->node->properties);
- }
- return((xmlNodePtr)cur->next);
- }
- #define IS_FUNCTION 200
- void
- xmlXPathRoot(xmlXPathParserContextPtr ctxt) {
- if ((ctxt == NULL) || (ctxt->context == NULL))
- return;
- ctxt->context->node = (xmlNodePtr) ctxt->context->doc;
- valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context,
- ctxt->context->node));
- }
- void
- xmlXPathLastFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- CHECK_ARITY(0);
- if (ctxt->context->contextSize >= 0) {
- valuePush(ctxt,
- xmlXPathCacheNewFloat(ctxt->context,
- (double) ctxt->context->contextSize));
- #ifdef DEBUG_EXPR
- xmlGenericError(xmlGenericErrorContext,
- "last() : %d\n", ctxt->context->contextSize);
- #endif
- } else {
- XP_ERROR(XPATH_INVALID_CTXT_SIZE);
- }
- }
- void
- xmlXPathPositionFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- CHECK_ARITY(0);
- if (ctxt->context->proximityPosition >= 0) {
- valuePush(ctxt,
- xmlXPathCacheNewFloat(ctxt->context,
- (double) ctxt->context->proximityPosition));
- #ifdef DEBUG_EXPR
- xmlGenericError(xmlGenericErrorContext, "position() : %d\n",
- ctxt->context->proximityPosition);
- #endif
- } else {
- XP_ERROR(XPATH_INVALID_CTXT_POSITION);
- }
- }
- void
- xmlXPathCountFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlXPathObjectPtr cur;
- CHECK_ARITY(1);
- if ((ctxt->value == NULL) ||
- ((ctxt->value->type != XPATH_NODESET) &&
- (ctxt->value->type != XPATH_XSLT_TREE)))
- XP_ERROR(XPATH_INVALID_TYPE);
- cur = valuePop(ctxt);
- if ((cur == NULL) || (cur->nodesetval == NULL))
- valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, (double) 0));
- else if ((cur->type == XPATH_NODESET) || (cur->type == XPATH_XSLT_TREE)) {
- valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context,
- (double) cur->nodesetval->nodeNr));
- } else {
- if ((cur->nodesetval->nodeNr != 1) ||
- (cur->nodesetval->nodeTab == NULL)) {
- valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, (double) 0));
- } else {
- xmlNodePtr tmp;
- int i = 0;
- tmp = cur->nodesetval->nodeTab[0];
- if (tmp != NULL) {
- tmp = tmp->children;
- while (tmp != NULL) {
- tmp = tmp->next;
- i++;
- }
- }
- valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, (double) i));
- }
- }
- xmlXPathReleaseObject(ctxt->context, cur);
- }
- static xmlNodeSetPtr
- xmlXPathGetElementsByIds (xmlDocPtr doc, const xmlChar *ids) {
- xmlNodeSetPtr ret;
- const xmlChar *cur = ids;
- xmlChar *ID;
- xmlAttrPtr attr;
- xmlNodePtr elem = NULL;
- if (ids == NULL) return(NULL);
- ret = xmlXPathNodeSetCreate(NULL);
- if (ret == NULL)
- return(ret);
- while (IS_BLANK_CH(*cur)) cur++;
- while (*cur != 0) {
- while ((!IS_BLANK_CH(*cur)) && (*cur != 0))
- cur++;
- ID = xmlStrndup(ids, cur - ids);
- if (ID != NULL) {
-
- attr = xmlGetID(doc, ID);
- if (attr != NULL) {
- if (attr->type == XML_ATTRIBUTE_NODE)
- elem = attr->parent;
- else if (attr->type == XML_ELEMENT_NODE)
- elem = (xmlNodePtr) attr;
- else
- elem = NULL;
- if (elem != NULL)
- xmlXPathNodeSetAdd(ret, elem);
- }
- xmlFree(ID);
- }
- while (IS_BLANK_CH(*cur)) cur++;
- ids = cur;
- }
- return(ret);
- }
- void
- xmlXPathIdFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlChar *tokens;
- xmlNodeSetPtr ret;
- xmlXPathObjectPtr obj;
- CHECK_ARITY(1);
- obj = valuePop(ctxt);
- if (obj == NULL) XP_ERROR(XPATH_INVALID_OPERAND);
- if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) {
- xmlNodeSetPtr ns;
- int i;
- ret = xmlXPathNodeSetCreate(NULL);
-
- if (obj->nodesetval != NULL) {
- for (i = 0; i < obj->nodesetval->nodeNr; i++) {
- tokens =
- xmlXPathCastNodeToString(obj->nodesetval->nodeTab[i]);
- ns = xmlXPathGetElementsByIds(ctxt->context->doc, tokens);
- ret = xmlXPathNodeSetMerge(ret, ns);
- xmlXPathFreeNodeSet(ns);
- if (tokens != NULL)
- xmlFree(tokens);
- }
- }
- xmlXPathReleaseObject(ctxt->context, obj);
- valuePush(ctxt, xmlXPathCacheWrapNodeSet(ctxt->context, ret));
- return;
- }
- obj = xmlXPathCacheConvertString(ctxt->context, obj);
- ret = xmlXPathGetElementsByIds(ctxt->context->doc, obj->stringval);
- valuePush(ctxt, xmlXPathCacheWrapNodeSet(ctxt->context, ret));
- xmlXPathReleaseObject(ctxt->context, obj);
- return;
- }
- void
- xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlXPathObjectPtr cur;
- if (ctxt == NULL) return;
- if (nargs == 0) {
- valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context,
- ctxt->context->node));
- nargs = 1;
- }
- CHECK_ARITY(1);
- if ((ctxt->value == NULL) ||
- ((ctxt->value->type != XPATH_NODESET) &&
- (ctxt->value->type != XPATH_XSLT_TREE)))
- XP_ERROR(XPATH_INVALID_TYPE);
- cur = valuePop(ctxt);
- if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) {
- valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, ""));
- } else {
- int i = 0;
- switch (cur->nodesetval->nodeTab[i]->type) {
- case XML_ELEMENT_NODE:
- case XML_ATTRIBUTE_NODE:
- case XML_PI_NODE:
- if (cur->nodesetval->nodeTab[i]->name[0] == ' ')
- valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, ""));
- else
- valuePush(ctxt,
- xmlXPathCacheNewString(ctxt->context,
- cur->nodesetval->nodeTab[i]->name));
- break;
- case XML_NAMESPACE_DECL:
- valuePush(ctxt, xmlXPathCacheNewString(ctxt->context,
- ((xmlNsPtr)cur->nodesetval->nodeTab[i])->prefix));
- break;
- default:
- valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, ""));
- }
- }
- xmlXPathReleaseObject(ctxt->context, cur);
- }
- void
- xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlXPathObjectPtr cur;
- if (ctxt == NULL) return;
- if (nargs == 0) {
- valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context,
- ctxt->context->node));
- nargs = 1;
- }
- CHECK_ARITY(1);
- if ((ctxt->value == NULL) ||
- ((ctxt->value->type != XPATH_NODESET) &&
- (ctxt->value->type != XPATH_XSLT_TREE)))
- XP_ERROR(XPATH_INVALID_TYPE);
- cur = valuePop(ctxt);
- if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) {
- valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, ""));
- } else {
- int i = 0;
- switch (cur->nodesetval->nodeTab[i]->type) {
- case XML_ELEMENT_NODE:
- case XML_ATTRIBUTE_NODE:
- if (cur->nodesetval->nodeTab[i]->ns == NULL)
- valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, ""));
- else
- valuePush(ctxt, xmlXPathCacheNewString(ctxt->context,
- cur->nodesetval->nodeTab[i]->ns->href));
- break;
- default:
- valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, ""));
- }
- }
- xmlXPathReleaseObject(ctxt->context, cur);
- }
- static void
- xmlXPathNameFunction(xmlXPathParserContextPtr ctxt, int nargs)
- {
- xmlXPathObjectPtr cur;
- if (nargs == 0) {
- valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context,
- ctxt->context->node));
- nargs = 1;
- }
- CHECK_ARITY(1);
- if ((ctxt->value == NULL) ||
- ((ctxt->value->type != XPATH_NODESET) &&
- (ctxt->value->type != XPATH_XSLT_TREE)))
- XP_ERROR(XPATH_INVALID_TYPE);
- cur = valuePop(ctxt);
- if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) {
- valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, ""));
- } else {
- int i = 0;
- switch (cur->nodesetval->nodeTab[i]->type) {
- case XML_ELEMENT_NODE:
- case XML_ATTRIBUTE_NODE:
- if (cur->nodesetval->nodeTab[i]->name[0] == ' ')
- valuePush(ctxt,
- xmlXPathCacheNewCString(ctxt->context, ""));
- else if ((cur->nodesetval->nodeTab[i]->ns == NULL) ||
- (cur->nodesetval->nodeTab[i]->ns->prefix == NULL)) {
- valuePush(ctxt,
- xmlXPathCacheNewString(ctxt->context,
- cur->nodesetval->nodeTab[i]->name));
- } else {
- xmlChar *fullname;
- fullname = xmlBuildQName(cur->nodesetval->nodeTab[i]->name,
- cur->nodesetval->nodeTab[i]->ns->prefix,
- NULL, 0);
- if (fullname == cur->nodesetval->nodeTab[i]->name)
- fullname = xmlStrdup(cur->nodesetval->nodeTab[i]->name);
- if (fullname == NULL) {
- XP_ERROR(XPATH_MEMORY_ERROR);
- }
- valuePush(ctxt, xmlXPathCacheWrapString(
- ctxt->context, fullname));
- }
- break;
- default:
- valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context,
- cur->nodesetval->nodeTab[i]));
- xmlXPathLocalNameFunction(ctxt, 1);
- }
- }
- xmlXPathReleaseObject(ctxt->context, cur);
- }
- void
- xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlXPathObjectPtr cur;
- if (ctxt == NULL) return;
- if (nargs == 0) {
- valuePush(ctxt,
- xmlXPathCacheWrapString(ctxt->context,
- xmlXPathCastNodeToString(ctxt->context->node)));
- return;
- }
- CHECK_ARITY(1);
- cur = valuePop(ctxt);
- if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND);
- valuePush(ctxt, xmlXPathCacheConvertString(ctxt->context, cur));
- }
- void
- xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlXPathObjectPtr cur;
- if (nargs == 0) {
- if ((ctxt == NULL) || (ctxt->context == NULL))
- return;
- if (ctxt->context->node == NULL) {
- valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, 0));
- } else {
- xmlChar *content;
- content = xmlXPathCastNodeToString(ctxt->context->node);
- valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context,
- xmlUTF8Strlen(content)));
- xmlFree(content);
- }
- return;
- }
- CHECK_ARITY(1);
- CAST_TO_STRING;
- CHECK_TYPE(XPATH_STRING);
- cur = valuePop(ctxt);
- valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context,
- xmlUTF8Strlen(cur->stringval)));
- xmlXPathReleaseObject(ctxt->context, cur);
- }
- void
- xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlXPathObjectPtr cur, newobj;
- xmlChar *tmp;
- if (ctxt == NULL) return;
- if (nargs < 2) {
- CHECK_ARITY(2);
- }
- CAST_TO_STRING;
- cur = valuePop(ctxt);
- if ((cur == NULL) || (cur->type != XPATH_STRING)) {
- xmlXPathReleaseObject(ctxt->context, cur);
- return;
- }
- nargs--;
- while (nargs > 0) {
- CAST_TO_STRING;
- newobj = valuePop(ctxt);
- if ((newobj == NULL) || (newobj->type != XPATH_STRING)) {
- xmlXPathReleaseObject(ctxt->context, newobj);
- xmlXPathReleaseObject(ctxt->context, cur);
- XP_ERROR(XPATH_INVALID_TYPE);
- }
- tmp = xmlStrcat(newobj->stringval, cur->stringval);
- newobj->stringval = cur->stringval;
- cur->stringval = tmp;
- xmlXPathReleaseObject(ctxt->context, newobj);
- nargs--;
- }
- valuePush(ctxt, cur);
- }
- void
- xmlXPathContainsFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlXPathObjectPtr hay, needle;
- CHECK_ARITY(2);
- CAST_TO_STRING;
- CHECK_TYPE(XPATH_STRING);
- needle = valuePop(ctxt);
- CAST_TO_STRING;
- hay = valuePop(ctxt);
- if ((hay == NULL) || (hay->type != XPATH_STRING)) {
- xmlXPathReleaseObject(ctxt->context, hay);
- xmlXPathReleaseObject(ctxt->context, needle);
- XP_ERROR(XPATH_INVALID_TYPE);
- }
- if (xmlStrstr(hay->stringval, needle->stringval))
- valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, 1));
- else
- valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, 0));
- xmlXPathReleaseObject(ctxt->context, hay);
- xmlXPathReleaseObject(ctxt->context, needle);
- }
- void
- xmlXPathStartsWithFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlXPathObjectPtr hay, needle;
- int n;
- CHECK_ARITY(2);
- CAST_TO_STRING;
- CHECK_TYPE(XPATH_STRING);
- needle = valuePop(ctxt);
- CAST_TO_STRING;
- hay = valuePop(ctxt);
- if ((hay == NULL) || (hay->type != XPATH_STRING)) {
- xmlXPathReleaseObject(ctxt->context, hay);
- xmlXPathReleaseObject(ctxt->context, needle);
- XP_ERROR(XPATH_INVALID_TYPE);
- }
- n = xmlStrlen(needle->stringval);
- if (xmlStrncmp(hay->stringval, needle->stringval, n))
- valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, 0));
- else
- valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, 1));
- xmlXPathReleaseObject(ctxt->context, hay);
- xmlXPathReleaseObject(ctxt->context, needle);
- }
- void
- xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlXPathObjectPtr str, start, len;
- double le=0, in;
- int i, l, m;
- xmlChar *ret;
- if (nargs < 2) {
- CHECK_ARITY(2);
- }
- if (nargs > 3) {
- CHECK_ARITY(3);
- }
-
- if (nargs == 3) {
- CAST_TO_NUMBER;
- CHECK_TYPE(XPATH_NUMBER);
- len = valuePop(ctxt);
- le = len->floatval;
- xmlXPathReleaseObject(ctxt->context, len);
- }
- CAST_TO_NUMBER;
- CHECK_TYPE(XPATH_NUMBER);
- start = valuePop(ctxt);
- in = start->floatval;
- xmlXPathReleaseObject(ctxt->context, start);
- CAST_TO_STRING;
- CHECK_TYPE(XPATH_STRING);
- str = valuePop(ctxt);
- m = xmlUTF8Strlen((const unsigned char *)str->stringval);
-
- if (nargs != 3) {
- le = (double)m;
- if (in < 1.0)
- in = 1.0;
- }
-
- if (!xmlXPathIsInf(in) && !xmlXPathIsNaN(in + le)) {
-
- i = (int) in;
- if (((double)i)+0.5 <= in) i++;
- if (xmlXPathIsInf(le) == 1) {
- l = m;
- if (i < 1)
- i = 1;
- }
- else if (xmlXPathIsInf(le) == -1 || le < 0.0)
- l = 0;
- else {
- l = (int) le;
- if (((double)l)+0.5 <= le) l++;
- }
-
- i -= 1;
- l += i;
- if (i < 0)
- i = 0;
- if (l > m)
- l = m;
-
- l -= i;
- ret = xmlUTF8Strsub(str->stringval, i, l);
- }
- else {
- ret = NULL;
- }
- if (ret == NULL)
- valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, ""));
- else {
- valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, ret));
- xmlFree(ret);
- }
- xmlXPathReleaseObject(ctxt->context, str);
- }
- void
- xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlXPathObjectPtr str;
- xmlXPathObjectPtr find;
- xmlBufferPtr target;
- const xmlChar *point;
- int offset;
- CHECK_ARITY(2);
- CAST_TO_STRING;
- find = valuePop(ctxt);
- CAST_TO_STRING;
- str = valuePop(ctxt);
- target = xmlBufferCreate();
- if (target) {
- point = xmlStrstr(str->stringval, find->stringval);
- if (point) {
- offset = (int)(point - str->stringval);
- xmlBufferAdd(target, str->stringval, offset);
- }
- valuePush(ctxt, xmlXPathCacheNewString(ctxt->context,
- xmlBufferContent(target)));
- xmlBufferFree(target);
- }
- xmlXPathReleaseObject(ctxt->context, str);
- xmlXPathReleaseObject(ctxt->context, find);
- }
- void
- xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlXPathObjectPtr str;
- xmlXPathObjectPtr find;
- xmlBufferPtr target;
- const xmlChar *point;
- int offset;
- CHECK_ARITY(2);
- CAST_TO_STRING;
- find = valuePop(ctxt);
- CAST_TO_STRING;
- str = valuePop(ctxt);
- target = xmlBufferCreate();
- if (target) {
- point = xmlStrstr(str->stringval, find->stringval);
- if (point) {
- offset = (int)(point - str->stringval) + xmlStrlen(find->stringval);
- xmlBufferAdd(target, &str->stringval[offset],
- xmlStrlen(str->stringval) - offset);
- }
- valuePush(ctxt, xmlXPathCacheNewString(ctxt->context,
- xmlBufferContent(target)));
- xmlBufferFree(target);
- }
- xmlXPathReleaseObject(ctxt->context, str);
- xmlXPathReleaseObject(ctxt->context, find);
- }
- void
- xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlXPathObjectPtr obj = NULL;
- xmlChar *source = NULL;
- xmlBufferPtr target;
- xmlChar blank;
- if (ctxt == NULL) return;
- if (nargs == 0) {
-
- valuePush(ctxt,
- xmlXPathCacheWrapString(ctxt->context,
- xmlXPathCastNodeToString(ctxt->context->node)));
- nargs = 1;
- }
- CHECK_ARITY(1);
- CAST_TO_STRING;
- CHECK_TYPE(XPATH_STRING);
- obj = valuePop(ctxt);
- source = obj->stringval;
- target = xmlBufferCreate();
- if (target && source) {
-
- while (IS_BLANK_CH(*source))
- source++;
-
- blank = 0;
- while (*source) {
- if (IS_BLANK_CH(*source)) {
- blank = 0x20;
- } else {
- if (blank) {
- xmlBufferAdd(target, &blank, 1);
- blank = 0;
- }
- xmlBufferAdd(target, source, 1);
- }
- source++;
- }
- valuePush(ctxt, xmlXPathCacheNewString(ctxt->context,
- xmlBufferContent(target)));
- xmlBufferFree(target);
- }
- xmlXPathReleaseObject(ctxt->context, obj);
- }
- void
- xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlXPathObjectPtr str;
- xmlXPathObjectPtr from;
- xmlXPathObjectPtr to;
- xmlBufferPtr target;
- int offset, max;
- xmlChar ch;
- const xmlChar *point;
- xmlChar *cptr;
- CHECK_ARITY(3);
- CAST_TO_STRING;
- to = valuePop(ctxt);
- CAST_TO_STRING;
- from = valuePop(ctxt);
- CAST_TO_STRING;
- str = valuePop(ctxt);
- target = xmlBufferCreate();
- if (target) {
- max = xmlUTF8Strlen(to->stringval);
- for (cptr = str->stringval; (ch=*cptr); ) {
- offset = xmlUTF8Strloc(from->stringval, cptr);
- if (offset >= 0) {
- if (offset < max) {
- point = xmlUTF8Strpos(to->stringval, offset);
- if (point)
- xmlBufferAdd(target, point, xmlUTF8Strsize(point, 1));
- }
- } else
- xmlBufferAdd(target, cptr, xmlUTF8Strsize(cptr, 1));
-
- cptr++;
- if ( ch & 0x80 ) {
-
- if ( (ch & 0xc0) != 0xc0 ) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlXPathTranslateFunction: Invalid UTF8 string\n");
- break;
- }
-
- while ( (ch <<= 1) & 0x80 )
- if ( (*cptr++ & 0xc0) != 0x80 ) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlXPathTranslateFunction: Invalid UTF8 string\n");
- break;
- }
- if (ch & 0x80)
- break;
- }
- }
- }
- valuePush(ctxt, xmlXPathCacheNewString(ctxt->context,
- xmlBufferContent(target)));
- xmlBufferFree(target);
- xmlXPathReleaseObject(ctxt->context, str);
- xmlXPathReleaseObject(ctxt->context, from);
- xmlXPathReleaseObject(ctxt->context, to);
- }
- void
- xmlXPathBooleanFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlXPathObjectPtr cur;
- CHECK_ARITY(1);
- cur = valuePop(ctxt);
- if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND);
- cur = xmlXPathCacheConvertBoolean(ctxt->context, cur);
- valuePush(ctxt, cur);
- }
- void
- xmlXPathNotFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- CHECK_ARITY(1);
- CAST_TO_BOOLEAN;
- CHECK_TYPE(XPATH_BOOLEAN);
- ctxt->value->boolval = ! ctxt->value->boolval;
- }
- void
- xmlXPathTrueFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- CHECK_ARITY(0);
- valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, 1));
- }
- void
- xmlXPathFalseFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- CHECK_ARITY(0);
- valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, 0));
- }
- void
- xmlXPathLangFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlXPathObjectPtr val = NULL;
- const xmlChar *theLang = NULL;
- const xmlChar *lang;
- int ret = 0;
- int i;
- CHECK_ARITY(1);
- CAST_TO_STRING;
- CHECK_TYPE(XPATH_STRING);
- val = valuePop(ctxt);
- lang = val->stringval;
- theLang = xmlNodeGetLang(ctxt->context->node);
- if ((theLang != NULL) && (lang != NULL)) {
- for (i = 0;lang[i] != 0;i++)
- if (toupper(lang[i]) != toupper(theLang[i]))
- goto not_equal;
- if ((theLang[i] == 0) || (theLang[i] == '-'))
- ret = 1;
- }
- not_equal:
- if (theLang != NULL)
- xmlFree((void *)theLang);
- xmlXPathReleaseObject(ctxt->context, val);
- valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, ret));
- }
- void
- xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlXPathObjectPtr cur;
- double res;
- if (ctxt == NULL) return;
- if (nargs == 0) {
- if (ctxt->context->node == NULL) {
- valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, 0.0));
- } else {
- xmlChar* content = xmlNodeGetContent(ctxt->context->node);
- res = xmlXPathStringEvalNumber(content);
- valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, res));
- xmlFree(content);
- }
- return;
- }
- CHECK_ARITY(1);
- cur = valuePop(ctxt);
- valuePush(ctxt, xmlXPathCacheConvertNumber(ctxt->context, cur));
- }
- void
- xmlXPathSumFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlXPathObjectPtr cur;
- int i;
- double res = 0.0;
- CHECK_ARITY(1);
- if ((ctxt->value == NULL) ||
- ((ctxt->value->type != XPATH_NODESET) &&
- (ctxt->value->type != XPATH_XSLT_TREE)))
- XP_ERROR(XPATH_INVALID_TYPE);
- cur = valuePop(ctxt);
- if ((cur->nodesetval != NULL) && (cur->nodesetval->nodeNr != 0)) {
- for (i = 0; i < cur->nodesetval->nodeNr; i++) {
- res += xmlXPathCastNodeToNumber(cur->nodesetval->nodeTab[i]);
- }
- }
- valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, res));
- xmlXPathReleaseObject(ctxt->context, cur);
- }
- #define XTRUNC(f, v) \
- f = fmod((v), INT_MAX); \
- f = (v) - (f) + (double)((int)(f));
- void
- xmlXPathFloorFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- double f;
- CHECK_ARITY(1);
- CAST_TO_NUMBER;
- CHECK_TYPE(XPATH_NUMBER);
- XTRUNC(f, ctxt->value->floatval);
- if (f != ctxt->value->floatval) {
- if (ctxt->value->floatval > 0)
- ctxt->value->floatval = f;
- else
- ctxt->value->floatval = f - 1;
- }
- }
- void
- xmlXPathCeilingFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- double f;
- CHECK_ARITY(1);
- CAST_TO_NUMBER;
- CHECK_TYPE(XPATH_NUMBER);
- #if 0
- ctxt->value->floatval = ceil(ctxt->value->floatval);
- #else
- XTRUNC(f, ctxt->value->floatval);
- if (f != ctxt->value->floatval) {
- if (ctxt->value->floatval > 0)
- ctxt->value->floatval = f + 1;
- else {
- if (ctxt->value->floatval < 0 && f == 0)
- ctxt->value->floatval = xmlXPathNZERO;
- else
- ctxt->value->floatval = f;
- }
- }
- #endif
- }
- void
- xmlXPathRoundFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- double f;
- CHECK_ARITY(1);
- CAST_TO_NUMBER;
- CHECK_TYPE(XPATH_NUMBER);
- if ((xmlXPathIsNaN(ctxt->value->floatval)) ||
- (xmlXPathIsInf(ctxt->value->floatval) == 1) ||
- (xmlXPathIsInf(ctxt->value->floatval) == -1) ||
- (ctxt->value->floatval == 0.0))
- return;
- XTRUNC(f, ctxt->value->floatval);
- if (ctxt->value->floatval < 0) {
- if (ctxt->value->floatval < f - 0.5)
- ctxt->value->floatval = f - 1;
- else
- ctxt->value->floatval = f;
- if (ctxt->value->floatval == 0)
- ctxt->value->floatval = xmlXPathNZERO;
- } else {
- if (ctxt->value->floatval < f + 0.5)
- ctxt->value->floatval = f;
- else
- ctxt->value->floatval = f + 1;
- }
- }
- static void xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt, int sort);
- static void xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter);
- static void xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt);
- static void xmlXPathCompRelativeLocationPath(xmlXPathParserContextPtr ctxt);
- static xmlChar * xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt,
- int qualified);
- static int
- xmlXPathCurrentChar(xmlXPathParserContextPtr ctxt, int *len) {
- unsigned char c;
- unsigned int val;
- const xmlChar *cur;
- if (ctxt == NULL)
- return(0);
- cur = ctxt->cur;
-
- c = *cur;
- if (c & 0x80) {
- if ((cur[1] & 0xc0) != 0x80)
- goto encoding_error;
- if ((c & 0xe0) == 0xe0) {
- if ((cur[2] & 0xc0) != 0x80)
- goto encoding_error;
- if ((c & 0xf0) == 0xf0) {
- if (((c & 0xf8) != 0xf0) ||
- ((cur[3] & 0xc0) != 0x80))
- goto encoding_error;
-
- *len = 4;
- val = (cur[0] & 0x7) << 18;
- val |= (cur[1] & 0x3f) << 12;
- val |= (cur[2] & 0x3f) << 6;
- val |= cur[3] & 0x3f;
- } else {
-
- *len = 3;
- val = (cur[0] & 0xf) << 12;
- val |= (cur[1] & 0x3f) << 6;
- val |= cur[2] & 0x3f;
- }
- } else {
-
- *len = 2;
- val = (cur[0] & 0x1f) << 6;
- val |= cur[1] & 0x3f;
- }
- if (!IS_CHAR(val)) {
- XP_ERROR0(XPATH_INVALID_CHAR_ERROR);
- }
- return(val);
- } else {
-
- *len = 1;
- return((int) *cur);
- }
- encoding_error:
-
- *len = 0;
- XP_ERROR0(XPATH_ENCODING_ERROR);
- }
- xmlChar *
- xmlXPathParseNCName(xmlXPathParserContextPtr ctxt) {
- const xmlChar *in;
- xmlChar *ret;
- int count = 0;
- if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL);
-
- in = ctxt->cur;
- if (((*in >= 0x61) && (*in <= 0x7A)) ||
- ((*in >= 0x41) && (*in <= 0x5A)) ||
- (*in == '_')) {
- in++;
- while (((*in >= 0x61) && (*in <= 0x7A)) ||
- ((*in >= 0x41) && (*in <= 0x5A)) ||
- ((*in >= 0x30) && (*in <= 0x39)) ||
- (*in == '_') || (*in == '.') ||
- (*in == '-'))
- in++;
- if ((*in == ' ') || (*in == '>') || (*in == '/') ||
- (*in == '[') || (*in == ']') || (*in == ':') ||
- (*in == '@') || (*in == '*')) {
- count = in - ctxt->cur;
- if (count == 0)
- return(NULL);
- ret = xmlStrndup(ctxt->cur, count);
- ctxt->cur = in;
- return(ret);
- }
- }
- return(xmlXPathParseNameComplex(ctxt, 0));
- }
- static xmlChar *
- xmlXPathParseQName(xmlXPathParserContextPtr ctxt, xmlChar **prefix) {
- xmlChar *ret = NULL;
- *prefix = NULL;
- ret = xmlXPathParseNCName(ctxt);
- if (ret && CUR == ':') {
- *prefix = ret;
- NEXT;
- ret = xmlXPathParseNCName(ctxt);
- }
- return(ret);
- }
- xmlChar *
- xmlXPathParseName(xmlXPathParserContextPtr ctxt) {
- const xmlChar *in;
- xmlChar *ret;
- int count = 0;
- if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL);
-
- in = ctxt->cur;
- if (((*in >= 0x61) && (*in <= 0x7A)) ||
- ((*in >= 0x41) && (*in <= 0x5A)) ||
- (*in == '_') || (*in == ':')) {
- in++;
- while (((*in >= 0x61) && (*in <= 0x7A)) ||
- ((*in >= 0x41) && (*in <= 0x5A)) ||
- ((*in >= 0x30) && (*in <= 0x39)) ||
- (*in == '_') || (*in == '-') ||
- (*in == ':') || (*in == '.'))
- in++;
- if ((*in > 0) && (*in < 0x80)) {
- count = in - ctxt->cur;
- ret = xmlStrndup(ctxt->cur, count);
- ctxt->cur = in;
- return(ret);
- }
- }
- return(xmlXPathParseNameComplex(ctxt, 1));
- }
- static xmlChar *
- xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, int qualified) {
- xmlChar buf[XML_MAX_NAMELEN + 5];
- int len = 0, l;
- int c;
-
- c = CUR_CHAR(l);
- if ((c == ' ') || (c == '>') || (c == '/') ||
- (c == '[') || (c == ']') || (c == '@') ||
- (c == '*') ||
- (!IS_LETTER(c) && (c != '_') &&
- ((qualified) && (c != ':')))) {
- return(NULL);
- }
- while ((c != ' ') && (c != '>') && (c != '/') &&
- ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
- (c == '.') || (c == '-') ||
- (c == '_') || ((qualified) && (c == ':')) ||
- (IS_COMBINING(c)) ||
- (IS_EXTENDER(c)))) {
- COPY_BUF(l,buf,len,c);
- NEXTL(l);
- c = CUR_CHAR(l);
- if (len >= XML_MAX_NAMELEN) {
-
- xmlChar *buffer;
- int max = len * 2;
- buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
- if (buffer == NULL) {
- XP_ERRORNULL(XPATH_MEMORY_ERROR);
- }
- memcpy(buffer, buf, len);
- while ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
- (c == '.') || (c == '-') ||
- (c == '_') || ((qualified) && (c == ':')) ||
- (IS_COMBINING(c)) ||
- (IS_EXTENDER(c))) {
- if (len + 10 > max) {
- max *= 2;
- buffer = (xmlChar *) xmlRealloc(buffer,
- max * sizeof(xmlChar));
- if (buffer == NULL) {
- XP_ERRORNULL(XPATH_MEMORY_ERROR);
- }
- }
- COPY_BUF(l,buffer,len,c);
- NEXTL(l);
- c = CUR_CHAR(l);
- }
- buffer[len] = 0;
- return(buffer);
- }
- }
- if (len == 0)
- return(NULL);
- return(xmlStrndup(buf, len));
- }
- #define MAX_FRAC 20
- static double my_pow10[MAX_FRAC+1] = {
- 1.0, 10.0, 100.0, 1000.0, 10000.0,
- 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0,
- 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0,
- 100000000000000.0,
- 1000000000000000.0, 10000000000000000.0, 100000000000000000.0,
- 1000000000000000000.0, 10000000000000000000.0, 100000000000000000000.0
- };
- double
- xmlXPathStringEvalNumber(const xmlChar *str) {
- const xmlChar *cur = str;
- double ret;
- int ok = 0;
- int isneg = 0;
- int exponent = 0;
- int is_exponent_negative = 0;
- #ifdef __GNUC__
- unsigned long tmp = 0;
- double temp;
- #endif
- if (cur == NULL) return(0);
- while (IS_BLANK_CH(*cur)) cur++;
- if ((*cur != '.') && ((*cur < '0') || (*cur > '9')) && (*cur != '-')) {
- return(xmlXPathNAN);
- }
- if (*cur == '-') {
- isneg = 1;
- cur++;
- }
- #ifdef __GNUC__
-
- ret = 0;
- while ((*cur >= '0') && (*cur <= '9')) {
- ret = ret * 10;
- tmp = (*cur - '0');
- ok = 1;
- cur++;
- temp = (double) tmp;
- ret = ret + temp;
- }
- #else
- ret = 0;
- while ((*cur >= '0') && (*cur <= '9')) {
- ret = ret * 10 + (*cur - '0');
- ok = 1;
- cur++;
- }
- #endif
- if (*cur == '.') {
- int v, frac = 0;
- double fraction = 0;
- cur++;
- if (((*cur < '0') || (*cur > '9')) && (!ok)) {
- return(xmlXPathNAN);
- }
- while (((*cur >= '0') && (*cur <= '9')) && (frac < MAX_FRAC)) {
- v = (*cur - '0');
- fraction = fraction * 10 + v;
- frac = frac + 1;
- cur++;
- }
- fraction /= my_pow10[frac];
- ret = ret + fraction;
- while ((*cur >= '0') && (*cur <= '9'))
- cur++;
- }
- if ((*cur == 'e') || (*cur == 'E')) {
- cur++;
- if (*cur == '-') {
- is_exponent_negative = 1;
- cur++;
- } else if (*cur == '+') {
- cur++;
- }
- while ((*cur >= '0') && (*cur <= '9')) {
- exponent = exponent * 10 + (*cur - '0');
- cur++;
- }
- }
- while (IS_BLANK_CH(*cur)) cur++;
- if (*cur != 0) return(xmlXPathNAN);
- if (isneg) ret = -ret;
- if (is_exponent_negative) exponent = -exponent;
- ret *= pow(10.0, (double)exponent);
- return(ret);
- }
- static void
- xmlXPathCompNumber(xmlXPathParserContextPtr ctxt)
- {
- double ret = 0.0;
- double mult = 1;
- int ok = 0;
- int exponent = 0;
- int is_exponent_negative = 0;
- #ifdef __GNUC__
- unsigned long tmp = 0;
- double temp;
- #endif
- CHECK_ERROR;
- if ((CUR != '.') && ((CUR < '0') || (CUR > '9'))) {
- XP_ERROR(XPATH_NUMBER_ERROR);
- }
- #ifdef __GNUC__
-
- ret = 0;
- while ((CUR >= '0') && (CUR <= '9')) {
- ret = ret * 10;
- tmp = (CUR - '0');
- ok = 1;
- NEXT;
- temp = (double) tmp;
- ret = ret + temp;
- }
- #else
- ret = 0;
- while ((CUR >= '0') && (CUR <= '9')) {
- ret = ret * 10 + (CUR - '0');
- ok = 1;
- NEXT;
- }
- #endif
- if (CUR == '.') {
- NEXT;
- if (((CUR < '0') || (CUR > '9')) && (!ok)) {
- XP_ERROR(XPATH_NUMBER_ERROR);
- }
- while ((CUR >= '0') && (CUR <= '9')) {
- mult /= 10;
- ret = ret + (CUR - '0') * mult;
- NEXT;
- }
- }
- if ((CUR == 'e') || (CUR == 'E')) {
- NEXT;
- if (CUR == '-') {
- is_exponent_negative = 1;
- NEXT;
- } else if (CUR == '+') {
- NEXT;
- }
- while ((CUR >= '0') && (CUR <= '9')) {
- exponent = exponent * 10 + (CUR - '0');
- NEXT;
- }
- if (is_exponent_negative)
- exponent = -exponent;
- ret *= pow(10.0, (double) exponent);
- }
- PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_NUMBER, 0, 0,
- xmlXPathCacheNewFloat(ctxt->context, ret), NULL);
- }
- static xmlChar *
- xmlXPathParseLiteral(xmlXPathParserContextPtr ctxt) {
- const xmlChar *q;
- xmlChar *ret = NULL;
- if (CUR == '"') {
- NEXT;
- q = CUR_PTR;
- while ((IS_CHAR_CH(CUR)) && (CUR != '"'))
- NEXT;
- if (!IS_CHAR_CH(CUR)) {
- XP_ERRORNULL(XPATH_UNFINISHED_LITERAL_ERROR);
- } else {
- ret = xmlStrndup(q, CUR_PTR - q);
- NEXT;
- }
- } else if (CUR == '\'') {
- NEXT;
- q = CUR_PTR;
- while ((IS_CHAR_CH(CUR)) && (CUR != '\''))
- NEXT;
- if (!IS_CHAR_CH(CUR)) {
- XP_ERRORNULL(XPATH_UNFINISHED_LITERAL_ERROR);
- } else {
- ret = xmlStrndup(q, CUR_PTR - q);
- NEXT;
- }
- } else {
- XP_ERRORNULL(XPATH_START_LITERAL_ERROR);
- }
- return(ret);
- }
- static void
- xmlXPathCompLiteral(xmlXPathParserContextPtr ctxt) {
- const xmlChar *q;
- xmlChar *ret = NULL;
- if (CUR == '"') {
- NEXT;
- q = CUR_PTR;
- while ((IS_CHAR_CH(CUR)) && (CUR != '"'))
- NEXT;
- if (!IS_CHAR_CH(CUR)) {
- XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR);
- } else {
- ret = xmlStrndup(q, CUR_PTR - q);
- NEXT;
- }
- } else if (CUR == '\'') {
- NEXT;
- q = CUR_PTR;
- while ((IS_CHAR_CH(CUR)) && (CUR != '\''))
- NEXT;
- if (!IS_CHAR_CH(CUR)) {
- XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR);
- } else {
- ret = xmlStrndup(q, CUR_PTR - q);
- NEXT;
- }
- } else {
- XP_ERROR(XPATH_START_LITERAL_ERROR);
- }
- if (ret == NULL) return;
- PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_STRING, 0, 0,
- xmlXPathCacheNewString(ctxt->context, ret), NULL);
- xmlFree(ret);
- }
- static void
- xmlXPathCompVariableReference(xmlXPathParserContextPtr ctxt) {
- xmlChar *name;
- xmlChar *prefix;
- SKIP_BLANKS;
- if (CUR != '$') {
- XP_ERROR(XPATH_VARIABLE_REF_ERROR);
- }
- NEXT;
- name = xmlXPathParseQName(ctxt, &prefix);
- if (name == NULL) {
- XP_ERROR(XPATH_VARIABLE_REF_ERROR);
- }
- ctxt->comp->last = -1;
- PUSH_LONG_EXPR(XPATH_OP_VARIABLE, 0, 0, 0,
- name, prefix);
- SKIP_BLANKS;
- if ((ctxt->context != NULL) && (ctxt->context->flags & XML_XPATH_NOVAR)) {
- XP_ERROR(XPATH_UNDEF_VARIABLE_ERROR);
- }
- }
- int
- xmlXPathIsNodeType(const xmlChar *name) {
- if (name == NULL)
- return(0);
- if (xmlStrEqual(name, BAD_CAST "node"))
- return(1);
- if (xmlStrEqual(name, BAD_CAST "text"))
- return(1);
- if (xmlStrEqual(name, BAD_CAST "comment"))
- return(1);
- if (xmlStrEqual(name, BAD_CAST "processing-instruction"))
- return(1);
- return(0);
- }
- static void
- xmlXPathCompFunctionCall(xmlXPathParserContextPtr ctxt) {
- xmlChar *name;
- xmlChar *prefix;
- int nbargs = 0;
- int sort = 1;
- name = xmlXPathParseQName(ctxt, &prefix);
- if (name == NULL) {
- xmlFree(prefix);
- XP_ERROR(XPATH_EXPR_ERROR);
- }
- SKIP_BLANKS;
- #ifdef DEBUG_EXPR
- if (prefix == NULL)
- xmlGenericError(xmlGenericErrorContext, "Calling function %s\n",
- name);
- else
- xmlGenericError(xmlGenericErrorContext, "Calling function %s:%s\n",
- prefix, name);
- #endif
- if (CUR != '(') {
- XP_ERROR(XPATH_EXPR_ERROR);
- }
- NEXT;
- SKIP_BLANKS;
-
- if ((prefix == NULL) && (name[0] == 'c') &&
- xmlStrEqual(name, BAD_CAST "count"))
- {
- sort = 0;
- }
- ctxt->comp->last = -1;
- if (CUR != ')') {
- while (CUR != 0) {
- int op1 = ctxt->comp->last;
- ctxt->comp->last = -1;
- xmlXPathCompileExpr(ctxt, sort);
- if (ctxt->error != XPATH_EXPRESSION_OK) {
- xmlFree(name);
- xmlFree(prefix);
- return;
- }
- PUSH_BINARY_EXPR(XPATH_OP_ARG, op1, ctxt->comp->last, 0, 0);
- nbargs++;
- if (CUR == ')') break;
- if (CUR != ',') {
- XP_ERROR(XPATH_EXPR_ERROR);
- }
- NEXT;
- SKIP_BLANKS;
- }
- }
- PUSH_LONG_EXPR(XPATH_OP_FUNCTION, nbargs, 0, 0,
- name, prefix);
- NEXT;
- SKIP_BLANKS;
- }
- static void
- xmlXPathCompPrimaryExpr(xmlXPathParserContextPtr ctxt) {
- SKIP_BLANKS;
- if (CUR == '$') xmlXPathCompVariableReference(ctxt);
- else if (CUR == '(') {
- NEXT;
- SKIP_BLANKS;
- xmlXPathCompileExpr(ctxt, 1);
- CHECK_ERROR;
- if (CUR != ')') {
- XP_ERROR(XPATH_EXPR_ERROR);
- }
- NEXT;
- SKIP_BLANKS;
- } else if (IS_ASCII_DIGIT(CUR) || (CUR == '.' && IS_ASCII_DIGIT(NXT(1)))) {
- xmlXPathCompNumber(ctxt);
- } else if ((CUR == '\'') || (CUR == '"')) {
- xmlXPathCompLiteral(ctxt);
- } else {
- xmlXPathCompFunctionCall(ctxt);
- }
- SKIP_BLANKS;
- }
- static void
- xmlXPathCompFilterExpr(xmlXPathParserContextPtr ctxt) {
- xmlXPathCompPrimaryExpr(ctxt);
- CHECK_ERROR;
- SKIP_BLANKS;
- while (CUR == '[') {
- xmlXPathCompPredicate(ctxt, 1);
- SKIP_BLANKS;
- }
- }
- static xmlChar *
- xmlXPathScanName(xmlXPathParserContextPtr ctxt) {
- int len = 0, l;
- int c;
- const xmlChar *cur;
- xmlChar *ret;
- cur = ctxt->cur;
- c = CUR_CHAR(l);
- if ((c == ' ') || (c == '>') || (c == '/') ||
- (!IS_LETTER(c) && (c != '_') &&
- (c != ':'))) {
- return(NULL);
- }
- while ((c != ' ') && (c != '>') && (c != '/') &&
- ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
- (c == '.') || (c == '-') ||
- (c == '_') || (c == ':') ||
- (IS_COMBINING(c)) ||
- (IS_EXTENDER(c)))) {
- len += l;
- NEXTL(l);
- c = CUR_CHAR(l);
- }
- ret = xmlStrndup(cur, ctxt->cur - cur);
- ctxt->cur = cur;
- return(ret);
- }
- static void
- xmlXPathCompPathExpr(xmlXPathParserContextPtr ctxt) {
- int lc = 1;
- xmlChar *name = NULL;
- SKIP_BLANKS;
- if ((CUR == '$') || (CUR == '(') ||
- (IS_ASCII_DIGIT(CUR)) ||
- (CUR == '\'') || (CUR == '"') ||
- (CUR == '.' && IS_ASCII_DIGIT(NXT(1)))) {
- lc = 0;
- } else if (CUR == '*') {
-
- lc = 1;
- } else if (CUR == '/') {
-
- lc = 1;
- } else if (CUR == '@') {
-
- lc = 1;
- } else if (CUR == '.') {
-
- lc = 1;
- } else {
-
- SKIP_BLANKS;
- name = xmlXPathScanName(ctxt);
- if ((name != NULL) && (xmlStrstr(name, (xmlChar *) "::") != NULL)) {
- #ifdef DEBUG_STEP
- xmlGenericError(xmlGenericErrorContext,
- "PathExpr: Axis\n");
- #endif
- lc = 1;
- xmlFree(name);
- } else if (name != NULL) {
- int len =xmlStrlen(name);
- while (NXT(len) != 0) {
- if (NXT(len) == '/') {
-
- #ifdef DEBUG_STEP
- xmlGenericError(xmlGenericErrorContext,
- "PathExpr: AbbrRelLocation\n");
- #endif
- lc = 1;
- break;
- } else if (IS_BLANK_CH(NXT(len))) {
-
- ;
- } else if (NXT(len) == ':') {
- #ifdef DEBUG_STEP
- xmlGenericError(xmlGenericErrorContext,
- "PathExpr: AbbrRelLocation\n");
- #endif
- lc = 1;
- break;
- } else if ((NXT(len) == '(')) {
-
- if (xmlXPathIsNodeType(name)) {
- #ifdef DEBUG_STEP
- xmlGenericError(xmlGenericErrorContext,
- "PathExpr: Type search\n");
- #endif
- lc = 1;
- } else {
- #ifdef DEBUG_STEP
- xmlGenericError(xmlGenericErrorContext,
- "PathExpr: function call\n");
- #endif
- lc = 0;
- }
- break;
- } else if ((NXT(len) == '[')) {
-
- #ifdef DEBUG_STEP
- xmlGenericError(xmlGenericErrorContext,
- "PathExpr: AbbrRelLocation\n");
- #endif
- lc = 1;
- break;
- } else if ((NXT(len) == '<') || (NXT(len) == '>') ||
- (NXT(len) == '=')) {
- lc = 1;
- break;
- } else {
- lc = 1;
- break;
- }
- len++;
- }
- if (NXT(len) == 0) {
- #ifdef DEBUG_STEP
- xmlGenericError(xmlGenericErrorContext,
- "PathExpr: AbbrRelLocation\n");
- #endif
-
- lc = 1;
- }
- xmlFree(name);
- } else {
-
- XP_ERROR(XPATH_EXPR_ERROR);
- }
- }
- if (lc) {
- if (CUR == '/') {
- PUSH_LEAVE_EXPR(XPATH_OP_ROOT, 0, 0);
- } else {
- PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0);
- }
- xmlXPathCompLocationPath(ctxt);
- } else {
- xmlXPathCompFilterExpr(ctxt);
- CHECK_ERROR;
- if ((CUR == '/') && (NXT(1) == '/')) {
- SKIP(2);
- SKIP_BLANKS;
- PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
- NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
- PUSH_UNARY_EXPR(XPATH_OP_RESET, ctxt->comp->last, 1, 0);
- xmlXPathCompRelativeLocationPath(ctxt);
- } else if (CUR == '/') {
- xmlXPathCompRelativeLocationPath(ctxt);
- }
- }
- SKIP_BLANKS;
- }
- static void
- xmlXPathCompUnionExpr(xmlXPathParserContextPtr ctxt) {
- xmlXPathCompPathExpr(ctxt);
- CHECK_ERROR;
- SKIP_BLANKS;
- while (CUR == '|') {
- int op1 = ctxt->comp->last;
- PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0);
- NEXT;
- SKIP_BLANKS;
- xmlXPathCompPathExpr(ctxt);
- PUSH_BINARY_EXPR(XPATH_OP_UNION, op1, ctxt->comp->last, 0, 0);
- SKIP_BLANKS;
- }
- }
- static void
- xmlXPathCompUnaryExpr(xmlXPathParserContextPtr ctxt) {
- int minus = 0;
- int found = 0;
- SKIP_BLANKS;
- while (CUR == '-') {
- minus = 1 - minus;
- found = 1;
- NEXT;
- SKIP_BLANKS;
- }
- xmlXPathCompUnionExpr(ctxt);
- CHECK_ERROR;
- if (found) {
- if (minus)
- PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 2, 0);
- else
- PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 3, 0);
- }
- }
- static void
- xmlXPathCompMultiplicativeExpr(xmlXPathParserContextPtr ctxt) {
- xmlXPathCompUnaryExpr(ctxt);
- CHECK_ERROR;
- SKIP_BLANKS;
- while ((CUR == '*') ||
- ((CUR == 'd') && (NXT(1) == 'i') && (NXT(2) == 'v')) ||
- ((CUR == 'm') && (NXT(1) == 'o') && (NXT(2) == 'd'))) {
- int op = -1;
- int op1 = ctxt->comp->last;
- if (CUR == '*') {
- op = 0;
- NEXT;
- } else if (CUR == 'd') {
- op = 1;
- SKIP(3);
- } else if (CUR == 'm') {
- op = 2;
- SKIP(3);
- }
- SKIP_BLANKS;
- xmlXPathCompUnaryExpr(ctxt);
- CHECK_ERROR;
- PUSH_BINARY_EXPR(XPATH_OP_MULT, op1, ctxt->comp->last, op, 0);
- SKIP_BLANKS;
- }
- }
- static void
- xmlXPathCompAdditiveExpr(xmlXPathParserContextPtr ctxt) {
- xmlXPathCompMultiplicativeExpr(ctxt);
- CHECK_ERROR;
- SKIP_BLANKS;
- while ((CUR == '+') || (CUR == '-')) {
- int plus;
- int op1 = ctxt->comp->last;
- if (CUR == '+') plus = 1;
- else plus = 0;
- NEXT;
- SKIP_BLANKS;
- xmlXPathCompMultiplicativeExpr(ctxt);
- CHECK_ERROR;
- PUSH_BINARY_EXPR(XPATH_OP_PLUS, op1, ctxt->comp->last, plus, 0);
- SKIP_BLANKS;
- }
- }
- static void
- xmlXPathCompRelationalExpr(xmlXPathParserContextPtr ctxt) {
- xmlXPathCompAdditiveExpr(ctxt);
- CHECK_ERROR;
- SKIP_BLANKS;
- while ((CUR == '<') ||
- (CUR == '>') ||
- ((CUR == '<') && (NXT(1) == '=')) ||
- ((CUR == '>') && (NXT(1) == '='))) {
- int inf, strict;
- int op1 = ctxt->comp->last;
- if (CUR == '<') inf = 1;
- else inf = 0;
- if (NXT(1) == '=') strict = 0;
- else strict = 1;
- NEXT;
- if (!strict) NEXT;
- SKIP_BLANKS;
- xmlXPathCompAdditiveExpr(ctxt);
- CHECK_ERROR;
- PUSH_BINARY_EXPR(XPATH_OP_CMP, op1, ctxt->comp->last, inf, strict);
- SKIP_BLANKS;
- }
- }
- static void
- xmlXPathCompEqualityExpr(xmlXPathParserContextPtr ctxt) {
- xmlXPathCompRelationalExpr(ctxt);
- CHECK_ERROR;
- SKIP_BLANKS;
- while ((CUR == '=') || ((CUR == '!') && (NXT(1) == '='))) {
- int eq;
- int op1 = ctxt->comp->last;
- if (CUR == '=') eq = 1;
- else eq = 0;
- NEXT;
- if (!eq) NEXT;
- SKIP_BLANKS;
- xmlXPathCompRelationalExpr(ctxt);
- CHECK_ERROR;
- PUSH_BINARY_EXPR(XPATH_OP_EQUAL, op1, ctxt->comp->last, eq, 0);
- SKIP_BLANKS;
- }
- }
- static void
- xmlXPathCompAndExpr(xmlXPathParserContextPtr ctxt) {
- xmlXPathCompEqualityExpr(ctxt);
- CHECK_ERROR;
- SKIP_BLANKS;
- while ((CUR == 'a') && (NXT(1) == 'n') && (NXT(2) == 'd')) {
- int op1 = ctxt->comp->last;
- SKIP(3);
- SKIP_BLANKS;
- xmlXPathCompEqualityExpr(ctxt);
- CHECK_ERROR;
- PUSH_BINARY_EXPR(XPATH_OP_AND, op1, ctxt->comp->last, 0, 0);
- SKIP_BLANKS;
- }
- }
- static void
- xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt, int sort) {
- xmlXPathCompAndExpr(ctxt);
- CHECK_ERROR;
- SKIP_BLANKS;
- while ((CUR == 'o') && (NXT(1) == 'r')) {
- int op1 = ctxt->comp->last;
- SKIP(2);
- SKIP_BLANKS;
- xmlXPathCompAndExpr(ctxt);
- CHECK_ERROR;
- PUSH_BINARY_EXPR(XPATH_OP_OR, op1, ctxt->comp->last, 0, 0);
- SKIP_BLANKS;
- }
- if ((sort) && (ctxt->comp->steps[ctxt->comp->last].op != XPATH_OP_VALUE)) {
-
-
- PUSH_UNARY_EXPR(XPATH_OP_SORT, ctxt->comp->last , 0, 0);
- }
- }
- static void
- xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter) {
- int op1 = ctxt->comp->last;
- SKIP_BLANKS;
- if (CUR != '[') {
- XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
- }
- NEXT;
- SKIP_BLANKS;
- ctxt->comp->last = -1;
-
- if (! filter)
- xmlXPathCompileExpr(ctxt, 0);
- else
- xmlXPathCompileExpr(ctxt, 1);
- CHECK_ERROR;
- if (CUR != ']') {
- XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
- }
- if (filter)
- PUSH_BINARY_EXPR(XPATH_OP_FILTER, op1, ctxt->comp->last, 0, 0);
- else
- PUSH_BINARY_EXPR(XPATH_OP_PREDICATE, op1, ctxt->comp->last, 0, 0);
- NEXT;
- SKIP_BLANKS;
- }
- static xmlChar *
- xmlXPathCompNodeTest(xmlXPathParserContextPtr ctxt, xmlXPathTestVal *test,
- xmlXPathTypeVal *type, const xmlChar **prefix,
- xmlChar *name) {
- int blanks;
- if ((test == NULL) || (type == NULL) || (prefix == NULL)) {
- STRANGE;
- return(NULL);
- }
- *type = (xmlXPathTypeVal) 0;
- *test = (xmlXPathTestVal) 0;
- *prefix = NULL;
- SKIP_BLANKS;
- if ((name == NULL) && (CUR == '*')) {
-
- NEXT;
- *test = NODE_TEST_ALL;
- return(NULL);
- }
- if (name == NULL)
- name = xmlXPathParseNCName(ctxt);
- if (name == NULL) {
- XP_ERRORNULL(XPATH_EXPR_ERROR);
- }
- blanks = IS_BLANK_CH(CUR);
- SKIP_BLANKS;
- if (CUR == '(') {
- NEXT;
-
- if (xmlStrEqual(name, BAD_CAST "comment"))
- *type = NODE_TYPE_COMMENT;
- else if (xmlStrEqual(name, BAD_CAST "node"))
- *type = NODE_TYPE_NODE;
- else if (xmlStrEqual(name, BAD_CAST "processing-instruction"))
- *type = NODE_TYPE_PI;
- else if (xmlStrEqual(name, BAD_CAST "text"))
- *type = NODE_TYPE_TEXT;
- else {
- if (name != NULL)
- xmlFree(name);
- XP_ERRORNULL(XPATH_EXPR_ERROR);
- }
- *test = NODE_TEST_TYPE;
- SKIP_BLANKS;
- if (*type == NODE_TYPE_PI) {
-
- if (name != NULL)
- xmlFree(name);
- name = NULL;
- if (CUR != ')') {
- name = xmlXPathParseLiteral(ctxt);
- CHECK_ERROR NULL;
- *test = NODE_TEST_PI;
- SKIP_BLANKS;
- }
- }
- if (CUR != ')') {
- if (name != NULL)
- xmlFree(name);
- XP_ERRORNULL(XPATH_UNCLOSED_ERROR);
- }
- NEXT;
- return(name);
- }
- *test = NODE_TEST_NAME;
- if ((!blanks) && (CUR == ':')) {
- NEXT;
-
- #if 0
- *prefix = xmlXPathNsLookup(ctxt->context, name);
- if (name != NULL)
- xmlFree(name);
- if (*prefix == NULL) {
- XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR);
- }
- #else
- *prefix = name;
- #endif
- if (CUR == '*') {
-
- NEXT;
- *test = NODE_TEST_ALL;
- return(NULL);
- }
- name = xmlXPathParseNCName(ctxt);
- if (name == NULL) {
- XP_ERRORNULL(XPATH_EXPR_ERROR);
- }
- }
- return(name);
- }
- static xmlXPathAxisVal
- xmlXPathIsAxisName(const xmlChar *name) {
- xmlXPathAxisVal ret = (xmlXPathAxisVal) 0;
- switch (name[0]) {
- case 'a':
- if (xmlStrEqual(name, BAD_CAST "ancestor"))
- ret = AXIS_ANCESTOR;
- if (xmlStrEqual(name, BAD_CAST "ancestor-or-self"))
- ret = AXIS_ANCESTOR_OR_SELF;
- if (xmlStrEqual(name, BAD_CAST "attribute"))
- ret = AXIS_ATTRIBUTE;
- break;
- case 'c':
- if (xmlStrEqual(name, BAD_CAST "child"))
- ret = AXIS_CHILD;
- break;
- case 'd':
- if (xmlStrEqual(name, BAD_CAST "descendant"))
- ret = AXIS_DESCENDANT;
- if (xmlStrEqual(name, BAD_CAST "descendant-or-self"))
- ret = AXIS_DESCENDANT_OR_SELF;
- break;
- case 'f':
- if (xmlStrEqual(name, BAD_CAST "following"))
- ret = AXIS_FOLLOWING;
- if (xmlStrEqual(name, BAD_CAST "following-sibling"))
- ret = AXIS_FOLLOWING_SIBLING;
- break;
- case 'n':
- if (xmlStrEqual(name, BAD_CAST "namespace"))
- ret = AXIS_NAMESPACE;
- break;
- case 'p':
- if (xmlStrEqual(name, BAD_CAST "parent"))
- ret = AXIS_PARENT;
- if (xmlStrEqual(name, BAD_CAST "preceding"))
- ret = AXIS_PRECEDING;
- if (xmlStrEqual(name, BAD_CAST "preceding-sibling"))
- ret = AXIS_PRECEDING_SIBLING;
- break;
- case 's':
- if (xmlStrEqual(name, BAD_CAST "self"))
- ret = AXIS_SELF;
- break;
- }
- return(ret);
- }
- static void
- xmlXPathCompStep(xmlXPathParserContextPtr ctxt) {
- #ifdef LIBXML_XPTR_ENABLED
- int rangeto = 0;
- int op2 = -1;
- #endif
- SKIP_BLANKS;
- if ((CUR == '.') && (NXT(1) == '.')) {
- SKIP(2);
- SKIP_BLANKS;
- PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_PARENT,
- NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
- } else if (CUR == '.') {
- NEXT;
- SKIP_BLANKS;
- } else {
- xmlChar *name = NULL;
- const xmlChar *prefix = NULL;
- xmlXPathTestVal test = (xmlXPathTestVal) 0;
- xmlXPathAxisVal axis = (xmlXPathAxisVal) 0;
- xmlXPathTypeVal type = (xmlXPathTypeVal) 0;
- int op1;
-
- #ifdef LIBXML_XPTR_ENABLED
- if (ctxt->xptr) {
- name = xmlXPathParseNCName(ctxt);
- if ((name != NULL) && (xmlStrEqual(name, BAD_CAST "range-to"))) {
- op2 = ctxt->comp->last;
- xmlFree(name);
- SKIP_BLANKS;
- if (CUR != '(') {
- XP_ERROR(XPATH_EXPR_ERROR);
- }
- NEXT;
- SKIP_BLANKS;
- xmlXPathCompileExpr(ctxt, 1);
-
- CHECK_ERROR;
- SKIP_BLANKS;
- if (CUR != ')') {
- XP_ERROR(XPATH_EXPR_ERROR);
- }
- NEXT;
- rangeto = 1;
- goto eval_predicates;
- }
- }
- #endif
- if (CUR == '*') {
- axis = AXIS_CHILD;
- } else {
- if (name == NULL)
- name = xmlXPathParseNCName(ctxt);
- if (name != NULL) {
- axis = xmlXPathIsAxisName(name);
- if (axis != 0) {
- SKIP_BLANKS;
- if ((CUR == ':') && (NXT(1) == ':')) {
- SKIP(2);
- xmlFree(name);
- name = NULL;
- } else {
-
- axis = AXIS_CHILD;
- }
- } else {
- axis = AXIS_CHILD;
- }
- } else if (CUR == '@') {
- NEXT;
- axis = AXIS_ATTRIBUTE;
- } else {
- axis = AXIS_CHILD;
- }
- }
- CHECK_ERROR;
- name = xmlXPathCompNodeTest(ctxt, &test, &type, &prefix, name);
- if (test == 0)
- return;
- if ((prefix != NULL) && (ctxt->context != NULL) &&
- (ctxt->context->flags & XML_XPATH_CHECKNS)) {
- if (xmlXPathNsLookup(ctxt->context, prefix) == NULL) {
- xmlXPathErr(ctxt, XPATH_UNDEF_PREFIX_ERROR);
- }
- }
- #ifdef DEBUG_STEP
- xmlGenericError(xmlGenericErrorContext,
- "Basis : computing new set\n");
- #endif
- #ifdef DEBUG_STEP
- xmlGenericError(xmlGenericErrorContext, "Basis : ");
- if (ctxt->value == NULL)
- xmlGenericError(xmlGenericErrorContext, "no value\n");
- else if (ctxt->value->nodesetval == NULL)
- xmlGenericError(xmlGenericErrorContext, "Empty\n");
- else
- xmlGenericErrorContextNodeSet(stdout, ctxt->value->nodesetval);
- #endif
- #ifdef LIBXML_XPTR_ENABLED
- eval_predicates:
- #endif
- op1 = ctxt->comp->last;
- ctxt->comp->last = -1;
- SKIP_BLANKS;
- while (CUR == '[') {
- xmlXPathCompPredicate(ctxt, 0);
- }
- #ifdef LIBXML_XPTR_ENABLED
- if (rangeto) {
- PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, op1, 0, 0);
- } else
- #endif
- PUSH_FULL_EXPR(XPATH_OP_COLLECT, op1, ctxt->comp->last, axis,
- test, type, (void *)prefix, (void *)name);
- }
- #ifdef DEBUG_STEP
- xmlGenericError(xmlGenericErrorContext, "Step : ");
- if (ctxt->value == NULL)
- xmlGenericError(xmlGenericErrorContext, "no value\n");
- else if (ctxt->value->nodesetval == NULL)
- xmlGenericError(xmlGenericErrorContext, "Empty\n");
- else
- xmlGenericErrorContextNodeSet(xmlGenericErrorContext,
- ctxt->value->nodesetval);
- #endif
- }
- static void
- xmlXPathCompRelativeLocationPath
- (xmlXPathParserContextPtr ctxt) {
- SKIP_BLANKS;
- if ((CUR == '/') && (NXT(1) == '/')) {
- SKIP(2);
- SKIP_BLANKS;
- PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
- NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
- } else if (CUR == '/') {
- NEXT;
- SKIP_BLANKS;
- }
- xmlXPathCompStep(ctxt);
- SKIP_BLANKS;
- while (CUR == '/') {
- if ((CUR == '/') && (NXT(1) == '/')) {
- SKIP(2);
- SKIP_BLANKS;
- PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
- NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
- xmlXPathCompStep(ctxt);
- } else if (CUR == '/') {
- NEXT;
- SKIP_BLANKS;
- xmlXPathCompStep(ctxt);
- }
- SKIP_BLANKS;
- }
- }
- static void
- xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt) {
- SKIP_BLANKS;
- if (CUR != '/') {
- xmlXPathCompRelativeLocationPath(ctxt);
- } else {
- while (CUR == '/') {
- if ((CUR == '/') && (NXT(1) == '/')) {
- SKIP(2);
- SKIP_BLANKS;
- PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
- NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
- xmlXPathCompRelativeLocationPath(ctxt);
- } else if (CUR == '/') {
- NEXT;
- SKIP_BLANKS;
- if ((CUR != 0 ) &&
- ((IS_ASCII_LETTER(CUR)) || (CUR == '_') || (CUR == '.') ||
- (CUR == '@') || (CUR == '*')))
- xmlXPathCompRelativeLocationPath(ctxt);
- }
- }
- }
- }
- static int
- xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op);
- #ifdef DEBUG_STEP
- static void
- xmlXPathDebugDumpStepAxis(xmlXPathStepOpPtr op,
- int nbNodes)
- {
- xmlGenericError(xmlGenericErrorContext, "new step : ");
- switch (op->value) {
- case AXIS_ANCESTOR:
- xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' ");
- break;
- case AXIS_ANCESTOR_OR_SELF:
- xmlGenericError(xmlGenericErrorContext,
- "axis 'ancestors-or-self' ");
- break;
- case AXIS_ATTRIBUTE:
- xmlGenericError(xmlGenericErrorContext, "axis 'attributes' ");
- break;
- case AXIS_CHILD:
- xmlGenericError(xmlGenericErrorContext, "axis 'child' ");
- break;
- case AXIS_DESCENDANT:
- xmlGenericError(xmlGenericErrorContext, "axis 'descendant' ");
- break;
- case AXIS_DESCENDANT_OR_SELF:
- xmlGenericError(xmlGenericErrorContext,
- "axis 'descendant-or-self' ");
- break;
- case AXIS_FOLLOWING:
- xmlGenericError(xmlGenericErrorContext, "axis 'following' ");
- break;
- case AXIS_FOLLOWING_SIBLING:
- xmlGenericError(xmlGenericErrorContext,
- "axis 'following-siblings' ");
- break;
- case AXIS_NAMESPACE:
- xmlGenericError(xmlGenericErrorContext, "axis 'namespace' ");
- break;
- case AXIS_PARENT:
- xmlGenericError(xmlGenericErrorContext, "axis 'parent' ");
- break;
- case AXIS_PRECEDING:
- xmlGenericError(xmlGenericErrorContext, "axis 'preceding' ");
- break;
- case AXIS_PRECEDING_SIBLING:
- xmlGenericError(xmlGenericErrorContext,
- "axis 'preceding-sibling' ");
- break;
- case AXIS_SELF:
- xmlGenericError(xmlGenericErrorContext, "axis 'self' ");
- break;
- }
- xmlGenericError(xmlGenericErrorContext,
- " context contains %d nodes\n", nbNodes);
- switch (op->value2) {
- case NODE_TEST_NONE:
- xmlGenericError(xmlGenericErrorContext,
- " searching for none !!!\n");
- break;
- case NODE_TEST_TYPE:
- xmlGenericError(xmlGenericErrorContext,
- " searching for type %d\n", op->value3);
- break;
- case NODE_TEST_PI:
- xmlGenericError(xmlGenericErrorContext,
- " searching for PI !!!\n");
- break;
- case NODE_TEST_ALL:
- xmlGenericError(xmlGenericErrorContext,
- " searching for *\n");
- break;
- case NODE_TEST_NS:
- xmlGenericError(xmlGenericErrorContext,
- " searching for namespace %s\n",
- op->value5);
- break;
- case NODE_TEST_NAME:
- xmlGenericError(xmlGenericErrorContext,
- " searching for name %s\n", op->value5);
- if (op->value4)
- xmlGenericError(xmlGenericErrorContext,
- " with namespace %s\n", op->value4);
- break;
- }
- xmlGenericError(xmlGenericErrorContext, "Testing : ");
- }
- #endif
- static int
- xmlXPathCompOpEvalPredicate(xmlXPathParserContextPtr ctxt,
- xmlXPathStepOpPtr op,
- xmlNodeSetPtr set,
- int contextSize,
- int hasNsNodes)
- {
- if (op->ch1 != -1) {
- xmlXPathCompExprPtr comp = ctxt->comp;
-
- if (comp->steps[op->ch1].op != XPATH_OP_PREDICATE) {
-
- }
- contextSize = xmlXPathCompOpEvalPredicate(ctxt,
- &comp->steps[op->ch1], set, contextSize, hasNsNodes);
- CHECK_ERROR0;
- if (contextSize <= 0)
- return(0);
- }
- if (op->ch2 != -1) {
- xmlXPathContextPtr xpctxt = ctxt->context;
- xmlNodePtr contextNode, oldContextNode;
- xmlDocPtr oldContextDoc;
- int i, res, contextPos = 0, newContextSize;
- xmlXPathStepOpPtr exprOp;
- xmlXPathObjectPtr contextObj = NULL, exprRes = NULL;
- #ifdef LIBXML_XPTR_ENABLED
-
- #endif
-
- oldContextNode = xpctxt->node;
- oldContextDoc = xpctxt->doc;
-
- exprOp = &ctxt->comp->steps[op->ch2];
- newContextSize = 0;
- for (i = 0; i < set->nodeNr; i++) {
- if (set->nodeTab[i] == NULL)
- continue;
- contextNode = set->nodeTab[i];
- xpctxt->node = contextNode;
- xpctxt->contextSize = contextSize;
- xpctxt->proximityPosition = ++contextPos;
-
- if ((contextNode->type != XML_NAMESPACE_DECL) &&
- (contextNode->doc != NULL))
- xpctxt->doc = contextNode->doc;
-
- if (contextObj == NULL)
- contextObj = xmlXPathCacheNewNodeSet(xpctxt, contextNode);
- else
- xmlXPathNodeSetAddUnique(contextObj->nodesetval,
- contextNode);
- valuePush(ctxt, contextObj);
- res = xmlXPathCompOpEvalToBoolean(ctxt, exprOp, 1);
- if ((ctxt->error != XPATH_EXPRESSION_OK) || (res == -1)) {
- xmlXPathNodeSetClear(set, hasNsNodes);
- newContextSize = 0;
- goto evaluation_exit;
- }
- if (res != 0) {
- newContextSize++;
- } else {
-
- set->nodeTab[i] = NULL;
- if (contextNode->type == XML_NAMESPACE_DECL)
- xmlXPathNodeSetFreeNs((xmlNsPtr) contextNode);
- }
- if (ctxt->value == contextObj) {
-
- valuePop(ctxt);
- xmlXPathNodeSetClear(contextObj->nodesetval, hasNsNodes);
- } else {
-
- contextObj = NULL;
- }
- }
- if (contextObj != NULL) {
- if (ctxt->value == contextObj)
- valuePop(ctxt);
- xmlXPathReleaseObject(xpctxt, contextObj);
- }
- evaluation_exit:
- if (exprRes != NULL)
- xmlXPathReleaseObject(ctxt->context, exprRes);
-
- xpctxt->node = oldContextNode;
- xpctxt->doc = oldContextDoc;
- xpctxt->contextSize = -1;
- xpctxt->proximityPosition = -1;
- return(newContextSize);
- }
- return(contextSize);
- }
- static int
- xmlXPathCompOpEvalPositionalPredicate(xmlXPathParserContextPtr ctxt,
- xmlXPathStepOpPtr op,
- xmlNodeSetPtr set,
- int contextSize,
- int minPos,
- int maxPos,
- int hasNsNodes)
- {
- if (op->ch1 != -1) {
- xmlXPathCompExprPtr comp = ctxt->comp;
- if (comp->steps[op->ch1].op != XPATH_OP_PREDICATE) {
-
- }
- contextSize = xmlXPathCompOpEvalPredicate(ctxt,
- &comp->steps[op->ch1], set, contextSize, hasNsNodes);
- CHECK_ERROR0;
- if (contextSize <= 0)
- return(0);
- }
-
- if (contextSize < minPos) {
- xmlXPathNodeSetClear(set, hasNsNodes);
- return(0);
- }
- if (op->ch2 == -1) {
-
- return (contextSize);
- } else {
- xmlDocPtr oldContextDoc;
- int i, pos = 0, newContextSize = 0, contextPos = 0, res;
- xmlXPathStepOpPtr exprOp;
- xmlXPathObjectPtr contextObj = NULL, exprRes = NULL;
- xmlNodePtr oldContextNode, contextNode = NULL;
- xmlXPathContextPtr xpctxt = ctxt->context;
- #ifdef LIBXML_XPTR_ENABLED
-
- #endif
-
- oldContextNode = xpctxt->node;
- oldContextDoc = xpctxt->doc;
-
- exprOp = &ctxt->comp->steps[op->ch2];
- for (i = 0; i < set->nodeNr; i++) {
- if (set->nodeTab[i] == NULL)
- continue;
- contextNode = set->nodeTab[i];
- xpctxt->node = contextNode;
- xpctxt->contextSize = contextSize;
- xpctxt->proximityPosition = ++contextPos;
-
- if ((contextNode->type != XML_NAMESPACE_DECL) &&
- (contextNode->doc != NULL))
- xpctxt->doc = contextNode->doc;
-
- if (contextObj == NULL)
- contextObj = xmlXPathCacheNewNodeSet(xpctxt, contextNode);
- else
- xmlXPathNodeSetAddUnique(contextObj->nodesetval,
- contextNode);
- valuePush(ctxt, contextObj);
- res = xmlXPathCompOpEvalToBoolean(ctxt, exprOp, 1);
- if ((ctxt->error != XPATH_EXPRESSION_OK) || (res == -1)) {
- xmlXPathObjectPtr tmp;
-
- tmp = valuePop(ctxt);
- xmlXPathReleaseObject(xpctxt, tmp);
-
- valuePop(ctxt);
- goto evaluation_error;
- }
- if (res)
- pos++;
- if (res && (pos >= minPos) && (pos <= maxPos)) {
-
- newContextSize++;
- if (minPos == maxPos) {
-
- if (contextNode->type == XML_NAMESPACE_DECL) {
-
- set->nodeTab[i] = NULL;
- }
- xmlXPathNodeSetClear(set, hasNsNodes);
- set->nodeNr = 1;
- set->nodeTab[0] = contextNode;
- goto evaluation_exit;
- }
- if (pos == maxPos) {
-
- xmlXPathNodeSetClearFromPos(set, i +1, hasNsNodes);
- goto evaluation_exit;
- }
- } else {
-
- set->nodeTab[i] = NULL;
- if (contextNode->type == XML_NAMESPACE_DECL)
- xmlXPathNodeSetFreeNs((xmlNsPtr) contextNode);
- }
- if (exprRes != NULL) {
- xmlXPathReleaseObject(ctxt->context, exprRes);
- exprRes = NULL;
- }
- if (ctxt->value == contextObj) {
-
- valuePop(ctxt);
- xmlXPathNodeSetClear(contextObj->nodesetval, hasNsNodes);
- } else {
-
- contextObj = NULL;
- }
- }
- goto evaluation_exit;
- evaluation_error:
- xmlXPathNodeSetClear(set, hasNsNodes);
- newContextSize = 0;
- evaluation_exit:
- if (contextObj != NULL) {
- if (ctxt->value == contextObj)
- valuePop(ctxt);
- xmlXPathReleaseObject(xpctxt, contextObj);
- }
- if (exprRes != NULL)
- xmlXPathReleaseObject(ctxt->context, exprRes);
-
- xpctxt->node = oldContextNode;
- xpctxt->doc = oldContextDoc;
- xpctxt->contextSize = -1;
- xpctxt->proximityPosition = -1;
- return(newContextSize);
- }
- return(contextSize);
- }
- static int
- xmlXPathIsPositionalPredicate(xmlXPathParserContextPtr ctxt,
- xmlXPathStepOpPtr op,
- int *maxPos)
- {
- xmlXPathStepOpPtr exprOp;
-
-
- if ((op->op != XPATH_OP_PREDICATE) && (op->op != XPATH_OP_FILTER))
- return(0);
- if (op->ch2 != -1) {
- exprOp = &ctxt->comp->steps[op->ch2];
- } else
- return(0);
- if ((exprOp != NULL) &&
- (exprOp->op == XPATH_OP_VALUE) &&
- (exprOp->value4 != NULL) &&
- (((xmlXPathObjectPtr) exprOp->value4)->type == XPATH_NUMBER))
- {
-
- *maxPos = (int) ((xmlXPathObjectPtr) exprOp->value4)->floatval;
- if (((xmlXPathObjectPtr) exprOp->value4)->floatval ==
- (float) *maxPos)
- {
- return(1);
- }
- }
- return(0);
- }
- static int
- xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt,
- xmlXPathStepOpPtr op,
- xmlNodePtr * first, xmlNodePtr * last,
- int toBool)
- {
- #define XP_TEST_HIT \
- if (hasAxisRange != 0) { \
- if (++pos == maxPos) { \
- addNode(seq, cur); \
- goto axis_range_end; } \
- } else { \
- addNode(seq, cur); \
- if (breakOnFirstHit) goto first_hit; }
- #define XP_TEST_HIT_NS \
- if (hasAxisRange != 0) { \
- if (++pos == maxPos) { \
- hasNsNodes = 1; \
- xmlXPathNodeSetAddNs(seq, xpctxt->node, (xmlNsPtr) cur); \
- goto axis_range_end; } \
- } else { \
- hasNsNodes = 1; \
- xmlXPathNodeSetAddNs(seq, \
- xpctxt->node, (xmlNsPtr) cur); \
- if (breakOnFirstHit) goto first_hit; }
- xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value;
- xmlXPathTestVal test = (xmlXPathTestVal) op->value2;
- xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3;
- const xmlChar *prefix = op->value4;
- const xmlChar *name = op->value5;
- const xmlChar *URI = NULL;
- #ifdef DEBUG_STEP
- int nbMatches = 0, prevMatches = 0;
- #endif
- int total = 0, hasNsNodes = 0;
-
- xmlXPathObjectPtr obj;
-
- xmlNodeSetPtr contextSeq;
- int contextIdx;
- xmlNodePtr contextNode;
-
- xmlNodePtr outerContextNode;
-
- xmlNodeSetPtr outSeq;
-
- xmlNodeSetPtr seq;
- xmlNodePtr cur;
-
- xmlXPathStepOpPtr predOp;
- int maxPos;
- int hasPredicateRange, hasAxisRange, pos, size, newSize;
- int breakOnFirstHit;
- xmlXPathTraversalFunction next = NULL;
-
- xmlXPathTraversalFunctionExt outerNext = NULL;
- void (*addNode) (xmlNodeSetPtr, xmlNodePtr);
- xmlXPathNodeSetMergeFunction mergeAndClear;
- xmlNodePtr oldContextNode;
- xmlXPathContextPtr xpctxt = ctxt->context;
- CHECK_TYPE0(XPATH_NODESET);
- obj = valuePop(ctxt);
-
- if (prefix != NULL) {
- URI = xmlXPathNsLookup(xpctxt, prefix);
- if (URI == NULL) {
- xmlXPathReleaseObject(xpctxt, obj);
- XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR);
- }
- }
-
- mergeAndClear = xmlXPathNodeSetMergeAndClear;
- switch (axis) {
- case AXIS_ANCESTOR:
- first = NULL;
- next = xmlXPathNextAncestor;
- break;
- case AXIS_ANCESTOR_OR_SELF:
- first = NULL;
- next = xmlXPathNextAncestorOrSelf;
- break;
- case AXIS_ATTRIBUTE:
- first = NULL;
- last = NULL;
- next = xmlXPathNextAttribute;
- mergeAndClear = xmlXPathNodeSetMergeAndClearNoDupls;
- break;
- case AXIS_CHILD:
- last = NULL;
- if (op->rewriteType == XP_REWRITE_DOS_CHILD_ELEM) {
-
- outerNext = xmlXPathNextDescendantOrSelfElemParent;
- }
- if (((test == NODE_TEST_NAME) || (test == NODE_TEST_ALL)) &&
- (type == NODE_TYPE_NODE))
- {
-
- next = xmlXPathNextChildElement;
- } else
- next = xmlXPathNextChild;
- mergeAndClear = xmlXPathNodeSetMergeAndClearNoDupls;
- break;
- case AXIS_DESCENDANT:
- last = NULL;
- next = xmlXPathNextDescendant;
- break;
- case AXIS_DESCENDANT_OR_SELF:
- last = NULL;
- next = xmlXPathNextDescendantOrSelf;
- break;
- case AXIS_FOLLOWING:
- last = NULL;
- next = xmlXPathNextFollowing;
- break;
- case AXIS_FOLLOWING_SIBLING:
- last = NULL;
- next = xmlXPathNextFollowingSibling;
- break;
- case AXIS_NAMESPACE:
- first = NULL;
- last = NULL;
- next = (xmlXPathTraversalFunction) xmlXPathNextNamespace;
- mergeAndClear = xmlXPathNodeSetMergeAndClearNoDupls;
- break;
- case AXIS_PARENT:
- first = NULL;
- next = xmlXPathNextParent;
- break;
- case AXIS_PRECEDING:
- first = NULL;
- next = xmlXPathNextPrecedingInternal;
- break;
- case AXIS_PRECEDING_SIBLING:
- first = NULL;
- next = xmlXPathNextPrecedingSibling;
- break;
- case AXIS_SELF:
- first = NULL;
- last = NULL;
- next = xmlXPathNextSelf;
- mergeAndClear = xmlXPathNodeSetMergeAndClearNoDupls;
- break;
- }
- #ifdef DEBUG_STEP
- xmlXPathDebugDumpStepAxis(op,
- (obj->nodesetval != NULL) ? obj->nodesetval->nodeNr : 0);
- #endif
- if (next == NULL) {
- xmlXPathReleaseObject(xpctxt, obj);
- return(0);
- }
- contextSeq = obj->nodesetval;
- if ((contextSeq == NULL) || (contextSeq->nodeNr <= 0)) {
- xmlXPathReleaseObject(xpctxt, obj);
- valuePush(ctxt, xmlXPathCacheWrapNodeSet(xpctxt, NULL));
- return(0);
- }
-
- maxPos = 0;
- predOp = NULL;
- hasPredicateRange = 0;
- hasAxisRange = 0;
- if (op->ch2 != -1) {
-
- predOp = &ctxt->comp->steps[op->ch2];
- if (xmlXPathIsPositionalPredicate(ctxt, predOp, &maxPos)) {
- if (predOp->ch1 != -1) {
-
- predOp = &ctxt->comp->steps[predOp->ch1];
- hasPredicateRange = 1;
- } else {
-
- predOp = NULL;
- hasAxisRange = 1;
- }
- }
- }
- breakOnFirstHit = ((toBool) && (predOp == NULL)) ? 1 : 0;
-
-
- oldContextNode = xpctxt->node;
- addNode = xmlXPathNodeSetAddUnique;
- outSeq = NULL;
- seq = NULL;
- outerContextNode = NULL;
- contextNode = NULL;
- contextIdx = 0;
- while ((contextIdx < contextSeq->nodeNr) || (contextNode != NULL)) {
- if (outerNext != NULL) {
-
- if (contextNode == NULL) {
-
- outerContextNode = contextSeq->nodeTab[contextIdx++];
- contextNode = outerNext(NULL, outerContextNode);
- } else
- contextNode = outerNext(contextNode, outerContextNode);
- if (contextNode == NULL)
- continue;
-
- xpctxt->node = contextNode;
- } else
- xpctxt->node = contextSeq->nodeTab[contextIdx++];
- if (seq == NULL) {
- seq = xmlXPathNodeSetCreate(NULL);
- if (seq == NULL) {
- total = 0;
- goto error;
- }
- }
-
- pos = 0;
- cur = NULL;
- hasNsNodes = 0;
- do {
- cur = next(ctxt, cur);
- if (cur == NULL)
- break;
-
- if ((first != NULL) && (*first != NULL)) {
- if (*first == cur)
- break;
- if (((total % 256) == 0) &&
- #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON
- (xmlXPathCmpNodesExt(*first, cur) >= 0))
- #else
- (xmlXPathCmpNodes(*first, cur) >= 0))
- #endif
- {
- break;
- }
- }
- if ((last != NULL) && (*last != NULL)) {
- if (*last == cur)
- break;
- if (((total % 256) == 0) &&
- #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON
- (xmlXPathCmpNodesExt(cur, *last) >= 0))
- #else
- (xmlXPathCmpNodes(cur, *last) >= 0))
- #endif
- {
- break;
- }
- }
- total++;
- #ifdef DEBUG_STEP
- xmlGenericError(xmlGenericErrorContext, " %s", cur->name);
- #endif
- switch (test) {
- case NODE_TEST_NONE:
- total = 0;
- STRANGE
- goto error;
- case NODE_TEST_TYPE:
-
- if (type == NODE_TYPE_NODE) {
- switch (cur->type) {
- case XML_DOCUMENT_NODE:
- case XML_HTML_DOCUMENT_NODE:
- #ifdef LIBXML_DOCB_ENABLED
- case XML_DOCB_DOCUMENT_NODE:
- #endif
- case XML_ELEMENT_NODE:
- case XML_ATTRIBUTE_NODE:
- case XML_PI_NODE:
- case XML_COMMENT_NODE:
- case XML_CDATA_SECTION_NODE:
- case XML_TEXT_NODE:
- case XML_NAMESPACE_DECL:
- XP_TEST_HIT
- break;
- default:
- break;
- }
- } else if (cur->type == type) {
- if (type == XML_NAMESPACE_DECL)
- XP_TEST_HIT_NS
- else
- XP_TEST_HIT
- } else if ((type == NODE_TYPE_TEXT) &&
- (cur->type == XML_CDATA_SECTION_NODE))
- {
- XP_TEST_HIT
- }
- break;
- case NODE_TEST_PI:
- if ((cur->type == XML_PI_NODE) &&
- ((name == NULL) || xmlStrEqual(name, cur->name)))
- {
- XP_TEST_HIT
- }
- break;
- case NODE_TEST_ALL:
- if (axis == AXIS_ATTRIBUTE) {
- if (cur->type == XML_ATTRIBUTE_NODE)
- {
- XP_TEST_HIT
- }
- } else if (axis == AXIS_NAMESPACE) {
- if (cur->type == XML_NAMESPACE_DECL)
- {
- XP_TEST_HIT_NS
- }
- } else {
- if (cur->type == XML_ELEMENT_NODE) {
- if (prefix == NULL)
- {
- XP_TEST_HIT
- } else if ((cur->ns != NULL) &&
- (xmlStrEqual(URI, cur->ns->href)))
- {
- XP_TEST_HIT
- }
- }
- }
- break;
- case NODE_TEST_NS:{
- TODO;
- break;
- }
- case NODE_TEST_NAME:
- if (axis == AXIS_ATTRIBUTE) {
- if (cur->type != XML_ATTRIBUTE_NODE)
- break;
- } else if (axis == AXIS_NAMESPACE) {
- if (cur->type != XML_NAMESPACE_DECL)
- break;
- } else {
- if (cur->type != XML_ELEMENT_NODE)
- break;
- }
- switch (cur->type) {
- case XML_ELEMENT_NODE:
- if (xmlStrEqual(name, cur->name)) {
- if (prefix == NULL) {
- if (cur->ns == NULL)
- {
- XP_TEST_HIT
- }
- } else {
- if ((cur->ns != NULL) &&
- (xmlStrEqual(URI, cur->ns->href)))
- {
- XP_TEST_HIT
- }
- }
- }
- break;
- case XML_ATTRIBUTE_NODE:{
- xmlAttrPtr attr = (xmlAttrPtr) cur;
- if (xmlStrEqual(name, attr->name)) {
- if (prefix == NULL) {
- if ((attr->ns == NULL) ||
- (attr->ns->prefix == NULL))
- {
- XP_TEST_HIT
- }
- } else {
- if ((attr->ns != NULL) &&
- (xmlStrEqual(URI,
- attr->ns->href)))
- {
- XP_TEST_HIT
- }
- }
- }
- break;
- }
- case XML_NAMESPACE_DECL:
- if (cur->type == XML_NAMESPACE_DECL) {
- xmlNsPtr ns = (xmlNsPtr) cur;
- if ((ns->prefix != NULL) && (name != NULL)
- && (xmlStrEqual(ns->prefix, name)))
- {
- XP_TEST_HIT_NS
- }
- }
- break;
- default:
- break;
- }
- break;
- }
- } while (cur != NULL);
- goto apply_predicates;
- axis_range_end:
-
- if (outSeq == NULL) {
- outSeq = seq;
- seq = NULL;
- } else
- outSeq = mergeAndClear(outSeq, seq, 0);
-
- if (toBool)
- break;
- continue;
- first_hit:
-
- if (outSeq == NULL) {
- outSeq = seq;
- seq = NULL;
- } else
- outSeq = mergeAndClear(outSeq, seq, 0);
- break;
- #ifdef DEBUG_STEP
- if (seq != NULL)
- nbMatches += seq->nodeNr;
- #endif
- apply_predicates:
-
- if ((predOp != NULL) && (seq->nodeNr > 0)) {
-
-
-
-
-
- size = seq->nodeNr;
- if (hasPredicateRange != 0)
- newSize = xmlXPathCompOpEvalPositionalPredicate(ctxt,
- predOp, seq, size, maxPos, maxPos, hasNsNodes);
- else
- newSize = xmlXPathCompOpEvalPredicate(ctxt,
- predOp, seq, size, hasNsNodes);
- if (ctxt->error != XPATH_EXPRESSION_OK) {
- total = 0;
- goto error;
- }
-
- if (newSize == 0) {
-
- xmlXPathNodeSetClear(seq, hasNsNodes);
- } else if (seq->nodeNr > 0) {
-
- if (outSeq == NULL) {
- if (size != newSize) {
-
- outSeq = mergeAndClear(NULL, seq, 1);
- } else {
- outSeq = seq;
- seq = NULL;
- }
- } else
- outSeq = mergeAndClear(outSeq, seq,
- (size != newSize) ? 1: 0);
-
- if (toBool)
- break;
- }
- } else if (seq->nodeNr > 0) {
-
- if (outSeq == NULL) {
- outSeq = seq;
- seq = NULL;
- } else {
- outSeq = mergeAndClear(outSeq, seq, 0);
- }
- }
- }
- error:
- if ((obj->boolval) && (obj->user != NULL)) {
-
- ctxt->value->boolval = 1;
- ctxt->value->user = obj->user;
- obj->user = NULL;
- obj->boolval = 0;
- }
- xmlXPathReleaseObject(xpctxt, obj);
-
- if (outSeq == NULL) {
- if ((seq != NULL) && (seq->nodeNr == 0))
- outSeq = seq;
- else
- outSeq = xmlXPathNodeSetCreate(NULL);
-
- }
- if ((seq != NULL) && (seq != outSeq)) {
- xmlXPathFreeNodeSet(seq);
- }
-
- valuePush(ctxt, xmlXPathCacheWrapNodeSet(xpctxt, outSeq));
-
- xpctxt->node = oldContextNode;
- #ifdef DEBUG_STEP
- xmlGenericError(xmlGenericErrorContext,
- "\nExamined %d nodes, found %d nodes at that step\n",
- total, nbMatches);
- #endif
- return(total);
- }
- static int
- xmlXPathCompOpEvalFilterFirst(xmlXPathParserContextPtr ctxt,
- xmlXPathStepOpPtr op, xmlNodePtr * first);
- static int
- xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt,
- xmlXPathStepOpPtr op, xmlNodePtr * first)
- {
- int total = 0, cur;
- xmlXPathCompExprPtr comp;
- xmlXPathObjectPtr arg1, arg2;
- CHECK_ERROR0;
- comp = ctxt->comp;
- switch (op->op) {
- case XPATH_OP_END:
- return (0);
- case XPATH_OP_UNION:
- total =
- xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1],
- first);
- CHECK_ERROR0;
- if ((ctxt->value != NULL)
- && (ctxt->value->type == XPATH_NODESET)
- && (ctxt->value->nodesetval != NULL)
- && (ctxt->value->nodesetval->nodeNr >= 1)) {
-
-
- if (ctxt->value->nodesetval->nodeNr > 1)
- xmlXPathNodeSetSort(ctxt->value->nodesetval);
- *first = ctxt->value->nodesetval->nodeTab[0];
- }
- cur =
- xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch2],
- first);
- CHECK_ERROR0;
- CHECK_TYPE0(XPATH_NODESET);
- arg2 = valuePop(ctxt);
- CHECK_TYPE0(XPATH_NODESET);
- arg1 = valuePop(ctxt);
- arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval,
- arg2->nodesetval);
- valuePush(ctxt, arg1);
- xmlXPathReleaseObject(ctxt->context, arg2);
-
- if (total > cur)
- xmlXPathCompSwap(op);
- return (total + cur);
- case XPATH_OP_ROOT:
- xmlXPathRoot(ctxt);
- return (0);
- case XPATH_OP_NODE:
- if (op->ch1 != -1)
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- if (op->ch2 != -1)
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
- CHECK_ERROR0;
- valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context,
- ctxt->context->node));
- return (total);
- case XPATH_OP_RESET:
- if (op->ch1 != -1)
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- if (op->ch2 != -1)
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
- CHECK_ERROR0;
- ctxt->context->node = NULL;
- return (total);
- case XPATH_OP_COLLECT:{
- if (op->ch1 == -1)
- return (total);
- total = xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- total += xmlXPathNodeCollectAndTest(ctxt, op, first, NULL, 0);
- return (total);
- }
- case XPATH_OP_VALUE:
- valuePush(ctxt,
- xmlXPathCacheObjectCopy(ctxt->context,
- (xmlXPathObjectPtr) op->value4));
- return (0);
- case XPATH_OP_SORT:
- if (op->ch1 != -1)
- total +=
- xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1],
- first);
- CHECK_ERROR0;
- if ((ctxt->value != NULL)
- && (ctxt->value->type == XPATH_NODESET)
- && (ctxt->value->nodesetval != NULL)
- && (ctxt->value->nodesetval->nodeNr > 1))
- xmlXPathNodeSetSort(ctxt->value->nodesetval);
- return (total);
- #ifdef XP_OPTIMIZED_FILTER_FIRST
- case XPATH_OP_FILTER:
- total =+ xmlXPathCompOpEvalFilterFirst(ctxt, op, first);
- return (total);
- #endif
- default:
- return (xmlXPathCompOpEval(ctxt, op));
- }
- }
- static int
- xmlXPathCompOpEvalLast(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op,
- xmlNodePtr * last)
- {
- int total = 0, cur;
- xmlXPathCompExprPtr comp;
- xmlXPathObjectPtr arg1, arg2;
- xmlNodePtr bak;
- xmlDocPtr bakd;
- int pp;
- int cs;
- CHECK_ERROR0;
- comp = ctxt->comp;
- switch (op->op) {
- case XPATH_OP_END:
- return (0);
- case XPATH_OP_UNION:
- bakd = ctxt->context->doc;
- bak = ctxt->context->node;
- pp = ctxt->context->proximityPosition;
- cs = ctxt->context->contextSize;
- total =
- xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], last);
- CHECK_ERROR0;
- if ((ctxt->value != NULL)
- && (ctxt->value->type == XPATH_NODESET)
- && (ctxt->value->nodesetval != NULL)
- && (ctxt->value->nodesetval->nodeNr >= 1)) {
-
- if (ctxt->value->nodesetval->nodeNr > 1)
- xmlXPathNodeSetSort(ctxt->value->nodesetval);
- *last =
- ctxt->value->nodesetval->nodeTab[ctxt->value->
- nodesetval->nodeNr -
- 1];
- }
- ctxt->context->doc = bakd;
- ctxt->context->node = bak;
- ctxt->context->proximityPosition = pp;
- ctxt->context->contextSize = cs;
- cur =
- xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch2], last);
- CHECK_ERROR0;
- if ((ctxt->value != NULL)
- && (ctxt->value->type == XPATH_NODESET)
- && (ctxt->value->nodesetval != NULL)
- && (ctxt->value->nodesetval->nodeNr >= 1)) {
- }
- CHECK_TYPE0(XPATH_NODESET);
- arg2 = valuePop(ctxt);
- CHECK_TYPE0(XPATH_NODESET);
- arg1 = valuePop(ctxt);
- arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval,
- arg2->nodesetval);
- valuePush(ctxt, arg1);
- xmlXPathReleaseObject(ctxt->context, arg2);
-
- if (total > cur)
- xmlXPathCompSwap(op);
- return (total + cur);
- case XPATH_OP_ROOT:
- xmlXPathRoot(ctxt);
- return (0);
- case XPATH_OP_NODE:
- if (op->ch1 != -1)
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- if (op->ch2 != -1)
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
- CHECK_ERROR0;
- valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context,
- ctxt->context->node));
- return (total);
- case XPATH_OP_RESET:
- if (op->ch1 != -1)
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- if (op->ch2 != -1)
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
- CHECK_ERROR0;
- ctxt->context->node = NULL;
- return (total);
- case XPATH_OP_COLLECT:{
- if (op->ch1 == -1)
- return (0);
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, last, 0);
- return (total);
- }
- case XPATH_OP_VALUE:
- valuePush(ctxt,
- xmlXPathCacheObjectCopy(ctxt->context,
- (xmlXPathObjectPtr) op->value4));
- return (0);
- case XPATH_OP_SORT:
- if (op->ch1 != -1)
- total +=
- xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1],
- last);
- CHECK_ERROR0;
- if ((ctxt->value != NULL)
- && (ctxt->value->type == XPATH_NODESET)
- && (ctxt->value->nodesetval != NULL)
- && (ctxt->value->nodesetval->nodeNr > 1))
- xmlXPathNodeSetSort(ctxt->value->nodesetval);
- return (total);
- default:
- return (xmlXPathCompOpEval(ctxt, op));
- }
- }
- #ifdef XP_OPTIMIZED_FILTER_FIRST
- static int
- xmlXPathCompOpEvalFilterFirst(xmlXPathParserContextPtr ctxt,
- xmlXPathStepOpPtr op, xmlNodePtr * first)
- {
- int total = 0;
- xmlXPathCompExprPtr comp;
- xmlXPathObjectPtr res;
- xmlXPathObjectPtr obj;
- xmlNodeSetPtr oldset;
- xmlNodePtr oldnode;
- xmlDocPtr oldDoc;
- int i;
- CHECK_ERROR0;
- comp = ctxt->comp;
-
- if ((op->ch1 != -1) && (op->ch2 != -1) &&
- (comp->steps[op->ch1].op == XPATH_OP_SORT) &&
- (comp->steps[op->ch2].op == XPATH_OP_SORT)) {
- int f = comp->steps[op->ch2].ch1;
- if ((f != -1) &&
- (comp->steps[f].op == XPATH_OP_FUNCTION) &&
- (comp->steps[f].value5 == NULL) &&
- (comp->steps[f].value == 0) &&
- (comp->steps[f].value4 != NULL) &&
- (xmlStrEqual
- (comp->steps[f].value4, BAD_CAST "last"))) {
- xmlNodePtr last = NULL;
- total +=
- xmlXPathCompOpEvalLast(ctxt,
- &comp->steps[op->ch1],
- &last);
- CHECK_ERROR0;
-
- if ((ctxt->value != NULL) &&
- (ctxt->value->type == XPATH_NODESET) &&
- (ctxt->value->nodesetval != NULL) &&
- (ctxt->value->nodesetval->nodeTab != NULL) &&
- (ctxt->value->nodesetval->nodeNr > 1)) {
- ctxt->value->nodesetval->nodeTab[0] =
- ctxt->value->nodesetval->nodeTab[ctxt->
- value->
- nodesetval->
- nodeNr -
- 1];
- ctxt->value->nodesetval->nodeNr = 1;
- *first = *(ctxt->value->nodesetval->nodeTab);
- }
- return (total);
- }
- }
- if (op->ch1 != -1)
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- if (op->ch2 == -1)
- return (total);
- if (ctxt->value == NULL)
- return (total);
- #ifdef LIBXML_XPTR_ENABLED
- oldnode = ctxt->context->node;
-
- if (ctxt->value->type == XPATH_LOCATIONSET) {
- xmlXPathObjectPtr tmp = NULL;
- xmlLocationSetPtr newlocset = NULL;
- xmlLocationSetPtr oldlocset;
-
- CHECK_TYPE0(XPATH_LOCATIONSET);
- obj = valuePop(ctxt);
- oldlocset = obj->user;
- ctxt->context->node = NULL;
- if ((oldlocset == NULL) || (oldlocset->locNr == 0)) {
- ctxt->context->contextSize = 0;
- ctxt->context->proximityPosition = 0;
- if (op->ch2 != -1)
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
- res = valuePop(ctxt);
- if (res != NULL) {
- xmlXPathReleaseObject(ctxt->context, res);
- }
- valuePush(ctxt, obj);
- CHECK_ERROR0;
- return (total);
- }
- newlocset = xmlXPtrLocationSetCreate(NULL);
- for (i = 0; i < oldlocset->locNr; i++) {
-
- ctxt->context->node = oldlocset->locTab[i]->user;
- ctxt->context->contextSize = oldlocset->locNr;
- ctxt->context->proximityPosition = i + 1;
- if (tmp == NULL) {
- tmp = xmlXPathCacheNewNodeSet(ctxt->context,
- ctxt->context->node);
- } else {
- xmlXPathNodeSetAddUnique(tmp->nodesetval,
- ctxt->context->node);
- }
- valuePush(ctxt, tmp);
- if (op->ch2 != -1)
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
- if (ctxt->error != XPATH_EXPRESSION_OK) {
- xmlXPathFreeObject(obj);
- return(0);
- }
-
- res = valuePop(ctxt);
- if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
- xmlXPtrLocationSetAdd(newlocset,
- xmlXPathCacheObjectCopy(ctxt->context,
- oldlocset->locTab[i]));
- }
-
- if (res != NULL) {
- xmlXPathReleaseObject(ctxt->context, res);
- }
- if (ctxt->value == tmp) {
- valuePop(ctxt);
- xmlXPathNodeSetClear(tmp->nodesetval, 1);
-
-
- } else
- tmp = NULL;
- ctxt->context->node = NULL;
-
- if (newlocset->locNr > 0) {
- *first = (xmlNodePtr) oldlocset->locTab[i]->user;
- break;
- }
- }
- if (tmp != NULL) {
- xmlXPathReleaseObject(ctxt->context, tmp);
- }
-
- xmlXPathReleaseObject(ctxt->context, obj);
- ctxt->context->node = NULL;
- ctxt->context->contextSize = -1;
- ctxt->context->proximityPosition = -1;
- valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset));
- ctxt->context->node = oldnode;
- return (total);
- }
- #endif
-
- CHECK_TYPE0(XPATH_NODESET);
- obj = valuePop(ctxt);
- oldset = obj->nodesetval;
- oldnode = ctxt->context->node;
- oldDoc = ctxt->context->doc;
- ctxt->context->node = NULL;
- if ((oldset == NULL) || (oldset->nodeNr == 0)) {
- ctxt->context->contextSize = 0;
- ctxt->context->proximityPosition = 0;
-
- valuePush(ctxt, obj);
- ctxt->context->node = oldnode;
- CHECK_ERROR0;
- } else {
- xmlNodeSetPtr newset;
- xmlXPathObjectPtr tmp = NULL;
-
- newset = xmlXPathNodeSetCreate(NULL);
-
- for (i = 0; i < oldset->nodeNr; i++) {
-
- ctxt->context->node = oldset->nodeTab[i];
- if ((oldset->nodeTab[i]->type != XML_NAMESPACE_DECL) &&
- (oldset->nodeTab[i]->doc != NULL))
- ctxt->context->doc = oldset->nodeTab[i]->doc;
- if (tmp == NULL) {
- tmp = xmlXPathCacheNewNodeSet(ctxt->context,
- ctxt->context->node);
- } else {
- xmlXPathNodeSetAddUnique(tmp->nodesetval,
- ctxt->context->node);
- }
- valuePush(ctxt, tmp);
- ctxt->context->contextSize = oldset->nodeNr;
- ctxt->context->proximityPosition = i + 1;
- if (op->ch2 != -1)
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
- if (ctxt->error != XPATH_EXPRESSION_OK) {
- xmlXPathFreeNodeSet(newset);
- xmlXPathFreeObject(obj);
- return(0);
- }
-
- res = valuePop(ctxt);
- if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
- xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]);
- }
-
- if (res != NULL) {
- xmlXPathReleaseObject(ctxt->context, res);
- }
- if (ctxt->value == tmp) {
- valuePop(ctxt);
-
- xmlXPathNodeSetClear(tmp->nodesetval, 1);
- } else
- tmp = NULL;
- ctxt->context->node = NULL;
-
- if (newset->nodeNr > 0) {
- *first = *(newset->nodeTab);
- break;
- }
- }
- if (tmp != NULL) {
- xmlXPathReleaseObject(ctxt->context, tmp);
- }
-
- xmlXPathReleaseObject(ctxt->context, obj);
- ctxt->context->node = NULL;
- ctxt->context->contextSize = -1;
- ctxt->context->proximityPosition = -1;
-
- ctxt->context->doc = oldDoc;
- valuePush(ctxt, xmlXPathCacheWrapNodeSet(ctxt->context, newset));
- }
- ctxt->context->node = oldnode;
- return(total);
- }
- #endif
- static int
- xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op)
- {
- int total = 0;
- int equal, ret;
- xmlXPathCompExprPtr comp;
- xmlXPathObjectPtr arg1, arg2;
- xmlNodePtr bak;
- xmlDocPtr bakd;
- int pp;
- int cs;
- CHECK_ERROR0;
- comp = ctxt->comp;
- switch (op->op) {
- case XPATH_OP_END:
- return (0);
- case XPATH_OP_AND:
- bakd = ctxt->context->doc;
- bak = ctxt->context->node;
- pp = ctxt->context->proximityPosition;
- cs = ctxt->context->contextSize;
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- xmlXPathBooleanFunction(ctxt, 1);
- if ((ctxt->value == NULL) || (ctxt->value->boolval == 0))
- return (total);
- arg2 = valuePop(ctxt);
- ctxt->context->doc = bakd;
- ctxt->context->node = bak;
- ctxt->context->proximityPosition = pp;
- ctxt->context->contextSize = cs;
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
- if (ctxt->error) {
- xmlXPathFreeObject(arg2);
- return(0);
- }
- xmlXPathBooleanFunction(ctxt, 1);
- arg1 = valuePop(ctxt);
- arg1->boolval &= arg2->boolval;
- valuePush(ctxt, arg1);
- xmlXPathReleaseObject(ctxt->context, arg2);
- return (total);
- case XPATH_OP_OR:
- bakd = ctxt->context->doc;
- bak = ctxt->context->node;
- pp = ctxt->context->proximityPosition;
- cs = ctxt->context->contextSize;
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- xmlXPathBooleanFunction(ctxt, 1);
- if ((ctxt->value == NULL) || (ctxt->value->boolval == 1))
- return (total);
- arg2 = valuePop(ctxt);
- ctxt->context->doc = bakd;
- ctxt->context->node = bak;
- ctxt->context->proximityPosition = pp;
- ctxt->context->contextSize = cs;
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
- if (ctxt->error) {
- xmlXPathFreeObject(arg2);
- return(0);
- }
- xmlXPathBooleanFunction(ctxt, 1);
- arg1 = valuePop(ctxt);
- arg1->boolval |= arg2->boolval;
- valuePush(ctxt, arg1);
- xmlXPathReleaseObject(ctxt->context, arg2);
- return (total);
- case XPATH_OP_EQUAL:
- bakd = ctxt->context->doc;
- bak = ctxt->context->node;
- pp = ctxt->context->proximityPosition;
- cs = ctxt->context->contextSize;
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- ctxt->context->doc = bakd;
- ctxt->context->node = bak;
- ctxt->context->proximityPosition = pp;
- ctxt->context->contextSize = cs;
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
- CHECK_ERROR0;
- if (op->value)
- equal = xmlXPathEqualValues(ctxt);
- else
- equal = xmlXPathNotEqualValues(ctxt);
- valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, equal));
- return (total);
- case XPATH_OP_CMP:
- bakd = ctxt->context->doc;
- bak = ctxt->context->node;
- pp = ctxt->context->proximityPosition;
- cs = ctxt->context->contextSize;
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- ctxt->context->doc = bakd;
- ctxt->context->node = bak;
- ctxt->context->proximityPosition = pp;
- ctxt->context->contextSize = cs;
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
- CHECK_ERROR0;
- ret = xmlXPathCompareValues(ctxt, op->value, op->value2);
- valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, ret));
- return (total);
- case XPATH_OP_PLUS:
- bakd = ctxt->context->doc;
- bak = ctxt->context->node;
- pp = ctxt->context->proximityPosition;
- cs = ctxt->context->contextSize;
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- if (op->ch2 != -1) {
- ctxt->context->doc = bakd;
- ctxt->context->node = bak;
- ctxt->context->proximityPosition = pp;
- ctxt->context->contextSize = cs;
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
- }
- CHECK_ERROR0;
- if (op->value == 0)
- xmlXPathSubValues(ctxt);
- else if (op->value == 1)
- xmlXPathAddValues(ctxt);
- else if (op->value == 2)
- xmlXPathValueFlipSign(ctxt);
- else if (op->value == 3) {
- CAST_TO_NUMBER;
- CHECK_TYPE0(XPATH_NUMBER);
- }
- return (total);
- case XPATH_OP_MULT:
- bakd = ctxt->context->doc;
- bak = ctxt->context->node;
- pp = ctxt->context->proximityPosition;
- cs = ctxt->context->contextSize;
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- ctxt->context->doc = bakd;
- ctxt->context->node = bak;
- ctxt->context->proximityPosition = pp;
- ctxt->context->contextSize = cs;
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
- CHECK_ERROR0;
- if (op->value == 0)
- xmlXPathMultValues(ctxt);
- else if (op->value == 1)
- xmlXPathDivValues(ctxt);
- else if (op->value == 2)
- xmlXPathModValues(ctxt);
- return (total);
- case XPATH_OP_UNION:
- bakd = ctxt->context->doc;
- bak = ctxt->context->node;
- pp = ctxt->context->proximityPosition;
- cs = ctxt->context->contextSize;
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- ctxt->context->doc = bakd;
- ctxt->context->node = bak;
- ctxt->context->proximityPosition = pp;
- ctxt->context->contextSize = cs;
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
- CHECK_ERROR0;
- CHECK_TYPE0(XPATH_NODESET);
- arg2 = valuePop(ctxt);
- CHECK_TYPE0(XPATH_NODESET);
- arg1 = valuePop(ctxt);
- if ((arg1->nodesetval == NULL) ||
- ((arg2->nodesetval != NULL) &&
- (arg2->nodesetval->nodeNr != 0)))
- {
- arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval,
- arg2->nodesetval);
- }
- valuePush(ctxt, arg1);
- xmlXPathReleaseObject(ctxt->context, arg2);
- return (total);
- case XPATH_OP_ROOT:
- xmlXPathRoot(ctxt);
- return (total);
- case XPATH_OP_NODE:
- if (op->ch1 != -1)
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- if (op->ch2 != -1)
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
- CHECK_ERROR0;
- valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context,
- ctxt->context->node));
- return (total);
- case XPATH_OP_RESET:
- if (op->ch1 != -1)
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- if (op->ch2 != -1)
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
- CHECK_ERROR0;
- ctxt->context->node = NULL;
- return (total);
- case XPATH_OP_COLLECT:{
- if (op->ch1 == -1)
- return (total);
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, NULL, 0);
- return (total);
- }
- case XPATH_OP_VALUE:
- valuePush(ctxt,
- xmlXPathCacheObjectCopy(ctxt->context,
- (xmlXPathObjectPtr) op->value4));
- return (total);
- case XPATH_OP_VARIABLE:{
- xmlXPathObjectPtr val;
- if (op->ch1 != -1)
- total +=
- xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- if (op->value5 == NULL) {
- val = xmlXPathVariableLookup(ctxt->context, op->value4);
- if (val == NULL) {
- ctxt->error = XPATH_UNDEF_VARIABLE_ERROR;
- return(0);
- }
- valuePush(ctxt, val);
- } else {
- const xmlChar *URI;
- URI = xmlXPathNsLookup(ctxt->context, op->value5);
- if (URI == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlXPathCompOpEval: variable %s bound to undefined prefix %s\n",
- (char *) op->value4, (char *)op->value5);
- return (total);
- }
- val = xmlXPathVariableLookupNS(ctxt->context,
- op->value4, URI);
- if (val == NULL) {
- ctxt->error = XPATH_UNDEF_VARIABLE_ERROR;
- return(0);
- }
- valuePush(ctxt, val);
- }
- return (total);
- }
- case XPATH_OP_FUNCTION:{
- xmlXPathFunction func;
- const xmlChar *oldFunc, *oldFuncURI;
- int i;
- if (op->ch1 != -1)
- total +=
- xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- if (ctxt->valueNr < op->value) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlXPathCompOpEval: parameter error\n");
- ctxt->error = XPATH_INVALID_OPERAND;
- return (total);
- }
- for (i = 0; i < op->value; i++)
- if (ctxt->valueTab[(ctxt->valueNr - 1) - i] == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlXPathCompOpEval: parameter error\n");
- ctxt->error = XPATH_INVALID_OPERAND;
- return (total);
- }
- if (op->cache != NULL)
- XML_CAST_FPTR(func) = op->cache;
- else {
- const xmlChar *URI = NULL;
- if (op->value5 == NULL)
- func =
- xmlXPathFunctionLookup(ctxt->context,
- op->value4);
- else {
- URI = xmlXPathNsLookup(ctxt->context, op->value5);
- if (URI == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlXPathCompOpEval: function %s bound to undefined prefix %s\n",
- (char *)op->value4, (char *)op->value5);
- return (total);
- }
- func = xmlXPathFunctionLookupNS(ctxt->context,
- op->value4, URI);
- }
- if (func == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlXPathCompOpEval: function %s not found\n",
- (char *)op->value4);
- XP_ERROR0(XPATH_UNKNOWN_FUNC_ERROR);
- }
- op->cache = XML_CAST_FPTR(func);
- op->cacheURI = (void *) URI;
- }
- oldFunc = ctxt->context->function;
- oldFuncURI = ctxt->context->functionURI;
- ctxt->context->function = op->value4;
- ctxt->context->functionURI = op->cacheURI;
- func(ctxt, op->value);
- ctxt->context->function = oldFunc;
- ctxt->context->functionURI = oldFuncURI;
- return (total);
- }
- case XPATH_OP_ARG:
- bakd = ctxt->context->doc;
- bak = ctxt->context->node;
- pp = ctxt->context->proximityPosition;
- cs = ctxt->context->contextSize;
- if (op->ch1 != -1)
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- ctxt->context->contextSize = cs;
- ctxt->context->proximityPosition = pp;
- ctxt->context->node = bak;
- ctxt->context->doc = bakd;
- CHECK_ERROR0;
- if (op->ch2 != -1) {
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
- ctxt->context->doc = bakd;
- ctxt->context->node = bak;
- CHECK_ERROR0;
- }
- return (total);
- case XPATH_OP_PREDICATE:
- case XPATH_OP_FILTER:{
- xmlXPathObjectPtr res;
- xmlXPathObjectPtr obj, tmp;
- xmlNodeSetPtr newset = NULL;
- xmlNodeSetPtr oldset;
- xmlNodePtr oldnode;
- xmlDocPtr oldDoc;
- int i;
-
- if ((op->ch1 != -1) && (op->ch2 != -1) &&
- #ifdef XP_OPTIMIZED_FILTER_FIRST
-
- ((comp->steps[op->ch1].op == XPATH_OP_SORT) ||
- (comp->steps[op->ch1].op == XPATH_OP_FILTER)) &&
- #else
- (comp->steps[op->ch1].op == XPATH_OP_SORT) &&
- #endif
- (comp->steps[op->ch2].op == XPATH_OP_VALUE)) {
- xmlXPathObjectPtr val;
- val = comp->steps[op->ch2].value4;
- if ((val != NULL) && (val->type == XPATH_NUMBER) &&
- (val->floatval == 1.0)) {
- xmlNodePtr first = NULL;
- total +=
- xmlXPathCompOpEvalFirst(ctxt,
- &comp->steps[op->ch1],
- &first);
- CHECK_ERROR0;
-
- if ((ctxt->value != NULL) &&
- (ctxt->value->type == XPATH_NODESET) &&
- (ctxt->value->nodesetval != NULL) &&
- (ctxt->value->nodesetval->nodeNr > 1))
- ctxt->value->nodesetval->nodeNr = 1;
- return (total);
- }
- }
-
- if ((op->ch1 != -1) && (op->ch2 != -1) &&
- (comp->steps[op->ch1].op == XPATH_OP_SORT) &&
- (comp->steps[op->ch2].op == XPATH_OP_SORT)) {
- int f = comp->steps[op->ch2].ch1;
- if ((f != -1) &&
- (comp->steps[f].op == XPATH_OP_FUNCTION) &&
- (comp->steps[f].value5 == NULL) &&
- (comp->steps[f].value == 0) &&
- (comp->steps[f].value4 != NULL) &&
- (xmlStrEqual
- (comp->steps[f].value4, BAD_CAST "last"))) {
- xmlNodePtr last = NULL;
- total +=
- xmlXPathCompOpEvalLast(ctxt,
- &comp->steps[op->ch1],
- &last);
- CHECK_ERROR0;
-
- if ((ctxt->value != NULL) &&
- (ctxt->value->type == XPATH_NODESET) &&
- (ctxt->value->nodesetval != NULL) &&
- (ctxt->value->nodesetval->nodeTab != NULL) &&
- (ctxt->value->nodesetval->nodeNr > 1)) {
- ctxt->value->nodesetval->nodeTab[0] =
- ctxt->value->nodesetval->nodeTab[ctxt->
- value->
- nodesetval->
- nodeNr -
- 1];
- ctxt->value->nodesetval->nodeNr = 1;
- }
- return (total);
- }
- }
-
- if (op->ch1 != -1)
- total +=
- xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- if (op->ch2 == -1)
- return (total);
- if (ctxt->value == NULL)
- return (total);
- oldnode = ctxt->context->node;
- #ifdef LIBXML_XPTR_ENABLED
-
- if (ctxt->value->type == XPATH_LOCATIONSET) {
- xmlLocationSetPtr newlocset = NULL;
- xmlLocationSetPtr oldlocset;
-
- CHECK_TYPE0(XPATH_LOCATIONSET);
- obj = valuePop(ctxt);
- oldlocset = obj->user;
- ctxt->context->node = NULL;
- if ((oldlocset == NULL) || (oldlocset->locNr == 0)) {
- ctxt->context->contextSize = 0;
- ctxt->context->proximityPosition = 0;
- if (op->ch2 != -1)
- total +=
- xmlXPathCompOpEval(ctxt,
- &comp->steps[op->ch2]);
- res = valuePop(ctxt);
- if (res != NULL) {
- xmlXPathReleaseObject(ctxt->context, res);
- }
- valuePush(ctxt, obj);
- CHECK_ERROR0;
- return (total);
- }
- newlocset = xmlXPtrLocationSetCreate(NULL);
- for (i = 0; i < oldlocset->locNr; i++) {
-
- ctxt->context->node = oldlocset->locTab[i]->user;
- ctxt->context->contextSize = oldlocset->locNr;
- ctxt->context->proximityPosition = i + 1;
- tmp = xmlXPathCacheNewNodeSet(ctxt->context,
- ctxt->context->node);
- valuePush(ctxt, tmp);
- if (op->ch2 != -1)
- total +=
- xmlXPathCompOpEval(ctxt,
- &comp->steps[op->ch2]);
- if (ctxt->error != XPATH_EXPRESSION_OK) {
- xmlXPathFreeObject(obj);
- return(0);
- }
-
- res = valuePop(ctxt);
- if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
- xmlXPtrLocationSetAdd(newlocset,
- xmlXPathObjectCopy
- (oldlocset->locTab[i]));
- }
-
- if (res != NULL) {
- xmlXPathReleaseObject(ctxt->context, res);
- }
- if (ctxt->value == tmp) {
- res = valuePop(ctxt);
- xmlXPathReleaseObject(ctxt->context, res);
- }
- ctxt->context->node = NULL;
- }
-
- xmlXPathReleaseObject(ctxt->context, obj);
- ctxt->context->node = NULL;
- ctxt->context->contextSize = -1;
- ctxt->context->proximityPosition = -1;
- valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset));
- ctxt->context->node = oldnode;
- return (total);
- }
- #endif
-
- CHECK_TYPE0(XPATH_NODESET);
- obj = valuePop(ctxt);
- oldset = obj->nodesetval;
- oldnode = ctxt->context->node;
- oldDoc = ctxt->context->doc;
- ctxt->context->node = NULL;
- if ((oldset == NULL) || (oldset->nodeNr == 0)) {
- ctxt->context->contextSize = 0;
- ctxt->context->proximityPosition = 0;
- valuePush(ctxt, obj);
- ctxt->context->node = oldnode;
- CHECK_ERROR0;
- } else {
- tmp = NULL;
-
- newset = xmlXPathNodeSetCreate(NULL);
-
- for (i = 0; i < oldset->nodeNr; i++) {
-
- ctxt->context->node = oldset->nodeTab[i];
- if ((oldset->nodeTab[i]->type != XML_NAMESPACE_DECL) &&
- (oldset->nodeTab[i]->doc != NULL))
- ctxt->context->doc = oldset->nodeTab[i]->doc;
- if (tmp == NULL) {
- tmp = xmlXPathCacheNewNodeSet(ctxt->context,
- ctxt->context->node);
- } else {
- xmlXPathNodeSetAddUnique(tmp->nodesetval,
- ctxt->context->node);
- }
- valuePush(ctxt, tmp);
- ctxt->context->contextSize = oldset->nodeNr;
- ctxt->context->proximityPosition = i + 1;
-
- if (op->ch2 != -1)
- total +=
- xmlXPathCompOpEval(ctxt,
- &comp->steps[op->ch2]);
- if (ctxt->error != XPATH_EXPRESSION_OK) {
- xmlXPathFreeNodeSet(newset);
- xmlXPathFreeObject(obj);
- return(0);
- }
-
-
- res = valuePop(ctxt);
- if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
- xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]);
- }
-
- if (res != NULL) {
- xmlXPathReleaseObject(ctxt->context, res);
- }
- if (ctxt->value == tmp) {
- valuePop(ctxt);
- xmlXPathNodeSetClear(tmp->nodesetval, 1);
-
- } else
- tmp = NULL;
- ctxt->context->node = NULL;
- }
- if (tmp != NULL)
- xmlXPathReleaseObject(ctxt->context, tmp);
-
- xmlXPathReleaseObject(ctxt->context, obj);
- ctxt->context->node = NULL;
- ctxt->context->contextSize = -1;
- ctxt->context->proximityPosition = -1;
-
- ctxt->context->doc = oldDoc;
- valuePush(ctxt,
- xmlXPathCacheWrapNodeSet(ctxt->context, newset));
- }
- ctxt->context->node = oldnode;
- return (total);
- }
- case XPATH_OP_SORT:
- if (op->ch1 != -1)
- total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- CHECK_ERROR0;
- if ((ctxt->value != NULL) &&
- (ctxt->value->type == XPATH_NODESET) &&
- (ctxt->value->nodesetval != NULL) &&
- (ctxt->value->nodesetval->nodeNr > 1))
- {
- xmlXPathNodeSetSort(ctxt->value->nodesetval);
- }
- return (total);
- #ifdef LIBXML_XPTR_ENABLED
- case XPATH_OP_RANGETO:{
- xmlXPathObjectPtr range;
- xmlXPathObjectPtr res, obj;
- xmlXPathObjectPtr tmp;
- xmlLocationSetPtr newlocset = NULL;
- xmlLocationSetPtr oldlocset;
- xmlNodeSetPtr oldset;
- int i, j;
- if (op->ch1 != -1)
- total +=
- xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- if (op->ch2 == -1)
- return (total);
- if (ctxt->value->type == XPATH_LOCATIONSET) {
-
- CHECK_TYPE0(XPATH_LOCATIONSET);
- obj = valuePop(ctxt);
- oldlocset = obj->user;
- if ((oldlocset == NULL) || (oldlocset->locNr == 0)) {
- ctxt->context->node = NULL;
- ctxt->context->contextSize = 0;
- ctxt->context->proximityPosition = 0;
- total += xmlXPathCompOpEval(ctxt,&comp->steps[op->ch2]);
- res = valuePop(ctxt);
- if (res != NULL) {
- xmlXPathReleaseObject(ctxt->context, res);
- }
- valuePush(ctxt, obj);
- CHECK_ERROR0;
- return (total);
- }
- newlocset = xmlXPtrLocationSetCreate(NULL);
- for (i = 0; i < oldlocset->locNr; i++) {
-
- ctxt->context->node = oldlocset->locTab[i]->user;
- ctxt->context->contextSize = oldlocset->locNr;
- ctxt->context->proximityPosition = i + 1;
- tmp = xmlXPathCacheNewNodeSet(ctxt->context,
- ctxt->context->node);
- valuePush(ctxt, tmp);
- if (op->ch2 != -1)
- total +=
- xmlXPathCompOpEval(ctxt,
- &comp->steps[op->ch2]);
- if (ctxt->error != XPATH_EXPRESSION_OK) {
- xmlXPathFreeObject(obj);
- return(0);
- }
- res = valuePop(ctxt);
- if (res->type == XPATH_LOCATIONSET) {
- xmlLocationSetPtr rloc =
- (xmlLocationSetPtr)res->user;
- for (j=0; j<rloc->locNr; j++) {
- range = xmlXPtrNewRange(
- oldlocset->locTab[i]->user,
- oldlocset->locTab[i]->index,
- rloc->locTab[j]->user2,
- rloc->locTab[j]->index2);
- if (range != NULL) {
- xmlXPtrLocationSetAdd(newlocset, range);
- }
- }
- } else {
- range = xmlXPtrNewRangeNodeObject(
- (xmlNodePtr)oldlocset->locTab[i]->user, res);
- if (range != NULL) {
- xmlXPtrLocationSetAdd(newlocset,range);
- }
- }
-
- if (res != NULL) {
- xmlXPathReleaseObject(ctxt->context, res);
- }
- if (ctxt->value == tmp) {
- res = valuePop(ctxt);
- xmlXPathReleaseObject(ctxt->context, res);
- }
- ctxt->context->node = NULL;
- }
- } else {
- CHECK_TYPE0(XPATH_NODESET);
- obj = valuePop(ctxt);
- oldset = obj->nodesetval;
- ctxt->context->node = NULL;
- newlocset = xmlXPtrLocationSetCreate(NULL);
- if (oldset != NULL) {
- for (i = 0; i < oldset->nodeNr; i++) {
-
- ctxt->context->node = oldset->nodeTab[i];
-
- tmp = xmlXPathCacheNewNodeSet(ctxt->context,
- ctxt->context->node);
- valuePush(ctxt, tmp);
- if (op->ch2 != -1)
- total +=
- xmlXPathCompOpEval(ctxt,
- &comp->steps[op->ch2]);
- if (ctxt->error != XPATH_EXPRESSION_OK) {
- xmlXPathFreeObject(obj);
- return(0);
- }
- res = valuePop(ctxt);
- range =
- xmlXPtrNewRangeNodeObject(oldset->nodeTab[i],
- res);
- if (range != NULL) {
- xmlXPtrLocationSetAdd(newlocset, range);
- }
-
- if (res != NULL) {
- xmlXPathReleaseObject(ctxt->context, res);
- }
- if (ctxt->value == tmp) {
- res = valuePop(ctxt);
- xmlXPathReleaseObject(ctxt->context, res);
- }
- ctxt->context->node = NULL;
- }
- }
- }
-
- xmlXPathReleaseObject(ctxt->context, obj);
- ctxt->context->node = NULL;
- ctxt->context->contextSize = -1;
- ctxt->context->proximityPosition = -1;
- valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset));
- return (total);
- }
- #endif
- }
- xmlGenericError(xmlGenericErrorContext,
- "XPath: unknown precompiled operation %d\n", op->op);
- return (total);
- }
- static int
- xmlXPathCompOpEvalToBoolean(xmlXPathParserContextPtr ctxt,
- xmlXPathStepOpPtr op,
- int isPredicate)
- {
- xmlXPathObjectPtr resObj = NULL;
- start:
-
- switch (op->op) {
- case XPATH_OP_END:
- return (0);
- case XPATH_OP_VALUE:
- resObj = (xmlXPathObjectPtr) op->value4;
- if (isPredicate)
- return(xmlXPathEvaluatePredicateResult(ctxt, resObj));
- return(xmlXPathCastToBoolean(resObj));
- case XPATH_OP_SORT:
-
- if (op->ch1 != -1) {
- op = &ctxt->comp->steps[op->ch1];
- goto start;
- }
- return(0);
- case XPATH_OP_COLLECT:
- if (op->ch1 == -1)
- return(0);
- xmlXPathCompOpEval(ctxt, &ctxt->comp->steps[op->ch1]);
- if (ctxt->error != XPATH_EXPRESSION_OK)
- return(-1);
- xmlXPathNodeCollectAndTest(ctxt, op, NULL, NULL, 1);
- if (ctxt->error != XPATH_EXPRESSION_OK)
- return(-1);
- resObj = valuePop(ctxt);
- if (resObj == NULL)
- return(-1);
- break;
- default:
-
- xmlXPathCompOpEval(ctxt, op);
- if (ctxt->error != XPATH_EXPRESSION_OK)
- return(-1);
- resObj = valuePop(ctxt);
- if (resObj == NULL)
- return(-1);
- break;
- }
- if (resObj) {
- int res;
- if (resObj->type == XPATH_BOOLEAN) {
- res = resObj->boolval;
- } else if (isPredicate) {
-
- res = xmlXPathEvaluatePredicateResult(ctxt, resObj);
- } else {
- res = xmlXPathCastToBoolean(resObj);
- }
- xmlXPathReleaseObject(ctxt->context, resObj);
- return(res);
- }
- return(0);
- }
- #ifdef XPATH_STREAMING
- static int
- xmlXPathRunStreamEval(xmlXPathContextPtr ctxt, xmlPatternPtr comp,
- xmlXPathObjectPtr *resultSeq, int toBool)
- {
- int max_depth, min_depth;
- int from_root;
- int ret, depth;
- int eval_all_nodes;
- xmlNodePtr cur = NULL, limit = NULL;
- xmlStreamCtxtPtr patstream = NULL;
- int nb_nodes = 0;
- if ((ctxt == NULL) || (comp == NULL))
- return(-1);
- max_depth = xmlPatternMaxDepth(comp);
- if (max_depth == -1)
- return(-1);
- if (max_depth == -2)
- max_depth = 10000;
- min_depth = xmlPatternMinDepth(comp);
- if (min_depth == -1)
- return(-1);
- from_root = xmlPatternFromRoot(comp);
- if (from_root < 0)
- return(-1);
- #if 0
- printf("stream eval: depth %d from root %d\n", max_depth, from_root);
- #endif
- if (! toBool) {
- if (resultSeq == NULL)
- return(-1);
- *resultSeq = xmlXPathCacheNewNodeSet(ctxt, NULL);
- if (*resultSeq == NULL)
- return(-1);
- }
-
- if (min_depth == 0) {
- if (from_root) {
-
- if (toBool)
- return(1);
- xmlXPathNodeSetAddUnique((*resultSeq)->nodesetval,
- (xmlNodePtr) ctxt->doc);
- } else {
-
- if (toBool)
- return(1);
- xmlXPathNodeSetAddUnique((*resultSeq)->nodesetval, ctxt->node);
- }
- }
- if (max_depth == 0) {
- return(0);
- }
- if (from_root) {
- cur = (xmlNodePtr)ctxt->doc;
- } else if (ctxt->node != NULL) {
- switch (ctxt->node->type) {
- case XML_ELEMENT_NODE:
- case XML_DOCUMENT_NODE:
- case XML_DOCUMENT_FRAG_NODE:
- case XML_HTML_DOCUMENT_NODE:
- #ifdef LIBXML_DOCB_ENABLED
- case XML_DOCB_DOCUMENT_NODE:
- #endif
- cur = ctxt->node;
- break;
- case XML_ATTRIBUTE_NODE:
- case XML_TEXT_NODE:
- case XML_CDATA_SECTION_NODE:
- case XML_ENTITY_REF_NODE:
- case XML_ENTITY_NODE:
- case XML_PI_NODE:
- case XML_COMMENT_NODE:
- case XML_NOTATION_NODE:
- case XML_DTD_NODE:
- case XML_DOCUMENT_TYPE_NODE:
- case XML_ELEMENT_DECL:
- case XML_ATTRIBUTE_DECL:
- case XML_ENTITY_DECL:
- case XML_NAMESPACE_DECL:
- case XML_XINCLUDE_START:
- case XML_XINCLUDE_END:
- break;
- }
- limit = cur;
- }
- if (cur == NULL) {
- return(0);
- }
- patstream = xmlPatternGetStreamCtxt(comp);
- if (patstream == NULL) {
-
- return(0);
- }
- eval_all_nodes = xmlStreamWantsAnyNode(patstream);
- if (from_root) {
- ret = xmlStreamPush(patstream, NULL, NULL);
- if (ret < 0) {
- } else if (ret == 1) {
- if (toBool)
- goto return_1;
- xmlXPathNodeSetAddUnique((*resultSeq)->nodesetval, cur);
- }
- }
- depth = 0;
- goto scan_children;
- next_node:
- do {
- nb_nodes++;
- switch (cur->type) {
- case XML_ELEMENT_NODE:
- case XML_TEXT_NODE:
- case XML_CDATA_SECTION_NODE:
- case XML_COMMENT_NODE:
- case XML_PI_NODE:
- if (cur->type == XML_ELEMENT_NODE) {
- ret = xmlStreamPush(patstream, cur->name,
- (cur->ns ? cur->ns->href : NULL));
- } else if (eval_all_nodes)
- ret = xmlStreamPushNode(patstream, NULL, NULL, cur->type);
- else
- break;
- if (ret < 0) {
-
- } else if (ret == 1) {
- if (toBool)
- goto return_1;
- xmlXPathNodeSetAddUnique((*resultSeq)->nodesetval, cur);
- }
- if ((cur->children == NULL) || (depth >= max_depth)) {
- ret = xmlStreamPop(patstream);
- while (cur->next != NULL) {
- cur = cur->next;
- if ((cur->type != XML_ENTITY_DECL) &&
- (cur->type != XML_DTD_NODE))
- goto next_node;
- }
- }
- default:
- break;
- }
- scan_children:
- if ((cur->children != NULL) && (depth < max_depth)) {
-
- if (cur->children->type != XML_ENTITY_DECL) {
- cur = cur->children;
- depth++;
-
- if (cur->type != XML_DTD_NODE)
- continue;
- }
- }
- if (cur == limit)
- break;
- while (cur->next != NULL) {
- cur = cur->next;
- if ((cur->type != XML_ENTITY_DECL) &&
- (cur->type != XML_DTD_NODE))
- goto next_node;
- }
- do {
- cur = cur->parent;
- depth--;
- if ((cur == NULL) || (cur == limit))
- goto done;
- if (cur->type == XML_ELEMENT_NODE) {
- ret = xmlStreamPop(patstream);
- } else if ((eval_all_nodes) &&
- ((cur->type == XML_TEXT_NODE) ||
- (cur->type == XML_CDATA_SECTION_NODE) ||
- (cur->type == XML_COMMENT_NODE) ||
- (cur->type == XML_PI_NODE)))
- {
- ret = xmlStreamPop(patstream);
- }
- if (cur->next != NULL) {
- cur = cur->next;
- break;
- }
- } while (cur != NULL);
- } while ((cur != NULL) && (depth >= 0));
- done:
- #if 0
- printf("stream eval: checked %d nodes selected %d\n",
- nb_nodes, retObj->nodesetval->nodeNr);
- #endif
- if (patstream)
- xmlFreeStreamCtxt(patstream);
- return(0);
- return_1:
- if (patstream)
- xmlFreeStreamCtxt(patstream);
- return(1);
- }
- #endif
- static int
- xmlXPathRunEval(xmlXPathParserContextPtr ctxt, int toBool)
- {
- xmlXPathCompExprPtr comp;
- if ((ctxt == NULL) || (ctxt->comp == NULL))
- return(-1);
- if (ctxt->valueTab == NULL) {
-
- ctxt->valueTab = (xmlXPathObjectPtr *)
- xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
- if (ctxt->valueTab == NULL) {
- xmlXPathPErrMemory(ctxt, "creating evaluation context\n");
- xmlFree(ctxt);
- }
- ctxt->valueNr = 0;
- ctxt->valueMax = 10;
- ctxt->value = NULL;
- }
- #ifdef XPATH_STREAMING
- if (ctxt->comp->stream) {
- int res;
- if (toBool) {
-
- res = xmlXPathRunStreamEval(ctxt->context,
- ctxt->comp->stream, NULL, 1);
- if (res != -1)
- return(res);
- } else {
- xmlXPathObjectPtr resObj = NULL;
-
- res = xmlXPathRunStreamEval(ctxt->context,
- ctxt->comp->stream, &resObj, 0);
- if ((res != -1) && (resObj != NULL)) {
- valuePush(ctxt, resObj);
- return(0);
- }
- if (resObj != NULL)
- xmlXPathReleaseObject(ctxt->context, resObj);
- }
-
- }
- #endif
- comp = ctxt->comp;
- if (comp->last < 0) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlXPathRunEval: last is less than zero\n");
- return(-1);
- }
- if (toBool)
- return(xmlXPathCompOpEvalToBoolean(ctxt,
- &comp->steps[comp->last], 0));
- else
- xmlXPathCompOpEval(ctxt, &comp->steps[comp->last]);
- return(0);
- }
- int
- xmlXPathEvalPredicate(xmlXPathContextPtr ctxt, xmlXPathObjectPtr res) {
- if ((ctxt == NULL) || (res == NULL)) return(0);
- switch (res->type) {
- case XPATH_BOOLEAN:
- return(res->boolval);
- case XPATH_NUMBER:
- return(res->floatval == ctxt->proximityPosition);
- case XPATH_NODESET:
- case XPATH_XSLT_TREE:
- if (res->nodesetval == NULL)
- return(0);
- return(res->nodesetval->nodeNr != 0);
- case XPATH_STRING:
- return((res->stringval != NULL) &&
- (xmlStrlen(res->stringval) != 0));
- default:
- STRANGE
- }
- return(0);
- }
- int
- xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt,
- xmlXPathObjectPtr res) {
- if ((ctxt == NULL) || (res == NULL)) return(0);
- switch (res->type) {
- case XPATH_BOOLEAN:
- return(res->boolval);
- case XPATH_NUMBER:
- #if defined(__BORLANDC__) || (defined(_MSC_VER) && (_MSC_VER == 1200))
- return((res->floatval == ctxt->context->proximityPosition) &&
- (!xmlXPathIsNaN(res->floatval)));
- #else
- return(res->floatval == ctxt->context->proximityPosition);
- #endif
- case XPATH_NODESET:
- case XPATH_XSLT_TREE:
- if (res->nodesetval == NULL)
- return(0);
- return(res->nodesetval->nodeNr != 0);
- case XPATH_STRING:
- return((res->stringval != NULL) && (res->stringval[0] != 0));
- #ifdef LIBXML_XPTR_ENABLED
- case XPATH_LOCATIONSET:{
- xmlLocationSetPtr ptr = res->user;
- if (ptr == NULL)
- return(0);
- return (ptr->locNr != 0);
- }
- #endif
- default:
- STRANGE
- }
- return(0);
- }
- #ifdef XPATH_STREAMING
- static xmlXPathCompExprPtr
- xmlXPathTryStreamCompile(xmlXPathContextPtr ctxt, const xmlChar *str) {
-
- xmlPatternPtr stream;
- xmlXPathCompExprPtr comp;
- xmlDictPtr dict = NULL;
- const xmlChar **namespaces = NULL;
- xmlNsPtr ns;
- int i, j;
- if ((!xmlStrchr(str, '[')) && (!xmlStrchr(str, '(')) &&
- (!xmlStrchr(str, '@'))) {
- const xmlChar *tmp;
-
- tmp = xmlStrchr(str, ':');
- if ((tmp != NULL) &&
- ((ctxt == NULL) || (ctxt->nsNr == 0) || (tmp[1] == ':')))
- return(NULL);
- if (ctxt != NULL) {
- dict = ctxt->dict;
- if (ctxt->nsNr > 0) {
- namespaces = xmlMalloc(2 * (ctxt->nsNr + 1) * sizeof(xmlChar*));
- if (namespaces == NULL) {
- xmlXPathErrMemory(ctxt, "allocating namespaces array\n");
- return(NULL);
- }
- for (i = 0, j = 0; (j < ctxt->nsNr); j++) {
- ns = ctxt->namespaces[j];
- namespaces[i++] = ns->href;
- namespaces[i++] = ns->prefix;
- }
- namespaces[i++] = NULL;
- namespaces[i] = NULL;
- }
- }
- stream = xmlPatterncompile(str, dict, XML_PATTERN_XPATH,
- &namespaces[0]);
- if (namespaces != NULL) {
- xmlFree((xmlChar **)namespaces);
- }
- if ((stream != NULL) && (xmlPatternStreamable(stream) == 1)) {
- comp = xmlXPathNewCompExpr();
- if (comp == NULL) {
- xmlXPathErrMemory(ctxt, "allocating streamable expression\n");
- return(NULL);
- }
- comp->stream = stream;
- comp->dict = dict;
- if (comp->dict)
- xmlDictReference(comp->dict);
- return(comp);
- }
- xmlFreePattern(stream);
- }
- return(NULL);
- }
- #endif
- static int
- xmlXPathCanRewriteDosExpression(xmlChar *expr)
- {
- if (expr == NULL)
- return(0);
- do {
- if ((*expr == '/') && (*(++expr) == '/'))
- return(1);
- } while (*expr++);
- return(0);
- }
- static void
- xmlXPathRewriteDOSExpression(xmlXPathCompExprPtr comp, xmlXPathStepOpPtr op)
- {
-
- if (op->ch1 != -1) {
- if ((op->op == XPATH_OP_COLLECT ) &&
- ((xmlXPathAxisVal) op->value == AXIS_CHILD ) &&
- ((xmlXPathTestVal) op->value2 == NODE_TEST_NAME ) &&
- ((xmlXPathTypeVal) op->value3 == NODE_TYPE_NODE ))
- {
-
- xmlXPathStepOpPtr prevop = &comp->steps[op->ch1];
- if ((prevop->op == XPATH_OP_COLLECT ) &&
- (prevop->ch1 != -1) &&
- ((xmlXPathAxisVal) prevop->value ==
- AXIS_DESCENDANT_OR_SELF) &&
- (prevop->ch2 == -1) &&
- ((xmlXPathTestVal) prevop->value2 == NODE_TEST_TYPE) &&
- ((xmlXPathTypeVal) prevop->value3 == NODE_TYPE_NODE) &&
- (comp->steps[prevop->ch1].op == XPATH_OP_ROOT))
- {
-
- op->ch1 = prevop->ch1;
- op->rewriteType = XP_REWRITE_DOS_CHILD_ELEM;
- }
- }
- if (op->ch1 != -1)
- xmlXPathRewriteDOSExpression(comp, &comp->steps[op->ch1]);
- }
- if (op->ch2 != -1)
- xmlXPathRewriteDOSExpression(comp, &comp->steps[op->ch2]);
- }
- xmlXPathCompExprPtr
- xmlXPathCtxtCompile(xmlXPathContextPtr ctxt, const xmlChar *str) {
- xmlXPathParserContextPtr pctxt;
- xmlXPathCompExprPtr comp;
- #ifdef XPATH_STREAMING
- comp = xmlXPathTryStreamCompile(ctxt, str);
- if (comp != NULL)
- return(comp);
- #endif
- xmlXPathInit();
- pctxt = xmlXPathNewParserContext(str, ctxt);
- if (pctxt == NULL)
- return NULL;
- xmlXPathCompileExpr(pctxt, 1);
- if( pctxt->error != XPATH_EXPRESSION_OK )
- {
- xmlXPathFreeParserContext(pctxt);
- return(NULL);
- }
- if (*pctxt->cur != 0) {
-
- xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR);
- comp = NULL;
- } else {
- comp = pctxt->comp;
- pctxt->comp = NULL;
- }
- xmlXPathFreeParserContext(pctxt);
- if (comp != NULL) {
- comp->expr = xmlStrdup(str);
- #ifdef DEBUG_EVAL_COUNTS
- comp->string = xmlStrdup(str);
- comp->nb = 0;
- #endif
- if ((comp->expr != NULL) &&
- (comp->nbStep > 2) &&
- (comp->last >= 0) &&
- (xmlXPathCanRewriteDosExpression(comp->expr) == 1))
- {
- xmlXPathRewriteDOSExpression(comp, &comp->steps[comp->last]);
- }
- }
- return(comp);
- }
- xmlXPathCompExprPtr
- xmlXPathCompile(const xmlChar *str) {
- return(xmlXPathCtxtCompile(NULL, str));
- }
- static int
- xmlXPathCompiledEvalInternal(xmlXPathCompExprPtr comp,
- xmlXPathContextPtr ctxt,
- xmlXPathObjectPtr *resObj,
- int toBool)
- {
- xmlXPathParserContextPtr pctxt;
- #ifndef LIBXML_THREAD_ENABLED
- static int reentance = 0;
- #endif
- int res;
- CHECK_CTXT_NEG(ctxt)
- if (comp == NULL)
- return(-1);
- xmlXPathInit();
- #ifndef LIBXML_THREAD_ENABLED
- reentance++;
- if (reentance > 1)
- xmlXPathDisableOptimizer = 1;
- #endif
- #ifdef DEBUG_EVAL_COUNTS
- comp->nb++;
- if ((comp->string != NULL) && (comp->nb > 100)) {
- fprintf(stderr, "100 x %s\n", comp->string);
- comp->nb = 0;
- }
- #endif
- pctxt = xmlXPathCompParserContext(comp, ctxt);
- res = xmlXPathRunEval(pctxt, toBool);
- if (resObj) {
- if (pctxt->value == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlXPathCompiledEval: evaluation failed\n");
- *resObj = NULL;
- } else {
- *resObj = valuePop(pctxt);
- }
- }
-
- if (pctxt->valueNr > 0) {
- xmlXPathObjectPtr tmp;
- int stack = 0;
- do {
- tmp = valuePop(pctxt);
- if (tmp != NULL) {
- stack++;
- xmlXPathReleaseObject(ctxt, tmp);
- }
- } while (tmp != NULL);
- if ((stack != 0) &&
- ((toBool) || ((resObj) && (*resObj))))
- {
- xmlGenericError(xmlGenericErrorContext,
- "xmlXPathCompiledEval: %d objects left on the stack.\n",
- stack);
- }
- }
- if ((pctxt->error != XPATH_EXPRESSION_OK) && (resObj) && (*resObj)) {
- xmlXPathFreeObject(*resObj);
- *resObj = NULL;
- }
- pctxt->comp = NULL;
- xmlXPathFreeParserContext(pctxt);
- #ifndef LIBXML_THREAD_ENABLED
- reentance--;
- #endif
- return(res);
- }
- xmlXPathObjectPtr
- xmlXPathCompiledEval(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctx)
- {
- xmlXPathObjectPtr res = NULL;
- xmlXPathCompiledEvalInternal(comp, ctx, &res, 0);
- return(res);
- }
- int
- xmlXPathCompiledEvalToBoolean(xmlXPathCompExprPtr comp,
- xmlXPathContextPtr ctxt)
- {
- return(xmlXPathCompiledEvalInternal(comp, ctxt, NULL, 1));
- }
- void
- xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt) {
- #ifdef XPATH_STREAMING
- xmlXPathCompExprPtr comp;
- #endif
- if (ctxt == NULL) return;
- #ifdef XPATH_STREAMING
- comp = xmlXPathTryStreamCompile(ctxt->context, ctxt->base);
- if (comp != NULL) {
- if (ctxt->comp != NULL)
- xmlXPathFreeCompExpr(ctxt->comp);
- ctxt->comp = comp;
- if (ctxt->cur != NULL)
- while (*ctxt->cur != 0) ctxt->cur++;
- } else
- #endif
- {
- xmlXPathCompileExpr(ctxt, 1);
-
- if ((ctxt->error == XPATH_EXPRESSION_OK) &&
- (ctxt->comp != NULL) &&
- (ctxt->base != NULL) &&
- (ctxt->comp->nbStep > 2) &&
- (ctxt->comp->last >= 0) &&
- (xmlXPathCanRewriteDosExpression((xmlChar *) ctxt->base) == 1))
- {
- xmlXPathRewriteDOSExpression(ctxt->comp,
- &ctxt->comp->steps[ctxt->comp->last]);
- }
- }
- CHECK_ERROR;
- xmlXPathRunEval(ctxt, 0);
- }
- xmlXPathObjectPtr
- xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx) {
- xmlXPathParserContextPtr ctxt;
- xmlXPathObjectPtr res, tmp, init = NULL;
- int stack = 0;
- CHECK_CTXT(ctx)
- xmlXPathInit();
- ctxt = xmlXPathNewParserContext(str, ctx);
- if (ctxt == NULL)
- return NULL;
- xmlXPathEvalExpr(ctxt);
- if (ctxt->value == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlXPathEval: evaluation failed\n");
- res = NULL;
- } else if ((*ctxt->cur != 0) && (ctxt->comp != NULL)
- #ifdef XPATH_STREAMING
- && (ctxt->comp->stream == NULL)
- #endif
- ) {
- xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR);
- res = NULL;
- } else {
- res = valuePop(ctxt);
- }
- do {
- tmp = valuePop(ctxt);
- if (tmp != NULL) {
- if (tmp != init)
- stack++;
- xmlXPathReleaseObject(ctx, tmp);
- }
- } while (tmp != NULL);
- if ((stack != 0) && (res != NULL)) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlXPathEval: %d object left on the stack\n",
- stack);
- }
- if (ctxt->error != XPATH_EXPRESSION_OK) {
- xmlXPathFreeObject(res);
- res = NULL;
- }
- xmlXPathFreeParserContext(ctxt);
- return(res);
- }
- xmlXPathObjectPtr
- xmlXPathEvalExpression(const xmlChar *str, xmlXPathContextPtr ctxt) {
- xmlXPathParserContextPtr pctxt;
- xmlXPathObjectPtr res, tmp;
- int stack = 0;
- CHECK_CTXT(ctxt)
- xmlXPathInit();
- pctxt = xmlXPathNewParserContext(str, ctxt);
- if (pctxt == NULL)
- return NULL;
- xmlXPathEvalExpr(pctxt);
- if ((*pctxt->cur != 0) || (pctxt->error != XPATH_EXPRESSION_OK)) {
- xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR);
- res = NULL;
- } else {
- res = valuePop(pctxt);
- }
- do {
- tmp = valuePop(pctxt);
- if (tmp != NULL) {
- xmlXPathReleaseObject(ctxt, tmp);
- stack++;
- }
- } while (tmp != NULL);
- if ((stack != 0) && (res != NULL)) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlXPathEvalExpression: %d object left on the stack\n",
- stack);
- }
- xmlXPathFreeParserContext(pctxt);
- return(res);
- }
- static void
- xmlXPathEscapeUriFunction(xmlXPathParserContextPtr ctxt, int nargs) {
- xmlXPathObjectPtr str;
- int escape_reserved;
- xmlBufferPtr target;
- xmlChar *cptr;
- xmlChar escape[4];
- CHECK_ARITY(2);
- escape_reserved = xmlXPathPopBoolean(ctxt);
- CAST_TO_STRING;
- str = valuePop(ctxt);
- target = xmlBufferCreate();
- escape[0] = '%';
- escape[3] = 0;
- if (target) {
- for (cptr = str->stringval; *cptr; cptr++) {
- if ((*cptr >= 'A' && *cptr <= 'Z') ||
- (*cptr >= 'a' && *cptr <= 'z') ||
- (*cptr >= '0' && *cptr <= '9') ||
- *cptr == '-' || *cptr == '_' || *cptr == '.' ||
- *cptr == '!' || *cptr == '~' || *cptr == '*' ||
- *cptr == '\''|| *cptr == '(' || *cptr == ')' ||
- (*cptr == '%' &&
- ((cptr[1] >= 'A' && cptr[1] <= 'F') ||
- (cptr[1] >= 'a' && cptr[1] <= 'f') ||
- (cptr[1] >= '0' && cptr[1] <= '9')) &&
- ((cptr[2] >= 'A' && cptr[2] <= 'F') ||
- (cptr[2] >= 'a' && cptr[2] <= 'f') ||
- (cptr[2] >= '0' && cptr[2] <= '9'))) ||
- (!escape_reserved &&
- (*cptr == ';' || *cptr == '/' || *cptr == '?' ||
- *cptr == ':' || *cptr == '@' || *cptr == '&' ||
- *cptr == '=' || *cptr == '+' || *cptr == '$' ||
- *cptr == ','))) {
- xmlBufferAdd(target, cptr, 1);
- } else {
- if ((*cptr >> 4) < 10)
- escape[1] = '0' + (*cptr >> 4);
- else
- escape[1] = 'A' - 10 + (*cptr >> 4);
- if ((*cptr & 0xF) < 10)
- escape[2] = '0' + (*cptr & 0xF);
- else
- escape[2] = 'A' - 10 + (*cptr & 0xF);
- xmlBufferAdd(target, &escape[0], 3);
- }
- }
- }
- valuePush(ctxt, xmlXPathCacheNewString(ctxt->context,
- xmlBufferContent(target)));
- xmlBufferFree(target);
- xmlXPathReleaseObject(ctxt->context, str);
- }
- void
- xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt)
- {
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"boolean",
- xmlXPathBooleanFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"ceiling",
- xmlXPathCeilingFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"count",
- xmlXPathCountFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"concat",
- xmlXPathConcatFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"contains",
- xmlXPathContainsFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"id",
- xmlXPathIdFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"false",
- xmlXPathFalseFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"floor",
- xmlXPathFloorFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"last",
- xmlXPathLastFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"lang",
- xmlXPathLangFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"local-name",
- xmlXPathLocalNameFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"not",
- xmlXPathNotFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"name",
- xmlXPathNameFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"namespace-uri",
- xmlXPathNamespaceURIFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"normalize-space",
- xmlXPathNormalizeFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"number",
- xmlXPathNumberFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"position",
- xmlXPathPositionFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"round",
- xmlXPathRoundFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string",
- xmlXPathStringFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string-length",
- xmlXPathStringLengthFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"starts-with",
- xmlXPathStartsWithFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring",
- xmlXPathSubstringFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-before",
- xmlXPathSubstringBeforeFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-after",
- xmlXPathSubstringAfterFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"sum",
- xmlXPathSumFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"true",
- xmlXPathTrueFunction);
- xmlXPathRegisterFunc(ctxt, (const xmlChar *)"translate",
- xmlXPathTranslateFunction);
- xmlXPathRegisterFuncNS(ctxt, (const xmlChar *)"escape-uri",
- (const xmlChar *)"http://www.w3.org/2002/08/xquery-functions",
- xmlXPathEscapeUriFunction);
- }
- #endif
- #define bottom_xpath
- #include "elfgcchack.h"
|