classes.pas 270 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913
  1. {
  2. This file is part of the Pas2JS run time library.
  3. Copyright (c) 2017 by Mattias Gaertner
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. unit Classes;
  11. {$mode objfpc}
  12. interface
  13. uses
  14. RTLConsts, Types, SysUtils, JS, TypInfo;
  15. type
  16. TNotifyEvent = procedure(Sender: TObject) of object;
  17. TNotifyEventRef = reference to procedure(Sender: TObject);
  18. TStringNotifyEventRef = Reference to Procedure(Sender: TObject; Const aString : String);
  19. // Notification operations :
  20. // Observer has changed, is freed, item added to/deleted from list, custom event.
  21. TFPObservedOperation = (ooChange,ooFree,ooAddItem,ooDeleteItem,ooCustom);
  22. EStreamError = class(Exception);
  23. EFCreateError = class(EStreamError);
  24. EFOpenError = class(EStreamError);
  25. EFilerError = class(EStreamError);
  26. EReadError = class(EFilerError);
  27. EWriteError = class(EFilerError);
  28. EClassNotFound = class(EFilerError);
  29. EMethodNotFound = class(EFilerError);
  30. EInvalidImage = class(EFilerError);
  31. EResNotFound = class(Exception);
  32. EListError = class(Exception);
  33. EBitsError = class(Exception);
  34. EStringListError = class(EListError);
  35. EComponentError = class(Exception);
  36. EParserError = class(Exception);
  37. EOutOfResources = class(EOutOfMemory);
  38. EInvalidOperation = class(Exception);
  39. TListAssignOp = (laCopy, laAnd, laOr, laXor, laSrcUnique, laDestUnique);
  40. TListSortCompare = function(Item1, Item2: JSValue): Integer;
  41. TListSortCompareFunc = reference to function (Item1, Item2: JSValue): Integer;
  42. TListCallback = Types.TListCallback;
  43. TListStaticCallback = Types.TListStaticCallback;
  44. TAlignment = (taLeftJustify, taRightJustify, taCenter);
  45. // Forward class definitions
  46. TFPList = Class;
  47. TReader = Class;
  48. TWriter = Class;
  49. TFiler = Class;
  50. { TFPListEnumerator }
  51. TFPListEnumerator = class
  52. private
  53. FList: TFPList;
  54. FPosition: Integer;
  55. public
  56. constructor Create(AList: TFPList); reintroduce;
  57. function GetCurrent: JSValue;
  58. function MoveNext: Boolean;
  59. property Current: JSValue read GetCurrent;
  60. end;
  61. { TFPList }
  62. TFPList = class(TObject)
  63. private
  64. FList: TJSValueDynArray;
  65. FCount: Integer;
  66. FCapacity: Integer;
  67. procedure CopyMove(aList: TFPList);
  68. procedure MergeMove(aList: TFPList);
  69. procedure DoCopy(ListA, ListB: TFPList);
  70. procedure DoSrcUnique(ListA, ListB: TFPList);
  71. procedure DoAnd(ListA, ListB: TFPList);
  72. procedure DoDestUnique(ListA, ListB: TFPList);
  73. procedure DoOr(ListA, ListB: TFPList);
  74. procedure DoXOr(ListA, ListB: TFPList);
  75. protected
  76. function Get(Index: Integer): JSValue; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  77. procedure Put(Index: Integer; Item: JSValue); {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  78. procedure SetCapacity(NewCapacity: Integer);
  79. procedure SetCount(NewCount: Integer);
  80. Procedure RaiseIndexError(Index: Integer);
  81. public
  82. //Type
  83. // TDirection = (FromBeginning, FromEnd);
  84. destructor Destroy; override;
  85. procedure AddList(AList: TFPList);
  86. function Add(Item: JSValue): Integer; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  87. procedure Clear;
  88. procedure Delete(Index: Integer); {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  89. class procedure Error(const Msg: string; const Data: String);
  90. procedure Exchange(Index1, Index2: Integer);
  91. function Expand: TFPList; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  92. function Extract(Item: JSValue): JSValue;
  93. function First: JSValue;
  94. function GetEnumerator: TFPListEnumerator;
  95. function IndexOf(Item: JSValue): Integer;
  96. function IndexOfItem(Item: JSValue; Direction: TDirection): Integer;
  97. procedure Insert(Index: Integer; Item: JSValue); {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  98. function Last: JSValue;
  99. procedure Move(CurIndex, NewIndex: Integer);
  100. procedure Assign (ListA: TFPList; AOperator: TListAssignOp=laCopy; ListB: TFPList=nil);
  101. function Remove(Item: JSValue): Integer;
  102. procedure Pack;
  103. procedure Sort(const Compare: TListSortCompare);
  104. procedure SortList(const Compare: TListSortCompareFunc);
  105. procedure ForEachCall(const proc2call: TListCallback; const arg: JSValue);
  106. procedure ForEachCall(const proc2call: TListStaticCallback; const arg: JSValue);
  107. property Capacity: Integer read FCapacity write SetCapacity;
  108. property Count: Integer read FCount write SetCount;
  109. property Items[Index: Integer]: JSValue read Get write Put; default;
  110. property List: TJSValueDynArray read FList;
  111. end;
  112. TListNotification = (lnAdded, lnExtracted, lnDeleted);
  113. TList = class;
  114. { TListEnumerator }
  115. TListEnumerator = class
  116. private
  117. FList: TList;
  118. FPosition: Integer;
  119. public
  120. constructor Create(AList: TList); reintroduce;
  121. function GetCurrent: JSValue;
  122. function MoveNext: Boolean;
  123. property Current: JSValue read GetCurrent;
  124. end;
  125. { TList }
  126. TList = class(TObject)
  127. private
  128. FList: TFPList;
  129. procedure CopyMove (aList : TList);
  130. procedure MergeMove (aList : TList);
  131. procedure DoCopy(ListA, ListB : TList);
  132. procedure DoSrcUnique(ListA, ListB : TList);
  133. procedure DoAnd(ListA, ListB : TList);
  134. procedure DoDestUnique(ListA, ListB : TList);
  135. procedure DoOr(ListA, ListB : TList);
  136. procedure DoXOr(ListA, ListB : TList);
  137. protected
  138. function Get(Index: Integer): JSValue;
  139. procedure Put(Index: Integer; Item: JSValue);
  140. procedure Notify(aValue: JSValue; Action: TListNotification); virtual;
  141. procedure SetCapacity(NewCapacity: Integer);
  142. function GetCapacity: integer;
  143. procedure SetCount(NewCount: Integer);
  144. function GetCount: integer;
  145. function GetList: TJSValueDynArray;
  146. property FPList : TFPList Read FList;
  147. public
  148. constructor Create; reintroduce;
  149. destructor Destroy; override;
  150. Procedure AddList(AList : TList);
  151. function Add(Item: JSValue): Integer;
  152. procedure Clear; virtual;
  153. procedure Delete(Index: Integer);
  154. class procedure Error(const Msg: string; Data: String); virtual;
  155. procedure Exchange(Index1, Index2: Integer);
  156. function Expand: TList;
  157. function Extract(Item: JSValue): JSValue;
  158. function First: JSValue;
  159. function GetEnumerator: TListEnumerator;
  160. function IndexOf(Item: JSValue): Integer;
  161. procedure Insert(Index: Integer; Item: JSValue);
  162. function Last: JSValue;
  163. procedure Move(CurIndex, NewIndex: Integer);
  164. procedure Assign (ListA: TList; AOperator: TListAssignOp=laCopy; ListB: TList=nil);
  165. function Remove(Item: JSValue): Integer;
  166. procedure Pack;
  167. procedure Sort(const Compare: TListSortCompare);
  168. procedure SortList(const Compare: TListSortCompareFunc);
  169. property Capacity: Integer read GetCapacity write SetCapacity;
  170. property Count: Integer read GetCount write SetCount;
  171. property Items[Index: Integer]: JSValue read Get write Put; default;
  172. property List: TJSValueDynArray read GetList;
  173. end;
  174. { TPersistent }
  175. {$M+}
  176. TPersistent = class(TObject)
  177. private
  178. //FObservers : TFPList;
  179. procedure AssignError(Source: TPersistent);
  180. protected
  181. procedure DefineProperties(Filer: TFiler); virtual;
  182. procedure AssignTo(Dest: TPersistent); virtual;
  183. function GetOwner: TPersistent; virtual;
  184. public
  185. procedure Assign(Source: TPersistent); virtual;
  186. //procedure FPOAttachObserver(AObserver : TObject);
  187. //procedure FPODetachObserver(AObserver : TObject);
  188. //procedure FPONotifyObservers(ASender : TObject; AOperation: TFPObservedOperation; Data: TObject);
  189. function GetNamePath: string; virtual;
  190. end;
  191. TPersistentClass = Class of TPersistent;
  192. { TInterfacedPersistent }
  193. TInterfacedPersistent = class(TPersistent, IInterface)
  194. private
  195. FOwnerInterface: IInterface;
  196. protected
  197. function _AddRef: Integer; {$IFDEF MAKESTUB}stdcall;{$ENDIF}
  198. function _Release: Integer; {$IFDEF MAKESTUB}stdcall;{$ENDIF}
  199. public
  200. function QueryInterface(const IID: TGUID; out Obj): HRESULT; virtual;{$IFDEF MAKESTUB} stdcall;{$ENDIF}
  201. procedure AfterConstruction; override;
  202. end;
  203. TStrings = Class;
  204. { TStringsEnumerator class }
  205. TStringsEnumerator = class
  206. private
  207. FStrings: TStrings;
  208. FPosition: Integer;
  209. public
  210. constructor Create(AStrings: TStrings); reintroduce;
  211. function GetCurrent: String;
  212. function MoveNext: Boolean;
  213. property Current: String read GetCurrent;
  214. end;
  215. { TStrings class }
  216. TStrings = class(TPersistent)
  217. private
  218. FSpecialCharsInited : boolean;
  219. FAlwaysQuote: Boolean;
  220. FQuoteChar : Char;
  221. FDelimiter : Char;
  222. FNameValueSeparator : Char;
  223. FUpdateCount: Integer;
  224. FLBS : TTextLineBreakStyle;
  225. FSkipLastLineBreak : Boolean;
  226. FStrictDelimiter : Boolean;
  227. FLineBreak : String;
  228. function GetCommaText: string;
  229. function GetName(Index: Integer): string;
  230. function GetValue(const Name: string): string;
  231. Function GetLBS : TTextLineBreakStyle;
  232. Procedure SetLBS (AValue : TTextLineBreakStyle);
  233. procedure SetCommaText(const Value: string);
  234. procedure SetValue(const Name : String; Const Value: string);
  235. procedure SetDelimiter(c:Char);
  236. procedure SetQuoteChar(c:Char);
  237. procedure SetNameValueSeparator(c:Char);
  238. procedure DoSetTextStr(const Value: string; DoClear : Boolean);
  239. Function GetDelimiter : Char;
  240. Function GetNameValueSeparator : Char;
  241. Function GetQuoteChar: Char;
  242. Function GetLineBreak : String;
  243. procedure SetLineBreak(const S : String);
  244. Function GetSkipLastLineBreak : Boolean;
  245. procedure SetSkipLastLineBreak(const AValue : Boolean);
  246. procedure ReadData(Reader: TReader);
  247. procedure WriteData(Writer: TWriter);
  248. protected
  249. procedure DefineProperties(Filer: TFiler); override;
  250. procedure Error(const Msg: string; Data: Integer);
  251. function Get(Index: Integer): string; virtual; abstract;
  252. function GetCapacity: Integer; virtual;
  253. function GetCount: Integer; virtual; abstract;
  254. function GetObject(Index: Integer): TObject; virtual;
  255. function GetTextStr: string; virtual;
  256. procedure Put(Index: Integer; const S: string); virtual;
  257. procedure PutObject(Index: Integer; AObject: TObject); virtual;
  258. procedure SetCapacity(NewCapacity: Integer); virtual;
  259. procedure SetTextStr(const Value: string); virtual;
  260. procedure SetUpdateState(Updating: Boolean); virtual;
  261. property UpdateCount: Integer read FUpdateCount;
  262. Function DoCompareText(const s1,s2 : string) : PtrInt; virtual;
  263. Function GetDelimitedText: string;
  264. Procedure SetDelimitedText(Const AValue: string);
  265. Function GetValueFromIndex(Index: Integer): string;
  266. Procedure SetValueFromIndex(Index: Integer; const Value: string);
  267. Procedure CheckSpecialChars;
  268. // Class Function GetNextLine (Const Value : String; Var S : String; Var P : Integer) : Boolean;
  269. Function GetNextLinebreak (Const Value : String; Out S : String; Var P : Integer) : Boolean;
  270. public
  271. constructor Create; reintroduce;
  272. destructor Destroy; override;
  273. function ToObjectArray: TObjectDynArray; overload;
  274. function ToObjectArray(aStart,aEnd : Integer): TObjectDynArray; overload;
  275. function ToStringArray: TStringDynArray; overload;
  276. function ToStringArray(aStart,aEnd : Integer): TStringDynArray; overload;
  277. function Add(const S: string): Integer; virtual; overload;
  278. function Add(const Fmt : string; const Args : Array of const): Integer; overload;
  279. function AddFmt(const Fmt : string; const Args : Array of const): Integer;
  280. function AddObject(const S: string; AObject: TObject): Integer; virtual; overload;
  281. function AddObject(const Fmt: string; Args : Array of const; AObject: TObject): Integer; overload;
  282. procedure Append(const S: string);
  283. procedure AddStrings(TheStrings: TStrings); overload; virtual;
  284. procedure AddStrings(TheStrings: TStrings; ClearFirst : Boolean); overload;
  285. procedure AddStrings(const TheStrings: array of string); overload; virtual;
  286. procedure AddStrings(const TheStrings: array of string; ClearFirst : Boolean); overload;
  287. function AddPair(const AName, AValue: string): TStrings; overload;
  288. function AddPair(const AName, AValue: string; AObject: TObject): TStrings; overload;
  289. Procedure AddText(Const S : String); virtual;
  290. procedure Assign(Source: TPersistent); override;
  291. procedure BeginUpdate;
  292. procedure Clear; virtual; abstract;
  293. procedure Delete(Index: Integer); virtual; abstract;
  294. procedure EndUpdate;
  295. function Equals(Obj: TObject): Boolean; override; overload;
  296. function Equals(TheStrings: TStrings): Boolean; overload;
  297. procedure Exchange(Index1, Index2: Integer); virtual;
  298. function GetEnumerator: TStringsEnumerator;
  299. function IndexOf(const S: string): Integer; virtual;
  300. function IndexOfName(const Name: string): Integer; virtual;
  301. function IndexOfObject(AObject: TObject): Integer; virtual;
  302. procedure Insert(Index: Integer; const S: string); virtual; abstract;
  303. procedure InsertObject(Index: Integer; const S: string; AObject: TObject);
  304. procedure Move(CurIndex, NewIndex: Integer); virtual;
  305. procedure GetNameValue(Index : Integer; Out AName,AValue : String);
  306. Procedure LoadFromURL(Const aURL : String; Async : Boolean = True; OnLoaded : TNotifyEventRef = Nil; OnError: TStringNotifyEventRef = Nil); virtual;
  307. // Delphi compatibility. Must be an URL
  308. Procedure LoadFromFile(Const aFileName : String; const OnLoaded : TProc = Nil; const AError: TProcString = Nil);
  309. function ExtractName(Const S:String):String;
  310. Property TextLineBreakStyle : TTextLineBreakStyle Read GetLBS Write SetLBS;
  311. property Delimiter: Char read GetDelimiter write SetDelimiter;
  312. property DelimitedText: string read GetDelimitedText write SetDelimitedText;
  313. property LineBreak : string Read GetLineBreak write SetLineBreak;
  314. Property StrictDelimiter : Boolean Read FStrictDelimiter Write FStrictDelimiter;
  315. property AlwaysQuote: Boolean read FAlwaysQuote write FAlwaysQuote;
  316. property QuoteChar: Char read GetQuoteChar write SetQuoteChar;
  317. Property NameValueSeparator : Char Read GetNameValueSeparator Write SetNameValueSeparator;
  318. property ValueFromIndex[Index: Integer]: string read GetValueFromIndex write SetValueFromIndex;
  319. property Capacity: Integer read GetCapacity write SetCapacity;
  320. property CommaText: string read GetCommaText write SetCommaText;
  321. property Count: Integer read GetCount;
  322. property Names[Index: Integer]: string read GetName;
  323. property Objects[Index: Integer]: TObject read GetObject write PutObject;
  324. property Values[const Name: string]: string read GetValue write SetValue;
  325. property Strings[Index: Integer]: string read Get write Put; default;
  326. property Text: string read GetTextStr write SetTextStr;
  327. Property SkipLastLineBreak : Boolean Read GetSkipLastLineBreak Write SetSkipLastLineBreak;
  328. end;
  329. { TStringList}
  330. TStringItem = record
  331. FString: string;
  332. FObject: TObject;
  333. end;
  334. TStringItemArray = Array of TStringItem;
  335. TStringList = class;
  336. TStringListSortCompare = function(List: TStringList; Index1, Index2: Integer): Integer;
  337. TStringsSortStyle = (sslNone,sslUser,sslAuto);
  338. TStringsSortStyles = Set of TStringsSortStyle;
  339. TStringList = class(TStrings)
  340. private
  341. FList: TStringItemArray;
  342. FCount: Integer;
  343. FOnChange: TNotifyEvent;
  344. FOnChanging: TNotifyEvent;
  345. FDuplicates: TDuplicates;
  346. FCaseSensitive : Boolean;
  347. FForceSort : Boolean;
  348. FOwnsObjects : Boolean;
  349. FSortStyle: TStringsSortStyle;
  350. procedure ExchangeItemsInt(Index1, Index2: Integer);
  351. function GetSorted: Boolean;
  352. procedure Grow;
  353. procedure InternalClear(FromIndex : Integer = 0; ClearOnly : Boolean = False);
  354. procedure QuickSort(L, R: Integer; CompareFn: TStringListSortCompare);
  355. procedure SetSorted(Value: Boolean);
  356. procedure SetCaseSensitive(b : boolean);
  357. procedure SetSortStyle(AValue: TStringsSortStyle);
  358. protected
  359. Procedure CheckIndex(AIndex : Integer);
  360. procedure ExchangeItems(Index1, Index2: Integer); virtual;
  361. procedure Changed; virtual;
  362. procedure Changing; virtual;
  363. function Get(Index: Integer): string; override;
  364. function GetCapacity: Integer; override;
  365. function GetCount: Integer; override;
  366. function GetObject(Index: Integer): TObject; override;
  367. procedure Put(Index: Integer; const S: string); override;
  368. procedure PutObject(Index: Integer; AObject: TObject); override;
  369. procedure SetCapacity(NewCapacity: Integer); override;
  370. procedure SetUpdateState(Updating: Boolean); override;
  371. procedure InsertItem(Index: Integer; const S: string); virtual;
  372. procedure InsertItem(Index: Integer; const S: string; O: TObject); virtual;
  373. Function DoCompareText(const s1,s2 : string) : PtrInt; override;
  374. function CompareStrings(const s1,s2 : string) : Integer; virtual;
  375. public
  376. destructor Destroy; override;
  377. function Add(const S: string): Integer; override;
  378. procedure Clear; override;
  379. procedure Delete(Index: Integer); override;
  380. procedure Exchange(Index1, Index2: Integer); override;
  381. function Find(const S: string; Out Index: Integer): Boolean; virtual;
  382. function IndexOf(const S: string): Integer; override;
  383. procedure Insert(Index: Integer; const S: string); override;
  384. procedure Sort; virtual;
  385. procedure CustomSort(CompareFn: TStringListSortCompare); virtual;
  386. property Duplicates: TDuplicates read FDuplicates write FDuplicates;
  387. property Sorted: Boolean read GetSorted write SetSorted;
  388. property CaseSensitive: Boolean read FCaseSensitive write SetCaseSensitive;
  389. property OnChange: TNotifyEvent read FOnChange write FOnChange;
  390. property OnChanging: TNotifyEvent read FOnChanging write FOnChanging;
  391. property OwnsObjects : boolean read FOwnsObjects write FOwnsObjects;
  392. Property SortStyle : TStringsSortStyle Read FSortStyle Write SetSortStyle;
  393. end;
  394. TCollection = class;
  395. { TCollectionItem }
  396. TCollectionItem = class(TPersistent)
  397. private
  398. FCollection: TCollection;
  399. FID: Integer;
  400. FUpdateCount: Integer;
  401. function GetIndex: Integer;
  402. protected
  403. procedure SetCollection(Value: TCollection);virtual;
  404. procedure Changed(AllItems: Boolean);
  405. function GetOwner: TPersistent; override;
  406. function GetDisplayName: string; virtual;
  407. procedure SetIndex(Value: Integer); virtual;
  408. procedure SetDisplayName(const Value: string); virtual;
  409. property UpdateCount: Integer read FUpdateCount;
  410. public
  411. constructor Create(ACollection: TCollection); virtual; reintroduce;
  412. destructor Destroy; override;
  413. function GetNamePath: string; override;
  414. property Collection: TCollection read FCollection write SetCollection;
  415. property ID: Integer read FID;
  416. property Index: Integer read GetIndex write SetIndex;
  417. property DisplayName: string read GetDisplayName write SetDisplayName;
  418. end;
  419. TCollectionEnumerator = class
  420. private
  421. FCollection: TCollection;
  422. FPosition: Integer;
  423. public
  424. constructor Create(ACollection: TCollection); reintroduce;
  425. function GetCurrent: TCollectionItem;
  426. function MoveNext: Boolean;
  427. property Current: TCollectionItem read GetCurrent;
  428. end;
  429. TCollectionItemClass = class of TCollectionItem;
  430. TCollectionNotification = (cnAdded, cnExtracting, cnDeleting);
  431. TCollectionSortCompare = function (Item1, Item2: TCollectionItem): Integer;
  432. TCollectionSortCompareFunc = reference to function (Item1, Item2: TCollectionItem): Integer;
  433. TCollection = class(TPersistent)
  434. private
  435. FItemClass: TCollectionItemClass;
  436. FItems: TFpList;
  437. FUpdateCount: Integer;
  438. FNextID: Integer;
  439. FPropName: string;
  440. function GetCount: Integer;
  441. function GetPropName: string;
  442. procedure InsertItem(Item: TCollectionItem);
  443. procedure RemoveItem(Item: TCollectionItem);
  444. procedure DoClear;
  445. protected
  446. { Design-time editor support }
  447. function GetAttrCount: Integer; virtual;
  448. function GetAttr(Index: Integer): string; virtual;
  449. function GetItemAttr(Index, ItemIndex: Integer): string; virtual;
  450. procedure Changed;
  451. function GetItem(Index: Integer): TCollectionItem;
  452. procedure SetItem(Index: Integer; Value: TCollectionItem);
  453. procedure SetItemName(Item: TCollectionItem); virtual;
  454. procedure SetPropName; virtual;
  455. procedure Update(Item: TCollectionItem); virtual;
  456. procedure Notify(Item: TCollectionItem;Action: TCollectionNotification); virtual;
  457. property PropName: string read GetPropName write FPropName;
  458. property UpdateCount: Integer read FUpdateCount;
  459. public
  460. constructor Create(AItemClass: TCollectionItemClass); reintroduce;
  461. destructor Destroy; override;
  462. function Owner: TPersistent;
  463. function Add: TCollectionItem;
  464. procedure Assign(Source: TPersistent); override;
  465. procedure BeginUpdate; virtual;
  466. procedure Clear;
  467. procedure EndUpdate; virtual;
  468. procedure Delete(Index: Integer);
  469. function GetEnumerator: TCollectionEnumerator;
  470. function GetNamePath: string; override;
  471. function Insert(Index: Integer): TCollectionItem;
  472. function FindItemID(ID: Integer): TCollectionItem;
  473. procedure Exchange(Const Index1, index2: integer);
  474. procedure Sort(Const Compare : TCollectionSortCompare);
  475. procedure SortList(Const Compare : TCollectionSortCompareFunc);
  476. property Count: Integer read GetCount;
  477. property ItemClass: TCollectionItemClass read FItemClass;
  478. property Items[Index: Integer]: TCollectionItem read GetItem write SetItem;
  479. end;
  480. TOwnedCollection = class(TCollection)
  481. private
  482. FOwner: TPersistent;
  483. protected
  484. Function GetOwner: TPersistent; override;
  485. public
  486. Constructor Create(AOwner: TPersistent; AItemClass: TCollectionItemClass); reintroduce;
  487. end;
  488. TComponent = Class;
  489. TOperation = (opInsert, opRemove);
  490. TComponentStateItem = ( csLoading, csReading, csWriting, csDestroying,
  491. csDesigning, csAncestor, csUpdating, csFixups, csFreeNotification,
  492. csInline, csDesignInstance);
  493. TComponentState = set of TComponentStateItem;
  494. TComponentStyleItem = (csInheritable, csCheckPropAvail, csSubComponent, csTransient);
  495. TComponentStyle = set of TComponentStyleItem;
  496. TGetChildProc = procedure (Child: TComponent) of object;
  497. TComponentName = string;
  498. { TComponentEnumerator }
  499. TComponentEnumerator = class
  500. private
  501. FComponent: TComponent;
  502. FPosition: Integer;
  503. public
  504. constructor Create(AComponent: TComponent); reintroduce;
  505. function GetCurrent: TComponent;
  506. function MoveNext: Boolean;
  507. property Current: TComponent read GetCurrent;
  508. end;
  509. TComponent = class(TPersistent, IInterface)
  510. private
  511. FOwner: TComponent;
  512. FName: TComponentName;
  513. FTag: Ptrint;
  514. FComponents: TFpList;
  515. FFreeNotifies: TFpList;
  516. FDesignInfo: Longint;
  517. FComponentState: TComponentState;
  518. function GetComponent(AIndex: Integer): TComponent;
  519. function GetComponentCount: Integer;
  520. function GetComponentIndex: Integer;
  521. procedure Insert(AComponent: TComponent);
  522. procedure ReadLeft(AReader: TReader);
  523. procedure ReadTop(AReader: TReader);
  524. procedure Remove(AComponent: TComponent);
  525. procedure RemoveNotification(AComponent: TComponent);
  526. procedure SetComponentIndex(Value: Integer);
  527. procedure SetReference(Enable: Boolean);
  528. procedure WriteLeft(AWriter: TWriter);
  529. procedure WriteTop(AWriter: TWriter);
  530. protected
  531. FComponentStyle: TComponentStyle;
  532. procedure ChangeName(const NewName: TComponentName);
  533. procedure DefineProperties(Filer: TFiler); override;
  534. procedure GetChildren(Proc: TGetChildProc; Root: TComponent); virtual;
  535. function GetChildOwner: TComponent; virtual;
  536. function GetChildParent: TComponent; virtual;
  537. function GetOwner: TPersistent; override;
  538. procedure Loaded; virtual;
  539. procedure Loading; virtual;
  540. procedure SetWriting(Value: Boolean); virtual;
  541. procedure SetReading(Value: Boolean); virtual;
  542. procedure Notification(AComponent: TComponent; Operation: TOperation); virtual;
  543. procedure PaletteCreated; virtual;
  544. procedure ReadState(Reader: TReader); virtual;
  545. procedure SetAncestor(Value: Boolean);
  546. procedure SetDesigning(Value: Boolean; SetChildren : Boolean = True);
  547. procedure SetDesignInstance(Value: Boolean);
  548. procedure SetInline(Value: Boolean);
  549. procedure SetName(const NewName: TComponentName); virtual;
  550. procedure SetChildOrder(Child: TComponent; Order: Integer); virtual;
  551. procedure SetParentComponent(Value: TComponent); virtual;
  552. procedure Updating; virtual;
  553. procedure Updated; virtual;
  554. procedure ValidateRename(AComponent: TComponent; const CurName, NewName: string); virtual;
  555. procedure ValidateContainer(AComponent: TComponent); virtual;
  556. procedure ValidateInsert(AComponent: TComponent); virtual;
  557. protected
  558. function _AddRef: Integer; {$IFDEF MAKESTUB}stdcall;{$ENDIF}
  559. function _Release: Integer; {$IFDEF MAKESTUB}stdcall;{$ENDIF}
  560. public
  561. constructor Create(AOwner: TComponent); virtual; reintroduce;
  562. destructor Destroy; override;
  563. procedure BeforeDestruction; override;
  564. procedure DestroyComponents;
  565. procedure Destroying;
  566. function QueryInterface(const IID: TGUID; out Obj): HRESULT; virtual; {$IFDEF MAKESTUB} stdcall;{$ENDIF}
  567. procedure WriteState(Writer: TWriter); virtual;
  568. // function ExecuteAction(Action: TBasicAction): Boolean; virtual;
  569. function FindComponent(const AName: string): TComponent;
  570. procedure FreeNotification(AComponent: TComponent);
  571. procedure RemoveFreeNotification(AComponent: TComponent);
  572. function GetNamePath: string; override;
  573. function GetParentComponent: TComponent; virtual;
  574. function HasParent: Boolean; virtual;
  575. procedure InsertComponent(AComponent: TComponent);
  576. procedure RemoveComponent(AComponent: TComponent);
  577. procedure SetSubComponent(ASubComponent: Boolean);
  578. function GetEnumerator: TComponentEnumerator;
  579. // function UpdateAction(Action: TBasicAction): Boolean; dynamic;
  580. property Components[Index: Integer]: TComponent read GetComponent;
  581. property ComponentCount: Integer read GetComponentCount;
  582. property ComponentIndex: Integer read GetComponentIndex write SetComponentIndex;
  583. property ComponentState: TComponentState read FComponentState;
  584. property ComponentStyle: TComponentStyle read FComponentStyle;
  585. property DesignInfo: Longint read FDesignInfo write FDesignInfo;
  586. property Owner: TComponent read FOwner;
  587. published
  588. property Name: TComponentName read FName write SetName stored False;
  589. property Tag: PtrInt read FTag write FTag default 0;
  590. end;
  591. TComponentClass = Class of TComponent;
  592. TSeekOrigin = (soBeginning, soCurrent, soEnd);
  593. { TStream }
  594. TStream = class(TObject)
  595. private
  596. FEndian: TEndian;
  597. function MakeInt(B: TBytes; aSize: Integer; Signed: Boolean): NativeInt;
  598. function MakeBytes(B: NativeInt; aSize: Integer; Signed: Boolean): TBytes;
  599. protected
  600. procedure InvalidSeek; virtual;
  601. procedure Discard(const Count: NativeInt);
  602. procedure DiscardLarge(Count: NativeInt; const MaxBufferSize: Longint);
  603. procedure FakeSeekForward(Offset: NativeInt; const Origin: TSeekOrigin; const Pos: NativeInt);
  604. function GetPosition: NativeInt; virtual;
  605. procedure SetPosition(const Pos: NativeInt); virtual;
  606. function GetSize: NativeInt; virtual;
  607. procedure SetSize(const NewSize: NativeInt); virtual;
  608. procedure SetSize64(const NewSize: NativeInt); virtual;
  609. procedure ReadNotImplemented;
  610. procedure WriteNotImplemented;
  611. function ReadMaxSizeData(Buffer : TBytes; aSize,aCount : NativeInt) : NativeInt;
  612. Procedure ReadExactSizeData(Buffer : TBytes; aSize,aCount : NativeInt);
  613. function WriteMaxSizeData(Const Buffer : TBytes; aSize,aCount : NativeInt) : NativeInt;
  614. Procedure WriteExactSizeData(Const Buffer : TBytes; aSize,aCount : NativeInt);
  615. public
  616. function Read(var Buffer: TBytes; Count: Longint): Longint; overload;
  617. function Read(Buffer : TBytes; aOffset, Count: Longint): Longint; virtual; abstract; overload;
  618. function Write(const Buffer: TBytes; Count: Longint): Longint; virtual; overload;
  619. function Write(const Buffer: TBytes; Offset, Count: Longint): Longint; virtual; abstract; overload;
  620. function Seek(const Offset: NativeInt; Origin: TSeekOrigin): NativeInt; virtual; abstract; overload;
  621. function ReadData(Buffer: TBytes; Count: NativeInt): NativeInt; overload;
  622. function ReadData(var Buffer: Boolean): NativeInt; overload;
  623. function ReadData(var Buffer: Boolean; Count: NativeInt): NativeInt; overload;
  624. function ReadData(var Buffer: WideChar): NativeInt; overload;
  625. function ReadData(var Buffer: WideChar; Count: NativeInt): NativeInt; overload;
  626. function ReadData(var Buffer: Int8): NativeInt; overload;
  627. function ReadData(var Buffer: Int8; Count: NativeInt): NativeInt; overload;
  628. function ReadData(var Buffer: UInt8): NativeInt; overload;
  629. function ReadData(var Buffer: UInt8; Count: NativeInt): NativeInt; overload;
  630. function ReadData(var Buffer: Int16): NativeInt; overload;
  631. function ReadData(var Buffer: Int16; Count: NativeInt): NativeInt; overload;
  632. function ReadData(var Buffer: UInt16): NativeInt; overload;
  633. function ReadData(var Buffer: UInt16; Count: NativeInt): NativeInt; overload;
  634. function ReadData(var Buffer: Int32): NativeInt; overload;
  635. function ReadData(var Buffer: Int32; Count: NativeInt): NativeInt; overload;
  636. function ReadData(var Buffer: UInt32): NativeInt; overload;
  637. function ReadData(var Buffer: UInt32; Count: NativeInt): NativeInt; overload;
  638. // NativeLargeint. Stored as a float64, Read as float64.
  639. function ReadData(var Buffer: NativeLargeInt): NativeInt; overload;
  640. function ReadData(var Buffer: NativeLargeInt; Count: NativeInt): NativeInt; overload;
  641. function ReadData(var Buffer: NativeLargeUInt): NativeInt; overload;
  642. function ReadData(var Buffer: NativeLargeUInt; Count: NativeInt): NativeInt; overload;
  643. // Note: a ReadData with Int64 would be Delphi/FPC incompatible
  644. function ReadData(var Buffer: Double): NativeInt; overload;
  645. function ReadData(var Buffer: Double; Count: NativeInt): NativeInt; overload;
  646. procedure ReadBuffer(var Buffer: TBytes; Count: NativeInt); overload;
  647. procedure ReadBuffer(var Buffer: TBytes; Offset, Count: NativeInt); overload;
  648. procedure ReadBufferData(var Buffer: Boolean); overload;
  649. procedure ReadBufferData(var Buffer: Boolean; Count: NativeInt); overload;
  650. procedure ReadBufferData(var Buffer: WideChar); overload;
  651. procedure ReadBufferData(var Buffer: WideChar; Count: NativeInt); overload;
  652. procedure ReadBufferData(var Buffer: Int8); overload;
  653. procedure ReadBufferData(var Buffer: Int8; Count: NativeInt); overload;
  654. procedure ReadBufferData(var Buffer: UInt8); overload;
  655. procedure ReadBufferData(var Buffer: UInt8; Count: NativeInt); overload;
  656. procedure ReadBufferData(var Buffer: Int16); overload;
  657. procedure ReadBufferData(var Buffer: Int16; Count: NativeInt); overload;
  658. procedure ReadBufferData(var Buffer: UInt16); overload;
  659. procedure ReadBufferData(var Buffer: UInt16; Count: NativeInt); overload;
  660. procedure ReadBufferData(var Buffer: Int32); overload;
  661. procedure ReadBufferData(var Buffer: Int32; Count: NativeInt); overload;
  662. procedure ReadBufferData(var Buffer: UInt32); overload;
  663. procedure ReadBufferData(var Buffer: UInt32; Count: NativeInt); overload;
  664. // NativeLargeint. Stored as a float64, Read as float64.
  665. procedure ReadBufferData(var Buffer: NativeLargeInt); overload;
  666. procedure ReadBufferData(var Buffer: NativeLargeInt; Count: NativeInt); overload;
  667. procedure ReadBufferData(var Buffer: NativeLargeUInt); overload;
  668. procedure ReadBufferData(var Buffer: NativeLargeUInt; Count: NativeInt); overload;
  669. procedure ReadBufferData(var Buffer: Double); overload;
  670. procedure ReadBufferData(var Buffer: Double; Count: NativeInt); overload;
  671. procedure WriteBuffer(const Buffer: TBytes; Count: NativeInt); overload;
  672. procedure WriteBuffer(const Buffer: TBytes; Offset, Count: NativeInt); overload;
  673. function WriteData(const Buffer: TBytes; Count: NativeInt): NativeInt; overload;
  674. function WriteData(const Buffer: Boolean): NativeInt; overload;
  675. function WriteData(const Buffer: Boolean; Count: NativeInt): NativeInt; overload;
  676. function WriteData(const Buffer: WideChar): NativeInt; overload;
  677. function WriteData(const Buffer: WideChar; Count: NativeInt): NativeInt; overload;
  678. function WriteData(const Buffer: Int8): NativeInt; overload;
  679. function WriteData(const Buffer: Int8; Count: NativeInt): NativeInt; overload;
  680. function WriteData(const Buffer: UInt8): NativeInt; overload;
  681. function WriteData(const Buffer: UInt8; Count: NativeInt): NativeInt; overload;
  682. function WriteData(const Buffer: Int16): NativeInt; overload;
  683. function WriteData(const Buffer: Int16; Count: NativeInt): NativeInt; overload;
  684. function WriteData(const Buffer: UInt16): NativeInt; overload;
  685. function WriteData(const Buffer: UInt16; Count: NativeInt): NativeInt; overload;
  686. function WriteData(const Buffer: Int32): NativeInt; overload;
  687. function WriteData(const Buffer: Int32; Count: NativeInt): NativeInt; overload;
  688. function WriteData(const Buffer: UInt32): NativeInt; overload;
  689. function WriteData(const Buffer: UInt32; Count: NativeInt): NativeInt; overload;
  690. // NativeLargeint. Stored as a float64, Read as float64.
  691. function WriteData(const Buffer: NativeLargeInt): NativeInt; overload;
  692. function WriteData(const Buffer: NativeLargeInt; Count: NativeInt): NativeInt; overload;
  693. function WriteData(const Buffer: NativeLargeUInt): NativeInt; overload;
  694. function WriteData(const Buffer: NativeLargeUInt; Count: NativeInt): NativeInt; overload;
  695. function WriteData(const Buffer: Double): NativeInt; overload;
  696. function WriteData(const Buffer: Double; Count: NativeInt): NativeInt; overload;
  697. {$IFDEF FPC_HAS_TYPE_EXTENDED}
  698. function WriteData(const Buffer: Extended): NativeInt; overload;
  699. function WriteData(const Buffer: Extended; Count: NativeInt): NativeInt; overload;
  700. function WriteData(const Buffer: TExtended80Rec): NativeInt; overload;
  701. function WriteData(const Buffer: TExtended80Rec; Count: NativeInt): NativeInt; overload;
  702. {$ENDIF}
  703. procedure WriteBufferData(Buffer: Int32); overload;
  704. procedure WriteBufferData(Buffer: Int32; Count: NativeInt); overload;
  705. procedure WriteBufferData(Buffer: Boolean); overload;
  706. procedure WriteBufferData(Buffer: Boolean; Count: NativeInt); overload;
  707. procedure WriteBufferData(Buffer: WideChar); overload;
  708. procedure WriteBufferData(Buffer: WideChar; Count: NativeInt); overload;
  709. procedure WriteBufferData(Buffer: Int8); overload;
  710. procedure WriteBufferData(Buffer: Int8; Count: NativeInt); overload;
  711. procedure WriteBufferData(Buffer: UInt8); overload;
  712. procedure WriteBufferData(Buffer: UInt8; Count: NativeInt); overload;
  713. procedure WriteBufferData(Buffer: Int16); overload;
  714. procedure WriteBufferData(Buffer: Int16; Count: NativeInt); overload;
  715. procedure WriteBufferData(Buffer: UInt16); overload;
  716. procedure WriteBufferData(Buffer: UInt16; Count: NativeInt); overload;
  717. procedure WriteBufferData(Buffer: UInt32); overload;
  718. procedure WriteBufferData(Buffer: UInt32; Count: NativeInt); overload;
  719. // NativeLargeint. Stored as a float64, Read as float64.
  720. procedure WriteBufferData(Buffer: NativeLargeInt); overload;
  721. procedure WriteBufferData(Buffer: NativeLargeInt; Count: NativeInt); overload;
  722. procedure WriteBufferData(Buffer: NativeLargeUInt); overload;
  723. procedure WriteBufferData(Buffer: NativeLargeUInt; Count: NativeInt); overload;
  724. procedure WriteBufferData(Buffer: Double); overload;
  725. procedure WriteBufferData(Buffer: Double; Count: NativeInt); overload;
  726. function CopyFrom(Source: TStream; Count: NativeInt): NativeInt;
  727. function ReadComponent(Instance: TComponent): TComponent;
  728. function ReadComponentRes(Instance: TComponent): TComponent;
  729. procedure WriteComponent(Instance: TComponent);
  730. procedure WriteComponentRes(const ResName: string; Instance: TComponent);
  731. procedure WriteDescendent(Instance, Ancestor: TComponent);
  732. procedure WriteDescendentRes(const ResName: string; Instance, Ancestor: TComponent);
  733. procedure WriteResourceHeader(const ResName: string; {!!!:out} var FixupInfo: Longint);
  734. procedure FixupResourceHeader(FixupInfo: Longint);
  735. procedure ReadResHeader;
  736. function ReadByte : Byte;
  737. function ReadWord : Word;
  738. function ReadDWord : Cardinal;
  739. function ReadQWord : NativeLargeUInt;
  740. procedure WriteByte(b : Byte);
  741. procedure WriteWord(w : Word);
  742. procedure WriteDWord(d : Cardinal);
  743. procedure WriteQWord(q : NativeLargeUInt);
  744. property Position: NativeInt read GetPosition write SetPosition;
  745. property Size: NativeInt read GetSize write SetSize64;
  746. Property Endian: TEndian Read FEndian Write FEndian;
  747. end;
  748. { TCustomMemoryStream abstract class }
  749. TCustomMemoryStream = class(TStream)
  750. private
  751. FMemory: TJSArrayBuffer;
  752. FDataView : TJSDataView;
  753. FDataArray : TJSUint8Array;
  754. FSize, FPosition: PtrInt;
  755. FSizeBoundsSeek : Boolean;
  756. function GetDataArray: TJSUint8Array;
  757. function GetDataView: TJSDataview;
  758. protected
  759. Function GetSize : NativeInt; Override;
  760. function GetPosition: NativeInt; Override;
  761. procedure SetPointer(Ptr: TJSArrayBuffer; ASize: PtrInt);
  762. Property DataView : TJSDataview Read GetDataView;
  763. Property DataArray : TJSUint8Array Read GetDataArray;
  764. public
  765. Class Function MemoryToBytes(Mem : TJSArrayBuffer) : TBytes; overload;
  766. Class Function MemoryToBytes(Mem : TJSUint8Array) : TBytes; overload;
  767. Class Function BytesToMemory(aBytes : TBytes) : TJSArrayBuffer;
  768. function Read(Buffer : TBytes; Offset, Count: LongInt): LongInt; override;
  769. function Seek(const Offset: NativeInt; Origin: TSeekOrigin): NativeInt; override;
  770. procedure SaveToStream(Stream: TStream);
  771. Procedure LoadFromURL(Const aURL : String; Async : Boolean = True; OnLoaded : TNotifyEventRef = Nil; OnError: TStringNotifyEventRef = Nil); virtual;
  772. // Delphi compatibility. Must be an URL
  773. Procedure LoadFromFile(Const aFileName : String; const OnLoaded : TProc = Nil; const AError: TProcString = Nil);
  774. property Memory: TJSArrayBuffer read FMemory;
  775. Property SizeBoundsSeek : Boolean Read FSizeBoundsSeek Write FSizeBoundsSeek;
  776. end;
  777. { TMemoryStream }
  778. TMemoryStream = class(TCustomMemoryStream)
  779. private
  780. FCapacity: PtrInt;
  781. procedure SetCapacity(NewCapacity: PtrInt);
  782. protected
  783. function Realloc(var NewCapacity: PtrInt): TJSArrayBuffer; virtual;
  784. property Capacity: PtrInt read FCapacity write SetCapacity;
  785. public
  786. destructor Destroy; override;
  787. procedure Clear;
  788. procedure LoadFromStream(Stream: TStream);
  789. procedure SetSize(const NewSize: NativeInt); override;
  790. function Write(const Buffer: TBytes; Offset, Count: LongInt): LongInt; override;
  791. end;
  792. { TBytesStream }
  793. TBytesStream = class(TMemoryStream)
  794. private
  795. function GetBytes: TBytes;
  796. public
  797. constructor Create(const ABytes: TBytes); virtual; overload;
  798. property Bytes: TBytes read GetBytes;
  799. end;
  800. { TStringStream }
  801. TStringStream = class(TMemoryStream)
  802. private
  803. function GetDataString : String;
  804. public
  805. constructor Create; reintroduce; overload;
  806. constructor Create(const aString: String); virtual; overload;
  807. function ReadString(Count: Integer): string;
  808. procedure WriteString(const AString: string);
  809. property DataString: String read GetDataString;
  810. end;
  811. TFilerFlag = (ffInherited, ffChildPos, ffInline);
  812. TFilerFlags = set of TFilerFlag;
  813. TReaderProc = procedure(Reader: TReader) of object;
  814. TWriterProc = procedure(Writer: TWriter) of object;
  815. TStreamProc = procedure(Stream: TStream) of object;
  816. TFiler = class(TObject)
  817. private
  818. FRoot: TComponent;
  819. FLookupRoot: TComponent;
  820. FAncestor: TPersistent;
  821. FIgnoreChildren: Boolean;
  822. protected
  823. procedure SetRoot(ARoot: TComponent); virtual;
  824. public
  825. procedure DefineProperty(const Name: string;
  826. ReadData: TReaderProc; WriteData: TWriterProc;
  827. HasData: Boolean); virtual; abstract;
  828. procedure DefineBinaryProperty(const Name: string;
  829. ReadData, WriteData: TStreamProc;
  830. HasData: Boolean); virtual; abstract;
  831. Procedure FlushBuffer; virtual; abstract;
  832. property Root: TComponent read FRoot write SetRoot;
  833. property LookupRoot: TComponent read FLookupRoot;
  834. property Ancestor: TPersistent read FAncestor write FAncestor;
  835. property IgnoreChildren: Boolean read FIgnoreChildren write FIgnoreChildren;
  836. end;
  837. TValueType = (
  838. vaNull, vaList, vaInt8, vaInt16, vaInt32, vaDouble,
  839. vaString, vaIdent, vaFalse, vaTrue, vaBinary, vaSet,
  840. vaNil, vaCollection, vaCurrency, vaDate, vaNativeInt
  841. );
  842. { TAbstractObjectReader }
  843. TAbstractObjectReader = class
  844. public
  845. Procedure FlushBuffer; virtual;
  846. function NextValue: TValueType; virtual; abstract;
  847. function ReadValue: TValueType; virtual; abstract;
  848. procedure BeginRootComponent; virtual; abstract;
  849. procedure BeginComponent(var Flags: TFilerFlags; var AChildPos: Integer;
  850. var CompClassName, CompName: String); virtual; abstract;
  851. function BeginProperty: String; virtual; abstract;
  852. //Please don't use read, better use ReadBinary whenever possible
  853. procedure Read(var Buffer : TBytes; Count: Longint); virtual;abstract;
  854. { All ReadXXX methods are called _after_ the value type has been read! }
  855. procedure ReadBinary(const DestData: TMemoryStream); virtual; abstract;
  856. function ReadFloat: Extended; virtual; abstract;
  857. function ReadCurrency: Currency; virtual; abstract;
  858. function ReadIdent(ValueType: TValueType): String; virtual; abstract;
  859. function ReadInt8: ShortInt; virtual; abstract;
  860. function ReadInt16: SmallInt; virtual; abstract;
  861. function ReadInt32: LongInt; virtual; abstract;
  862. function ReadNativeInt: NativeInt; virtual; abstract;
  863. function ReadSet(EnumType: TTypeInfoEnum): Integer; virtual; abstract;
  864. procedure ReadSignature; virtual; abstract;
  865. function ReadStr: String; virtual; abstract;
  866. function ReadString(StringType: TValueType): String; virtual; abstract;
  867. function ReadWideString: WideString;virtual;abstract;
  868. function ReadUnicodeString: UnicodeString;virtual;abstract;
  869. procedure SkipComponent(SkipComponentInfos: Boolean); virtual; abstract;
  870. procedure SkipValue; virtual; abstract;
  871. end;
  872. { TBinaryObjectReader }
  873. TBinaryObjectReader = class(TAbstractObjectReader)
  874. protected
  875. FStream: TStream;
  876. function ReadWord : word;
  877. function ReadDWord : longword;
  878. procedure SkipProperty;
  879. procedure SkipSetBody;
  880. public
  881. constructor Create(Stream: TStream);
  882. function NextValue: TValueType; override;
  883. function ReadValue: TValueType; override;
  884. procedure BeginRootComponent; override;
  885. procedure BeginComponent(var Flags: TFilerFlags; var AChildPos: Integer;
  886. var CompClassName, CompName: String); override;
  887. function BeginProperty: String; override;
  888. //Please don't use read, better use ReadBinary whenever possible
  889. procedure Read(var Buffer : TBytes; Count: Longint); override;
  890. procedure ReadBinary(const DestData: TMemoryStream); override;
  891. function ReadFloat: Extended; override;
  892. function ReadCurrency: Currency; override;
  893. function ReadIdent(ValueType: TValueType): String; override;
  894. function ReadInt8: ShortInt; override;
  895. function ReadInt16: SmallInt; override;
  896. function ReadInt32: LongInt; override;
  897. function ReadNativeInt: NativeInt; override;
  898. function ReadSet(EnumType: TTypeInfoEnum): Integer; override;
  899. procedure ReadSignature; override;
  900. function ReadStr: String; override;
  901. function ReadString(StringType: TValueType): String; override;
  902. function ReadWideString: WideString;override;
  903. function ReadUnicodeString: UnicodeString;override;
  904. procedure SkipComponent(SkipComponentInfos: Boolean); override;
  905. procedure SkipValue; override;
  906. end;
  907. TFindMethodEvent = procedure(Reader: TReader; const MethodName: string; var Address: CodePointer; var Error: Boolean) of object;
  908. TSetNameEvent = procedure(Reader: TReader; Component: TComponent; var Name: string) of object;
  909. TReferenceNameEvent = procedure(Reader: TReader; var Name: string) of object;
  910. TAncestorNotFoundEvent = procedure(Reader: TReader; const ComponentName: string; ComponentClass: TPersistentClass; var Component: TComponent) of object;
  911. TReadComponentsProc = procedure(Component: TComponent) of object;
  912. TReaderError = procedure(Reader: TReader; const Message: string; var Handled: Boolean) of object;
  913. TPropertyNotFoundEvent = procedure(Reader: TReader; Instance: TPersistent; var PropName: string; IsPath: boolean; var Handled, Skip: Boolean) of object;
  914. TFindComponentClassEvent = procedure(Reader: TReader; const ClassName: string; var ComponentClass: TComponentClass) of object;
  915. TCreateComponentEvent = procedure(Reader: TReader; ComponentClass: TComponentClass; var Component: TComponent) of object;
  916. TSetMethodPropertyEvent = procedure(Reader: TReader; Instance: TPersistent; PropInfo: TTypeMemberProperty; const TheMethodName: string;
  917. var Handled: boolean) of object;
  918. TReadWriteStringPropertyEvent = procedure(Sender:TObject; const Instance: TPersistent; PropInfo: TTypeMemberProperty; var Content:string) of object;
  919. { TReader }
  920. TReader = class(TFiler)
  921. private
  922. FDriver: TAbstractObjectReader;
  923. FOwner: TComponent;
  924. FParent: TComponent;
  925. FFixups: TObject;
  926. FLoaded: TFpList;
  927. FOnFindMethod: TFindMethodEvent;
  928. FOnSetMethodProperty: TSetMethodPropertyEvent;
  929. FOnSetName: TSetNameEvent;
  930. FOnReferenceName: TReferenceNameEvent;
  931. FOnAncestorNotFound: TAncestorNotFoundEvent;
  932. FOnError: TReaderError;
  933. FOnPropertyNotFound: TPropertyNotFoundEvent;
  934. FOnFindComponentClass: TFindComponentClassEvent;
  935. FOnCreateComponent: TCreateComponentEvent;
  936. FPropName: string;
  937. FCanHandleExcepts: Boolean;
  938. FOnReadStringProperty:TReadWriteStringPropertyEvent;
  939. procedure DoFixupReferences;
  940. function FindComponentClass(const AClassName: string): TComponentClass;
  941. protected
  942. function Error(const Message: string): Boolean; virtual;
  943. function FindMethod(ARoot: TComponent; const AMethodName: string): CodePointer; virtual;
  944. procedure ReadProperty(AInstance: TPersistent);
  945. procedure ReadPropValue(Instance: TPersistent; PropInfo: TTypeMemberProperty);
  946. procedure PropertyError;
  947. procedure ReadData(Instance: TComponent);
  948. property PropName: string read FPropName;
  949. property CanHandleExceptions: Boolean read FCanHandleExcepts;
  950. function CreateDriver(Stream: TStream): TAbstractObjectReader; virtual;
  951. public
  952. constructor Create(Stream: TStream);
  953. destructor Destroy; override;
  954. Procedure FlushBuffer; override;
  955. procedure BeginReferences;
  956. procedure CheckValue(Value: TValueType);
  957. procedure DefineProperty(const Name: string;
  958. AReadData: TReaderProc; WriteData: TWriterProc;
  959. HasData: Boolean); override;
  960. procedure DefineBinaryProperty(const Name: string;
  961. AReadData, WriteData: TStreamProc;
  962. HasData: Boolean); override;
  963. function EndOfList: Boolean;
  964. procedure EndReferences;
  965. procedure FixupReferences;
  966. function NextValue: TValueType;
  967. //Please don't use read, better use ReadBinary whenever possible
  968. //uuups, ReadBinary is protected ..
  969. procedure Read(var Buffer : TBytes; Count: LongInt); virtual;
  970. function ReadBoolean: Boolean;
  971. function ReadChar: Char;
  972. function ReadWideChar: WideChar;
  973. function ReadUnicodeChar: UnicodeChar;
  974. procedure ReadCollection(Collection: TCollection);
  975. function ReadComponent(Component: TComponent): TComponent;
  976. procedure ReadComponents(AOwner, AParent: TComponent;
  977. Proc: TReadComponentsProc);
  978. function ReadFloat: Extended;
  979. function ReadCurrency: Currency;
  980. function ReadIdent: string;
  981. function ReadInteger: Longint;
  982. function ReadNativeInt: NativeInt;
  983. function ReadSet(EnumType: Pointer): Integer;
  984. procedure ReadListBegin;
  985. procedure ReadListEnd;
  986. function ReadRootComponent(ARoot: TComponent): TComponent;
  987. function ReadVariant: JSValue;
  988. procedure ReadSignature;
  989. function ReadString: string;
  990. function ReadWideString: WideString;
  991. function ReadUnicodeString: UnicodeString;
  992. function ReadValue: TValueType;
  993. procedure CopyValue(Writer: TWriter);
  994. property Driver: TAbstractObjectReader read FDriver;
  995. property Owner: TComponent read FOwner write FOwner;
  996. property Parent: TComponent read FParent write FParent;
  997. property OnError: TReaderError read FOnError write FOnError;
  998. property OnPropertyNotFound: TPropertyNotFoundEvent read FOnPropertyNotFound write FOnPropertyNotFound;
  999. property OnFindMethod: TFindMethodEvent read FOnFindMethod write FOnFindMethod;
  1000. property OnSetMethodProperty: TSetMethodPropertyEvent read FOnSetMethodProperty write FOnSetMethodProperty;
  1001. property OnSetName: TSetNameEvent read FOnSetName write FOnSetName;
  1002. property OnReferenceName: TReferenceNameEvent read FOnReferenceName write FOnReferenceName;
  1003. property OnAncestorNotFound: TAncestorNotFoundEvent read FOnAncestorNotFound write FOnAncestorNotFound;
  1004. property OnCreateComponent: TCreateComponentEvent read FOnCreateComponent write FOnCreateComponent;
  1005. property OnFindComponentClass: TFindComponentClassEvent read FOnFindComponentClass write FOnFindComponentClass;
  1006. property OnReadStringProperty: TReadWriteStringPropertyEvent read FOnReadStringProperty write FOnReadStringProperty;
  1007. end;
  1008. { TAbstractObjectWriter }
  1009. TAbstractObjectWriter = class
  1010. public
  1011. { Begin/End markers. Those ones who don't have an end indicator, use
  1012. "EndList", after the occurrence named in the comment. Note that this
  1013. only counts for "EndList" calls on the same level; each BeginXXX call
  1014. increases the current level. }
  1015. procedure BeginCollection; virtual; abstract; { Ends with the next "EndList" }
  1016. procedure BeginComponent(Component: TComponent; Flags: TFilerFlags;
  1017. ChildPos: Integer); virtual; abstract; { Ends after the second "EndList" }
  1018. procedure WriteSignature; virtual; abstract;
  1019. procedure BeginList; virtual; abstract;
  1020. procedure EndList; virtual; abstract;
  1021. procedure BeginProperty(const PropName: String); virtual; abstract;
  1022. procedure EndProperty; virtual; abstract;
  1023. //Please don't use write, better use WriteBinary whenever possible
  1024. procedure Write(const Buffer : TBytes; Count: Longint); virtual;abstract;
  1025. Procedure FlushBuffer; virtual; abstract;
  1026. procedure WriteBinary(const Buffer : TBytes; Count: Longint); virtual; abstract;
  1027. procedure WriteBoolean(Value: Boolean); virtual; abstract;
  1028. // procedure WriteChar(Value: Char);
  1029. procedure WriteFloat(const Value: Extended); virtual; abstract;
  1030. procedure WriteCurrency(const Value: Currency); virtual; abstract;
  1031. procedure WriteIdent(const Ident: string); virtual; abstract;
  1032. procedure WriteInteger(Value: NativeInt); virtual; abstract;
  1033. procedure WriteNativeInt(Value: NativeInt); virtual; abstract;
  1034. procedure WriteVariant(const Value: JSValue); virtual; abstract;
  1035. procedure WriteMethodName(const Name: String); virtual; abstract;
  1036. procedure WriteSet(Value: LongInt; SetType: Pointer); virtual; abstract;
  1037. procedure WriteString(const Value: String); virtual; abstract;
  1038. procedure WriteWideString(const Value: WideString);virtual;abstract;
  1039. procedure WriteUnicodeString(const Value: UnicodeString);virtual;abstract;
  1040. end;
  1041. { TBinaryObjectWriter }
  1042. TBinaryObjectWriter = class(TAbstractObjectWriter)
  1043. protected
  1044. FStream: TStream;
  1045. FBuffer: Pointer;
  1046. FBufSize: Integer;
  1047. FBufPos: Integer;
  1048. FBufEnd: Integer;
  1049. procedure WriteWord(w : word);
  1050. procedure WriteDWord(lw : longword);
  1051. procedure WriteValue(Value: TValueType);
  1052. public
  1053. constructor Create(Stream: TStream);
  1054. procedure WriteSignature; override;
  1055. procedure BeginCollection; override;
  1056. procedure BeginComponent(Component: TComponent; Flags: TFilerFlags;
  1057. ChildPos: Integer); override;
  1058. procedure BeginList; override;
  1059. procedure EndList; override;
  1060. procedure BeginProperty(const PropName: String); override;
  1061. procedure EndProperty; override;
  1062. Procedure FlushBuffer; override;
  1063. //Please don't use write, better use WriteBinary whenever possible
  1064. procedure Write(const Buffer : TBytes; Count: Longint); override;
  1065. procedure WriteBinary(const Buffer : TBytes; Count: LongInt); override;
  1066. procedure WriteBoolean(Value: Boolean); override;
  1067. procedure WriteFloat(const Value: Extended); override;
  1068. procedure WriteCurrency(const Value: Currency); override;
  1069. procedure WriteIdent(const Ident: string); override;
  1070. procedure WriteInteger(Value: NativeInt); override;
  1071. procedure WriteNativeInt(Value: NativeInt); override;
  1072. procedure WriteMethodName(const Name: String); override;
  1073. procedure WriteSet(Value: LongInt; SetType: Pointer); override;
  1074. procedure WriteStr(const Value: String);
  1075. procedure WriteString(const Value: String); override;
  1076. procedure WriteWideString(const Value: WideString); override;
  1077. procedure WriteUnicodeString(const Value: UnicodeString); override;
  1078. procedure WriteVariant(const VarValue: JSValue);override;
  1079. end;
  1080. TFindAncestorEvent = procedure (Writer: TWriter; Component: TComponent;
  1081. const Name: string; var Ancestor, RootAncestor: TComponent) of object;
  1082. TWriteMethodPropertyEvent = procedure (Writer: TWriter; Instance: TPersistent;
  1083. PropInfo: TTypeMemberProperty;
  1084. const MethodValue, DefMethodValue: TMethod;
  1085. var Handled: boolean) of object;
  1086. { TWriter }
  1087. TWriter = class(TFiler)
  1088. private
  1089. FDriver: TAbstractObjectWriter;
  1090. FDestroyDriver: Boolean;
  1091. FRootAncestor: TComponent;
  1092. FPropPath: String;
  1093. FAncestors: TStringList;
  1094. FAncestorPos: Integer;
  1095. FCurrentPos: Integer;
  1096. FOnFindAncestor: TFindAncestorEvent;
  1097. FOnWriteMethodProperty: TWriteMethodPropertyEvent;
  1098. FOnWriteStringProperty:TReadWriteStringPropertyEvent;
  1099. procedure AddToAncestorList(Component: TComponent);
  1100. procedure WriteComponentData(Instance: TComponent);
  1101. Procedure DetermineAncestor(Component: TComponent);
  1102. procedure DoFindAncestor(Component : TComponent);
  1103. protected
  1104. procedure SetRoot(ARoot: TComponent); override;
  1105. procedure WriteBinary(AWriteData: TStreamProc);
  1106. procedure WriteProperty(Instance: TPersistent; PropInfo: TTypeMemberProperty);
  1107. procedure WriteProperties(Instance: TPersistent);
  1108. procedure WriteChildren(Component: TComponent);
  1109. function CreateDriver(Stream: TStream): TAbstractObjectWriter; virtual;
  1110. public
  1111. constructor Create(ADriver: TAbstractObjectWriter);
  1112. constructor Create(Stream: TStream);
  1113. destructor Destroy; override;
  1114. procedure DefineProperty(const Name: string;
  1115. ReadData: TReaderProc; AWriteData: TWriterProc;
  1116. HasData: Boolean); override;
  1117. procedure DefineBinaryProperty(const Name: string;
  1118. ReadData, AWriteData: TStreamProc;
  1119. HasData: Boolean); override;
  1120. Procedure FlushBuffer; override;
  1121. procedure Write(const Buffer : TBytes; Count: Longint); virtual;
  1122. procedure WriteBoolean(Value: Boolean);
  1123. procedure WriteCollection(Value: TCollection);
  1124. procedure WriteComponent(Component: TComponent);
  1125. procedure WriteChar(Value: Char);
  1126. procedure WriteWideChar(Value: WideChar);
  1127. procedure WriteDescendent(ARoot: TComponent; AAncestor: TComponent);
  1128. procedure WriteFloat(const Value: Extended);
  1129. procedure WriteCurrency(const Value: Currency);
  1130. procedure WriteIdent(const Ident: string);
  1131. procedure WriteInteger(Value: Longint); overload;
  1132. procedure WriteInteger(Value: NativeInt); overload;
  1133. procedure WriteSet(Value: LongInt; SetType: Pointer);
  1134. procedure WriteListBegin;
  1135. procedure WriteListEnd;
  1136. Procedure WriteSignature;
  1137. procedure WriteRootComponent(ARoot: TComponent);
  1138. procedure WriteString(const Value: string);
  1139. procedure WriteWideString(const Value: WideString);
  1140. procedure WriteUnicodeString(const Value: UnicodeString);
  1141. procedure WriteVariant(const VarValue: JSValue);
  1142. property RootAncestor: TComponent read FRootAncestor write FRootAncestor;
  1143. property OnFindAncestor: TFindAncestorEvent read FOnFindAncestor write FOnFindAncestor;
  1144. property OnWriteMethodProperty: TWriteMethodPropertyEvent read FOnWriteMethodProperty write FOnWriteMethodProperty;
  1145. property OnWriteStringProperty: TReadWriteStringPropertyEvent read FOnWriteStringProperty write FOnWriteStringProperty;
  1146. property Driver: TAbstractObjectWriter read FDriver;
  1147. property PropertyPath: string read FPropPath;
  1148. end;
  1149. TParserToken = (toUnknown, // everything else
  1150. toEOF, // EOF
  1151. toSymbol, // Symbol (identifier)
  1152. toString, // ''string''
  1153. toInteger, // 123
  1154. toFloat, // 12.3
  1155. toMinus, // -
  1156. toSetStart, // [
  1157. toListStart, // (
  1158. toCollectionStart, // <
  1159. toBinaryStart, // {
  1160. toSetEnd, // ]
  1161. toListEnd, // )
  1162. toCollectionEnd, // >
  1163. toBinaryEnd, // }
  1164. toComma, // ,
  1165. toDot, // .
  1166. toEqual, // =
  1167. toColon, // :
  1168. toPlus // +
  1169. );
  1170. TParser = class(TObject)
  1171. private
  1172. fStream : TStream;
  1173. fBuf : Array of Char;
  1174. FBufLen : integer;
  1175. fPos : integer;
  1176. fDeltaPos : integer;
  1177. fFloatType : char;
  1178. fSourceLine : integer;
  1179. fToken : TParserToken;
  1180. fEofReached : boolean;
  1181. fLastTokenStr : string;
  1182. function GetTokenName(aTok : TParserToken) : string;
  1183. procedure LoadBuffer;
  1184. procedure CheckLoadBuffer; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  1185. procedure ProcessChar; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  1186. function IsNumber : boolean; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  1187. function IsHexNum : boolean; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  1188. function IsAlpha : boolean; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  1189. function IsAlphaNum : boolean; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  1190. function GetHexValue(c : char) : byte; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  1191. function GetAlphaNum : string;
  1192. procedure HandleNewLine;
  1193. procedure SkipBOM;
  1194. procedure SkipSpaces;
  1195. procedure SkipWhitespace;
  1196. procedure HandleEof;
  1197. procedure HandleAlphaNum;
  1198. procedure HandleNumber;
  1199. procedure HandleHexNumber;
  1200. function HandleQuotedString : string;
  1201. Function HandleDecimalCharacter: char;
  1202. procedure HandleString;
  1203. procedure HandleMinus;
  1204. procedure HandleUnknown;
  1205. procedure GotoToNextChar;
  1206. public
  1207. // Input stream is expected to be UTF16 !
  1208. constructor Create(Stream: TStream);
  1209. destructor Destroy; override;
  1210. procedure CheckToken(T: TParserToken);
  1211. procedure CheckTokenSymbol(const S: string);
  1212. procedure Error(const Ident: string);
  1213. procedure ErrorFmt(const Ident: string; const Args: array of const);
  1214. procedure ErrorStr(const Message: string);
  1215. procedure HexToBinary(Stream: TStream);
  1216. function NextToken: TParserToken;
  1217. function SourcePos: Longint;
  1218. function TokenComponentIdent: string;
  1219. function TokenFloat: Double;
  1220. function TokenInt: NativeInt;
  1221. function TokenString: string;
  1222. function TokenSymbolIs(const S: string): Boolean;
  1223. property FloatType: Char read fFloatType;
  1224. property SourceLine: Integer read fSourceLine;
  1225. property Token: TParserToken read fToken;
  1226. end;
  1227. { TObjectStreamConverter }
  1228. TObjectTextEncoding = (oteDFM,oteLFM);
  1229. TObjectStreamConverter = Class
  1230. private
  1231. FIndent: String;
  1232. FInput : TStream;
  1233. FOutput : TStream;
  1234. FEncoding : TObjectTextEncoding;
  1235. Private
  1236. FPlainStrings: Boolean;
  1237. // Low level writing
  1238. procedure Outchars(S : String); virtual;
  1239. procedure OutLn(s: String); virtual;
  1240. procedure OutStr(s: String); virtual;
  1241. procedure OutString(s: String); virtual;
  1242. // Low level reading
  1243. function ReadWord: word;
  1244. function ReadDWord: longword;
  1245. function ReadDouble: Double;
  1246. function ReadInt(ValueType: TValueType): NativeInt;
  1247. function ReadInt: NativeInt;
  1248. function ReadNativeInt: NativeInt;
  1249. function ReadStr: String;
  1250. function ReadString(StringType: TValueType): String; virtual;
  1251. // High-level
  1252. procedure ProcessBinary; virtual;
  1253. procedure ProcessValue(ValueType: TValueType; Indent: String); virtual;
  1254. procedure ReadObject(indent: String); virtual;
  1255. procedure ReadPropList(indent: String); virtual;
  1256. Public
  1257. procedure ObjectBinaryToText(aInput, aOutput: TStream);
  1258. procedure ObjectBinaryToText(aInput, aOutput: TStream; aEncoding: TObjectTextEncoding);
  1259. Procedure Execute;
  1260. // use this to get previous streaming behavour: strings written as-is
  1261. Property PlainStrings : Boolean Read FPlainStrings Write FPlainStrings;
  1262. Property Input : TStream Read FInput Write FInput;
  1263. Property Output : TStream Read Foutput Write FOutput;
  1264. Property Encoding : TObjectTextEncoding Read FEncoding Write FEncoding;
  1265. Property Indent : String Read FIndent Write Findent;
  1266. end;
  1267. { TObjectTextConverter }
  1268. TObjectTextConverter = Class
  1269. private
  1270. FParser: TParser;
  1271. private
  1272. FInput: TStream;
  1273. Foutput: TStream;
  1274. procedure WriteDouble(e: double);
  1275. procedure WriteDWord(lw: longword);
  1276. procedure WriteInteger(value: nativeInt);
  1277. //procedure WriteLString(const s: String);
  1278. procedure WriteQWord(q: nativeint);
  1279. procedure WriteString(s: String);
  1280. procedure WriteWord(w: word);
  1281. procedure WriteWString(const s: WideString);
  1282. procedure ProcessObject; virtual;
  1283. procedure ProcessProperty; virtual;
  1284. procedure ProcessValue; virtual;
  1285. procedure ProcessWideString(const left: string);
  1286. Property Parser : TParser Read FParser;
  1287. Public
  1288. // Input stream must be UTF16 !
  1289. procedure ObjectTextToBinary(aInput, aOutput: TStream);
  1290. Procedure Execute; virtual;
  1291. Property Input : TStream Read FInput Write FInput;
  1292. Property Output: TStream Read Foutput Write Foutput;
  1293. end;
  1294. TLoadHelper = Class (TObject)
  1295. Public
  1296. Type
  1297. TTextLoadedCallBack = reference to procedure (const aText : String);
  1298. TBytesLoadedCallBack = reference to procedure (const aBuffer : TJSArrayBuffer);
  1299. TErrorCallBack = reference to procedure (const aError : String);
  1300. Class Procedure LoadText(aURL : String; aSync : Boolean; OnLoaded : TTextLoadedCallBack; OnError : TErrorCallBack); virtual; abstract;
  1301. Class Procedure LoadBytes(aURL : String; aSync : Boolean; OnLoaded : TBytesLoadedCallBack; OnError : TErrorCallBack); virtual; abstract;
  1302. end;
  1303. TLoadHelperClass = Class of TLoadHelper;
  1304. type
  1305. TIdentMapEntry = record
  1306. Value: Integer;
  1307. Name: String;
  1308. end;
  1309. TIdentToInt = function(const Ident: string; var Int: Longint): Boolean;
  1310. TIntToIdent = function(Int: Longint; var Ident: string): Boolean;
  1311. TFindGlobalComponent = function(const Name: string): TComponent;
  1312. TInitComponentHandler = function(Instance: TComponent; RootAncestor : TClass): boolean;
  1313. procedure RegisterInitComponentHandler(ComponentClass: TComponentClass; Handler: TInitComponentHandler);
  1314. Procedure RegisterClass(AClass : TPersistentClass);
  1315. Procedure RegisterClasses(AClasses : specialize TArray<TPersistentClass>);
  1316. Function GetClass(AClassName : string) : TPersistentClass;
  1317. procedure RegisterFindGlobalComponentProc(AFindGlobalComponent: TFindGlobalComponent);
  1318. procedure UnregisterFindGlobalComponentProc(AFindGlobalComponent: TFindGlobalComponent);
  1319. function FindGlobalComponent(const Name: string): TComponent;
  1320. Function FindNestedComponent(Root : TComponent; APath : String; CStyle : Boolean = True) : TComponent;
  1321. procedure RedirectFixupReferences(Root: TComponent; const OldRootName, NewRootName: string);
  1322. procedure RemoveFixupReferences(Root: TComponent; const RootName: string);
  1323. procedure RegisterIntegerConsts(IntegerType: Pointer; IdentToIntFn: TIdentToInt; IntToIdentFn: TIntToIdent);
  1324. function ExtractStrings(Separators, WhiteSpace: TSysCharSet; Content: String; Strings: TStrings; AddEmptyStrings : Boolean = False): Integer;
  1325. function IdentToInt(const Ident: string; out Int: Longint; const Map: array of TIdentMapEntry): Boolean;
  1326. function IntToIdent(Int: Longint; var Ident: string; const Map: array of TIdentMapEntry): Boolean;
  1327. function FindIntToIdent(AIntegerType: Pointer): TIntToIdent;
  1328. function FindIdentToInt(AIntegerType: Pointer): TIdentToInt;
  1329. function FindClass(const AClassName: string): TPersistentClass;
  1330. function CollectionsEqual(C1, C2: TCollection): Boolean;
  1331. function CollectionsEqual(C1, C2: TCollection; Owner1, Owner2: TComponent): Boolean;
  1332. procedure GetFixupReferenceNames(Root: TComponent; Names: TStrings);
  1333. procedure GetFixupInstanceNames(Root: TComponent; const ReferenceRootName: string; Names: TStrings);
  1334. procedure ObjectBinaryToText(aInput, aOutput: TStream);
  1335. procedure ObjectBinaryToText(aInput, aOutput: TStream; aEncoding: TObjectTextEncoding);
  1336. procedure ObjectTextToBinary(aInput, aOutput: TStream);
  1337. Function SetLoadHelperClass(aClass : TLoadHelperClass) : TLoadHelperClass;
  1338. // Create buffer from string. aLen in bytes, not in characters
  1339. Function StringToBuffer(aString : String; aLen : Integer) : TJSArrayBuffer;
  1340. // Create buffer from string. aPos,aLen are in bytes, not in characters.
  1341. Function BufferToString(aBuffer : TJSArrayBuffer; aPos,aLen : Integer) : String;
  1342. Const
  1343. // Some aliases
  1344. vaSingle = vaDouble;
  1345. vaExtended = vaDouble;
  1346. vaLString = vaString;
  1347. vaUTF8String = vaString;
  1348. vaUString = vaString;
  1349. vaWString = vaString;
  1350. vaQWord = vaNativeInt;
  1351. vaInt64 = vaNativeInt;
  1352. toWString = toString;
  1353. implementation
  1354. uses simplelinkedlist;
  1355. var
  1356. GlobalLoaded,
  1357. IntConstList: TFPList;
  1358. GlobalLoadHelper : TLoadHelperClass;
  1359. Function SetLoadHelperClass(aClass : TLoadHelperClass) : TLoadHelperClass;
  1360. begin
  1361. Result:=GlobalLoadHelper;
  1362. GlobalLoadHelper:=aClass;
  1363. end;
  1364. Procedure CheckLoadHelper;
  1365. begin
  1366. If (GlobalLoadHelper=Nil) then
  1367. Raise EInOutError.Create('No support for loading URLS. Include Rtl.BrowserLoadHelper in your project uses clause');
  1368. end;
  1369. Function StringToBuffer(aString : String; aLen : Integer) : TJSArrayBuffer;
  1370. var
  1371. I : Integer;
  1372. begin
  1373. Result:=TJSArrayBuffer.new(aLen*2);// 2 bytes for each char
  1374. With TJSUint16Array.new(Result) do
  1375. for i:=0 to aLen-1 do
  1376. values[i] := TJSString(aString).charCodeAt(i);
  1377. end;
  1378. function BufferToString(aBuffer: TJSArrayBuffer; aPos, aLen: Integer): String;
  1379. var
  1380. a : TJSUint16Array;
  1381. begin
  1382. Result:=''; // Silence warning
  1383. a:=TJSUint16Array.New(aBuffer.slice(aPos,aLen));
  1384. if a<>nil then
  1385. Result:=String(TJSFunction(@TJSString.fromCharCode).apply(nil,TJSValueDynArray(JSValue(a))));
  1386. end;
  1387. type
  1388. TIntConst = class
  1389. Private
  1390. IntegerType: PTypeInfo; // The integer type RTTI pointer
  1391. IdentToIntFn: TIdentToInt; // Identifier to Integer conversion
  1392. IntToIdentFn: TIntToIdent; // Integer to Identifier conversion
  1393. Public
  1394. constructor Create(AIntegerType: PTypeInfo; AIdentToInt: TIdentToInt;
  1395. AIntToIdent: TIntToIdent);
  1396. end;
  1397. { TStringStream }
  1398. function TStringStream.GetDataString: String;
  1399. var
  1400. a : TJSUint16Array;
  1401. begin
  1402. Result:=''; // Silence warning
  1403. a:=TJSUint16Array.New(Memory.slice(0,Size));
  1404. if a<>nil then
  1405. asm
  1406. // Result=String.fromCharCode.apply(null, new Uint16Array(a));
  1407. Result=String.fromCharCode.apply(null, a);
  1408. end;
  1409. end;
  1410. constructor TStringStream.Create;
  1411. begin
  1412. Create('');
  1413. end;
  1414. constructor TStringStream.Create(const aString: String);
  1415. var
  1416. Len : Integer;
  1417. begin
  1418. inherited Create;
  1419. Len:=Length(aString);
  1420. SetPointer(StringToBuffer(aString,Len),Len*2);
  1421. FCapacity:=Len*2;
  1422. end;
  1423. function TStringStream.ReadString(Count: Integer): string;
  1424. Var
  1425. B : TBytes;
  1426. Buf : TJSArrayBuffer;
  1427. BytesLeft : Integer;
  1428. ByteCount : Integer;
  1429. begin
  1430. // Top off
  1431. ByteCount:=Count*2; // UTF-16
  1432. BytesLeft:=(Size-Position);
  1433. if BytesLeft<ByteCount then
  1434. ByteCount:=BytesLeft;
  1435. SetLength(B,ByteCount);
  1436. ReadBuffer(B,0,ByteCount);
  1437. Buf:=BytesToMemory(B);
  1438. Result:=BufferToString(Buf,0,ByteCount);
  1439. end;
  1440. procedure TStringStream.WriteString(const AString: string);
  1441. Var
  1442. Buf : TJSArrayBuffer;
  1443. B : TBytes;
  1444. begin
  1445. Buf:=StringToBuffer(aString,Length(aString));
  1446. B:=MemoryToBytes(Buf);
  1447. WriteBuffer(B,Length(B));
  1448. end;
  1449. constructor TIntConst.Create(AIntegerType: PTypeInfo; AIdentToInt: TIdentToInt;
  1450. AIntToIdent: TIntToIdent);
  1451. begin
  1452. IntegerType := AIntegerType;
  1453. IdentToIntFn := AIdentToInt;
  1454. IntToIdentFn := AIntToIdent;
  1455. end;
  1456. procedure RegisterIntegerConsts(IntegerType: Pointer; IdentToIntFn: TIdentToInt;
  1457. IntToIdentFn: TIntToIdent);
  1458. begin
  1459. if Not Assigned(IntConstList) then
  1460. IntConstList:=TFPList.Create;
  1461. IntConstList.Add(TIntConst.Create(IntegerType, IdentToIntFn, IntToIdentFn));
  1462. end;
  1463. function ExtractStrings(Separators, WhiteSpace: TSysCharSet; Content: String; Strings: TStrings; AddEmptyStrings : Boolean = False): Integer;
  1464. var
  1465. b,c : integer;
  1466. procedure SkipWhitespace;
  1467. begin
  1468. while (Content[c] in Whitespace) do
  1469. inc (C);
  1470. end;
  1471. procedure AddString;
  1472. var
  1473. l : integer;
  1474. begin
  1475. l := c-b;
  1476. if (l > 0) or AddEmptyStrings then
  1477. begin
  1478. if assigned(Strings) then
  1479. begin
  1480. if l>0 then
  1481. Strings.Add (Copy(Content,B,L))
  1482. else
  1483. Strings.Add('');
  1484. end;
  1485. inc (result);
  1486. end;
  1487. end;
  1488. var
  1489. cc,quoted : char;
  1490. aLen : Integer;
  1491. begin
  1492. result := 0;
  1493. c := 1;
  1494. Quoted := #0;
  1495. Separators := Separators + [#13, #10] - ['''','"'];
  1496. SkipWhitespace;
  1497. b := c;
  1498. aLen:=Length(Content);
  1499. while C<=aLen do
  1500. begin
  1501. CC:=Content[c];
  1502. if (CC = Quoted) then
  1503. begin
  1504. if (C<aLen) and (Content[C+1] = Quoted) then
  1505. inc (c)
  1506. else
  1507. Quoted := #0
  1508. end
  1509. else if (Quoted = #0) and (CC in ['''','"']) then
  1510. Quoted := CC;
  1511. if (Quoted = #0) and (CC in Separators) then
  1512. begin
  1513. AddString;
  1514. inc (c);
  1515. SkipWhitespace;
  1516. b := c;
  1517. end
  1518. else
  1519. inc (c);
  1520. end;
  1521. if (c <> b) then
  1522. AddString;
  1523. end;
  1524. function FindIntToIdent(AIntegerType: Pointer): TIntToIdent;
  1525. var
  1526. i: Integer;
  1527. begin
  1528. Result := nil;
  1529. if Not Assigned(IntConstList) then
  1530. exit;
  1531. with IntConstList do
  1532. for i := 0 to Count - 1 do
  1533. if TIntConst(Items[i]).IntegerType = AIntegerType then
  1534. exit(TIntConst(Items[i]).IntToIdentFn);
  1535. end;
  1536. function FindIdentToInt(AIntegerType: Pointer): TIdentToInt;
  1537. var
  1538. i: Integer;
  1539. begin
  1540. Result := nil;
  1541. if Not Assigned(IntConstList) then
  1542. exit;
  1543. with IntConstList do
  1544. for i := 0 to Count - 1 do
  1545. with TIntConst(Items[I]) do
  1546. if TIntConst(Items[I]).IntegerType = AIntegerType then
  1547. exit(IdentToIntFn);
  1548. end;
  1549. function IdentToInt(const Ident: String; out Int: LongInt;
  1550. const Map: array of TIdentMapEntry): Boolean;
  1551. var
  1552. i: Integer;
  1553. begin
  1554. for i := Low(Map) to High(Map) do
  1555. if CompareText(Map[i].Name, Ident) = 0 then
  1556. begin
  1557. Int := Map[i].Value;
  1558. exit(True);
  1559. end;
  1560. Result := False;
  1561. end;
  1562. function IntToIdent(Int: LongInt; var Ident: String;
  1563. const Map: array of TIdentMapEntry): Boolean;
  1564. var
  1565. i: Integer;
  1566. begin
  1567. for i := Low(Map) to High(Map) do
  1568. if Map[i].Value = Int then
  1569. begin
  1570. Ident := Map[i].Name;
  1571. exit(True);
  1572. end;
  1573. Result := False;
  1574. end;
  1575. function GlobalIdentToInt(const Ident: String; var Int: LongInt):boolean;
  1576. var
  1577. i : Integer;
  1578. begin
  1579. Result := false;
  1580. if Not Assigned(IntConstList) then
  1581. exit;
  1582. with IntConstList do
  1583. for i := 0 to Count - 1 do
  1584. if TIntConst(Items[I]).IdentToIntFn(Ident, Int) then
  1585. Exit(True);
  1586. end;
  1587. function FindClass(const AClassName: string): TPersistentClass;
  1588. begin
  1589. Result := GetClass(AClassName);
  1590. if not Assigned(Result) then
  1591. raise EClassNotFound.CreateFmt(SClassNotFound, [AClassName]);
  1592. end;
  1593. function CollectionsEqual(C1, C2: TCollection): Boolean;
  1594. Var
  1595. Comp1,Comp2 : TComponent;
  1596. begin
  1597. Comp2:=Nil;
  1598. Comp1:=TComponent.Create;
  1599. try
  1600. Result:=CollectionsEqual(C1,C2,Comp1,Comp2);
  1601. finally
  1602. Comp1.Free;
  1603. Comp2.Free;
  1604. end;
  1605. end;
  1606. function CollectionsEqual(C1, C2: TCollection; Owner1, Owner2: TComponent): Boolean;
  1607. procedure stream_collection(s : tstream;c : tcollection;o : tcomponent);
  1608. var
  1609. w : twriter;
  1610. begin
  1611. w:=twriter.create(s);
  1612. try
  1613. w.root:=o;
  1614. w.flookuproot:=o;
  1615. w.writecollection(c);
  1616. finally
  1617. w.free;
  1618. end;
  1619. end;
  1620. var
  1621. s1,s2 : tbytesstream;
  1622. b1,b2 : TBytes;
  1623. I,Len : Integer;
  1624. begin
  1625. result:=false;
  1626. if (c1.classtype<>c2.classtype) or
  1627. (c1.count<>c2.count) then
  1628. exit;
  1629. if c1.count = 0 then
  1630. begin
  1631. result:= true;
  1632. exit;
  1633. end;
  1634. s2:=Nil;
  1635. s1:=tbytesstream.create;
  1636. try
  1637. s2:=tbytesstream.create;
  1638. stream_collection(s1,c1,owner1);
  1639. stream_collection(s2,c2,owner2);
  1640. result:=(s1.size=s2.size);
  1641. if Result then
  1642. begin
  1643. b1:=S1.Bytes;
  1644. b2:=S2.Bytes;
  1645. I:=0;
  1646. Len:=S1.Size; // Not length of B
  1647. While Result and (I<Len) do
  1648. begin
  1649. Result:=b1[I]=b2[i];
  1650. Inc(i);
  1651. end;
  1652. end;
  1653. finally
  1654. s2.free;
  1655. s1.free;
  1656. end;
  1657. end;
  1658. { TInterfacedPersistent }
  1659. function TInterfacedPersistent._AddRef: Integer;
  1660. begin
  1661. Result:=-1;
  1662. if Assigned(FOwnerInterface) then
  1663. Result:=FOwnerInterface._AddRef;
  1664. end;
  1665. function TInterfacedPersistent._Release: Integer;
  1666. begin
  1667. Result:=-1;
  1668. if Assigned(FOwnerInterface) then
  1669. Result:=FOwnerInterface._Release;
  1670. end;
  1671. function TInterfacedPersistent.QueryInterface(const IID: TGUID; out Obj): HRESULT;
  1672. begin
  1673. Result:=E_NOINTERFACE;
  1674. if GetInterface(IID, Obj) then
  1675. Result:=0;
  1676. end;
  1677. procedure TInterfacedPersistent.AfterConstruction;
  1678. begin
  1679. inherited AfterConstruction;
  1680. if (GetOwner<>nil) then
  1681. GetOwner.GetInterface(IInterface, FOwnerInterface);
  1682. end;
  1683. { TComponentEnumerator }
  1684. constructor TComponentEnumerator.Create(AComponent: TComponent);
  1685. begin
  1686. inherited Create;
  1687. FComponent := AComponent;
  1688. FPosition := -1;
  1689. end;
  1690. function TComponentEnumerator.GetCurrent: TComponent;
  1691. begin
  1692. Result := FComponent.Components[FPosition];
  1693. end;
  1694. function TComponentEnumerator.MoveNext: Boolean;
  1695. begin
  1696. Inc(FPosition);
  1697. Result := FPosition < FComponent.ComponentCount;
  1698. end;
  1699. { TListEnumerator }
  1700. constructor TListEnumerator.Create(AList: TList);
  1701. begin
  1702. inherited Create;
  1703. FList := AList;
  1704. FPosition := -1;
  1705. end;
  1706. function TListEnumerator.GetCurrent: JSValue;
  1707. begin
  1708. Result := FList[FPosition];
  1709. end;
  1710. function TListEnumerator.MoveNext: Boolean;
  1711. begin
  1712. Inc(FPosition);
  1713. Result := FPosition < FList.Count;
  1714. end;
  1715. { TFPListEnumerator }
  1716. constructor TFPListEnumerator.Create(AList: TFPList);
  1717. begin
  1718. inherited Create;
  1719. FList := AList;
  1720. FPosition := -1;
  1721. end;
  1722. function TFPListEnumerator.GetCurrent: JSValue;
  1723. begin
  1724. Result := FList[FPosition];
  1725. end;
  1726. function TFPListEnumerator.MoveNext: Boolean;
  1727. begin
  1728. Inc(FPosition);
  1729. Result := FPosition < FList.Count;
  1730. end;
  1731. { TFPList }
  1732. procedure TFPList.CopyMove(aList: TFPList);
  1733. var r : integer;
  1734. begin
  1735. Clear;
  1736. for r := 0 to aList.count-1 do
  1737. Add(aList[r]);
  1738. end;
  1739. procedure TFPList.MergeMove(aList: TFPList);
  1740. var r : integer;
  1741. begin
  1742. For r := 0 to aList.count-1 do
  1743. if IndexOf(aList[r]) < 0 then
  1744. Add(aList[r]);
  1745. end;
  1746. procedure TFPList.DoCopy(ListA, ListB: TFPList);
  1747. begin
  1748. if Assigned(ListB) then
  1749. CopyMove(ListB)
  1750. else
  1751. CopyMove(ListA);
  1752. end;
  1753. procedure TFPList.DoSrcUnique(ListA, ListB: TFPList);
  1754. var r : integer;
  1755. begin
  1756. if Assigned(ListB) then
  1757. begin
  1758. Clear;
  1759. for r := 0 to ListA.Count-1 do
  1760. if ListB.IndexOf(ListA[r]) < 0 then
  1761. Add(ListA[r]);
  1762. end
  1763. else
  1764. begin
  1765. for r := Count-1 downto 0 do
  1766. if ListA.IndexOf(Self[r]) >= 0 then
  1767. Delete(r);
  1768. end;
  1769. end;
  1770. procedure TFPList.DoAnd(ListA, ListB: TFPList);
  1771. var r : integer;
  1772. begin
  1773. if Assigned(ListB) then
  1774. begin
  1775. Clear;
  1776. for r := 0 to ListA.count-1 do
  1777. if ListB.IndexOf(ListA[r]) >= 0 then
  1778. Add(ListA[r]);
  1779. end
  1780. else
  1781. begin
  1782. for r := Count-1 downto 0 do
  1783. if ListA.IndexOf(Self[r]) < 0 then
  1784. Delete(r);
  1785. end;
  1786. end;
  1787. procedure TFPList.DoDestUnique(ListA, ListB: TFPList);
  1788. procedure MoveElements(Src, Dest: TFPList);
  1789. var r : integer;
  1790. begin
  1791. Clear;
  1792. for r := 0 to Src.count-1 do
  1793. if Dest.IndexOf(Src[r]) < 0 then
  1794. self.Add(Src[r]);
  1795. end;
  1796. var Dest : TFPList;
  1797. begin
  1798. if Assigned(ListB) then
  1799. MoveElements(ListB, ListA)
  1800. else
  1801. Dest := TFPList.Create;
  1802. try
  1803. Dest.CopyMove(Self);
  1804. MoveElements(ListA, Dest)
  1805. finally
  1806. Dest.Destroy;
  1807. end;
  1808. end;
  1809. procedure TFPList.DoOr(ListA, ListB: TFPList);
  1810. begin
  1811. if Assigned(ListB) then
  1812. begin
  1813. CopyMove(ListA);
  1814. MergeMove(ListB);
  1815. end
  1816. else
  1817. MergeMove(ListA);
  1818. end;
  1819. procedure TFPList.DoXOr(ListA, ListB: TFPList);
  1820. var
  1821. r : integer;
  1822. l : TFPList;
  1823. begin
  1824. if Assigned(ListB) then
  1825. begin
  1826. Clear;
  1827. for r := 0 to ListA.Count-1 do
  1828. if ListB.IndexOf(ListA[r]) < 0 then
  1829. Add(ListA[r]);
  1830. for r := 0 to ListB.Count-1 do
  1831. if ListA.IndexOf(ListB[r]) < 0 then
  1832. Add(ListB[r]);
  1833. end
  1834. else
  1835. begin
  1836. l := TFPList.Create;
  1837. try
  1838. l.CopyMove(Self);
  1839. for r := Count-1 downto 0 do
  1840. if listA.IndexOf(Self[r]) >= 0 then
  1841. Delete(r);
  1842. for r := 0 to ListA.Count-1 do
  1843. if l.IndexOf(ListA[r]) < 0 then
  1844. Add(ListA[r]);
  1845. finally
  1846. l.Destroy;
  1847. end;
  1848. end;
  1849. end;
  1850. function TFPList.Get(Index: Integer): JSValue;
  1851. begin
  1852. If (Index < 0) or (Index >= FCount) then
  1853. RaiseIndexError(Index);
  1854. Result:=FList[Index];
  1855. end;
  1856. procedure TFPList.Put(Index: Integer; Item: JSValue);
  1857. begin
  1858. if (Index < 0) or (Index >= FCount) then
  1859. RaiseIndexError(Index);
  1860. FList[Index] := Item;
  1861. end;
  1862. procedure TFPList.SetCapacity(NewCapacity: Integer);
  1863. begin
  1864. If (NewCapacity < FCount) then
  1865. Error (SListCapacityError, str(NewCapacity));
  1866. if NewCapacity = FCapacity then
  1867. exit;
  1868. SetLength(FList,NewCapacity);
  1869. FCapacity := NewCapacity;
  1870. end;
  1871. procedure TFPList.SetCount(NewCount: Integer);
  1872. begin
  1873. if (NewCount < 0) then
  1874. Error(SListCountError, str(NewCount));
  1875. If NewCount > FCount then
  1876. begin
  1877. If NewCount > FCapacity then
  1878. SetCapacity(NewCount);
  1879. end;
  1880. FCount := NewCount;
  1881. end;
  1882. procedure TFPList.RaiseIndexError(Index: Integer);
  1883. begin
  1884. Error(SListIndexError, str(Index));
  1885. end;
  1886. destructor TFPList.Destroy;
  1887. begin
  1888. Clear;
  1889. inherited Destroy;
  1890. end;
  1891. procedure TFPList.AddList(AList: TFPList);
  1892. Var
  1893. I : Integer;
  1894. begin
  1895. If (Capacity<Count+AList.Count) then
  1896. Capacity:=Count+AList.Count;
  1897. For I:=0 to AList.Count-1 do
  1898. Add(AList[i]);
  1899. end;
  1900. function TFPList.Add(Item: JSValue): Integer;
  1901. begin
  1902. if FCount = FCapacity then
  1903. Expand;
  1904. FList[FCount] := Item;
  1905. Result := FCount;
  1906. Inc(FCount);
  1907. end;
  1908. procedure TFPList.Clear;
  1909. begin
  1910. if Assigned(FList) then
  1911. begin
  1912. SetCount(0);
  1913. SetCapacity(0);
  1914. end;
  1915. end;
  1916. procedure TFPList.Delete(Index: Integer);
  1917. begin
  1918. If (Index<0) or (Index>=FCount) then
  1919. Error (SListIndexError, str(Index));
  1920. FCount := FCount-1;
  1921. System.Delete(FList,Index,1);
  1922. Dec(FCapacity);
  1923. end;
  1924. class procedure TFPList.Error(const Msg: string; const Data: String);
  1925. begin
  1926. Raise EListError.CreateFmt(Msg,[Data]);
  1927. end;
  1928. procedure TFPList.Exchange(Index1, Index2: Integer);
  1929. var
  1930. Temp : JSValue;
  1931. begin
  1932. If (Index1 >= FCount) or (Index1 < 0) then
  1933. Error(SListIndexError, str(Index1));
  1934. If (Index2 >= FCount) or (Index2 < 0) then
  1935. Error(SListIndexError, str(Index2));
  1936. Temp := FList[Index1];
  1937. FList[Index1] := FList[Index2];
  1938. FList[Index2] := Temp;
  1939. end;
  1940. function TFPList.Expand: TFPList;
  1941. var
  1942. IncSize : Integer;
  1943. begin
  1944. if FCount < FCapacity then exit(self);
  1945. IncSize := 4;
  1946. if FCapacity > 3 then IncSize := IncSize + 4;
  1947. if FCapacity > 8 then IncSize := IncSize+8;
  1948. if FCapacity > 127 then Inc(IncSize, FCapacity shr 2);
  1949. SetCapacity(FCapacity + IncSize);
  1950. Result := Self;
  1951. end;
  1952. function TFPList.Extract(Item: JSValue): JSValue;
  1953. var
  1954. i : Integer;
  1955. begin
  1956. i := IndexOf(Item);
  1957. if i >= 0 then
  1958. begin
  1959. Result := Item;
  1960. Delete(i);
  1961. end
  1962. else
  1963. Result := nil;
  1964. end;
  1965. function TFPList.First: JSValue;
  1966. begin
  1967. If FCount = 0 then
  1968. Result := Nil
  1969. else
  1970. Result := Items[0];
  1971. end;
  1972. function TFPList.GetEnumerator: TFPListEnumerator;
  1973. begin
  1974. Result:=TFPListEnumerator.Create(Self);
  1975. end;
  1976. function TFPList.IndexOf(Item: JSValue): Integer;
  1977. Var
  1978. C : Integer;
  1979. begin
  1980. Result:=0;
  1981. C:=Count;
  1982. while (Result<C) and (FList[Result]<>Item) do
  1983. Inc(Result);
  1984. If Result>=C then
  1985. Result:=-1;
  1986. end;
  1987. function TFPList.IndexOfItem(Item: JSValue; Direction: TDirection): Integer;
  1988. begin
  1989. if Direction=fromBeginning then
  1990. Result:=IndexOf(Item)
  1991. else
  1992. begin
  1993. Result:=Count-1;
  1994. while (Result >=0) and (Flist[Result]<>Item) do
  1995. Result:=Result - 1;
  1996. end;
  1997. end;
  1998. procedure TFPList.Insert(Index: Integer; Item: JSValue);
  1999. begin
  2000. if (Index < 0) or (Index > FCount )then
  2001. Error(SlistIndexError, str(Index));
  2002. TJSArray(FList).splice(Index, 0, Item);
  2003. inc(FCapacity);
  2004. inc(FCount);
  2005. end;
  2006. function TFPList.Last: JSValue;
  2007. begin
  2008. If FCount = 0 then
  2009. Result := nil
  2010. else
  2011. Result := Items[FCount - 1];
  2012. end;
  2013. procedure TFPList.Move(CurIndex, NewIndex: Integer);
  2014. var
  2015. Temp: JSValue;
  2016. begin
  2017. if (CurIndex < 0) or (CurIndex > Count - 1) then
  2018. Error(SListIndexError, str(CurIndex));
  2019. if (NewIndex < 0) or (NewIndex > Count -1) then
  2020. Error(SlistIndexError, str(NewIndex));
  2021. if CurIndex=NewIndex then exit;
  2022. Temp:=FList[CurIndex];
  2023. // ToDo: use TJSArray.copyWithin if available
  2024. TJSArray(FList).splice(CurIndex,1);
  2025. TJSArray(FList).splice(NewIndex,0,Temp);
  2026. end;
  2027. procedure TFPList.Assign(ListA: TFPList; AOperator: TListAssignOp;
  2028. ListB: TFPList);
  2029. begin
  2030. case AOperator of
  2031. laCopy : DoCopy (ListA, ListB); // replace dest with src
  2032. laSrcUnique : DoSrcUnique (ListA, ListB); // replace dest with src that are not in dest
  2033. laAnd : DoAnd (ListA, ListB); // remove from dest that are not in src
  2034. laDestUnique: DoDestUnique (ListA, ListB);// remove from dest that are in src
  2035. laOr : DoOr (ListA, ListB); // add to dest from src and not in dest
  2036. laXOr : DoXOr (ListA, ListB); // add to dest from src and not in dest, remove from dest that are in src
  2037. end;
  2038. end;
  2039. function TFPList.Remove(Item: JSValue): Integer;
  2040. begin
  2041. Result := IndexOf(Item);
  2042. If Result <> -1 then
  2043. Delete(Result);
  2044. end;
  2045. procedure TFPList.Pack;
  2046. var
  2047. Dst, i: Integer;
  2048. V: JSValue;
  2049. begin
  2050. Dst:=0;
  2051. for i:=0 to Count-1 do
  2052. begin
  2053. V:=FList[i];
  2054. if not Assigned(V) then continue;
  2055. FList[Dst]:=V;
  2056. inc(Dst);
  2057. end;
  2058. end;
  2059. // Needed by Sort method.
  2060. Procedure QuickSort(aList: TJSValueDynArray; L, R : Longint;
  2061. const Compare: TListSortCompareFunc
  2062. );
  2063. var
  2064. I, J, PivotIdx : SizeUInt;
  2065. P, Q : JSValue;
  2066. begin
  2067. repeat
  2068. I := L;
  2069. J := R;
  2070. PivotIdx := L + ((R - L) shr 1); { same as ((L + R) div 2), but without the possibility of overflow }
  2071. P := aList[PivotIdx];
  2072. repeat
  2073. while (I < PivotIdx) and (Compare(P, aList[i]) > 0) do
  2074. Inc(I);
  2075. while (J > PivotIdx) and (Compare(P, aList[J]) < 0) do
  2076. Dec(J);
  2077. if I < J then
  2078. begin
  2079. Q := aList[I];
  2080. aList[I] := aList[J];
  2081. aList[J] := Q;
  2082. if PivotIdx = I then
  2083. begin
  2084. PivotIdx := J;
  2085. Inc(I);
  2086. end
  2087. else if PivotIdx = J then
  2088. begin
  2089. PivotIdx := I;
  2090. Dec(J);
  2091. end
  2092. else
  2093. begin
  2094. Inc(I);
  2095. Dec(J);
  2096. end;
  2097. end;
  2098. until I >= J;
  2099. // sort the smaller range recursively
  2100. // sort the bigger range via the loop
  2101. // Reasons: memory usage is O(log(n)) instead of O(n) and loop is faster than recursion
  2102. if (PivotIdx - L) < (R - PivotIdx) then
  2103. begin
  2104. if (L + 1) < PivotIdx then
  2105. QuickSort(aList, L, PivotIdx - 1, Compare);
  2106. L := PivotIdx + 1;
  2107. end
  2108. else
  2109. begin
  2110. if (PivotIdx + 1) < R then
  2111. QuickSort(aList, PivotIdx + 1, R, Compare);
  2112. if (L + 1) < PivotIdx then
  2113. R := PivotIdx - 1
  2114. else
  2115. exit;
  2116. end;
  2117. until L >= R;
  2118. end;
  2119. (*
  2120. Procedure QuickSort(aList: TJSValueDynArray; L, R : Longint;
  2121. const Compare: TListSortCompareFunc);
  2122. var
  2123. I, J : Longint;
  2124. P, Q : JSValue;
  2125. begin
  2126. repeat
  2127. I := L;
  2128. J := R;
  2129. P := aList[ (L + R) div 2 ];
  2130. repeat
  2131. while Compare(P, aList[i]) > 0 do
  2132. I := I + 1;
  2133. while Compare(P, aList[J]) < 0 do
  2134. J := J - 1;
  2135. If I <= J then
  2136. begin
  2137. Q := aList[I];
  2138. aList[I] := aList[J];
  2139. aList[J] := Q;
  2140. I := I + 1;
  2141. J := J - 1;
  2142. end;
  2143. until I > J;
  2144. // sort the smaller range recursively
  2145. // sort the bigger range via the loop
  2146. // Reasons: memory usage is O(log(n)) instead of O(n) and loop is faster than recursion
  2147. if J - L < R - I then
  2148. begin
  2149. if L < J then
  2150. QuickSort(aList, L, J, Compare);
  2151. L := I;
  2152. end
  2153. else
  2154. begin
  2155. if I < R then
  2156. QuickSort(aList, I, R, Compare);
  2157. R := J;
  2158. end;
  2159. until L >= R;
  2160. end;
  2161. *)
  2162. procedure TFPList.Sort(const Compare: TListSortCompare);
  2163. begin
  2164. if Not Assigned(FList) or (FCount < 2) then exit;
  2165. QuickSort(Flist, 0, FCount-1,
  2166. function(Item1, Item2: JSValue): Integer
  2167. begin
  2168. Result := Compare(Item1, Item2);
  2169. end);
  2170. end;
  2171. procedure TFPList.SortList(const Compare: TListSortCompareFunc);
  2172. begin
  2173. if Not Assigned(FList) or (FCount < 2) then exit;
  2174. QuickSort(Flist, 0, FCount-1, Compare);
  2175. end;
  2176. procedure TFPList.ForEachCall(const proc2call: TListCallback; const arg: JSValue
  2177. );
  2178. var
  2179. i : integer;
  2180. v : JSValue;
  2181. begin
  2182. For I:=0 To Count-1 Do
  2183. begin
  2184. v:=FList[i];
  2185. if Assigned(v) then
  2186. proc2call(v,arg);
  2187. end;
  2188. end;
  2189. procedure TFPList.ForEachCall(const proc2call: TListStaticCallback;
  2190. const arg: JSValue);
  2191. var
  2192. i : integer;
  2193. v : JSValue;
  2194. begin
  2195. For I:=0 To Count-1 Do
  2196. begin
  2197. v:=FList[i];
  2198. if Assigned(v) then
  2199. proc2call(v,arg);
  2200. end;
  2201. end;
  2202. { TList }
  2203. procedure TList.CopyMove(aList: TList);
  2204. var
  2205. r : integer;
  2206. begin
  2207. Clear;
  2208. for r := 0 to aList.count-1 do
  2209. Add(aList[r]);
  2210. end;
  2211. procedure TList.MergeMove(aList: TList);
  2212. var r : integer;
  2213. begin
  2214. For r := 0 to aList.count-1 do
  2215. if IndexOf(aList[r]) < 0 then
  2216. Add(aList[r]);
  2217. end;
  2218. procedure TList.DoCopy(ListA, ListB: TList);
  2219. begin
  2220. if Assigned(ListB) then
  2221. CopyMove(ListB)
  2222. else
  2223. CopyMove(ListA);
  2224. end;
  2225. procedure TList.DoSrcUnique(ListA, ListB: TList);
  2226. var r : integer;
  2227. begin
  2228. if Assigned(ListB) then
  2229. begin
  2230. Clear;
  2231. for r := 0 to ListA.Count-1 do
  2232. if ListB.IndexOf(ListA[r]) < 0 then
  2233. Add(ListA[r]);
  2234. end
  2235. else
  2236. begin
  2237. for r := Count-1 downto 0 do
  2238. if ListA.IndexOf(Self[r]) >= 0 then
  2239. Delete(r);
  2240. end;
  2241. end;
  2242. procedure TList.DoAnd(ListA, ListB: TList);
  2243. var r : integer;
  2244. begin
  2245. if Assigned(ListB) then
  2246. begin
  2247. Clear;
  2248. for r := 0 to ListA.Count-1 do
  2249. if ListB.IndexOf(ListA[r]) >= 0 then
  2250. Add(ListA[r]);
  2251. end
  2252. else
  2253. begin
  2254. for r := Count-1 downto 0 do
  2255. if ListA.IndexOf(Self[r]) < 0 then
  2256. Delete(r);
  2257. end;
  2258. end;
  2259. procedure TList.DoDestUnique(ListA, ListB: TList);
  2260. procedure MoveElements(Src, Dest : TList);
  2261. var r : integer;
  2262. begin
  2263. Clear;
  2264. for r := 0 to Src.Count-1 do
  2265. if Dest.IndexOf(Src[r]) < 0 then
  2266. Add(Src[r]);
  2267. end;
  2268. var Dest : TList;
  2269. begin
  2270. if Assigned(ListB) then
  2271. MoveElements(ListB, ListA)
  2272. else
  2273. try
  2274. Dest := TList.Create;
  2275. Dest.CopyMove(Self);
  2276. MoveElements(ListA, Dest)
  2277. finally
  2278. Dest.Destroy;
  2279. end;
  2280. end;
  2281. procedure TList.DoOr(ListA, ListB: TList);
  2282. begin
  2283. if Assigned(ListB) then
  2284. begin
  2285. CopyMove(ListA);
  2286. MergeMove(ListB);
  2287. end
  2288. else
  2289. MergeMove(ListA);
  2290. end;
  2291. procedure TList.DoXOr(ListA, ListB: TList);
  2292. var
  2293. r : integer;
  2294. l : TList;
  2295. begin
  2296. if Assigned(ListB) then
  2297. begin
  2298. Clear;
  2299. for r := 0 to ListA.Count-1 do
  2300. if ListB.IndexOf(ListA[r]) < 0 then
  2301. Add(ListA[r]);
  2302. for r := 0 to ListB.Count-1 do
  2303. if ListA.IndexOf(ListB[r]) < 0 then
  2304. Add(ListB[r]);
  2305. end
  2306. else
  2307. try
  2308. l := TList.Create;
  2309. l.CopyMove (Self);
  2310. for r := Count-1 downto 0 do
  2311. if listA.IndexOf(Self[r]) >= 0 then
  2312. Delete(r);
  2313. for r := 0 to ListA.Count-1 do
  2314. if l.IndexOf(ListA[r]) < 0 then
  2315. Add(ListA[r]);
  2316. finally
  2317. l.Destroy;
  2318. end;
  2319. end;
  2320. function TList.Get(Index: Integer): JSValue;
  2321. begin
  2322. Result := FList.Get(Index);
  2323. end;
  2324. procedure TList.Put(Index: Integer; Item: JSValue);
  2325. var V : JSValue;
  2326. begin
  2327. V := Get(Index);
  2328. FList.Put(Index, Item);
  2329. if Assigned(V) then
  2330. Notify(V, lnDeleted);
  2331. if Assigned(Item) then
  2332. Notify(Item, lnAdded);
  2333. end;
  2334. procedure TList.Notify(aValue: JSValue; Action: TListNotification);
  2335. begin
  2336. if Assigned(aValue) then ;
  2337. if Action=lnExtracted then ;
  2338. end;
  2339. procedure TList.SetCapacity(NewCapacity: Integer);
  2340. begin
  2341. FList.SetCapacity(NewCapacity);
  2342. end;
  2343. function TList.GetCapacity: integer;
  2344. begin
  2345. Result := FList.Capacity;
  2346. end;
  2347. procedure TList.SetCount(NewCount: Integer);
  2348. begin
  2349. if NewCount < FList.Count then
  2350. while FList.Count > NewCount do
  2351. Delete(FList.Count - 1)
  2352. else
  2353. FList.SetCount(NewCount);
  2354. end;
  2355. function TList.GetCount: integer;
  2356. begin
  2357. Result := FList.Count;
  2358. end;
  2359. function TList.GetList: TJSValueDynArray;
  2360. begin
  2361. Result := FList.List;
  2362. end;
  2363. constructor TList.Create;
  2364. begin
  2365. inherited Create;
  2366. FList := TFPList.Create;
  2367. end;
  2368. destructor TList.Destroy;
  2369. begin
  2370. if Assigned(FList) then
  2371. Clear;
  2372. FreeAndNil(FList);
  2373. end;
  2374. procedure TList.AddList(AList: TList);
  2375. var
  2376. I: Integer;
  2377. begin
  2378. { this only does FList.AddList(AList.FList), avoiding notifications }
  2379. FList.AddList(AList.FList);
  2380. { make lnAdded notifications }
  2381. for I := 0 to AList.Count - 1 do
  2382. if Assigned(AList[I]) then
  2383. Notify(AList[I], lnAdded);
  2384. end;
  2385. function TList.Add(Item: JSValue): Integer;
  2386. begin
  2387. Result := FList.Add(Item);
  2388. if Assigned(Item) then
  2389. Notify(Item, lnAdded);
  2390. end;
  2391. procedure TList.Clear;
  2392. begin
  2393. While (FList.Count>0) do
  2394. Delete(Count-1);
  2395. end;
  2396. procedure TList.Delete(Index: Integer);
  2397. var V : JSValue;
  2398. begin
  2399. V:=FList.Get(Index);
  2400. FList.Delete(Index);
  2401. if assigned(V) then
  2402. Notify(V, lnDeleted);
  2403. end;
  2404. class procedure TList.Error(const Msg: string; Data: String);
  2405. begin
  2406. Raise EListError.CreateFmt(Msg,[Data]);
  2407. end;
  2408. procedure TList.Exchange(Index1, Index2: Integer);
  2409. begin
  2410. FList.Exchange(Index1, Index2);
  2411. end;
  2412. function TList.Expand: TList;
  2413. begin
  2414. FList.Expand;
  2415. Result:=Self;
  2416. end;
  2417. function TList.Extract(Item: JSValue): JSValue;
  2418. var c : integer;
  2419. begin
  2420. c := FList.Count;
  2421. Result := FList.Extract(Item);
  2422. if c <> FList.Count then
  2423. Notify (Result, lnExtracted);
  2424. end;
  2425. function TList.First: JSValue;
  2426. begin
  2427. Result := FList.First;
  2428. end;
  2429. function TList.GetEnumerator: TListEnumerator;
  2430. begin
  2431. Result:=TListEnumerator.Create(Self);
  2432. end;
  2433. function TList.IndexOf(Item: JSValue): Integer;
  2434. begin
  2435. Result := FList.IndexOf(Item);
  2436. end;
  2437. procedure TList.Insert(Index: Integer; Item: JSValue);
  2438. begin
  2439. FList.Insert(Index, Item);
  2440. if Assigned(Item) then
  2441. Notify(Item,lnAdded);
  2442. end;
  2443. function TList.Last: JSValue;
  2444. begin
  2445. Result := FList.Last;
  2446. end;
  2447. procedure TList.Move(CurIndex, NewIndex: Integer);
  2448. begin
  2449. FList.Move(CurIndex, NewIndex);
  2450. end;
  2451. procedure TList.Assign(ListA: TList; AOperator: TListAssignOp; ListB: TList);
  2452. begin
  2453. case AOperator of
  2454. laCopy : DoCopy (ListA, ListB); // replace dest with src
  2455. laSrcUnique : DoSrcUnique (ListA, ListB); // replace dest with src that are not in dest
  2456. laAnd : DoAnd (ListA, ListB); // remove from dest that are not in src
  2457. laDestUnique: DoDestUnique (ListA, ListB);// remove from dest that are in src
  2458. laOr : DoOr (ListA, ListB); // add to dest from src and not in dest
  2459. laXOr : DoXOr (ListA, ListB); // add to dest from src and not in dest, remove from dest that are in src
  2460. end;
  2461. end;
  2462. function TList.Remove(Item: JSValue): Integer;
  2463. begin
  2464. Result := IndexOf(Item);
  2465. if Result <> -1 then
  2466. Self.Delete(Result);
  2467. end;
  2468. procedure TList.Pack;
  2469. begin
  2470. FList.Pack;
  2471. end;
  2472. procedure TList.Sort(const Compare: TListSortCompare);
  2473. begin
  2474. FList.Sort(Compare);
  2475. end;
  2476. procedure TList.SortList(const Compare: TListSortCompareFunc);
  2477. begin
  2478. FList.SortList(Compare);
  2479. end;
  2480. { TPersistent }
  2481. procedure TPersistent.AssignError(Source: TPersistent);
  2482. var
  2483. SourceName: String;
  2484. begin
  2485. if Source<>Nil then
  2486. SourceName:=Source.ClassName
  2487. else
  2488. SourceName:='Nil';
  2489. raise EConvertError.Create('Cannot assign a '+SourceName+' to a '+ClassName+'.');
  2490. end;
  2491. procedure TPersistent.DefineProperties(Filer: TFiler);
  2492. begin
  2493. if Filer=Nil then exit;
  2494. // Do nothing
  2495. end;
  2496. procedure TPersistent.AssignTo(Dest: TPersistent);
  2497. begin
  2498. Dest.AssignError(Self);
  2499. end;
  2500. function TPersistent.GetOwner: TPersistent;
  2501. begin
  2502. Result:=nil;
  2503. end;
  2504. procedure TPersistent.Assign(Source: TPersistent);
  2505. begin
  2506. If Source<>Nil then
  2507. Source.AssignTo(Self)
  2508. else
  2509. AssignError(Nil);
  2510. end;
  2511. function TPersistent.GetNamePath: string;
  2512. var
  2513. OwnerName: String;
  2514. TheOwner: TPersistent;
  2515. begin
  2516. Result:=ClassName;
  2517. TheOwner:=GetOwner;
  2518. if TheOwner<>Nil then
  2519. begin
  2520. OwnerName:=TheOwner.GetNamePath;
  2521. if OwnerName<>'' then Result:=OwnerName+'.'+Result;
  2522. end;
  2523. end;
  2524. {
  2525. This file is part of the Free Component Library (FCL)
  2526. Copyright (c) 1999-2000 by the Free Pascal development team
  2527. See the file COPYING.FPC, included in this distribution,
  2528. for details about the copyright.
  2529. This program is distributed in the hope that it will be useful,
  2530. but WITHOUT ANY WARRANTY; without even the implied warranty of
  2531. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  2532. **********************************************************************}
  2533. {****************************************************************************}
  2534. {* TStringsEnumerator *}
  2535. {****************************************************************************}
  2536. constructor TStringsEnumerator.Create(AStrings: TStrings);
  2537. begin
  2538. inherited Create;
  2539. FStrings := AStrings;
  2540. FPosition := -1;
  2541. end;
  2542. function TStringsEnumerator.GetCurrent: String;
  2543. begin
  2544. Result := FStrings[FPosition];
  2545. end;
  2546. function TStringsEnumerator.MoveNext: Boolean;
  2547. begin
  2548. Inc(FPosition);
  2549. Result := FPosition < FStrings.Count;
  2550. end;
  2551. {****************************************************************************}
  2552. {* TStrings *}
  2553. {****************************************************************************}
  2554. // Function to quote text. Should move maybe to sysutils !!
  2555. // Also, it is not clear at this point what exactly should be done.
  2556. { //!! is used to mark unsupported things. }
  2557. {
  2558. For compatibility we can't add a Constructor to TSTrings to initialize
  2559. the special characters. Therefore we add a routine which is called whenever
  2560. the special chars are needed.
  2561. }
  2562. procedure TStrings.CheckSpecialChars;
  2563. begin
  2564. If Not FSpecialCharsInited then
  2565. begin
  2566. FQuoteChar:='"';
  2567. FDelimiter:=',';
  2568. FNameValueSeparator:='=';
  2569. FLBS:=DefaultTextLineBreakStyle;
  2570. FSpecialCharsInited:=true;
  2571. FLineBreak:=sLineBreak;
  2572. end;
  2573. end;
  2574. function TStrings.GetSkipLastLineBreak: Boolean;
  2575. begin
  2576. CheckSpecialChars;
  2577. Result:=FSkipLastLineBreak;
  2578. end;
  2579. procedure TStrings.SetSkipLastLineBreak(const AValue : Boolean);
  2580. begin
  2581. CheckSpecialChars;
  2582. FSkipLastLineBreak:=AValue;
  2583. end;
  2584. procedure TStrings.ReadData(Reader: TReader);
  2585. begin
  2586. Reader.ReadListBegin;
  2587. BeginUpdate;
  2588. try
  2589. Clear;
  2590. while not Reader.EndOfList do
  2591. Add(Reader.ReadString);
  2592. finally
  2593. EndUpdate;
  2594. end;
  2595. Reader.ReadListEnd;
  2596. end;
  2597. procedure TStrings.WriteData(Writer: TWriter);
  2598. var
  2599. i: Integer;
  2600. begin
  2601. Writer.WriteListBegin;
  2602. for i := 0 to Count - 1 do
  2603. Writer.WriteString(Strings[i]);
  2604. Writer.WriteListEnd;
  2605. end;
  2606. procedure TStrings.DefineProperties(Filer: TFiler);
  2607. var
  2608. HasData: Boolean;
  2609. begin
  2610. if Assigned(Filer.Ancestor) then
  2611. // Only serialize if string list is different from ancestor
  2612. if Filer.Ancestor.InheritsFrom(TStrings) then
  2613. HasData := not Equals(TStrings(Filer.Ancestor))
  2614. else
  2615. HasData := True
  2616. else
  2617. HasData := Count > 0;
  2618. Filer.DefineProperty('Strings', @ReadData, @WriteData, HasData);
  2619. end;
  2620. function TStrings.GetLBS: TTextLineBreakStyle;
  2621. begin
  2622. CheckSpecialChars;
  2623. Result:=FLBS;
  2624. end;
  2625. procedure TStrings.SetLBS(AValue: TTextLineBreakStyle);
  2626. begin
  2627. CheckSpecialChars;
  2628. FLBS:=AValue;
  2629. end;
  2630. procedure TStrings.SetDelimiter(c:Char);
  2631. begin
  2632. CheckSpecialChars;
  2633. FDelimiter:=c;
  2634. end;
  2635. function TStrings.GetDelimiter: Char;
  2636. begin
  2637. CheckSpecialChars;
  2638. Result:=FDelimiter;
  2639. end;
  2640. procedure TStrings.SetLineBreak(const S: String);
  2641. begin
  2642. CheckSpecialChars;
  2643. FLineBreak:=S;
  2644. end;
  2645. function TStrings.GetLineBreak: String;
  2646. begin
  2647. CheckSpecialChars;
  2648. Result:=FLineBreak;
  2649. end;
  2650. procedure TStrings.SetQuoteChar(c:Char);
  2651. begin
  2652. CheckSpecialChars;
  2653. FQuoteChar:=c;
  2654. end;
  2655. function TStrings.GetQuoteChar: Char;
  2656. begin
  2657. CheckSpecialChars;
  2658. Result:=FQuoteChar;
  2659. end;
  2660. procedure TStrings.SetNameValueSeparator(c:Char);
  2661. begin
  2662. CheckSpecialChars;
  2663. FNameValueSeparator:=c;
  2664. end;
  2665. function TStrings.GetNameValueSeparator: Char;
  2666. begin
  2667. CheckSpecialChars;
  2668. Result:=FNameValueSeparator;
  2669. end;
  2670. function TStrings.GetCommaText: string;
  2671. Var
  2672. C1,C2 : Char;
  2673. FSD : Boolean;
  2674. begin
  2675. CheckSpecialChars;
  2676. FSD:=StrictDelimiter;
  2677. C1:=Delimiter;
  2678. C2:=QuoteChar;
  2679. Delimiter:=',';
  2680. QuoteChar:='"';
  2681. StrictDelimiter:=False;
  2682. Try
  2683. Result:=GetDelimitedText;
  2684. Finally
  2685. Delimiter:=C1;
  2686. QuoteChar:=C2;
  2687. StrictDelimiter:=FSD;
  2688. end;
  2689. end;
  2690. function TStrings.GetDelimitedText: string;
  2691. Var
  2692. I: integer;
  2693. RE : string;
  2694. S : String;
  2695. doQuote : Boolean;
  2696. begin
  2697. CheckSpecialChars;
  2698. result:='';
  2699. RE:=QuoteChar+'|'+Delimiter;
  2700. if not StrictDelimiter then
  2701. RE:=' |'+RE;
  2702. RE:='/'+RE+'/';
  2703. // Check for break characters and quote if required.
  2704. For i:=0 to count-1 do
  2705. begin
  2706. S:=Strings[i];
  2707. doQuote:=FAlwaysQuote or (TJSString(s).search(RE)<>-1);
  2708. if DoQuote then
  2709. Result:=Result+QuoteString(S,QuoteChar)
  2710. else
  2711. Result:=Result+S;
  2712. if I<Count-1 then
  2713. Result:=Result+Delimiter;
  2714. end;
  2715. // Quote empty string:
  2716. If (Length(Result)=0) and (Count=1) then
  2717. Result:=QuoteChar+QuoteChar;
  2718. end;
  2719. procedure TStrings.GetNameValue(Index: Integer; out AName, AValue: String);
  2720. Var L : longint;
  2721. begin
  2722. CheckSpecialChars;
  2723. AValue:=Strings[Index];
  2724. L:=Pos(FNameValueSeparator,AValue);
  2725. If L<>0 then
  2726. begin
  2727. AName:=Copy(AValue,1,L-1);
  2728. // System.Delete(AValue,1,L);
  2729. AValue:=Copy(AValue,L+1,length(AValue)-L);
  2730. end
  2731. else
  2732. AName:='';
  2733. end;
  2734. procedure TStrings.LoadFromURL(const aURL: String; Async: Boolean; OnLoaded: TNotifyEventRef; OnError: TStringNotifyEventRef);
  2735. procedure DoLoaded(const aString : String);
  2736. begin
  2737. Text:=aString;
  2738. if Assigned(OnLoaded) then
  2739. OnLoaded(Self);
  2740. end;
  2741. procedure DoError(const AError : String);
  2742. begin
  2743. if Assigned(OnError) then
  2744. OnError(Self,aError)
  2745. else
  2746. Raise EInOutError.Create('Failed to load from URL:'+aError);
  2747. end;
  2748. begin
  2749. CheckLoadHelper;
  2750. GlobalLoadHelper.LoadText(aURL,aSync,@DoLoaded,@DoError);
  2751. end;
  2752. procedure TStrings.LoadFromFile(const aFileName: String; const OnLoaded: TProc; const AError: TProcString);
  2753. begin
  2754. LoadFromURL(aFileName,False,
  2755. Procedure (Sender : TObject)
  2756. begin
  2757. If Assigned(OnLoaded) then
  2758. OnLoaded
  2759. end,
  2760. Procedure (Sender : TObject; Const ErrorMsg : String)
  2761. begin
  2762. if Assigned(aError) then
  2763. aError(ErrorMsg)
  2764. end);
  2765. end;
  2766. function TStrings.ExtractName(const S: String): String;
  2767. var
  2768. L: Longint;
  2769. begin
  2770. CheckSpecialChars;
  2771. L:=Pos(FNameValueSeparator,S);
  2772. If L<>0 then
  2773. Result:=Copy(S,1,L-1)
  2774. else
  2775. Result:='';
  2776. end;
  2777. function TStrings.GetName(Index: Integer): string;
  2778. Var
  2779. V : String;
  2780. begin
  2781. GetNameValue(Index,Result,V);
  2782. end;
  2783. function TStrings.GetValue(const Name: string): string;
  2784. Var
  2785. L : longint;
  2786. N : String;
  2787. begin
  2788. Result:='';
  2789. L:=IndexOfName(Name);
  2790. If L<>-1 then
  2791. GetNameValue(L,N,Result);
  2792. end;
  2793. function TStrings.GetValueFromIndex(Index: Integer): string;
  2794. Var
  2795. N : String;
  2796. begin
  2797. GetNameValue(Index,N,Result);
  2798. end;
  2799. procedure TStrings.SetValueFromIndex(Index: Integer; const Value: string);
  2800. begin
  2801. If (Value='') then
  2802. Delete(Index)
  2803. else
  2804. begin
  2805. If (Index<0) then
  2806. Index:=Add('');
  2807. CheckSpecialChars;
  2808. Strings[Index]:=GetName(Index)+FNameValueSeparator+Value;
  2809. end;
  2810. end;
  2811. procedure TStrings.SetDelimitedText(const AValue: string);
  2812. var i,j:integer;
  2813. aNotFirst:boolean;
  2814. begin
  2815. CheckSpecialChars;
  2816. BeginUpdate;
  2817. i:=1;
  2818. j:=1;
  2819. aNotFirst:=false;
  2820. { Paraphrased from Delphi XE2 help:
  2821. Strings must be separated by Delimiter characters or spaces.
  2822. They may be enclosed in QuoteChars.
  2823. QuoteChars in the string must be repeated to distinguish them from the QuoteChars enclosing the string.
  2824. }
  2825. try
  2826. Clear;
  2827. If StrictDelimiter then
  2828. begin
  2829. while i<=length(AValue) do begin
  2830. // skip delimiter
  2831. if aNotFirst and (i<=length(AValue)) and (AValue[i]=FDelimiter) then inc(i);
  2832. // read next string
  2833. if i<=length(AValue) then begin
  2834. if AValue[i]=FQuoteChar then begin
  2835. // next string is quoted
  2836. j:=i+1;
  2837. while (j<=length(AValue)) and
  2838. ( (AValue[j]<>FQuoteChar) or
  2839. ( (j+1<=length(AValue)) and (AValue[j+1]=FQuoteChar) ) ) do begin
  2840. if (j<=length(AValue)) and (AValue[j]=FQuoteChar) then inc(j,2)
  2841. else inc(j);
  2842. end;
  2843. // j is position of closing quote
  2844. Add( StringReplace (Copy(AValue,i+1,j-i-1),
  2845. FQuoteChar+FQuoteChar,FQuoteChar, [rfReplaceAll]));
  2846. i:=j+1;
  2847. end else begin
  2848. // next string is not quoted; read until delimiter
  2849. j:=i;
  2850. while (j<=length(AValue)) and
  2851. (AValue[j]<>FDelimiter) do inc(j);
  2852. Add( Copy(AValue,i,j-i));
  2853. i:=j;
  2854. end;
  2855. end else begin
  2856. if aNotFirst then Add('');
  2857. end;
  2858. aNotFirst:=true;
  2859. end;
  2860. end
  2861. else
  2862. begin
  2863. while i<=length(AValue) do begin
  2864. // skip delimiter
  2865. if aNotFirst and (i<=length(AValue)) and (AValue[i]=FDelimiter) then inc(i);
  2866. // skip spaces
  2867. while (i<=length(AValue)) and (Ord(AValue[i])<=Ord(' ')) do inc(i);
  2868. // read next string
  2869. if i<=length(AValue) then begin
  2870. if AValue[i]=FQuoteChar then begin
  2871. // next string is quoted
  2872. j:=i+1;
  2873. while (j<=length(AValue)) and
  2874. ( (AValue[j]<>FQuoteChar) or
  2875. ( (j+1<=length(AValue)) and (AValue[j+1]=FQuoteChar) ) ) do begin
  2876. if (j<=length(AValue)) and (AValue[j]=FQuoteChar) then inc(j,2)
  2877. else inc(j);
  2878. end;
  2879. // j is position of closing quote
  2880. Add( StringReplace (Copy(AValue,i+1,j-i-1),
  2881. FQuoteChar+FQuoteChar,FQuoteChar, [rfReplaceAll]));
  2882. i:=j+1;
  2883. end else begin
  2884. // next string is not quoted; read until control character/space/delimiter
  2885. j:=i;
  2886. while (j<=length(AValue)) and
  2887. (Ord(AValue[j])>Ord(' ')) and
  2888. (AValue[j]<>FDelimiter) do inc(j);
  2889. Add( Copy(AValue,i,j-i));
  2890. i:=j;
  2891. end;
  2892. end else begin
  2893. if aNotFirst then Add('');
  2894. end;
  2895. // skip spaces
  2896. while (i<=length(AValue)) and (Ord(AValue[i])<=Ord(' ')) do inc(i);
  2897. aNotFirst:=true;
  2898. end;
  2899. end;
  2900. finally
  2901. EndUpdate;
  2902. end;
  2903. end;
  2904. procedure TStrings.SetCommaText(const Value: string);
  2905. Var
  2906. C1,C2 : Char;
  2907. begin
  2908. CheckSpecialChars;
  2909. C1:=Delimiter;
  2910. C2:=QuoteChar;
  2911. Delimiter:=',';
  2912. QuoteChar:='"';
  2913. Try
  2914. SetDelimitedText(Value);
  2915. Finally
  2916. Delimiter:=C1;
  2917. QuoteChar:=C2;
  2918. end;
  2919. end;
  2920. procedure TStrings.SetValue(const Name: String; const Value: string);
  2921. Var L : longint;
  2922. begin
  2923. CheckSpecialChars;
  2924. L:=IndexOfName(Name);
  2925. if L=-1 then
  2926. Add (Name+FNameValueSeparator+Value)
  2927. else
  2928. Strings[L]:=Name+FNameValueSeparator+value;
  2929. end;
  2930. procedure TStrings.Error(const Msg: string; Data: Integer);
  2931. begin
  2932. Raise EStringListError.CreateFmt(Msg,[IntToStr(Data)]);
  2933. end;
  2934. function TStrings.GetCapacity: Integer;
  2935. begin
  2936. Result:=Count;
  2937. end;
  2938. function TStrings.GetObject(Index: Integer): TObject;
  2939. begin
  2940. if Index=0 then ;
  2941. Result:=Nil;
  2942. end;
  2943. function TStrings.GetTextStr: string;
  2944. Var
  2945. I : Longint;
  2946. S,NL : String;
  2947. begin
  2948. CheckSpecialChars;
  2949. // Determine needed place
  2950. if FLineBreak<>sLineBreak then
  2951. NL:=FLineBreak
  2952. else
  2953. Case FLBS of
  2954. tlbsLF : NL:=#10;
  2955. tlbsCRLF : NL:=#13#10;
  2956. tlbsCR : NL:=#13;
  2957. end;
  2958. Result:='';
  2959. For i:=0 To count-1 do
  2960. begin
  2961. S:=Strings[I];
  2962. Result:=Result+S;
  2963. if (I<Count-1) or Not SkipLastLineBreak then
  2964. Result:=Result+NL;
  2965. end;
  2966. end;
  2967. procedure TStrings.Put(Index: Integer; const S: string);
  2968. Var Obj : TObject;
  2969. begin
  2970. Obj:=Objects[Index];
  2971. Delete(Index);
  2972. InsertObject(Index,S,Obj);
  2973. end;
  2974. procedure TStrings.PutObject(Index: Integer; AObject: TObject);
  2975. begin
  2976. // Empty.
  2977. if Index=0 then exit;
  2978. if AObject=nil then exit;
  2979. end;
  2980. procedure TStrings.SetCapacity(NewCapacity: Integer);
  2981. begin
  2982. // Empty.
  2983. if NewCapacity=0 then ;
  2984. end;
  2985. function TStrings.GetNextLinebreak(const Value: String; out S: String; var P: Integer): Boolean;
  2986. var
  2987. PPLF,PPCR,PP,PL: Integer;
  2988. begin
  2989. S:='';
  2990. Result:=False;
  2991. If ((Length(Value)-P)<0) then
  2992. Exit;
  2993. PPLF:=TJSString(Value).IndexOf(#10,P-1)+1;
  2994. PPCR:=TJSString(Value).IndexOf(#13,P-1)+1;
  2995. PL:=1;
  2996. if (PPLF>0) and (PPCR>0) then
  2997. begin
  2998. if (PPLF-PPCR)=1 then
  2999. PL:=2;
  3000. if PPLF<PPCR then
  3001. PP:=PPLF
  3002. else
  3003. PP:=PPCR;
  3004. end
  3005. else if (PPLF>0) and (PPCR<1) then
  3006. PP:=PPLF
  3007. else if (PPCR > 0) and (PPLF<1) then
  3008. PP:=PPCR
  3009. else
  3010. PP:=Length(Value)+1;
  3011. S:=Copy(Value,P,PP-P);
  3012. P:=PP+PL;
  3013. Result:=True;
  3014. end;
  3015. procedure TStrings.DoSetTextStr(const Value: string; DoClear: Boolean);
  3016. Var
  3017. S : String;
  3018. P : Integer;
  3019. begin
  3020. Try
  3021. BeginUpdate;
  3022. if DoClear then
  3023. Clear;
  3024. P:=1;
  3025. While GetNextLineBreak (Value,S,P) do
  3026. Add(S);
  3027. finally
  3028. EndUpdate;
  3029. end;
  3030. end;
  3031. procedure TStrings.SetTextStr(const Value: string);
  3032. begin
  3033. CheckSpecialChars;
  3034. DoSetTextStr(Value,True);
  3035. end;
  3036. procedure TStrings.AddText(const S: String);
  3037. begin
  3038. CheckSpecialChars;
  3039. DoSetTextStr(S,False);
  3040. end;
  3041. procedure TStrings.SetUpdateState(Updating: Boolean);
  3042. begin
  3043. // FPONotifyObservers(Self,ooChange,Nil);
  3044. if Updating then ;
  3045. end;
  3046. destructor TStrings.Destroy;
  3047. begin
  3048. inherited destroy;
  3049. end;
  3050. constructor TStrings.Create;
  3051. begin
  3052. inherited Create;
  3053. FAlwaysQuote:=False;
  3054. end;
  3055. function TStrings.ToObjectArray: TObjectDynArray;
  3056. begin
  3057. Result:=ToObjectArray(0,Count-1);
  3058. end;
  3059. function TStrings.ToObjectArray(aStart,aEnd : Integer): TObjectDynArray;
  3060. Var
  3061. I : Integer;
  3062. begin
  3063. Result:=Nil;
  3064. if aStart>aEnd then exit;
  3065. SetLength(Result,aEnd-aStart+1);
  3066. For I:=aStart to aEnd do
  3067. Result[i-aStart]:=Objects[i];
  3068. end;
  3069. function TStrings.ToStringArray: TStringDynArray;
  3070. begin
  3071. Result:=ToStringArray(0,Count-1);
  3072. end;
  3073. function TStrings.ToStringArray(aStart,aEnd : Integer): TStringDynArray;
  3074. Var
  3075. I : Integer;
  3076. begin
  3077. Result:=Nil;
  3078. if aStart>aEnd then exit;
  3079. SetLength(Result,aEnd-aStart+1);
  3080. For I:=aStart to aEnd do
  3081. Result[i-aStart]:=Strings[i];
  3082. end;
  3083. function TStrings.Add(const S: string): Integer;
  3084. begin
  3085. Result:=Count;
  3086. Insert (Count,S);
  3087. end;
  3088. function TStrings.Add(const Fmt: string; const Args: array of const): Integer;
  3089. begin
  3090. Result:=Add(Format(Fmt,Args));
  3091. end;
  3092. function TStrings.AddFmt(const Fmt: string; const Args: array of const): Integer;
  3093. begin
  3094. Result:=Add(Format(Fmt,Args));
  3095. end;
  3096. function TStrings.AddObject(const S: string; AObject: TObject): Integer;
  3097. begin
  3098. Result:=Add(S);
  3099. Objects[result]:=AObject;
  3100. end;
  3101. function TStrings.AddObject(const Fmt: string; Args: array of const; AObject: TObject): Integer;
  3102. begin
  3103. Result:=AddObject(Format(Fmt,Args),AObject);
  3104. end;
  3105. procedure TStrings.Append(const S: string);
  3106. begin
  3107. Add (S);
  3108. end;
  3109. procedure TStrings.AddStrings(TheStrings: TStrings; ClearFirst: Boolean);
  3110. begin
  3111. beginupdate;
  3112. try
  3113. if ClearFirst then
  3114. Clear;
  3115. AddStrings(TheStrings);
  3116. finally
  3117. EndUpdate;
  3118. end;
  3119. end;
  3120. procedure TStrings.AddStrings(TheStrings: TStrings);
  3121. Var Runner : longint;
  3122. begin
  3123. For Runner:=0 to TheStrings.Count-1 do
  3124. self.AddObject (Thestrings[Runner],TheStrings.Objects[Runner]);
  3125. end;
  3126. procedure TStrings.AddStrings(const TheStrings: array of string);
  3127. Var Runner : longint;
  3128. begin
  3129. if Count + High(TheStrings)+1 > Capacity then
  3130. Capacity := Count + High(TheStrings)+1;
  3131. For Runner:=Low(TheStrings) to High(TheStrings) do
  3132. self.Add(Thestrings[Runner]);
  3133. end;
  3134. procedure TStrings.AddStrings(const TheStrings: array of string; ClearFirst: Boolean);
  3135. begin
  3136. beginupdate;
  3137. try
  3138. if ClearFirst then
  3139. Clear;
  3140. AddStrings(TheStrings);
  3141. finally
  3142. EndUpdate;
  3143. end;
  3144. end;
  3145. function TStrings.AddPair(const AName, AValue: string): TStrings;
  3146. begin
  3147. Result:=AddPair(AName,AValue,Nil);
  3148. end;
  3149. function TStrings.AddPair(const AName, AValue: string; AObject: TObject): TStrings;
  3150. begin
  3151. Result := Self;
  3152. AddObject(AName+NameValueSeparator+AValue, AObject);
  3153. end;
  3154. procedure TStrings.Assign(Source: TPersistent);
  3155. Var
  3156. S : TStrings;
  3157. begin
  3158. If Source is TStrings then
  3159. begin
  3160. S:=TStrings(Source);
  3161. BeginUpdate;
  3162. Try
  3163. clear;
  3164. FSpecialCharsInited:=S.FSpecialCharsInited;
  3165. FQuoteChar:=S.FQuoteChar;
  3166. FDelimiter:=S.FDelimiter;
  3167. FNameValueSeparator:=S.FNameValueSeparator;
  3168. FLBS:=S.FLBS;
  3169. FLineBreak:=S.FLineBreak;
  3170. AddStrings(S);
  3171. finally
  3172. EndUpdate;
  3173. end;
  3174. end
  3175. else
  3176. Inherited Assign(Source);
  3177. end;
  3178. procedure TStrings.BeginUpdate;
  3179. begin
  3180. if FUpdateCount = 0 then SetUpdateState(true);
  3181. inc(FUpdateCount);
  3182. end;
  3183. procedure TStrings.EndUpdate;
  3184. begin
  3185. If FUpdateCount>0 then
  3186. Dec(FUpdateCount);
  3187. if FUpdateCount=0 then
  3188. SetUpdateState(False);
  3189. end;
  3190. function TStrings.Equals(Obj: TObject): Boolean;
  3191. begin
  3192. if Obj is TStrings then
  3193. Result := Equals(TStrings(Obj))
  3194. else
  3195. Result := inherited Equals(Obj);
  3196. end;
  3197. function TStrings.Equals(TheStrings: TStrings): Boolean;
  3198. Var Runner,Nr : Longint;
  3199. begin
  3200. Result:=False;
  3201. Nr:=Self.Count;
  3202. if Nr<>TheStrings.Count then exit;
  3203. For Runner:=0 to Nr-1 do
  3204. If Strings[Runner]<>TheStrings[Runner] then exit;
  3205. Result:=True;
  3206. end;
  3207. procedure TStrings.Exchange(Index1, Index2: Integer);
  3208. Var
  3209. Obj : TObject;
  3210. Str : String;
  3211. begin
  3212. beginUpdate;
  3213. Try
  3214. Obj:=Objects[Index1];
  3215. Str:=Strings[Index1];
  3216. Objects[Index1]:=Objects[Index2];
  3217. Strings[Index1]:=Strings[Index2];
  3218. Objects[Index2]:=Obj;
  3219. Strings[Index2]:=Str;
  3220. finally
  3221. EndUpdate;
  3222. end;
  3223. end;
  3224. function TStrings.GetEnumerator: TStringsEnumerator;
  3225. begin
  3226. Result:=TStringsEnumerator.Create(Self);
  3227. end;
  3228. function TStrings.DoCompareText(const s1, s2: string): PtrInt;
  3229. begin
  3230. result:=CompareText(s1,s2);
  3231. end;
  3232. function TStrings.IndexOf(const S: string): Integer;
  3233. begin
  3234. Result:=0;
  3235. While (Result<Count) and (DoCompareText(Strings[Result],S)<>0) do Result:=Result+1;
  3236. if Result=Count then Result:=-1;
  3237. end;
  3238. function TStrings.IndexOfName(const Name: string): Integer;
  3239. Var
  3240. len : longint;
  3241. S : String;
  3242. begin
  3243. CheckSpecialChars;
  3244. Result:=0;
  3245. while (Result<Count) do
  3246. begin
  3247. S:=Strings[Result];
  3248. len:=pos(FNameValueSeparator,S)-1;
  3249. if (len>=0) and (DoCompareText(Name,Copy(S,1,Len))=0) then
  3250. exit;
  3251. inc(result);
  3252. end;
  3253. result:=-1;
  3254. end;
  3255. function TStrings.IndexOfObject(AObject: TObject): Integer;
  3256. begin
  3257. Result:=0;
  3258. While (Result<count) and (Objects[Result]<>AObject) do Result:=Result+1;
  3259. If Result=Count then Result:=-1;
  3260. end;
  3261. procedure TStrings.InsertObject(Index: Integer; const S: string; AObject: TObject);
  3262. begin
  3263. Insert (Index,S);
  3264. Objects[Index]:=AObject;
  3265. end;
  3266. procedure TStrings.Move(CurIndex, NewIndex: Integer);
  3267. Var
  3268. Obj : TObject;
  3269. Str : String;
  3270. begin
  3271. BeginUpdate;
  3272. Try
  3273. Obj:=Objects[CurIndex];
  3274. Str:=Strings[CurIndex];
  3275. Objects[CurIndex]:=Nil; // Prevent Delete from freeing.
  3276. Delete(Curindex);
  3277. InsertObject(NewIndex,Str,Obj);
  3278. finally
  3279. EndUpdate;
  3280. end;
  3281. end;
  3282. {****************************************************************************}
  3283. {* TStringList *}
  3284. {****************************************************************************}
  3285. procedure TStringList.ExchangeItemsInt(Index1, Index2: Integer);
  3286. Var
  3287. S : String;
  3288. O : TObject;
  3289. begin
  3290. S:=Flist[Index1].FString;
  3291. O:=Flist[Index1].FObject;
  3292. Flist[Index1].Fstring:=Flist[Index2].Fstring;
  3293. Flist[Index1].FObject:=Flist[Index2].FObject;
  3294. Flist[Index2].Fstring:=S;
  3295. Flist[Index2].FObject:=O;
  3296. end;
  3297. function TStringList.GetSorted: Boolean;
  3298. begin
  3299. Result:=FSortStyle in [sslUser,sslAuto];
  3300. end;
  3301. procedure TStringList.ExchangeItems(Index1, Index2: Integer);
  3302. begin
  3303. ExchangeItemsInt(Index1, Index2);
  3304. end;
  3305. procedure TStringList.Grow;
  3306. Var
  3307. NC : Integer;
  3308. begin
  3309. NC:=Capacity;
  3310. If NC>=256 then
  3311. NC:=NC+(NC Div 4)
  3312. else if NC=0 then
  3313. NC:=4
  3314. else
  3315. NC:=NC*4;
  3316. SetCapacity(NC);
  3317. end;
  3318. procedure TStringList.InternalClear(FromIndex: Integer; ClearOnly: Boolean);
  3319. Var
  3320. I: Integer;
  3321. begin
  3322. if FromIndex < FCount then
  3323. begin
  3324. if FOwnsObjects then
  3325. begin
  3326. For I:=FromIndex to FCount-1 do
  3327. begin
  3328. Flist[I].FString:='';
  3329. freeandnil(Flist[i].FObject);
  3330. end;
  3331. end
  3332. else
  3333. begin
  3334. For I:=FromIndex to FCount-1 do
  3335. Flist[I].FString:='';
  3336. end;
  3337. FCount:=FromIndex;
  3338. end;
  3339. if Not ClearOnly then
  3340. SetCapacity(0);
  3341. end;
  3342. procedure TStringList.QuickSort(L, R: Integer; CompareFn: TStringListSortCompare
  3343. );
  3344. var
  3345. Pivot, vL, vR: Integer;
  3346. begin
  3347. //if ExchangeItems is override call that, else call (faster) ExchangeItemsInt
  3348. if R - L <= 1 then begin // a little bit of time saver
  3349. if L < R then
  3350. if CompareFn(Self, L, R) > 0 then
  3351. ExchangeItems(L, R);
  3352. Exit;
  3353. end;
  3354. vL := L;
  3355. vR := R;
  3356. Pivot := L + Random(R - L); // they say random is best
  3357. while vL < vR do begin
  3358. while (vL < Pivot) and (CompareFn(Self, vL, Pivot) <= 0) do
  3359. Inc(vL);
  3360. while (vR > Pivot) and (CompareFn(Self, vR, Pivot) > 0) do
  3361. Dec(vR);
  3362. ExchangeItems(vL, vR);
  3363. if Pivot = vL then // swap pivot if we just hit it from one side
  3364. Pivot := vR
  3365. else if Pivot = vR then
  3366. Pivot := vL;
  3367. end;
  3368. if Pivot - 1 >= L then
  3369. QuickSort(L, Pivot - 1, CompareFn);
  3370. if Pivot + 1 <= R then
  3371. QuickSort(Pivot + 1, R, CompareFn);
  3372. end;
  3373. procedure TStringList.InsertItem(Index: Integer; const S: string);
  3374. begin
  3375. InsertItem(Index, S, nil);
  3376. end;
  3377. procedure TStringList.InsertItem(Index: Integer; const S: string; O: TObject);
  3378. Var
  3379. It : TStringItem;
  3380. begin
  3381. Changing;
  3382. If FCount=Capacity then Grow;
  3383. it.FString:=S;
  3384. it.FObject:=O;
  3385. TJSArray(FList).Splice(Index,0,It);
  3386. Inc(FCount);
  3387. Changed;
  3388. end;
  3389. procedure TStringList.SetSorted(Value: Boolean);
  3390. begin
  3391. If Value then
  3392. SortStyle:=sslAuto
  3393. else
  3394. SortStyle:=sslNone
  3395. end;
  3396. procedure TStringList.Changed;
  3397. begin
  3398. If (FUpdateCount=0) Then
  3399. begin
  3400. If Assigned(FOnChange) then
  3401. FOnchange(Self);
  3402. end;
  3403. end;
  3404. procedure TStringList.Changing;
  3405. begin
  3406. If FUpdateCount=0 then
  3407. if Assigned(FOnChanging) then
  3408. FOnchanging(Self);
  3409. end;
  3410. function TStringList.Get(Index: Integer): string;
  3411. begin
  3412. CheckIndex(Index);
  3413. Result:=Flist[Index].FString;
  3414. end;
  3415. function TStringList.GetCapacity: Integer;
  3416. begin
  3417. Result:=Length(FList);
  3418. end;
  3419. function TStringList.GetCount: Integer;
  3420. begin
  3421. Result:=FCount;
  3422. end;
  3423. function TStringList.GetObject(Index: Integer): TObject;
  3424. begin
  3425. CheckIndex(Index);
  3426. Result:=Flist[Index].FObject;
  3427. end;
  3428. procedure TStringList.Put(Index: Integer; const S: string);
  3429. begin
  3430. If Sorted then
  3431. Error(SSortedListError,0);
  3432. CheckIndex(Index);
  3433. Changing;
  3434. Flist[Index].FString:=S;
  3435. Changed;
  3436. end;
  3437. procedure TStringList.PutObject(Index: Integer; AObject: TObject);
  3438. begin
  3439. CheckIndex(Index);
  3440. Changing;
  3441. Flist[Index].FObject:=AObject;
  3442. Changed;
  3443. end;
  3444. procedure TStringList.SetCapacity(NewCapacity: Integer);
  3445. begin
  3446. If (NewCapacity<0) then
  3447. Error (SListCapacityError,NewCapacity);
  3448. If NewCapacity<>Capacity then
  3449. SetLength(FList,NewCapacity)
  3450. end;
  3451. procedure TStringList.SetUpdateState(Updating: Boolean);
  3452. begin
  3453. If Updating then
  3454. Changing
  3455. else
  3456. Changed
  3457. end;
  3458. destructor TStringList.Destroy;
  3459. begin
  3460. InternalClear;
  3461. Inherited destroy;
  3462. end;
  3463. function TStringList.Add(const S: string): Integer;
  3464. begin
  3465. If Not (SortStyle=sslAuto) then
  3466. Result:=FCount
  3467. else
  3468. If Find (S,Result) then
  3469. Case DUplicates of
  3470. DupIgnore : Exit;
  3471. DupError : Error(SDuplicateString,0)
  3472. end;
  3473. InsertItem (Result,S);
  3474. end;
  3475. procedure TStringList.Clear;
  3476. begin
  3477. if FCount = 0 then Exit;
  3478. Changing;
  3479. InternalClear;
  3480. Changed;
  3481. end;
  3482. procedure TStringList.Delete(Index: Integer);
  3483. begin
  3484. CheckIndex(Index);
  3485. Changing;
  3486. if FOwnsObjects then
  3487. FreeAndNil(Flist[Index].FObject);
  3488. TJSArray(FList).splice(Index,1);
  3489. FList[Count-1].FString:='';
  3490. Flist[Count-1].FObject:=Nil;
  3491. Dec(FCount);
  3492. Changed;
  3493. end;
  3494. procedure TStringList.Exchange(Index1, Index2: Integer);
  3495. begin
  3496. CheckIndex(Index1);
  3497. CheckIndex(Index2);
  3498. Changing;
  3499. ExchangeItemsInt(Index1,Index2);
  3500. changed;
  3501. end;
  3502. procedure TStringList.SetCaseSensitive(b : boolean);
  3503. begin
  3504. if b=FCaseSensitive then
  3505. Exit;
  3506. FCaseSensitive:=b;
  3507. if FSortStyle=sslAuto then
  3508. begin
  3509. FForceSort:=True;
  3510. try
  3511. Sort;
  3512. finally
  3513. FForceSort:=False;
  3514. end;
  3515. end;
  3516. end;
  3517. procedure TStringList.SetSortStyle(AValue: TStringsSortStyle);
  3518. begin
  3519. if FSortStyle=AValue then Exit;
  3520. if (AValue=sslAuto) then
  3521. Sort;
  3522. FSortStyle:=AValue;
  3523. end;
  3524. procedure TStringList.CheckIndex(AIndex: Integer);
  3525. begin
  3526. If (AIndex<0) or (AIndex>=FCount) then
  3527. Error(SListIndexError,AIndex);
  3528. end;
  3529. function TStringList.DoCompareText(const s1, s2: string): PtrInt;
  3530. begin
  3531. if FCaseSensitive then
  3532. result:=CompareStr(s1,s2)
  3533. else
  3534. result:=CompareText(s1,s2);
  3535. end;
  3536. function TStringList.CompareStrings(const s1,s2 : string) : Integer;
  3537. begin
  3538. Result := DoCompareText(s1, s2);
  3539. end;
  3540. function TStringList.Find(const S: string; out Index: Integer): Boolean;
  3541. var
  3542. L, R, I: Integer;
  3543. CompareRes: PtrInt;
  3544. begin
  3545. Result := false;
  3546. Index:=-1;
  3547. if Not Sorted then
  3548. Raise EListError.Create(SErrFindNeedsSortedList);
  3549. // Use binary search.
  3550. L := 0;
  3551. R := Count - 1;
  3552. while (L<=R) do
  3553. begin
  3554. I := L + (R - L) div 2;
  3555. CompareRes := DoCompareText(S, Flist[I].FString);
  3556. if (CompareRes>0) then
  3557. L := I+1
  3558. else begin
  3559. R := I-1;
  3560. if (CompareRes=0) then begin
  3561. Result := true;
  3562. if (Duplicates<>dupAccept) then
  3563. L := I; // forces end of while loop
  3564. end;
  3565. end;
  3566. end;
  3567. Index := L;
  3568. end;
  3569. function TStringList.IndexOf(const S: string): Integer;
  3570. begin
  3571. If Not Sorted then
  3572. Result:=Inherited indexOf(S)
  3573. else
  3574. // faster using binary search...
  3575. If Not Find (S,Result) then
  3576. Result:=-1;
  3577. end;
  3578. procedure TStringList.Insert(Index: Integer; const S: string);
  3579. begin
  3580. If SortStyle=sslAuto then
  3581. Error (SSortedListError,0)
  3582. else
  3583. begin
  3584. If (Index<0) or (Index>FCount) then
  3585. Error(SListIndexError,Index); // Cannot use CheckIndex, because there >= FCount...
  3586. InsertItem (Index,S);
  3587. end;
  3588. end;
  3589. procedure TStringList.CustomSort(CompareFn: TStringListSortCompare);
  3590. begin
  3591. If (FForceSort or (Not (FSortStyle=sslAuto))) and (FCount>1) then
  3592. begin
  3593. Changing;
  3594. QuickSort(0,FCount-1, CompareFn);
  3595. Changed;
  3596. end;
  3597. end;
  3598. function StringListAnsiCompare(List: TStringList; Index1, Index: Integer): Integer;
  3599. begin
  3600. Result := List.DoCompareText(List.FList[Index1].FString,
  3601. List.FList[Index].FString);
  3602. end;
  3603. procedure TStringList.Sort;
  3604. begin
  3605. CustomSort(@StringListAnsiCompare);
  3606. end;
  3607. {****************************************************************************}
  3608. {* TCollectionItem *}
  3609. {****************************************************************************}
  3610. function TCollectionItem.GetIndex: Integer;
  3611. begin
  3612. if Assigned(FCollection) then
  3613. Result:=FCollection.FItems.IndexOf(Self)
  3614. else
  3615. Result:=-1;
  3616. end;
  3617. procedure TCollectionItem.SetCollection(Value: TCollection);
  3618. begin
  3619. IF Value<>FCollection then
  3620. begin
  3621. if Assigned(FCollection) then FCollection.RemoveItem(Self);
  3622. if Assigned(Value) then Value.InsertItem(Self);
  3623. end;
  3624. end;
  3625. procedure TCollectionItem.Changed(AllItems: Boolean);
  3626. begin
  3627. If (FCollection<>Nil) and (FCollection.UpdateCount=0) then
  3628. begin
  3629. If AllItems then
  3630. FCollection.Update(Nil)
  3631. else
  3632. FCollection.Update(Self);
  3633. end;
  3634. end;
  3635. function TCollectionItem.GetNamePath: string;
  3636. begin
  3637. If FCollection<>Nil then
  3638. Result:=FCollection.GetNamePath+'['+IntToStr(Index)+']'
  3639. else
  3640. Result:=ClassName;
  3641. end;
  3642. function TCollectionItem.GetOwner: TPersistent;
  3643. begin
  3644. Result:=FCollection;
  3645. end;
  3646. function TCollectionItem.GetDisplayName: string;
  3647. begin
  3648. Result:=ClassName;
  3649. end;
  3650. procedure TCollectionItem.SetIndex(Value: Integer);
  3651. Var Temp : Longint;
  3652. begin
  3653. Temp:=GetIndex;
  3654. If (Temp>-1) and (Temp<>Value) then
  3655. begin
  3656. FCollection.FItems.Move(Temp,Value);
  3657. Changed(True);
  3658. end;
  3659. end;
  3660. procedure TCollectionItem.SetDisplayName(const Value: string);
  3661. begin
  3662. Changed(False);
  3663. if Value='' then ;
  3664. end;
  3665. constructor TCollectionItem.Create(ACollection: TCollection);
  3666. begin
  3667. Inherited Create;
  3668. SetCollection(ACollection);
  3669. end;
  3670. destructor TCollectionItem.Destroy;
  3671. begin
  3672. SetCollection(Nil);
  3673. Inherited Destroy;
  3674. end;
  3675. {****************************************************************************}
  3676. {* TCollectionEnumerator *}
  3677. {****************************************************************************}
  3678. constructor TCollectionEnumerator.Create(ACollection: TCollection);
  3679. begin
  3680. inherited Create;
  3681. FCollection := ACollection;
  3682. FPosition := -1;
  3683. end;
  3684. function TCollectionEnumerator.GetCurrent: TCollectionItem;
  3685. begin
  3686. Result := FCollection.Items[FPosition];
  3687. end;
  3688. function TCollectionEnumerator.MoveNext: Boolean;
  3689. begin
  3690. Inc(FPosition);
  3691. Result := FPosition < FCollection.Count;
  3692. end;
  3693. {****************************************************************************}
  3694. {* TCollection *}
  3695. {****************************************************************************}
  3696. function TCollection.Owner: TPersistent;
  3697. begin
  3698. result:=getowner;
  3699. end;
  3700. function TCollection.GetCount: Integer;
  3701. begin
  3702. Result:=FItems.Count;
  3703. end;
  3704. Procedure TCollection.SetPropName;
  3705. {
  3706. Var
  3707. TheOwner : TPersistent;
  3708. PropList : PPropList;
  3709. I, PropCount : Integer;
  3710. }
  3711. begin
  3712. FPropName:='';
  3713. {
  3714. TheOwner:=GetOwner;
  3715. // TODO: This needs to wait till Mattias finishes typeinfo.
  3716. // It's normally only used in the designer so should not be a problem currently.
  3717. if (TheOwner=Nil) Or (TheOwner.Classinfo=Nil) Then Exit;
  3718. // get information from the owner RTTI
  3719. PropCount:=GetPropList(TheOwner, PropList);
  3720. Try
  3721. For I:=0 To PropCount-1 Do
  3722. If (PropList^[i]^.PropType^.Kind=tkClass) And
  3723. (GetObjectProp(TheOwner, PropList^[i], ClassType)=Self) Then
  3724. Begin
  3725. FPropName:=PropList^[i]^.Name;
  3726. Exit;
  3727. End;
  3728. Finally
  3729. FreeMem(PropList);
  3730. End;
  3731. }
  3732. end;
  3733. function TCollection.GetPropName: string;
  3734. {Var
  3735. TheOwner : TPersistent;}
  3736. begin
  3737. Result:=FPropNAme;
  3738. // TheOwner:=GetOwner;
  3739. // If (Result<>'') or (TheOwner=Nil) Or (TheOwner.Classinfo=Nil) then exit;
  3740. SetPropName;
  3741. Result:=FPropName;
  3742. end;
  3743. procedure TCollection.InsertItem(Item: TCollectionItem);
  3744. begin
  3745. If Not(Item Is FitemClass) then
  3746. exit;
  3747. FItems.add(Item);
  3748. Item.FCollection:=Self;
  3749. Item.FID:=FNextID;
  3750. inc(FNextID);
  3751. SetItemName(Item);
  3752. Notify(Item,cnAdded);
  3753. Changed;
  3754. end;
  3755. procedure TCollection.RemoveItem(Item: TCollectionItem);
  3756. Var
  3757. I : Integer;
  3758. begin
  3759. Notify(Item,cnExtracting);
  3760. I:=FItems.IndexOfItem(Item,fromEnd);
  3761. If (I<>-1) then
  3762. FItems.Delete(I);
  3763. Item.FCollection:=Nil;
  3764. Changed;
  3765. end;
  3766. function TCollection.GetAttrCount: Integer;
  3767. begin
  3768. Result:=0;
  3769. end;
  3770. function TCollection.GetAttr(Index: Integer): string;
  3771. begin
  3772. Result:='';
  3773. if Index=0 then ;
  3774. end;
  3775. function TCollection.GetItemAttr(Index, ItemIndex: Integer): string;
  3776. begin
  3777. Result:=TCollectionItem(FItems.Items[ItemIndex]).DisplayName;
  3778. if Index=0 then ;
  3779. end;
  3780. function TCollection.GetEnumerator: TCollectionEnumerator;
  3781. begin
  3782. Result := TCollectionEnumerator.Create(Self);
  3783. end;
  3784. function TCollection.GetNamePath: string;
  3785. var o : TPersistent;
  3786. begin
  3787. o:=getowner;
  3788. if assigned(o) and (propname<>'') then
  3789. result:=o.getnamepath+'.'+propname
  3790. else
  3791. result:=classname;
  3792. end;
  3793. procedure TCollection.Changed;
  3794. begin
  3795. if FUpdateCount=0 then
  3796. Update(Nil);
  3797. end;
  3798. function TCollection.GetItem(Index: Integer): TCollectionItem;
  3799. begin
  3800. Result:=TCollectionItem(FItems.Items[Index]);
  3801. end;
  3802. procedure TCollection.SetItem(Index: Integer; Value: TCollectionItem);
  3803. begin
  3804. TCollectionItem(FItems.items[Index]).Assign(Value);
  3805. end;
  3806. procedure TCollection.SetItemName(Item: TCollectionItem);
  3807. begin
  3808. if Item=nil then ;
  3809. end;
  3810. procedure TCollection.Update(Item: TCollectionItem);
  3811. begin
  3812. if Item=nil then ;
  3813. end;
  3814. constructor TCollection.Create(AItemClass: TCollectionItemClass);
  3815. begin
  3816. inherited create;
  3817. FItemClass:=AItemClass;
  3818. FItems:=TFpList.Create;
  3819. end;
  3820. destructor TCollection.Destroy;
  3821. begin
  3822. FUpdateCount:=1; // Prevent OnChange
  3823. try
  3824. DoClear;
  3825. Finally
  3826. FUpdateCount:=0;
  3827. end;
  3828. if assigned(FItems) then
  3829. FItems.Destroy;
  3830. Inherited Destroy;
  3831. end;
  3832. function TCollection.Add: TCollectionItem;
  3833. begin
  3834. Result:=FItemClass.Create(Self);
  3835. end;
  3836. procedure TCollection.Assign(Source: TPersistent);
  3837. Var I : Longint;
  3838. begin
  3839. If Source is TCollection then
  3840. begin
  3841. Clear;
  3842. For I:=0 To TCollection(Source).Count-1 do
  3843. Add.Assign(TCollection(Source).Items[I]);
  3844. exit;
  3845. end
  3846. else
  3847. Inherited Assign(Source);
  3848. end;
  3849. procedure TCollection.BeginUpdate;
  3850. begin
  3851. inc(FUpdateCount);
  3852. end;
  3853. procedure TCollection.Clear;
  3854. begin
  3855. if FItems.Count=0 then
  3856. exit; // Prevent Changed
  3857. BeginUpdate;
  3858. try
  3859. DoClear;
  3860. finally
  3861. EndUpdate;
  3862. end;
  3863. end;
  3864. procedure TCollection.DoClear;
  3865. var
  3866. Item: TCollectionItem;
  3867. begin
  3868. While FItems.Count>0 do
  3869. begin
  3870. Item:=TCollectionItem(FItems.Last);
  3871. if Assigned(Item) then
  3872. Item.Destroy;
  3873. end;
  3874. end;
  3875. procedure TCollection.EndUpdate;
  3876. begin
  3877. if FUpdateCount>0 then
  3878. dec(FUpdateCount);
  3879. if FUpdateCount=0 then
  3880. Changed;
  3881. end;
  3882. function TCollection.FindItemID(ID: Integer): TCollectionItem;
  3883. Var
  3884. I : Longint;
  3885. begin
  3886. For I:=0 to Fitems.Count-1 do
  3887. begin
  3888. Result:=TCollectionItem(FItems.items[I]);
  3889. If Result.Id=Id then
  3890. exit;
  3891. end;
  3892. Result:=Nil;
  3893. end;
  3894. procedure TCollection.Delete(Index: Integer);
  3895. Var
  3896. Item : TCollectionItem;
  3897. begin
  3898. Item:=TCollectionItem(FItems[Index]);
  3899. Notify(Item,cnDeleting);
  3900. If assigned(Item) then
  3901. Item.Destroy;
  3902. end;
  3903. function TCollection.Insert(Index: Integer): TCollectionItem;
  3904. begin
  3905. Result:=Add;
  3906. Result.Index:=Index;
  3907. end;
  3908. procedure TCollection.Notify(Item: TCollectionItem;Action: TCollectionNotification);
  3909. begin
  3910. if Item=nil then ;
  3911. if Action=cnAdded then ;
  3912. end;
  3913. procedure TCollection.Sort(Const Compare : TCollectionSortCompare);
  3914. begin
  3915. BeginUpdate;
  3916. try
  3917. FItems.Sort(TListSortCompare(Compare));
  3918. Finally
  3919. EndUpdate;
  3920. end;
  3921. end;
  3922. procedure TCollection.SortList(const Compare: TCollectionSortCompareFunc);
  3923. begin
  3924. BeginUpdate;
  3925. try
  3926. FItems.SortList(TListSortCompareFunc(Compare));
  3927. Finally
  3928. EndUpdate;
  3929. end;
  3930. end;
  3931. procedure TCollection.Exchange(Const Index1, index2: integer);
  3932. begin
  3933. FItems.Exchange(Index1,Index2);
  3934. end;
  3935. {****************************************************************************}
  3936. {* TOwnedCollection *}
  3937. {****************************************************************************}
  3938. Constructor TOwnedCollection.Create(AOwner: TPersistent; AItemClass: TCollectionItemClass);
  3939. Begin
  3940. FOwner := AOwner;
  3941. inherited Create(AItemClass);
  3942. end;
  3943. Function TOwnedCollection.GetOwner: TPersistent;
  3944. begin
  3945. Result:=FOwner;
  3946. end;
  3947. {****************************************************************************}
  3948. {* TComponent *}
  3949. {****************************************************************************}
  3950. function TComponent.GetComponent(AIndex: Integer): TComponent;
  3951. begin
  3952. If not assigned(FComponents) then
  3953. Result:=Nil
  3954. else
  3955. Result:=TComponent(FComponents.Items[Aindex]);
  3956. end;
  3957. function TComponent.GetComponentCount: Integer;
  3958. begin
  3959. If not assigned(FComponents) then
  3960. result:=0
  3961. else
  3962. Result:=FComponents.Count;
  3963. end;
  3964. function TComponent.GetComponentIndex: Integer;
  3965. begin
  3966. If Assigned(FOwner) and Assigned(FOwner.FComponents) then
  3967. Result:=FOWner.FComponents.IndexOf(Self)
  3968. else
  3969. Result:=-1;
  3970. end;
  3971. procedure TComponent.Insert(AComponent: TComponent);
  3972. begin
  3973. If not assigned(FComponents) then
  3974. FComponents:=TFpList.Create;
  3975. FComponents.Add(AComponent);
  3976. AComponent.FOwner:=Self;
  3977. end;
  3978. procedure TComponent.ReadLeft(AReader: TReader);
  3979. begin
  3980. FDesignInfo := (FDesignInfo and $ffff0000) or (AReader.ReadInteger and $ffff);
  3981. end;
  3982. procedure TComponent.ReadTop(AReader: TReader);
  3983. begin
  3984. FDesignInfo := ((AReader.ReadInteger and $ffff) shl 16) or (FDesignInfo and $ffff);
  3985. end;
  3986. procedure TComponent.Remove(AComponent: TComponent);
  3987. begin
  3988. AComponent.FOwner:=Nil;
  3989. If assigned(FCOmponents) then
  3990. begin
  3991. FComponents.Remove(AComponent);
  3992. IF FComponents.Count=0 then
  3993. begin
  3994. FComponents.Destroy;
  3995. FComponents:=Nil;
  3996. end;
  3997. end;
  3998. end;
  3999. procedure TComponent.RemoveNotification(AComponent: TComponent);
  4000. begin
  4001. if FFreeNotifies<>nil then
  4002. begin
  4003. FFreeNotifies.Remove(AComponent);
  4004. if FFreeNotifies.Count=0 then
  4005. begin
  4006. FFreeNotifies.Destroy;
  4007. FFreeNotifies:=nil;
  4008. Exclude(FComponentState,csFreeNotification);
  4009. end;
  4010. end;
  4011. end;
  4012. procedure TComponent.SetComponentIndex(Value: Integer);
  4013. Var Temp,Count : longint;
  4014. begin
  4015. If Not assigned(Fowner) then exit;
  4016. Temp:=getcomponentindex;
  4017. If temp<0 then exit;
  4018. If value<0 then value:=0;
  4019. Count:=Fowner.FComponents.Count;
  4020. If Value>=Count then value:=count-1;
  4021. If Value<>Temp then
  4022. begin
  4023. FOWner.FComponents.Delete(Temp);
  4024. FOwner.FComponents.Insert(Value,Self);
  4025. end;
  4026. end;
  4027. procedure TComponent.ChangeName(const NewName: TComponentName);
  4028. begin
  4029. FName:=NewName;
  4030. end;
  4031. procedure TComponent.DefineProperties(Filer: TFiler);
  4032. var
  4033. Temp: LongInt;
  4034. Ancestor: TComponent;
  4035. begin
  4036. Ancestor := TComponent(Filer.Ancestor);
  4037. if Assigned(Ancestor) then
  4038. Temp := Ancestor.FDesignInfo
  4039. else
  4040. Temp := 0;
  4041. Filer.DefineProperty('Left', @ReadLeft, @WriteLeft, (FDesignInfo and $ffff) <> (Temp and $ffff));
  4042. Filer.DefineProperty('Top', @ReadTop, @WriteTop, (FDesignInfo and $ffff0000) <> (Temp and $ffff0000));
  4043. end;
  4044. procedure TComponent.GetChildren(Proc: TGetChildProc; Root: TComponent);
  4045. begin
  4046. // Does nothing.
  4047. if Proc=nil then ;
  4048. if Root=nil then ;
  4049. end;
  4050. function TComponent.GetChildOwner: TComponent;
  4051. begin
  4052. Result:=Nil;
  4053. end;
  4054. function TComponent.GetChildParent: TComponent;
  4055. begin
  4056. Result:=Self;
  4057. end;
  4058. function TComponent.GetNamePath: string;
  4059. begin
  4060. Result:=FName;
  4061. end;
  4062. function TComponent.GetOwner: TPersistent;
  4063. begin
  4064. Result:=FOwner;
  4065. end;
  4066. procedure TComponent.Loaded;
  4067. begin
  4068. Exclude(FComponentState,csLoading);
  4069. end;
  4070. procedure TComponent.Loading;
  4071. begin
  4072. Include(FComponentState,csLoading);
  4073. end;
  4074. procedure TComponent.SetWriting(Value: Boolean);
  4075. begin
  4076. If Value then
  4077. Include(FComponentState,csWriting)
  4078. else
  4079. Exclude(FComponentState,csWriting);
  4080. end;
  4081. procedure TComponent.SetReading(Value: Boolean);
  4082. begin
  4083. If Value then
  4084. Include(FComponentState,csReading)
  4085. else
  4086. Exclude(FComponentState,csReading);
  4087. end;
  4088. procedure TComponent.Notification(AComponent: TComponent; Operation: TOperation);
  4089. Var
  4090. C : Longint;
  4091. begin
  4092. If (Operation=opRemove) then
  4093. RemoveFreeNotification(AComponent);
  4094. If Not assigned(FComponents) then
  4095. exit;
  4096. C:=FComponents.Count-1;
  4097. While (C>=0) do
  4098. begin
  4099. TComponent(FComponents.Items[C]).Notification(AComponent,Operation);
  4100. Dec(C);
  4101. if C>=FComponents.Count then
  4102. C:=FComponents.Count-1;
  4103. end;
  4104. end;
  4105. procedure TComponent.PaletteCreated;
  4106. begin
  4107. end;
  4108. procedure TComponent.ReadState(Reader: TReader);
  4109. begin
  4110. Reader.ReadData(Self);
  4111. end;
  4112. procedure TComponent.SetAncestor(Value: Boolean);
  4113. Var Runner : Longint;
  4114. begin
  4115. If Value then
  4116. Include(FComponentState,csAncestor)
  4117. else
  4118. Exclude(FCOmponentState,csAncestor);
  4119. if Assigned(FComponents) then
  4120. For Runner:=0 To FComponents.Count-1 do
  4121. TComponent(FComponents.Items[Runner]).SetAncestor(Value);
  4122. end;
  4123. procedure TComponent.SetDesigning(Value: Boolean; SetChildren: Boolean);
  4124. Var Runner : Longint;
  4125. begin
  4126. If Value then
  4127. Include(FComponentState,csDesigning)
  4128. else
  4129. Exclude(FComponentState,csDesigning);
  4130. if Assigned(FComponents) and SetChildren then
  4131. For Runner:=0 To FComponents.Count - 1 do
  4132. TComponent(FComponents.items[Runner]).SetDesigning(Value);
  4133. end;
  4134. procedure TComponent.SetDesignInstance(Value: Boolean);
  4135. begin
  4136. If Value then
  4137. Include(FComponentState,csDesignInstance)
  4138. else
  4139. Exclude(FComponentState,csDesignInstance);
  4140. end;
  4141. procedure TComponent.SetInline(Value: Boolean);
  4142. begin
  4143. If Value then
  4144. Include(FComponentState,csInline)
  4145. else
  4146. Exclude(FComponentState,csInline);
  4147. end;
  4148. procedure TComponent.SetName(const NewName: TComponentName);
  4149. begin
  4150. If FName=NewName then exit;
  4151. If (NewName<>'') and not IsValidIdent(NewName) then
  4152. Raise EComponentError.CreateFmt(SInvalidName,[NewName]);
  4153. If Assigned(FOwner) Then
  4154. FOwner.ValidateRename(Self,FName,NewName)
  4155. else
  4156. ValidateRename(Nil,FName,NewName);
  4157. SetReference(False);
  4158. ChangeName(NewName);
  4159. SetReference(True);
  4160. end;
  4161. procedure TComponent.SetChildOrder(Child: TComponent; Order: Integer);
  4162. begin
  4163. // does nothing
  4164. if Child=nil then ;
  4165. if Order=0 then ;
  4166. end;
  4167. procedure TComponent.SetParentComponent(Value: TComponent);
  4168. begin
  4169. // Does nothing
  4170. if Value=nil then ;
  4171. end;
  4172. procedure TComponent.Updating;
  4173. begin
  4174. Include (FComponentState,csUpdating);
  4175. end;
  4176. procedure TComponent.Updated;
  4177. begin
  4178. Exclude(FComponentState,csUpdating);
  4179. end;
  4180. procedure TComponent.ValidateRename(AComponent: TComponent; const CurName, NewName: string);
  4181. begin
  4182. //!! This contradicts the Delphi manual.
  4183. If (AComponent<>Nil) and (CompareText(CurName,NewName)<>0) and (AComponent.Owner = Self) and
  4184. (FindComponent(NewName)<>Nil) then
  4185. raise EComponentError.Createfmt(SDuplicateName,[newname]);
  4186. If (csDesigning in FComponentState) and (FOwner<>Nil) then
  4187. FOwner.ValidateRename(AComponent,Curname,Newname);
  4188. end;
  4189. Procedure TComponent.SetReference(Enable: Boolean);
  4190. var
  4191. aField, aValue, aOwner : Pointer;
  4192. begin
  4193. if Name='' then
  4194. exit;
  4195. if Assigned(Owner) then
  4196. begin
  4197. aOwner:=Owner; // so as not to depend on low-level names
  4198. aField := Owner.FieldAddress(Name);
  4199. if Assigned(aField) then
  4200. begin
  4201. if Enable then
  4202. aValue:= Self
  4203. else
  4204. aValue := nil;
  4205. TJSObject(aOwner)[String(TJSObject(aField)['name'])]:=aValue;
  4206. end;
  4207. end;
  4208. end;
  4209. procedure TComponent.WriteLeft(AWriter: TWriter);
  4210. begin
  4211. AWriter.WriteInteger(FDesignInfo and $ffff);
  4212. end;
  4213. procedure TComponent.WriteTop(AWriter: TWriter);
  4214. begin
  4215. AWriter.WriteInteger((FDesignInfo shr 16) and $ffff);
  4216. end;
  4217. procedure TComponent.ValidateContainer(AComponent: TComponent);
  4218. begin
  4219. AComponent.ValidateInsert(Self);
  4220. end;
  4221. procedure TComponent.ValidateInsert(AComponent: TComponent);
  4222. begin
  4223. // Does nothing.
  4224. if AComponent=nil then ;
  4225. end;
  4226. function TComponent._AddRef: Integer;
  4227. begin
  4228. Result:=-1;
  4229. end;
  4230. function TComponent._Release: Integer;
  4231. begin
  4232. Result:=-1;
  4233. end;
  4234. constructor TComponent.Create(AOwner: TComponent);
  4235. begin
  4236. FComponentStyle:=[csInheritable];
  4237. If Assigned(AOwner) then AOwner.InsertComponent(Self);
  4238. end;
  4239. destructor TComponent.Destroy;
  4240. Var
  4241. I : Integer;
  4242. C : TComponent;
  4243. begin
  4244. Destroying;
  4245. If Assigned(FFreeNotifies) then
  4246. begin
  4247. I:=FFreeNotifies.Count-1;
  4248. While (I>=0) do
  4249. begin
  4250. C:=TComponent(FFreeNotifies.Items[I]);
  4251. // Delete, so one component is not notified twice, if it is owned.
  4252. FFreeNotifies.Delete(I);
  4253. C.Notification (self,opRemove);
  4254. If (FFreeNotifies=Nil) then
  4255. I:=0
  4256. else if (I>FFreeNotifies.Count) then
  4257. I:=FFreeNotifies.Count;
  4258. dec(i);
  4259. end;
  4260. FreeAndNil(FFreeNotifies);
  4261. end;
  4262. DestroyComponents;
  4263. If FOwner<>Nil Then FOwner.RemoveComponent(Self);
  4264. inherited destroy;
  4265. end;
  4266. procedure TComponent.BeforeDestruction;
  4267. begin
  4268. if not(csDestroying in FComponentstate) then
  4269. Destroying;
  4270. end;
  4271. procedure TComponent.DestroyComponents;
  4272. Var acomponent: TComponent;
  4273. begin
  4274. While assigned(FComponents) do
  4275. begin
  4276. aComponent:=TComponent(FComponents.Last);
  4277. Remove(aComponent);
  4278. Acomponent.Destroy;
  4279. end;
  4280. end;
  4281. procedure TComponent.Destroying;
  4282. Var Runner : longint;
  4283. begin
  4284. If csDestroying in FComponentstate Then Exit;
  4285. include (FComponentState,csDestroying);
  4286. If Assigned(FComponents) then
  4287. for Runner:=0 to FComponents.Count-1 do
  4288. TComponent(FComponents.Items[Runner]).Destroying;
  4289. end;
  4290. function TComponent.QueryInterface(const IID: TGUID; out Obj): HRESULT;
  4291. begin
  4292. if GetInterface(IID, Obj) then
  4293. Result := S_OK
  4294. else
  4295. Result := E_NOINTERFACE;
  4296. end;
  4297. procedure TComponent.WriteState(Writer: TWriter);
  4298. begin
  4299. Writer.WriteComponentData(Self);
  4300. end;
  4301. function TComponent.FindComponent(const AName: string): TComponent;
  4302. Var I : longint;
  4303. begin
  4304. Result:=Nil;
  4305. If (AName='') or Not assigned(FComponents) then exit;
  4306. For i:=0 to FComponents.Count-1 do
  4307. if (CompareText(TComponent(FComponents[I]).Name,AName)=0) then
  4308. begin
  4309. Result:=TComponent(FComponents.Items[I]);
  4310. exit;
  4311. end;
  4312. end;
  4313. procedure TComponent.FreeNotification(AComponent: TComponent);
  4314. begin
  4315. If (Owner<>Nil) and (AComponent=Owner) then exit;
  4316. If not (Assigned(FFreeNotifies)) then
  4317. FFreeNotifies:=TFpList.Create;
  4318. If FFreeNotifies.IndexOf(AComponent)=-1 then
  4319. begin
  4320. FFreeNotifies.Add(AComponent);
  4321. AComponent.FreeNotification (self);
  4322. end;
  4323. end;
  4324. procedure TComponent.RemoveFreeNotification(AComponent: TComponent);
  4325. begin
  4326. RemoveNotification(AComponent);
  4327. AComponent.RemoveNotification (self);
  4328. end;
  4329. function TComponent.GetParentComponent: TComponent;
  4330. begin
  4331. Result:=Nil;
  4332. end;
  4333. function TComponent.HasParent: Boolean;
  4334. begin
  4335. Result:=False;
  4336. end;
  4337. procedure TComponent.InsertComponent(AComponent: TComponent);
  4338. begin
  4339. AComponent.ValidateContainer(Self);
  4340. ValidateRename(AComponent,'',AComponent.FName);
  4341. Insert(AComponent);
  4342. If csDesigning in FComponentState then
  4343. AComponent.SetDesigning(true);
  4344. Notification(AComponent,opInsert);
  4345. end;
  4346. procedure TComponent.RemoveComponent(AComponent: TComponent);
  4347. begin
  4348. Notification(AComponent,opRemove);
  4349. Remove(AComponent);
  4350. Acomponent.Setdesigning(False);
  4351. ValidateRename(AComponent,AComponent.FName,'');
  4352. end;
  4353. procedure TComponent.SetSubComponent(ASubComponent: Boolean);
  4354. begin
  4355. if ASubComponent then
  4356. Include(FComponentStyle, csSubComponent)
  4357. else
  4358. Exclude(FComponentStyle, csSubComponent);
  4359. end;
  4360. function TComponent.GetEnumerator: TComponentEnumerator;
  4361. begin
  4362. Result:=TComponentEnumerator.Create(Self);
  4363. end;
  4364. { ---------------------------------------------------------------------
  4365. TStream
  4366. ---------------------------------------------------------------------}
  4367. Resourcestring
  4368. SStreamInvalidSeek = 'Seek is not implemented for class %s';
  4369. SStreamNoReading = 'Stream reading is not implemented for class %s';
  4370. SStreamNoWriting = 'Stream writing is not implemented for class %s';
  4371. SReadError = 'Could not read data from stream';
  4372. SWriteError = 'Could not write data to stream';
  4373. SMemoryStreamError = 'Could not allocate memory';
  4374. SerrInvalidStreamSize = 'Invalid Stream size';
  4375. procedure TStream.ReadNotImplemented;
  4376. begin
  4377. raise EStreamError.CreateFmt(SStreamNoReading, [ClassName]);
  4378. end;
  4379. procedure TStream.WriteNotImplemented;
  4380. begin
  4381. raise EStreamError.CreateFmt(SStreamNoWriting, [ClassName]);
  4382. end;
  4383. function TStream.Read(var Buffer: TBytes; Count: Longint): Longint;
  4384. begin
  4385. Result:=Read(Buffer,0,Count);
  4386. end;
  4387. function TStream.Write(const Buffer: TBytes; Count: Longint): Longint;
  4388. begin
  4389. Result:=Self.Write(Buffer,0,Count);
  4390. end;
  4391. function TStream.GetPosition: NativeInt;
  4392. begin
  4393. Result:=Seek(0,soCurrent);
  4394. end;
  4395. procedure TStream.SetPosition(const Pos: NativeInt);
  4396. begin
  4397. Seek(pos,soBeginning);
  4398. end;
  4399. procedure TStream.SetSize64(const NewSize: NativeInt);
  4400. begin
  4401. // Required because can't use overloaded functions in properties
  4402. SetSize(NewSize);
  4403. end;
  4404. function TStream.GetSize: NativeInt;
  4405. var
  4406. p : NativeInt;
  4407. begin
  4408. p:=Seek(0,soCurrent);
  4409. GetSize:=Seek(0,soEnd);
  4410. Seek(p,soBeginning);
  4411. end;
  4412. procedure TStream.SetSize(const NewSize: NativeInt);
  4413. begin
  4414. if NewSize<0 then
  4415. Raise EStreamError.Create(SerrInvalidStreamSize);
  4416. end;
  4417. procedure TStream.Discard(const Count: NativeInt);
  4418. const
  4419. CSmallSize =255;
  4420. CLargeMaxBuffer =32*1024; // 32 KiB
  4421. var
  4422. Buffer: TBytes;
  4423. begin
  4424. if Count=0 then
  4425. Exit;
  4426. if (Count<=CSmallSize) then
  4427. begin
  4428. SetLength(Buffer,CSmallSize);
  4429. ReadBuffer(Buffer,Count)
  4430. end
  4431. else
  4432. DiscardLarge(Count,CLargeMaxBuffer);
  4433. end;
  4434. procedure TStream.DiscardLarge(Count: NativeInt; const MaxBufferSize: Longint);
  4435. var
  4436. Buffer: TBytes;
  4437. begin
  4438. if Count=0 then
  4439. Exit;
  4440. if Count>MaxBufferSize then
  4441. SetLength(Buffer,MaxBufferSize)
  4442. else
  4443. SetLength(Buffer,Count);
  4444. while (Count>=Length(Buffer)) do
  4445. begin
  4446. ReadBuffer(Buffer,Length(Buffer));
  4447. Dec(Count,Length(Buffer));
  4448. end;
  4449. if Count>0 then
  4450. ReadBuffer(Buffer,Count);
  4451. end;
  4452. procedure TStream.InvalidSeek;
  4453. begin
  4454. raise EStreamError.CreateFmt(SStreamInvalidSeek, [ClassName]);
  4455. end;
  4456. procedure TStream.FakeSeekForward(Offset: NativeInt; const Origin: TSeekOrigin; const Pos: NativeInt);
  4457. begin
  4458. if Origin=soBeginning then
  4459. Dec(Offset,Pos);
  4460. if (Offset<0) or (Origin=soEnd) then
  4461. InvalidSeek;
  4462. if Offset>0 then
  4463. Discard(Offset);
  4464. end;
  4465. function TStream.ReadData({var} Buffer: TBytes; Count: NativeInt): NativeInt;
  4466. begin
  4467. Result:=Read(Buffer,0,Count);
  4468. end;
  4469. function TStream.ReadMaxSizeData(Buffer : TBytes; aSize,aCount : NativeInt) : NativeInt;
  4470. Var
  4471. CP : NativeInt;
  4472. begin
  4473. if aCount<=aSize then
  4474. Result:=read(Buffer,aCount)
  4475. else
  4476. begin
  4477. Result:=Read(Buffer,aSize);
  4478. CP:=Position;
  4479. Result:=Result+Seek(aCount-aSize,soCurrent)-CP;
  4480. end
  4481. end;
  4482. function TStream.WriteMaxSizeData(const Buffer : TBytes; aSize,aCount : NativeInt) : NativeInt;
  4483. Var
  4484. CP : NativeInt;
  4485. begin
  4486. if aCount<=aSize then
  4487. Result:=Self.Write(Buffer,aCount)
  4488. else
  4489. begin
  4490. Result:=Self.Write(Buffer,aSize);
  4491. CP:=Position;
  4492. Result:=Result+Seek(aCount-aSize,soCurrent)-CP;
  4493. end
  4494. end;
  4495. procedure TStream.WriteExactSizeData(const Buffer : TBytes; aSize, aCount: NativeInt);
  4496. begin
  4497. // Embarcadero docs mentions no exception. Does not seem very logical
  4498. WriteMaxSizeData(Buffer,aSize,ACount);
  4499. end;
  4500. procedure TStream.ReadExactSizeData(Buffer : TBytes; aSize, aCount: NativeInt);
  4501. begin
  4502. if ReadMaxSizeData(Buffer,aSize,ACount)<>aCount then
  4503. Raise EReadError.Create(SReadError);
  4504. end;
  4505. function TStream.ReadData(var Buffer: Boolean): NativeInt;
  4506. Var
  4507. B : Byte;
  4508. begin
  4509. Result:=ReadData(B,1);
  4510. if Result=1 then
  4511. Buffer:=B<>0;
  4512. end;
  4513. function TStream.ReadData(var Buffer: Boolean; Count: NativeInt): NativeInt;
  4514. Var
  4515. B : TBytes;
  4516. begin
  4517. SetLength(B,Count);
  4518. Result:=ReadMaxSizeData(B,1,Count);
  4519. if Result>0 then
  4520. Buffer:=B[0]<>0
  4521. end;
  4522. function TStream.ReadData(var Buffer: WideChar): NativeInt;
  4523. begin
  4524. Result:=ReadData(Buffer,2);
  4525. end;
  4526. function TStream.ReadData(var Buffer: WideChar; Count: NativeInt): NativeInt;
  4527. Var
  4528. W : Word;
  4529. begin
  4530. Result:=ReadData(W,Count);
  4531. if Result=2 then
  4532. Buffer:=WideChar(W);
  4533. end;
  4534. function TStream.ReadData(var Buffer: Int8): NativeInt;
  4535. begin
  4536. Result:=ReadData(Buffer,1);
  4537. end;
  4538. Function TStream.MakeInt(B : TBytes; aSize : Integer; Signed : Boolean) : NativeInt;
  4539. Var
  4540. Mem : TJSArrayBuffer;
  4541. A : TJSUInt8Array;
  4542. D : TJSDataView;
  4543. isLittle : Boolean;
  4544. begin
  4545. IsLittle:=(Endian=TEndian.Little);
  4546. Mem:=TJSArrayBuffer.New(Length(B));
  4547. A:=TJSUInt8Array.new(Mem);
  4548. A._set(B);
  4549. D:=TJSDataView.New(Mem);
  4550. if Signed then
  4551. case aSize of
  4552. 1 : Result:=D.getInt8(0);
  4553. 2 : Result:=D.getInt16(0,IsLittle);
  4554. 4 : Result:=D.getInt32(0,IsLittle);
  4555. // Todo : fix sign
  4556. 8 : Result:=Round(D.getFloat64(0,IsLittle));
  4557. end
  4558. else
  4559. case aSize of
  4560. 1 : Result:=D.getUInt8(0);
  4561. 2 : Result:=D.getUInt16(0,IsLittle);
  4562. 4 : Result:=D.getUInt32(0,IsLittle);
  4563. 8 : Result:=Round(D.getFloat64(0,IsLittle));
  4564. end
  4565. end;
  4566. function TStream.MakeBytes(B: NativeInt; aSize: Integer; Signed: Boolean): TBytes;
  4567. Var
  4568. Mem : TJSArrayBuffer;
  4569. A : TJSUInt8Array;
  4570. D : TJSDataView;
  4571. isLittle : Boolean;
  4572. begin
  4573. IsLittle:=(Endian=TEndian.Little);
  4574. Mem:=TJSArrayBuffer.New(aSize);
  4575. D:=TJSDataView.New(Mem);
  4576. if Signed then
  4577. case aSize of
  4578. 1 : D.setInt8(0,B);
  4579. 2 : D.setInt16(0,B,IsLittle);
  4580. 4 : D.setInt32(0,B,IsLittle);
  4581. 8 : D.setFloat64(0,B,IsLittle);
  4582. end
  4583. else
  4584. case aSize of
  4585. 1 : D.SetUInt8(0,B);
  4586. 2 : D.SetUInt16(0,B,IsLittle);
  4587. 4 : D.SetUInt32(0,B,IsLittle);
  4588. 8 : D.setFloat64(0,B,IsLittle);
  4589. end;
  4590. SetLength(Result,aSize);
  4591. A:=TJSUInt8Array.new(Mem);
  4592. Result:=TMemoryStream.MemoryToBytes(A);
  4593. end;
  4594. function TStream.ReadData(var Buffer: Int8; Count: NativeInt): NativeInt;
  4595. Var
  4596. B : TBytes;
  4597. begin
  4598. SetLength(B,Count);
  4599. Result:=ReadMaxSizeData(B,1,Count);
  4600. if Result>=1 then
  4601. Buffer:=MakeInt(B,1,True);
  4602. end;
  4603. function TStream.ReadData(var Buffer: UInt8): NativeInt;
  4604. begin
  4605. Result:=ReadData(Buffer,1);
  4606. end;
  4607. function TStream.ReadData(var Buffer: UInt8; Count: NativeInt): NativeInt;
  4608. Var
  4609. B : TBytes;
  4610. begin
  4611. SetLength(B,Count);
  4612. Result:=ReadMaxSizeData(B,1,Count);
  4613. if Result>=1 then
  4614. Buffer:=MakeInt(B,1,False);
  4615. end;
  4616. function TStream.ReadData(var Buffer: Int16): NativeInt;
  4617. begin
  4618. Result:=ReadData(Buffer,2);
  4619. end;
  4620. function TStream.ReadData(var Buffer: Int16; Count: NativeInt): NativeInt;
  4621. Var
  4622. B : TBytes;
  4623. begin
  4624. SetLength(B,Count);
  4625. Result:=ReadMaxSizeData(B,2,Count);
  4626. if Result>=2 then
  4627. Buffer:=MakeInt(B,2,True);
  4628. end;
  4629. function TStream.ReadData(var Buffer: UInt16): NativeInt;
  4630. begin
  4631. Result:=ReadData(Buffer,2);
  4632. end;
  4633. function TStream.ReadData(var Buffer: UInt16; Count: NativeInt): NativeInt;
  4634. Var
  4635. B : TBytes;
  4636. begin
  4637. SetLength(B,Count);
  4638. Result:=ReadMaxSizeData(B,2,Count);
  4639. if Result>=2 then
  4640. Buffer:=MakeInt(B,2,False);
  4641. end;
  4642. function TStream.ReadData(var Buffer: Int32): NativeInt;
  4643. begin
  4644. Result:=ReadData(Buffer,4);
  4645. end;
  4646. function TStream.ReadData(var Buffer: Int32; Count: NativeInt): NativeInt;
  4647. Var
  4648. B : TBytes;
  4649. begin
  4650. SetLength(B,Count);
  4651. Result:=ReadMaxSizeData(B,4,Count);
  4652. if Result>=4 then
  4653. Buffer:=MakeInt(B,4,True);
  4654. end;
  4655. function TStream.ReadData(var Buffer: UInt32): NativeInt;
  4656. begin
  4657. Result:=ReadData(Buffer,4);
  4658. end;
  4659. function TStream.ReadData(var Buffer: UInt32; Count: NativeInt): NativeInt;
  4660. Var
  4661. B : TBytes;
  4662. begin
  4663. SetLength(B,Count);
  4664. Result:=ReadMaxSizeData(B,4,Count);
  4665. if Result>=4 then
  4666. Buffer:=MakeInt(B,4,False);
  4667. end;
  4668. function TStream.ReadData(var Buffer: NativeInt): NativeInt;
  4669. begin
  4670. Result:=ReadData(Buffer,8);
  4671. end;
  4672. function TStream.ReadData(var Buffer: NativeInt; Count: NativeInt): NativeInt;
  4673. Var
  4674. B : TBytes;
  4675. begin
  4676. SetLength(B,Count);
  4677. Result:=ReadMaxSizeData(B,8,8);
  4678. if Result>=8 then
  4679. Buffer:=MakeInt(B,8,True);
  4680. end;
  4681. function TStream.ReadData(var Buffer: NativeLargeUInt): NativeInt;
  4682. begin
  4683. Result:=ReadData(Buffer,8);
  4684. end;
  4685. function TStream.ReadData(var Buffer: NativeLargeUInt; Count: NativeInt): NativeInt;
  4686. Var
  4687. B : TBytes;
  4688. B1 : Integer;
  4689. begin
  4690. SetLength(B,Count);
  4691. Result:=ReadMaxSizeData(B,4,4);
  4692. if Result>=4 then
  4693. begin
  4694. B1:=MakeInt(B,4,False);
  4695. Result:=Result+ReadMaxSizeData(B,4,4);
  4696. Buffer:=MakeInt(B,4,False);
  4697. Buffer:=(Buffer shl 32) or B1;
  4698. end;
  4699. end;
  4700. function TStream.ReadData(var Buffer: Double): NativeInt;
  4701. begin
  4702. Result:=ReadData(Buffer,8);
  4703. end;
  4704. function TStream.ReadData(var Buffer: Double; Count: NativeInt): NativeInt;
  4705. Var
  4706. B : TBytes;
  4707. Mem : TJSArrayBuffer;
  4708. A : TJSUInt8Array;
  4709. D : TJSDataView;
  4710. begin
  4711. SetLength(B,Count);
  4712. Result:=ReadMaxSizeData(B,8,Count);
  4713. if Result>=8 then
  4714. begin
  4715. Mem:=TJSArrayBuffer.New(8);
  4716. A:=TJSUInt8Array.new(Mem);
  4717. A._set(B);
  4718. D:=TJSDataView.New(Mem);
  4719. Buffer:=D.getFloat64(0);
  4720. end;
  4721. end;
  4722. procedure TStream.ReadBuffer(var Buffer: TBytes; Count: NativeInt);
  4723. begin
  4724. ReadBuffer(Buffer,0,Count);
  4725. end;
  4726. procedure TStream.ReadBuffer(var Buffer: TBytes; Offset, Count: NativeInt);
  4727. begin
  4728. if Read(Buffer,OffSet,Count)<>Count then
  4729. Raise EStreamError.Create(SReadError);
  4730. end;
  4731. procedure TStream.ReadBufferData(var Buffer: Boolean);
  4732. begin
  4733. ReadBufferData(Buffer,1);
  4734. end;
  4735. procedure TStream.ReadBufferData(var Buffer: Boolean; Count: NativeInt);
  4736. begin
  4737. if (ReadData(Buffer,Count)<>Count) then
  4738. Raise EStreamError.Create(SReadError);
  4739. end;
  4740. procedure TStream.ReadBufferData(var Buffer: WideChar);
  4741. begin
  4742. ReadBufferData(Buffer,2);
  4743. end;
  4744. procedure TStream.ReadBufferData(var Buffer: WideChar; Count: NativeInt);
  4745. begin
  4746. if (ReadData(Buffer,Count)<>Count) then
  4747. Raise EStreamError.Create(SReadError);
  4748. end;
  4749. procedure TStream.ReadBufferData(var Buffer: Int8);
  4750. begin
  4751. ReadBufferData(Buffer,1);
  4752. end;
  4753. procedure TStream.ReadBufferData(var Buffer: Int8; Count: NativeInt);
  4754. begin
  4755. if (ReadData(Buffer,Count)<>Count) then
  4756. Raise EStreamError.Create(SReadError);
  4757. end;
  4758. procedure TStream.ReadBufferData(var Buffer: UInt8);
  4759. begin
  4760. ReadBufferData(Buffer,1);
  4761. end;
  4762. procedure TStream.ReadBufferData(var Buffer: UInt8; Count: NativeInt);
  4763. begin
  4764. if (ReadData(Buffer,Count)<>Count) then
  4765. Raise EStreamError.Create(SReadError);
  4766. end;
  4767. procedure TStream.ReadBufferData(var Buffer: Int16);
  4768. begin
  4769. ReadBufferData(Buffer,2);
  4770. end;
  4771. procedure TStream.ReadBufferData(var Buffer: Int16; Count: NativeInt);
  4772. begin
  4773. if (ReadData(Buffer,Count)<>Count) then
  4774. Raise EStreamError.Create(SReadError);
  4775. end;
  4776. procedure TStream.ReadBufferData(var Buffer: UInt16);
  4777. begin
  4778. ReadBufferData(Buffer,2);
  4779. end;
  4780. procedure TStream.ReadBufferData(var Buffer: UInt16; Count: NativeInt);
  4781. begin
  4782. if (ReadData(Buffer,Count)<>Count) then
  4783. Raise EStreamError.Create(SReadError);
  4784. end;
  4785. procedure TStream.ReadBufferData(var Buffer: Int32);
  4786. begin
  4787. ReadBufferData(Buffer,4);
  4788. end;
  4789. procedure TStream.ReadBufferData(var Buffer: Int32; Count: NativeInt);
  4790. begin
  4791. if (ReadData(Buffer,Count)<>Count) then
  4792. Raise EStreamError.Create(SReadError);
  4793. end;
  4794. procedure TStream.ReadBufferData(var Buffer: UInt32);
  4795. begin
  4796. ReadBufferData(Buffer,4);
  4797. end;
  4798. procedure TStream.ReadBufferData(var Buffer: UInt32; Count: NativeInt);
  4799. begin
  4800. if (ReadData(Buffer,Count)<>Count) then
  4801. Raise EStreamError.Create(SReadError);
  4802. end;
  4803. procedure TStream.ReadBufferData(var Buffer: NativeLargeInt);
  4804. begin
  4805. ReadBufferData(Buffer,8)
  4806. end;
  4807. procedure TStream.ReadBufferData(var Buffer: NativeLargeInt; Count: NativeInt);
  4808. begin
  4809. if (ReadData(Buffer,Count)<>Count) then
  4810. Raise EStreamError.Create(SReadError);
  4811. end;
  4812. procedure TStream.ReadBufferData(var Buffer: NativeLargeUInt);
  4813. begin
  4814. ReadBufferData(Buffer,8);
  4815. end;
  4816. procedure TStream.ReadBufferData(var Buffer: NativeLargeUInt; Count: NativeInt);
  4817. begin
  4818. if (ReadData(Buffer,Count)<>Count) then
  4819. Raise EStreamError.Create(SReadError);
  4820. end;
  4821. procedure TStream.ReadBufferData(var Buffer: Double);
  4822. begin
  4823. ReadBufferData(Buffer,8);
  4824. end;
  4825. procedure TStream.ReadBufferData(var Buffer: Double; Count: NativeInt);
  4826. begin
  4827. if (ReadData(Buffer,Count)<>Count) then
  4828. Raise EStreamError.Create(SReadError);
  4829. end;
  4830. procedure TStream.WriteBuffer(const Buffer: TBytes; Count: NativeInt);
  4831. begin
  4832. WriteBuffer(Buffer,0,Count);
  4833. end;
  4834. procedure TStream.WriteBuffer(const Buffer: TBytes; Offset, Count: NativeInt);
  4835. begin
  4836. if Self.Write(Buffer,Offset,Count)<>Count then
  4837. Raise EStreamError.Create(SWriteError);
  4838. end;
  4839. function TStream.WriteData(const Buffer: TBytes; Count: NativeInt): NativeInt;
  4840. begin
  4841. Result:=Self.Write(Buffer, 0, Count);
  4842. end;
  4843. function TStream.WriteData(const Buffer: Boolean): NativeInt;
  4844. begin
  4845. Result:=WriteData(Buffer,1);
  4846. end;
  4847. function TStream.WriteData(const Buffer: Boolean; Count: NativeInt): NativeInt;
  4848. Var
  4849. B : Int8;
  4850. begin
  4851. B:=Ord(Buffer);
  4852. Result:=WriteData(B,Count);
  4853. end;
  4854. function TStream.WriteData(const Buffer: WideChar): NativeInt;
  4855. begin
  4856. Result:=WriteData(Buffer,2);
  4857. end;
  4858. function TStream.WriteData(const Buffer: WideChar; Count: NativeInt): NativeInt;
  4859. Var
  4860. U : UInt16;
  4861. begin
  4862. U:=Ord(Buffer);
  4863. Result:=WriteData(U,Count);
  4864. end;
  4865. function TStream.WriteData(const Buffer: Int8): NativeInt;
  4866. begin
  4867. Result:=WriteData(Buffer,1);
  4868. end;
  4869. function TStream.WriteData(const Buffer: Int8; Count: NativeInt): NativeInt;
  4870. begin
  4871. Result:=WriteMaxSizeData(MakeBytes(Buffer,1,True),1,Count);
  4872. end;
  4873. function TStream.WriteData(const Buffer: UInt8): NativeInt;
  4874. begin
  4875. Result:=WriteData(Buffer,1);
  4876. end;
  4877. function TStream.WriteData(const Buffer: UInt8; Count: NativeInt): NativeInt;
  4878. begin
  4879. Result:=WriteMaxSizeData(MakeBytes(Buffer,1,False),1,Count);
  4880. end;
  4881. function TStream.WriteData(const Buffer: Int16): NativeInt;
  4882. begin
  4883. Result:=WriteData(Buffer,2);
  4884. end;
  4885. function TStream.WriteData(const Buffer: Int16; Count: NativeInt): NativeInt;
  4886. begin
  4887. Result:=WriteMaxSizeData(MakeBytes(Buffer,2,True),2,Count);
  4888. end;
  4889. function TStream.WriteData(const Buffer: UInt16): NativeInt;
  4890. begin
  4891. Result:=WriteData(Buffer,2);
  4892. end;
  4893. function TStream.WriteData(const Buffer: UInt16; Count: NativeInt): NativeInt;
  4894. begin
  4895. Result:=WriteMaxSizeData(MakeBytes(Buffer,2,True),2,Count);
  4896. end;
  4897. function TStream.WriteData(const Buffer: Int32): NativeInt;
  4898. begin
  4899. Result:=WriteData(Buffer,4);
  4900. end;
  4901. function TStream.WriteData(const Buffer: Int32; Count: NativeInt): NativeInt;
  4902. begin
  4903. Result:=WriteMaxSizeData(MakeBytes(Buffer,4,True),4,Count);
  4904. end;
  4905. function TStream.WriteData(const Buffer: UInt32): NativeInt;
  4906. begin
  4907. Result:=WriteData(Buffer,4);
  4908. end;
  4909. function TStream.WriteData(const Buffer: UInt32; Count: NativeInt): NativeInt;
  4910. begin
  4911. Result:=WriteMaxSizeData(MakeBytes(Buffer,4,False),4,Count);
  4912. end;
  4913. function TStream.WriteData(const Buffer: NativeLargeInt): NativeInt;
  4914. begin
  4915. Result:=WriteData(Buffer,8);
  4916. end;
  4917. function TStream.WriteData(const Buffer: NativeLargeInt; Count: NativeInt): NativeInt;
  4918. begin
  4919. Result:=WriteMaxSizeData(MakeBytes(Buffer,8,True),8,Count);
  4920. end;
  4921. function TStream.WriteData(const Buffer: NativeLargeUInt): NativeInt;
  4922. begin
  4923. Result:=WriteData(Buffer,8);
  4924. end;
  4925. function TStream.WriteData(const Buffer: NativeLargeUInt; Count: NativeInt): NativeInt;
  4926. begin
  4927. Result:=WriteMaxSizeData(MakeBytes(Buffer,8,False),8,Count);
  4928. end;
  4929. function TStream.WriteData(const Buffer: Double): NativeInt;
  4930. begin
  4931. Result:=WriteData(Buffer,8);
  4932. end;
  4933. function TStream.WriteData(const Buffer: Double; Count: NativeInt): NativeInt;
  4934. Var
  4935. Mem : TJSArrayBuffer;
  4936. A : TJSUint8array;
  4937. D : TJSDataview;
  4938. B : TBytes;
  4939. I : Integer;
  4940. begin
  4941. Mem:=TJSArrayBuffer.New(8);
  4942. D:=TJSDataView.new(Mem);
  4943. D.setFloat64(0,Buffer);
  4944. SetLength(B,8);
  4945. A:=TJSUint8array.New(Mem);
  4946. For I:=0 to 7 do
  4947. B[i]:=A[i];
  4948. Result:=WriteMaxSizeData(B,8,Count);
  4949. end;
  4950. procedure TStream.WriteBufferData(Buffer: Int32);
  4951. begin
  4952. WriteBufferData(Buffer,4);
  4953. end;
  4954. procedure TStream.WriteBufferData(Buffer: Int32; Count: NativeInt);
  4955. begin
  4956. if (WriteData(Buffer,Count)<>Count) then
  4957. Raise EStreamError.Create(SWriteError);
  4958. end;
  4959. procedure TStream.WriteBufferData(Buffer: Boolean);
  4960. begin
  4961. WriteBufferData(Buffer,1);
  4962. end;
  4963. procedure TStream.WriteBufferData(Buffer: Boolean; Count: NativeInt);
  4964. begin
  4965. if (WriteData(Buffer,Count)<>Count) then
  4966. Raise EStreamError.Create(SWriteError);
  4967. end;
  4968. procedure TStream.WriteBufferData(Buffer: WideChar);
  4969. begin
  4970. WriteBufferData(Buffer,2);
  4971. end;
  4972. procedure TStream.WriteBufferData(Buffer: WideChar; Count: NativeInt);
  4973. begin
  4974. if (WriteData(Buffer,Count)<>Count) then
  4975. Raise EStreamError.Create(SWriteError);
  4976. end;
  4977. procedure TStream.WriteBufferData(Buffer: Int8);
  4978. begin
  4979. WriteBufferData(Buffer,1);
  4980. end;
  4981. procedure TStream.WriteBufferData(Buffer: Int8; Count: NativeInt);
  4982. begin
  4983. if (WriteData(Buffer,Count)<>Count) then
  4984. Raise EStreamError.Create(SWriteError);
  4985. end;
  4986. procedure TStream.WriteBufferData(Buffer: UInt8);
  4987. begin
  4988. WriteBufferData(Buffer,1);
  4989. end;
  4990. procedure TStream.WriteBufferData(Buffer: UInt8; Count: NativeInt);
  4991. begin
  4992. if (WriteData(Buffer,Count)<>Count) then
  4993. Raise EStreamError.Create(SWriteError);
  4994. end;
  4995. procedure TStream.WriteBufferData(Buffer: Int16);
  4996. begin
  4997. WriteBufferData(Buffer,2);
  4998. end;
  4999. procedure TStream.WriteBufferData(Buffer: Int16; Count: NativeInt);
  5000. begin
  5001. if (WriteData(Buffer,Count)<>Count) then
  5002. Raise EStreamError.Create(SWriteError);
  5003. end;
  5004. procedure TStream.WriteBufferData(Buffer: UInt16);
  5005. begin
  5006. WriteBufferData(Buffer,2);
  5007. end;
  5008. procedure TStream.WriteBufferData(Buffer: UInt16; Count: NativeInt);
  5009. begin
  5010. if (WriteData(Buffer,Count)<>Count) then
  5011. Raise EStreamError.Create(SWriteError);
  5012. end;
  5013. procedure TStream.WriteBufferData(Buffer: UInt32);
  5014. begin
  5015. WriteBufferData(Buffer,4);
  5016. end;
  5017. procedure TStream.WriteBufferData(Buffer: UInt32; Count: NativeInt);
  5018. begin
  5019. if (WriteData(Buffer,Count)<>Count) then
  5020. Raise EStreamError.Create(SWriteError);
  5021. end;
  5022. procedure TStream.WriteBufferData(Buffer: NativeInt);
  5023. begin
  5024. WriteBufferData(Buffer,8);
  5025. end;
  5026. procedure TStream.WriteBufferData(Buffer: NativeInt; Count: NativeInt);
  5027. begin
  5028. if (WriteData(Buffer,Count)<>Count) then
  5029. Raise EStreamError.Create(SWriteError);
  5030. end;
  5031. procedure TStream.WriteBufferData(Buffer: NativeLargeUInt);
  5032. begin
  5033. WriteBufferData(Buffer,8);
  5034. end;
  5035. procedure TStream.WriteBufferData(Buffer: NativeLargeUInt; Count: NativeInt);
  5036. begin
  5037. if (WriteData(Buffer,Count)<>Count) then
  5038. Raise EStreamError.Create(SWriteError);
  5039. end;
  5040. procedure TStream.WriteBufferData(Buffer: Double);
  5041. begin
  5042. WriteBufferData(Buffer,8);
  5043. end;
  5044. procedure TStream.WriteBufferData(Buffer: Double; Count: NativeInt);
  5045. begin
  5046. if (WriteData(Buffer,Count)<>Count) then
  5047. Raise EStreamError.Create(SWriteError);
  5048. end;
  5049. function TStream.CopyFrom(Source: TStream; Count: NativeInt): NativeInt;
  5050. var
  5051. Buffer: TBytes;
  5052. BufferSize, i: LongInt;
  5053. const
  5054. MaxSize = $20000;
  5055. begin
  5056. Result:=0;
  5057. if Count=0 then
  5058. Source.Position:=0; // This WILL fail for non-seekable streams...
  5059. BufferSize:=MaxSize;
  5060. if (Count>0) and (Count<BufferSize) then
  5061. BufferSize:=Count; // do not allocate more than needed
  5062. SetLength(Buffer,BufferSize);
  5063. if Count=0 then
  5064. repeat
  5065. i:=Source.Read(Buffer,BufferSize);
  5066. if i>0 then
  5067. WriteBuffer(Buffer,i);
  5068. Inc(Result,i);
  5069. until i<BufferSize
  5070. else
  5071. while Count>0 do
  5072. begin
  5073. if Count>BufferSize then
  5074. i:=BufferSize
  5075. else
  5076. i:=Count;
  5077. Source.ReadBuffer(Buffer,i);
  5078. WriteBuffer(Buffer,i);
  5079. Dec(count,i);
  5080. Inc(Result,i);
  5081. end;
  5082. end;
  5083. function TStream.ReadComponent(Instance: TComponent): TComponent;
  5084. var
  5085. Reader: TReader;
  5086. begin
  5087. Reader := TReader.Create(Self);
  5088. try
  5089. Result := Reader.ReadRootComponent(Instance);
  5090. finally
  5091. Reader.Free;
  5092. end;
  5093. end;
  5094. function TStream.ReadComponentRes(Instance: TComponent): TComponent;
  5095. begin
  5096. ReadResHeader;
  5097. Result := ReadComponent(Instance);
  5098. end;
  5099. procedure TStream.WriteComponent(Instance: TComponent);
  5100. begin
  5101. WriteDescendent(Instance, nil);
  5102. end;
  5103. procedure TStream.WriteComponentRes(const ResName: string; Instance: TComponent);
  5104. begin
  5105. WriteDescendentRes(ResName, Instance, nil);
  5106. end;
  5107. procedure TStream.WriteDescendent(Instance, Ancestor: TComponent);
  5108. var
  5109. Driver : TAbstractObjectWriter;
  5110. Writer : TWriter;
  5111. begin
  5112. Driver := TBinaryObjectWriter.Create(Self);
  5113. Try
  5114. Writer := TWriter.Create(Driver);
  5115. Try
  5116. Writer.WriteDescendent(Instance, Ancestor);
  5117. Finally
  5118. Writer.Destroy;
  5119. end;
  5120. Finally
  5121. Driver.Free;
  5122. end;
  5123. end;
  5124. procedure TStream.WriteDescendentRes(const ResName: string; Instance, Ancestor: TComponent);
  5125. var
  5126. FixupInfo: Longint;
  5127. begin
  5128. { Write a resource header }
  5129. WriteResourceHeader(ResName, FixupInfo);
  5130. { Write the instance itself }
  5131. WriteDescendent(Instance, Ancestor);
  5132. { Insert the correct resource size into the resource header }
  5133. FixupResourceHeader(FixupInfo);
  5134. end;
  5135. procedure TStream.WriteResourceHeader(const ResName: string; {!!!: out} var FixupInfo: Longint);
  5136. var
  5137. ResType, Flags : word;
  5138. B : Byte;
  5139. I : Integer;
  5140. begin
  5141. ResType:=Word($000A);
  5142. Flags:=Word($1030);
  5143. { Note: This is a Windows 16 bit resource }
  5144. { Numeric resource type }
  5145. WriteByte($ff);
  5146. { Application defined data }
  5147. WriteWord(ResType);
  5148. { write the name as asciiz }
  5149. For I:=1 to Length(ResName) do
  5150. begin
  5151. B:=Ord(ResName[i]);
  5152. WriteByte(B);
  5153. end;
  5154. WriteByte(0);
  5155. { Movable, Pure and Discardable }
  5156. WriteWord(Flags);
  5157. { Placeholder for the resource size }
  5158. WriteDWord(0);
  5159. { Return current stream position so that the resource size can be
  5160. inserted later }
  5161. FixupInfo := Position;
  5162. end;
  5163. procedure TStream.FixupResourceHeader(FixupInfo: Longint);
  5164. var
  5165. ResSize,TmpResSize : Longint;
  5166. begin
  5167. ResSize := Position - FixupInfo;
  5168. TmpResSize := longword(ResSize);
  5169. { Insert the correct resource size into the placeholder written by
  5170. WriteResourceHeader }
  5171. Position := FixupInfo - 4;
  5172. WriteDWord(TmpResSize);
  5173. { Seek back to the end of the resource }
  5174. Position := FixupInfo + ResSize;
  5175. end;
  5176. procedure TStream.ReadResHeader;
  5177. var
  5178. ResType, Flags : word;
  5179. begin
  5180. try
  5181. { Note: This is a Windows 16 bit resource }
  5182. { application specific resource ? }
  5183. if ReadByte<>$ff then
  5184. raise EInvalidImage.Create(SInvalidImage);
  5185. ResType:=ReadWord;
  5186. if ResType<>$000a then
  5187. raise EInvalidImage.Create(SInvalidImage);
  5188. { read name }
  5189. while ReadByte<>0 do
  5190. ;
  5191. { check the access specifier }
  5192. Flags:=ReadWord;
  5193. if Flags<>$1030 then
  5194. raise EInvalidImage.Create(SInvalidImage);
  5195. { ignore the size }
  5196. ReadDWord;
  5197. except
  5198. on EInvalidImage do
  5199. raise;
  5200. else
  5201. raise EInvalidImage.create(SInvalidImage);
  5202. end;
  5203. end;
  5204. function TStream.ReadByte : Byte;
  5205. begin
  5206. ReadBufferData(Result,1);
  5207. end;
  5208. function TStream.ReadWord : Word;
  5209. begin
  5210. ReadBufferData(Result,2);
  5211. end;
  5212. function TStream.ReadDWord : Cardinal;
  5213. begin
  5214. ReadBufferData(Result,4);
  5215. end;
  5216. function TStream.ReadQWord: NativeLargeUInt;
  5217. begin
  5218. ReadBufferData(Result,8);
  5219. end;
  5220. procedure TStream.WriteByte(b : Byte);
  5221. begin
  5222. WriteBufferData(b,1);
  5223. end;
  5224. procedure TStream.WriteWord(w : Word);
  5225. begin
  5226. WriteBufferData(W,2);
  5227. end;
  5228. procedure TStream.WriteDWord(d : Cardinal);
  5229. begin
  5230. WriteBufferData(d,4);
  5231. end;
  5232. procedure TStream.WriteQWord(q: NativeLargeUInt);
  5233. begin
  5234. WriteBufferData(q,8);
  5235. end;
  5236. {****************************************************************************}
  5237. {* TCustomMemoryStream *}
  5238. {****************************************************************************}
  5239. procedure TCustomMemoryStream.SetPointer(Ptr: TJSArrayBuffer; ASize: PtrInt);
  5240. begin
  5241. FMemory:=Ptr;
  5242. FSize:=ASize;
  5243. FDataView:=Nil;
  5244. FDataArray:=Nil;
  5245. end;
  5246. class function TCustomMemoryStream.MemoryToBytes(Mem: TJSArrayBuffer): TBytes;
  5247. begin
  5248. Result:=MemoryToBytes(TJSUint8Array.New(Mem));
  5249. end;
  5250. class function TCustomMemoryStream.MemoryToBytes(Mem: TJSUint8Array): TBytes;
  5251. Var
  5252. I : Integer;
  5253. begin
  5254. // This must be improved, but needs some asm or TJSFunction.call() to implement answers in
  5255. // https://stackoverflow.com/questions/29676635/convert-uint8array-to-array-in-javascript
  5256. for i:=0 to mem.length-1 do
  5257. Result[i]:=Mem[i];
  5258. end;
  5259. class function TCustomMemoryStream.BytesToMemory(aBytes: TBytes): TJSArrayBuffer;
  5260. Var
  5261. a : TJSUint8Array;
  5262. begin
  5263. Result:=TJSArrayBuffer.new(Length(aBytes));
  5264. A:=TJSUint8Array.New(Result);
  5265. A._set(aBytes);
  5266. end;
  5267. function TCustomMemoryStream.GetDataArray: TJSUint8Array;
  5268. begin
  5269. if FDataArray=Nil then
  5270. FDataArray:=TJSUint8Array.new(Memory);
  5271. Result:=FDataArray;
  5272. end;
  5273. function TCustomMemoryStream.GetDataView: TJSDataview;
  5274. begin
  5275. if FDataView=Nil then
  5276. FDataView:=TJSDataView.New(Memory);
  5277. Result:=FDataView;
  5278. end;
  5279. function TCustomMemoryStream.GetSize: NativeInt;
  5280. begin
  5281. Result:=FSize;
  5282. end;
  5283. function TCustomMemoryStream.GetPosition: NativeInt;
  5284. begin
  5285. Result:=FPosition;
  5286. end;
  5287. function TCustomMemoryStream.Read(Buffer: TBytes; Offset, Count: LongInt): LongInt;
  5288. Var
  5289. I,Src,Dest : Integer;
  5290. begin
  5291. Result:=0;
  5292. If (FSize>0) and (FPosition<Fsize) and (FPosition>=0) then
  5293. begin
  5294. Result:=Count;
  5295. If (Result>(FSize-FPosition)) then
  5296. Result:=(FSize-FPosition);
  5297. Src:=FPosition;
  5298. Dest:=Offset;
  5299. I:=0;
  5300. While I<Result do
  5301. begin
  5302. Buffer[Dest]:=DataView.getUint8(Src);
  5303. inc(Src);
  5304. inc(Dest);
  5305. inc(I);
  5306. end;
  5307. FPosition:=Fposition+Result;
  5308. end;
  5309. end;
  5310. function TCustomMemoryStream.Seek(const Offset: NativeInt; Origin: TSeekOrigin): NativeInt;
  5311. begin
  5312. Case Origin of
  5313. soBeginning : FPosition:=Offset;
  5314. soEnd : FPosition:=FSize+Offset;
  5315. soCurrent : FPosition:=FPosition+Offset;
  5316. end;
  5317. if SizeBoundsSeek and (FPosition>FSize) then
  5318. FPosition:=FSize;
  5319. Result:=FPosition;
  5320. {$IFDEF DEBUG}
  5321. if Result < 0 then
  5322. raise Exception.Create('TCustomMemoryStream');
  5323. {$ENDIF}
  5324. end;
  5325. procedure TCustomMemoryStream.SaveToStream(Stream: TStream);
  5326. begin
  5327. if FSize>0 then
  5328. Stream.WriteBuffer(TMemoryStream.MemoryToBytes(Memory),FSize);
  5329. end;
  5330. procedure TCustomMemoryStream.LoadFromURL(const aURL: String; Async: Boolean; OnLoaded: TNotifyEventRef; OnError: TStringNotifyEventRef = Nil);
  5331. procedure DoLoaded(const abytes : TJSArrayBuffer);
  5332. begin
  5333. SetPointer(aBytes,aBytes.byteLength);
  5334. if Assigned(OnLoaded) then
  5335. OnLoaded(Self);
  5336. end;
  5337. procedure DoError(const AError : String);
  5338. begin
  5339. if Assigned(OnError) then
  5340. OnError(Self,aError)
  5341. else
  5342. Raise EInOutError.Create('Failed to load from URL:'+aError);
  5343. end;
  5344. begin
  5345. CheckLoadHelper;
  5346. GlobalLoadHelper.LoadBytes(aURL,aSync,@DoLoaded,@DoError);
  5347. end;
  5348. procedure TCustomMemoryStream.LoadFromFile(const aFileName: String; const OnLoaded: TProc; const AError: TProcString);
  5349. begin
  5350. LoadFromURL(aFileName,False,
  5351. Procedure (Sender : TObject)
  5352. begin
  5353. If Assigned(OnLoaded) then
  5354. OnLoaded
  5355. end,
  5356. Procedure (Sender : TObject; Const ErrorMsg : String)
  5357. begin
  5358. if Assigned(aError) then
  5359. aError(ErrorMsg)
  5360. end);
  5361. end;
  5362. {****************************************************************************}
  5363. {* TMemoryStream *}
  5364. {****************************************************************************}
  5365. Const TMSGrow = 4096; { Use 4k blocks. }
  5366. procedure TMemoryStream.SetCapacity(NewCapacity: PtrInt);
  5367. begin
  5368. SetPointer (Realloc(NewCapacity),Fsize);
  5369. FCapacity:=NewCapacity;
  5370. end;
  5371. function TMemoryStream.Realloc(var NewCapacity: PtrInt): TJSArrayBuffer;
  5372. Var
  5373. GC : PtrInt;
  5374. DestView : TJSUInt8array;
  5375. begin
  5376. If NewCapacity<0 Then
  5377. NewCapacity:=0
  5378. else
  5379. begin
  5380. GC:=FCapacity + (FCapacity div 4);
  5381. // if growing, grow at least a quarter
  5382. if (NewCapacity>FCapacity) and (NewCapacity < GC) then
  5383. NewCapacity := GC;
  5384. // round off to block size.
  5385. NewCapacity := (NewCapacity + (TMSGrow-1)) and not (TMSGROW-1);
  5386. end;
  5387. // Only now check !
  5388. If NewCapacity=FCapacity then
  5389. Result:=FMemory
  5390. else if NewCapacity=0 then
  5391. Result:=Nil
  5392. else
  5393. begin
  5394. // New buffer
  5395. Result:=TJSArrayBuffer.New(NewCapacity);
  5396. If (Result=Nil) then
  5397. Raise EStreamError.Create(SMemoryStreamError);
  5398. // Transfer
  5399. DestView:=TJSUInt8array.New(Result);
  5400. Destview._Set(Self.DataArray);
  5401. end;
  5402. end;
  5403. destructor TMemoryStream.Destroy;
  5404. begin
  5405. Clear;
  5406. Inherited Destroy;
  5407. end;
  5408. procedure TMemoryStream.Clear;
  5409. begin
  5410. FSize:=0;
  5411. FPosition:=0;
  5412. SetCapacity (0);
  5413. end;
  5414. procedure TMemoryStream.LoadFromStream(Stream: TStream);
  5415. begin
  5416. Position:=0;
  5417. Stream.Position:=0;
  5418. SetSize(Stream.Size);
  5419. If (Size>0) then
  5420. CopyFrom(Stream,0);
  5421. end;
  5422. procedure TMemoryStream.SetSize(const NewSize: NativeInt);
  5423. begin
  5424. SetCapacity (NewSize);
  5425. FSize:=NewSize;
  5426. IF FPosition>FSize then
  5427. FPosition:=FSize;
  5428. end;
  5429. function TMemoryStream.Write(Const Buffer : TBytes; OffSet, Count: LongInt): LongInt;
  5430. Var NewPos : PtrInt;
  5431. begin
  5432. If (Count=0) or (FPosition<0) then
  5433. exit(0);
  5434. NewPos:=FPosition+Count;
  5435. If NewPos>Fsize then
  5436. begin
  5437. IF NewPos>FCapacity then
  5438. SetCapacity (NewPos);
  5439. FSize:=Newpos;
  5440. end;
  5441. DataArray._set(Copy(Buffer,Offset,Count),FPosition);
  5442. FPosition:=NewPos;
  5443. Result:=Count;
  5444. end;
  5445. {****************************************************************************}
  5446. {* TBytesStream *}
  5447. {****************************************************************************}
  5448. constructor TBytesStream.Create(const ABytes: TBytes);
  5449. begin
  5450. inherited Create;
  5451. SetPointer(TMemoryStream.BytesToMemory(aBytes),Length(ABytes));
  5452. FCapacity:=Length(ABytes);
  5453. end;
  5454. function TBytesStream.GetBytes: TBytes;
  5455. begin
  5456. Result:=TMemoryStream.MemoryToBytes(Memory);
  5457. end;
  5458. { *********************************************************************
  5459. * TFiler *
  5460. *********************************************************************}
  5461. procedure TFiler.SetRoot(ARoot: TComponent);
  5462. begin
  5463. FRoot := ARoot;
  5464. end;
  5465. {
  5466. This file is part of the Free Component Library (FCL)
  5467. Copyright (c) 1999-2000 by the Free Pascal development team
  5468. See the file COPYING.FPC, included in this distribution,
  5469. for details about the copyright.
  5470. This program is distributed in the hope that it will be useful,
  5471. but WITHOUT ANY WARRANTY; without even the implied warranty of
  5472. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  5473. **********************************************************************}
  5474. {****************************************************************************}
  5475. {* TBinaryObjectReader *}
  5476. {****************************************************************************}
  5477. function TBinaryObjectReader.ReadWord : word;
  5478. begin
  5479. FStream.ReadBufferData(Result);
  5480. end;
  5481. function TBinaryObjectReader.ReadDWord : longword;
  5482. begin
  5483. FStream.ReadBufferData(Result);
  5484. end;
  5485. constructor TBinaryObjectReader.Create(Stream: TStream);
  5486. begin
  5487. inherited Create;
  5488. If (Stream=Nil) then
  5489. Raise EReadError.Create(SEmptyStreamIllegalReader);
  5490. FStream := Stream;
  5491. end;
  5492. function TBinaryObjectReader.ReadValue: TValueType;
  5493. var
  5494. b: byte;
  5495. begin
  5496. FStream.ReadBufferData(b);
  5497. Result := TValueType(b);
  5498. end;
  5499. function TBinaryObjectReader.NextValue: TValueType;
  5500. begin
  5501. Result := ReadValue;
  5502. { We only 'peek' at the next value, so seek back to unget the read value: }
  5503. FStream.Seek(-1,soCurrent);
  5504. end;
  5505. procedure TBinaryObjectReader.BeginRootComponent;
  5506. begin
  5507. { Read filer signature }
  5508. ReadSignature;
  5509. end;
  5510. procedure TBinaryObjectReader.BeginComponent(var Flags: TFilerFlags;
  5511. var AChildPos: Integer; var CompClassName, CompName: String);
  5512. var
  5513. Prefix: Byte;
  5514. ValueType: TValueType;
  5515. begin
  5516. { Every component can start with a special prefix: }
  5517. Flags := [];
  5518. if (Byte(NextValue) and $f0) = $f0 then
  5519. begin
  5520. Prefix := Byte(ReadValue);
  5521. Flags:=[];
  5522. if (Prefix and $01)<>0 then
  5523. Include(Flags,ffInherited);
  5524. if (Prefix and $02)<>0 then
  5525. Include(Flags,ffChildPos);
  5526. if (Prefix and $04)<>0 then
  5527. Include(Flags,ffInline);
  5528. if ffChildPos in Flags then
  5529. begin
  5530. ValueType := ReadValue;
  5531. case ValueType of
  5532. vaInt8:
  5533. AChildPos := ReadInt8;
  5534. vaInt16:
  5535. AChildPos := ReadInt16;
  5536. vaInt32:
  5537. AChildPos := ReadInt32;
  5538. vaNativeInt:
  5539. AChildPos := ReadNativeInt;
  5540. else
  5541. raise EReadError.Create(SInvalidPropertyValue);
  5542. end;
  5543. end;
  5544. end;
  5545. CompClassName := ReadStr;
  5546. CompName := ReadStr;
  5547. end;
  5548. function TBinaryObjectReader.BeginProperty: String;
  5549. begin
  5550. Result := ReadStr;
  5551. end;
  5552. procedure TBinaryObjectReader.Read(var Buffer: TBytes; Count: Longint);
  5553. begin
  5554. FStream.Read(Buffer,Count);
  5555. end;
  5556. procedure TBinaryObjectReader.ReadBinary(const DestData: TMemoryStream);
  5557. var
  5558. BinSize: LongInt;
  5559. begin
  5560. BinSize:=LongInt(ReadDWord);
  5561. DestData.Size := BinSize;
  5562. DestData.CopyFrom(FStream,BinSize);
  5563. end;
  5564. function TBinaryObjectReader.ReadFloat: Extended;
  5565. begin
  5566. FStream.ReadBufferData(Result);
  5567. end;
  5568. function TBinaryObjectReader.ReadCurrency: Currency;
  5569. begin
  5570. Result:=ReadFloat;
  5571. end;
  5572. function TBinaryObjectReader.ReadIdent(ValueType: TValueType): String;
  5573. var
  5574. i: Byte;
  5575. c : Char;
  5576. begin
  5577. case ValueType of
  5578. vaIdent:
  5579. begin
  5580. FStream.ReadBufferData(i);
  5581. SetLength(Result,i);
  5582. For I:=1 to Length(Result) do
  5583. begin
  5584. FStream.ReadBufferData(C);
  5585. Result[I]:=C;
  5586. end;
  5587. end;
  5588. vaNil:
  5589. Result := 'nil';
  5590. vaFalse:
  5591. Result := 'False';
  5592. vaTrue:
  5593. Result := 'True';
  5594. vaNull:
  5595. Result := 'Null';
  5596. end;
  5597. end;
  5598. function TBinaryObjectReader.ReadInt8: ShortInt;
  5599. begin
  5600. FStream.ReadBufferData(Result);
  5601. end;
  5602. function TBinaryObjectReader.ReadInt16: SmallInt;
  5603. begin
  5604. FStream.ReadBufferData(Result);
  5605. end;
  5606. function TBinaryObjectReader.ReadInt32: LongInt;
  5607. begin
  5608. FStream.ReadBufferData(Result);
  5609. end;
  5610. function TBinaryObjectReader.ReadNativeInt : NativeInt;
  5611. begin
  5612. FStream.ReadBufferData(Result);
  5613. end;
  5614. function TBinaryObjectReader.ReadSet(EnumType: TTypeInfoEnum): Integer;
  5615. var
  5616. Name: String;
  5617. Value: Integer;
  5618. begin
  5619. try
  5620. Result := 0;
  5621. while True do
  5622. begin
  5623. Name := ReadStr;
  5624. if Length(Name) = 0 then
  5625. break;
  5626. Value:=EnumType.EnumType.NameToInt[Name];
  5627. if Value=-1 then
  5628. raise EReadError.Create(SInvalidPropertyValue);
  5629. Result:=Result or (1 shl Value);
  5630. end;
  5631. except
  5632. SkipSetBody;
  5633. raise;
  5634. end;
  5635. end;
  5636. Const
  5637. // Integer version of 4 chars 'TPF0'
  5638. FilerSignatureInt = 809914452;
  5639. procedure TBinaryObjectReader.ReadSignature;
  5640. var
  5641. Signature: LongInt;
  5642. begin
  5643. FStream.ReadBufferData(Signature);
  5644. if Signature <> FilerSignatureInt then
  5645. raise EReadError.Create(SInvalidImage);
  5646. end;
  5647. function TBinaryObjectReader.ReadStr: String;
  5648. var
  5649. l,i: Byte;
  5650. c : Char;
  5651. begin
  5652. FStream.ReadBufferData(L);
  5653. SetLength(Result,L);
  5654. For I:=1 to L do
  5655. begin
  5656. FStream.ReadBufferData(C);
  5657. Result[i]:=C;
  5658. end;
  5659. end;
  5660. function TBinaryObjectReader.ReadString(StringType: TValueType): String;
  5661. var
  5662. i: Integer;
  5663. C : Char;
  5664. begin
  5665. Result:='';
  5666. if StringType<>vaString then
  5667. Raise EFilerError.Create('Invalid string type passed to ReadString');
  5668. i:=ReadDWord;
  5669. SetLength(Result, i);
  5670. for I:=1 to Length(Result) do
  5671. begin
  5672. FStream.ReadbufferData(C);
  5673. Result[i]:=C;
  5674. end;
  5675. end;
  5676. function TBinaryObjectReader.ReadWideString: WideString;
  5677. begin
  5678. Result:=ReadString(vaWString);
  5679. end;
  5680. function TBinaryObjectReader.ReadUnicodeString: UnicodeString;
  5681. begin
  5682. Result:=ReadString(vaWString);
  5683. end;
  5684. procedure TBinaryObjectReader.SkipComponent(SkipComponentInfos: Boolean);
  5685. var
  5686. Flags: TFilerFlags;
  5687. Dummy: Integer;
  5688. CompClassName, CompName: String;
  5689. begin
  5690. if SkipComponentInfos then
  5691. { Skip prefix, component class name and component object name }
  5692. BeginComponent(Flags, Dummy, CompClassName, CompName);
  5693. { Skip properties }
  5694. while NextValue <> vaNull do
  5695. SkipProperty;
  5696. ReadValue;
  5697. { Skip children }
  5698. while NextValue <> vaNull do
  5699. SkipComponent(True);
  5700. ReadValue;
  5701. end;
  5702. procedure TBinaryObjectReader.SkipValue;
  5703. procedure SkipBytes(Count: LongInt);
  5704. var
  5705. Dummy: TBytes;
  5706. SkipNow: Integer;
  5707. begin
  5708. while Count > 0 do
  5709. begin
  5710. if Count > 1024 then
  5711. SkipNow := 1024
  5712. else
  5713. SkipNow := Count;
  5714. SetLength(Dummy,SkipNow);
  5715. Read(Dummy, SkipNow);
  5716. Dec(Count, SkipNow);
  5717. end;
  5718. end;
  5719. var
  5720. Count: LongInt;
  5721. begin
  5722. case ReadValue of
  5723. vaNull, vaFalse, vaTrue, vaNil: ;
  5724. vaList:
  5725. begin
  5726. while NextValue <> vaNull do
  5727. SkipValue;
  5728. ReadValue;
  5729. end;
  5730. vaInt8:
  5731. SkipBytes(1);
  5732. vaInt16:
  5733. SkipBytes(2);
  5734. vaInt32:
  5735. SkipBytes(4);
  5736. vaInt64,
  5737. vaDouble:
  5738. SkipBytes(8);
  5739. vaIdent:
  5740. ReadStr;
  5741. vaString:
  5742. ReadString(vaString);
  5743. vaBinary:
  5744. begin
  5745. Count:=LongInt(ReadDWord);
  5746. SkipBytes(Count);
  5747. end;
  5748. vaSet:
  5749. SkipSetBody;
  5750. vaCollection:
  5751. begin
  5752. while NextValue <> vaNull do
  5753. begin
  5754. { Skip the order value if present }
  5755. if NextValue in [vaInt8, vaInt16, vaInt32] then
  5756. SkipValue;
  5757. SkipBytes(1);
  5758. while NextValue <> vaNull do
  5759. SkipProperty;
  5760. ReadValue;
  5761. end;
  5762. ReadValue;
  5763. end;
  5764. end;
  5765. end;
  5766. { private methods }
  5767. procedure TBinaryObjectReader.SkipProperty;
  5768. begin
  5769. { Skip property name, then the property value }
  5770. ReadStr;
  5771. SkipValue;
  5772. end;
  5773. procedure TBinaryObjectReader.SkipSetBody;
  5774. begin
  5775. while Length(ReadStr) > 0 do;
  5776. end;
  5777. // Quadruple representing an unresolved component property.
  5778. Type
  5779. { TUnresolvedReference }
  5780. TUnresolvedReference = class(TlinkedListItem)
  5781. Private
  5782. FRoot: TComponent; // Root component when streaming
  5783. FPropInfo: TTypeMemberProperty; // Property to set.
  5784. FGlobal, // Global component.
  5785. FRelative : string; // Path relative to global component.
  5786. Function Resolve(Instance : TPersistent) : Boolean; // Resolve this reference
  5787. Function RootMatches(ARoot : TComponent) : Boolean; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE} // True if Froot matches or ARoot is nil.
  5788. Function NextRef : TUnresolvedReference; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  5789. end;
  5790. TLocalUnResolvedReference = class(TUnresolvedReference)
  5791. Finstance : TPersistent;
  5792. end;
  5793. // Linked list of TPersistent items that have unresolved properties.
  5794. { TUnResolvedInstance }
  5795. TUnResolvedInstance = Class(TLinkedListItem)
  5796. Public
  5797. Instance : TPersistent; // Instance we're handling unresolveds for
  5798. FUnresolved : TLinkedList; // The list
  5799. Destructor Destroy; override;
  5800. Function AddReference(ARoot : TComponent; APropInfo : TTypeMemberProperty; AGlobal,ARelative : String) : TUnresolvedReference;
  5801. Function RootUnresolved : TUnresolvedReference; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE} // Return root element in list.
  5802. Function ResolveReferences : Boolean; // Return true if all unresolveds were resolved.
  5803. end;
  5804. // Builds a list of TUnResolvedInstances, removes them from global list on free.
  5805. TBuildListVisitor = Class(TLinkedListVisitor)
  5806. Private
  5807. List : TFPList;
  5808. Public
  5809. Procedure Add(Item : TlinkedListItem); // Add TUnResolvedInstance item to list. Create list if needed
  5810. Destructor Destroy; override; // All elements in list (if any) are removed from the global list.
  5811. end;
  5812. // Visitor used to try and resolve instances in the global list
  5813. TResolveReferenceVisitor = Class(TBuildListVisitor)
  5814. Function Visit(Item : TLinkedListItem) : Boolean; override;
  5815. end;
  5816. // Visitor used to remove all references to a certain component.
  5817. TRemoveReferenceVisitor = Class(TBuildListVisitor)
  5818. Private
  5819. FRef : String;
  5820. FRoot : TComponent;
  5821. Public
  5822. Constructor Create(ARoot : TComponent;Const ARef : String);
  5823. Function Visit(Item : TLinkedListItem) : Boolean; override;
  5824. end;
  5825. // Visitor used to collect reference names.
  5826. TReferenceNamesVisitor = Class(TLinkedListVisitor)
  5827. Private
  5828. FList : TStrings;
  5829. FRoot : TComponent;
  5830. Public
  5831. Function Visit(Item : TLinkedListItem) : Boolean; override;
  5832. Constructor Create(ARoot : TComponent;AList : TStrings);
  5833. end;
  5834. // Visitor used to collect instance names.
  5835. TReferenceInstancesVisitor = Class(TLinkedListVisitor)
  5836. Private
  5837. FList : TStrings;
  5838. FRef : String;
  5839. FRoot : TComponent;
  5840. Public
  5841. Function Visit(Item : TLinkedListItem) : Boolean; override;
  5842. Constructor Create(ARoot : TComponent;Const ARef : String; AList : TStrings);
  5843. end;
  5844. // Visitor used to redirect links to another root component.
  5845. TRedirectReferenceVisitor = Class(TLinkedListVisitor)
  5846. Private
  5847. FOld,
  5848. FNew : String;
  5849. FRoot : TComponent;
  5850. Public
  5851. Function Visit(Item : TLinkedListItem) : Boolean; override;
  5852. Constructor Create(ARoot : TComponent;Const AOld,ANew : String);
  5853. end;
  5854. var
  5855. NeedResolving : TLinkedList;
  5856. // Add an instance to the global list of instances which need resolving.
  5857. Function FindUnresolvedInstance(AInstance: TPersistent) : TUnResolvedInstance;
  5858. begin
  5859. Result:=Nil;
  5860. {$ifdef FPC_HAS_FEATURE_THREADING}
  5861. EnterCriticalSection(ResolveSection);
  5862. Try
  5863. {$endif}
  5864. If Assigned(NeedResolving) then
  5865. begin
  5866. Result:=TUnResolvedInstance(NeedResolving.Root);
  5867. While (Result<>Nil) and (Result.Instance<>AInstance) do
  5868. Result:=TUnResolvedInstance(Result.Next);
  5869. end;
  5870. {$ifdef FPC_HAS_FEATURE_THREADING}
  5871. finally
  5872. LeaveCriticalSection(ResolveSection);
  5873. end;
  5874. {$endif}
  5875. end;
  5876. Function AddtoResolveList(AInstance: TPersistent) : TUnResolvedInstance;
  5877. begin
  5878. Result:=FindUnresolvedInstance(AInstance);
  5879. If (Result=Nil) then
  5880. begin
  5881. {$ifdef FPC_HAS_FEATURE_THREADING}
  5882. EnterCriticalSection(ResolveSection);
  5883. Try
  5884. {$endif}
  5885. If not Assigned(NeedResolving) then
  5886. NeedResolving:=TLinkedList.Create(TUnResolvedInstance);
  5887. Result:=NeedResolving.Add as TUnResolvedInstance;
  5888. Result.Instance:=AInstance;
  5889. {$ifdef FPC_HAS_FEATURE_THREADING}
  5890. finally
  5891. LeaveCriticalSection(ResolveSection);
  5892. end;
  5893. {$endif}
  5894. end;
  5895. end;
  5896. // Walk through the global list of instances to be resolved.
  5897. Procedure VisitResolveList(V : TLinkedListVisitor);
  5898. begin
  5899. {$ifdef FPC_HAS_FEATURE_THREADING}
  5900. EnterCriticalSection(ResolveSection);
  5901. Try
  5902. {$endif}
  5903. try
  5904. NeedResolving.Foreach(V);
  5905. Finally
  5906. FreeAndNil(V);
  5907. end;
  5908. {$ifdef FPC_HAS_FEATURE_THREADING}
  5909. Finally
  5910. LeaveCriticalSection(ResolveSection);
  5911. end;
  5912. {$endif}
  5913. end;
  5914. procedure GlobalFixupReferences;
  5915. begin
  5916. If (NeedResolving=Nil) then
  5917. Exit;
  5918. {$ifdef FPC_HAS_FEATURE_THREADING}
  5919. GlobalNameSpace.BeginWrite;
  5920. try
  5921. {$endif}
  5922. VisitResolveList(TResolveReferenceVisitor.Create);
  5923. {$ifdef FPC_HAS_FEATURE_THREADING}
  5924. finally
  5925. GlobalNameSpace.EndWrite;
  5926. end;
  5927. {$endif}
  5928. end;
  5929. procedure GetFixupReferenceNames(Root: TComponent; Names: TStrings);
  5930. begin
  5931. If (NeedResolving=Nil) then
  5932. Exit;
  5933. VisitResolveList(TReferenceNamesVisitor.Create(Root,Names));
  5934. end;
  5935. procedure GetFixupInstanceNames(Root: TComponent; const ReferenceRootName: string; Names: TStrings);
  5936. begin
  5937. If (NeedResolving=Nil) then
  5938. Exit;
  5939. VisitResolveList(TReferenceInstancesVisitor.Create(Root,ReferenceRootName,Names));
  5940. end;
  5941. procedure ObjectBinaryToText(aInput, aOutput: TStream);
  5942. begin
  5943. ObjectBinaryToText(aInput,aOutput,oteLFM);
  5944. end;
  5945. procedure ObjectBinaryToText(aInput, aOutput: TStream; aEncoding: TObjectTextEncoding);
  5946. var
  5947. Conv : TObjectStreamConverter;
  5948. begin
  5949. Conv:=TObjectStreamConverter.Create;
  5950. try
  5951. Conv.ObjectBinaryToText(aInput,aOutput,aEncoding);
  5952. finally
  5953. Conv.Free;
  5954. end;
  5955. end;
  5956. procedure RedirectFixupReferences(Root: TComponent; const OldRootName, NewRootName: string);
  5957. begin
  5958. If (NeedResolving=Nil) then
  5959. Exit;
  5960. VisitResolveList(TRedirectReferenceVisitor.Create(Root,OldRootName,NewRootName));
  5961. end;
  5962. procedure RemoveFixupReferences(Root: TComponent; const RootName: string);
  5963. begin
  5964. If (NeedResolving=Nil) then
  5965. Exit;
  5966. VisitResolveList(TRemoveReferenceVisitor.Create(Root,RootName));
  5967. end;
  5968. { TUnresolvedReference }
  5969. Function TUnresolvedReference.Resolve(Instance : TPersistent) : Boolean;
  5970. Var
  5971. C : TComponent;
  5972. begin
  5973. C:=FindGlobalComponent(FGlobal);
  5974. Result:=(C<>Nil);
  5975. If Result then
  5976. begin
  5977. C:=FindNestedComponent(C,FRelative);
  5978. Result:=C<>Nil;
  5979. If Result then
  5980. SetObjectProp(Instance, FPropInfo,C);
  5981. end;
  5982. end;
  5983. Function TUnresolvedReference.RootMatches(ARoot : TComponent) : Boolean; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  5984. begin
  5985. Result:=(ARoot=Nil) or (ARoot=FRoot);
  5986. end;
  5987. Function TUnResolvedReference.NextRef : TUnresolvedReference;
  5988. begin
  5989. Result:=TUnresolvedReference(Next);
  5990. end;
  5991. { TUnResolvedInstance }
  5992. destructor TUnResolvedInstance.Destroy;
  5993. begin
  5994. FUnresolved.Free;
  5995. inherited Destroy;
  5996. end;
  5997. function TUnResolvedInstance.AddReference(ARoot: TComponent; APropInfo : TTypeMemberProperty; AGlobal, ARelative: String): TUnresolvedReference;
  5998. begin
  5999. If (FUnResolved=Nil) then
  6000. FUnResolved:=TLinkedList.Create(TUnresolvedReference);
  6001. Result:=FUnResolved.Add as TUnresolvedReference;
  6002. Result.FGlobal:=AGLobal;
  6003. Result.FRelative:=ARelative;
  6004. Result.FPropInfo:=APropInfo;
  6005. Result.FRoot:=ARoot;
  6006. end;
  6007. Function TUnResolvedInstance.RootUnresolved : TUnresolvedReference;
  6008. begin
  6009. Result:=Nil;
  6010. If Assigned(FUnResolved) then
  6011. Result:=TUnresolvedReference(FUnResolved.Root);
  6012. end;
  6013. Function TUnResolvedInstance.ResolveReferences:Boolean;
  6014. Var
  6015. R,RN : TUnresolvedReference;
  6016. begin
  6017. R:=RootUnResolved;
  6018. While (R<>Nil) do
  6019. begin
  6020. RN:=R.NextRef;
  6021. If R.Resolve(Self.Instance) then
  6022. FUnresolved.RemoveItem(R,True);
  6023. R:=RN;
  6024. end;
  6025. Result:=RootUnResolved=Nil;
  6026. end;
  6027. { TReferenceNamesVisitor }
  6028. Constructor TReferenceNamesVisitor.Create(ARoot : TComponent;AList : TStrings);
  6029. begin
  6030. FRoot:=ARoot;
  6031. FList:=AList;
  6032. end;
  6033. Function TReferenceNamesVisitor.Visit(Item : TLinkedListItem) : Boolean;
  6034. Var
  6035. R : TUnresolvedReference;
  6036. begin
  6037. R:=TUnResolvedInstance(Item).RootUnresolved;
  6038. While (R<>Nil) do
  6039. begin
  6040. If R.RootMatches(FRoot) then
  6041. If (FList.IndexOf(R.FGlobal)=-1) then
  6042. FList.Add(R.FGlobal);
  6043. R:=R.NextRef;
  6044. end;
  6045. Result:=True;
  6046. end;
  6047. { TReferenceInstancesVisitor }
  6048. Constructor TReferenceInstancesVisitor.Create(ARoot : TComponent; Const ARef : String;AList : TStrings);
  6049. begin
  6050. FRoot:=ARoot;
  6051. FRef:=UpperCase(ARef);
  6052. FList:=AList;
  6053. end;
  6054. Function TReferenceInstancesVisitor.Visit(Item : TLinkedListItem) : Boolean;
  6055. Var
  6056. R : TUnresolvedReference;
  6057. begin
  6058. R:=TUnResolvedInstance(Item).RootUnresolved;
  6059. While (R<>Nil) do
  6060. begin
  6061. If (FRoot=R.FRoot) and (FRef=UpperCase(R.FGLobal)) Then
  6062. If Flist.IndexOf(R.FRelative)=-1 then
  6063. Flist.Add(R.FRelative);
  6064. R:=R.NextRef;
  6065. end;
  6066. Result:=True;
  6067. end;
  6068. { TRedirectReferenceVisitor }
  6069. Constructor TRedirectReferenceVisitor.Create(ARoot : TComponent; Const AOld,ANew : String);
  6070. begin
  6071. FRoot:=ARoot;
  6072. FOld:=UpperCase(AOld);
  6073. FNew:=ANew;
  6074. end;
  6075. Function TRedirectReferenceVisitor.Visit(Item : TLinkedListItem) : Boolean;
  6076. Var
  6077. R : TUnresolvedReference;
  6078. begin
  6079. R:=TUnResolvedInstance(Item).RootUnresolved;
  6080. While (R<>Nil) do
  6081. begin
  6082. If R.RootMatches(FRoot) and (FOld=UpperCase(R.FGLobal)) Then
  6083. R.FGlobal:=FNew;
  6084. R:=R.NextRef;
  6085. end;
  6086. Result:=True;
  6087. end;
  6088. { TRemoveReferenceVisitor }
  6089. Constructor TRemoveReferenceVisitor.Create(ARoot : TComponent; Const ARef : String);
  6090. begin
  6091. FRoot:=ARoot;
  6092. FRef:=UpperCase(ARef);
  6093. end;
  6094. Function TRemoveReferenceVisitor.Visit(Item : TLinkedListItem) : Boolean;
  6095. Var
  6096. I : Integer;
  6097. UI : TUnResolvedInstance;
  6098. R : TUnresolvedReference;
  6099. L : TFPList;
  6100. begin
  6101. UI:=TUnResolvedInstance(Item);
  6102. R:=UI.RootUnresolved;
  6103. L:=Nil;
  6104. Try
  6105. // Collect all matches.
  6106. While (R<>Nil) do
  6107. begin
  6108. If R.RootMatches(FRoot) and ((FRef = '') or (FRef=UpperCase(R.FGLobal))) Then
  6109. begin
  6110. If Not Assigned(L) then
  6111. L:=TFPList.Create;
  6112. L.Add(R);
  6113. end;
  6114. R:=R.NextRef;
  6115. end;
  6116. // Remove all matches.
  6117. IF Assigned(L) then
  6118. begin
  6119. For I:=0 to L.Count-1 do
  6120. UI.FUnresolved.RemoveItem(TLinkedListitem(L[i]),True);
  6121. end;
  6122. // If any references are left, leave them.
  6123. If UI.FUnResolved.Root=Nil then
  6124. begin
  6125. If List=Nil then
  6126. List:=TFPList.Create;
  6127. List.Add(UI);
  6128. end;
  6129. Finally
  6130. L.Free;
  6131. end;
  6132. Result:=True;
  6133. end;
  6134. { TBuildListVisitor }
  6135. Procedure TBuildListVisitor.Add(Item : TlinkedListItem);
  6136. begin
  6137. If (List=Nil) then
  6138. List:=TFPList.Create;
  6139. List.Add(Item);
  6140. end;
  6141. Destructor TBuildListVisitor.Destroy;
  6142. Var
  6143. I : Integer;
  6144. begin
  6145. If Assigned(List) then
  6146. For I:=0 to List.Count-1 do
  6147. NeedResolving.RemoveItem(TLinkedListItem(List[I]),True);
  6148. FreeAndNil(List);
  6149. Inherited;
  6150. end;
  6151. { TResolveReferenceVisitor }
  6152. Function TResolveReferenceVisitor.Visit(Item : TLinkedListItem) : Boolean;
  6153. begin
  6154. If TUnResolvedInstance(Item).ResolveReferences then
  6155. Add(Item);
  6156. Result:=True;
  6157. end;
  6158. {****************************************************************************}
  6159. {* TREADER *}
  6160. {****************************************************************************}
  6161. constructor TReader.Create(Stream: TStream);
  6162. begin
  6163. inherited Create;
  6164. If (Stream=Nil) then
  6165. Raise EReadError.Create(SEmptyStreamIllegalReader);
  6166. FDriver := CreateDriver(Stream);
  6167. end;
  6168. destructor TReader.Destroy;
  6169. begin
  6170. FDriver.Free;
  6171. inherited Destroy;
  6172. end;
  6173. procedure TReader.FlushBuffer;
  6174. begin
  6175. Driver.FlushBuffer;
  6176. end;
  6177. function TReader.CreateDriver(Stream: TStream): TAbstractObjectReader;
  6178. begin
  6179. Result := TBinaryObjectReader.Create(Stream);
  6180. end;
  6181. procedure TReader.BeginReferences;
  6182. begin
  6183. FLoaded := TFpList.Create;
  6184. end;
  6185. procedure TReader.CheckValue(Value: TValueType);
  6186. begin
  6187. if FDriver.NextValue <> Value then
  6188. raise EReadError.Create(SInvalidPropertyValue)
  6189. else
  6190. FDriver.ReadValue;
  6191. end;
  6192. procedure TReader.DefineProperty(const Name: String; AReadData: TReaderProc;
  6193. WriteData: TWriterProc; HasData: Boolean);
  6194. begin
  6195. if Assigned(AReadData) and SameText(Name,FPropName) then
  6196. begin
  6197. AReadData(Self);
  6198. SetLength(FPropName, 0);
  6199. end else if assigned(WriteData) and HasData then
  6200. ;
  6201. end;
  6202. procedure TReader.DefineBinaryProperty(const Name: String;
  6203. AReadData, WriteData: TStreamProc; HasData: Boolean);
  6204. var
  6205. MemBuffer: TMemoryStream;
  6206. begin
  6207. if Assigned(AReadData) and SameText(Name,FPropName) then
  6208. begin
  6209. { Check if the next property really is a binary property}
  6210. if FDriver.NextValue <> vaBinary then
  6211. begin
  6212. FDriver.SkipValue;
  6213. FCanHandleExcepts := True;
  6214. raise EReadError.Create(SInvalidPropertyValue);
  6215. end else
  6216. FDriver.ReadValue;
  6217. MemBuffer := TMemoryStream.Create;
  6218. try
  6219. FDriver.ReadBinary(MemBuffer);
  6220. FCanHandleExcepts := True;
  6221. AReadData(MemBuffer);
  6222. finally
  6223. MemBuffer.Free;
  6224. end;
  6225. SetLength(FPropName, 0);
  6226. end else if assigned(WriteData) and HasData then ;
  6227. end;
  6228. function TReader.EndOfList: Boolean;
  6229. begin
  6230. Result := FDriver.NextValue = vaNull;
  6231. end;
  6232. procedure TReader.EndReferences;
  6233. begin
  6234. FLoaded.Free;
  6235. FLoaded := nil;
  6236. end;
  6237. function TReader.Error(const Message: String): Boolean;
  6238. begin
  6239. Result := False;
  6240. if Assigned(FOnError) then
  6241. FOnError(Self, Message, Result);
  6242. end;
  6243. function TReader.FindMethod(ARoot: TComponent; const AMethodName: String): CodePointer;
  6244. var
  6245. ErrorResult: Boolean;
  6246. begin
  6247. Result:=nil;
  6248. if (ARoot=Nil) or (aMethodName='') then
  6249. exit;
  6250. Result := ARoot.MethodAddress(AMethodName);
  6251. ErrorResult := Result = nil;
  6252. { always give the OnFindMethod callback a chance to locate the method }
  6253. if Assigned(FOnFindMethod) then
  6254. FOnFindMethod(Self, AMethodName, Result, ErrorResult);
  6255. if ErrorResult then
  6256. raise EReadError.Create(SInvalidPropertyValue);
  6257. end;
  6258. procedure TReader.DoFixupReferences;
  6259. Var
  6260. R,RN : TLocalUnresolvedReference;
  6261. G : TUnresolvedInstance;
  6262. Ref : String;
  6263. C : TComponent;
  6264. P : integer;
  6265. L : TLinkedList;
  6266. begin
  6267. If Assigned(FFixups) then
  6268. begin
  6269. L:=TLinkedList(FFixups);
  6270. R:=TLocalUnresolvedReference(L.Root);
  6271. While (R<>Nil) do
  6272. begin
  6273. RN:=TLocalUnresolvedReference(R.Next);
  6274. Ref:=R.FRelative;
  6275. If Assigned(FOnReferenceName) then
  6276. FOnReferenceName(Self,Ref);
  6277. C:=FindNestedComponent(R.FRoot,Ref);
  6278. If Assigned(C) then
  6279. if R.FPropInfo.TypeInfo.Kind = tkInterface then
  6280. SetInterfaceProp(R.FInstance,R.FPropInfo,C)
  6281. else
  6282. SetObjectProp(R.FInstance,R.FPropInfo,C)
  6283. else
  6284. begin
  6285. P:=Pos('.',R.FRelative);
  6286. If (P<>0) then
  6287. begin
  6288. G:=AddToResolveList(R.FInstance);
  6289. G.Addreference(R.FRoot,R.FPropInfo,Copy(R.FRelative,1,P-1),Copy(R.FRelative,P+1,Length(R.FRelative)-P));
  6290. end;
  6291. end;
  6292. L.RemoveItem(R,True);
  6293. R:=RN;
  6294. end;
  6295. FreeAndNil(FFixups);
  6296. end;
  6297. end;
  6298. procedure TReader.FixupReferences;
  6299. var
  6300. i: Integer;
  6301. begin
  6302. DoFixupReferences;
  6303. GlobalFixupReferences;
  6304. for i := 0 to FLoaded.Count - 1 do
  6305. TComponent(FLoaded[I]).Loaded;
  6306. end;
  6307. function TReader.NextValue: TValueType;
  6308. begin
  6309. Result := FDriver.NextValue;
  6310. end;
  6311. procedure TReader.Read(var Buffer : TBytes; Count: LongInt);
  6312. begin
  6313. //This should give an exception if read is not implemented (i.e. TTextObjectReader)
  6314. //but should work with TBinaryObjectReader.
  6315. Driver.Read(Buffer, Count);
  6316. end;
  6317. procedure TReader.PropertyError;
  6318. begin
  6319. FDriver.SkipValue;
  6320. raise EReadError.CreateFmt(SUnknownProperty,[FPropName]);
  6321. end;
  6322. function TReader.ReadBoolean: Boolean;
  6323. var
  6324. ValueType: TValueType;
  6325. begin
  6326. ValueType := FDriver.ReadValue;
  6327. if ValueType = vaTrue then
  6328. Result := True
  6329. else if ValueType = vaFalse then
  6330. Result := False
  6331. else
  6332. raise EReadError.Create(SInvalidPropertyValue);
  6333. end;
  6334. function TReader.ReadChar: Char;
  6335. var
  6336. s: String;
  6337. begin
  6338. s := ReadString;
  6339. if Length(s) = 1 then
  6340. Result := s[1]
  6341. else
  6342. raise EReadError.Create(SInvalidPropertyValue);
  6343. end;
  6344. function TReader.ReadWideChar: WideChar;
  6345. var
  6346. W: WideString;
  6347. begin
  6348. W := ReadWideString;
  6349. if Length(W) = 1 then
  6350. Result := W[1]
  6351. else
  6352. raise EReadError.Create(SInvalidPropertyValue);
  6353. end;
  6354. function TReader.ReadUnicodeChar: UnicodeChar;
  6355. var
  6356. U: UnicodeString;
  6357. begin
  6358. U := ReadUnicodeString;
  6359. if Length(U) = 1 then
  6360. Result := U[1]
  6361. else
  6362. raise EReadError.Create(SInvalidPropertyValue);
  6363. end;
  6364. procedure TReader.ReadCollection(Collection: TCollection);
  6365. var
  6366. Item: TCollectionItem;
  6367. begin
  6368. Collection.BeginUpdate;
  6369. if not EndOfList then
  6370. Collection.Clear;
  6371. while not EndOfList do begin
  6372. ReadListBegin;
  6373. Item := Collection.Add;
  6374. while NextValue<>vaNull do
  6375. ReadProperty(Item);
  6376. ReadListEnd;
  6377. end;
  6378. Collection.EndUpdate;
  6379. ReadListEnd;
  6380. end;
  6381. function TReader.ReadComponent(Component: TComponent): TComponent;
  6382. var
  6383. Flags: TFilerFlags;
  6384. function Recover(E : Exception; var aComponent: TComponent): Boolean;
  6385. begin
  6386. Result := False;
  6387. if not ((ffInherited in Flags) or Assigned(Component)) then
  6388. aComponent.Free;
  6389. aComponent := nil;
  6390. FDriver.SkipComponent(False);
  6391. Result := Error(E.Message);
  6392. end;
  6393. var
  6394. CompClassName, Name: String;
  6395. n, ChildPos: Integer;
  6396. SavedParent, SavedLookupRoot: TComponent;
  6397. ComponentClass: TComponentClass;
  6398. C, NewComponent: TComponent;
  6399. SubComponents: TList;
  6400. begin
  6401. FDriver.BeginComponent(Flags, ChildPos, CompClassName, Name);
  6402. SavedParent := Parent;
  6403. SavedLookupRoot := FLookupRoot;
  6404. SubComponents := nil;
  6405. try
  6406. Result := Component;
  6407. if not Assigned(Result) then
  6408. try
  6409. if ffInherited in Flags then
  6410. begin
  6411. { Try to locate the existing ancestor component }
  6412. if Assigned(FLookupRoot) then
  6413. Result := FLookupRoot.FindComponent(Name)
  6414. else
  6415. Result := nil;
  6416. if not Assigned(Result) then
  6417. begin
  6418. if Assigned(FOnAncestorNotFound) then
  6419. FOnAncestorNotFound(Self, Name,
  6420. FindComponentClass(CompClassName), Result);
  6421. if not Assigned(Result) then
  6422. raise EReadError.CreateFmt(SAncestorNotFound, [Name]);
  6423. end;
  6424. Parent := Result.GetParentComponent;
  6425. if not Assigned(Parent) then
  6426. Parent := Root;
  6427. end else
  6428. begin
  6429. Result := nil;
  6430. ComponentClass := FindComponentClass(CompClassName);
  6431. if Assigned(FOnCreateComponent) then
  6432. FOnCreateComponent(Self, ComponentClass, Result);
  6433. if not Assigned(Result) then
  6434. begin
  6435. asm
  6436. NewComponent = Object.create(ComponentClass);
  6437. NewComponent.$init();
  6438. end;
  6439. if ffInline in Flags then
  6440. NewComponent.FComponentState :=
  6441. NewComponent.FComponentState + [csLoading, csInline];
  6442. NewComponent.Create(Owner);
  6443. NewComponent.AfterConstruction;
  6444. { Don't set Result earlier because else we would come in trouble
  6445. with the exception recover mechanism! (Result should be NIL if
  6446. an error occurred) }
  6447. Result := NewComponent;
  6448. end;
  6449. Include(Result.FComponentState, csLoading);
  6450. end;
  6451. except
  6452. On E: Exception do
  6453. if not Recover(E,Result) then
  6454. raise;
  6455. end;
  6456. if Assigned(Result) then
  6457. try
  6458. Include(Result.FComponentState, csLoading);
  6459. { create list of subcomponents and set loading}
  6460. SubComponents := TList.Create;
  6461. for n := 0 to Result.ComponentCount - 1 do
  6462. begin
  6463. C := Result.Components[n];
  6464. if csSubcomponent in C.ComponentStyle
  6465. then begin
  6466. SubComponents.Add(C);
  6467. Include(C.FComponentState, csLoading);
  6468. end;
  6469. end;
  6470. if not (ffInherited in Flags) then
  6471. try
  6472. Result.SetParentComponent(Parent);
  6473. if Assigned(FOnSetName) then
  6474. FOnSetName(Self, Result, Name);
  6475. Result.Name := Name;
  6476. if FindGlobalComponent(Name) = Result then
  6477. Include(Result.FComponentState, csInline);
  6478. except
  6479. On E : Exception do
  6480. if not Recover(E,Result) then
  6481. raise;
  6482. end;
  6483. if not Assigned(Result) then
  6484. exit;
  6485. if csInline in Result.ComponentState then
  6486. FLookupRoot := Result;
  6487. { Read the component state }
  6488. Include(Result.FComponentState, csReading);
  6489. for n := 0 to Subcomponents.Count - 1 do
  6490. Include(TComponent(Subcomponents[n]).FComponentState, csReading);
  6491. Result.ReadState(Self);
  6492. Exclude(Result.FComponentState, csReading);
  6493. for n := 0 to Subcomponents.Count - 1 do
  6494. Exclude(TComponent(Subcomponents[n]).FComponentState, csReading);
  6495. if ffChildPos in Flags then
  6496. Parent.SetChildOrder(Result, ChildPos);
  6497. { Add component to list of loaded components, if necessary }
  6498. if (not ((ffInherited in Flags) or (csInline in Result.ComponentState))) or
  6499. (FLoaded.IndexOf(Result) < 0)
  6500. then begin
  6501. for n := 0 to Subcomponents.Count - 1 do
  6502. FLoaded.Add(Subcomponents[n]);
  6503. FLoaded.Add(Result);
  6504. end;
  6505. except
  6506. if ((ffInherited in Flags) or Assigned(Component)) then
  6507. Result.Free;
  6508. raise;
  6509. end;
  6510. finally
  6511. Parent := SavedParent;
  6512. FLookupRoot := SavedLookupRoot;
  6513. Subcomponents.Free;
  6514. end;
  6515. end;
  6516. procedure TReader.ReadData(Instance: TComponent);
  6517. var
  6518. SavedOwner, SavedParent: TComponent;
  6519. begin
  6520. { Read properties }
  6521. while not EndOfList do
  6522. ReadProperty(Instance);
  6523. ReadListEnd;
  6524. { Read children }
  6525. SavedOwner := Owner;
  6526. SavedParent := Parent;
  6527. try
  6528. Owner := Instance.GetChildOwner;
  6529. if not Assigned(Owner) then
  6530. Owner := Root;
  6531. Parent := Instance.GetChildParent;
  6532. while not EndOfList do
  6533. ReadComponent(nil);
  6534. ReadListEnd;
  6535. finally
  6536. Owner := SavedOwner;
  6537. Parent := SavedParent;
  6538. end;
  6539. { Fixup references if necessary (normally only if this is the root) }
  6540. If (Instance=FRoot) then
  6541. DoFixupReferences;
  6542. end;
  6543. function TReader.ReadFloat: Extended;
  6544. begin
  6545. if FDriver.NextValue = vaExtended then
  6546. begin
  6547. ReadValue;
  6548. Result := FDriver.ReadFloat
  6549. end else
  6550. Result := ReadNativeInt;
  6551. end;
  6552. procedure TReader.ReadSignature;
  6553. begin
  6554. FDriver.ReadSignature;
  6555. end;
  6556. function TReader.ReadCurrency: Currency;
  6557. begin
  6558. if FDriver.NextValue = vaCurrency then
  6559. begin
  6560. FDriver.ReadValue;
  6561. Result := FDriver.ReadCurrency;
  6562. end else
  6563. Result := ReadInteger;
  6564. end;
  6565. function TReader.ReadIdent: String;
  6566. var
  6567. ValueType: TValueType;
  6568. begin
  6569. ValueType := FDriver.ReadValue;
  6570. if ValueType in [vaIdent, vaNil, vaFalse, vaTrue, vaNull] then
  6571. Result := FDriver.ReadIdent(ValueType)
  6572. else
  6573. raise EReadError.Create(SInvalidPropertyValue);
  6574. end;
  6575. function TReader.ReadInteger: LongInt;
  6576. begin
  6577. case FDriver.ReadValue of
  6578. vaInt8:
  6579. Result := FDriver.ReadInt8;
  6580. vaInt16:
  6581. Result := FDriver.ReadInt16;
  6582. vaInt32:
  6583. Result := FDriver.ReadInt32;
  6584. else
  6585. raise EReadError.Create(SInvalidPropertyValue);
  6586. end;
  6587. end;
  6588. function TReader.ReadNativeInt: NativeInt;
  6589. begin
  6590. if FDriver.NextValue = vaInt64 then
  6591. begin
  6592. FDriver.ReadValue;
  6593. Result := FDriver.ReadNativeInt;
  6594. end else
  6595. Result := ReadInteger;
  6596. end;
  6597. function TReader.ReadSet(EnumType: Pointer): Integer;
  6598. begin
  6599. if FDriver.NextValue = vaSet then
  6600. begin
  6601. FDriver.ReadValue;
  6602. Result := FDriver.ReadSet(enumtype);
  6603. end
  6604. else
  6605. Result := ReadInteger;
  6606. end;
  6607. procedure TReader.ReadListBegin;
  6608. begin
  6609. CheckValue(vaList);
  6610. end;
  6611. procedure TReader.ReadListEnd;
  6612. begin
  6613. CheckValue(vaNull);
  6614. end;
  6615. function TReader.ReadVariant: JSValue;
  6616. var
  6617. nv: TValueType;
  6618. begin
  6619. nv:=NextValue;
  6620. case nv of
  6621. vaNil:
  6622. begin
  6623. Result:=Undefined;
  6624. readvalue;
  6625. end;
  6626. vaNull:
  6627. begin
  6628. Result:=Nil;
  6629. readvalue;
  6630. end;
  6631. { all integer sizes must be split for big endian systems }
  6632. vaInt8,vaInt16,vaInt32:
  6633. begin
  6634. Result:=ReadInteger;
  6635. end;
  6636. vaInt64:
  6637. begin
  6638. Result:=ReadNativeInt;
  6639. end;
  6640. {
  6641. vaQWord:
  6642. begin
  6643. Result:=QWord(ReadInt64);
  6644. end;
  6645. } vaFalse,vaTrue:
  6646. begin
  6647. Result:=(nv<>vaFalse);
  6648. readValue;
  6649. end;
  6650. vaCurrency:
  6651. begin
  6652. Result:=ReadCurrency;
  6653. end;
  6654. vaDouble:
  6655. begin
  6656. Result:=ReadFloat;
  6657. end;
  6658. vaString:
  6659. begin
  6660. Result:=ReadString;
  6661. end;
  6662. else
  6663. raise EReadError.CreateFmt(SUnsupportedPropertyVariantType, [Ord(nv)]);
  6664. end;
  6665. end;
  6666. procedure TReader.ReadProperty(AInstance: TPersistent);
  6667. var
  6668. Path: String;
  6669. Instance: TPersistent;
  6670. PropInfo: TTypeMemberProperty;
  6671. Obj: TObject;
  6672. Name: String;
  6673. Skip: Boolean;
  6674. Handled: Boolean;
  6675. OldPropName: String;
  6676. DotPos : String;
  6677. NextPos: Integer;
  6678. function HandleMissingProperty(IsPath: Boolean): boolean;
  6679. begin
  6680. Result:=true;
  6681. if Assigned(OnPropertyNotFound) then begin
  6682. // user defined property error handling
  6683. OldPropName:=FPropName;
  6684. Handled:=false;
  6685. Skip:=false;
  6686. OnPropertyNotFound(Self,Instance,FPropName,IsPath,Handled,Skip);
  6687. if Handled and (not Skip) and (OldPropName<>FPropName) then
  6688. // try alias property
  6689. PropInfo := GetPropInfo(Instance.ClassType, FPropName);
  6690. if Skip then begin
  6691. FDriver.SkipValue;
  6692. Result:=false;
  6693. exit;
  6694. end;
  6695. end;
  6696. end;
  6697. begin
  6698. try
  6699. Path := FDriver.BeginProperty;
  6700. try
  6701. Instance := AInstance;
  6702. FCanHandleExcepts := True;
  6703. DotPos := Path;
  6704. while True do
  6705. begin
  6706. NextPos := Pos('.',DotPos);
  6707. if NextPos>0 then
  6708. FPropName := Copy(DotPos, 1, NextPos-1)
  6709. else
  6710. begin
  6711. FPropName := DotPos;
  6712. break;
  6713. end;
  6714. Delete(DotPos,1,NextPos);
  6715. PropInfo := GetPropInfo(Instance.ClassType, FPropName);
  6716. if not Assigned(PropInfo) then begin
  6717. if not HandleMissingProperty(true) then exit;
  6718. if not Assigned(PropInfo) then
  6719. PropertyError;
  6720. end;
  6721. if PropInfo.TypeInfo.Kind = tkClass then
  6722. Obj := TObject(GetObjectProp(Instance, PropInfo))
  6723. //else if PropInfo^.PropType^.Kind = tkInterface then
  6724. // Obj := TObject(GetInterfaceProp(Instance, PropInfo))
  6725. else
  6726. Obj := nil;
  6727. if not (Obj is TPersistent) then
  6728. begin
  6729. { All path elements must be persistent objects! }
  6730. FDriver.SkipValue;
  6731. raise EReadError.Create(SInvalidPropertyPath);
  6732. end;
  6733. Instance := TPersistent(Obj);
  6734. end;
  6735. PropInfo := GetPropInfo(Instance.ClassType, FPropName);
  6736. if Assigned(PropInfo) then
  6737. ReadPropValue(Instance, PropInfo)
  6738. else
  6739. begin
  6740. FCanHandleExcepts := False;
  6741. Instance.DefineProperties(Self);
  6742. FCanHandleExcepts := True;
  6743. if Length(FPropName) > 0 then begin
  6744. if not HandleMissingProperty(false) then exit;
  6745. if not Assigned(PropInfo) then
  6746. PropertyError;
  6747. end;
  6748. end;
  6749. except
  6750. on e: Exception do
  6751. begin
  6752. SetLength(Name, 0);
  6753. if AInstance.InheritsFrom(TComponent) then
  6754. Name := TComponent(AInstance).Name;
  6755. if Length(Name) = 0 then
  6756. Name := AInstance.ClassName;
  6757. raise EReadError.CreateFmt(SPropertyException, [Name, '.', Path, e.Message]);
  6758. end;
  6759. end;
  6760. except
  6761. on e: Exception do
  6762. if not FCanHandleExcepts or not Error(E.Message) then
  6763. raise;
  6764. end;
  6765. end;
  6766. procedure TReader.ReadPropValue(Instance: TPersistent; PropInfo: TTypeMemberProperty);
  6767. const
  6768. NullMethod: TMethod = (Code: nil; Data: nil);
  6769. var
  6770. PropType: TTypeInfo;
  6771. Value: LongInt;
  6772. { IdentToIntFn: TIdentToInt; }
  6773. Ident: String;
  6774. Method: TMethod;
  6775. Handled: Boolean;
  6776. TmpStr: String;
  6777. begin
  6778. if (PropInfo.Setter='') then
  6779. raise EReadError.Create(SReadOnlyProperty);
  6780. PropType := PropInfo.TypeInfo;
  6781. case PropType.Kind of
  6782. tkInteger:
  6783. case FDriver.NextValue of
  6784. vaIdent :
  6785. begin
  6786. Ident := ReadIdent;
  6787. if GlobalIdentToInt(Ident,Value) then
  6788. SetOrdProp(Instance, PropInfo, Value)
  6789. else
  6790. raise EReadError.Create(SInvalidPropertyValue);
  6791. end;
  6792. vaNativeInt :
  6793. SetOrdProp(Instance, PropInfo, ReadNativeInt);
  6794. vaCurrency:
  6795. SetFloatProp(Instance, PropInfo, ReadCurrency);
  6796. else
  6797. SetOrdProp(Instance, PropInfo, ReadInteger);
  6798. end;
  6799. tkBool:
  6800. SetOrdProp(Instance, PropInfo, Ord(ReadBoolean));
  6801. tkChar:
  6802. SetOrdProp(Instance, PropInfo, Ord(ReadChar));
  6803. tkEnumeration:
  6804. begin
  6805. Value := GetEnumValue(TTypeInfoEnum(PropType), ReadIdent);
  6806. if Value = -1 then
  6807. raise EReadError.Create(SInvalidPropertyValue);
  6808. SetOrdProp(Instance, PropInfo, Value);
  6809. end;
  6810. {$ifndef FPUNONE}
  6811. tkFloat:
  6812. SetFloatProp(Instance, PropInfo, ReadFloat);
  6813. {$endif}
  6814. tkSet:
  6815. begin
  6816. CheckValue(vaSet);
  6817. if TTypeInfoSet(PropType).CompType.Kind=tkEnumeration then
  6818. SetOrdProp(Instance, PropInfo, FDriver.ReadSet(TTypeInfoEnum(TTypeInfoSet(PropType).CompType)));
  6819. end;
  6820. tkMethod, tkRefToProcVar:
  6821. if FDriver.NextValue = vaNil then
  6822. begin
  6823. FDriver.ReadValue;
  6824. SetMethodProp(Instance, PropInfo, NullMethod);
  6825. end else
  6826. begin
  6827. Handled:=false;
  6828. Ident:=ReadIdent;
  6829. if Assigned(OnSetMethodProperty) then
  6830. OnSetMethodProperty(Self,Instance,PropInfo,Ident,Handled);
  6831. if not Handled then begin
  6832. Method.Code := FindMethod(Root, Ident);
  6833. Method.Data := Root;
  6834. if Assigned(Method.Code) then
  6835. SetMethodProp(Instance, PropInfo, Method);
  6836. end;
  6837. end;
  6838. tkString:
  6839. begin
  6840. TmpStr:=ReadString;
  6841. if Assigned(FOnReadStringProperty) then
  6842. FOnReadStringProperty(Self,Instance,PropInfo,TmpStr);
  6843. SetStrProp(Instance, PropInfo, TmpStr);
  6844. end;
  6845. tkJSValue:
  6846. begin
  6847. SetJSValueProp(Instance,PropInfo,ReadVariant);
  6848. end;
  6849. tkClass, tkInterface:
  6850. case FDriver.NextValue of
  6851. vaNil:
  6852. begin
  6853. FDriver.ReadValue;
  6854. SetOrdProp(Instance, PropInfo, 0)
  6855. end;
  6856. vaCollection:
  6857. begin
  6858. FDriver.ReadValue;
  6859. ReadCollection(TCollection(GetObjectProp(Instance, PropInfo)));
  6860. end
  6861. else
  6862. begin
  6863. If Not Assigned(FFixups) then
  6864. FFixups:=TLinkedList.Create(TLocalUnresolvedReference);
  6865. With TLocalUnresolvedReference(TLinkedList(FFixups).Add) do
  6866. begin
  6867. FInstance:=Instance;
  6868. FRoot:=Root;
  6869. FPropInfo:=PropInfo;
  6870. FRelative:=ReadIdent;
  6871. end;
  6872. end;
  6873. end;
  6874. {tkint64:
  6875. SetInt64Prop(Instance, PropInfo, ReadInt64);}
  6876. else
  6877. raise EReadError.CreateFmt(SUnknownPropertyType, [Str(PropType.Kind)]);
  6878. end;
  6879. end;
  6880. function TReader.ReadRootComponent(ARoot: TComponent): TComponent;
  6881. var
  6882. Dummy, i: Integer;
  6883. Flags: TFilerFlags;
  6884. CompClassName, CompName, ResultName: String;
  6885. begin
  6886. FDriver.BeginRootComponent;
  6887. Result := nil;
  6888. {!!!: GlobalNameSpace.BeginWrite; // Loading from stream adds to name space
  6889. try}
  6890. try
  6891. FDriver.BeginComponent(Flags, Dummy, CompClassName, CompName);
  6892. if not Assigned(ARoot) then
  6893. begin
  6894. { Read the class name and the object name and create a new object: }
  6895. Result := TComponentClass(FindClass(CompClassName)).Create(nil);
  6896. Result.Name := CompName;
  6897. end else
  6898. begin
  6899. Result := ARoot;
  6900. if not (csDesigning in Result.ComponentState) then
  6901. begin
  6902. Result.FComponentState :=
  6903. Result.FComponentState + [csLoading, csReading];
  6904. { We need an unique name }
  6905. i := 0;
  6906. { Don't use Result.Name directly, as this would influence
  6907. FindGlobalComponent in successive loop runs }
  6908. ResultName := CompName;
  6909. while Assigned(FindGlobalComponent(ResultName)) do
  6910. begin
  6911. Inc(i);
  6912. ResultName := CompName + '_' + IntToStr(i);
  6913. end;
  6914. Result.Name := ResultName;
  6915. end;
  6916. end;
  6917. FRoot := Result;
  6918. FLookupRoot := Result;
  6919. if Assigned(GlobalLoaded) then
  6920. FLoaded := GlobalLoaded
  6921. else
  6922. FLoaded := TFpList.Create;
  6923. try
  6924. if FLoaded.IndexOf(FRoot) < 0 then
  6925. FLoaded.Add(FRoot);
  6926. FOwner := FRoot;
  6927. FRoot.FComponentState := FRoot.FComponentState + [csLoading, csReading];
  6928. FRoot.ReadState(Self);
  6929. Exclude(FRoot.FComponentState, csReading);
  6930. if not Assigned(GlobalLoaded) then
  6931. for i := 0 to FLoaded.Count - 1 do
  6932. TComponent(FLoaded[i]).Loaded;
  6933. finally
  6934. if not Assigned(GlobalLoaded) then
  6935. FLoaded.Free;
  6936. FLoaded := nil;
  6937. end;
  6938. GlobalFixupReferences;
  6939. except
  6940. RemoveFixupReferences(ARoot, '');
  6941. if not Assigned(ARoot) then
  6942. Result.Free;
  6943. raise;
  6944. end;
  6945. {finally
  6946. GlobalNameSpace.EndWrite;
  6947. end;}
  6948. end;
  6949. procedure TReader.ReadComponents(AOwner, AParent: TComponent;
  6950. Proc: TReadComponentsProc);
  6951. var
  6952. Component: TComponent;
  6953. begin
  6954. Root := AOwner;
  6955. Owner := AOwner;
  6956. Parent := AParent;
  6957. BeginReferences;
  6958. try
  6959. while not EndOfList do
  6960. begin
  6961. FDriver.BeginRootComponent;
  6962. Component := ReadComponent(nil);
  6963. if Assigned(Proc) then
  6964. Proc(Component);
  6965. end;
  6966. ReadListEnd;
  6967. FixupReferences;
  6968. finally
  6969. EndReferences;
  6970. end;
  6971. end;
  6972. function TReader.ReadString: String;
  6973. var
  6974. StringType: TValueType;
  6975. begin
  6976. StringType := FDriver.ReadValue;
  6977. if StringType=vaString then
  6978. Result := FDriver.ReadString(StringType)
  6979. else
  6980. raise EReadError.Create(SInvalidPropertyValue);
  6981. end;
  6982. function TReader.ReadWideString: WideString;
  6983. begin
  6984. Result:=ReadString;
  6985. end;
  6986. function TReader.ReadUnicodeString: UnicodeString;
  6987. begin
  6988. Result:=ReadString;
  6989. end;
  6990. function TReader.ReadValue: TValueType;
  6991. begin
  6992. Result := FDriver.ReadValue;
  6993. end;
  6994. procedure TReader.CopyValue(Writer: TWriter);
  6995. (*
  6996. procedure CopyBytes(Count: Integer);
  6997. { var
  6998. Buffer: array[0..1023] of Byte; }
  6999. begin
  7000. {!!!: while Count > 1024 do
  7001. begin
  7002. FDriver.Read(Buffer, 1024);
  7003. Writer.Driver.Write(Buffer, 1024);
  7004. Dec(Count, 1024);
  7005. end;
  7006. if Count > 0 then
  7007. begin
  7008. FDriver.Read(Buffer, Count);
  7009. Writer.Driver.Write(Buffer, Count);
  7010. end;}
  7011. end;
  7012. *)
  7013. {var
  7014. s: String;
  7015. Count: LongInt; }
  7016. begin
  7017. case FDriver.NextValue of
  7018. vaNull:
  7019. Writer.WriteIdent('NULL');
  7020. vaFalse:
  7021. Writer.WriteIdent('FALSE');
  7022. vaTrue:
  7023. Writer.WriteIdent('TRUE');
  7024. vaNil:
  7025. Writer.WriteIdent('NIL');
  7026. {!!!: vaList, vaCollection:
  7027. begin
  7028. Writer.WriteValue(FDriver.ReadValue);
  7029. while not EndOfList do
  7030. CopyValue(Writer);
  7031. ReadListEnd;
  7032. Writer.WriteListEnd;
  7033. end;}
  7034. vaInt8, vaInt16, vaInt32:
  7035. Writer.WriteInteger(ReadInteger);
  7036. {$ifndef FPUNONE}
  7037. vaExtended:
  7038. Writer.WriteFloat(ReadFloat);
  7039. {$endif}
  7040. vaString:
  7041. Writer.WriteString(ReadString);
  7042. vaIdent:
  7043. Writer.WriteIdent(ReadIdent);
  7044. {!!!: vaBinary, vaLString, vaWString:
  7045. begin
  7046. Writer.WriteValue(FDriver.ReadValue);
  7047. FDriver.Read(Count, SizeOf(Count));
  7048. Writer.Driver.Write(Count, SizeOf(Count));
  7049. CopyBytes(Count);
  7050. end;}
  7051. {!!!: vaSet:
  7052. Writer.WriteSet(ReadSet);}
  7053. {!!!: vaCurrency:
  7054. Writer.WriteCurrency(ReadCurrency);}
  7055. vaInt64:
  7056. Writer.WriteInteger(ReadNativeInt);
  7057. end;
  7058. end;
  7059. function TReader.FindComponentClass(const AClassName: String): TComponentClass;
  7060. var
  7061. PersistentClass: TPersistentClass;
  7062. function FindClassInFieldTable(Instance: TComponent): TComponentClass;
  7063. var
  7064. aClass: TClass;
  7065. i: longint;
  7066. ClassTI, MemberClassTI: TTypeInfoClass;
  7067. MemberTI: TTypeInfo;
  7068. begin
  7069. aClass:=Instance.ClassType;
  7070. while aClass<>nil do
  7071. begin
  7072. ClassTI:=typeinfo(aClass);
  7073. for i:=0 to ClassTI.FieldCount-1 do
  7074. begin
  7075. MemberTI:=ClassTI.GetField(i).TypeInfo;
  7076. if MemberTI.Kind=tkClass then
  7077. begin
  7078. MemberClassTI:=TTypeInfoClass(MemberTI);
  7079. if SameText(MemberClassTI.Name,aClassName)
  7080. and (MemberClassTI.ClassType is TComponent) then
  7081. exit(TComponentClass(MemberClassTI.ClassType));
  7082. end;
  7083. end;
  7084. aClass:=aClass.ClassParent;
  7085. end;
  7086. end;
  7087. begin
  7088. Result := nil;
  7089. Result:=FindClassInFieldTable(Root);
  7090. if (Result=nil) and assigned(LookupRoot) and (LookupRoot<>Root) then
  7091. Result:=FindClassInFieldTable(LookupRoot);
  7092. if (Result=nil) then begin
  7093. PersistentClass := GetClass(AClassName);
  7094. if PersistentClass.InheritsFrom(TComponent) then
  7095. Result := TComponentClass(PersistentClass);
  7096. end;
  7097. if (Result=nil) and assigned(OnFindComponentClass) then
  7098. OnFindComponentClass(Self, AClassName, Result);
  7099. if (Result=nil) or (not Result.InheritsFrom(TComponent)) then
  7100. raise EClassNotFound.CreateFmt(SClassNotFound, [AClassName]);
  7101. end;
  7102. { TAbstractObjectReader }
  7103. procedure TAbstractObjectReader.FlushBuffer;
  7104. begin
  7105. // Do nothing
  7106. end;
  7107. {
  7108. This file is part of the Free Component Library (FCL)
  7109. Copyright (c) 1999-2000 by the Free Pascal development team
  7110. See the file COPYING.FPC, included in this distribution,
  7111. for details about the copyright.
  7112. This program is distributed in the hope that it will be useful,
  7113. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7114. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  7115. **********************************************************************}
  7116. {****************************************************************************}
  7117. {* TBinaryObjectWriter *}
  7118. {****************************************************************************}
  7119. procedure TBinaryObjectWriter.WriteWord(w : word);
  7120. begin
  7121. FStream.WriteBufferData(w);
  7122. end;
  7123. procedure TBinaryObjectWriter.WriteDWord(lw : longword);
  7124. begin
  7125. FStream.WriteBufferData(lw);
  7126. end;
  7127. constructor TBinaryObjectWriter.Create(Stream: TStream);
  7128. begin
  7129. inherited Create;
  7130. If (Stream=Nil) then
  7131. Raise EWriteError.Create(SEmptyStreamIllegalWriter);
  7132. FStream := Stream;
  7133. end;
  7134. procedure TBinaryObjectWriter.BeginCollection;
  7135. begin
  7136. WriteValue(vaCollection);
  7137. end;
  7138. procedure TBinaryObjectWriter.WriteSignature;
  7139. begin
  7140. FStream.WriteBufferData(FilerSignatureInt);
  7141. end;
  7142. procedure TBinaryObjectWriter.BeginComponent(Component: TComponent;
  7143. Flags: TFilerFlags; ChildPos: Integer);
  7144. var
  7145. Prefix: Byte;
  7146. begin
  7147. { Only write the flags if they are needed! }
  7148. if Flags <> [] then
  7149. begin
  7150. Prefix:=0;
  7151. if ffInherited in Flags then
  7152. Prefix:=Prefix or $01;
  7153. if ffChildPos in Flags then
  7154. Prefix:=Prefix or $02;
  7155. if ffInline in Flags then
  7156. Prefix:=Prefix or $04;
  7157. Prefix := Prefix or $f0;
  7158. FStream.WriteBufferData(Prefix);
  7159. if ffChildPos in Flags then
  7160. WriteInteger(ChildPos);
  7161. end;
  7162. WriteStr(Component.ClassName);
  7163. WriteStr(Component.Name);
  7164. end;
  7165. procedure TBinaryObjectWriter.BeginList;
  7166. begin
  7167. WriteValue(vaList);
  7168. end;
  7169. procedure TBinaryObjectWriter.EndList;
  7170. begin
  7171. WriteValue(vaNull);
  7172. end;
  7173. procedure TBinaryObjectWriter.BeginProperty(const PropName: String);
  7174. begin
  7175. WriteStr(PropName);
  7176. end;
  7177. procedure TBinaryObjectWriter.EndProperty;
  7178. begin
  7179. end;
  7180. procedure TBinaryObjectWriter.FlushBuffer;
  7181. begin
  7182. // Do nothing;
  7183. end;
  7184. procedure TBinaryObjectWriter.WriteBinary(const Buffer : TBytes; Count: LongInt);
  7185. begin
  7186. WriteValue(vaBinary);
  7187. WriteDWord(longword(Count));
  7188. FStream.Write(Buffer, Count);
  7189. end;
  7190. procedure TBinaryObjectWriter.WriteBoolean(Value: Boolean);
  7191. begin
  7192. if Value then
  7193. WriteValue(vaTrue)
  7194. else
  7195. WriteValue(vaFalse);
  7196. end;
  7197. procedure TBinaryObjectWriter.WriteFloat(const Value: Extended);
  7198. begin
  7199. WriteValue(vaDouble);
  7200. FStream.WriteBufferData(Value);
  7201. end;
  7202. procedure TBinaryObjectWriter.WriteCurrency(const Value: Currency);
  7203. Var
  7204. F : Double;
  7205. begin
  7206. WriteValue(vaCurrency);
  7207. F:=Value;
  7208. FStream.WriteBufferData(F);
  7209. end;
  7210. procedure TBinaryObjectWriter.WriteIdent(const Ident: string);
  7211. begin
  7212. { Check if Ident is a special identifier before trying to just write
  7213. Ident directly }
  7214. if UpperCase(Ident) = 'NIL' then
  7215. WriteValue(vaNil)
  7216. else if UpperCase(Ident) = 'FALSE' then
  7217. WriteValue(vaFalse)
  7218. else if UpperCase(Ident) = 'TRUE' then
  7219. WriteValue(vaTrue)
  7220. else if UpperCase(Ident) = 'NULL' then
  7221. WriteValue(vaNull) else
  7222. begin
  7223. WriteValue(vaIdent);
  7224. WriteStr(Ident);
  7225. end;
  7226. end;
  7227. procedure TBinaryObjectWriter.WriteInteger(Value: NativeInt);
  7228. var
  7229. s: ShortInt;
  7230. i: SmallInt;
  7231. l: Longint;
  7232. begin
  7233. { Use the smallest possible integer type for the given value: }
  7234. if (Value >= -128) and (Value <= 127) then
  7235. begin
  7236. WriteValue(vaInt8);
  7237. s := Value;
  7238. FStream.WriteBufferData(s);
  7239. end else if (Value >= -32768) and (Value <= 32767) then
  7240. begin
  7241. WriteValue(vaInt16);
  7242. i := Value;
  7243. WriteWord(word(i));
  7244. end else if (Value >= -$80000000) and (Value <= $7fffffff) then
  7245. begin
  7246. WriteValue(vaInt32);
  7247. l := Value;
  7248. WriteDWord(longword(l));
  7249. end else
  7250. begin
  7251. WriteValue(vaInt64);
  7252. FStream.WriteBufferData(Value);
  7253. end;
  7254. end;
  7255. procedure TBinaryObjectWriter.WriteNativeInt(Value: NativeInt);
  7256. var
  7257. s: Int8;
  7258. i: Int16;
  7259. l: Int32;
  7260. begin
  7261. { Use the smallest possible integer type for the given value: }
  7262. if (Value <= 127) then
  7263. begin
  7264. WriteValue(vaInt8);
  7265. s := Value;
  7266. FStream.WriteBufferData(s);
  7267. end else if (Value <= 32767) then
  7268. begin
  7269. WriteValue(vaInt16);
  7270. i := Value;
  7271. WriteWord(word(i));
  7272. end else if (Value <= $7fffffff) then
  7273. begin
  7274. WriteValue(vaInt32);
  7275. l := Value;
  7276. WriteDWord(longword(l));
  7277. end else
  7278. begin
  7279. WriteValue(vaQWord);
  7280. FStream.WriteBufferData(Value);
  7281. end;
  7282. end;
  7283. procedure TBinaryObjectWriter.WriteMethodName(const Name: String);
  7284. begin
  7285. if Length(Name) > 0 then
  7286. begin
  7287. WriteValue(vaIdent);
  7288. WriteStr(Name);
  7289. end else
  7290. WriteValue(vaNil);
  7291. end;
  7292. procedure TBinaryObjectWriter.WriteSet(Value: LongInt; SetType: Pointer);
  7293. var
  7294. i: Integer;
  7295. b : Integer;
  7296. begin
  7297. WriteValue(vaSet);
  7298. B:=1;
  7299. for i:=0 to 31 do
  7300. begin
  7301. if (Value and b) <>0 then
  7302. begin
  7303. WriteStr(GetEnumName(PTypeInfo(SetType), i));
  7304. end;
  7305. b:=b shl 1;
  7306. end;
  7307. WriteStr('');
  7308. end;
  7309. procedure TBinaryObjectWriter.WriteString(const Value: String);
  7310. var
  7311. i, len: Integer;
  7312. begin
  7313. len := Length(Value);
  7314. WriteValue(vaString);
  7315. WriteDWord(len);
  7316. For I:=1 to len do
  7317. FStream.WriteBufferData(Value[i]);
  7318. end;
  7319. procedure TBinaryObjectWriter.WriteWideString(const Value: WideString);
  7320. begin
  7321. WriteString(Value);
  7322. end;
  7323. procedure TBinaryObjectWriter.WriteUnicodeString(const Value: UnicodeString);
  7324. begin
  7325. WriteString(Value);
  7326. end;
  7327. procedure TBinaryObjectWriter.WriteVariant(const VarValue: JSValue);
  7328. begin
  7329. if isUndefined(varValue) then
  7330. WriteValue(vaNil)
  7331. else if IsNull(VarValue) then
  7332. WriteValue(vaNull)
  7333. else if IsNumber(VarValue) then
  7334. begin
  7335. if Frac(Double(varValue))=0 then
  7336. WriteInteger(NativeInt(VarValue))
  7337. else
  7338. WriteFloat(Double(varValue))
  7339. end
  7340. else if isBoolean(varValue) then
  7341. WriteBoolean(Boolean(VarValue))
  7342. else if isString(varValue) then
  7343. WriteString(String(VarValue))
  7344. else
  7345. raise EWriteError.Create(SUnsupportedPropertyVariantType);
  7346. end;
  7347. procedure TBinaryObjectWriter.Write(const Buffer : TBytes; Count: LongInt);
  7348. begin
  7349. FStream.Write(Buffer,Count);
  7350. end;
  7351. procedure TBinaryObjectWriter.WriteValue(Value: TValueType);
  7352. var
  7353. b: uint8;
  7354. begin
  7355. b := uint8(Value);
  7356. FStream.WriteBufferData(b);
  7357. end;
  7358. procedure TBinaryObjectWriter.WriteStr(const Value: String);
  7359. var
  7360. len,i: integer;
  7361. b: uint8;
  7362. begin
  7363. len:= Length(Value);
  7364. if len > 255 then
  7365. len := 255;
  7366. b := len;
  7367. FStream.WriteBufferData(b);
  7368. For I:=1 to len do
  7369. FStream.WriteBufferData(Value[i]);
  7370. end;
  7371. {****************************************************************************}
  7372. {* TWriter *}
  7373. {****************************************************************************}
  7374. constructor TWriter.Create(ADriver: TAbstractObjectWriter);
  7375. begin
  7376. inherited Create;
  7377. FDriver := ADriver;
  7378. end;
  7379. constructor TWriter.Create(Stream: TStream);
  7380. begin
  7381. inherited Create;
  7382. If (Stream=Nil) then
  7383. Raise EWriteError.Create(SEmptyStreamIllegalWriter);
  7384. FDriver := CreateDriver(Stream);
  7385. FDestroyDriver := True;
  7386. end;
  7387. destructor TWriter.Destroy;
  7388. begin
  7389. if FDestroyDriver then
  7390. FDriver.Free;
  7391. inherited Destroy;
  7392. end;
  7393. function TWriter.CreateDriver(Stream: TStream): TAbstractObjectWriter;
  7394. begin
  7395. Result := TBinaryObjectWriter.Create(Stream);
  7396. end;
  7397. Type
  7398. TPosComponent = Class(TObject)
  7399. Private
  7400. FPos : Integer;
  7401. FComponent : TComponent;
  7402. Public
  7403. Constructor Create(APos : Integer; AComponent : TComponent);
  7404. end;
  7405. Constructor TPosComponent.Create(APos : Integer; AComponent : TComponent);
  7406. begin
  7407. FPos:=APos;
  7408. FComponent:=AComponent;
  7409. end;
  7410. // Used as argument for calls to TComponent.GetChildren:
  7411. procedure TWriter.AddToAncestorList(Component: TComponent);
  7412. begin
  7413. FAncestors.AddObject(Component.Name,TPosComponent.Create(FAncestors.Count,Component));
  7414. end;
  7415. procedure TWriter.DefineProperty(const Name: String;
  7416. ReadData: TReaderProc; AWriteData: TWriterProc; HasData: Boolean);
  7417. begin
  7418. if HasData and Assigned(AWriteData) then
  7419. begin
  7420. // Write the property name and then the data itself
  7421. Driver.BeginProperty(FPropPath + Name);
  7422. AWriteData(Self);
  7423. Driver.EndProperty;
  7424. end else if assigned(ReadData) then ;
  7425. end;
  7426. procedure TWriter.DefineBinaryProperty(const Name: String;
  7427. ReadData, AWriteData: TStreamProc; HasData: Boolean);
  7428. begin
  7429. if HasData and Assigned(AWriteData) then
  7430. begin
  7431. // Write the property name and then the data itself
  7432. Driver.BeginProperty(FPropPath + Name);
  7433. WriteBinary(AWriteData);
  7434. Driver.EndProperty;
  7435. end else if assigned(ReadData) then ;
  7436. end;
  7437. procedure TWriter.FlushBuffer;
  7438. begin
  7439. Driver.FlushBuffer;
  7440. end;
  7441. procedure TWriter.Write(const Buffer : TBytes; Count: Longint);
  7442. begin
  7443. //This should give an exception if write is not implemented (i.e. TTextObjectWriter)
  7444. //but should work with TBinaryObjectWriter.
  7445. Driver.Write(Buffer, Count);
  7446. end;
  7447. procedure TWriter.SetRoot(ARoot: TComponent);
  7448. begin
  7449. inherited SetRoot(ARoot);
  7450. // Use the new root as lookup root too
  7451. FLookupRoot := ARoot;
  7452. end;
  7453. procedure TWriter.WriteSignature;
  7454. begin
  7455. FDriver.WriteSignature;
  7456. end;
  7457. procedure TWriter.WriteBinary(AWriteData: TStreamProc);
  7458. var
  7459. MemBuffer: TBytesStream;
  7460. begin
  7461. { First write the binary data into a memory stream, then copy this buffered
  7462. stream into the writing destination. This is necessary as we have to know
  7463. the size of the binary data in advance (we're assuming that seeking within
  7464. the writer stream is not possible) }
  7465. MemBuffer := TBytesStream.Create;
  7466. try
  7467. AWriteData(MemBuffer);
  7468. Driver.WriteBinary(MemBuffer.Bytes, MemBuffer.Size);
  7469. finally
  7470. MemBuffer.Free;
  7471. end;
  7472. end;
  7473. procedure TWriter.WriteBoolean(Value: Boolean);
  7474. begin
  7475. Driver.WriteBoolean(Value);
  7476. end;
  7477. procedure TWriter.WriteChar(Value: Char);
  7478. begin
  7479. WriteString(Value);
  7480. end;
  7481. procedure TWriter.WriteWideChar(Value: WideChar);
  7482. begin
  7483. WriteWideString(Value);
  7484. end;
  7485. procedure TWriter.WriteCollection(Value: TCollection);
  7486. var
  7487. i: Integer;
  7488. begin
  7489. Driver.BeginCollection;
  7490. if Assigned(Value) then
  7491. for i := 0 to Value.Count - 1 do
  7492. begin
  7493. { Each collection item needs its own ListBegin/ListEnd tag, or else the
  7494. reader wouldn't be able to know where an item ends and where the next
  7495. one starts }
  7496. WriteListBegin;
  7497. WriteProperties(Value.Items[i]);
  7498. WriteListEnd;
  7499. end;
  7500. WriteListEnd;
  7501. end;
  7502. procedure TWriter.DetermineAncestor(Component : TComponent);
  7503. Var
  7504. I : Integer;
  7505. begin
  7506. // Should be set only when we write an inherited with children.
  7507. if Not Assigned(FAncestors) then
  7508. exit;
  7509. I:=FAncestors.IndexOf(Component.Name);
  7510. If (I=-1) then
  7511. begin
  7512. FAncestor:=Nil;
  7513. FAncestorPos:=-1;
  7514. end
  7515. else
  7516. With TPosComponent(FAncestors.Objects[i]) do
  7517. begin
  7518. FAncestor:=FComponent;
  7519. FAncestorPos:=FPos;
  7520. end;
  7521. end;
  7522. procedure TWriter.DoFindAncestor(Component : TComponent);
  7523. Var
  7524. C : TComponent;
  7525. begin
  7526. if Assigned(FOnFindAncestor) then
  7527. if (Ancestor=Nil) or (Ancestor is TComponent) then
  7528. begin
  7529. C:=TComponent(Ancestor);
  7530. FOnFindAncestor(Self,Component,Component.Name,C,FRootAncestor);
  7531. Ancestor:=C;
  7532. end;
  7533. end;
  7534. procedure TWriter.WriteComponent(Component: TComponent);
  7535. var
  7536. SA : TPersistent;
  7537. SR, SRA : TComponent;
  7538. begin
  7539. SR:=FRoot;
  7540. SA:=FAncestor;
  7541. SRA:=FRootAncestor;
  7542. Try
  7543. Component.FComponentState:=Component.FComponentState+[csWriting];
  7544. Try
  7545. // Possibly set ancestor.
  7546. DetermineAncestor(Component);
  7547. DoFindAncestor(Component); // Mainly for IDE when a parent form had an ancestor renamed...
  7548. // Will call WriteComponentData.
  7549. Component.WriteState(Self);
  7550. FDriver.EndList;
  7551. Finally
  7552. Component.FComponentState:=Component.FComponentState-[csWriting];
  7553. end;
  7554. Finally
  7555. FAncestor:=SA;
  7556. FRoot:=SR;
  7557. FRootAncestor:=SRA;
  7558. end;
  7559. end;
  7560. procedure TWriter.WriteChildren(Component : TComponent);
  7561. Var
  7562. SRoot, SRootA : TComponent;
  7563. SList : TStringList;
  7564. SPos, I , SAncestorPos: Integer;
  7565. O : TObject;
  7566. begin
  7567. // Write children list.
  7568. // While writing children, the ancestor environment must be saved
  7569. // This is recursive...
  7570. SRoot:=FRoot;
  7571. SRootA:=FRootAncestor;
  7572. SList:=FAncestors;
  7573. SPos:=FCurrentPos;
  7574. SAncestorPos:=FAncestorPos;
  7575. try
  7576. FAncestors:=Nil;
  7577. FCurrentPos:=0;
  7578. FAncestorPos:=-1;
  7579. if csInline in Component.ComponentState then
  7580. FRoot:=Component;
  7581. if (FAncestor is TComponent) then
  7582. begin
  7583. FAncestors:=TStringList.Create;
  7584. if csInline in TComponent(FAncestor).ComponentState then
  7585. FRootAncestor := TComponent(FAncestor);
  7586. TComponent(FAncestor).GetChildren(@AddToAncestorList,FRootAncestor);
  7587. FAncestors.Sorted:=True;
  7588. end;
  7589. try
  7590. Component.GetChildren(@WriteComponent, FRoot);
  7591. Finally
  7592. If Assigned(Fancestors) then
  7593. For I:=0 to FAncestors.Count-1 do
  7594. begin
  7595. O:=FAncestors.Objects[i];
  7596. FAncestors.Objects[i]:=Nil;
  7597. O.Free;
  7598. end;
  7599. FreeAndNil(FAncestors);
  7600. end;
  7601. finally
  7602. FAncestors:=Slist;
  7603. FRoot:=SRoot;
  7604. FRootAncestor:=SRootA;
  7605. FCurrentPos:=SPos;
  7606. FAncestorPos:=SAncestorPos;
  7607. end;
  7608. end;
  7609. procedure TWriter.WriteComponentData(Instance: TComponent);
  7610. var
  7611. Flags: TFilerFlags;
  7612. begin
  7613. Flags := [];
  7614. If (Assigned(FAncestor)) and //has ancestor
  7615. (not (csInline in Instance.ComponentState) or // no inline component
  7616. // .. or the inline component is inherited
  7617. (csAncestor in Instance.Componentstate) and (FAncestors <> nil)) then
  7618. Flags:=[ffInherited]
  7619. else If csInline in Instance.ComponentState then
  7620. Flags:=[ffInline];
  7621. If (FAncestors<>Nil) and ((FCurrentPos<>FAncestorPos) or (FAncestor=Nil)) then
  7622. Include(Flags,ffChildPos);
  7623. FDriver.BeginComponent(Instance,Flags,FCurrentPos);
  7624. If (FAncestors<>Nil) then
  7625. Inc(FCurrentPos);
  7626. WriteProperties(Instance);
  7627. WriteListEnd;
  7628. // Needs special handling of ancestor.
  7629. If not IgnoreChildren then
  7630. WriteChildren(Instance);
  7631. end;
  7632. procedure TWriter.WriteDescendent(ARoot: TComponent; AAncestor: TComponent);
  7633. begin
  7634. FRoot := ARoot;
  7635. FAncestor := AAncestor;
  7636. FRootAncestor := AAncestor;
  7637. FLookupRoot := ARoot;
  7638. WriteSignature;
  7639. WriteComponent(ARoot);
  7640. end;
  7641. procedure TWriter.WriteFloat(const Value: Extended);
  7642. begin
  7643. Driver.WriteFloat(Value);
  7644. end;
  7645. procedure TWriter.WriteCurrency(const Value: Currency);
  7646. begin
  7647. Driver.WriteCurrency(Value);
  7648. end;
  7649. procedure TWriter.WriteIdent(const Ident: string);
  7650. begin
  7651. Driver.WriteIdent(Ident);
  7652. end;
  7653. procedure TWriter.WriteInteger(Value: LongInt);
  7654. begin
  7655. Driver.WriteInteger(Value);
  7656. end;
  7657. procedure TWriter.WriteInteger(Value: NativeInt);
  7658. begin
  7659. Driver.WriteInteger(Value);
  7660. end;
  7661. procedure TWriter.WriteSet(Value: LongInt; SetType: Pointer);
  7662. begin
  7663. Driver.WriteSet(Value,SetType);
  7664. end;
  7665. procedure TWriter.WriteVariant(const VarValue: JSValue);
  7666. begin
  7667. Driver.WriteVariant(VarValue);
  7668. end;
  7669. procedure TWriter.WriteListBegin;
  7670. begin
  7671. Driver.BeginList;
  7672. end;
  7673. procedure TWriter.WriteListEnd;
  7674. begin
  7675. Driver.EndList;
  7676. end;
  7677. procedure TWriter.WriteProperties(Instance: TPersistent);
  7678. var
  7679. PropCount,i : integer;
  7680. PropList : TTypeMemberPropertyDynArray;
  7681. begin
  7682. PropList:=GetPropList(Instance);
  7683. PropCount:=Length(PropList);
  7684. if PropCount>0 then
  7685. for i := 0 to PropCount-1 do
  7686. if IsStoredProp(Instance,PropList[i]) then
  7687. WriteProperty(Instance,PropList[i]);
  7688. Instance.DefineProperties(Self);
  7689. end;
  7690. procedure TWriter.WriteProperty(Instance: TPersistent; PropInfo: TTypeMemberProperty);
  7691. var
  7692. HasAncestor: Boolean;
  7693. PropType: TTypeInfo;
  7694. N,Value, DefValue: LongInt;
  7695. Ident: String;
  7696. IntToIdentFn: TIntToIdent;
  7697. {$ifndef FPUNONE}
  7698. FloatValue, DefFloatValue: Extended;
  7699. {$endif}
  7700. MethodValue: TMethod;
  7701. DefMethodValue: TMethod;
  7702. StrValue, DefStrValue: String;
  7703. AncestorObj: TObject;
  7704. C,Component: TComponent;
  7705. ObjValue: TObject;
  7706. SavedAncestor: TPersistent;
  7707. Key, SavedPropPath, Name, lMethodName: String;
  7708. VarValue, DefVarValue : JSValue;
  7709. BoolValue, DefBoolValue: boolean;
  7710. Handled: Boolean;
  7711. O : TJSObject;
  7712. begin
  7713. // do not stream properties without getter
  7714. if PropInfo.Getter='' then
  7715. exit;
  7716. // properties without setter are only allowed, if they are subcomponents
  7717. PropType := PropInfo.TypeInfo;
  7718. if (PropInfo.Setter='') then
  7719. begin
  7720. if PropType.Kind<>tkClass then
  7721. exit;
  7722. ObjValue := TObject(GetObjectProp(Instance, PropInfo));
  7723. if not ObjValue.InheritsFrom(TComponent) or
  7724. not (csSubComponent in TComponent(ObjValue).ComponentStyle) then
  7725. exit;
  7726. end;
  7727. { Check if the ancestor can be used }
  7728. HasAncestor := Assigned(Ancestor) and ((Instance = Root) or
  7729. (Instance.ClassType = Ancestor.ClassType));
  7730. //writeln('TWriter.WriteProperty Name=',PropType^.Name,' Kind=',GetEnumName(TypeInfo(TTypeKind),ord(PropType^.Kind)),' HasAncestor=',HasAncestor);
  7731. case PropType.Kind of
  7732. tkInteger, tkChar, tkEnumeration, tkSet:
  7733. begin
  7734. Value := GetOrdProp(Instance, PropInfo);
  7735. if HasAncestor then
  7736. DefValue := GetOrdProp(Ancestor, PropInfo)
  7737. else
  7738. begin
  7739. if PropType.Kind<>tkSet then
  7740. DefValue := Longint(PropInfo.Default)
  7741. else
  7742. begin
  7743. o:=TJSObject(PropInfo.Default);
  7744. DefValue:=0;
  7745. for Key in o do
  7746. begin
  7747. n:=parseInt(Key,10);
  7748. if n<32 then
  7749. DefValue:=DefValue+(1 shl n);
  7750. end;
  7751. end;
  7752. end;
  7753. // writeln(PPropInfo(PropInfo)^.Name, ', HasAncestor=', ord(HasAncestor), ', Value=', Value, ', Default=', DefValue);
  7754. if (Value <> DefValue) or (DefValue=longint($80000000)) then
  7755. begin
  7756. Driver.BeginProperty(FPropPath + PropInfo.Name);
  7757. case PropType.Kind of
  7758. tkInteger:
  7759. begin
  7760. // Check if this integer has a string identifier
  7761. IntToIdentFn := FindIntToIdent(PropInfo.TypeInfo);
  7762. if Assigned(IntToIdentFn) and IntToIdentFn(Value, Ident) then
  7763. // Integer can be written a human-readable identifier
  7764. WriteIdent(Ident)
  7765. else
  7766. // Integer has to be written just as number
  7767. WriteInteger(Value);
  7768. end;
  7769. tkChar:
  7770. WriteChar(Chr(Value));
  7771. tkSet:
  7772. begin
  7773. Driver.WriteSet(Value, TTypeInfoSet(PropType).CompType);
  7774. end;
  7775. tkEnumeration:
  7776. WriteIdent(GetEnumName(TTypeInfoEnum(PropType), Value));
  7777. end;
  7778. Driver.EndProperty;
  7779. end;
  7780. end;
  7781. {$ifndef FPUNONE}
  7782. tkFloat:
  7783. begin
  7784. FloatValue := GetFloatProp(Instance, PropInfo);
  7785. if HasAncestor then
  7786. DefFloatValue := GetFloatProp(Ancestor, PropInfo)
  7787. else
  7788. begin
  7789. // This is really ugly..
  7790. DefFloatValue:=Double(PropInfo.Default);
  7791. end;
  7792. if (FloatValue<>DefFloatValue) or (not HasAncestor and (int(DefFloatValue)=longint($80000000))) then
  7793. begin
  7794. Driver.BeginProperty(FPropPath + PropInfo.Name);
  7795. WriteFloat(FloatValue);
  7796. Driver.EndProperty;
  7797. end;
  7798. end;
  7799. {$endif}
  7800. tkMethod:
  7801. begin
  7802. MethodValue := GetMethodProp(Instance, PropInfo);
  7803. if HasAncestor then
  7804. DefMethodValue := GetMethodProp(Ancestor, PropInfo)
  7805. else begin
  7806. DefMethodValue.Data := nil;
  7807. DefMethodValue.Code := nil;
  7808. end;
  7809. Handled:=false;
  7810. if Assigned(OnWriteMethodProperty) then
  7811. OnWriteMethodProperty(Self,Instance,PropInfo,MethodValue,
  7812. DefMethodValue,Handled);
  7813. if isString(MethodValue.Code) then
  7814. lMethodName:=String(MethodValue.Code)
  7815. else
  7816. lMethodName:=FLookupRoot.MethodName(MethodValue.Code);
  7817. //Writeln('Writeln A: ',lMethodName);
  7818. if (not Handled) and
  7819. (MethodValue.Code <> DefMethodValue.Code) and
  7820. ((not Assigned(MethodValue.Code)) or
  7821. ((Length(lMethodName) > 0))) then
  7822. begin
  7823. //Writeln('Writeln B',FPropPath + PropInfo.Name);
  7824. Driver.BeginProperty(FPropPath + PropInfo.Name);
  7825. if Assigned(MethodValue.Code) then
  7826. Driver.WriteMethodName(lMethodName)
  7827. else
  7828. Driver.WriteMethodName('');
  7829. Driver.EndProperty;
  7830. end;
  7831. end;
  7832. tkString: // tkSString, tkLString, tkAString are not supported
  7833. begin
  7834. StrValue := GetStrProp(Instance, PropInfo);
  7835. if HasAncestor then
  7836. DefStrValue := GetStrProp(Ancestor, PropInfo)
  7837. else
  7838. begin
  7839. DefValue :=Longint(PropInfo.Default);
  7840. SetLength(DefStrValue, 0);
  7841. end;
  7842. if (StrValue<>DefStrValue) or (not HasAncestor and (DefValue=longint($80000000))) then
  7843. begin
  7844. Driver.BeginProperty(FPropPath + PropInfo.Name);
  7845. if Assigned(FOnWriteStringProperty) then
  7846. FOnWriteStringProperty(Self,Instance,PropInfo,StrValue);
  7847. WriteString(StrValue);
  7848. Driver.EndProperty;
  7849. end;
  7850. end;
  7851. tkJSValue:
  7852. begin
  7853. { Ensure that a Variant manager is installed }
  7854. VarValue := GetJSValueProp(Instance, PropInfo);
  7855. if HasAncestor then
  7856. DefVarValue := GetJSValueProp(Ancestor, PropInfo)
  7857. else
  7858. DefVarValue:=null;
  7859. if (VarValue<>DefVarValue) then
  7860. begin
  7861. Driver.BeginProperty(FPropPath + PropInfo.Name);
  7862. { can't use variant() typecast, pulls in variants unit }
  7863. WriteVariant(VarValue);
  7864. Driver.EndProperty;
  7865. end;
  7866. end;
  7867. tkClass:
  7868. begin
  7869. ObjValue := TObject(GetObjectProp(Instance, PropInfo));
  7870. if HasAncestor then
  7871. begin
  7872. AncestorObj := TObject(GetObjectProp(Ancestor, PropInfo));
  7873. if (AncestorObj is TComponent) and
  7874. (ObjValue is TComponent) then
  7875. begin
  7876. //writeln('TWriter.WriteProperty AncestorObj=',TComponent(AncestorObj).Name,' OwnerFit=',TComponent(AncestorObj).Owner = FRootAncestor,' ',TComponent(ObjValue).Name,' OwnerFit=',TComponent(ObjValue).Owner = Root);
  7877. if (AncestorObj<> ObjValue) and
  7878. (TComponent(AncestorObj).Owner = FRootAncestor) and
  7879. (TComponent(ObjValue).Owner = Root) and
  7880. (UpperCase(TComponent(AncestorObj).Name) = UpperCase(TComponent(ObjValue).Name)) then
  7881. begin
  7882. // different components, but with the same name
  7883. // treat it like an override
  7884. AncestorObj := ObjValue;
  7885. end;
  7886. end;
  7887. end else
  7888. AncestorObj := nil;
  7889. if not Assigned(ObjValue) then
  7890. begin
  7891. if ObjValue <> AncestorObj then
  7892. begin
  7893. Driver.BeginProperty(FPropPath + PropInfo.Name);
  7894. Driver.WriteIdent('NIL');
  7895. Driver.EndProperty;
  7896. end
  7897. end
  7898. else if ObjValue.InheritsFrom(TPersistent) then
  7899. begin
  7900. { Subcomponents are streamed the same way as persistents }
  7901. if ObjValue.InheritsFrom(TComponent)
  7902. and ((not (csSubComponent in TComponent(ObjValue).ComponentStyle))
  7903. or ((TComponent(ObjValue).Owner<>Instance) and (TComponent(ObjValue).Owner<>Nil))) then
  7904. begin
  7905. Component := TComponent(ObjValue);
  7906. if (ObjValue <> AncestorObj)
  7907. and not (csTransient in Component.ComponentStyle) then
  7908. begin
  7909. Name:= '';
  7910. C:= Component;
  7911. While (C<>Nil) and (C.Name<>'') do
  7912. begin
  7913. If (Name<>'') Then
  7914. Name:='.'+Name;
  7915. if C.Owner = LookupRoot then
  7916. begin
  7917. Name := C.Name+Name;
  7918. break;
  7919. end
  7920. else if C = LookupRoot then
  7921. begin
  7922. Name := 'Owner' + Name;
  7923. break;
  7924. end;
  7925. Name:=C.Name + Name;
  7926. C:= C.Owner;
  7927. end;
  7928. if (C=nil) and (Component.Owner=nil) then
  7929. if (Name<>'') then //foreign root
  7930. Name:=Name+'.Owner';
  7931. if Length(Name) > 0 then
  7932. begin
  7933. Driver.BeginProperty(FPropPath + PropInfo.Name);
  7934. WriteIdent(Name);
  7935. Driver.EndProperty;
  7936. end; // length Name>0
  7937. end; //(ObjValue <> AncestorObj)
  7938. end // ObjValue.InheritsFrom(TComponent)
  7939. else
  7940. begin
  7941. SavedAncestor := Ancestor;
  7942. SavedPropPath := FPropPath;
  7943. try
  7944. FPropPath := FPropPath + PropInfo.Name + '.';
  7945. if HasAncestor then
  7946. Ancestor := TPersistent(GetObjectProp(Ancestor, PropInfo));
  7947. WriteProperties(TPersistent(ObjValue));
  7948. finally
  7949. Ancestor := SavedAncestor;
  7950. FPropPath := SavedPropPath;
  7951. end;
  7952. if ObjValue.InheritsFrom(TCollection) then
  7953. begin
  7954. if (not HasAncestor) or (not CollectionsEqual(TCollection(ObjValue),
  7955. TCollection(GetObjectProp(Ancestor, PropInfo)),root,rootancestor)) then
  7956. begin
  7957. Driver.BeginProperty(FPropPath + PropInfo.Name);
  7958. SavedPropPath := FPropPath;
  7959. try
  7960. SetLength(FPropPath, 0);
  7961. WriteCollection(TCollection(ObjValue));
  7962. finally
  7963. FPropPath := SavedPropPath;
  7964. Driver.EndProperty;
  7965. end;
  7966. end;
  7967. end // Tcollection
  7968. end;
  7969. end; // Inheritsfrom(TPersistent)
  7970. end;
  7971. { tkInt64, tkQWord:
  7972. begin
  7973. Int64Value := GetInt64Prop(Instance, PropInfo);
  7974. if HasAncestor then
  7975. DefInt64Value := GetInt64Prop(Ancestor, PropInfo)
  7976. else
  7977. DefInt64Value := 0;
  7978. if Int64Value <> DefInt64Value then
  7979. begin
  7980. Driver.BeginProperty(FPropPath + PPropInfo(PropInfo)^.Name);
  7981. WriteInteger(Int64Value);
  7982. Driver.EndProperty;
  7983. end;
  7984. end;}
  7985. tkBool:
  7986. begin
  7987. BoolValue := GetOrdProp(Instance, PropInfo)<>0;
  7988. if HasAncestor then
  7989. DefBoolValue := GetOrdProp(Ancestor, PropInfo)<>0
  7990. else
  7991. begin
  7992. DefBoolValue := PropInfo.Default<>0;
  7993. DefValue:=Longint(PropInfo.Default);
  7994. end;
  7995. // writeln(PropInfo.Name, ', HasAncestor=', ord(HasAncestor), ', Value=', Value, ', Default=', DefBoolValue);
  7996. if (BoolValue<>DefBoolValue) or (DefValue=longint($80000000)) then
  7997. begin
  7998. Driver.BeginProperty(FPropPath + PropInfo.Name);
  7999. WriteBoolean(BoolValue);
  8000. Driver.EndProperty;
  8001. end;
  8002. end;
  8003. tkInterface:
  8004. begin
  8005. { IntfValue := GetInterfaceProp(Instance, PropInfo);
  8006. if Assigned(IntfValue) and Supports(IntfValue, IInterfaceComponentReference, CompRef) then
  8007. begin
  8008. Component := CompRef.GetComponent;
  8009. if HasAncestor then
  8010. begin
  8011. AncestorObj := TObject(GetObjectProp(Ancestor, PropInfo));
  8012. if (AncestorObj is TComponent) then
  8013. begin
  8014. //writeln('TWriter.WriteProperty AncestorObj=',TComponent(AncestorObj).Name,' OwnerFit=',TComponent(AncestorObj).Owner = FRootAncestor,' ',TComponent(ObjValue).Name,' OwnerFit=',TComponent(ObjValue).Owner = Root);
  8015. if (AncestorObj<> Component) and
  8016. (TComponent(AncestorObj).Owner = FRootAncestor) and
  8017. (Component.Owner = Root) and
  8018. (UpperCase(TComponent(AncestorObj).Name) = UpperCase(Component.Name)) then
  8019. begin
  8020. // different components, but with the same name
  8021. // treat it like an override
  8022. AncestorObj := Component;
  8023. end;
  8024. end;
  8025. end else
  8026. AncestorObj := nil;
  8027. if not Assigned(Component) then
  8028. begin
  8029. if Component <> AncestorObj then
  8030. begin
  8031. Driver.BeginProperty(FPropPath + PropInfo.Name);
  8032. Driver.WriteIdent('NIL');
  8033. Driver.EndProperty;
  8034. end
  8035. end
  8036. else if ((not (csSubComponent in Component.ComponentStyle))
  8037. or ((Component.Owner<>Instance) and (Component.Owner<>Nil))) then
  8038. begin
  8039. if (Component <> AncestorObj)
  8040. and not (csTransient in Component.ComponentStyle) then
  8041. begin
  8042. Name:= '';
  8043. C:= Component;
  8044. While (C<>Nil) and (C.Name<>'') do
  8045. begin
  8046. If (Name<>'') Then
  8047. Name:='.'+Name;
  8048. if C.Owner = LookupRoot then
  8049. begin
  8050. Name := C.Name+Name;
  8051. break;
  8052. end
  8053. else if C = LookupRoot then
  8054. begin
  8055. Name := 'Owner' + Name;
  8056. break;
  8057. end;
  8058. Name:=C.Name + Name;
  8059. C:= C.Owner;
  8060. end;
  8061. if (C=nil) and (Component.Owner=nil) then
  8062. if (Name<>'') then //foreign root
  8063. Name:=Name+'.Owner';
  8064. if Length(Name) > 0 then
  8065. begin
  8066. Driver.BeginProperty(FPropPath + PropInfo.Name);
  8067. WriteIdent(Name);
  8068. Driver.EndProperty;
  8069. end; // length Name>0
  8070. end; //(Component <> AncestorObj)
  8071. end;
  8072. end; //Assigned(IntfValue) and Supports(IntfValue,..
  8073. //else write NIL ?
  8074. } end;
  8075. end;
  8076. end;
  8077. procedure TWriter.WriteRootComponent(ARoot: TComponent);
  8078. begin
  8079. WriteDescendent(ARoot, nil);
  8080. end;
  8081. procedure TWriter.WriteString(const Value: String);
  8082. begin
  8083. Driver.WriteString(Value);
  8084. end;
  8085. procedure TWriter.WriteWideString(const Value: WideString);
  8086. begin
  8087. Driver.WriteWideString(Value);
  8088. end;
  8089. procedure TWriter.WriteUnicodeString(const Value: UnicodeString);
  8090. begin
  8091. Driver.WriteUnicodeString(Value);
  8092. end;
  8093. { TAbstractObjectWriter }
  8094. { ---------------------------------------------------------------------
  8095. Global routines
  8096. ---------------------------------------------------------------------}
  8097. var
  8098. ClassList : TJSObject;
  8099. InitHandlerList : TList;
  8100. FindGlobalComponentList : TFPList;
  8101. Procedure RegisterClass(AClass : TPersistentClass);
  8102. begin
  8103. ClassList[AClass.ClassName]:=AClass;
  8104. end;
  8105. Procedure RegisterClasses(AClasses : specialize TArray<TPersistentClass>);
  8106. var
  8107. AClass : TPersistentClass;
  8108. begin
  8109. for AClass in AClasses do
  8110. RegisterClass(AClass);
  8111. end;
  8112. Function GetClass(AClassName : string) : TPersistentClass;
  8113. begin
  8114. Result:=nil;
  8115. if AClassName='' then exit;
  8116. if not ClassList.hasOwnProperty(AClassName) then exit;
  8117. Result:=TPersistentClass(ClassList[AClassName]);
  8118. end;
  8119. procedure RegisterFindGlobalComponentProc(AFindGlobalComponent: TFindGlobalComponent);
  8120. begin
  8121. if not(assigned(FindGlobalComponentList)) then
  8122. FindGlobalComponentList:=TFPList.Create;
  8123. if FindGlobalComponentList.IndexOf(CodePointer(AFindGlobalComponent))<0 then
  8124. FindGlobalComponentList.Add(CodePointer(AFindGlobalComponent));
  8125. end;
  8126. procedure UnregisterFindGlobalComponentProc(AFindGlobalComponent: TFindGlobalComponent);
  8127. begin
  8128. if assigned(FindGlobalComponentList) then
  8129. FindGlobalComponentList.Remove(CodePointer(AFindGlobalComponent));
  8130. end;
  8131. function FindGlobalComponent(const Name: string): TComponent;
  8132. var
  8133. i : sizeint;
  8134. begin
  8135. Result:=nil;
  8136. if assigned(FindGlobalComponentList) then
  8137. begin
  8138. for i:=FindGlobalComponentList.Count-1 downto 0 do
  8139. begin
  8140. FindGlobalComponent:=TFindGlobalComponent(FindGlobalComponentList[i])(name);
  8141. if assigned(Result) then
  8142. break;
  8143. end;
  8144. end;
  8145. end;
  8146. Function FindNestedComponent(Root : TComponent; APath : String; CStyle : Boolean = True) : TComponent;
  8147. Function GetNextName : String; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  8148. Var
  8149. P : Integer;
  8150. CM : Boolean;
  8151. begin
  8152. P:=Pos('.',APath);
  8153. CM:=False;
  8154. If (P=0) then
  8155. begin
  8156. If CStyle then
  8157. begin
  8158. P:=Pos('->',APath);
  8159. CM:=P<>0;
  8160. end;
  8161. If (P=0) Then
  8162. P:=Length(APath)+1;
  8163. end;
  8164. Result:=Copy(APath,1,P-1);
  8165. Delete(APath,1,P+Ord(CM));
  8166. end;
  8167. Var
  8168. C : TComponent;
  8169. S : String;
  8170. begin
  8171. If (APath='') then
  8172. Result:=Nil
  8173. else
  8174. begin
  8175. Result:=Root;
  8176. While (APath<>'') And (Result<>Nil) do
  8177. begin
  8178. C:=Result;
  8179. S:=Uppercase(GetNextName);
  8180. Result:=C.FindComponent(S);
  8181. If (Result=Nil) And (S='OWNER') then
  8182. Result:=C;
  8183. end;
  8184. end;
  8185. end;
  8186. Type
  8187. TInitHandler = Class(TObject)
  8188. AHandler : TInitComponentHandler;
  8189. AClass : TComponentClass;
  8190. end;
  8191. procedure RegisterInitComponentHandler(ComponentClass: TComponentClass; Handler: TInitComponentHandler);
  8192. Var
  8193. I : Integer;
  8194. H: TInitHandler;
  8195. begin
  8196. If (InitHandlerList=Nil) then
  8197. InitHandlerList:=TList.Create;
  8198. H:=TInitHandler.Create;
  8199. H.Aclass:=ComponentClass;
  8200. H.AHandler:=Handler;
  8201. try
  8202. With InitHandlerList do
  8203. begin
  8204. I:=0;
  8205. While (I<Count) and not H.AClass.InheritsFrom(TInitHandler(Items[I]).AClass) do
  8206. Inc(I);
  8207. { override? }
  8208. if (I<Count) and (TInitHandler(Items[I]).AClass=H.AClass) then
  8209. begin
  8210. TInitHandler(Items[I]).AHandler:=Handler;
  8211. H.Free;
  8212. end
  8213. else
  8214. InitHandlerList.Insert(I,H);
  8215. end;
  8216. except
  8217. H.Free;
  8218. raise;
  8219. end;
  8220. end;
  8221. procedure TObjectStreamConverter.OutStr(s: String);
  8222. Var
  8223. I : integer;
  8224. begin
  8225. For I:=1 to Length(S) do
  8226. Output.WriteBufferData(s[i]);
  8227. end;
  8228. procedure TObjectStreamConverter.OutLn(s: String);
  8229. begin
  8230. OutStr(s + LineEnding);
  8231. end;
  8232. procedure TObjectStreamConverter.Outchars(S: String);
  8233. var
  8234. res, NewStr: String;
  8235. i,len,w: Cardinal;
  8236. InString, NewInString: Boolean;
  8237. SObj : TJSString absolute s;
  8238. begin
  8239. if S = '' then
  8240. res:= ''''''
  8241. else
  8242. begin
  8243. res := '';
  8244. InString := False;
  8245. len:= Length(S);
  8246. i:=0;
  8247. while i < Len do
  8248. begin
  8249. NewInString := InString;
  8250. w := SObj.charCodeAt(i);
  8251. if w = ord('''') then
  8252. begin //quote char
  8253. if not InString then
  8254. NewInString := True;
  8255. NewStr := '''''';
  8256. end
  8257. else if (w >= 32) and (w < 127) then
  8258. begin //printable ascii or bytes
  8259. if not InString then
  8260. NewInString := True;
  8261. NewStr := TJSString.FromCharCode(w);
  8262. end
  8263. else
  8264. begin //ascii control chars, non ascii
  8265. if InString then
  8266. NewInString := False;
  8267. NewStr := '#' + IntToStr(w);
  8268. end;
  8269. if NewInString <> InString then
  8270. begin
  8271. NewStr := '''' + NewStr;
  8272. InString := NewInString;
  8273. end;
  8274. res := res + NewStr;
  8275. Inc(i);
  8276. end;
  8277. if InString then
  8278. res := res + '''';
  8279. end;
  8280. OutStr(res);
  8281. end;
  8282. procedure TObjectStreamConverter.OutString(s: String);
  8283. begin
  8284. OutChars(S);
  8285. end;
  8286. (*
  8287. procedure TObjectStreamConverter.OutUtf8Str(s: String);
  8288. begin
  8289. if Encoding=oteLFM then
  8290. OutChars(Pointer(S),PChar(S)+Length(S),@CharToOrd)
  8291. else
  8292. OutChars(Pointer(S),PChar(S)+Length(S),@Utf8ToOrd);
  8293. end;
  8294. *)
  8295. function TObjectStreamConverter.ReadWord : word; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  8296. begin
  8297. Input.ReadBufferData(Result);
  8298. end;
  8299. function TObjectStreamConverter.ReadDWord : longword; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  8300. begin
  8301. Input.ReadBufferData(Result);
  8302. end;
  8303. function TObjectStreamConverter.ReadNativeInt : NativeInt; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  8304. begin
  8305. Input.ReadBufferData(Result);
  8306. end;
  8307. function TObjectStreamConverter.ReadInt(ValueType: TValueType): NativeInt;
  8308. begin
  8309. case ValueType of
  8310. vaInt8: Result := ShortInt(Input.ReadByte);
  8311. vaInt16: Result := SmallInt(ReadWord);
  8312. vaInt32: Result := LongInt(ReadDWord);
  8313. vaNativeInt: Result := ReadNativeInt;
  8314. end;
  8315. end;
  8316. function TObjectStreamConverter.ReadInt: NativeInt;
  8317. begin
  8318. Result := ReadInt(TValueType(Input.ReadByte));
  8319. end;
  8320. function TObjectStreamConverter.ReadDouble : Double;
  8321. begin
  8322. Input.ReadBufferData(Result);
  8323. end;
  8324. function TObjectStreamConverter.ReadStr: String;
  8325. var
  8326. l,i: Byte;
  8327. c : Char;
  8328. begin
  8329. Input.ReadBufferData(L);
  8330. SetLength(Result,L);
  8331. For I:=1 to L do
  8332. begin
  8333. Input.ReadBufferData(C);
  8334. Result[i]:=C;
  8335. end;
  8336. end;
  8337. function TObjectStreamConverter.ReadString(StringType: TValueType): String;
  8338. var
  8339. i: Integer;
  8340. C : Char;
  8341. begin
  8342. Result:='';
  8343. if StringType<>vaString then
  8344. Raise EFilerError.Create('Invalid string type passed to ReadString');
  8345. i:=ReadDWord;
  8346. SetLength(Result, i);
  8347. for I:=1 to Length(Result) do
  8348. begin
  8349. Input.ReadbufferData(C);
  8350. Result[i]:=C;
  8351. end;
  8352. end;
  8353. procedure TObjectStreamConverter.ProcessBinary;
  8354. var
  8355. ToDo, DoNow, i: LongInt;
  8356. lbuf: TBytes;
  8357. s: String;
  8358. begin
  8359. ToDo := ReadDWord;
  8360. SetLength(lBuf,32);
  8361. OutLn('{');
  8362. while ToDo > 0 do
  8363. begin
  8364. DoNow := ToDo;
  8365. if DoNow > 32 then
  8366. DoNow := 32;
  8367. Dec(ToDo, DoNow);
  8368. s := Indent + ' ';
  8369. Input.ReadBuffer(lbuf, DoNow);
  8370. for i := 0 to DoNow - 1 do
  8371. s := s + IntToHex(lbuf[i], 2);
  8372. OutLn(s);
  8373. end;
  8374. OutLn(indent + '}');
  8375. end;
  8376. procedure TObjectStreamConverter.ProcessValue(ValueType: TValueType; Indent: String);
  8377. var
  8378. s: String;
  8379. { len: LongInt; }
  8380. IsFirst: Boolean;
  8381. {$ifndef FPUNONE}
  8382. ext: Extended;
  8383. {$endif}
  8384. begin
  8385. case ValueType of
  8386. vaList: begin
  8387. OutStr('(');
  8388. IsFirst := True;
  8389. while True do begin
  8390. ValueType := TValueType(Input.ReadByte);
  8391. if ValueType = vaNull then break;
  8392. if IsFirst then begin
  8393. OutLn('');
  8394. IsFirst := False;
  8395. end;
  8396. OutStr(Indent + ' ');
  8397. ProcessValue(ValueType, Indent + ' ');
  8398. end;
  8399. OutLn(Indent + ')');
  8400. end;
  8401. vaInt8: OutLn(IntToStr(ShortInt(Input.ReadByte)));
  8402. vaInt16: OutLn( IntToStr(SmallInt(ReadWord)));
  8403. vaInt32: OutLn(IntToStr(LongInt(ReadDWord)));
  8404. vaNativeInt: OutLn(IntToStr(ReadNativeInt));
  8405. vaDouble: begin
  8406. ext:=ReadDouble;
  8407. Str(ext,S);// Do not use localized strings.
  8408. OutLn(S);
  8409. end;
  8410. vaString: begin
  8411. if PlainStrings then
  8412. OutStr( ''''+StringReplace(ReadString(vaString),'''','''''',[rfReplaceAll])+'''')
  8413. else
  8414. OutString(ReadString(vaString) {''''+StringReplace(ReadString(vaString),'''','''''',[rfReplaceAll])+''''});
  8415. OutLn('');
  8416. end;
  8417. vaIdent: OutLn(ReadStr);
  8418. vaFalse: OutLn('False');
  8419. vaTrue: OutLn('True');
  8420. vaBinary: ProcessBinary;
  8421. vaSet: begin
  8422. OutStr('[');
  8423. IsFirst := True;
  8424. while True do begin
  8425. s := ReadStr;
  8426. if Length(s) = 0 then break;
  8427. if not IsFirst then OutStr(', ');
  8428. IsFirst := False;
  8429. OutStr(s);
  8430. end;
  8431. OutLn(']');
  8432. end;
  8433. vaNil:
  8434. OutLn('nil');
  8435. vaCollection: begin
  8436. OutStr('<');
  8437. while Input.ReadByte <> 0 do begin
  8438. OutLn(Indent);
  8439. Input.Seek(-1, soCurrent);
  8440. OutStr(indent + ' item');
  8441. ValueType := TValueType(Input.ReadByte);
  8442. if ValueType <> vaList then
  8443. OutStr('[' + IntToStr(ReadInt(ValueType)) + ']');
  8444. OutLn('');
  8445. ReadPropList(indent + ' ');
  8446. OutStr(indent + ' end');
  8447. end;
  8448. OutLn('>');
  8449. end;
  8450. {vaSingle: begin OutLn('!!Single!!'); exit end;
  8451. vaCurrency: begin OutLn('!!Currency!!'); exit end;
  8452. vaDate: begin OutLn('!!Date!!'); exit end;}
  8453. else
  8454. Raise EReadError.CreateFmt(SErrInvalidPropertyType,[Ord(ValueType)]);
  8455. end;
  8456. end;
  8457. procedure TObjectStreamConverter.ReadPropList(indent: String);
  8458. begin
  8459. while Input.ReadByte <> 0 do begin
  8460. Input.Seek(-1, soCurrent);
  8461. OutStr(indent + ReadStr + ' = ');
  8462. ProcessValue(TValueType(Input.ReadByte), Indent);
  8463. end;
  8464. end;
  8465. procedure TObjectStreamConverter.ReadObject(indent: String);
  8466. var
  8467. b: Byte;
  8468. ObjClassName, ObjName: String;
  8469. ChildPos: LongInt;
  8470. begin
  8471. // Check for FilerFlags
  8472. b := Input.ReadByte;
  8473. if (b and $f0) = $f0 then begin
  8474. if (b and 2) <> 0 then ChildPos := ReadInt;
  8475. end else begin
  8476. b := 0;
  8477. Input.Seek(-1, soCurrent);
  8478. end;
  8479. ObjClassName := ReadStr;
  8480. ObjName := ReadStr;
  8481. OutStr(Indent);
  8482. if (b and 1) <> 0 then OutStr('inherited')
  8483. else
  8484. if (b and 4) <> 0 then OutStr('inline')
  8485. else OutStr('object');
  8486. OutStr(' ');
  8487. if ObjName <> '' then
  8488. OutStr(ObjName + ': ');
  8489. OutStr(ObjClassName);
  8490. if (b and 2) <> 0 then OutStr('[' + IntToStr(ChildPos) + ']');
  8491. OutLn('');
  8492. ReadPropList(indent + ' ');
  8493. while Input.ReadByte <> 0 do begin
  8494. Input.Seek(-1, soCurrent);
  8495. ReadObject(indent + ' ');
  8496. end;
  8497. OutLn(indent + 'end');
  8498. end;
  8499. procedure TObjectStreamConverter.ObjectBinaryToText(aInput, aOutput: TStream; aEncoding: TObjectTextEncoding);
  8500. begin
  8501. FInput:=aInput;
  8502. FOutput:=aOutput;
  8503. FEncoding:=aEncoding;
  8504. Execute;
  8505. end;
  8506. procedure TObjectStreamConverter.Execute;
  8507. var
  8508. Signature: LongInt;
  8509. begin
  8510. if FIndent = '' then FInDent:=' ';
  8511. If Not Assigned(Input) then
  8512. raise EReadError.Create('Missing input stream');
  8513. If Not Assigned(Output) then
  8514. raise EReadError.Create('Missing output stream');
  8515. FInput.ReadBufferData(Signature);
  8516. if Signature <> FilerSignatureInt then
  8517. raise EReadError.Create(SInvalidImage);
  8518. ReadObject('');
  8519. end;
  8520. procedure TObjectStreamConverter.ObjectBinaryToText(aInput, aOutput: TStream);
  8521. begin
  8522. ObjectBinaryToText(aInput,aOutput,oteDFM);
  8523. end;
  8524. {
  8525. This file is part of the Free Component Library (FCL)
  8526. Copyright (c) 1999-2007 by the Free Pascal development team
  8527. See the file COPYING.FPC, included in this distribution,
  8528. for details about the copyright.
  8529. This program is distributed in the hope that it will be useful,
  8530. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8531. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8532. **********************************************************************}
  8533. {****************************************************************************}
  8534. {* TParser *}
  8535. {****************************************************************************}
  8536. const
  8537. {$ifdef CPU16}
  8538. { Avoid too big local stack use for
  8539. MSDOS tiny memory model that uses less than 4096
  8540. bytes for total stack by default. }
  8541. ParseBufSize = 512;
  8542. {$else not CPU16}
  8543. ParseBufSize = 4096;
  8544. {$endif not CPU16}
  8545. TokNames : array[TParserToken] of string = (
  8546. '?',
  8547. 'EOF',
  8548. 'Symbol',
  8549. 'String',
  8550. 'Integer',
  8551. 'Float',
  8552. '-',
  8553. '[',
  8554. '(',
  8555. '<',
  8556. '{',
  8557. ']',
  8558. ')',
  8559. '>',
  8560. '}',
  8561. ',',
  8562. '.',
  8563. '=',
  8564. ':',
  8565. '+'
  8566. );
  8567. function TParser.GetTokenName(aTok: TParserToken): string;
  8568. begin
  8569. Result:=TokNames[aTok]
  8570. end;
  8571. procedure TParser.LoadBuffer;
  8572. var
  8573. CharsRead,i: integer;
  8574. begin
  8575. CharsRead:=0;
  8576. for I:=0 to ParseBufSize-1 do
  8577. begin
  8578. if FStream.ReadData(FBuf[i])<>2 then
  8579. Break;
  8580. Inc(CharsRead);
  8581. end;
  8582. Inc(FDeltaPos, CharsRead);
  8583. FPos := 0;
  8584. FBufLen := CharsRead;
  8585. FEofReached:=CharsRead = 0;
  8586. end;
  8587. procedure TParser.CheckLoadBuffer; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  8588. begin
  8589. if fPos>=FBufLen then
  8590. LoadBuffer;
  8591. end;
  8592. procedure TParser.ProcessChar; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  8593. begin
  8594. fLastTokenStr:=fLastTokenStr+fBuf[fPos];
  8595. GotoToNextChar;
  8596. end;
  8597. function TParser.IsNumber: boolean; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  8598. begin
  8599. Result:=fBuf[fPos] in ['0'..'9'];
  8600. end;
  8601. function TParser.IsHexNum: boolean; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  8602. begin
  8603. Result:=fBuf[fPos] in ['0'..'9','A'..'F','a'..'f'];
  8604. end;
  8605. function TParser.IsAlpha: boolean; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  8606. begin
  8607. Result:=fBuf[fPos] in ['_','A'..'Z','a'..'z'];
  8608. end;
  8609. function TParser.IsAlphaNum: boolean; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  8610. begin
  8611. Result:=IsAlpha or IsNumber;
  8612. end;
  8613. function TParser.GetHexValue(c: char): byte; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  8614. begin
  8615. case c of
  8616. '0'..'9' : Result:=ord(c)-$30;
  8617. 'A'..'F' : Result:=ord(c)-$37; //-$41+$0A
  8618. 'a'..'f' : Result:=ord(c)-$57; //-$61+$0A
  8619. end;
  8620. end;
  8621. function TParser.GetAlphaNum: string;
  8622. begin
  8623. if not IsAlpha then
  8624. ErrorFmt(SParserExpected,[GetTokenName(toSymbol)]);
  8625. Result:='';
  8626. while IsAlphaNum do
  8627. begin
  8628. Result:=Result+fBuf[fPos];
  8629. GotoToNextChar;
  8630. end;
  8631. end;
  8632. procedure TParser.HandleNewLine;
  8633. begin
  8634. if fBuf[fPos]=#13 then //CR
  8635. GotoToNextChar;
  8636. if fBuf[fPos]=#10 then //LF
  8637. GotoToNextChar;
  8638. inc(fSourceLine);
  8639. fDeltaPos:=-(fPos-1);
  8640. end;
  8641. procedure TParser.SkipBOM;
  8642. begin
  8643. // No BOM support
  8644. end;
  8645. procedure TParser.SkipSpaces;
  8646. begin
  8647. while not FEofReached and (fBuf[fPos] in [' ',#9]) do GotoToNextChar;
  8648. end;
  8649. procedure TParser.SkipWhitespace;
  8650. begin
  8651. while not FEofReached do
  8652. begin
  8653. case fBuf[fPos] of
  8654. ' ',#9 : SkipSpaces;
  8655. #10,#13 : HandleNewLine
  8656. else break;
  8657. end;
  8658. end;
  8659. end;
  8660. procedure TParser.HandleEof;
  8661. begin
  8662. fToken:=toEOF;
  8663. fLastTokenStr:='';
  8664. end;
  8665. procedure TParser.HandleAlphaNum;
  8666. begin
  8667. fLastTokenStr:=GetAlphaNum;
  8668. fToken:=toSymbol;
  8669. end;
  8670. procedure TParser.HandleNumber;
  8671. type
  8672. floatPunct = (fpDot,fpE);
  8673. floatPuncts = set of floatPunct;
  8674. var
  8675. allowed : floatPuncts;
  8676. begin
  8677. fLastTokenStr:='';
  8678. while IsNumber do
  8679. ProcessChar;
  8680. fToken:=toInteger;
  8681. if (fBuf[fPos] in ['.','e','E']) then
  8682. begin
  8683. fToken:=toFloat;
  8684. allowed:=[fpDot,fpE];
  8685. while (fBuf[fPos] in ['.','e','E','0'..'9']) do
  8686. begin
  8687. case fBuf[fPos] of
  8688. '.' : if fpDot in allowed then Exclude(allowed,fpDot) else break;
  8689. 'E','e' : if fpE in allowed then
  8690. begin
  8691. allowed:=[];
  8692. ProcessChar;
  8693. if (fBuf[fPos] in ['+','-']) then ProcessChar;
  8694. if not (fBuf[fPos] in ['0'..'9']) then
  8695. ErrorFmt(SParserInvalidFloat,[fLastTokenStr+fBuf[fPos]]);
  8696. end
  8697. else break;
  8698. end;
  8699. ProcessChar;
  8700. end;
  8701. end;
  8702. if (fBuf[fPos] in ['s','S','d','D','c','C']) then //single, date, currency
  8703. begin
  8704. fFloatType:=fBuf[fPos];
  8705. GotoToNextChar;
  8706. fToken:=toFloat;
  8707. end
  8708. else fFloatType:=#0;
  8709. end;
  8710. procedure TParser.HandleHexNumber;
  8711. var valid : boolean;
  8712. begin
  8713. fLastTokenStr:='$';
  8714. GotoToNextChar;
  8715. valid:=false;
  8716. while IsHexNum do
  8717. begin
  8718. valid:=true;
  8719. ProcessChar;
  8720. end;
  8721. if not valid then
  8722. ErrorFmt(SParserInvalidInteger,[fLastTokenStr]);
  8723. fToken:=toInteger;
  8724. end;
  8725. function TParser.HandleQuotedString: string;
  8726. begin
  8727. Result:='';
  8728. GotoToNextChar;
  8729. while true do
  8730. begin
  8731. case fBuf[fPos] of
  8732. #0 : ErrorStr(SParserUnterminatedString);
  8733. #13,#10 : ErrorStr(SParserUnterminatedString);
  8734. '''' : begin
  8735. GotoToNextChar;
  8736. if fBuf[fPos]<>'''' then exit;
  8737. end;
  8738. end;
  8739. Result:=Result+fBuf[fPos];
  8740. GotoToNextChar;
  8741. end;
  8742. end;
  8743. Function TParser.HandleDecimalCharacter : Char;
  8744. var
  8745. i : integer;
  8746. begin
  8747. GotoToNextChar;
  8748. // read a word number
  8749. i:=0;
  8750. while IsNumber and (i<high(word)) do
  8751. begin
  8752. i:=i*10+Ord(fBuf[fPos])-ord('0');
  8753. GotoToNextChar;
  8754. end;
  8755. if i>high(word) then i:=0;
  8756. Result:=Char(i);
  8757. end;
  8758. procedure TParser.HandleString;
  8759. var
  8760. s: string;
  8761. begin
  8762. fLastTokenStr:='';
  8763. while true do
  8764. begin
  8765. case fBuf[fPos] of
  8766. '''' :
  8767. begin
  8768. s:=HandleQuotedString;
  8769. fLastTokenStr:=fLastTokenStr+s;
  8770. end;
  8771. '#' :
  8772. begin
  8773. fLastTokenStr:=fLastTokenStr+HandleDecimalCharacter;
  8774. end;
  8775. else break;
  8776. end;
  8777. end;
  8778. fToken:=Classes.toString
  8779. end;
  8780. procedure TParser.HandleMinus;
  8781. begin
  8782. GotoToNextChar;
  8783. if IsNumber then
  8784. begin
  8785. HandleNumber;
  8786. fLastTokenStr:='-'+fLastTokenStr;
  8787. end
  8788. else
  8789. begin
  8790. fToken:=toMinus;
  8791. fLastTokenStr:='-';
  8792. end;
  8793. end;
  8794. procedure TParser.HandleUnknown;
  8795. begin
  8796. fToken:=toUnknown;
  8797. fLastTokenStr:=fBuf[fPos];
  8798. GotoToNextChar;
  8799. end;
  8800. constructor TParser.Create(Stream: TStream);
  8801. begin
  8802. fStream:=Stream;
  8803. SetLength(fBuf,ParseBufSize);
  8804. fBufLen:=0;
  8805. fPos:=0;
  8806. fDeltaPos:=1;
  8807. fSourceLine:=1;
  8808. fEofReached:=false;
  8809. fLastTokenStr:='';
  8810. fFloatType:=#0;
  8811. fToken:=toEOF;
  8812. LoadBuffer;
  8813. SkipBom;
  8814. NextToken;
  8815. end;
  8816. procedure TParser.GotoToNextChar;
  8817. begin
  8818. Inc(FPos);
  8819. CheckLoadBuffer;
  8820. end;
  8821. destructor TParser.Destroy;
  8822. Var
  8823. aCount : Integer;
  8824. begin
  8825. aCount:=Length(fLastTokenStr)*2;
  8826. fStream.Position:=SourcePos-aCount;
  8827. end;
  8828. procedure TParser.CheckToken(T: tParserToken);
  8829. begin
  8830. if fToken<>T then
  8831. ErrorFmt(SParserWrongTokenType,[GetTokenName(T),GetTokenName(fToken)]);
  8832. end;
  8833. procedure TParser.CheckTokenSymbol(const S: string);
  8834. begin
  8835. CheckToken(toSymbol);
  8836. if CompareText(fLastTokenStr,S)<>0 then
  8837. ErrorFmt(SParserWrongTokenSymbol,[s,fLastTokenStr]);
  8838. end;
  8839. procedure TParser.Error(const Ident: string);
  8840. begin
  8841. ErrorStr(Ident);
  8842. end;
  8843. procedure TParser.ErrorFmt(const Ident: string; const Args: array of const);
  8844. begin
  8845. ErrorStr(Format(Ident,Args));
  8846. end;
  8847. procedure TParser.ErrorStr(const Message: string);
  8848. begin
  8849. raise EParserError.CreateFmt(Message+SParserLocInfo,[SourceLine,fPos+fDeltaPos,SourcePos]);
  8850. end;
  8851. procedure TParser.HexToBinary(Stream: TStream);
  8852. var
  8853. outbuf : TBytes;
  8854. b : byte;
  8855. i : integer;
  8856. begin
  8857. SetLength(OutBuf,ParseBufSize);
  8858. i:=0;
  8859. SkipWhitespace;
  8860. while IsHexNum do
  8861. begin
  8862. b:=(GetHexValue(fBuf[fPos]) shl 4);
  8863. GotoToNextChar;
  8864. if not IsHexNum then
  8865. Error(SParserUnterminatedBinValue);
  8866. b:=b or GetHexValue(fBuf[fPos]);
  8867. GotoToNextChar;
  8868. outbuf[i]:=b;
  8869. inc(i);
  8870. if i>=ParseBufSize then
  8871. begin
  8872. Stream.WriteBuffer(outbuf,i);
  8873. i:=0;
  8874. end;
  8875. SkipWhitespace;
  8876. end;
  8877. if i>0 then
  8878. Stream.WriteBuffer(outbuf,i);
  8879. NextToken;
  8880. end;
  8881. function TParser.NextToken: TParserToken;
  8882. Procedure SetToken(aToken : TParserToken);
  8883. begin
  8884. FToken:=aToken;
  8885. GotoToNextChar;
  8886. end;
  8887. begin
  8888. SkipWhiteSpace;
  8889. if fEofReached then
  8890. HandleEof
  8891. else
  8892. case fBuf[fPos] of
  8893. '_','A'..'Z','a'..'z' : HandleAlphaNum;
  8894. '$' : HandleHexNumber;
  8895. '-' : HandleMinus;
  8896. '0'..'9' : HandleNumber;
  8897. '''','#' : HandleString;
  8898. '[' : SetToken(toSetStart);
  8899. '(' : SetToken(toListStart);
  8900. '<' : SetToken(toCollectionStart);
  8901. '{' : SetToken(toBinaryStart);
  8902. ']' : SetToken(toSetEnd);
  8903. ')' : SetToken(toListEnd);
  8904. '>' : SetToken(toCollectionEnd);
  8905. '}' : SetToken(toBinaryEnd);
  8906. ',' : SetToken(toComma);
  8907. '.' : SetToken(toDot);
  8908. '=' : SetToken(toEqual);
  8909. ':' : SetToken(toColon);
  8910. '+' : SetToken(toPlus);
  8911. else
  8912. HandleUnknown;
  8913. end;
  8914. Result:=fToken;
  8915. end;
  8916. function TParser.SourcePos: Longint;
  8917. begin
  8918. Result:=fStream.Position-fBufLen+fPos;
  8919. end;
  8920. function TParser.TokenComponentIdent: string;
  8921. begin
  8922. if fToken<>toSymbol then
  8923. ErrorFmt(SParserExpected,[GetTokenName(toSymbol)]);
  8924. CheckLoadBuffer;
  8925. while fBuf[fPos]='.' do
  8926. begin
  8927. ProcessChar;
  8928. fLastTokenStr:=fLastTokenStr+GetAlphaNum;
  8929. end;
  8930. Result:=fLastTokenStr;
  8931. end;
  8932. Function TParser.TokenFloat: double;
  8933. var
  8934. errcode : integer;
  8935. begin
  8936. Val(fLastTokenStr,Result,errcode);
  8937. if errcode<>0 then
  8938. ErrorFmt(SParserInvalidFloat,[fLastTokenStr]);
  8939. end;
  8940. Function TParser.TokenInt: NativeInt;
  8941. begin
  8942. if not TryStrToInt64(fLastTokenStr,Result) then
  8943. Result:=StrToQWord(fLastTokenStr); //second chance for malformed files
  8944. end;
  8945. function TParser.TokenString: string;
  8946. begin
  8947. case fToken of
  8948. toFloat : if fFloatType<>#0 then
  8949. Result:=fLastTokenStr+fFloatType
  8950. else Result:=fLastTokenStr;
  8951. else
  8952. Result:=fLastTokenStr;
  8953. end;
  8954. end;
  8955. function TParser.TokenSymbolIs(const S: string): Boolean;
  8956. begin
  8957. Result:=(fToken=toSymbol) and (CompareText(fLastTokenStr,S)=0);
  8958. end;
  8959. procedure TObjectTextConverter.WriteWord(w : word); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  8960. begin
  8961. Output.WriteBufferData(w);
  8962. end;
  8963. procedure TObjectTextConverter.WriteDWord(lw : longword); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  8964. begin
  8965. Output.WriteBufferData(lw);
  8966. end;
  8967. procedure TObjectTextConverter.WriteQWord(q : NativeInt); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  8968. begin
  8969. Output.WriteBufferData(q);
  8970. end;
  8971. procedure TObjectTextConverter.WriteDouble(e : double);
  8972. begin
  8973. Output.WriteBufferData(e);
  8974. end;
  8975. procedure TObjectTextConverter.WriteString(s: String);
  8976. var
  8977. i,size : byte;
  8978. begin
  8979. if length(s)>255 then
  8980. size:=255
  8981. else
  8982. size:=length(s);
  8983. Output.WriteByte(size);
  8984. For I:=1 to Length(S) do
  8985. Output.WriteBufferData(s[i]);
  8986. end;
  8987. procedure TObjectTextConverter.WriteWString(Const s: WideString);
  8988. var
  8989. i : Integer;
  8990. begin
  8991. WriteDWord(Length(s));
  8992. For I:=1 to Length(S) do
  8993. Output.WriteBufferData(s[i]);
  8994. end;
  8995. procedure TObjectTextConverter.WriteInteger(value: NativeInt);
  8996. begin
  8997. if (value >= -128) and (value <= 127) then begin
  8998. Output.WriteByte(Ord(vaInt8));
  8999. Output.WriteByte(byte(value));
  9000. end else if (value >= -32768) and (value <= 32767) then begin
  9001. Output.WriteByte(Ord(vaInt16));
  9002. WriteWord(word(value));
  9003. end else if (value >= -2147483648) and (value <= 2147483647) then begin
  9004. Output.WriteByte(Ord(vaInt32));
  9005. WriteDWord(longword(value));
  9006. end else begin
  9007. Output.WriteByte(ord(vaInt64));
  9008. WriteQWord(NativeUInt(value));
  9009. end;
  9010. end;
  9011. procedure TObjectTextConverter.ProcessWideString(const left : string);
  9012. var
  9013. ws : string;
  9014. begin
  9015. ws:=left+parser.TokenString;
  9016. while parser.NextToken = toPlus do
  9017. begin
  9018. parser.NextToken; // Get next string fragment
  9019. if not (parser.Token=Classes.toString) then
  9020. parser.CheckToken(Classes.toString);
  9021. ws:=ws+parser.TokenString;
  9022. end;
  9023. Output.WriteByte(Ord(vaWstring));
  9024. WriteWString(ws);
  9025. end;
  9026. procedure TObjectTextConverter.ProcessValue;
  9027. var
  9028. flt: double;
  9029. stream: TBytesStream;
  9030. begin
  9031. case parser.Token of
  9032. toInteger:
  9033. begin
  9034. WriteInteger(parser.TokenInt);
  9035. parser.NextToken;
  9036. end;
  9037. toFloat:
  9038. begin
  9039. Output.WriteByte(Ord(vaExtended));
  9040. flt := Parser.TokenFloat;
  9041. WriteDouble(flt);
  9042. parser.NextToken;
  9043. end;
  9044. classes.toString:
  9045. ProcessWideString('');
  9046. toSymbol:
  9047. begin
  9048. if CompareText(parser.TokenString, 'True') = 0 then
  9049. Output.WriteByte(Ord(vaTrue))
  9050. else if CompareText(parser.TokenString, 'False') = 0 then
  9051. Output.WriteByte(Ord(vaFalse))
  9052. else if CompareText(parser.TokenString, 'nil') = 0 then
  9053. Output.WriteByte(Ord(vaNil))
  9054. else
  9055. begin
  9056. Output.WriteByte(Ord(vaIdent));
  9057. WriteString(parser.TokenComponentIdent);
  9058. end;
  9059. Parser.NextToken;
  9060. end;
  9061. // Set
  9062. toSetStart:
  9063. begin
  9064. parser.NextToken;
  9065. Output.WriteByte(Ord(vaSet));
  9066. if parser.Token <> toSetEnd then
  9067. while True do
  9068. begin
  9069. parser.CheckToken(toSymbol);
  9070. WriteString(parser.TokenString);
  9071. parser.NextToken;
  9072. if parser.Token = toSetEnd then
  9073. break;
  9074. parser.CheckToken(toComma);
  9075. parser.NextToken;
  9076. end;
  9077. Output.WriteByte(0);
  9078. parser.NextToken;
  9079. end;
  9080. // List
  9081. toListStart:
  9082. begin
  9083. parser.NextToken;
  9084. Output.WriteByte(Ord(vaList));
  9085. while parser.Token <> toListEnd do
  9086. ProcessValue;
  9087. Output.WriteByte(0);
  9088. parser.NextToken;
  9089. end;
  9090. // Collection
  9091. toCollectionStart:
  9092. begin
  9093. parser.NextToken;
  9094. Output.WriteByte(Ord(vaCollection));
  9095. while parser.Token <> toCollectionEnd do
  9096. begin
  9097. parser.CheckTokenSymbol('item');
  9098. parser.NextToken;
  9099. // ConvertOrder
  9100. Output.WriteByte(Ord(vaList));
  9101. while not parser.TokenSymbolIs('end') do
  9102. ProcessProperty;
  9103. parser.NextToken; // Skip 'end'
  9104. Output.WriteByte(0);
  9105. end;
  9106. Output.WriteByte(0);
  9107. parser.NextToken;
  9108. end;
  9109. // Binary data
  9110. toBinaryStart:
  9111. begin
  9112. Output.WriteByte(Ord(vaBinary));
  9113. stream := TBytesStream.Create;
  9114. try
  9115. parser.HexToBinary(stream);
  9116. WriteDWord(stream.Size);
  9117. Output.WriteBuffer(Stream.Bytes,Stream.Size);
  9118. finally
  9119. stream.Free;
  9120. end;
  9121. parser.NextToken;
  9122. end;
  9123. else
  9124. parser.Error(SParserInvalidProperty);
  9125. end;
  9126. end;
  9127. procedure TObjectTextConverter.ProcessProperty;
  9128. var
  9129. name: String;
  9130. begin
  9131. // Get name of property
  9132. parser.CheckToken(toSymbol);
  9133. name := parser.TokenString;
  9134. while True do begin
  9135. parser.NextToken;
  9136. if parser.Token <> toDot then break;
  9137. parser.NextToken;
  9138. parser.CheckToken(toSymbol);
  9139. name := name + '.' + parser.TokenString;
  9140. end;
  9141. WriteString(name);
  9142. parser.CheckToken(toEqual);
  9143. parser.NextToken;
  9144. ProcessValue;
  9145. end;
  9146. procedure TObjectTextConverter.ProcessObject;
  9147. var
  9148. Flags: Byte;
  9149. ObjectName, ObjectType: String;
  9150. ChildPos: Integer;
  9151. begin
  9152. if parser.TokenSymbolIs('OBJECT') then
  9153. Flags :=0 { IsInherited := False }
  9154. else begin
  9155. if parser.TokenSymbolIs('INHERITED') then
  9156. Flags := 1 { IsInherited := True; }
  9157. else begin
  9158. parser.CheckTokenSymbol('INLINE');
  9159. Flags := 4;
  9160. end;
  9161. end;
  9162. parser.NextToken;
  9163. parser.CheckToken(toSymbol);
  9164. ObjectName := '';
  9165. ObjectType := parser.TokenString;
  9166. parser.NextToken;
  9167. if parser.Token = toColon then begin
  9168. parser.NextToken;
  9169. parser.CheckToken(toSymbol);
  9170. ObjectName := ObjectType;
  9171. ObjectType := parser.TokenString;
  9172. parser.NextToken;
  9173. if parser.Token = toSetStart then begin
  9174. parser.NextToken;
  9175. ChildPos := parser.TokenInt;
  9176. parser.NextToken;
  9177. parser.CheckToken(toSetEnd);
  9178. parser.NextToken;
  9179. Flags := Flags or 2;
  9180. end;
  9181. end;
  9182. if Flags <> 0 then begin
  9183. Output.WriteByte($f0 or Flags);
  9184. if (Flags and 2) <> 0 then
  9185. WriteInteger(ChildPos);
  9186. end;
  9187. WriteString(ObjectType);
  9188. WriteString(ObjectName);
  9189. // Convert property list
  9190. while not (parser.TokenSymbolIs('END') or
  9191. parser.TokenSymbolIs('OBJECT') or
  9192. parser.TokenSymbolIs('INHERITED') or
  9193. parser.TokenSymbolIs('INLINE')) do
  9194. ProcessProperty;
  9195. Output.WriteByte(0); // Terminate property list
  9196. // Convert child objects
  9197. while not parser.TokenSymbolIs('END') do ProcessObject;
  9198. parser.NextToken; // Skip end token
  9199. Output.WriteByte(0); // Terminate property list
  9200. end;
  9201. procedure TObjectTextConverter.ObjectTextToBinary(aInput, aOutput: TStream);
  9202. begin
  9203. FinPut:=aInput;
  9204. FOutput:=aOutput;
  9205. Execute;
  9206. end;
  9207. procedure TObjectTextConverter.Execute;
  9208. begin
  9209. If Not Assigned(Input) then
  9210. raise EReadError.Create('Missing input stream');
  9211. If Not Assigned(Output) then
  9212. raise EReadError.Create('Missing output stream');
  9213. FParser := TParser.Create(Input);
  9214. try
  9215. Output.WriteBufferData(FilerSignatureInt);
  9216. ProcessObject;
  9217. finally
  9218. FParser.Free;
  9219. end;
  9220. end;
  9221. procedure ObjectTextToBinary(aInput, aOutput: TStream);
  9222. var
  9223. Conv : TObjectTextConverter;
  9224. begin
  9225. Conv:=TObjectTextConverter.Create;
  9226. try
  9227. Conv.ObjectTextToBinary(aInput, aOutput);
  9228. finally
  9229. Conv.free;
  9230. end;
  9231. end;
  9232. initialization
  9233. ClassList:=TJSObject.New;
  9234. end.