trio.c 157 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892
  1. /*************************************************************************
  2. *
  3. * $Id$
  4. *
  5. * Copyright (C) 1998 Bjorn Reese and Daniel Stenberg.
  6. *
  7. * Permission to use, copy, modify, and distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  12. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
  14. * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
  15. *
  16. *************************************************************************
  17. *
  18. * A note to trio contributors:
  19. *
  20. * Avoid heap allocation at all costs to ensure that the trio functions
  21. * are async-safe. The exceptions are the printf/fprintf functions, which
  22. * uses fputc, and the asprintf functions and the <alloc> modifier, which
  23. * by design are required to allocate form the heap.
  24. *
  25. ************************************************************************/
  26. /*
  27. * TODO:
  28. * - Scan is probably too permissive about its modifiers.
  29. * - C escapes in %#[] ?
  30. * - Multibyte characters (done for format parsing, except scan groups)
  31. * - Complex numbers? (C99 _Complex)
  32. * - Boolean values? (C99 _Bool)
  33. * - C99 NaN(n-char-sequence) missing. The n-char-sequence can be used
  34. * to print the mantissa, e.g. NaN(0xc000000000000000)
  35. * - Should we support the GNU %a alloc modifier? GNU has an ugly hack
  36. * for %a, because C99 used %a for other purposes. If specified as
  37. * %as or %a[ it is interpreted as the alloc modifier, otherwise as
  38. * the C99 hex-float. This means that you cannot scan %as as a hex-float
  39. * immediately followed by an 's'.
  40. * - Scanning of collating symbols.
  41. */
  42. /*************************************************************************
  43. * Trio include files
  44. */
  45. #include "triodef.h"
  46. #include "trio.h"
  47. #include "triop.h"
  48. #include "trionan.h"
  49. #if !defined(TRIO_MINIMAL)
  50. # include "triostr.h"
  51. #endif
  52. /**************************************************************************
  53. *
  54. * Definitions
  55. *
  56. *************************************************************************/
  57. #include <math.h>
  58. #include <limits.h>
  59. #include <float.h>
  60. #if (defined(__STDC_ISO_10646__) || defined(MB_LEN_MAX) \
  61. || defined(USE_MULTIBYTE) || TRIO_WIDECHAR) \
  62. && !defined(_WIN32_WCE)
  63. # define TRIO_COMPILER_SUPPORTS_MULTIBYTE
  64. # if !defined(MB_LEN_MAX)
  65. # define MB_LEN_MAX 6
  66. # endif
  67. #endif
  68. #if (defined(TRIO_COMPILER_MSVC) && (_MSC_VER >= 1100)) || defined(TRIO_COMPILER_BCB)
  69. # define TRIO_COMPILER_SUPPORTS_MSVC_INT
  70. #endif
  71. #if defined(_WIN32_WCE)
  72. #include <wincecompat.h>
  73. #endif
  74. /*************************************************************************
  75. * Generic definitions
  76. */
  77. #if !(defined(DEBUG) || defined(NDEBUG))
  78. # define NDEBUG
  79. #endif
  80. #include <assert.h>
  81. #include <ctype.h>
  82. #if !defined(TRIO_COMPILER_SUPPORTS_C99)
  83. # define isblank(x) (((x)==32) || ((x)==9))
  84. #endif
  85. #if defined(TRIO_COMPILER_ANCIENT)
  86. # include <varargs.h>
  87. #else
  88. # include <stdarg.h>
  89. #endif
  90. #include <stddef.h>
  91. #include <errno.h>
  92. #ifndef NULL
  93. # define NULL 0
  94. #endif
  95. #define NIL ((char)0)
  96. #ifndef FALSE
  97. # define FALSE (1 == 0)
  98. # define TRUE (! FALSE)
  99. #endif
  100. #define BOOLEAN_T int
  101. /* mincore() can be used for debugging purposes */
  102. #define VALID(x) (NULL != (x))
  103. #if TRIO_ERRORS
  104. /*
  105. * Encode the error code and the position. This is decoded
  106. * with TRIO_ERROR_CODE and TRIO_ERROR_POSITION.
  107. */
  108. # define TRIO_ERROR_RETURN(x,y) (- ((x) + ((y) << 8)))
  109. #else
  110. # define TRIO_ERROR_RETURN(x,y) (-1)
  111. #endif
  112. #ifndef VA_LIST_IS_ARRAY
  113. #define TRIO_VA_LIST_PTR va_list *
  114. #define TRIO_VA_LIST_ADDR(l) (&(l))
  115. #define TRIO_VA_LIST_DEREF(l) (*(l))
  116. #else
  117. #define TRIO_VA_LIST_PTR va_list
  118. #define TRIO_VA_LIST_ADDR(l) (l)
  119. #define TRIO_VA_LIST_DEREF(l) (l)
  120. #endif
  121. typedef unsigned long trio_flags_t;
  122. /*************************************************************************
  123. * Platform specific definitions
  124. */
  125. #if defined(TRIO_PLATFORM_UNIX) || defined(TRIO_PLATFORM_OS400)
  126. # include <unistd.h>
  127. # include <signal.h>
  128. # include <locale.h>
  129. # define USE_LOCALE
  130. #endif /* TRIO_PLATFORM_UNIX */
  131. #if defined(TRIO_PLATFORM_VMS)
  132. # include <unistd.h>
  133. #endif
  134. #if defined(TRIO_PLATFORM_WIN32)
  135. # if defined(_WIN32_WCE)
  136. # include <wincecompat.h>
  137. # else
  138. # include <io.h>
  139. # define read _read
  140. # define write _write
  141. # endif
  142. #endif /* TRIO_PLATFORM_WIN32 */
  143. #if TRIO_WIDECHAR
  144. # if defined(TRIO_COMPILER_SUPPORTS_ISO94)
  145. # include <wchar.h>
  146. # include <wctype.h>
  147. typedef wchar_t trio_wchar_t;
  148. typedef wint_t trio_wint_t;
  149. # else
  150. typedef char trio_wchar_t;
  151. typedef int trio_wint_t;
  152. # define WCONST(x) L ## x
  153. # define WEOF EOF
  154. # define iswalnum(x) isalnum(x)
  155. # define iswalpha(x) isalpha(x)
  156. # define iswblank(x) isblank(x)
  157. # define iswcntrl(x) iscntrl(x)
  158. # define iswdigit(x) isdigit(x)
  159. # define iswgraph(x) isgraph(x)
  160. # define iswlower(x) islower(x)
  161. # define iswprint(x) isprint(x)
  162. # define iswpunct(x) ispunct(x)
  163. # define iswspace(x) isspace(x)
  164. # define iswupper(x) isupper(x)
  165. # define iswxdigit(x) isxdigit(x)
  166. # endif
  167. #endif
  168. /*************************************************************************
  169. * Compiler dependent definitions
  170. */
  171. /* Support for long long */
  172. #ifndef __cplusplus
  173. # if !defined(USE_LONGLONG)
  174. # if defined(TRIO_COMPILER_GCC) && !defined(__STRICT_ANSI__)
  175. # define USE_LONGLONG
  176. # elif defined(TRIO_COMPILER_SUNPRO)
  177. # define USE_LONGLONG
  178. # elif defined(_LONG_LONG) || defined(_LONGLONG)
  179. # define USE_LONGLONG
  180. # endif
  181. # endif
  182. #endif
  183. /* The extra long numbers */
  184. #if defined(USE_LONGLONG)
  185. typedef signed long long int trio_longlong_t;
  186. typedef unsigned long long int trio_ulonglong_t;
  187. #elif defined(TRIO_COMPILER_SUPPORTS_MSVC_INT)
  188. typedef signed __int64 trio_longlong_t;
  189. typedef unsigned __int64 trio_ulonglong_t;
  190. #else
  191. typedef TRIO_SIGNED long int trio_longlong_t;
  192. typedef unsigned long int trio_ulonglong_t;
  193. #endif
  194. /* Maximal and fixed integer types */
  195. #if defined(TRIO_COMPILER_SUPPORTS_C99) && !defined( __VMS )
  196. # include <stdint.h>
  197. typedef intmax_t trio_intmax_t;
  198. typedef uintmax_t trio_uintmax_t;
  199. typedef int8_t trio_int8_t;
  200. typedef int16_t trio_int16_t;
  201. typedef int32_t trio_int32_t;
  202. typedef int64_t trio_int64_t;
  203. #elif defined(TRIO_COMPILER_SUPPORTS_UNIX98) || defined( __VMS )
  204. # include <inttypes.h>
  205. #ifdef __VMS
  206. typedef long long int intmax_t;
  207. typedef unsigned long long int uintmax_t;
  208. #endif
  209. typedef intmax_t trio_intmax_t;
  210. typedef uintmax_t trio_uintmax_t;
  211. typedef int8_t trio_int8_t;
  212. typedef int16_t trio_int16_t;
  213. typedef int32_t trio_int32_t;
  214. typedef int64_t trio_int64_t;
  215. #elif defined(TRIO_COMPILER_SUPPORTS_MSVC_INT)
  216. typedef trio_longlong_t trio_intmax_t;
  217. typedef trio_ulonglong_t trio_uintmax_t;
  218. typedef __int8 trio_int8_t;
  219. typedef __int16 trio_int16_t;
  220. typedef __int32 trio_int32_t;
  221. typedef __int64 trio_int64_t;
  222. #else
  223. typedef trio_longlong_t trio_intmax_t;
  224. typedef trio_ulonglong_t trio_uintmax_t;
  225. # if defined(TRIO_INT8_T)
  226. typedef TRIO_INT8_T trio_int8_t;
  227. # else
  228. typedef TRIO_SIGNED char trio_int8_t;
  229. # endif
  230. # if defined(TRIO_INT16_T)
  231. typedef TRIO_INT16_T trio_int16_t;
  232. # else
  233. typedef TRIO_SIGNED short trio_int16_t;
  234. # endif
  235. # if defined(TRIO_INT32_T)
  236. typedef TRIO_INT32_T trio_int32_t;
  237. # else
  238. typedef TRIO_SIGNED int trio_int32_t;
  239. # endif
  240. # if defined(TRIO_INT64_T)
  241. typedef TRIO_INT64_T trio_int64_t;
  242. # else
  243. typedef trio_longlong_t trio_int64_t;
  244. # endif
  245. #endif
  246. #if (!(defined(TRIO_COMPILER_SUPPORTS_C99) \
  247. || defined(TRIO_COMPILER_SUPPORTS_UNIX01))) \
  248. && !defined(_WIN32_WCE)
  249. # define floorl(x) floor((double)(x))
  250. # define fmodl(x,y) fmod((double)(x),(double)(y))
  251. # define powl(x,y) pow((double)(x),(double)(y))
  252. #endif
  253. #define TRIO_FABS(x) (((x) < 0.0) ? -(x) : (x))
  254. /*************************************************************************
  255. * Internal Definitions
  256. */
  257. #ifndef DECIMAL_DIG
  258. # define DECIMAL_DIG DBL_DIG
  259. #endif
  260. /* Long double sizes */
  261. #ifdef LDBL_DIG
  262. # define MAX_MANTISSA_DIGITS LDBL_DIG
  263. # define MAX_EXPONENT_DIGITS 4
  264. # define MAX_DOUBLE_DIGITS LDBL_MAX_10_EXP
  265. #else
  266. # define MAX_MANTISSA_DIGITS DECIMAL_DIG
  267. # define MAX_EXPONENT_DIGITS 3
  268. # define MAX_DOUBLE_DIGITS DBL_MAX_10_EXP
  269. #endif
  270. #if defined(TRIO_COMPILER_ANCIENT) || !defined(LDBL_DIG)
  271. # undef LDBL_DIG
  272. # undef LDBL_MANT_DIG
  273. # undef LDBL_EPSILON
  274. # define LDBL_DIG DBL_DIG
  275. # define LDBL_MANT_DIG DBL_MANT_DIG
  276. # define LDBL_EPSILON DBL_EPSILON
  277. #endif
  278. /* The maximal number of digits is for base 2 */
  279. #define MAX_CHARS_IN(x) (sizeof(x) * CHAR_BIT)
  280. /* The width of a pointer. The number of bits in a hex digit is 4 */
  281. #define POINTER_WIDTH ((sizeof("0x") - 1) + sizeof(trio_pointer_t) * CHAR_BIT / 4)
  282. /* Infinite and Not-A-Number for floating-point */
  283. #define INFINITE_LOWER "inf"
  284. #define INFINITE_UPPER "INF"
  285. #define LONG_INFINITE_LOWER "infinite"
  286. #define LONG_INFINITE_UPPER "INFINITE"
  287. #define NAN_LOWER "nan"
  288. #define NAN_UPPER "NAN"
  289. #if !defined(HAVE_ISASCII) && !defined(isascii)
  290. #ifndef __VMS
  291. # define isascii(x) ((unsigned int)(x) < 128)
  292. #endif
  293. #endif
  294. /* Various constants */
  295. enum {
  296. TYPE_PRINT = 1,
  297. TYPE_SCAN = 2,
  298. /* Flags. FLAGS_LAST must be less than ULONG_MAX */
  299. FLAGS_NEW = 0,
  300. FLAGS_STICKY = 1,
  301. FLAGS_SPACE = 2 * FLAGS_STICKY,
  302. FLAGS_SHOWSIGN = 2 * FLAGS_SPACE,
  303. FLAGS_LEFTADJUST = 2 * FLAGS_SHOWSIGN,
  304. FLAGS_ALTERNATIVE = 2 * FLAGS_LEFTADJUST,
  305. FLAGS_SHORT = 2 * FLAGS_ALTERNATIVE,
  306. FLAGS_SHORTSHORT = 2 * FLAGS_SHORT,
  307. FLAGS_LONG = 2 * FLAGS_SHORTSHORT,
  308. FLAGS_QUAD = 2 * FLAGS_LONG,
  309. FLAGS_LONGDOUBLE = 2 * FLAGS_QUAD,
  310. FLAGS_SIZE_T = 2 * FLAGS_LONGDOUBLE,
  311. FLAGS_PTRDIFF_T = 2 * FLAGS_SIZE_T,
  312. FLAGS_INTMAX_T = 2 * FLAGS_PTRDIFF_T,
  313. FLAGS_NILPADDING = 2 * FLAGS_INTMAX_T,
  314. FLAGS_UNSIGNED = 2 * FLAGS_NILPADDING,
  315. FLAGS_UPPER = 2 * FLAGS_UNSIGNED,
  316. FLAGS_WIDTH = 2 * FLAGS_UPPER,
  317. FLAGS_WIDTH_PARAMETER = 2 * FLAGS_WIDTH,
  318. FLAGS_PRECISION = 2 * FLAGS_WIDTH_PARAMETER,
  319. FLAGS_PRECISION_PARAMETER = 2 * FLAGS_PRECISION,
  320. FLAGS_BASE = 2 * FLAGS_PRECISION_PARAMETER,
  321. FLAGS_BASE_PARAMETER = 2 * FLAGS_BASE,
  322. FLAGS_FLOAT_E = 2 * FLAGS_BASE_PARAMETER,
  323. FLAGS_FLOAT_G = 2 * FLAGS_FLOAT_E,
  324. FLAGS_QUOTE = 2 * FLAGS_FLOAT_G,
  325. FLAGS_WIDECHAR = 2 * FLAGS_QUOTE,
  326. FLAGS_ALLOC = 2 * FLAGS_WIDECHAR,
  327. FLAGS_IGNORE = 2 * FLAGS_ALLOC,
  328. FLAGS_IGNORE_PARAMETER = 2 * FLAGS_IGNORE,
  329. FLAGS_VARSIZE_PARAMETER = 2 * FLAGS_IGNORE_PARAMETER,
  330. FLAGS_FIXED_SIZE = 2 * FLAGS_VARSIZE_PARAMETER,
  331. FLAGS_LAST = FLAGS_FIXED_SIZE,
  332. /* Reused flags */
  333. FLAGS_EXCLUDE = FLAGS_SHORT,
  334. FLAGS_USER_DEFINED = FLAGS_IGNORE,
  335. FLAGS_ROUNDING = FLAGS_INTMAX_T,
  336. /* Compounded flags */
  337. FLAGS_ALL_VARSIZES = FLAGS_LONG | FLAGS_QUAD | FLAGS_INTMAX_T | FLAGS_PTRDIFF_T | FLAGS_SIZE_T,
  338. FLAGS_ALL_SIZES = FLAGS_ALL_VARSIZES | FLAGS_SHORTSHORT | FLAGS_SHORT,
  339. NO_POSITION = -1,
  340. NO_WIDTH = 0,
  341. NO_PRECISION = -1,
  342. NO_SIZE = -1,
  343. /* Do not change these */
  344. NO_BASE = -1,
  345. MIN_BASE = 2,
  346. MAX_BASE = 36,
  347. BASE_BINARY = 2,
  348. BASE_OCTAL = 8,
  349. BASE_DECIMAL = 10,
  350. BASE_HEX = 16,
  351. /* Maximal number of allowed parameters */
  352. MAX_PARAMETERS = 64,
  353. /* Maximal number of characters in class */
  354. MAX_CHARACTER_CLASS = UCHAR_MAX + 1,
  355. /* Maximal string lengths for user-defined specifiers */
  356. MAX_USER_NAME = 64,
  357. MAX_USER_DATA = 256,
  358. /* Maximal length of locale separator strings */
  359. MAX_LOCALE_SEPARATOR_LENGTH = MB_LEN_MAX,
  360. /* Maximal number of integers in grouping */
  361. MAX_LOCALE_GROUPS = 64,
  362. /* Initial size of asprintf buffer */
  363. DYNAMIC_START_SIZE = 32
  364. };
  365. #define NO_GROUPING ((int)CHAR_MAX)
  366. /* Fundamental formatting parameter types */
  367. #define FORMAT_UNKNOWN 0
  368. #define FORMAT_INT 1
  369. #define FORMAT_DOUBLE 2
  370. #define FORMAT_CHAR 3
  371. #define FORMAT_STRING 4
  372. #define FORMAT_POINTER 5
  373. #define FORMAT_COUNT 6
  374. #define FORMAT_PARAMETER 7
  375. #define FORMAT_GROUP 8
  376. #if TRIO_GNU
  377. # define FORMAT_ERRNO 9
  378. #endif
  379. #if TRIO_EXTENSION
  380. # define FORMAT_USER_DEFINED 10
  381. #endif
  382. /* Character constants */
  383. #define CHAR_IDENTIFIER '%'
  384. #define CHAR_BACKSLASH '\\'
  385. #define CHAR_QUOTE '\"'
  386. #define CHAR_ADJUST ' '
  387. /* Character class expressions */
  388. #define CLASS_ALNUM "[:alnum:]"
  389. #define CLASS_ALPHA "[:alpha:]"
  390. #define CLASS_BLANK "[:blank:]"
  391. #define CLASS_CNTRL "[:cntrl:]"
  392. #define CLASS_DIGIT "[:digit:]"
  393. #define CLASS_GRAPH "[:graph:]"
  394. #define CLASS_LOWER "[:lower:]"
  395. #define CLASS_PRINT "[:print:]"
  396. #define CLASS_PUNCT "[:punct:]"
  397. #define CLASS_SPACE "[:space:]"
  398. #define CLASS_UPPER "[:upper:]"
  399. #define CLASS_XDIGIT "[:xdigit:]"
  400. /*
  401. * SPECIFIERS:
  402. *
  403. *
  404. * a Hex-float
  405. * A Hex-float
  406. * c Character
  407. * C Widechar character (wint_t)
  408. * d Decimal
  409. * e Float
  410. * E Float
  411. * F Float
  412. * F Float
  413. * g Float
  414. * G Float
  415. * i Integer
  416. * m Error message
  417. * n Count
  418. * o Octal
  419. * p Pointer
  420. * s String
  421. * S Widechar string (wchar_t *)
  422. * u Unsigned
  423. * x Hex
  424. * X Hex
  425. * [] Group
  426. * <> User-defined
  427. *
  428. * Reserved:
  429. *
  430. * D Binary Coded Decimal %D(length,precision) (OS/390)
  431. */
  432. #define SPECIFIER_CHAR 'c'
  433. #define SPECIFIER_STRING 's'
  434. #define SPECIFIER_DECIMAL 'd'
  435. #define SPECIFIER_INTEGER 'i'
  436. #define SPECIFIER_UNSIGNED 'u'
  437. #define SPECIFIER_OCTAL 'o'
  438. #define SPECIFIER_HEX 'x'
  439. #define SPECIFIER_HEX_UPPER 'X'
  440. #define SPECIFIER_FLOAT_E 'e'
  441. #define SPECIFIER_FLOAT_E_UPPER 'E'
  442. #define SPECIFIER_FLOAT_F 'f'
  443. #define SPECIFIER_FLOAT_F_UPPER 'F'
  444. #define SPECIFIER_FLOAT_G 'g'
  445. #define SPECIFIER_FLOAT_G_UPPER 'G'
  446. #define SPECIFIER_POINTER 'p'
  447. #define SPECIFIER_GROUP '['
  448. #define SPECIFIER_UNGROUP ']'
  449. #define SPECIFIER_COUNT 'n'
  450. #if TRIO_UNIX98
  451. # define SPECIFIER_CHAR_UPPER 'C'
  452. # define SPECIFIER_STRING_UPPER 'S'
  453. #endif
  454. #if TRIO_C99
  455. # define SPECIFIER_HEXFLOAT 'a'
  456. # define SPECIFIER_HEXFLOAT_UPPER 'A'
  457. #endif
  458. #if TRIO_GNU
  459. # define SPECIFIER_ERRNO 'm'
  460. #endif
  461. #if TRIO_EXTENSION
  462. # define SPECIFIER_BINARY 'b'
  463. # define SPECIFIER_BINARY_UPPER 'B'
  464. # define SPECIFIER_USER_DEFINED_BEGIN '<'
  465. # define SPECIFIER_USER_DEFINED_END '>'
  466. # define SPECIFIER_USER_DEFINED_SEPARATOR ':'
  467. #endif
  468. /*
  469. * QUALIFIERS:
  470. *
  471. *
  472. * Numbers = d,i,o,u,x,X
  473. * Float = a,A,e,E,f,F,g,G
  474. * String = s
  475. * Char = c
  476. *
  477. *
  478. * 9$ Position
  479. * Use the 9th parameter. 9 can be any number between 1 and
  480. * the maximal argument
  481. *
  482. * 9 Width
  483. * Set width to 9. 9 can be any number, but must not be postfixed
  484. * by '$'
  485. *
  486. * h Short
  487. * Numbers:
  488. * (unsigned) short int
  489. *
  490. * hh Short short
  491. * Numbers:
  492. * (unsigned) char
  493. *
  494. * l Long
  495. * Numbers:
  496. * (unsigned) long int
  497. * String:
  498. * as the S specifier
  499. * Char:
  500. * as the C specifier
  501. *
  502. * ll Long Long
  503. * Numbers:
  504. * (unsigned) long long int
  505. *
  506. * L Long Double
  507. * Float
  508. * long double
  509. *
  510. * # Alternative
  511. * Float:
  512. * Decimal-point is always present
  513. * String:
  514. * non-printable characters are handled as \number
  515. *
  516. * Spacing
  517. *
  518. * + Sign
  519. *
  520. * - Alignment
  521. *
  522. * . Precision
  523. *
  524. * * Parameter
  525. * print: use parameter
  526. * scan: no parameter (ignore)
  527. *
  528. * q Quad
  529. *
  530. * Z size_t
  531. *
  532. * w Widechar
  533. *
  534. * ' Thousands/quote
  535. * Numbers:
  536. * Integer part grouped in thousands
  537. * Binary numbers:
  538. * Number grouped in nibbles (4 bits)
  539. * String:
  540. * Quoted string
  541. *
  542. * j intmax_t
  543. * t prtdiff_t
  544. * z size_t
  545. *
  546. * ! Sticky
  547. * @ Parameter (for both print and scan)
  548. *
  549. * I n-bit Integer
  550. * Numbers:
  551. * The following options exists
  552. * I8 = 8-bit integer
  553. * I16 = 16-bit integer
  554. * I32 = 32-bit integer
  555. * I64 = 64-bit integer
  556. */
  557. #define QUALIFIER_POSITION '$'
  558. #define QUALIFIER_SHORT 'h'
  559. #define QUALIFIER_LONG 'l'
  560. #define QUALIFIER_LONG_UPPER 'L'
  561. #define QUALIFIER_ALTERNATIVE '#'
  562. #define QUALIFIER_SPACE ' '
  563. #define QUALIFIER_PLUS '+'
  564. #define QUALIFIER_MINUS '-'
  565. #define QUALIFIER_DOT '.'
  566. #define QUALIFIER_STAR '*'
  567. #define QUALIFIER_CIRCUMFLEX '^' /* For scanlists */
  568. #if TRIO_C99
  569. # define QUALIFIER_SIZE_T 'z'
  570. # define QUALIFIER_PTRDIFF_T 't'
  571. # define QUALIFIER_INTMAX_T 'j'
  572. #endif
  573. #if TRIO_BSD || TRIO_GNU
  574. # define QUALIFIER_QUAD 'q'
  575. #endif
  576. #if TRIO_GNU
  577. # define QUALIFIER_SIZE_T_UPPER 'Z'
  578. #endif
  579. #if TRIO_MISC
  580. # define QUALIFIER_WIDECHAR 'w'
  581. #endif
  582. #if TRIO_MICROSOFT
  583. # define QUALIFIER_FIXED_SIZE 'I'
  584. #endif
  585. #if TRIO_EXTENSION
  586. # define QUALIFIER_QUOTE '\''
  587. # define QUALIFIER_STICKY '!'
  588. # define QUALIFIER_VARSIZE '&' /* This should remain undocumented */
  589. # define QUALIFIER_PARAM '@' /* Experimental */
  590. # define QUALIFIER_COLON ':' /* For scanlists */
  591. # define QUALIFIER_EQUAL '=' /* For scanlists */
  592. # define QUALIFIER_ROUNDING_UPPER 'R'
  593. #endif
  594. /*************************************************************************
  595. *
  596. * Internal Structures
  597. *
  598. *************************************************************************/
  599. /* Parameters */
  600. typedef struct {
  601. /* An indication of which entry in the data union is used */
  602. int type;
  603. /* The flags */
  604. trio_flags_t flags;
  605. /* The width qualifier */
  606. int width;
  607. /* The precision qualifier */
  608. int precision;
  609. /* The base qualifier */
  610. int base;
  611. /* The size for the variable size qualifier */
  612. int varsize;
  613. /* The marker of the end of the specifier */
  614. int indexAfterSpecifier;
  615. /* The data from the argument list */
  616. union {
  617. char *string;
  618. #if TRIO_WIDECHAR
  619. trio_wchar_t *wstring;
  620. #endif
  621. trio_pointer_t pointer;
  622. union {
  623. trio_intmax_t as_signed;
  624. trio_uintmax_t as_unsigned;
  625. } number;
  626. double doubleNumber;
  627. double *doublePointer;
  628. trio_long_double_t longdoubleNumber;
  629. trio_long_double_t *longdoublePointer;
  630. int errorNumber;
  631. } data;
  632. /* For the user-defined specifier */
  633. char user_name[MAX_USER_NAME];
  634. char user_data[MAX_USER_DATA];
  635. } trio_parameter_t;
  636. /* Container for customized functions */
  637. typedef struct {
  638. union {
  639. trio_outstream_t out;
  640. trio_instream_t in;
  641. } stream;
  642. trio_pointer_t closure;
  643. } trio_custom_t;
  644. /* General trio "class" */
  645. typedef struct _trio_class_t {
  646. /*
  647. * The function to write characters to a stream.
  648. */
  649. void (*OutStream) TRIO_PROTO((struct _trio_class_t *, int));
  650. /*
  651. * The function to read characters from a stream.
  652. */
  653. void (*InStream) TRIO_PROTO((struct _trio_class_t *, int *));
  654. /*
  655. * The current location in the stream.
  656. */
  657. trio_pointer_t location;
  658. /*
  659. * The character currently being processed.
  660. */
  661. int current;
  662. /*
  663. * The number of characters that would have been written/read
  664. * if there had been sufficient space.
  665. */
  666. int processed;
  667. /*
  668. * The number of characters that are actually written/read.
  669. * Processed and committed will only differ for the *nprintf
  670. * and *nscanf functions.
  671. */
  672. int committed;
  673. /*
  674. * The upper limit of characters that may be written/read.
  675. */
  676. int max;
  677. /*
  678. * The last output error that was detected.
  679. */
  680. int error;
  681. } trio_class_t;
  682. /* References (for user-defined callbacks) */
  683. typedef struct _trio_reference_t {
  684. trio_class_t *data;
  685. trio_parameter_t *parameter;
  686. } trio_reference_t;
  687. /* Registered entries (for user-defined callbacks) */
  688. typedef struct _trio_userdef_t {
  689. struct _trio_userdef_t *next;
  690. trio_callback_t callback;
  691. char *name;
  692. } trio_userdef_t;
  693. /*************************************************************************
  694. *
  695. * Internal Variables
  696. *
  697. *************************************************************************/
  698. static TRIO_CONST char rcsid[] = "@(#)$Id$";
  699. /*
  700. * Need this to workaround a parser bug in HP C/iX compiler that fails
  701. * to resolves macro definitions that includes type 'long double',
  702. * e.g: va_arg(arg_ptr, long double)
  703. */
  704. #if defined(TRIO_PLATFORM_MPEIX)
  705. static TRIO_CONST trio_long_double_t ___dummy_long_double = 0;
  706. #endif
  707. static TRIO_CONST char internalNullString[] = "(nil)";
  708. #if defined(USE_LOCALE)
  709. static struct lconv *internalLocaleValues = NULL;
  710. #endif
  711. /*
  712. * UNIX98 says "in a locale where the radix character is not defined,
  713. * the radix character defaults to a period (.)"
  714. */
  715. static int internalDecimalPointLength = 1;
  716. static int internalThousandSeparatorLength = 1;
  717. static char internalDecimalPoint = '.';
  718. static char internalDecimalPointString[MAX_LOCALE_SEPARATOR_LENGTH + 1] = ".";
  719. static char internalThousandSeparator[MAX_LOCALE_SEPARATOR_LENGTH + 1] = ",";
  720. static char internalGrouping[MAX_LOCALE_GROUPS] = { (char)NO_GROUPING };
  721. static TRIO_CONST char internalDigitsLower[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  722. static TRIO_CONST char internalDigitsUpper[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  723. static BOOLEAN_T internalDigitsUnconverted = TRUE;
  724. static int internalDigitArray[128];
  725. #if TRIO_EXTENSION
  726. static BOOLEAN_T internalCollationUnconverted = TRUE;
  727. static char internalCollationArray[MAX_CHARACTER_CLASS][MAX_CHARACTER_CLASS];
  728. #endif
  729. #if TRIO_EXTENSION
  730. static TRIO_VOLATILE trio_callback_t internalEnterCriticalRegion = NULL;
  731. static TRIO_VOLATILE trio_callback_t internalLeaveCriticalRegion = NULL;
  732. static trio_userdef_t *internalUserDef = NULL;
  733. #endif
  734. /*************************************************************************
  735. *
  736. * Internal Functions
  737. *
  738. ************************************************************************/
  739. #if defined(TRIO_MINIMAL)
  740. # define TRIO_STRING_PUBLIC static
  741. # include "triostr.c"
  742. #endif /* defined(TRIO_MINIMAL) */
  743. /*************************************************************************
  744. * TrioIsQualifier
  745. *
  746. * Description:
  747. * Remember to add all new qualifiers to this function.
  748. * QUALIFIER_POSITION must not be added.
  749. */
  750. TRIO_PRIVATE BOOLEAN_T
  751. TrioIsQualifier
  752. TRIO_ARGS1((character),
  753. TRIO_CONST char character)
  754. {
  755. /* QUALIFIER_POSITION is not included */
  756. switch (character)
  757. {
  758. case '0': case '1': case '2': case '3': case '4':
  759. case '5': case '6': case '7': case '8': case '9':
  760. case QUALIFIER_PLUS:
  761. case QUALIFIER_MINUS:
  762. case QUALIFIER_SPACE:
  763. case QUALIFIER_DOT:
  764. case QUALIFIER_STAR:
  765. case QUALIFIER_ALTERNATIVE:
  766. case QUALIFIER_SHORT:
  767. case QUALIFIER_LONG:
  768. case QUALIFIER_LONG_UPPER:
  769. case QUALIFIER_CIRCUMFLEX:
  770. #if defined(QUALIFIER_SIZE_T)
  771. case QUALIFIER_SIZE_T:
  772. #endif
  773. #if defined(QUALIFIER_PTRDIFF_T)
  774. case QUALIFIER_PTRDIFF_T:
  775. #endif
  776. #if defined(QUALIFIER_INTMAX_T)
  777. case QUALIFIER_INTMAX_T:
  778. #endif
  779. #if defined(QUALIFIER_QUAD)
  780. case QUALIFIER_QUAD:
  781. #endif
  782. #if defined(QUALIFIER_SIZE_T_UPPER)
  783. case QUALIFIER_SIZE_T_UPPER:
  784. #endif
  785. #if defined(QUALIFIER_WIDECHAR)
  786. case QUALIFIER_WIDECHAR:
  787. #endif
  788. #if defined(QUALIFIER_QUOTE)
  789. case QUALIFIER_QUOTE:
  790. #endif
  791. #if defined(QUALIFIER_STICKY)
  792. case QUALIFIER_STICKY:
  793. #endif
  794. #if defined(QUALIFIER_VARSIZE)
  795. case QUALIFIER_VARSIZE:
  796. #endif
  797. #if defined(QUALIFIER_PARAM)
  798. case QUALIFIER_PARAM:
  799. #endif
  800. #if defined(QUALIFIER_FIXED_SIZE)
  801. case QUALIFIER_FIXED_SIZE:
  802. #endif
  803. #if defined(QUALIFIER_ROUNDING_UPPER)
  804. case QUALIFIER_ROUNDING_UPPER:
  805. #endif
  806. return TRUE;
  807. default:
  808. return FALSE;
  809. }
  810. }
  811. /*************************************************************************
  812. * TrioSetLocale
  813. */
  814. #if defined(USE_LOCALE)
  815. TRIO_PRIVATE void
  816. TrioSetLocale(TRIO_NOARGS)
  817. {
  818. internalLocaleValues = (struct lconv *)localeconv();
  819. if (internalLocaleValues)
  820. {
  821. if ((internalLocaleValues->decimal_point) &&
  822. (internalLocaleValues->decimal_point[0] != NIL))
  823. {
  824. internalDecimalPointLength = trio_length(internalLocaleValues->decimal_point);
  825. if (internalDecimalPointLength == 1)
  826. {
  827. internalDecimalPoint = internalLocaleValues->decimal_point[0];
  828. }
  829. else
  830. {
  831. internalDecimalPoint = NIL;
  832. trio_copy_max(internalDecimalPointString,
  833. sizeof(internalDecimalPointString),
  834. internalLocaleValues->decimal_point);
  835. }
  836. }
  837. if ((internalLocaleValues->thousands_sep) &&
  838. (internalLocaleValues->thousands_sep[0] != NIL))
  839. {
  840. trio_copy_max(internalThousandSeparator,
  841. sizeof(internalThousandSeparator),
  842. internalLocaleValues->thousands_sep);
  843. internalThousandSeparatorLength = trio_length(internalThousandSeparator);
  844. }
  845. if ((internalLocaleValues->grouping) &&
  846. (internalLocaleValues->grouping[0] != NIL))
  847. {
  848. trio_copy_max(internalGrouping,
  849. sizeof(internalGrouping),
  850. internalLocaleValues->grouping);
  851. }
  852. }
  853. }
  854. #endif /* defined(USE_LOCALE) */
  855. TRIO_PRIVATE int
  856. TrioCalcThousandSeparatorLength
  857. TRIO_ARGS1((digits),
  858. int digits)
  859. {
  860. #if TRIO_EXTENSION
  861. int count = 0;
  862. int step = NO_GROUPING;
  863. char *groupingPointer = internalGrouping;
  864. while (digits > 0)
  865. {
  866. if (*groupingPointer == CHAR_MAX)
  867. {
  868. /* Disable grouping */
  869. break; /* while */
  870. }
  871. else if (*groupingPointer == 0)
  872. {
  873. /* Repeat last group */
  874. if (step == NO_GROUPING)
  875. {
  876. /* Error in locale */
  877. break; /* while */
  878. }
  879. }
  880. else
  881. {
  882. step = *groupingPointer++;
  883. }
  884. if (digits > step)
  885. count += internalThousandSeparatorLength;
  886. digits -= step;
  887. }
  888. return count;
  889. #else
  890. return 0;
  891. #endif
  892. }
  893. TRIO_PRIVATE BOOLEAN_T
  894. TrioFollowedBySeparator
  895. TRIO_ARGS1((position),
  896. int position)
  897. {
  898. #if TRIO_EXTENSION
  899. int step = 0;
  900. char *groupingPointer = internalGrouping;
  901. position--;
  902. if (position == 0)
  903. return FALSE;
  904. while (position > 0)
  905. {
  906. if (*groupingPointer == CHAR_MAX)
  907. {
  908. /* Disable grouping */
  909. break; /* while */
  910. }
  911. else if (*groupingPointer != 0)
  912. {
  913. step = *groupingPointer++;
  914. }
  915. if (step == 0)
  916. break;
  917. position -= step;
  918. }
  919. return (position == 0);
  920. #else
  921. return FALSE;
  922. #endif
  923. }
  924. /*************************************************************************
  925. * TrioGetPosition
  926. *
  927. * Get the %n$ position.
  928. */
  929. TRIO_PRIVATE int
  930. TrioGetPosition
  931. TRIO_ARGS2((format, indexPointer),
  932. TRIO_CONST char *format,
  933. int *indexPointer)
  934. {
  935. #if TRIO_UNIX98
  936. char *tmpformat;
  937. int number = 0;
  938. int index = *indexPointer;
  939. number = (int)trio_to_long(&format[index], &tmpformat, BASE_DECIMAL);
  940. index = (int)(tmpformat - format);
  941. if ((number != 0) && (QUALIFIER_POSITION == format[index++]))
  942. {
  943. *indexPointer = index;
  944. /*
  945. * number is decreased by 1, because n$ starts from 1, whereas
  946. * the array it is indexing starts from 0.
  947. */
  948. return number - 1;
  949. }
  950. #endif
  951. return NO_POSITION;
  952. }
  953. #if TRIO_EXTENSION
  954. /*************************************************************************
  955. * TrioFindNamespace
  956. *
  957. * Find registered user-defined specifier.
  958. * The prev argument is used for optimization only.
  959. */
  960. TRIO_PRIVATE trio_userdef_t *
  961. TrioFindNamespace
  962. TRIO_ARGS2((name, prev),
  963. TRIO_CONST char *name,
  964. trio_userdef_t **prev)
  965. {
  966. trio_userdef_t *def;
  967. if (internalEnterCriticalRegion)
  968. (void)internalEnterCriticalRegion(NULL);
  969. for (def = internalUserDef; def; def = def->next)
  970. {
  971. /* Case-sensitive string comparison */
  972. if (trio_equal_case(def->name, name))
  973. break;
  974. if (prev)
  975. *prev = def;
  976. }
  977. if (internalLeaveCriticalRegion)
  978. (void)internalLeaveCriticalRegion(NULL);
  979. return def;
  980. }
  981. #endif
  982. /*************************************************************************
  983. * TrioPower
  984. *
  985. * Description:
  986. * Calculate pow(base, exponent), where number and exponent are integers.
  987. */
  988. TRIO_PRIVATE trio_long_double_t
  989. TrioPower
  990. TRIO_ARGS2((number, exponent),
  991. int number,
  992. int exponent)
  993. {
  994. trio_long_double_t result;
  995. if (number == 10)
  996. {
  997. switch (exponent)
  998. {
  999. /* Speed up calculation of common cases */
  1000. case 0:
  1001. result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E-1);
  1002. break;
  1003. case 1:
  1004. result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+0);
  1005. break;
  1006. case 2:
  1007. result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+1);
  1008. break;
  1009. case 3:
  1010. result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+2);
  1011. break;
  1012. case 4:
  1013. result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+3);
  1014. break;
  1015. case 5:
  1016. result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+4);
  1017. break;
  1018. case 6:
  1019. result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+5);
  1020. break;
  1021. case 7:
  1022. result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+6);
  1023. break;
  1024. case 8:
  1025. result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+7);
  1026. break;
  1027. case 9:
  1028. result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+8);
  1029. break;
  1030. default:
  1031. result = powl((trio_long_double_t)number,
  1032. (trio_long_double_t)exponent);
  1033. break;
  1034. }
  1035. }
  1036. else
  1037. {
  1038. return powl((trio_long_double_t)number, (trio_long_double_t)exponent);
  1039. }
  1040. return result;
  1041. }
  1042. /*************************************************************************
  1043. * TrioLogarithm
  1044. */
  1045. TRIO_PRIVATE double
  1046. TrioLogarithm
  1047. TRIO_ARGS2((number, base),
  1048. double number,
  1049. int base)
  1050. {
  1051. double result;
  1052. if (number <= 0.0)
  1053. {
  1054. /* xlC crashes on log(0) */
  1055. result = (number == 0.0) ? trio_ninf() : trio_nan();
  1056. }
  1057. else
  1058. {
  1059. if (base == 10)
  1060. {
  1061. result = log10(number);
  1062. }
  1063. else
  1064. {
  1065. result = log10(number) / log10((double)base);
  1066. }
  1067. }
  1068. return result;
  1069. }
  1070. /*************************************************************************
  1071. * TrioLogarithmBase
  1072. */
  1073. TRIO_PRIVATE double
  1074. TrioLogarithmBase
  1075. TRIO_ARGS1((base),
  1076. int base)
  1077. {
  1078. switch (base)
  1079. {
  1080. case BASE_BINARY : return 1.0;
  1081. case BASE_OCTAL : return 3.0;
  1082. case BASE_DECIMAL: return 3.321928094887362345;
  1083. case BASE_HEX : return 4.0;
  1084. default : return TrioLogarithm((double)base, 2);
  1085. }
  1086. }
  1087. /*************************************************************************
  1088. * TrioParse
  1089. *
  1090. * Description:
  1091. * Parse the format string
  1092. */
  1093. TRIO_PRIVATE int
  1094. TrioParse
  1095. TRIO_ARGS5((type, format, parameters, arglist, argarray),
  1096. int type,
  1097. TRIO_CONST char *format,
  1098. trio_parameter_t *parameters,
  1099. TRIO_VA_LIST_PTR arglist,
  1100. trio_pointer_t *argarray)
  1101. {
  1102. /* Count the number of times a parameter is referenced */
  1103. unsigned short usedEntries[MAX_PARAMETERS];
  1104. /* Parameter counters */
  1105. int parameterPosition;
  1106. int currentParam;
  1107. int maxParam = -1;
  1108. /* Utility variables */
  1109. trio_flags_t flags;
  1110. int width;
  1111. int precision;
  1112. int varsize;
  1113. int base;
  1114. int index; /* Index into formatting string */
  1115. int dots; /* Count number of dots in modifier part */
  1116. BOOLEAN_T positional; /* Does the specifier have a positional? */
  1117. BOOLEAN_T gotSticky = FALSE; /* Are there any sticky modifiers at all? */
  1118. /*
  1119. * indices specifies the order in which the parameters must be
  1120. * read from the va_args (this is necessary to handle positionals)
  1121. */
  1122. int indices[MAX_PARAMETERS];
  1123. int pos = 0;
  1124. /* Various variables */
  1125. char ch;
  1126. #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
  1127. int charlen;
  1128. #endif
  1129. int save_errno;
  1130. int i = -1;
  1131. int num;
  1132. char *tmpformat;
  1133. /* One and only one of arglist and argarray must be used */
  1134. assert((arglist != NULL) ^ (argarray != NULL));
  1135. /*
  1136. * The 'parameters' array is not initialized, but we need to
  1137. * know which entries we have used.
  1138. */
  1139. memset(usedEntries, 0, sizeof(usedEntries));
  1140. save_errno = errno;
  1141. index = 0;
  1142. parameterPosition = 0;
  1143. #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
  1144. (void)mblen(NULL, 0);
  1145. #endif
  1146. while (format[index])
  1147. {
  1148. #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
  1149. if (! isascii(format[index]))
  1150. {
  1151. /*
  1152. * Multibyte characters cannot be legal specifiers or
  1153. * modifiers, so we skip over them.
  1154. */
  1155. charlen = mblen(&format[index], MB_LEN_MAX);
  1156. index += (charlen > 0) ? charlen : 1;
  1157. continue; /* while */
  1158. }
  1159. #endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
  1160. if (CHAR_IDENTIFIER == format[index++])
  1161. {
  1162. if (CHAR_IDENTIFIER == format[index])
  1163. {
  1164. index++;
  1165. continue; /* while */
  1166. }
  1167. flags = FLAGS_NEW;
  1168. dots = 0;
  1169. currentParam = TrioGetPosition(format, &index);
  1170. positional = (NO_POSITION != currentParam);
  1171. if (!positional)
  1172. {
  1173. /* We have no positional, get the next counter */
  1174. currentParam = parameterPosition;
  1175. }
  1176. if(currentParam >= MAX_PARAMETERS)
  1177. {
  1178. /* Bail out completely to make the error more obvious */
  1179. return TRIO_ERROR_RETURN(TRIO_ETOOMANY, index);
  1180. }
  1181. if (currentParam > maxParam)
  1182. maxParam = currentParam;
  1183. /* Default values */
  1184. width = NO_WIDTH;
  1185. precision = NO_PRECISION;
  1186. base = NO_BASE;
  1187. varsize = NO_SIZE;
  1188. while (TrioIsQualifier(format[index]))
  1189. {
  1190. ch = format[index++];
  1191. switch (ch)
  1192. {
  1193. case QUALIFIER_SPACE:
  1194. flags |= FLAGS_SPACE;
  1195. break;
  1196. case QUALIFIER_PLUS:
  1197. flags |= FLAGS_SHOWSIGN;
  1198. break;
  1199. case QUALIFIER_MINUS:
  1200. flags |= FLAGS_LEFTADJUST;
  1201. flags &= ~FLAGS_NILPADDING;
  1202. break;
  1203. case QUALIFIER_ALTERNATIVE:
  1204. flags |= FLAGS_ALTERNATIVE;
  1205. break;
  1206. case QUALIFIER_DOT:
  1207. if (dots == 0) /* Precision */
  1208. {
  1209. dots++;
  1210. /* Skip if no precision */
  1211. if (QUALIFIER_DOT == format[index])
  1212. break;
  1213. /* After the first dot we have the precision */
  1214. flags |= FLAGS_PRECISION;
  1215. if ((QUALIFIER_STAR == format[index])
  1216. #if defined(QUALIFIER_PARAM)
  1217. || (QUALIFIER_PARAM == format[index])
  1218. #endif
  1219. )
  1220. {
  1221. index++;
  1222. flags |= FLAGS_PRECISION_PARAMETER;
  1223. precision = TrioGetPosition(format, &index);
  1224. if (precision == NO_POSITION)
  1225. {
  1226. parameterPosition++;
  1227. if (positional)
  1228. precision = parameterPosition;
  1229. else
  1230. {
  1231. precision = currentParam;
  1232. currentParam = precision + 1;
  1233. }
  1234. }
  1235. else
  1236. {
  1237. if (! positional)
  1238. currentParam = precision + 1;
  1239. if (width > maxParam)
  1240. maxParam = precision;
  1241. }
  1242. if (currentParam > maxParam)
  1243. maxParam = currentParam;
  1244. }
  1245. else
  1246. {
  1247. precision = trio_to_long(&format[index],
  1248. &tmpformat,
  1249. BASE_DECIMAL);
  1250. index = (int)(tmpformat - format);
  1251. }
  1252. }
  1253. else if (dots == 1) /* Base */
  1254. {
  1255. dots++;
  1256. /* After the second dot we have the base */
  1257. flags |= FLAGS_BASE;
  1258. if ((QUALIFIER_STAR == format[index])
  1259. #if defined(QUALIFIER_PARAM)
  1260. || (QUALIFIER_PARAM == format[index])
  1261. #endif
  1262. )
  1263. {
  1264. index++;
  1265. flags |= FLAGS_BASE_PARAMETER;
  1266. base = TrioGetPosition(format, &index);
  1267. if (base == NO_POSITION)
  1268. {
  1269. parameterPosition++;
  1270. if (positional)
  1271. base = parameterPosition;
  1272. else
  1273. {
  1274. base = currentParam;
  1275. currentParam = base + 1;
  1276. }
  1277. }
  1278. else
  1279. {
  1280. if (! positional)
  1281. currentParam = base + 1;
  1282. if (base > maxParam)
  1283. maxParam = base;
  1284. }
  1285. if (currentParam > maxParam)
  1286. maxParam = currentParam;
  1287. }
  1288. else
  1289. {
  1290. base = trio_to_long(&format[index],
  1291. &tmpformat,
  1292. BASE_DECIMAL);
  1293. if (base > MAX_BASE)
  1294. return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
  1295. index = (int)(tmpformat - format);
  1296. }
  1297. }
  1298. else
  1299. {
  1300. return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
  1301. }
  1302. break; /* QUALIFIER_DOT */
  1303. #if defined(QUALIFIER_PARAM)
  1304. case QUALIFIER_PARAM:
  1305. type = TYPE_PRINT;
  1306. /* FALLTHROUGH */
  1307. #endif
  1308. case QUALIFIER_STAR:
  1309. /* This has different meanings for print and scan */
  1310. if (TYPE_PRINT == type)
  1311. {
  1312. /* Read with from parameter */
  1313. flags |= (FLAGS_WIDTH | FLAGS_WIDTH_PARAMETER);
  1314. width = TrioGetPosition(format, &index);
  1315. if (width == NO_POSITION)
  1316. {
  1317. parameterPosition++;
  1318. if (positional)
  1319. width = parameterPosition;
  1320. else
  1321. {
  1322. width = currentParam;
  1323. currentParam = width + 1;
  1324. }
  1325. }
  1326. else
  1327. {
  1328. if (! positional)
  1329. currentParam = width + 1;
  1330. if (width > maxParam)
  1331. maxParam = width;
  1332. }
  1333. if (currentParam > maxParam)
  1334. maxParam = currentParam;
  1335. }
  1336. else
  1337. {
  1338. /* Scan, but do not store result */
  1339. flags |= FLAGS_IGNORE;
  1340. }
  1341. break; /* QUALIFIER_STAR */
  1342. case '0':
  1343. if (! (flags & FLAGS_LEFTADJUST))
  1344. flags |= FLAGS_NILPADDING;
  1345. /* FALLTHROUGH */
  1346. case '1': case '2': case '3': case '4':
  1347. case '5': case '6': case '7': case '8': case '9':
  1348. flags |= FLAGS_WIDTH;
  1349. /* &format[index - 1] is used to "rewind" the read
  1350. * character from format
  1351. */
  1352. width = trio_to_long(&format[index - 1],
  1353. &tmpformat,
  1354. BASE_DECIMAL);
  1355. index = (int)(tmpformat - format);
  1356. break;
  1357. case QUALIFIER_SHORT:
  1358. if (flags & FLAGS_SHORTSHORT)
  1359. return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
  1360. else if (flags & FLAGS_SHORT)
  1361. flags |= FLAGS_SHORTSHORT;
  1362. else
  1363. flags |= FLAGS_SHORT;
  1364. break;
  1365. case QUALIFIER_LONG:
  1366. if (flags & FLAGS_QUAD)
  1367. return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
  1368. else if (flags & FLAGS_LONG)
  1369. flags |= FLAGS_QUAD;
  1370. else
  1371. flags |= FLAGS_LONG;
  1372. break;
  1373. case QUALIFIER_LONG_UPPER:
  1374. flags |= FLAGS_LONGDOUBLE;
  1375. break;
  1376. #if defined(QUALIFIER_SIZE_T)
  1377. case QUALIFIER_SIZE_T:
  1378. flags |= FLAGS_SIZE_T;
  1379. /* Modify flags for later truncation of number */
  1380. if (sizeof(size_t) == sizeof(trio_ulonglong_t))
  1381. flags |= FLAGS_QUAD;
  1382. else if (sizeof(size_t) == sizeof(long))
  1383. flags |= FLAGS_LONG;
  1384. break;
  1385. #endif
  1386. #if defined(QUALIFIER_PTRDIFF_T)
  1387. case QUALIFIER_PTRDIFF_T:
  1388. flags |= FLAGS_PTRDIFF_T;
  1389. if (sizeof(ptrdiff_t) == sizeof(trio_ulonglong_t))
  1390. flags |= FLAGS_QUAD;
  1391. else if (sizeof(ptrdiff_t) == sizeof(long))
  1392. flags |= FLAGS_LONG;
  1393. break;
  1394. #endif
  1395. #if defined(QUALIFIER_INTMAX_T)
  1396. case QUALIFIER_INTMAX_T:
  1397. flags |= FLAGS_INTMAX_T;
  1398. if (sizeof(trio_intmax_t) == sizeof(trio_ulonglong_t))
  1399. flags |= FLAGS_QUAD;
  1400. else if (sizeof(trio_intmax_t) == sizeof(long))
  1401. flags |= FLAGS_LONG;
  1402. break;
  1403. #endif
  1404. #if defined(QUALIFIER_QUAD)
  1405. case QUALIFIER_QUAD:
  1406. flags |= FLAGS_QUAD;
  1407. break;
  1408. #endif
  1409. #if defined(QUALIFIER_FIXED_SIZE)
  1410. case QUALIFIER_FIXED_SIZE:
  1411. if (flags & FLAGS_FIXED_SIZE)
  1412. return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
  1413. if (flags & (FLAGS_ALL_SIZES | FLAGS_LONGDOUBLE |
  1414. FLAGS_WIDECHAR | FLAGS_VARSIZE_PARAMETER))
  1415. return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
  1416. if ((format[index] == '6') &&
  1417. (format[index + 1] == '4'))
  1418. {
  1419. varsize = sizeof(trio_int64_t);
  1420. index += 2;
  1421. }
  1422. else if ((format[index] == '3') &&
  1423. (format[index + 1] == '2'))
  1424. {
  1425. varsize = sizeof(trio_int32_t);
  1426. index += 2;
  1427. }
  1428. else if ((format[index] == '1') &&
  1429. (format[index + 1] == '6'))
  1430. {
  1431. varsize = sizeof(trio_int16_t);
  1432. index += 2;
  1433. }
  1434. else if (format[index] == '8')
  1435. {
  1436. varsize = sizeof(trio_int8_t);
  1437. index++;
  1438. }
  1439. else
  1440. return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
  1441. flags |= FLAGS_FIXED_SIZE;
  1442. break;
  1443. #endif
  1444. #if defined(QUALIFIER_WIDECHAR)
  1445. case QUALIFIER_WIDECHAR:
  1446. flags |= FLAGS_WIDECHAR;
  1447. break;
  1448. #endif
  1449. #if defined(QUALIFIER_SIZE_T_UPPER)
  1450. case QUALIFIER_SIZE_T_UPPER:
  1451. break;
  1452. #endif
  1453. #if defined(QUALIFIER_QUOTE)
  1454. case QUALIFIER_QUOTE:
  1455. flags |= FLAGS_QUOTE;
  1456. break;
  1457. #endif
  1458. #if defined(QUALIFIER_STICKY)
  1459. case QUALIFIER_STICKY:
  1460. flags |= FLAGS_STICKY;
  1461. gotSticky = TRUE;
  1462. break;
  1463. #endif
  1464. #if defined(QUALIFIER_VARSIZE)
  1465. case QUALIFIER_VARSIZE:
  1466. flags |= FLAGS_VARSIZE_PARAMETER;
  1467. parameterPosition++;
  1468. if (positional)
  1469. varsize = parameterPosition;
  1470. else
  1471. {
  1472. varsize = currentParam;
  1473. currentParam = varsize + 1;
  1474. }
  1475. if (currentParam > maxParam)
  1476. maxParam = currentParam;
  1477. break;
  1478. #endif
  1479. #if defined(QUALIFIER_ROUNDING_UPPER)
  1480. case QUALIFIER_ROUNDING_UPPER:
  1481. flags |= FLAGS_ROUNDING;
  1482. break;
  1483. #endif
  1484. default:
  1485. /* Bail out completely to make the error more obvious */
  1486. return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
  1487. }
  1488. } /* while qualifier */
  1489. /*
  1490. * Parameters only need the type and value. The value is
  1491. * read later.
  1492. */
  1493. if (flags & FLAGS_WIDTH_PARAMETER)
  1494. {
  1495. usedEntries[width] += 1;
  1496. parameters[pos].type = FORMAT_PARAMETER;
  1497. parameters[pos].flags = 0;
  1498. indices[width] = pos;
  1499. width = pos++;
  1500. }
  1501. if (flags & FLAGS_PRECISION_PARAMETER)
  1502. {
  1503. usedEntries[precision] += 1;
  1504. parameters[pos].type = FORMAT_PARAMETER;
  1505. parameters[pos].flags = 0;
  1506. indices[precision] = pos;
  1507. precision = pos++;
  1508. }
  1509. if (flags & FLAGS_BASE_PARAMETER)
  1510. {
  1511. usedEntries[base] += 1;
  1512. parameters[pos].type = FORMAT_PARAMETER;
  1513. parameters[pos].flags = 0;
  1514. indices[base] = pos;
  1515. base = pos++;
  1516. }
  1517. if (flags & FLAGS_VARSIZE_PARAMETER)
  1518. {
  1519. usedEntries[varsize] += 1;
  1520. parameters[pos].type = FORMAT_PARAMETER;
  1521. parameters[pos].flags = 0;
  1522. indices[varsize] = pos;
  1523. varsize = pos++;
  1524. }
  1525. indices[currentParam] = pos;
  1526. switch (format[index++])
  1527. {
  1528. #if defined(SPECIFIER_CHAR_UPPER)
  1529. case SPECIFIER_CHAR_UPPER:
  1530. flags |= FLAGS_WIDECHAR;
  1531. /* FALLTHROUGH */
  1532. #endif
  1533. case SPECIFIER_CHAR:
  1534. if (flags & FLAGS_LONG)
  1535. flags |= FLAGS_WIDECHAR;
  1536. else if (flags & FLAGS_SHORT)
  1537. flags &= ~FLAGS_WIDECHAR;
  1538. parameters[pos].type = FORMAT_CHAR;
  1539. break;
  1540. #if defined(SPECIFIER_STRING_UPPER)
  1541. case SPECIFIER_STRING_UPPER:
  1542. flags |= FLAGS_WIDECHAR;
  1543. /* FALLTHROUGH */
  1544. #endif
  1545. case SPECIFIER_STRING:
  1546. if (flags & FLAGS_LONG)
  1547. flags |= FLAGS_WIDECHAR;
  1548. else if (flags & FLAGS_SHORT)
  1549. flags &= ~FLAGS_WIDECHAR;
  1550. parameters[pos].type = FORMAT_STRING;
  1551. break;
  1552. case SPECIFIER_GROUP:
  1553. if (TYPE_SCAN == type)
  1554. {
  1555. int depth = 1;
  1556. parameters[pos].type = FORMAT_GROUP;
  1557. if (format[index] == QUALIFIER_CIRCUMFLEX)
  1558. index++;
  1559. if (format[index] == SPECIFIER_UNGROUP)
  1560. index++;
  1561. if (format[index] == QUALIFIER_MINUS)
  1562. index++;
  1563. /* Skip nested brackets */
  1564. while (format[index] != NIL)
  1565. {
  1566. if (format[index] == SPECIFIER_GROUP)
  1567. {
  1568. depth++;
  1569. }
  1570. else if (format[index] == SPECIFIER_UNGROUP)
  1571. {
  1572. if (--depth <= 0)
  1573. {
  1574. index++;
  1575. break;
  1576. }
  1577. }
  1578. index++;
  1579. }
  1580. }
  1581. break;
  1582. case SPECIFIER_INTEGER:
  1583. parameters[pos].type = FORMAT_INT;
  1584. break;
  1585. case SPECIFIER_UNSIGNED:
  1586. flags |= FLAGS_UNSIGNED;
  1587. parameters[pos].type = FORMAT_INT;
  1588. break;
  1589. case SPECIFIER_DECIMAL:
  1590. /* Disable base modifier */
  1591. flags &= ~FLAGS_BASE_PARAMETER;
  1592. base = BASE_DECIMAL;
  1593. parameters[pos].type = FORMAT_INT;
  1594. break;
  1595. case SPECIFIER_OCTAL:
  1596. flags |= FLAGS_UNSIGNED;
  1597. flags &= ~FLAGS_BASE_PARAMETER;
  1598. base = BASE_OCTAL;
  1599. parameters[pos].type = FORMAT_INT;
  1600. break;
  1601. #if defined(SPECIFIER_BINARY)
  1602. case SPECIFIER_BINARY_UPPER:
  1603. flags |= FLAGS_UPPER;
  1604. /* FALLTHROUGH */
  1605. case SPECIFIER_BINARY:
  1606. flags |= FLAGS_NILPADDING;
  1607. flags &= ~FLAGS_BASE_PARAMETER;
  1608. base = BASE_BINARY;
  1609. parameters[pos].type = FORMAT_INT;
  1610. break;
  1611. #endif
  1612. case SPECIFIER_HEX_UPPER:
  1613. flags |= FLAGS_UPPER;
  1614. /* FALLTHROUGH */
  1615. case SPECIFIER_HEX:
  1616. flags |= FLAGS_UNSIGNED;
  1617. flags &= ~FLAGS_BASE_PARAMETER;
  1618. base = BASE_HEX;
  1619. parameters[pos].type = FORMAT_INT;
  1620. break;
  1621. case SPECIFIER_FLOAT_E_UPPER:
  1622. flags |= FLAGS_UPPER;
  1623. /* FALLTHROUGH */
  1624. case SPECIFIER_FLOAT_E:
  1625. flags |= FLAGS_FLOAT_E;
  1626. parameters[pos].type = FORMAT_DOUBLE;
  1627. break;
  1628. case SPECIFIER_FLOAT_G_UPPER:
  1629. flags |= FLAGS_UPPER;
  1630. /* FALLTHROUGH */
  1631. case SPECIFIER_FLOAT_G:
  1632. flags |= FLAGS_FLOAT_G;
  1633. parameters[pos].type = FORMAT_DOUBLE;
  1634. break;
  1635. case SPECIFIER_FLOAT_F_UPPER:
  1636. flags |= FLAGS_UPPER;
  1637. /* FALLTHROUGH */
  1638. case SPECIFIER_FLOAT_F:
  1639. parameters[pos].type = FORMAT_DOUBLE;
  1640. break;
  1641. case SPECIFIER_POINTER:
  1642. if (sizeof(trio_pointer_t) == sizeof(trio_ulonglong_t))
  1643. flags |= FLAGS_QUAD;
  1644. else if (sizeof(trio_pointer_t) == sizeof(long))
  1645. flags |= FLAGS_LONG;
  1646. parameters[pos].type = FORMAT_POINTER;
  1647. break;
  1648. case SPECIFIER_COUNT:
  1649. parameters[pos].type = FORMAT_COUNT;
  1650. break;
  1651. #if defined(SPECIFIER_HEXFLOAT)
  1652. # if defined(SPECIFIER_HEXFLOAT_UPPER)
  1653. case SPECIFIER_HEXFLOAT_UPPER:
  1654. flags |= FLAGS_UPPER;
  1655. /* FALLTHROUGH */
  1656. # endif
  1657. case SPECIFIER_HEXFLOAT:
  1658. base = BASE_HEX;
  1659. parameters[pos].type = FORMAT_DOUBLE;
  1660. break;
  1661. #endif
  1662. #if defined(FORMAT_ERRNO)
  1663. case SPECIFIER_ERRNO:
  1664. parameters[pos].type = FORMAT_ERRNO;
  1665. break;
  1666. #endif
  1667. #if defined(SPECIFIER_USER_DEFINED_BEGIN)
  1668. case SPECIFIER_USER_DEFINED_BEGIN:
  1669. {
  1670. unsigned int max;
  1671. int without_namespace = TRUE;
  1672. parameters[pos].type = FORMAT_USER_DEFINED;
  1673. parameters[pos].user_name[0] = NIL;
  1674. tmpformat = (char *)&format[index];
  1675. while ((ch = format[index]))
  1676. {
  1677. index++;
  1678. if (ch == SPECIFIER_USER_DEFINED_END)
  1679. {
  1680. if (without_namespace)
  1681. {
  1682. /* We must get the handle first */
  1683. parameters[pos].type = FORMAT_PARAMETER;
  1684. parameters[pos].indexAfterSpecifier = index;
  1685. parameters[pos].flags = FLAGS_USER_DEFINED;
  1686. /* Adjust parameters for insertion of new one */
  1687. pos++;
  1688. usedEntries[currentParam] += 1;
  1689. parameters[pos].type = FORMAT_USER_DEFINED;
  1690. currentParam++;
  1691. indices[currentParam] = pos;
  1692. if (currentParam > maxParam)
  1693. maxParam = currentParam;
  1694. }
  1695. /* Copy the user data */
  1696. max = (unsigned int)(&format[index] - tmpformat);
  1697. if (max > MAX_USER_DATA)
  1698. max = MAX_USER_DATA;
  1699. trio_copy_max(parameters[pos].user_data,
  1700. max,
  1701. tmpformat);
  1702. break; /* while */
  1703. }
  1704. if (ch == SPECIFIER_USER_DEFINED_SEPARATOR)
  1705. {
  1706. without_namespace = FALSE;
  1707. /* Copy the namespace for later looking-up */
  1708. max = (int)(&format[index] - tmpformat);
  1709. if (max > MAX_USER_NAME)
  1710. max = MAX_USER_NAME;
  1711. trio_copy_max(parameters[pos].user_name,
  1712. max,
  1713. tmpformat);
  1714. tmpformat = (char *)&format[index];
  1715. }
  1716. }
  1717. if (ch != SPECIFIER_USER_DEFINED_END)
  1718. return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
  1719. }
  1720. break;
  1721. #endif /* defined(SPECIFIER_USER_DEFINED_BEGIN) */
  1722. default:
  1723. /* Bail out completely to make the error more obvious */
  1724. return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
  1725. }
  1726. /* Count the number of times this entry has been used */
  1727. usedEntries[currentParam] += 1;
  1728. /* Find last sticky parameters */
  1729. if (gotSticky && !(flags & FLAGS_STICKY))
  1730. {
  1731. for (i = pos - 1; i >= 0; i--)
  1732. {
  1733. if (parameters[i].type == FORMAT_PARAMETER)
  1734. continue;
  1735. if ((parameters[i].flags & FLAGS_STICKY) &&
  1736. (parameters[i].type == parameters[pos].type))
  1737. {
  1738. /* Do not overwrite current qualifiers */
  1739. flags |= (parameters[i].flags & (unsigned long)~FLAGS_STICKY);
  1740. if (width == NO_WIDTH)
  1741. width = parameters[i].width;
  1742. if (precision == NO_PRECISION)
  1743. precision = parameters[i].precision;
  1744. if (base == NO_BASE)
  1745. base = parameters[i].base;
  1746. break;
  1747. }
  1748. }
  1749. }
  1750. parameters[pos].indexAfterSpecifier = index;
  1751. parameters[pos].flags = flags;
  1752. parameters[pos].width = width;
  1753. parameters[pos].precision = precision;
  1754. parameters[pos].base = (base == NO_BASE) ? BASE_DECIMAL : base;
  1755. parameters[pos].varsize = varsize;
  1756. pos++;
  1757. if (! positional)
  1758. parameterPosition++;
  1759. } /* if identifier */
  1760. } /* while format characters left */
  1761. for (num = 0; num <= maxParam; num++)
  1762. {
  1763. if (usedEntries[num] != 1)
  1764. {
  1765. if (usedEntries[num] == 0) /* gap detected */
  1766. return TRIO_ERROR_RETURN(TRIO_EGAP, num);
  1767. else /* double references detected */
  1768. return TRIO_ERROR_RETURN(TRIO_EDBLREF, num);
  1769. }
  1770. i = indices[num];
  1771. /*
  1772. * FORMAT_PARAMETERS are only present if they must be read,
  1773. * so it makes no sense to check the ignore flag (besides,
  1774. * the flags variable is not set for that particular type)
  1775. */
  1776. if ((parameters[i].type != FORMAT_PARAMETER) &&
  1777. (parameters[i].flags & FLAGS_IGNORE))
  1778. continue; /* for all arguments */
  1779. /*
  1780. * The stack arguments are read according to ANSI C89
  1781. * default argument promotions:
  1782. *
  1783. * char = int
  1784. * short = int
  1785. * unsigned char = unsigned int
  1786. * unsigned short = unsigned int
  1787. * float = double
  1788. *
  1789. * In addition to the ANSI C89 these types are read (the
  1790. * default argument promotions of C99 has not been
  1791. * considered yet)
  1792. *
  1793. * long long
  1794. * long double
  1795. * size_t
  1796. * ptrdiff_t
  1797. * intmax_t
  1798. */
  1799. switch (parameters[i].type)
  1800. {
  1801. case FORMAT_GROUP:
  1802. case FORMAT_STRING:
  1803. #if TRIO_WIDECHAR
  1804. if (flags & FLAGS_WIDECHAR)
  1805. {
  1806. parameters[i].data.wstring = (argarray == NULL)
  1807. ? va_arg(TRIO_VA_LIST_DEREF(arglist), trio_wchar_t *)
  1808. : (trio_wchar_t *)(argarray[num]);
  1809. }
  1810. else
  1811. #endif
  1812. {
  1813. parameters[i].data.string = (argarray == NULL)
  1814. ? va_arg(TRIO_VA_LIST_DEREF(arglist), char *)
  1815. : (char *)(argarray[num]);
  1816. }
  1817. break;
  1818. #if defined(FORMAT_USER_DEFINED)
  1819. case FORMAT_USER_DEFINED:
  1820. #endif
  1821. case FORMAT_POINTER:
  1822. case FORMAT_COUNT:
  1823. case FORMAT_UNKNOWN:
  1824. parameters[i].data.pointer = (argarray == NULL)
  1825. ? va_arg(TRIO_VA_LIST_DEREF(arglist), trio_pointer_t )
  1826. : argarray[num];
  1827. break;
  1828. case FORMAT_CHAR:
  1829. case FORMAT_INT:
  1830. if (TYPE_SCAN == type)
  1831. {
  1832. if (argarray == NULL)
  1833. parameters[i].data.pointer =
  1834. (trio_pointer_t)va_arg(TRIO_VA_LIST_DEREF(arglist), trio_pointer_t);
  1835. else
  1836. {
  1837. if (parameters[i].type == FORMAT_CHAR)
  1838. parameters[i].data.pointer =
  1839. (trio_pointer_t)((char *)argarray[num]);
  1840. else if (parameters[i].flags & FLAGS_SHORT)
  1841. parameters[i].data.pointer =
  1842. (trio_pointer_t)((short *)argarray[num]);
  1843. else
  1844. parameters[i].data.pointer =
  1845. (trio_pointer_t)((int *)argarray[num]);
  1846. }
  1847. }
  1848. else
  1849. {
  1850. #if defined(QUALIFIER_VARSIZE) || defined(QUALIFIER_FIXED_SIZE)
  1851. if (parameters[i].flags
  1852. & (FLAGS_VARSIZE_PARAMETER | FLAGS_FIXED_SIZE))
  1853. {
  1854. if (parameters[i].flags & FLAGS_VARSIZE_PARAMETER)
  1855. {
  1856. /*
  1857. * Variable sizes are mapped onto the fixed sizes, in
  1858. * accordance with integer promotion.
  1859. *
  1860. * Please note that this may not be portable, as we
  1861. * only guess the size, not the layout of the numbers.
  1862. * For example, if int is little-endian, and long is
  1863. * big-endian, then this will fail.
  1864. */
  1865. varsize = (int)parameters[parameters[i].varsize].data.number.as_unsigned;
  1866. }
  1867. else
  1868. {
  1869. /* Used for the I<bits> modifiers */
  1870. varsize = parameters[i].varsize;
  1871. }
  1872. parameters[i].flags &= ~FLAGS_ALL_VARSIZES;
  1873. if (varsize <= (int)sizeof(int))
  1874. ;
  1875. else if (varsize <= (int)sizeof(long))
  1876. parameters[i].flags |= FLAGS_LONG;
  1877. #if defined(QUALIFIER_INTMAX_T)
  1878. else if (varsize <= (int)sizeof(trio_longlong_t))
  1879. parameters[i].flags |= FLAGS_QUAD;
  1880. else
  1881. parameters[i].flags |= FLAGS_INTMAX_T;
  1882. #else
  1883. else
  1884. parameters[i].flags |= FLAGS_QUAD;
  1885. #endif
  1886. }
  1887. #endif /* defined(QUALIFIER_VARSIZE) */
  1888. #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
  1889. if (parameters[i].flags & FLAGS_SIZE_T)
  1890. parameters[i].data.number.as_unsigned = (argarray == NULL)
  1891. ? (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), size_t)
  1892. : (trio_uintmax_t)(*((size_t *)argarray[num]));
  1893. else
  1894. #endif
  1895. #if defined(QUALIFIER_PTRDIFF_T)
  1896. if (parameters[i].flags & FLAGS_PTRDIFF_T)
  1897. parameters[i].data.number.as_unsigned = (argarray == NULL)
  1898. ? (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), ptrdiff_t)
  1899. : (trio_uintmax_t)(*((ptrdiff_t *)argarray[num]));
  1900. else
  1901. #endif
  1902. #if defined(QUALIFIER_INTMAX_T)
  1903. if (parameters[i].flags & FLAGS_INTMAX_T)
  1904. parameters[i].data.number.as_unsigned = (argarray == NULL)
  1905. ? (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), trio_intmax_t)
  1906. : (trio_uintmax_t)(*((trio_intmax_t *)argarray[num]));
  1907. else
  1908. #endif
  1909. if (parameters[i].flags & FLAGS_QUAD)
  1910. parameters[i].data.number.as_unsigned = (argarray == NULL)
  1911. ? (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), trio_ulonglong_t)
  1912. : (trio_uintmax_t)(*((trio_ulonglong_t *)argarray[num]));
  1913. else if (parameters[i].flags & FLAGS_LONG)
  1914. parameters[i].data.number.as_unsigned = (argarray == NULL)
  1915. ? (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), long)
  1916. : (trio_uintmax_t)(*((long *)argarray[num]));
  1917. else
  1918. {
  1919. if (argarray == NULL)
  1920. parameters[i].data.number.as_unsigned = (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), int);
  1921. else
  1922. {
  1923. if (parameters[i].type == FORMAT_CHAR)
  1924. parameters[i].data.number.as_unsigned = (trio_uintmax_t)(*((char *)argarray[num]));
  1925. else if (parameters[i].flags & FLAGS_SHORT)
  1926. parameters[i].data.number.as_unsigned = (trio_uintmax_t)(*((short *)argarray[num]));
  1927. else
  1928. parameters[i].data.number.as_unsigned = (trio_uintmax_t)(*((int *)argarray[num]));
  1929. }
  1930. }
  1931. }
  1932. break;
  1933. case FORMAT_PARAMETER:
  1934. /*
  1935. * The parameter for the user-defined specifier is a pointer,
  1936. * whereas the rest (width, precision, base) uses an integer.
  1937. */
  1938. if (parameters[i].flags & FLAGS_USER_DEFINED)
  1939. parameters[i].data.pointer = (argarray == NULL)
  1940. ? va_arg(TRIO_VA_LIST_DEREF(arglist), trio_pointer_t )
  1941. : argarray[num];
  1942. else
  1943. parameters[i].data.number.as_unsigned = (argarray == NULL)
  1944. ? (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), int)
  1945. : (trio_uintmax_t)(*((int *)argarray[num]));
  1946. break;
  1947. case FORMAT_DOUBLE:
  1948. if (TYPE_SCAN == type)
  1949. {
  1950. if (parameters[i].flags & FLAGS_LONGDOUBLE)
  1951. parameters[i].data.longdoublePointer = (argarray == NULL)
  1952. ? va_arg(TRIO_VA_LIST_DEREF(arglist), trio_long_double_t *)
  1953. : (trio_long_double_t *)argarray[num];
  1954. else
  1955. {
  1956. if (parameters[i].flags & FLAGS_LONG)
  1957. parameters[i].data.doublePointer = (argarray == NULL)
  1958. ? va_arg(TRIO_VA_LIST_DEREF(arglist), double *)
  1959. : (double *)argarray[num];
  1960. else
  1961. parameters[i].data.doublePointer = (argarray == NULL)
  1962. ? (double *)va_arg(TRIO_VA_LIST_DEREF(arglist), float *)
  1963. : (double *)((float *)argarray[num]);
  1964. }
  1965. }
  1966. else
  1967. {
  1968. if (parameters[i].flags & FLAGS_LONGDOUBLE)
  1969. parameters[i].data.longdoubleNumber = (argarray == NULL)
  1970. ? va_arg(TRIO_VA_LIST_DEREF(arglist), trio_long_double_t)
  1971. : (trio_long_double_t)(*((trio_long_double_t *)argarray[num]));
  1972. else
  1973. {
  1974. if (argarray == NULL)
  1975. parameters[i].data.longdoubleNumber =
  1976. (trio_long_double_t)va_arg(TRIO_VA_LIST_DEREF(arglist), double);
  1977. else
  1978. {
  1979. if (parameters[i].flags & FLAGS_SHORT)
  1980. parameters[i].data.longdoubleNumber =
  1981. (trio_long_double_t)(*((float *)argarray[num]));
  1982. else
  1983. parameters[i].data.longdoubleNumber =
  1984. (trio_long_double_t)(*((double *)argarray[num]));
  1985. }
  1986. }
  1987. }
  1988. break;
  1989. #if defined(FORMAT_ERRNO)
  1990. case FORMAT_ERRNO:
  1991. parameters[i].data.errorNumber = save_errno;
  1992. break;
  1993. #endif
  1994. default:
  1995. break;
  1996. }
  1997. } /* for all specifiers */
  1998. return num;
  1999. }
  2000. /*************************************************************************
  2001. *
  2002. * FORMATTING
  2003. *
  2004. ************************************************************************/
  2005. /*************************************************************************
  2006. * TrioWriteNumber
  2007. *
  2008. * Description:
  2009. * Output a number.
  2010. * The complexity of this function is a result of the complexity
  2011. * of the dependencies of the flags.
  2012. */
  2013. TRIO_PRIVATE void
  2014. TrioWriteNumber
  2015. TRIO_ARGS6((self, number, flags, width, precision, base),
  2016. trio_class_t *self,
  2017. trio_uintmax_t number,
  2018. trio_flags_t flags,
  2019. int width,
  2020. int precision,
  2021. int base)
  2022. {
  2023. BOOLEAN_T isNegative;
  2024. BOOLEAN_T isNumberZero;
  2025. BOOLEAN_T isPrecisionZero;
  2026. BOOLEAN_T ignoreNumber;
  2027. char buffer[MAX_CHARS_IN(trio_uintmax_t) * (1 + MAX_LOCALE_SEPARATOR_LENGTH) + 1];
  2028. char *bufferend;
  2029. char *pointer;
  2030. TRIO_CONST char *digits;
  2031. int i;
  2032. int length;
  2033. char *p;
  2034. int count;
  2035. assert(VALID(self));
  2036. assert(VALID(self->OutStream));
  2037. assert(((base >= MIN_BASE) && (base <= MAX_BASE)) || (base == NO_BASE));
  2038. digits = (flags & FLAGS_UPPER) ? internalDigitsUpper : internalDigitsLower;
  2039. if (base == NO_BASE)
  2040. base = BASE_DECIMAL;
  2041. isNumberZero = (number == 0);
  2042. isPrecisionZero = (precision == 0);
  2043. ignoreNumber = (isNumberZero
  2044. && isPrecisionZero
  2045. && !((flags & FLAGS_ALTERNATIVE) && (base == BASE_OCTAL)));
  2046. if (flags & FLAGS_UNSIGNED)
  2047. {
  2048. isNegative = FALSE;
  2049. flags &= ~FLAGS_SHOWSIGN;
  2050. }
  2051. else
  2052. {
  2053. isNegative = ((trio_intmax_t)number < 0);
  2054. if (isNegative)
  2055. number = -((trio_intmax_t)number);
  2056. }
  2057. if (flags & FLAGS_QUAD)
  2058. number &= (trio_ulonglong_t)-1;
  2059. else if (flags & FLAGS_LONG)
  2060. number &= (unsigned long)-1;
  2061. else
  2062. number &= (unsigned int)-1;
  2063. /* Build number */
  2064. pointer = bufferend = &buffer[sizeof(buffer) - 1];
  2065. *pointer-- = NIL;
  2066. for (i = 1; i < (int)sizeof(buffer); i++)
  2067. {
  2068. *pointer-- = digits[number % base];
  2069. number /= base;
  2070. if (number == 0)
  2071. break;
  2072. if ((flags & FLAGS_QUOTE) && TrioFollowedBySeparator(i + 1))
  2073. {
  2074. /*
  2075. * We are building the number from the least significant
  2076. * to the most significant digit, so we have to copy the
  2077. * thousand separator backwards
  2078. */
  2079. length = internalThousandSeparatorLength;
  2080. if (((int)(pointer - buffer) - length) > 0)
  2081. {
  2082. p = &internalThousandSeparator[length - 1];
  2083. while (length-- > 0)
  2084. *pointer-- = *p--;
  2085. }
  2086. }
  2087. }
  2088. if (! ignoreNumber)
  2089. {
  2090. /* Adjust width */
  2091. width -= (bufferend - pointer) - 1;
  2092. }
  2093. /* Adjust precision */
  2094. if (NO_PRECISION != precision)
  2095. {
  2096. precision -= (bufferend - pointer) - 1;
  2097. if (precision < 0)
  2098. precision = 0;
  2099. flags |= FLAGS_NILPADDING;
  2100. }
  2101. /* Calculate padding */
  2102. count = (! ((flags & FLAGS_LEFTADJUST) || (precision == NO_PRECISION)))
  2103. ? precision
  2104. : 0;
  2105. /* Adjust width further */
  2106. if (isNegative || (flags & FLAGS_SHOWSIGN) || (flags & FLAGS_SPACE))
  2107. width--;
  2108. if ((flags & FLAGS_ALTERNATIVE) && !isNumberZero)
  2109. {
  2110. switch (base)
  2111. {
  2112. case BASE_BINARY:
  2113. case BASE_HEX:
  2114. width -= 2;
  2115. break;
  2116. case BASE_OCTAL:
  2117. if (!(flags & FLAGS_NILPADDING) || (count == 0))
  2118. width--;
  2119. break;
  2120. default:
  2121. break;
  2122. }
  2123. }
  2124. /* Output prefixes spaces if needed */
  2125. if (! ((flags & FLAGS_LEFTADJUST) ||
  2126. ((flags & FLAGS_NILPADDING) && (precision == NO_PRECISION))))
  2127. {
  2128. while (width-- > count)
  2129. self->OutStream(self, CHAR_ADJUST);
  2130. }
  2131. /* width has been adjusted for signs and alternatives */
  2132. if (isNegative)
  2133. self->OutStream(self, '-');
  2134. else if (flags & FLAGS_SHOWSIGN)
  2135. self->OutStream(self, '+');
  2136. else if (flags & FLAGS_SPACE)
  2137. self->OutStream(self, ' ');
  2138. /* Prefix is not written when the value is zero */
  2139. if ((flags & FLAGS_ALTERNATIVE) && !isNumberZero)
  2140. {
  2141. switch (base)
  2142. {
  2143. case BASE_BINARY:
  2144. self->OutStream(self, '0');
  2145. self->OutStream(self, (flags & FLAGS_UPPER) ? 'B' : 'b');
  2146. break;
  2147. case BASE_OCTAL:
  2148. if (!(flags & FLAGS_NILPADDING) || (count == 0))
  2149. self->OutStream(self, '0');
  2150. break;
  2151. case BASE_HEX:
  2152. self->OutStream(self, '0');
  2153. self->OutStream(self, (flags & FLAGS_UPPER) ? 'X' : 'x');
  2154. break;
  2155. default:
  2156. break;
  2157. } /* switch base */
  2158. }
  2159. /* Output prefixed zero padding if needed */
  2160. if (flags & FLAGS_NILPADDING)
  2161. {
  2162. if (precision == NO_PRECISION)
  2163. precision = width;
  2164. while (precision-- > 0)
  2165. {
  2166. self->OutStream(self, '0');
  2167. width--;
  2168. }
  2169. }
  2170. if (! ignoreNumber)
  2171. {
  2172. /* Output the number itself */
  2173. while (*(++pointer))
  2174. {
  2175. self->OutStream(self, *pointer);
  2176. }
  2177. }
  2178. /* Output trailing spaces if needed */
  2179. if (flags & FLAGS_LEFTADJUST)
  2180. {
  2181. while (width-- > 0)
  2182. self->OutStream(self, CHAR_ADJUST);
  2183. }
  2184. }
  2185. /*************************************************************************
  2186. * TrioWriteStringCharacter
  2187. *
  2188. * Description:
  2189. * Output a single character of a string
  2190. */
  2191. TRIO_PRIVATE void
  2192. TrioWriteStringCharacter
  2193. TRIO_ARGS3((self, ch, flags),
  2194. trio_class_t *self,
  2195. int ch,
  2196. trio_flags_t flags)
  2197. {
  2198. if (flags & FLAGS_ALTERNATIVE)
  2199. {
  2200. if (! isprint(ch))
  2201. {
  2202. /*
  2203. * Non-printable characters are converted to C escapes or
  2204. * \number, if no C escape exists.
  2205. */
  2206. self->OutStream(self, CHAR_BACKSLASH);
  2207. switch (ch)
  2208. {
  2209. case '\007': self->OutStream(self, 'a'); break;
  2210. case '\b': self->OutStream(self, 'b'); break;
  2211. case '\f': self->OutStream(self, 'f'); break;
  2212. case '\n': self->OutStream(self, 'n'); break;
  2213. case '\r': self->OutStream(self, 'r'); break;
  2214. case '\t': self->OutStream(self, 't'); break;
  2215. case '\v': self->OutStream(self, 'v'); break;
  2216. case '\\': self->OutStream(self, '\\'); break;
  2217. default:
  2218. self->OutStream(self, 'x');
  2219. TrioWriteNumber(self, (trio_uintmax_t)ch,
  2220. FLAGS_UNSIGNED | FLAGS_NILPADDING,
  2221. 2, 2, BASE_HEX);
  2222. break;
  2223. }
  2224. }
  2225. else if (ch == CHAR_BACKSLASH)
  2226. {
  2227. self->OutStream(self, CHAR_BACKSLASH);
  2228. self->OutStream(self, CHAR_BACKSLASH);
  2229. }
  2230. else
  2231. {
  2232. self->OutStream(self, ch);
  2233. }
  2234. }
  2235. else
  2236. {
  2237. self->OutStream(self, ch);
  2238. }
  2239. }
  2240. /*************************************************************************
  2241. * TrioWriteString
  2242. *
  2243. * Description:
  2244. * Output a string
  2245. */
  2246. TRIO_PRIVATE void
  2247. TrioWriteString
  2248. TRIO_ARGS5((self, string, flags, width, precision),
  2249. trio_class_t *self,
  2250. TRIO_CONST char *string,
  2251. trio_flags_t flags,
  2252. int width,
  2253. int precision)
  2254. {
  2255. int length;
  2256. int ch;
  2257. assert(VALID(self));
  2258. assert(VALID(self->OutStream));
  2259. if (string == NULL)
  2260. {
  2261. string = internalNullString;
  2262. length = sizeof(internalNullString) - 1;
  2263. /* Disable quoting for the null pointer */
  2264. flags &= (~FLAGS_QUOTE);
  2265. width = 0;
  2266. }
  2267. else
  2268. {
  2269. length = trio_length(string);
  2270. }
  2271. if ((NO_PRECISION != precision) &&
  2272. (precision < length))
  2273. {
  2274. length = precision;
  2275. }
  2276. width -= length;
  2277. if (flags & FLAGS_QUOTE)
  2278. self->OutStream(self, CHAR_QUOTE);
  2279. if (! (flags & FLAGS_LEFTADJUST))
  2280. {
  2281. while (width-- > 0)
  2282. self->OutStream(self, CHAR_ADJUST);
  2283. }
  2284. while (length-- > 0)
  2285. {
  2286. /* The ctype parameters must be an unsigned char (or EOF) */
  2287. ch = (int)((unsigned char)(*string++));
  2288. TrioWriteStringCharacter(self, ch, flags);
  2289. }
  2290. if (flags & FLAGS_LEFTADJUST)
  2291. {
  2292. while (width-- > 0)
  2293. self->OutStream(self, CHAR_ADJUST);
  2294. }
  2295. if (flags & FLAGS_QUOTE)
  2296. self->OutStream(self, CHAR_QUOTE);
  2297. }
  2298. /*************************************************************************
  2299. * TrioWriteWideStringCharacter
  2300. *
  2301. * Description:
  2302. * Output a wide string as a multi-byte sequence
  2303. */
  2304. #if TRIO_WIDECHAR
  2305. TRIO_PRIVATE int
  2306. TrioWriteWideStringCharacter
  2307. TRIO_ARGS4((self, wch, flags, width),
  2308. trio_class_t *self,
  2309. trio_wchar_t wch,
  2310. trio_flags_t flags,
  2311. int width)
  2312. {
  2313. int size;
  2314. int i;
  2315. int ch;
  2316. char *string;
  2317. char buffer[MB_LEN_MAX + 1];
  2318. if (width == NO_WIDTH)
  2319. width = sizeof(buffer);
  2320. size = wctomb(buffer, wch);
  2321. if ((size <= 0) || (size > width) || (buffer[0] == NIL))
  2322. return 0;
  2323. string = buffer;
  2324. i = size;
  2325. while ((width >= i) && (width-- > 0) && (i-- > 0))
  2326. {
  2327. /* The ctype parameters must be an unsigned char (or EOF) */
  2328. ch = (int)((unsigned char)(*string++));
  2329. TrioWriteStringCharacter(self, ch, flags);
  2330. }
  2331. return size;
  2332. }
  2333. #endif /* TRIO_WIDECHAR */
  2334. /*************************************************************************
  2335. * TrioWriteWideString
  2336. *
  2337. * Description:
  2338. * Output a wide character string as a multi-byte string
  2339. */
  2340. #if TRIO_WIDECHAR
  2341. TRIO_PRIVATE void
  2342. TrioWriteWideString
  2343. TRIO_ARGS5((self, wstring, flags, width, precision),
  2344. trio_class_t *self,
  2345. TRIO_CONST trio_wchar_t *wstring,
  2346. trio_flags_t flags,
  2347. int width,
  2348. int precision)
  2349. {
  2350. int length;
  2351. int size;
  2352. assert(VALID(self));
  2353. assert(VALID(self->OutStream));
  2354. #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
  2355. (void)mblen(NULL, 0);
  2356. #endif
  2357. if (wstring == NULL)
  2358. {
  2359. TrioWriteString(self, NULL, flags, width, precision);
  2360. return;
  2361. }
  2362. if (NO_PRECISION == precision)
  2363. {
  2364. length = INT_MAX;
  2365. }
  2366. else
  2367. {
  2368. length = precision;
  2369. width -= length;
  2370. }
  2371. if (flags & FLAGS_QUOTE)
  2372. self->OutStream(self, CHAR_QUOTE);
  2373. if (! (flags & FLAGS_LEFTADJUST))
  2374. {
  2375. while (width-- > 0)
  2376. self->OutStream(self, CHAR_ADJUST);
  2377. }
  2378. while (length > 0)
  2379. {
  2380. size = TrioWriteWideStringCharacter(self, *wstring++, flags, length);
  2381. if (size == 0)
  2382. break; /* while */
  2383. length -= size;
  2384. }
  2385. if (flags & FLAGS_LEFTADJUST)
  2386. {
  2387. while (width-- > 0)
  2388. self->OutStream(self, CHAR_ADJUST);
  2389. }
  2390. if (flags & FLAGS_QUOTE)
  2391. self->OutStream(self, CHAR_QUOTE);
  2392. }
  2393. #endif /* TRIO_WIDECHAR */
  2394. /*************************************************************************
  2395. * TrioWriteDouble
  2396. *
  2397. * http://wwwold.dkuug.dk/JTC1/SC22/WG14/www/docs/dr_211.htm
  2398. *
  2399. * "5.2.4.2.2 paragraph #4
  2400. *
  2401. * The accuracy [...] is implementation defined, as is the accuracy
  2402. * of the conversion between floating-point internal representations
  2403. * and string representations performed by the library routine in
  2404. * <stdio.h>"
  2405. */
  2406. /* FIXME: handle all instances of constant long-double number (L)
  2407. * and *l() math functions.
  2408. */
  2409. TRIO_PRIVATE void
  2410. TrioWriteDouble
  2411. TRIO_ARGS6((self, number, flags, width, precision, base),
  2412. trio_class_t *self,
  2413. trio_long_double_t number,
  2414. trio_flags_t flags,
  2415. int width,
  2416. int precision,
  2417. int base)
  2418. {
  2419. trio_long_double_t integerNumber;
  2420. trio_long_double_t fractionNumber;
  2421. trio_long_double_t workNumber;
  2422. int integerDigits;
  2423. int fractionDigits;
  2424. int exponentDigits;
  2425. int baseDigits;
  2426. int integerThreshold;
  2427. int fractionThreshold;
  2428. int expectedWidth;
  2429. int exponent = 0;
  2430. unsigned int uExponent = 0;
  2431. int exponentBase;
  2432. trio_long_double_t dblBase;
  2433. trio_long_double_t dblIntegerBase;
  2434. trio_long_double_t dblFractionBase;
  2435. trio_long_double_t integerAdjust;
  2436. trio_long_double_t fractionAdjust;
  2437. BOOLEAN_T isNegative;
  2438. BOOLEAN_T isExponentNegative = FALSE;
  2439. BOOLEAN_T requireTwoDigitExponent;
  2440. BOOLEAN_T isHex;
  2441. TRIO_CONST char *digits;
  2442. char *groupingPointer;
  2443. int i;
  2444. int index;
  2445. BOOLEAN_T hasOnlyZeroes;
  2446. int zeroes = 0;
  2447. register int trailingZeroes;
  2448. BOOLEAN_T keepTrailingZeroes;
  2449. BOOLEAN_T keepDecimalPoint;
  2450. trio_long_double_t epsilon;
  2451. assert(VALID(self));
  2452. assert(VALID(self->OutStream));
  2453. assert(((base >= MIN_BASE) && (base <= MAX_BASE)) || (base == NO_BASE));
  2454. /* Determine sign and look for special quantities */
  2455. switch (trio_fpclassify_and_signbit(number, &isNegative))
  2456. {
  2457. case TRIO_FP_NAN:
  2458. TrioWriteString(self,
  2459. (flags & FLAGS_UPPER)
  2460. ? NAN_UPPER
  2461. : NAN_LOWER,
  2462. flags, width, precision);
  2463. return;
  2464. case TRIO_FP_INFINITE:
  2465. if (isNegative)
  2466. {
  2467. /* Negative infinity */
  2468. TrioWriteString(self,
  2469. (flags & FLAGS_UPPER)
  2470. ? "-" INFINITE_UPPER
  2471. : "-" INFINITE_LOWER,
  2472. flags, width, precision);
  2473. return;
  2474. }
  2475. else
  2476. {
  2477. /* Positive infinity */
  2478. TrioWriteString(self,
  2479. (flags & FLAGS_UPPER)
  2480. ? INFINITE_UPPER
  2481. : INFINITE_LOWER,
  2482. flags, width, precision);
  2483. return;
  2484. }
  2485. default:
  2486. /* Finitude */
  2487. break;
  2488. }
  2489. /* Normal numbers */
  2490. if (flags & FLAGS_LONGDOUBLE)
  2491. {
  2492. baseDigits = (base == 10)
  2493. ? LDBL_DIG
  2494. : (int)floor(LDBL_MANT_DIG / TrioLogarithmBase(base));
  2495. epsilon = LDBL_EPSILON;
  2496. }
  2497. else if (flags & FLAGS_SHORT)
  2498. {
  2499. baseDigits = (base == BASE_DECIMAL)
  2500. ? FLT_DIG
  2501. : (int)floor(FLT_MANT_DIG / TrioLogarithmBase(base));
  2502. epsilon = FLT_EPSILON;
  2503. }
  2504. else
  2505. {
  2506. baseDigits = (base == BASE_DECIMAL)
  2507. ? DBL_DIG
  2508. : (int)floor(DBL_MANT_DIG / TrioLogarithmBase(base));
  2509. epsilon = DBL_EPSILON;
  2510. }
  2511. digits = (flags & FLAGS_UPPER) ? internalDigitsUpper : internalDigitsLower;
  2512. isHex = (base == BASE_HEX);
  2513. if (base == NO_BASE)
  2514. base = BASE_DECIMAL;
  2515. dblBase = (trio_long_double_t)base;
  2516. keepTrailingZeroes = !( (flags & FLAGS_ROUNDING) ||
  2517. ( (flags & FLAGS_FLOAT_G) &&
  2518. !(flags & FLAGS_ALTERNATIVE) ) );
  2519. if (flags & FLAGS_ROUNDING)
  2520. precision = baseDigits;
  2521. if (precision == NO_PRECISION)
  2522. {
  2523. if (isHex)
  2524. {
  2525. keepTrailingZeroes = FALSE;
  2526. precision = FLT_MANT_DIG;
  2527. }
  2528. else
  2529. {
  2530. precision = FLT_DIG;
  2531. }
  2532. }
  2533. if (isNegative)
  2534. number = -number;
  2535. if (isHex)
  2536. flags |= FLAGS_FLOAT_E;
  2537. if (flags & FLAGS_FLOAT_G)
  2538. {
  2539. if (precision == 0)
  2540. precision = 1;
  2541. if ((number < 1.0E-4) || (number > powl(base,
  2542. (trio_long_double_t)precision)))
  2543. {
  2544. /* Use scientific notation */
  2545. flags |= FLAGS_FLOAT_E;
  2546. }
  2547. else if (number < 1.0)
  2548. {
  2549. /*
  2550. * Use normal notation. If the integer part of the number is
  2551. * zero, then adjust the precision to include leading fractional
  2552. * zeros.
  2553. */
  2554. workNumber = TrioLogarithm(number, base);
  2555. workNumber = TRIO_FABS(workNumber);
  2556. if (workNumber - floorl(workNumber) < 0.001)
  2557. workNumber--;
  2558. zeroes = (int)floorl(workNumber);
  2559. }
  2560. }
  2561. if (flags & FLAGS_FLOAT_E)
  2562. {
  2563. /* Scale the number */
  2564. workNumber = TrioLogarithm(number, base);
  2565. if (trio_isinf(workNumber) == -1)
  2566. {
  2567. exponent = 0;
  2568. /* Undo setting */
  2569. if (flags & FLAGS_FLOAT_G)
  2570. flags &= ~FLAGS_FLOAT_E;
  2571. }
  2572. else
  2573. {
  2574. exponent = (int)floorl(workNumber);
  2575. number /= powl(dblBase, (trio_long_double_t)exponent);
  2576. isExponentNegative = (exponent < 0);
  2577. uExponent = (isExponentNegative) ? -exponent : exponent;
  2578. if (isHex)
  2579. uExponent *= 4; /* log16(2) */
  2580. /* No thousand separators */
  2581. flags &= ~FLAGS_QUOTE;
  2582. }
  2583. }
  2584. integerNumber = floorl(number);
  2585. fractionNumber = number - integerNumber;
  2586. /*
  2587. * Truncated number.
  2588. *
  2589. * Precision is number of significant digits for FLOAT_G
  2590. * and number of fractional digits for others.
  2591. */
  2592. integerDigits = (integerNumber > epsilon)
  2593. ? 1 + (int)TrioLogarithm(integerNumber, base)
  2594. : 1;
  2595. fractionDigits = ((flags & FLAGS_FLOAT_G) && (zeroes == 0))
  2596. ? precision - integerDigits
  2597. : zeroes + precision;
  2598. dblFractionBase = TrioPower(base, fractionDigits);
  2599. workNumber = number + 0.5 / dblFractionBase;
  2600. if (floorl(number) != floorl(workNumber))
  2601. {
  2602. if (flags & FLAGS_FLOAT_E)
  2603. {
  2604. /* Adjust if number was rounded up one digit (ie. 0.99 to 1.00) */
  2605. exponent++;
  2606. isExponentNegative = (exponent < 0);
  2607. uExponent = (isExponentNegative) ? -exponent : exponent;
  2608. if (isHex)
  2609. uExponent *= 4; /* log16(2) */
  2610. workNumber = (number + 0.5 / dblFractionBase) / dblBase;
  2611. integerNumber = floorl(workNumber);
  2612. fractionNumber = workNumber - integerNumber;
  2613. }
  2614. else
  2615. {
  2616. /* Adjust if number was rounded up one digit (ie. 99 to 100) */
  2617. integerNumber = floorl(number + 0.5);
  2618. fractionNumber = 0.0;
  2619. integerDigits = (integerNumber > epsilon)
  2620. ? 1 + (int)TrioLogarithm(integerNumber, base)
  2621. : 1;
  2622. }
  2623. }
  2624. /* Estimate accuracy */
  2625. integerAdjust = fractionAdjust = 0.5;
  2626. if (flags & FLAGS_ROUNDING)
  2627. {
  2628. if (integerDigits > baseDigits)
  2629. {
  2630. integerThreshold = baseDigits;
  2631. fractionDigits = 0;
  2632. dblFractionBase = 1.0;
  2633. fractionThreshold = 0;
  2634. precision = 0; /* Disable decimal-point */
  2635. integerAdjust = TrioPower(base, integerDigits - integerThreshold - 1);
  2636. fractionAdjust = 0.0;
  2637. }
  2638. else
  2639. {
  2640. integerThreshold = integerDigits;
  2641. fractionThreshold = fractionDigits - integerThreshold;
  2642. fractionAdjust = 1.0;
  2643. }
  2644. }
  2645. else
  2646. {
  2647. integerThreshold = INT_MAX;
  2648. fractionThreshold = INT_MAX;
  2649. }
  2650. /*
  2651. * Calculate expected width.
  2652. * sign + integer part + thousands separators + decimal point
  2653. * + fraction + exponent
  2654. */
  2655. fractionAdjust /= dblFractionBase;
  2656. hasOnlyZeroes = (floorl((fractionNumber + fractionAdjust) * dblFractionBase) < epsilon);
  2657. keepDecimalPoint = ( (flags & FLAGS_ALTERNATIVE) ||
  2658. !((precision == 0) ||
  2659. (!keepTrailingZeroes && hasOnlyZeroes)) );
  2660. if (flags & FLAGS_FLOAT_E)
  2661. {
  2662. exponentDigits = (uExponent == 0)
  2663. ? 1
  2664. : (int)ceil(TrioLogarithm((double)(uExponent + 1),
  2665. (isHex) ? 10.0 : base));
  2666. }
  2667. else
  2668. exponentDigits = 0;
  2669. requireTwoDigitExponent = ((base == BASE_DECIMAL) && (exponentDigits == 1));
  2670. expectedWidth = integerDigits + fractionDigits
  2671. + (keepDecimalPoint
  2672. ? internalDecimalPointLength
  2673. : 0)
  2674. + ((flags & FLAGS_QUOTE)
  2675. ? TrioCalcThousandSeparatorLength(integerDigits)
  2676. : 0);
  2677. if (isNegative || (flags & FLAGS_SHOWSIGN) || (flags & FLAGS_SPACE))
  2678. expectedWidth += sizeof("-") - 1;
  2679. if (exponentDigits > 0)
  2680. expectedWidth += exponentDigits +
  2681. ((requireTwoDigitExponent ? sizeof("E+0") : sizeof("E+")) - 1);
  2682. if (isHex)
  2683. expectedWidth += sizeof("0X") - 1;
  2684. /* Output prefixing */
  2685. if (flags & FLAGS_NILPADDING)
  2686. {
  2687. /* Leading zeros must be after sign */
  2688. if (isNegative)
  2689. self->OutStream(self, '-');
  2690. else if (flags & FLAGS_SHOWSIGN)
  2691. self->OutStream(self, '+');
  2692. else if (flags & FLAGS_SPACE)
  2693. self->OutStream(self, ' ');
  2694. if (isHex)
  2695. {
  2696. self->OutStream(self, '0');
  2697. self->OutStream(self, (flags & FLAGS_UPPER) ? 'X' : 'x');
  2698. }
  2699. if (!(flags & FLAGS_LEFTADJUST))
  2700. {
  2701. for (i = expectedWidth; i < width; i++)
  2702. {
  2703. self->OutStream(self, '0');
  2704. }
  2705. }
  2706. }
  2707. else
  2708. {
  2709. /* Leading spaces must be before sign */
  2710. if (!(flags & FLAGS_LEFTADJUST))
  2711. {
  2712. for (i = expectedWidth; i < width; i++)
  2713. {
  2714. self->OutStream(self, CHAR_ADJUST);
  2715. }
  2716. }
  2717. if (isNegative)
  2718. self->OutStream(self, '-');
  2719. else if (flags & FLAGS_SHOWSIGN)
  2720. self->OutStream(self, '+');
  2721. else if (flags & FLAGS_SPACE)
  2722. self->OutStream(self, ' ');
  2723. if (isHex)
  2724. {
  2725. self->OutStream(self, '0');
  2726. self->OutStream(self, (flags & FLAGS_UPPER) ? 'X' : 'x');
  2727. }
  2728. }
  2729. /* Output the integer part and thousand separators */
  2730. dblIntegerBase = 1.0 / TrioPower(base, integerDigits - 1);
  2731. for (i = 0; i < integerDigits; i++)
  2732. {
  2733. workNumber = floorl(((integerNumber + integerAdjust) * dblIntegerBase));
  2734. if (i > integerThreshold)
  2735. {
  2736. /* Beyond accuracy */
  2737. self->OutStream(self, digits[0]);
  2738. }
  2739. else
  2740. {
  2741. self->OutStream(self, digits[(int)fmodl(workNumber, dblBase)]);
  2742. }
  2743. dblIntegerBase *= dblBase;
  2744. if (((flags & (FLAGS_FLOAT_E | FLAGS_QUOTE)) == FLAGS_QUOTE)
  2745. && TrioFollowedBySeparator(integerDigits - i))
  2746. {
  2747. for (groupingPointer = internalThousandSeparator;
  2748. *groupingPointer != NIL;
  2749. groupingPointer++)
  2750. {
  2751. self->OutStream(self, *groupingPointer);
  2752. }
  2753. }
  2754. }
  2755. /* Insert decimal point and build the fraction part */
  2756. trailingZeroes = 0;
  2757. if (keepDecimalPoint)
  2758. {
  2759. if (internalDecimalPoint)
  2760. {
  2761. self->OutStream(self, internalDecimalPoint);
  2762. }
  2763. else
  2764. {
  2765. for (i = 0; i < internalDecimalPointLength; i++)
  2766. {
  2767. self->OutStream(self, internalDecimalPointString[i]);
  2768. }
  2769. }
  2770. }
  2771. for (i = 0; i < fractionDigits; i++)
  2772. {
  2773. if ((integerDigits > integerThreshold) || (i > fractionThreshold))
  2774. {
  2775. /* Beyond accuracy */
  2776. trailingZeroes++;
  2777. }
  2778. else
  2779. {
  2780. fractionNumber *= dblBase;
  2781. fractionAdjust *= dblBase;
  2782. workNumber = floorl(fractionNumber + fractionAdjust);
  2783. fractionNumber -= workNumber;
  2784. index = (int)fmodl(workNumber, dblBase);
  2785. if (index == 0)
  2786. {
  2787. trailingZeroes++;
  2788. }
  2789. else
  2790. {
  2791. while (trailingZeroes > 0)
  2792. {
  2793. /* Not trailing zeroes after all */
  2794. self->OutStream(self, digits[0]);
  2795. trailingZeroes--;
  2796. }
  2797. self->OutStream(self, digits[index]);
  2798. }
  2799. }
  2800. }
  2801. if (keepTrailingZeroes)
  2802. {
  2803. while (trailingZeroes > 0)
  2804. {
  2805. self->OutStream(self, digits[0]);
  2806. trailingZeroes--;
  2807. }
  2808. }
  2809. /* Output exponent */
  2810. if (exponentDigits > 0)
  2811. {
  2812. self->OutStream(self,
  2813. isHex
  2814. ? ((flags & FLAGS_UPPER) ? 'P' : 'p')
  2815. : ((flags & FLAGS_UPPER) ? 'E' : 'e'));
  2816. self->OutStream(self, (isExponentNegative) ? '-' : '+');
  2817. /* The exponent must contain at least two digits */
  2818. if (requireTwoDigitExponent)
  2819. self->OutStream(self, '0');
  2820. if (isHex)
  2821. base = 10.0;
  2822. exponentBase = (int)TrioPower(base, exponentDigits - 1);
  2823. for (i = 0; i < exponentDigits; i++)
  2824. {
  2825. self->OutStream(self, digits[(uExponent / exponentBase) % base]);
  2826. exponentBase /= base;
  2827. }
  2828. }
  2829. /* Output trailing spaces */
  2830. if (flags & FLAGS_LEFTADJUST)
  2831. {
  2832. for (i = expectedWidth; i < width; i++)
  2833. {
  2834. self->OutStream(self, CHAR_ADJUST);
  2835. }
  2836. }
  2837. }
  2838. /*************************************************************************
  2839. * TrioFormatProcess
  2840. *
  2841. * Description:
  2842. * This is the main engine for formatting output
  2843. */
  2844. TRIO_PRIVATE int
  2845. TrioFormatProcess
  2846. TRIO_ARGS3((data, format, parameters),
  2847. trio_class_t *data,
  2848. TRIO_CONST char *format,
  2849. trio_parameter_t *parameters)
  2850. {
  2851. #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
  2852. int charlen;
  2853. #endif
  2854. int i;
  2855. TRIO_CONST char *string;
  2856. trio_pointer_t pointer;
  2857. trio_flags_t flags;
  2858. int width;
  2859. int precision;
  2860. int base;
  2861. int index;
  2862. index = 0;
  2863. i = 0;
  2864. #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
  2865. (void)mblen(NULL, 0);
  2866. #endif
  2867. while (format[index])
  2868. {
  2869. #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
  2870. if (! isascii(format[index]))
  2871. {
  2872. charlen = mblen(&format[index], MB_LEN_MAX);
  2873. /*
  2874. * Only valid multibyte characters are handled here. Invalid
  2875. * multibyte characters (charlen == -1) are handled as normal
  2876. * characters.
  2877. */
  2878. if (charlen != -1)
  2879. {
  2880. while (charlen-- > 0)
  2881. {
  2882. data->OutStream(data, format[index++]);
  2883. }
  2884. continue; /* while characters left in formatting string */
  2885. }
  2886. }
  2887. #endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
  2888. if (CHAR_IDENTIFIER == format[index])
  2889. {
  2890. if (CHAR_IDENTIFIER == format[index + 1])
  2891. {
  2892. data->OutStream(data, CHAR_IDENTIFIER);
  2893. index += 2;
  2894. }
  2895. else
  2896. {
  2897. /* Skip the parameter entries */
  2898. while (parameters[i].type == FORMAT_PARAMETER)
  2899. i++;
  2900. flags = parameters[i].flags;
  2901. /* Find width */
  2902. width = parameters[i].width;
  2903. if (flags & FLAGS_WIDTH_PARAMETER)
  2904. {
  2905. /* Get width from parameter list */
  2906. width = (int)parameters[width].data.number.as_signed;
  2907. if (width < 0)
  2908. {
  2909. /*
  2910. * A negative width is the same as the - flag and
  2911. * a positive width.
  2912. */
  2913. flags |= FLAGS_LEFTADJUST;
  2914. flags &= ~FLAGS_NILPADDING;
  2915. width = -width;
  2916. }
  2917. }
  2918. /* Find precision */
  2919. if (flags & FLAGS_PRECISION)
  2920. {
  2921. precision = parameters[i].precision;
  2922. if (flags & FLAGS_PRECISION_PARAMETER)
  2923. {
  2924. /* Get precision from parameter list */
  2925. precision = (int)parameters[precision].data.number.as_signed;
  2926. if (precision < 0)
  2927. {
  2928. /*
  2929. * A negative precision is the same as no
  2930. * precision
  2931. */
  2932. precision = NO_PRECISION;
  2933. }
  2934. }
  2935. }
  2936. else
  2937. {
  2938. precision = NO_PRECISION;
  2939. }
  2940. /* Find base */
  2941. base = parameters[i].base;
  2942. if (flags & FLAGS_BASE_PARAMETER)
  2943. {
  2944. /* Get base from parameter list */
  2945. base = (int)parameters[base].data.number.as_signed;
  2946. }
  2947. switch (parameters[i].type)
  2948. {
  2949. case FORMAT_CHAR:
  2950. if (flags & FLAGS_QUOTE)
  2951. data->OutStream(data, CHAR_QUOTE);
  2952. if (! (flags & FLAGS_LEFTADJUST))
  2953. {
  2954. while (--width > 0)
  2955. data->OutStream(data, CHAR_ADJUST);
  2956. }
  2957. #if TRIO_WIDECHAR
  2958. if (flags & FLAGS_WIDECHAR)
  2959. {
  2960. TrioWriteWideStringCharacter(data,
  2961. (trio_wchar_t)parameters[i].data.number.as_signed,
  2962. flags,
  2963. NO_WIDTH);
  2964. }
  2965. else
  2966. #endif
  2967. {
  2968. TrioWriteStringCharacter(data,
  2969. (int)parameters[i].data.number.as_signed,
  2970. flags);
  2971. }
  2972. if (flags & FLAGS_LEFTADJUST)
  2973. {
  2974. while(--width > 0)
  2975. data->OutStream(data, CHAR_ADJUST);
  2976. }
  2977. if (flags & FLAGS_QUOTE)
  2978. data->OutStream(data, CHAR_QUOTE);
  2979. break; /* FORMAT_CHAR */
  2980. case FORMAT_INT:
  2981. TrioWriteNumber(data,
  2982. parameters[i].data.number.as_unsigned,
  2983. flags,
  2984. width,
  2985. precision,
  2986. base);
  2987. break; /* FORMAT_INT */
  2988. case FORMAT_DOUBLE:
  2989. TrioWriteDouble(data,
  2990. parameters[i].data.longdoubleNumber,
  2991. flags,
  2992. width,
  2993. precision,
  2994. base);
  2995. break; /* FORMAT_DOUBLE */
  2996. case FORMAT_STRING:
  2997. #if TRIO_WIDECHAR
  2998. if (flags & FLAGS_WIDECHAR)
  2999. {
  3000. TrioWriteWideString(data,
  3001. parameters[i].data.wstring,
  3002. flags,
  3003. width,
  3004. precision);
  3005. }
  3006. else
  3007. #endif
  3008. {
  3009. TrioWriteString(data,
  3010. parameters[i].data.string,
  3011. flags,
  3012. width,
  3013. precision);
  3014. }
  3015. break; /* FORMAT_STRING */
  3016. case FORMAT_POINTER:
  3017. {
  3018. trio_reference_t reference;
  3019. reference.data = data;
  3020. reference.parameter = &parameters[i];
  3021. trio_print_pointer(&reference, parameters[i].data.pointer);
  3022. }
  3023. break; /* FORMAT_POINTER */
  3024. case FORMAT_COUNT:
  3025. pointer = parameters[i].data.pointer;
  3026. if (NULL != pointer)
  3027. {
  3028. /*
  3029. * C99 paragraph 7.19.6.1.8 says "the number of
  3030. * characters written to the output stream so far by
  3031. * this call", which is data->committed
  3032. */
  3033. #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
  3034. if (flags & FLAGS_SIZE_T)
  3035. *(size_t *)pointer = (size_t)data->committed;
  3036. else
  3037. #endif
  3038. #if defined(QUALIFIER_PTRDIFF_T)
  3039. if (flags & FLAGS_PTRDIFF_T)
  3040. *(ptrdiff_t *)pointer = (ptrdiff_t)data->committed;
  3041. else
  3042. #endif
  3043. #if defined(QUALIFIER_INTMAX_T)
  3044. if (flags & FLAGS_INTMAX_T)
  3045. *(trio_intmax_t *)pointer = (trio_intmax_t)data->committed;
  3046. else
  3047. #endif
  3048. if (flags & FLAGS_QUAD)
  3049. {
  3050. *(trio_ulonglong_t *)pointer = (trio_ulonglong_t)data->committed;
  3051. }
  3052. else if (flags & FLAGS_LONG)
  3053. {
  3054. *(long int *)pointer = (long int)data->committed;
  3055. }
  3056. else if (flags & FLAGS_SHORT)
  3057. {
  3058. *(short int *)pointer = (short int)data->committed;
  3059. }
  3060. else
  3061. {
  3062. *(int *)pointer = (int)data->committed;
  3063. }
  3064. }
  3065. break; /* FORMAT_COUNT */
  3066. case FORMAT_PARAMETER:
  3067. break; /* FORMAT_PARAMETER */
  3068. #if defined(FORMAT_ERRNO)
  3069. case FORMAT_ERRNO:
  3070. string = trio_error(parameters[i].data.errorNumber);
  3071. if (string)
  3072. {
  3073. TrioWriteString(data,
  3074. string,
  3075. flags,
  3076. width,
  3077. precision);
  3078. }
  3079. else
  3080. {
  3081. data->OutStream(data, '#');
  3082. TrioWriteNumber(data,
  3083. (trio_uintmax_t)parameters[i].data.errorNumber,
  3084. flags,
  3085. width,
  3086. precision,
  3087. BASE_DECIMAL);
  3088. }
  3089. break; /* FORMAT_ERRNO */
  3090. #endif /* defined(FORMAT_ERRNO) */
  3091. #if defined(FORMAT_USER_DEFINED)
  3092. case FORMAT_USER_DEFINED:
  3093. {
  3094. trio_reference_t reference;
  3095. trio_userdef_t *def = NULL;
  3096. if (parameters[i].user_name[0] == NIL)
  3097. {
  3098. /* Use handle */
  3099. if ((i > 0) ||
  3100. (parameters[i - 1].type == FORMAT_PARAMETER))
  3101. def = (trio_userdef_t *)parameters[i - 1].data.pointer;
  3102. }
  3103. else
  3104. {
  3105. /* Look up namespace */
  3106. def = TrioFindNamespace(parameters[i].user_name, NULL);
  3107. }
  3108. if (def) {
  3109. reference.data = data;
  3110. reference.parameter = &parameters[i];
  3111. def->callback(&reference);
  3112. }
  3113. }
  3114. break;
  3115. #endif /* defined(FORMAT_USER_DEFINED) */
  3116. default:
  3117. break;
  3118. } /* switch parameter type */
  3119. /* Prepare for next */
  3120. index = parameters[i].indexAfterSpecifier;
  3121. i++;
  3122. }
  3123. }
  3124. else /* not identifier */
  3125. {
  3126. data->OutStream(data, format[index++]);
  3127. }
  3128. }
  3129. return data->processed;
  3130. }
  3131. /*************************************************************************
  3132. * TrioFormatRef
  3133. */
  3134. TRIO_PRIVATE int
  3135. TrioFormatRef
  3136. TRIO_ARGS4((reference, format, arglist, argarray),
  3137. trio_reference_t *reference,
  3138. TRIO_CONST char *format,
  3139. TRIO_VA_LIST_PTR arglist,
  3140. trio_pointer_t *argarray)
  3141. {
  3142. int status;
  3143. trio_parameter_t parameters[MAX_PARAMETERS];
  3144. status = TrioParse(TYPE_PRINT, format, parameters, arglist, argarray);
  3145. if (status < 0)
  3146. return status;
  3147. status = TrioFormatProcess(reference->data, format, parameters);
  3148. if (reference->data->error != 0)
  3149. {
  3150. status = reference->data->error;
  3151. }
  3152. return status;
  3153. }
  3154. /*************************************************************************
  3155. * TrioFormat
  3156. */
  3157. TRIO_PRIVATE int
  3158. TrioFormat
  3159. TRIO_ARGS6((destination, destinationSize, OutStream, format, arglist, argarray),
  3160. trio_pointer_t destination,
  3161. size_t destinationSize,
  3162. void (*OutStream) TRIO_PROTO((trio_class_t *, int)),
  3163. TRIO_CONST char *format,
  3164. TRIO_VA_LIST_PTR arglist,
  3165. trio_pointer_t *argarray)
  3166. {
  3167. int status;
  3168. trio_class_t data;
  3169. trio_parameter_t parameters[MAX_PARAMETERS];
  3170. assert(VALID(OutStream));
  3171. assert(VALID(format));
  3172. memset(&data, 0, sizeof(data));
  3173. data.OutStream = OutStream;
  3174. data.location = destination;
  3175. data.max = destinationSize;
  3176. data.error = 0;
  3177. #if defined(USE_LOCALE)
  3178. if (NULL == internalLocaleValues)
  3179. {
  3180. TrioSetLocale();
  3181. }
  3182. #endif
  3183. status = TrioParse(TYPE_PRINT, format, parameters, arglist, argarray);
  3184. if (status < 0)
  3185. return status;
  3186. status = TrioFormatProcess(&data, format, parameters);
  3187. if (data.error != 0)
  3188. {
  3189. status = data.error;
  3190. }
  3191. return status;
  3192. }
  3193. /*************************************************************************
  3194. * TrioOutStreamFile
  3195. */
  3196. TRIO_PRIVATE void
  3197. TrioOutStreamFile
  3198. TRIO_ARGS2((self, output),
  3199. trio_class_t *self,
  3200. int output)
  3201. {
  3202. FILE *file;
  3203. assert(VALID(self));
  3204. assert(VALID(self->location));
  3205. file = (FILE *)self->location;
  3206. self->processed++;
  3207. if (fputc(output, file) == EOF)
  3208. {
  3209. self->error = TRIO_ERROR_RETURN(TRIO_EOF, 0);
  3210. }
  3211. else
  3212. {
  3213. self->committed++;
  3214. }
  3215. }
  3216. /*************************************************************************
  3217. * TrioOutStreamFileDescriptor
  3218. */
  3219. TRIO_PRIVATE void
  3220. TrioOutStreamFileDescriptor
  3221. TRIO_ARGS2((self, output),
  3222. trio_class_t *self,
  3223. int output)
  3224. {
  3225. int fd;
  3226. char ch;
  3227. assert(VALID(self));
  3228. fd = *((int *)self->location);
  3229. ch = (char)output;
  3230. self->processed++;
  3231. if (write(fd, &ch, sizeof(char)) == -1)
  3232. {
  3233. self->error = TRIO_ERROR_RETURN(TRIO_ERRNO, 0);
  3234. }
  3235. else
  3236. {
  3237. self->committed++;
  3238. }
  3239. }
  3240. /*************************************************************************
  3241. * TrioOutStreamCustom
  3242. */
  3243. TRIO_PRIVATE void
  3244. TrioOutStreamCustom
  3245. TRIO_ARGS2((self, output),
  3246. trio_class_t *self,
  3247. int output)
  3248. {
  3249. int status;
  3250. trio_custom_t *data;
  3251. assert(VALID(self));
  3252. assert(VALID(self->location));
  3253. data = (trio_custom_t *)self->location;
  3254. if (data->stream.out)
  3255. {
  3256. status = (data->stream.out)(data->closure, output);
  3257. if (status >= 0)
  3258. {
  3259. self->committed++;
  3260. }
  3261. else
  3262. {
  3263. if (self->error == 0)
  3264. {
  3265. self->error = TRIO_ERROR_RETURN(TRIO_ECUSTOM, -status);
  3266. }
  3267. }
  3268. }
  3269. self->processed++;
  3270. }
  3271. /*************************************************************************
  3272. * TrioOutStreamString
  3273. */
  3274. TRIO_PRIVATE void
  3275. TrioOutStreamString
  3276. TRIO_ARGS2((self, output),
  3277. trio_class_t *self,
  3278. int output)
  3279. {
  3280. char **buffer;
  3281. assert(VALID(self));
  3282. assert(VALID(self->location));
  3283. buffer = (char **)self->location;
  3284. **buffer = (char)output;
  3285. (*buffer)++;
  3286. self->processed++;
  3287. self->committed++;
  3288. }
  3289. /*************************************************************************
  3290. * TrioOutStreamStringMax
  3291. */
  3292. TRIO_PRIVATE void
  3293. TrioOutStreamStringMax
  3294. TRIO_ARGS2((self, output),
  3295. trio_class_t *self,
  3296. int output)
  3297. {
  3298. char **buffer;
  3299. assert(VALID(self));
  3300. assert(VALID(self->location));
  3301. buffer = (char **)self->location;
  3302. if (self->processed < self->max)
  3303. {
  3304. **buffer = (char)output;
  3305. (*buffer)++;
  3306. self->committed++;
  3307. }
  3308. self->processed++;
  3309. }
  3310. /*************************************************************************
  3311. * TrioOutStreamStringDynamic
  3312. */
  3313. TRIO_PRIVATE void
  3314. TrioOutStreamStringDynamic
  3315. TRIO_ARGS2((self, output),
  3316. trio_class_t *self,
  3317. int output)
  3318. {
  3319. assert(VALID(self));
  3320. assert(VALID(self->location));
  3321. if (self->error == 0)
  3322. {
  3323. trio_xstring_append_char((trio_string_t *)self->location,
  3324. (char)output);
  3325. self->committed++;
  3326. }
  3327. /* The processed variable must always be increased */
  3328. self->processed++;
  3329. }
  3330. /*************************************************************************
  3331. *
  3332. * Formatted printing functions
  3333. *
  3334. ************************************************************************/
  3335. #if defined(TRIO_DOCUMENTATION)
  3336. # include "doc/doc_printf.h"
  3337. #endif
  3338. /** @addtogroup Printf
  3339. @{
  3340. */
  3341. /*************************************************************************
  3342. * printf
  3343. */
  3344. /**
  3345. Print to standard output stream.
  3346. @param format Formatting string.
  3347. @param ... Arguments.
  3348. @return Number of printed characters.
  3349. */
  3350. TRIO_PUBLIC int
  3351. trio_printf
  3352. TRIO_VARGS2((format, va_alist),
  3353. TRIO_CONST char *format,
  3354. TRIO_VA_DECL)
  3355. {
  3356. int status;
  3357. va_list args;
  3358. assert(VALID(format));
  3359. TRIO_VA_START(args, format);
  3360. status = TrioFormat(stdout, 0, TrioOutStreamFile, format, TRIO_VA_LIST_ADDR(args), NULL);
  3361. TRIO_VA_END(args);
  3362. return status;
  3363. }
  3364. /**
  3365. Print to standard output stream.
  3366. @param format Formatting string.
  3367. @param args Arguments.
  3368. @return Number of printed characters.
  3369. */
  3370. TRIO_PUBLIC int
  3371. trio_vprintf
  3372. TRIO_ARGS2((format, args),
  3373. TRIO_CONST char *format,
  3374. va_list args)
  3375. {
  3376. assert(VALID(format));
  3377. return TrioFormat(stdout, 0, TrioOutStreamFile, format, TRIO_VA_LIST_ADDR(args), NULL);
  3378. }
  3379. /**
  3380. Print to standard output stream.
  3381. @param format Formatting string.
  3382. @param args Arguments.
  3383. @return Number of printed characters.
  3384. */
  3385. TRIO_PUBLIC int
  3386. trio_printfv
  3387. TRIO_ARGS2((format, args),
  3388. TRIO_CONST char *format,
  3389. trio_pointer_t * args)
  3390. {
  3391. assert(VALID(format));
  3392. return TrioFormat(stdout, 0, TrioOutStreamFile, format, NULL, args);
  3393. }
  3394. /*************************************************************************
  3395. * fprintf
  3396. */
  3397. /**
  3398. Print to file.
  3399. @param file File pointer.
  3400. @param format Formatting string.
  3401. @param ... Arguments.
  3402. @return Number of printed characters.
  3403. */
  3404. TRIO_PUBLIC int
  3405. trio_fprintf
  3406. TRIO_VARGS3((file, format, va_alist),
  3407. FILE *file,
  3408. TRIO_CONST char *format,
  3409. TRIO_VA_DECL)
  3410. {
  3411. int status;
  3412. va_list args;
  3413. assert(VALID(file));
  3414. assert(VALID(format));
  3415. TRIO_VA_START(args, format);
  3416. status = TrioFormat(file, 0, TrioOutStreamFile, format, TRIO_VA_LIST_ADDR(args), NULL);
  3417. TRIO_VA_END(args);
  3418. return status;
  3419. }
  3420. /**
  3421. Print to file.
  3422. @param file File pointer.
  3423. @param format Formatting string.
  3424. @param args Arguments.
  3425. @return Number of printed characters.
  3426. */
  3427. TRIO_PUBLIC int
  3428. trio_vfprintf
  3429. TRIO_ARGS3((file, format, args),
  3430. FILE *file,
  3431. TRIO_CONST char *format,
  3432. va_list args)
  3433. {
  3434. assert(VALID(file));
  3435. assert(VALID(format));
  3436. return TrioFormat(file, 0, TrioOutStreamFile, format, TRIO_VA_LIST_ADDR(args), NULL);
  3437. }
  3438. /**
  3439. Print to file.
  3440. @param file File pointer.
  3441. @param format Formatting string.
  3442. @param args Arguments.
  3443. @return Number of printed characters.
  3444. */
  3445. TRIO_PUBLIC int
  3446. trio_fprintfv
  3447. TRIO_ARGS3((file, format, args),
  3448. FILE *file,
  3449. TRIO_CONST char *format,
  3450. trio_pointer_t * args)
  3451. {
  3452. assert(VALID(file));
  3453. assert(VALID(format));
  3454. return TrioFormat(file, 0, TrioOutStreamFile, format, NULL, args);
  3455. }
  3456. /*************************************************************************
  3457. * dprintf
  3458. */
  3459. /**
  3460. Print to file descriptor.
  3461. @param fd File descriptor.
  3462. @param format Formatting string.
  3463. @param ... Arguments.
  3464. @return Number of printed characters.
  3465. */
  3466. TRIO_PUBLIC int
  3467. trio_dprintf
  3468. TRIO_VARGS3((fd, format, va_alist),
  3469. int fd,
  3470. TRIO_CONST char *format,
  3471. TRIO_VA_DECL)
  3472. {
  3473. int status;
  3474. va_list args;
  3475. assert(VALID(format));
  3476. TRIO_VA_START(args, format);
  3477. status = TrioFormat(&fd, 0, TrioOutStreamFileDescriptor, format, TRIO_VA_LIST_ADDR(args), NULL);
  3478. TRIO_VA_END(args);
  3479. return status;
  3480. }
  3481. /**
  3482. Print to file descriptor.
  3483. @param fd File descriptor.
  3484. @param format Formatting string.
  3485. @param args Arguments.
  3486. @return Number of printed characters.
  3487. */
  3488. TRIO_PUBLIC int
  3489. trio_vdprintf
  3490. TRIO_ARGS3((fd, format, args),
  3491. int fd,
  3492. TRIO_CONST char *format,
  3493. va_list args)
  3494. {
  3495. assert(VALID(format));
  3496. return TrioFormat(&fd, 0, TrioOutStreamFileDescriptor, format, TRIO_VA_LIST_ADDR(args), NULL);
  3497. }
  3498. /**
  3499. Print to file descriptor.
  3500. @param fd File descriptor.
  3501. @param format Formatting string.
  3502. @param args Arguments.
  3503. @return Number of printed characters.
  3504. */
  3505. TRIO_PUBLIC int
  3506. trio_dprintfv
  3507. TRIO_ARGS3((fd, format, args),
  3508. int fd,
  3509. TRIO_CONST char *format,
  3510. trio_pointer_t *args)
  3511. {
  3512. assert(VALID(format));
  3513. return TrioFormat(&fd, 0, TrioOutStreamFileDescriptor, format, NULL, args);
  3514. }
  3515. /*************************************************************************
  3516. * cprintf
  3517. */
  3518. TRIO_PUBLIC int
  3519. trio_cprintf
  3520. TRIO_VARGS4((stream, closure, format, va_alist),
  3521. trio_outstream_t stream,
  3522. trio_pointer_t closure,
  3523. TRIO_CONST char *format,
  3524. TRIO_VA_DECL)
  3525. {
  3526. int status;
  3527. va_list args;
  3528. trio_custom_t data;
  3529. assert(VALID(stream));
  3530. assert(VALID(format));
  3531. TRIO_VA_START(args, format);
  3532. data.stream.out = stream;
  3533. data.closure = closure;
  3534. status = TrioFormat(&data, 0, TrioOutStreamCustom, format, TRIO_VA_LIST_ADDR(args), NULL);
  3535. TRIO_VA_END(args);
  3536. return status;
  3537. }
  3538. TRIO_PUBLIC int
  3539. trio_vcprintf
  3540. TRIO_ARGS4((stream, closure, format, args),
  3541. trio_outstream_t stream,
  3542. trio_pointer_t closure,
  3543. TRIO_CONST char *format,
  3544. va_list args)
  3545. {
  3546. trio_custom_t data;
  3547. assert(VALID(stream));
  3548. assert(VALID(format));
  3549. data.stream.out = stream;
  3550. data.closure = closure;
  3551. return TrioFormat(&data, 0, TrioOutStreamCustom, format, TRIO_VA_LIST_ADDR(args), NULL);
  3552. }
  3553. TRIO_PUBLIC int
  3554. trio_cprintfv
  3555. TRIO_ARGS4((stream, closure, format, args),
  3556. trio_outstream_t stream,
  3557. trio_pointer_t closure,
  3558. TRIO_CONST char *format,
  3559. void **args)
  3560. {
  3561. trio_custom_t data;
  3562. assert(VALID(stream));
  3563. assert(VALID(format));
  3564. data.stream.out = stream;
  3565. data.closure = closure;
  3566. return TrioFormat(&data, 0, TrioOutStreamCustom, format, NULL, args);
  3567. }
  3568. /*************************************************************************
  3569. * sprintf
  3570. */
  3571. /**
  3572. Print to string.
  3573. @param buffer Output string.
  3574. @param format Formatting string.
  3575. @param ... Arguments.
  3576. @return Number of printed characters.
  3577. */
  3578. TRIO_PUBLIC int
  3579. trio_sprintf
  3580. TRIO_VARGS3((buffer, format, va_alist),
  3581. char *buffer,
  3582. TRIO_CONST char *format,
  3583. TRIO_VA_DECL)
  3584. {
  3585. int status;
  3586. va_list args;
  3587. assert(VALID(buffer));
  3588. assert(VALID(format));
  3589. TRIO_VA_START(args, format);
  3590. status = TrioFormat(&buffer, 0, TrioOutStreamString, format, TRIO_VA_LIST_ADDR(args), NULL);
  3591. *buffer = NIL; /* Terminate with NIL character */
  3592. TRIO_VA_END(args);
  3593. return status;
  3594. }
  3595. /**
  3596. Print to string.
  3597. @param buffer Output string.
  3598. @param format Formatting string.
  3599. @param args Arguments.
  3600. @return Number of printed characters.
  3601. */
  3602. TRIO_PUBLIC int
  3603. trio_vsprintf
  3604. TRIO_ARGS3((buffer, format, args),
  3605. char *buffer,
  3606. TRIO_CONST char *format,
  3607. va_list args)
  3608. {
  3609. int status;
  3610. assert(VALID(buffer));
  3611. assert(VALID(format));
  3612. status = TrioFormat(&buffer, 0, TrioOutStreamString, format, TRIO_VA_LIST_ADDR(args), NULL);
  3613. *buffer = NIL;
  3614. return status;
  3615. }
  3616. /**
  3617. Print to string.
  3618. @param buffer Output string.
  3619. @param format Formatting string.
  3620. @param args Arguments.
  3621. @return Number of printed characters.
  3622. */
  3623. TRIO_PUBLIC int
  3624. trio_sprintfv
  3625. TRIO_ARGS3((buffer, format, args),
  3626. char *buffer,
  3627. TRIO_CONST char *format,
  3628. trio_pointer_t *args)
  3629. {
  3630. int status;
  3631. assert(VALID(buffer));
  3632. assert(VALID(format));
  3633. status = TrioFormat(&buffer, 0, TrioOutStreamString, format, NULL, args);
  3634. *buffer = NIL;
  3635. return status;
  3636. }
  3637. /*************************************************************************
  3638. * snprintf
  3639. */
  3640. /**
  3641. Print at most @p max characters to string.
  3642. @param buffer Output string.
  3643. @param max Maximum number of characters to print.
  3644. @param format Formatting string.
  3645. @param ... Arguments.
  3646. @return Number of printed characters.
  3647. */
  3648. TRIO_PUBLIC int
  3649. trio_snprintf
  3650. TRIO_VARGS4((buffer, max, format, va_alist),
  3651. char *buffer,
  3652. size_t max,
  3653. TRIO_CONST char *format,
  3654. TRIO_VA_DECL)
  3655. {
  3656. int status;
  3657. va_list args;
  3658. assert(VALID(buffer));
  3659. assert(VALID(format));
  3660. TRIO_VA_START(args, format);
  3661. status = TrioFormat(&buffer, max > 0 ? max - 1 : 0,
  3662. TrioOutStreamStringMax, format, TRIO_VA_LIST_ADDR(args), NULL);
  3663. if (max > 0)
  3664. *buffer = NIL;
  3665. TRIO_VA_END(args);
  3666. return status;
  3667. }
  3668. /**
  3669. Print at most @p max characters to string.
  3670. @param buffer Output string.
  3671. @param max Maximum number of characters to print.
  3672. @param format Formatting string.
  3673. @param args Arguments.
  3674. @return Number of printed characters.
  3675. */
  3676. TRIO_PUBLIC int
  3677. trio_vsnprintf
  3678. TRIO_ARGS4((buffer, max, format, args),
  3679. char *buffer,
  3680. size_t max,
  3681. TRIO_CONST char *format,
  3682. va_list args)
  3683. {
  3684. int status;
  3685. assert(VALID(buffer));
  3686. assert(VALID(format));
  3687. status = TrioFormat(&buffer, max > 0 ? max - 1 : 0,
  3688. TrioOutStreamStringMax, format, TRIO_VA_LIST_ADDR(args), NULL);
  3689. if (max > 0)
  3690. *buffer = NIL;
  3691. return status;
  3692. }
  3693. /**
  3694. Print at most @p max characters to string.
  3695. @param buffer Output string.
  3696. @param max Maximum number of characters to print.
  3697. @param format Formatting string.
  3698. @param args Arguments.
  3699. @return Number of printed characters.
  3700. */
  3701. TRIO_PUBLIC int
  3702. trio_snprintfv
  3703. TRIO_ARGS4((buffer, max, format, args),
  3704. char *buffer,
  3705. size_t max,
  3706. TRIO_CONST char *format,
  3707. trio_pointer_t *args)
  3708. {
  3709. int status;
  3710. assert(VALID(buffer));
  3711. assert(VALID(format));
  3712. status = TrioFormat(&buffer, max > 0 ? max - 1 : 0,
  3713. TrioOutStreamStringMax, format, NULL, args);
  3714. if (max > 0)
  3715. *buffer = NIL;
  3716. return status;
  3717. }
  3718. /*************************************************************************
  3719. * snprintfcat
  3720. * Appends the new string to the buffer string overwriting the '\0'
  3721. * character at the end of buffer.
  3722. */
  3723. TRIO_PUBLIC int
  3724. trio_snprintfcat
  3725. TRIO_VARGS4((buffer, max, format, va_alist),
  3726. char *buffer,
  3727. size_t max,
  3728. TRIO_CONST char *format,
  3729. TRIO_VA_DECL)
  3730. {
  3731. int status;
  3732. va_list args;
  3733. size_t buf_len;
  3734. TRIO_VA_START(args, format);
  3735. assert(VALID(buffer));
  3736. assert(VALID(format));
  3737. buf_len = trio_length(buffer);
  3738. buffer = &buffer[buf_len];
  3739. status = TrioFormat(&buffer, max - 1 - buf_len,
  3740. TrioOutStreamStringMax, format, TRIO_VA_LIST_ADDR(args), NULL);
  3741. TRIO_VA_END(args);
  3742. *buffer = NIL;
  3743. return status;
  3744. }
  3745. TRIO_PUBLIC int
  3746. trio_vsnprintfcat
  3747. TRIO_ARGS4((buffer, max, format, args),
  3748. char *buffer,
  3749. size_t max,
  3750. TRIO_CONST char *format,
  3751. va_list args)
  3752. {
  3753. int status;
  3754. size_t buf_len;
  3755. assert(VALID(buffer));
  3756. assert(VALID(format));
  3757. buf_len = trio_length(buffer);
  3758. buffer = &buffer[buf_len];
  3759. status = TrioFormat(&buffer, max - 1 - buf_len,
  3760. TrioOutStreamStringMax, format, TRIO_VA_LIST_ADDR(args), NULL);
  3761. *buffer = NIL;
  3762. return status;
  3763. }
  3764. /*************************************************************************
  3765. * trio_aprintf
  3766. */
  3767. /* Deprecated */
  3768. TRIO_PUBLIC char *
  3769. trio_aprintf
  3770. TRIO_VARGS2((format, va_alist),
  3771. TRIO_CONST char *format,
  3772. TRIO_VA_DECL)
  3773. {
  3774. va_list args;
  3775. trio_string_t *info;
  3776. char *result = NULL;
  3777. assert(VALID(format));
  3778. info = trio_xstring_duplicate("");
  3779. if (info)
  3780. {
  3781. TRIO_VA_START(args, format);
  3782. (void)TrioFormat(info, 0, TrioOutStreamStringDynamic,
  3783. format, TRIO_VA_LIST_ADDR(args), NULL);
  3784. TRIO_VA_END(args);
  3785. trio_string_terminate(info);
  3786. result = trio_string_extract(info);
  3787. trio_string_destroy(info);
  3788. }
  3789. return result;
  3790. }
  3791. /* Deprecated */
  3792. TRIO_PUBLIC char *
  3793. trio_vaprintf
  3794. TRIO_ARGS2((format, args),
  3795. TRIO_CONST char *format,
  3796. va_list args)
  3797. {
  3798. trio_string_t *info;
  3799. char *result = NULL;
  3800. assert(VALID(format));
  3801. info = trio_xstring_duplicate("");
  3802. if (info)
  3803. {
  3804. (void)TrioFormat(info, 0, TrioOutStreamStringDynamic,
  3805. format, TRIO_VA_LIST_ADDR(args), NULL);
  3806. trio_string_terminate(info);
  3807. result = trio_string_extract(info);
  3808. trio_string_destroy(info);
  3809. }
  3810. return result;
  3811. }
  3812. TRIO_PUBLIC int
  3813. trio_asprintf
  3814. TRIO_VARGS3((result, format, va_alist),
  3815. char **result,
  3816. TRIO_CONST char *format,
  3817. TRIO_VA_DECL)
  3818. {
  3819. va_list args;
  3820. int status;
  3821. trio_string_t *info;
  3822. assert(VALID(format));
  3823. *result = NULL;
  3824. info = trio_xstring_duplicate("");
  3825. if (info == NULL)
  3826. {
  3827. status = TRIO_ERROR_RETURN(TRIO_ENOMEM, 0);
  3828. }
  3829. else
  3830. {
  3831. TRIO_VA_START(args, format);
  3832. status = TrioFormat(info, 0, TrioOutStreamStringDynamic,
  3833. format, TRIO_VA_LIST_ADDR(args), NULL);
  3834. TRIO_VA_END(args);
  3835. if (status >= 0)
  3836. {
  3837. trio_string_terminate(info);
  3838. *result = trio_string_extract(info);
  3839. }
  3840. trio_string_destroy(info);
  3841. }
  3842. return status;
  3843. }
  3844. TRIO_PUBLIC int
  3845. trio_vasprintf
  3846. TRIO_ARGS3((result, format, args),
  3847. char **result,
  3848. TRIO_CONST char *format,
  3849. va_list args)
  3850. {
  3851. int status;
  3852. trio_string_t *info;
  3853. assert(VALID(format));
  3854. *result = NULL;
  3855. info = trio_xstring_duplicate("");
  3856. if (info == NULL)
  3857. {
  3858. status = TRIO_ERROR_RETURN(TRIO_ENOMEM, 0);
  3859. }
  3860. else
  3861. {
  3862. status = TrioFormat(info, 0, TrioOutStreamStringDynamic,
  3863. format, TRIO_VA_LIST_ADDR(args), NULL);
  3864. if (status >= 0)
  3865. {
  3866. trio_string_terminate(info);
  3867. *result = trio_string_extract(info);
  3868. }
  3869. trio_string_destroy(info);
  3870. }
  3871. return status;
  3872. }
  3873. /** @} End of Printf documentation module */
  3874. /*************************************************************************
  3875. *
  3876. * CALLBACK
  3877. *
  3878. ************************************************************************/
  3879. #if defined(TRIO_DOCUMENTATION)
  3880. # include "doc/doc_register.h"
  3881. #endif
  3882. /**
  3883. @addtogroup UserDefined
  3884. @{
  3885. */
  3886. #if TRIO_EXTENSION
  3887. /*************************************************************************
  3888. * trio_register
  3889. */
  3890. /**
  3891. Register new user-defined specifier.
  3892. @param callback
  3893. @param name
  3894. @return Handle.
  3895. */
  3896. TRIO_PUBLIC trio_pointer_t
  3897. trio_register
  3898. TRIO_ARGS2((callback, name),
  3899. trio_callback_t callback,
  3900. TRIO_CONST char *name)
  3901. {
  3902. trio_userdef_t *def;
  3903. trio_userdef_t *prev = NULL;
  3904. if (callback == NULL)
  3905. return NULL;
  3906. if (name)
  3907. {
  3908. /* Handle built-in namespaces */
  3909. if (name[0] == ':')
  3910. {
  3911. if (trio_equal(name, ":enter"))
  3912. {
  3913. internalEnterCriticalRegion = callback;
  3914. }
  3915. else if (trio_equal(name, ":leave"))
  3916. {
  3917. internalLeaveCriticalRegion = callback;
  3918. }
  3919. return NULL;
  3920. }
  3921. /* Bail out if namespace is too long */
  3922. if (trio_length(name) >= MAX_USER_NAME)
  3923. return NULL;
  3924. /* Bail out if namespace already is registered */
  3925. def = TrioFindNamespace(name, &prev);
  3926. if (def)
  3927. return NULL;
  3928. }
  3929. def = (trio_userdef_t *)TRIO_MALLOC(sizeof(trio_userdef_t));
  3930. if (def)
  3931. {
  3932. if (internalEnterCriticalRegion)
  3933. (void)internalEnterCriticalRegion(NULL);
  3934. if (name)
  3935. {
  3936. /* Link into internal list */
  3937. if (prev == NULL)
  3938. internalUserDef = def;
  3939. else
  3940. prev->next = def;
  3941. }
  3942. /* Initialize */
  3943. def->callback = callback;
  3944. def->name = (name == NULL)
  3945. ? NULL
  3946. : trio_duplicate(name);
  3947. def->next = NULL;
  3948. if (internalLeaveCriticalRegion)
  3949. (void)internalLeaveCriticalRegion(NULL);
  3950. }
  3951. return (trio_pointer_t)def;
  3952. }
  3953. /**
  3954. Unregister an existing user-defined specifier.
  3955. @param handle
  3956. */
  3957. void
  3958. trio_unregister
  3959. TRIO_ARGS1((handle),
  3960. trio_pointer_t handle)
  3961. {
  3962. trio_userdef_t *self = (trio_userdef_t *)handle;
  3963. trio_userdef_t *def;
  3964. trio_userdef_t *prev = NULL;
  3965. assert(VALID(self));
  3966. if (self->name)
  3967. {
  3968. def = TrioFindNamespace(self->name, &prev);
  3969. if (def)
  3970. {
  3971. if (internalEnterCriticalRegion)
  3972. (void)internalEnterCriticalRegion(NULL);
  3973. if (prev == NULL)
  3974. internalUserDef = NULL;
  3975. else
  3976. prev->next = def->next;
  3977. if (internalLeaveCriticalRegion)
  3978. (void)internalLeaveCriticalRegion(NULL);
  3979. }
  3980. trio_destroy(self->name);
  3981. }
  3982. TRIO_FREE(self);
  3983. }
  3984. /*************************************************************************
  3985. * trio_get_format [public]
  3986. */
  3987. TRIO_CONST char *
  3988. trio_get_format
  3989. TRIO_ARGS1((ref),
  3990. trio_pointer_t ref)
  3991. {
  3992. #if defined(FORMAT_USER_DEFINED)
  3993. assert(((trio_reference_t *)ref)->parameter->type == FORMAT_USER_DEFINED);
  3994. #endif
  3995. return (((trio_reference_t *)ref)->parameter->user_data);
  3996. }
  3997. /*************************************************************************
  3998. * trio_get_argument [public]
  3999. */
  4000. trio_pointer_t
  4001. trio_get_argument
  4002. TRIO_ARGS1((ref),
  4003. trio_pointer_t ref)
  4004. {
  4005. #if defined(FORMAT_USER_DEFINED)
  4006. assert(((trio_reference_t *)ref)->parameter->type == FORMAT_USER_DEFINED);
  4007. #endif
  4008. return ((trio_reference_t *)ref)->parameter->data.pointer;
  4009. }
  4010. /*************************************************************************
  4011. * trio_get_width / trio_set_width [public]
  4012. */
  4013. int
  4014. trio_get_width
  4015. TRIO_ARGS1((ref),
  4016. trio_pointer_t ref)
  4017. {
  4018. return ((trio_reference_t *)ref)->parameter->width;
  4019. }
  4020. void
  4021. trio_set_width
  4022. TRIO_ARGS2((ref, width),
  4023. trio_pointer_t ref,
  4024. int width)
  4025. {
  4026. ((trio_reference_t *)ref)->parameter->width = width;
  4027. }
  4028. /*************************************************************************
  4029. * trio_get_precision / trio_set_precision [public]
  4030. */
  4031. int
  4032. trio_get_precision
  4033. TRIO_ARGS1((ref),
  4034. trio_pointer_t ref)
  4035. {
  4036. return (((trio_reference_t *)ref)->parameter->precision);
  4037. }
  4038. void
  4039. trio_set_precision
  4040. TRIO_ARGS2((ref, precision),
  4041. trio_pointer_t ref,
  4042. int precision)
  4043. {
  4044. ((trio_reference_t *)ref)->parameter->precision = precision;
  4045. }
  4046. /*************************************************************************
  4047. * trio_get_base / trio_set_base [public]
  4048. */
  4049. int
  4050. trio_get_base
  4051. TRIO_ARGS1((ref),
  4052. trio_pointer_t ref)
  4053. {
  4054. return (((trio_reference_t *)ref)->parameter->base);
  4055. }
  4056. void
  4057. trio_set_base
  4058. TRIO_ARGS2((ref, base),
  4059. trio_pointer_t ref,
  4060. int base)
  4061. {
  4062. ((trio_reference_t *)ref)->parameter->base = base;
  4063. }
  4064. /*************************************************************************
  4065. * trio_get_long / trio_set_long [public]
  4066. */
  4067. int
  4068. trio_get_long
  4069. TRIO_ARGS1((ref),
  4070. trio_pointer_t ref)
  4071. {
  4072. return (((trio_reference_t *)ref)->parameter->flags & FLAGS_LONG)
  4073. ? TRUE
  4074. : FALSE;
  4075. }
  4076. void
  4077. trio_set_long
  4078. TRIO_ARGS2((ref, is_long),
  4079. trio_pointer_t ref,
  4080. int is_long)
  4081. {
  4082. if (is_long)
  4083. ((trio_reference_t *)ref)->parameter->flags |= FLAGS_LONG;
  4084. else
  4085. ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_LONG;
  4086. }
  4087. /*************************************************************************
  4088. * trio_get_longlong / trio_set_longlong [public]
  4089. */
  4090. int
  4091. trio_get_longlong
  4092. TRIO_ARGS1((ref),
  4093. trio_pointer_t ref)
  4094. {
  4095. return (((trio_reference_t *)ref)->parameter->flags & FLAGS_QUAD)
  4096. ? TRUE
  4097. : FALSE;
  4098. }
  4099. void
  4100. trio_set_longlong
  4101. TRIO_ARGS2((ref, is_longlong),
  4102. trio_pointer_t ref,
  4103. int is_longlong)
  4104. {
  4105. if (is_longlong)
  4106. ((trio_reference_t *)ref)->parameter->flags |= FLAGS_QUAD;
  4107. else
  4108. ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_QUAD;
  4109. }
  4110. /*************************************************************************
  4111. * trio_get_longdouble / trio_set_longdouble [public]
  4112. */
  4113. int
  4114. trio_get_longdouble
  4115. TRIO_ARGS1((ref),
  4116. trio_pointer_t ref)
  4117. {
  4118. return (((trio_reference_t *)ref)->parameter->flags & FLAGS_LONGDOUBLE)
  4119. ? TRUE
  4120. : FALSE;
  4121. }
  4122. void
  4123. trio_set_longdouble
  4124. TRIO_ARGS2((ref, is_longdouble),
  4125. trio_pointer_t ref,
  4126. int is_longdouble)
  4127. {
  4128. if (is_longdouble)
  4129. ((trio_reference_t *)ref)->parameter->flags |= FLAGS_LONGDOUBLE;
  4130. else
  4131. ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_LONGDOUBLE;
  4132. }
  4133. /*************************************************************************
  4134. * trio_get_short / trio_set_short [public]
  4135. */
  4136. int
  4137. trio_get_short
  4138. TRIO_ARGS1((ref),
  4139. trio_pointer_t ref)
  4140. {
  4141. return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SHORT)
  4142. ? TRUE
  4143. : FALSE;
  4144. }
  4145. void
  4146. trio_set_short
  4147. TRIO_ARGS2((ref, is_short),
  4148. trio_pointer_t ref,
  4149. int is_short)
  4150. {
  4151. if (is_short)
  4152. ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SHORT;
  4153. else
  4154. ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SHORT;
  4155. }
  4156. /*************************************************************************
  4157. * trio_get_shortshort / trio_set_shortshort [public]
  4158. */
  4159. int
  4160. trio_get_shortshort
  4161. TRIO_ARGS1((ref),
  4162. trio_pointer_t ref)
  4163. {
  4164. return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SHORTSHORT)
  4165. ? TRUE
  4166. : FALSE;
  4167. }
  4168. void
  4169. trio_set_shortshort
  4170. TRIO_ARGS2((ref, is_shortshort),
  4171. trio_pointer_t ref,
  4172. int is_shortshort)
  4173. {
  4174. if (is_shortshort)
  4175. ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SHORTSHORT;
  4176. else
  4177. ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SHORTSHORT;
  4178. }
  4179. /*************************************************************************
  4180. * trio_get_alternative / trio_set_alternative [public]
  4181. */
  4182. int
  4183. trio_get_alternative
  4184. TRIO_ARGS1((ref),
  4185. trio_pointer_t ref)
  4186. {
  4187. return (((trio_reference_t *)ref)->parameter->flags & FLAGS_ALTERNATIVE)
  4188. ? TRUE
  4189. : FALSE;
  4190. }
  4191. void
  4192. trio_set_alternative
  4193. TRIO_ARGS2((ref, is_alternative),
  4194. trio_pointer_t ref,
  4195. int is_alternative)
  4196. {
  4197. if (is_alternative)
  4198. ((trio_reference_t *)ref)->parameter->flags |= FLAGS_ALTERNATIVE;
  4199. else
  4200. ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_ALTERNATIVE;
  4201. }
  4202. /*************************************************************************
  4203. * trio_get_alignment / trio_set_alignment [public]
  4204. */
  4205. int
  4206. trio_get_alignment
  4207. TRIO_ARGS1((ref),
  4208. trio_pointer_t ref)
  4209. {
  4210. return (((trio_reference_t *)ref)->parameter->flags & FLAGS_LEFTADJUST)
  4211. ? TRUE
  4212. : FALSE;
  4213. }
  4214. void
  4215. trio_set_alignment
  4216. TRIO_ARGS2((ref, is_leftaligned),
  4217. trio_pointer_t ref,
  4218. int is_leftaligned)
  4219. {
  4220. if (is_leftaligned)
  4221. ((trio_reference_t *)ref)->parameter->flags |= FLAGS_LEFTADJUST;
  4222. else
  4223. ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_LEFTADJUST;
  4224. }
  4225. /*************************************************************************
  4226. * trio_get_spacing /trio_set_spacing [public]
  4227. */
  4228. int
  4229. trio_get_spacing
  4230. TRIO_ARGS1((ref),
  4231. trio_pointer_t ref)
  4232. {
  4233. return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SPACE)
  4234. ? TRUE
  4235. : FALSE;
  4236. }
  4237. void
  4238. trio_set_spacing
  4239. TRIO_ARGS2((ref, is_space),
  4240. trio_pointer_t ref,
  4241. int is_space)
  4242. {
  4243. if (is_space)
  4244. ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SPACE;
  4245. else
  4246. ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SPACE;
  4247. }
  4248. /*************************************************************************
  4249. * trio_get_sign / trio_set_sign [public]
  4250. */
  4251. int
  4252. trio_get_sign
  4253. TRIO_ARGS1((ref),
  4254. trio_pointer_t ref)
  4255. {
  4256. return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SHOWSIGN)
  4257. ? TRUE
  4258. : FALSE;
  4259. }
  4260. void
  4261. trio_set_sign
  4262. TRIO_ARGS2((ref, is_sign),
  4263. trio_pointer_t ref,
  4264. int is_sign)
  4265. {
  4266. if (is_sign)
  4267. ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SHOWSIGN;
  4268. else
  4269. ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SHOWSIGN;
  4270. }
  4271. /*************************************************************************
  4272. * trio_get_padding / trio_set_padding [public]
  4273. */
  4274. int
  4275. trio_get_padding
  4276. TRIO_ARGS1((ref),
  4277. trio_pointer_t ref)
  4278. {
  4279. return (((trio_reference_t *)ref)->parameter->flags & FLAGS_NILPADDING)
  4280. ? TRUE
  4281. : FALSE;
  4282. }
  4283. void
  4284. trio_set_padding
  4285. TRIO_ARGS2((ref, is_padding),
  4286. trio_pointer_t ref,
  4287. int is_padding)
  4288. {
  4289. if (is_padding)
  4290. ((trio_reference_t *)ref)->parameter->flags |= FLAGS_NILPADDING;
  4291. else
  4292. ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_NILPADDING;
  4293. }
  4294. /*************************************************************************
  4295. * trio_get_quote / trio_set_quote [public]
  4296. */
  4297. int
  4298. trio_get_quote
  4299. TRIO_ARGS1((ref),
  4300. trio_pointer_t ref)
  4301. {
  4302. return (((trio_reference_t *)ref)->parameter->flags & FLAGS_QUOTE)
  4303. ? TRUE
  4304. : FALSE;
  4305. }
  4306. void
  4307. trio_set_quote
  4308. TRIO_ARGS2((ref, is_quote),
  4309. trio_pointer_t ref,
  4310. int is_quote)
  4311. {
  4312. if (is_quote)
  4313. ((trio_reference_t *)ref)->parameter->flags |= FLAGS_QUOTE;
  4314. else
  4315. ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_QUOTE;
  4316. }
  4317. /*************************************************************************
  4318. * trio_get_upper / trio_set_upper [public]
  4319. */
  4320. int
  4321. trio_get_upper
  4322. TRIO_ARGS1((ref),
  4323. trio_pointer_t ref)
  4324. {
  4325. return (((trio_reference_t *)ref)->parameter->flags & FLAGS_UPPER)
  4326. ? TRUE
  4327. : FALSE;
  4328. }
  4329. void
  4330. trio_set_upper
  4331. TRIO_ARGS2((ref, is_upper),
  4332. trio_pointer_t ref,
  4333. int is_upper)
  4334. {
  4335. if (is_upper)
  4336. ((trio_reference_t *)ref)->parameter->flags |= FLAGS_UPPER;
  4337. else
  4338. ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_UPPER;
  4339. }
  4340. /*************************************************************************
  4341. * trio_get_largest / trio_set_largest [public]
  4342. */
  4343. #if TRIO_C99
  4344. int
  4345. trio_get_largest
  4346. TRIO_ARGS1((ref),
  4347. trio_pointer_t ref)
  4348. {
  4349. return (((trio_reference_t *)ref)->parameter->flags & FLAGS_INTMAX_T)
  4350. ? TRUE
  4351. : FALSE;
  4352. }
  4353. void
  4354. trio_set_largest
  4355. TRIO_ARGS2((ref, is_largest),
  4356. trio_pointer_t ref,
  4357. int is_largest)
  4358. {
  4359. if (is_largest)
  4360. ((trio_reference_t *)ref)->parameter->flags |= FLAGS_INTMAX_T;
  4361. else
  4362. ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_INTMAX_T;
  4363. }
  4364. #endif
  4365. /*************************************************************************
  4366. * trio_get_ptrdiff / trio_set_ptrdiff [public]
  4367. */
  4368. int
  4369. trio_get_ptrdiff
  4370. TRIO_ARGS1((ref),
  4371. trio_pointer_t ref)
  4372. {
  4373. return (((trio_reference_t *)ref)->parameter->flags & FLAGS_PTRDIFF_T)
  4374. ? TRUE
  4375. : FALSE;
  4376. }
  4377. void
  4378. trio_set_ptrdiff
  4379. TRIO_ARGS2((ref, is_ptrdiff),
  4380. trio_pointer_t ref,
  4381. int is_ptrdiff)
  4382. {
  4383. if (is_ptrdiff)
  4384. ((trio_reference_t *)ref)->parameter->flags |= FLAGS_PTRDIFF_T;
  4385. else
  4386. ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_PTRDIFF_T;
  4387. }
  4388. /*************************************************************************
  4389. * trio_get_size / trio_set_size [public]
  4390. */
  4391. #if TRIO_C99
  4392. int
  4393. trio_get_size
  4394. TRIO_ARGS1((ref),
  4395. trio_pointer_t ref)
  4396. {
  4397. return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SIZE_T)
  4398. ? TRUE
  4399. : FALSE;
  4400. }
  4401. void
  4402. trio_set_size
  4403. TRIO_ARGS2((ref, is_size),
  4404. trio_pointer_t ref,
  4405. int is_size)
  4406. {
  4407. if (is_size)
  4408. ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SIZE_T;
  4409. else
  4410. ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SIZE_T;
  4411. }
  4412. #endif
  4413. /*************************************************************************
  4414. * trio_print_int [public]
  4415. */
  4416. void
  4417. trio_print_int
  4418. TRIO_ARGS2((ref, number),
  4419. trio_pointer_t ref,
  4420. int number)
  4421. {
  4422. trio_reference_t *self = (trio_reference_t *)ref;
  4423. TrioWriteNumber(self->data,
  4424. (trio_uintmax_t)number,
  4425. self->parameter->flags,
  4426. self->parameter->width,
  4427. self->parameter->precision,
  4428. self->parameter->base);
  4429. }
  4430. /*************************************************************************
  4431. * trio_print_uint [public]
  4432. */
  4433. void
  4434. trio_print_uint
  4435. TRIO_ARGS2((ref, number),
  4436. trio_pointer_t ref,
  4437. unsigned int number)
  4438. {
  4439. trio_reference_t *self = (trio_reference_t *)ref;
  4440. TrioWriteNumber(self->data,
  4441. (trio_uintmax_t)number,
  4442. self->parameter->flags | FLAGS_UNSIGNED,
  4443. self->parameter->width,
  4444. self->parameter->precision,
  4445. self->parameter->base);
  4446. }
  4447. /*************************************************************************
  4448. * trio_print_double [public]
  4449. */
  4450. void
  4451. trio_print_double
  4452. TRIO_ARGS2((ref, number),
  4453. trio_pointer_t ref,
  4454. double number)
  4455. {
  4456. trio_reference_t *self = (trio_reference_t *)ref;
  4457. TrioWriteDouble(self->data,
  4458. number,
  4459. self->parameter->flags,
  4460. self->parameter->width,
  4461. self->parameter->precision,
  4462. self->parameter->base);
  4463. }
  4464. /*************************************************************************
  4465. * trio_print_string [public]
  4466. */
  4467. void
  4468. trio_print_string
  4469. TRIO_ARGS2((ref, string),
  4470. trio_pointer_t ref,
  4471. char *string)
  4472. {
  4473. trio_reference_t *self = (trio_reference_t *)ref;
  4474. TrioWriteString(self->data,
  4475. string,
  4476. self->parameter->flags,
  4477. self->parameter->width,
  4478. self->parameter->precision);
  4479. }
  4480. /*************************************************************************
  4481. * trio_print_ref [public]
  4482. */
  4483. int
  4484. trio_print_ref
  4485. TRIO_VARGS3((ref, format, va_alist),
  4486. trio_pointer_t ref,
  4487. TRIO_CONST char *format,
  4488. TRIO_VA_DECL)
  4489. {
  4490. int status;
  4491. va_list arglist;
  4492. assert(VALID(format));
  4493. TRIO_VA_START(arglist, format);
  4494. status = TrioFormatRef((trio_reference_t *)ref, format, TRIO_VA_LIST_ADDR(arglist), NULL);
  4495. TRIO_VA_END(arglist);
  4496. return status;
  4497. }
  4498. /*************************************************************************
  4499. * trio_vprint_ref [public]
  4500. */
  4501. int
  4502. trio_vprint_ref
  4503. TRIO_ARGS3((ref, format, arglist),
  4504. trio_pointer_t ref,
  4505. TRIO_CONST char *format,
  4506. va_list arglist)
  4507. {
  4508. assert(VALID(format));
  4509. return TrioFormatRef((trio_reference_t *)ref, format, TRIO_VA_LIST_ADDR(arglist), NULL);
  4510. }
  4511. /*************************************************************************
  4512. * trio_printv_ref [public]
  4513. */
  4514. int
  4515. trio_printv_ref
  4516. TRIO_ARGS3((ref, format, argarray),
  4517. trio_pointer_t ref,
  4518. TRIO_CONST char *format,
  4519. trio_pointer_t *argarray)
  4520. {
  4521. assert(VALID(format));
  4522. return TrioFormatRef((trio_reference_t *)ref, format, NULL, argarray);
  4523. }
  4524. #endif /* TRIO_EXTENSION */
  4525. /*************************************************************************
  4526. * trio_print_pointer [public]
  4527. */
  4528. void
  4529. trio_print_pointer
  4530. TRIO_ARGS2((ref, pointer),
  4531. trio_pointer_t ref,
  4532. trio_pointer_t pointer)
  4533. {
  4534. trio_reference_t *self = (trio_reference_t *)ref;
  4535. trio_flags_t flags;
  4536. trio_uintmax_t number;
  4537. if (NULL == pointer)
  4538. {
  4539. TRIO_CONST char *string = internalNullString;
  4540. while (*string)
  4541. self->data->OutStream(self->data, *string++);
  4542. }
  4543. else
  4544. {
  4545. /*
  4546. * The subtraction of the null pointer is a workaround
  4547. * to avoid a compiler warning. The performance overhead
  4548. * is negligible (and likely to be removed by an
  4549. * optimizing compiler). The (char *) casting is done
  4550. * to please ANSI C++.
  4551. */
  4552. number = (trio_uintmax_t)((char *)pointer - (char *)0);
  4553. /* Shrink to size of pointer */
  4554. number &= (trio_uintmax_t)-1;
  4555. flags = self->parameter->flags;
  4556. flags |= (FLAGS_UNSIGNED | FLAGS_ALTERNATIVE |
  4557. FLAGS_NILPADDING);
  4558. TrioWriteNumber(self->data,
  4559. number,
  4560. flags,
  4561. POINTER_WIDTH,
  4562. NO_PRECISION,
  4563. BASE_HEX);
  4564. }
  4565. }
  4566. /** @} End of UserDefined documentation module */
  4567. /*************************************************************************
  4568. *
  4569. * LOCALES
  4570. *
  4571. ************************************************************************/
  4572. /*************************************************************************
  4573. * trio_locale_set_decimal_point
  4574. *
  4575. * Decimal point can only be one character. The input argument is a
  4576. * string to enable multibyte characters. At most MB_LEN_MAX characters
  4577. * will be used.
  4578. */
  4579. TRIO_PUBLIC void
  4580. trio_locale_set_decimal_point
  4581. TRIO_ARGS1((decimalPoint),
  4582. char *decimalPoint)
  4583. {
  4584. #if defined(USE_LOCALE)
  4585. if (NULL == internalLocaleValues)
  4586. {
  4587. TrioSetLocale();
  4588. }
  4589. #endif
  4590. internalDecimalPointLength = trio_length(decimalPoint);
  4591. if (internalDecimalPointLength == 1)
  4592. {
  4593. internalDecimalPoint = *decimalPoint;
  4594. }
  4595. else
  4596. {
  4597. internalDecimalPoint = NIL;
  4598. trio_copy_max(internalDecimalPointString,
  4599. sizeof(internalDecimalPointString),
  4600. decimalPoint);
  4601. }
  4602. }
  4603. /*************************************************************************
  4604. * trio_locale_set_thousand_separator
  4605. *
  4606. * See trio_locale_set_decimal_point
  4607. */
  4608. TRIO_PUBLIC void
  4609. trio_locale_set_thousand_separator
  4610. TRIO_ARGS1((thousandSeparator),
  4611. char *thousandSeparator)
  4612. {
  4613. #if defined(USE_LOCALE)
  4614. if (NULL == internalLocaleValues)
  4615. {
  4616. TrioSetLocale();
  4617. }
  4618. #endif
  4619. trio_copy_max(internalThousandSeparator,
  4620. sizeof(internalThousandSeparator),
  4621. thousandSeparator);
  4622. internalThousandSeparatorLength = trio_length(internalThousandSeparator);
  4623. }
  4624. /*************************************************************************
  4625. * trio_locale_set_grouping
  4626. *
  4627. * Array of bytes. Reversed order.
  4628. *
  4629. * CHAR_MAX : No further grouping
  4630. * 0 : Repeat last group for the remaining digits (not necessary
  4631. * as C strings are zero-terminated)
  4632. * n : Set current group to n
  4633. *
  4634. * Same order as the grouping attribute in LC_NUMERIC.
  4635. */
  4636. TRIO_PUBLIC void
  4637. trio_locale_set_grouping
  4638. TRIO_ARGS1((grouping),
  4639. char *grouping)
  4640. {
  4641. #if defined(USE_LOCALE)
  4642. if (NULL == internalLocaleValues)
  4643. {
  4644. TrioSetLocale();
  4645. }
  4646. #endif
  4647. trio_copy_max(internalGrouping,
  4648. sizeof(internalGrouping),
  4649. grouping);
  4650. }
  4651. /*************************************************************************
  4652. *
  4653. * SCANNING
  4654. *
  4655. ************************************************************************/
  4656. /*************************************************************************
  4657. * TrioSkipWhitespaces
  4658. */
  4659. TRIO_PRIVATE int
  4660. TrioSkipWhitespaces
  4661. TRIO_ARGS1((self),
  4662. trio_class_t *self)
  4663. {
  4664. int ch;
  4665. ch = self->current;
  4666. while (isspace(ch))
  4667. {
  4668. self->InStream(self, &ch);
  4669. }
  4670. return ch;
  4671. }
  4672. /*************************************************************************
  4673. * TrioGetCollation
  4674. */
  4675. #if TRIO_EXTENSION
  4676. TRIO_PRIVATE void
  4677. TrioGetCollation(TRIO_NOARGS)
  4678. {
  4679. int i;
  4680. int j;
  4681. int k;
  4682. char first[2];
  4683. char second[2];
  4684. /* This is computationally expensive */
  4685. first[1] = NIL;
  4686. second[1] = NIL;
  4687. for (i = 0; i < MAX_CHARACTER_CLASS; i++)
  4688. {
  4689. k = 0;
  4690. first[0] = (char)i;
  4691. for (j = 0; j < MAX_CHARACTER_CLASS; j++)
  4692. {
  4693. second[0] = (char)j;
  4694. if (trio_equal_locale(first, second))
  4695. internalCollationArray[i][k++] = (char)j;
  4696. }
  4697. internalCollationArray[i][k] = NIL;
  4698. }
  4699. }
  4700. #endif
  4701. /*************************************************************************
  4702. * TrioGetCharacterClass
  4703. *
  4704. * FIXME:
  4705. * multibyte
  4706. */
  4707. TRIO_PRIVATE int
  4708. TrioGetCharacterClass
  4709. TRIO_ARGS4((format, indexPointer, flagsPointer, characterclass),
  4710. TRIO_CONST char *format,
  4711. int *indexPointer,
  4712. trio_flags_t *flagsPointer,
  4713. int *characterclass)
  4714. {
  4715. int index = *indexPointer;
  4716. int i;
  4717. char ch;
  4718. char range_begin;
  4719. char range_end;
  4720. *flagsPointer &= ~FLAGS_EXCLUDE;
  4721. if (format[index] == QUALIFIER_CIRCUMFLEX)
  4722. {
  4723. *flagsPointer |= FLAGS_EXCLUDE;
  4724. index++;
  4725. }
  4726. /*
  4727. * If the ungroup character is at the beginning of the scanlist,
  4728. * it will be part of the class, and a second ungroup character
  4729. * must follow to end the group.
  4730. */
  4731. if (format[index] == SPECIFIER_UNGROUP)
  4732. {
  4733. characterclass[(int)SPECIFIER_UNGROUP]++;
  4734. index++;
  4735. }
  4736. /*
  4737. * Minus is used to specify ranges. To include minus in the class,
  4738. * it must be at the beginning of the list
  4739. */
  4740. if (format[index] == QUALIFIER_MINUS)
  4741. {
  4742. characterclass[(int)QUALIFIER_MINUS]++;
  4743. index++;
  4744. }
  4745. /* Collect characters */
  4746. for (ch = format[index];
  4747. (ch != SPECIFIER_UNGROUP) && (ch != NIL);
  4748. ch = format[++index])
  4749. {
  4750. switch (ch)
  4751. {
  4752. case QUALIFIER_MINUS: /* Scanlist ranges */
  4753. /*
  4754. * Both C99 and UNIX98 describes ranges as implementation-
  4755. * defined.
  4756. *
  4757. * We support the following behaviour (although this may
  4758. * change as we become wiser)
  4759. * - only increasing ranges, ie. [a-b] but not [b-a]
  4760. * - transitive ranges, ie. [a-b-c] == [a-c]
  4761. * - trailing minus, ie. [a-] is interpreted as an 'a'
  4762. * and a '-'
  4763. * - duplicates (although we can easily convert these
  4764. * into errors)
  4765. */
  4766. range_begin = format[index - 1];
  4767. range_end = format[++index];
  4768. if (range_end == SPECIFIER_UNGROUP)
  4769. {
  4770. /* Trailing minus is included */
  4771. characterclass[(int)ch]++;
  4772. ch = range_end;
  4773. break; /* for */
  4774. }
  4775. if (range_end == NIL)
  4776. return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
  4777. if (range_begin > range_end)
  4778. return TRIO_ERROR_RETURN(TRIO_ERANGE, index);
  4779. for (i = (int)range_begin; i <= (int)range_end; i++)
  4780. characterclass[i]++;
  4781. ch = range_end;
  4782. break;
  4783. #if TRIO_EXTENSION
  4784. case SPECIFIER_GROUP:
  4785. switch (format[index + 1])
  4786. {
  4787. case QUALIFIER_DOT: /* Collating symbol */
  4788. /*
  4789. * FIXME: This will be easier to implement when multibyte
  4790. * characters have been implemented. Until now, we ignore
  4791. * this feature.
  4792. */
  4793. for (i = index + 2; ; i++)
  4794. {
  4795. if (format[i] == NIL)
  4796. /* Error in syntax */
  4797. return -1;
  4798. else if (format[i] == QUALIFIER_DOT)
  4799. break; /* for */
  4800. }
  4801. if (format[++i] != SPECIFIER_UNGROUP)
  4802. return -1;
  4803. index = i;
  4804. break;
  4805. case QUALIFIER_EQUAL: /* Equivalence class expressions */
  4806. {
  4807. unsigned int j;
  4808. unsigned int k;
  4809. if (internalCollationUnconverted)
  4810. {
  4811. /* Lazy evaluation of collation array */
  4812. TrioGetCollation();
  4813. internalCollationUnconverted = FALSE;
  4814. }
  4815. for (i = index + 2; ; i++)
  4816. {
  4817. if (format[i] == NIL)
  4818. /* Error in syntax */
  4819. return -1;
  4820. else if (format[i] == QUALIFIER_EQUAL)
  4821. break; /* for */
  4822. else
  4823. {
  4824. /* Mark any equivalent character */
  4825. k = (unsigned int)format[i];
  4826. for (j = 0; internalCollationArray[k][j] != NIL; j++)
  4827. characterclass[(int)internalCollationArray[k][j]]++;
  4828. }
  4829. }
  4830. if (format[++i] != SPECIFIER_UNGROUP)
  4831. return -1;
  4832. index = i;
  4833. }
  4834. break;
  4835. case QUALIFIER_COLON: /* Character class expressions */
  4836. if (trio_equal_max(CLASS_ALNUM, sizeof(CLASS_ALNUM) - 1,
  4837. &format[index]))
  4838. {
  4839. for (i = 0; i < MAX_CHARACTER_CLASS; i++)
  4840. if (isalnum(i))
  4841. characterclass[i]++;
  4842. index += sizeof(CLASS_ALNUM) - 1;
  4843. }
  4844. else if (trio_equal_max(CLASS_ALPHA, sizeof(CLASS_ALPHA) - 1,
  4845. &format[index]))
  4846. {
  4847. for (i = 0; i < MAX_CHARACTER_CLASS; i++)
  4848. if (isalpha(i))
  4849. characterclass[i]++;
  4850. index += sizeof(CLASS_ALPHA) - 1;
  4851. }
  4852. else if (trio_equal_max(CLASS_CNTRL, sizeof(CLASS_CNTRL) - 1,
  4853. &format[index]))
  4854. {
  4855. for (i = 0; i < MAX_CHARACTER_CLASS; i++)
  4856. if (iscntrl(i))
  4857. characterclass[i]++;
  4858. index += sizeof(CLASS_CNTRL) - 1;
  4859. }
  4860. else if (trio_equal_max(CLASS_DIGIT, sizeof(CLASS_DIGIT) - 1,
  4861. &format[index]))
  4862. {
  4863. for (i = 0; i < MAX_CHARACTER_CLASS; i++)
  4864. if (isdigit(i))
  4865. characterclass[i]++;
  4866. index += sizeof(CLASS_DIGIT) - 1;
  4867. }
  4868. else if (trio_equal_max(CLASS_GRAPH, sizeof(CLASS_GRAPH) - 1,
  4869. &format[index]))
  4870. {
  4871. for (i = 0; i < MAX_CHARACTER_CLASS; i++)
  4872. if (isgraph(i))
  4873. characterclass[i]++;
  4874. index += sizeof(CLASS_GRAPH) - 1;
  4875. }
  4876. else if (trio_equal_max(CLASS_LOWER, sizeof(CLASS_LOWER) - 1,
  4877. &format[index]))
  4878. {
  4879. for (i = 0; i < MAX_CHARACTER_CLASS; i++)
  4880. if (islower(i))
  4881. characterclass[i]++;
  4882. index += sizeof(CLASS_LOWER) - 1;
  4883. }
  4884. else if (trio_equal_max(CLASS_PRINT, sizeof(CLASS_PRINT) - 1,
  4885. &format[index]))
  4886. {
  4887. for (i = 0; i < MAX_CHARACTER_CLASS; i++)
  4888. if (isprint(i))
  4889. characterclass[i]++;
  4890. index += sizeof(CLASS_PRINT) - 1;
  4891. }
  4892. else if (trio_equal_max(CLASS_PUNCT, sizeof(CLASS_PUNCT) - 1,
  4893. &format[index]))
  4894. {
  4895. for (i = 0; i < MAX_CHARACTER_CLASS; i++)
  4896. if (ispunct(i))
  4897. characterclass[i]++;
  4898. index += sizeof(CLASS_PUNCT) - 1;
  4899. }
  4900. else if (trio_equal_max(CLASS_SPACE, sizeof(CLASS_SPACE) - 1,
  4901. &format[index]))
  4902. {
  4903. for (i = 0; i < MAX_CHARACTER_CLASS; i++)
  4904. if (isspace(i))
  4905. characterclass[i]++;
  4906. index += sizeof(CLASS_SPACE) - 1;
  4907. }
  4908. else if (trio_equal_max(CLASS_UPPER, sizeof(CLASS_UPPER) - 1,
  4909. &format[index]))
  4910. {
  4911. for (i = 0; i < MAX_CHARACTER_CLASS; i++)
  4912. if (isupper(i))
  4913. characterclass[i]++;
  4914. index += sizeof(CLASS_UPPER) - 1;
  4915. }
  4916. else if (trio_equal_max(CLASS_XDIGIT, sizeof(CLASS_XDIGIT) - 1,
  4917. &format[index]))
  4918. {
  4919. for (i = 0; i < MAX_CHARACTER_CLASS; i++)
  4920. if (isxdigit(i))
  4921. characterclass[i]++;
  4922. index += sizeof(CLASS_XDIGIT) - 1;
  4923. }
  4924. else
  4925. {
  4926. characterclass[(int)ch]++;
  4927. }
  4928. break;
  4929. default:
  4930. characterclass[(int)ch]++;
  4931. break;
  4932. }
  4933. break;
  4934. #endif /* TRIO_EXTENSION */
  4935. default:
  4936. characterclass[(int)ch]++;
  4937. break;
  4938. }
  4939. }
  4940. return 0;
  4941. }
  4942. /*************************************************************************
  4943. * TrioReadNumber
  4944. *
  4945. * We implement our own number conversion in preference of strtol and
  4946. * strtoul, because we must handle 'long long' and thousand separators.
  4947. */
  4948. TRIO_PRIVATE BOOLEAN_T
  4949. TrioReadNumber
  4950. TRIO_ARGS5((self, target, flags, width, base),
  4951. trio_class_t *self,
  4952. trio_uintmax_t *target,
  4953. trio_flags_t flags,
  4954. int width,
  4955. int base)
  4956. {
  4957. trio_uintmax_t number = 0;
  4958. int digit;
  4959. int count;
  4960. BOOLEAN_T isNegative = FALSE;
  4961. BOOLEAN_T gotNumber = FALSE;
  4962. int j;
  4963. assert(VALID(self));
  4964. assert(VALID(self->InStream));
  4965. assert((base >= MIN_BASE && base <= MAX_BASE) || (base == NO_BASE));
  4966. if (internalDigitsUnconverted)
  4967. {
  4968. /* Lazy evaluation of digits array */
  4969. memset(internalDigitArray, -1, sizeof(internalDigitArray));
  4970. for (j = 0; j < (int)sizeof(internalDigitsLower) - 1; j++)
  4971. {
  4972. internalDigitArray[(int)internalDigitsLower[j]] = j;
  4973. internalDigitArray[(int)internalDigitsUpper[j]] = j;
  4974. }
  4975. internalDigitsUnconverted = FALSE;
  4976. }
  4977. TrioSkipWhitespaces(self);
  4978. if (!(flags & FLAGS_UNSIGNED))
  4979. {
  4980. /* Leading sign */
  4981. if (self->current == '+')
  4982. {
  4983. self->InStream(self, NULL);
  4984. }
  4985. else if (self->current == '-')
  4986. {
  4987. self->InStream(self, NULL);
  4988. isNegative = TRUE;
  4989. }
  4990. }
  4991. count = self->processed;
  4992. if (flags & FLAGS_ALTERNATIVE)
  4993. {
  4994. switch (base)
  4995. {
  4996. case NO_BASE:
  4997. case BASE_OCTAL:
  4998. case BASE_HEX:
  4999. case BASE_BINARY:
  5000. if (self->current == '0')
  5001. {
  5002. self->InStream(self, NULL);
  5003. if (self->current)
  5004. {
  5005. if ((base == BASE_HEX) &&
  5006. (trio_to_upper(self->current) == 'X'))
  5007. {
  5008. self->InStream(self, NULL);
  5009. }
  5010. else if ((base == BASE_BINARY) &&
  5011. (trio_to_upper(self->current) == 'B'))
  5012. {
  5013. self->InStream(self, NULL);
  5014. }
  5015. }
  5016. }
  5017. else
  5018. return FALSE;
  5019. break;
  5020. default:
  5021. break;
  5022. }
  5023. }
  5024. while (((width == NO_WIDTH) || (self->processed - count < width)) &&
  5025. (! ((self->current == EOF) || isspace(self->current))))
  5026. {
  5027. if (isascii(self->current))
  5028. {
  5029. digit = internalDigitArray[self->current];
  5030. /* Abort if digit is not allowed in the specified base */
  5031. if ((digit == -1) || (digit >= base))
  5032. break;
  5033. }
  5034. else if (flags & FLAGS_QUOTE)
  5035. {
  5036. /* Compare with thousands separator */
  5037. for (j = 0; internalThousandSeparator[j] && self->current; j++)
  5038. {
  5039. if (internalThousandSeparator[j] != self->current)
  5040. break;
  5041. self->InStream(self, NULL);
  5042. }
  5043. if (internalThousandSeparator[j])
  5044. break; /* Mismatch */
  5045. else
  5046. continue; /* Match */
  5047. }
  5048. else
  5049. break;
  5050. number *= base;
  5051. number += digit;
  5052. gotNumber = TRUE; /* we need at least one digit */
  5053. self->InStream(self, NULL);
  5054. }
  5055. /* Was anything read at all? */
  5056. if (!gotNumber)
  5057. return FALSE;
  5058. if (target)
  5059. *target = (isNegative) ? -((trio_intmax_t)number) : number;
  5060. return TRUE;
  5061. }
  5062. /*************************************************************************
  5063. * TrioReadChar
  5064. */
  5065. TRIO_PRIVATE int
  5066. TrioReadChar
  5067. TRIO_ARGS4((self, target, flags, width),
  5068. trio_class_t *self,
  5069. char *target,
  5070. trio_flags_t flags,
  5071. int width)
  5072. {
  5073. int i;
  5074. char ch;
  5075. trio_uintmax_t number;
  5076. assert(VALID(self));
  5077. assert(VALID(self->InStream));
  5078. for (i = 0;
  5079. (self->current != EOF) && (i < width);
  5080. i++)
  5081. {
  5082. ch = (char)self->current;
  5083. self->InStream(self, NULL);
  5084. if ((flags & FLAGS_ALTERNATIVE) && (ch == CHAR_BACKSLASH))
  5085. {
  5086. switch (self->current)
  5087. {
  5088. case '\\': ch = '\\'; break;
  5089. case 'a': ch = '\007'; break;
  5090. case 'b': ch = '\b'; break;
  5091. case 'f': ch = '\f'; break;
  5092. case 'n': ch = '\n'; break;
  5093. case 'r': ch = '\r'; break;
  5094. case 't': ch = '\t'; break;
  5095. case 'v': ch = '\v'; break;
  5096. default:
  5097. if (isdigit(self->current))
  5098. {
  5099. /* Read octal number */
  5100. if (!TrioReadNumber(self, &number, 0, 3, BASE_OCTAL))
  5101. return 0;
  5102. ch = (char)number;
  5103. }
  5104. else if (trio_to_upper(self->current) == 'X')
  5105. {
  5106. /* Read hexadecimal number */
  5107. self->InStream(self, NULL);
  5108. if (!TrioReadNumber(self, &number, 0, 2, BASE_HEX))
  5109. return 0;
  5110. ch = (char)number;
  5111. }
  5112. else
  5113. {
  5114. ch = (char)self->current;
  5115. }
  5116. break;
  5117. }
  5118. }
  5119. if (target)
  5120. target[i] = ch;
  5121. }
  5122. return i + 1;
  5123. }
  5124. /*************************************************************************
  5125. * TrioReadString
  5126. */
  5127. TRIO_PRIVATE BOOLEAN_T
  5128. TrioReadString
  5129. TRIO_ARGS4((self, target, flags, width),
  5130. trio_class_t *self,
  5131. char *target,
  5132. trio_flags_t flags,
  5133. int width)
  5134. {
  5135. int i;
  5136. assert(VALID(self));
  5137. assert(VALID(self->InStream));
  5138. TrioSkipWhitespaces(self);
  5139. /*
  5140. * Continue until end of string is reached, a whitespace is encountered,
  5141. * or width is exceeded
  5142. */
  5143. for (i = 0;
  5144. ((width == NO_WIDTH) || (i < width)) &&
  5145. (! ((self->current == EOF) || isspace(self->current)));
  5146. i++)
  5147. {
  5148. if (TrioReadChar(self, (target ? &target[i] : 0), flags, 1) == 0)
  5149. break; /* for */
  5150. }
  5151. if (target)
  5152. target[i] = NIL;
  5153. return TRUE;
  5154. }
  5155. /*************************************************************************
  5156. * TrioReadWideChar
  5157. */
  5158. #if TRIO_WIDECHAR
  5159. TRIO_PRIVATE int
  5160. TrioReadWideChar
  5161. TRIO_ARGS4((self, target, flags, width),
  5162. trio_class_t *self,
  5163. trio_wchar_t *target,
  5164. trio_flags_t flags,
  5165. int width)
  5166. {
  5167. int i;
  5168. int j;
  5169. int size;
  5170. int amount = 0;
  5171. trio_wchar_t wch;
  5172. char buffer[MB_LEN_MAX + 1];
  5173. assert(VALID(self));
  5174. assert(VALID(self->InStream));
  5175. for (i = 0;
  5176. (self->current != EOF) && (i < width);
  5177. i++)
  5178. {
  5179. if (isascii(self->current))
  5180. {
  5181. if (TrioReadChar(self, buffer, flags, 1) == 0)
  5182. return 0;
  5183. buffer[1] = NIL;
  5184. }
  5185. else
  5186. {
  5187. /*
  5188. * Collect a multibyte character, by enlarging buffer until
  5189. * it contains a fully legal multibyte character, or the
  5190. * buffer is full.
  5191. */
  5192. j = 0;
  5193. do
  5194. {
  5195. buffer[j++] = (char)self->current;
  5196. buffer[j] = NIL;
  5197. self->InStream(self, NULL);
  5198. }
  5199. while ((j < (int)sizeof(buffer)) && (mblen(buffer, (size_t)j) != j));
  5200. }
  5201. if (target)
  5202. {
  5203. size = mbtowc(&wch, buffer, sizeof(buffer));
  5204. if (size > 0)
  5205. target[i] = wch;
  5206. }
  5207. amount += size;
  5208. self->InStream(self, NULL);
  5209. }
  5210. return amount;
  5211. }
  5212. #endif /* TRIO_WIDECHAR */
  5213. /*************************************************************************
  5214. * TrioReadWideString
  5215. */
  5216. #if TRIO_WIDECHAR
  5217. TRIO_PRIVATE BOOLEAN_T
  5218. TrioReadWideString
  5219. TRIO_ARGS4((self, target, flags, width),
  5220. trio_class_t *self,
  5221. trio_wchar_t *target,
  5222. trio_flags_t flags,
  5223. int width)
  5224. {
  5225. int i;
  5226. int size;
  5227. assert(VALID(self));
  5228. assert(VALID(self->InStream));
  5229. TrioSkipWhitespaces(self);
  5230. #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
  5231. (void)mblen(NULL, 0);
  5232. #endif
  5233. /*
  5234. * Continue until end of string is reached, a whitespace is encountered,
  5235. * or width is exceeded
  5236. */
  5237. for (i = 0;
  5238. ((width == NO_WIDTH) || (i < width)) &&
  5239. (! ((self->current == EOF) || isspace(self->current)));
  5240. )
  5241. {
  5242. size = TrioReadWideChar(self, &target[i], flags, 1);
  5243. if (size == 0)
  5244. break; /* for */
  5245. i += size;
  5246. }
  5247. if (target)
  5248. target[i] = WCONST('\0');
  5249. return TRUE;
  5250. }
  5251. #endif /* TRIO_WIDECHAR */
  5252. /*************************************************************************
  5253. * TrioReadGroup
  5254. *
  5255. * FIXME: characterclass does not work with multibyte characters
  5256. */
  5257. TRIO_PRIVATE BOOLEAN_T
  5258. TrioReadGroup
  5259. TRIO_ARGS5((self, target, characterclass, flags, width),
  5260. trio_class_t *self,
  5261. char *target,
  5262. int *characterclass,
  5263. trio_flags_t flags,
  5264. int width)
  5265. {
  5266. int ch;
  5267. int i;
  5268. assert(VALID(self));
  5269. assert(VALID(self->InStream));
  5270. ch = self->current;
  5271. for (i = 0;
  5272. ((width == NO_WIDTH) || (i < width)) &&
  5273. (! ((ch == EOF) ||
  5274. (((flags & FLAGS_EXCLUDE) != 0) ^ (characterclass[ch] == 0))));
  5275. i++)
  5276. {
  5277. if (target)
  5278. target[i] = (char)ch;
  5279. self->InStream(self, &ch);
  5280. }
  5281. if (target)
  5282. target[i] = NIL;
  5283. return TRUE;
  5284. }
  5285. /*************************************************************************
  5286. * TrioReadDouble
  5287. *
  5288. * FIXME:
  5289. * add long double
  5290. * handle base
  5291. */
  5292. TRIO_PRIVATE BOOLEAN_T
  5293. TrioReadDouble
  5294. TRIO_ARGS4((self, target, flags, width),
  5295. trio_class_t *self,
  5296. trio_pointer_t target,
  5297. trio_flags_t flags,
  5298. int width)
  5299. {
  5300. int ch;
  5301. char doubleString[512];
  5302. int index = 0;
  5303. int start;
  5304. int j;
  5305. BOOLEAN_T isHex = FALSE;
  5306. doubleString[0] = 0;
  5307. if ((width == NO_WIDTH) || (width > (int)sizeof(doubleString) - 1))
  5308. width = sizeof(doubleString) - 1;
  5309. TrioSkipWhitespaces(self);
  5310. /*
  5311. * Read entire double number from stream. trio_to_double requires
  5312. * a string as input, but InStream can be anything, so we have to
  5313. * collect all characters.
  5314. */
  5315. ch = self->current;
  5316. if ((ch == '+') || (ch == '-'))
  5317. {
  5318. doubleString[index++] = (char)ch;
  5319. self->InStream(self, &ch);
  5320. width--;
  5321. }
  5322. start = index;
  5323. switch (ch)
  5324. {
  5325. case 'n':
  5326. case 'N':
  5327. /* Not-a-number */
  5328. if (index != 0)
  5329. break;
  5330. /* FALLTHROUGH */
  5331. case 'i':
  5332. case 'I':
  5333. /* Infinity */
  5334. while (isalpha(ch) && (index - start < width))
  5335. {
  5336. doubleString[index++] = (char)ch;
  5337. self->InStream(self, &ch);
  5338. }
  5339. doubleString[index] = NIL;
  5340. /* Case insensitive string comparison */
  5341. if (trio_equal(&doubleString[start], INFINITE_UPPER) ||
  5342. trio_equal(&doubleString[start], LONG_INFINITE_UPPER))
  5343. {
  5344. if (flags & FLAGS_LONGDOUBLE)
  5345. {
  5346. if ((start == 1) && (doubleString[0] == '-'))
  5347. {
  5348. *((trio_long_double_t *)target) = trio_ninf();
  5349. }
  5350. else
  5351. {
  5352. *((trio_long_double_t *)target) = trio_pinf();
  5353. }
  5354. }
  5355. else
  5356. {
  5357. if ((start == 1) && (doubleString[0] == '-'))
  5358. {
  5359. *((double *)target) = trio_ninf();
  5360. }
  5361. else
  5362. {
  5363. *((double *)target) = trio_pinf();
  5364. }
  5365. }
  5366. return TRUE;
  5367. }
  5368. if (trio_equal(doubleString, NAN_UPPER))
  5369. {
  5370. /* NaN must not have a preceding + nor - */
  5371. if (flags & FLAGS_LONGDOUBLE)
  5372. {
  5373. *((trio_long_double_t *)target) = trio_nan();
  5374. }
  5375. else
  5376. {
  5377. *((double *)target) = trio_nan();
  5378. }
  5379. return TRUE;
  5380. }
  5381. return FALSE;
  5382. case '0':
  5383. doubleString[index++] = (char)ch;
  5384. self->InStream(self, &ch);
  5385. if (trio_to_upper(ch) == 'X')
  5386. {
  5387. isHex = TRUE;
  5388. doubleString[index++] = (char)ch;
  5389. self->InStream(self, &ch);
  5390. }
  5391. break;
  5392. default:
  5393. break;
  5394. }
  5395. while ((ch != EOF) && (index - start < width))
  5396. {
  5397. /* Integer part */
  5398. if (isHex ? isxdigit(ch) : isdigit(ch))
  5399. {
  5400. doubleString[index++] = (char)ch;
  5401. self->InStream(self, &ch);
  5402. }
  5403. else if (flags & FLAGS_QUOTE)
  5404. {
  5405. /* Compare with thousands separator */
  5406. for (j = 0; internalThousandSeparator[j] && self->current; j++)
  5407. {
  5408. if (internalThousandSeparator[j] != self->current)
  5409. break;
  5410. self->InStream(self, &ch);
  5411. }
  5412. if (internalThousandSeparator[j])
  5413. break; /* Mismatch */
  5414. else
  5415. continue; /* Match */
  5416. }
  5417. else
  5418. break; /* while */
  5419. }
  5420. if (ch == '.')
  5421. {
  5422. /* Decimal part */
  5423. doubleString[index++] = (char)ch;
  5424. self->InStream(self, &ch);
  5425. while ((isHex ? isxdigit(ch) : isdigit(ch)) &&
  5426. (index - start < width))
  5427. {
  5428. doubleString[index++] = (char)ch;
  5429. self->InStream(self, &ch);
  5430. }
  5431. if (isHex ? (trio_to_upper(ch) == 'P') : (trio_to_upper(ch) == 'E'))
  5432. {
  5433. /* Exponent */
  5434. doubleString[index++] = (char)ch;
  5435. self->InStream(self, &ch);
  5436. if ((ch == '+') || (ch == '-'))
  5437. {
  5438. doubleString[index++] = (char)ch;
  5439. self->InStream(self, &ch);
  5440. }
  5441. while (isdigit(ch) && (index - start < width))
  5442. {
  5443. doubleString[index++] = (char)ch;
  5444. self->InStream(self, &ch);
  5445. }
  5446. }
  5447. }
  5448. if ((index == start) || (*doubleString == NIL))
  5449. return FALSE;
  5450. doubleString[index] = 0;
  5451. if (flags & FLAGS_LONGDOUBLE)
  5452. {
  5453. *((trio_long_double_t *)target) = trio_to_long_double(doubleString, NULL);
  5454. }
  5455. else
  5456. {
  5457. *((double *)target) = trio_to_double(doubleString, NULL);
  5458. }
  5459. return TRUE;
  5460. }
  5461. /*************************************************************************
  5462. * TrioReadPointer
  5463. */
  5464. TRIO_PRIVATE BOOLEAN_T
  5465. TrioReadPointer
  5466. TRIO_ARGS3((self, target, flags),
  5467. trio_class_t *self,
  5468. trio_pointer_t *target,
  5469. trio_flags_t flags)
  5470. {
  5471. trio_uintmax_t number;
  5472. char buffer[sizeof(internalNullString)];
  5473. flags |= (FLAGS_UNSIGNED | FLAGS_ALTERNATIVE | FLAGS_NILPADDING);
  5474. if (TrioReadNumber(self,
  5475. &number,
  5476. flags,
  5477. POINTER_WIDTH,
  5478. BASE_HEX))
  5479. {
  5480. /*
  5481. * The strange assignment of number is a workaround for a compiler
  5482. * warning
  5483. */
  5484. if (target)
  5485. *target = (char *)0 + number;
  5486. return TRUE;
  5487. }
  5488. else if (TrioReadString(self,
  5489. (flags & FLAGS_IGNORE)
  5490. ? NULL
  5491. : buffer,
  5492. 0,
  5493. sizeof(internalNullString) - 1))
  5494. {
  5495. if (trio_equal_case(buffer, internalNullString))
  5496. {
  5497. if (target)
  5498. *target = NULL;
  5499. return TRUE;
  5500. }
  5501. }
  5502. return FALSE;
  5503. }
  5504. /*************************************************************************
  5505. * TrioScanProcess
  5506. */
  5507. TRIO_PRIVATE int
  5508. TrioScanProcess
  5509. TRIO_ARGS3((data, format, parameters),
  5510. trio_class_t *data,
  5511. TRIO_CONST char *format,
  5512. trio_parameter_t *parameters)
  5513. {
  5514. #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
  5515. int charlen;
  5516. int cnt;
  5517. #endif
  5518. int assignment;
  5519. int ch;
  5520. int index; /* Index of format string */
  5521. int i; /* Index of current parameter */
  5522. trio_flags_t flags;
  5523. int width;
  5524. int base;
  5525. trio_pointer_t pointer;
  5526. assignment = 0;
  5527. i = 0;
  5528. index = 0;
  5529. data->InStream(data, &ch);
  5530. #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
  5531. (void)mblen(NULL, 0);
  5532. #endif
  5533. while (format[index])
  5534. {
  5535. #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
  5536. if (! isascii(format[index]))
  5537. {
  5538. charlen = mblen(&format[index], MB_LEN_MAX);
  5539. if (charlen != -1)
  5540. {
  5541. /* Compare multibyte characters in format string */
  5542. for (cnt = 0; cnt < charlen - 1; cnt++)
  5543. {
  5544. if (ch != format[index + cnt])
  5545. {
  5546. return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
  5547. }
  5548. data->InStream(data, &ch);
  5549. }
  5550. continue; /* while characters left in formatting string */
  5551. }
  5552. }
  5553. #endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
  5554. if ((EOF == ch) && (parameters[i].type != FORMAT_COUNT))
  5555. {
  5556. return (assignment > 0) ? assignment : EOF;
  5557. }
  5558. if (CHAR_IDENTIFIER == format[index])
  5559. {
  5560. if (CHAR_IDENTIFIER == format[index + 1])
  5561. {
  5562. /* Two % in format matches one % in input stream */
  5563. if (CHAR_IDENTIFIER == ch)
  5564. {
  5565. data->InStream(data, &ch);
  5566. index += 2;
  5567. continue; /* while format chars left */
  5568. }
  5569. else
  5570. return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
  5571. }
  5572. /* Skip the parameter entries */
  5573. while (parameters[i].type == FORMAT_PARAMETER)
  5574. i++;
  5575. flags = parameters[i].flags;
  5576. /* Find width */
  5577. width = parameters[i].width;
  5578. if (flags & FLAGS_WIDTH_PARAMETER)
  5579. {
  5580. /* Get width from parameter list */
  5581. width = (int)parameters[width].data.number.as_signed;
  5582. }
  5583. /* Find base */
  5584. base = parameters[i].base;
  5585. if (flags & FLAGS_BASE_PARAMETER)
  5586. {
  5587. /* Get base from parameter list */
  5588. base = (int)parameters[base].data.number.as_signed;
  5589. }
  5590. switch (parameters[i].type)
  5591. {
  5592. case FORMAT_INT:
  5593. {
  5594. trio_uintmax_t number;
  5595. if (0 == base)
  5596. base = BASE_DECIMAL;
  5597. if (!TrioReadNumber(data,
  5598. &number,
  5599. flags,
  5600. width,
  5601. base))
  5602. return assignment;
  5603. if (!(flags & FLAGS_IGNORE))
  5604. {
  5605. assignment++;
  5606. pointer = parameters[i].data.pointer;
  5607. #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
  5608. if (flags & FLAGS_SIZE_T)
  5609. *(size_t *)pointer = (size_t)number;
  5610. else
  5611. #endif
  5612. #if defined(QUALIFIER_PTRDIFF_T)
  5613. if (flags & FLAGS_PTRDIFF_T)
  5614. *(ptrdiff_t *)pointer = (ptrdiff_t)number;
  5615. else
  5616. #endif
  5617. #if defined(QUALIFIER_INTMAX_T)
  5618. if (flags & FLAGS_INTMAX_T)
  5619. *(trio_intmax_t *)pointer = (trio_intmax_t)number;
  5620. else
  5621. #endif
  5622. if (flags & FLAGS_QUAD)
  5623. *(trio_ulonglong_t *)pointer = (trio_ulonglong_t)number;
  5624. else if (flags & FLAGS_LONG)
  5625. *(long int *)pointer = (long int)number;
  5626. else if (flags & FLAGS_SHORT)
  5627. *(short int *)pointer = (short int)number;
  5628. else
  5629. *(int *)pointer = (int)number;
  5630. }
  5631. }
  5632. break; /* FORMAT_INT */
  5633. case FORMAT_STRING:
  5634. #if TRIO_WIDECHAR
  5635. if (flags & FLAGS_WIDECHAR)
  5636. {
  5637. if (!TrioReadWideString(data,
  5638. (flags & FLAGS_IGNORE)
  5639. ? NULL
  5640. : parameters[i].data.wstring,
  5641. flags,
  5642. width))
  5643. return assignment;
  5644. }
  5645. else
  5646. #endif
  5647. {
  5648. if (!TrioReadString(data,
  5649. (flags & FLAGS_IGNORE)
  5650. ? NULL
  5651. : parameters[i].data.string,
  5652. flags,
  5653. width))
  5654. return assignment;
  5655. }
  5656. if (!(flags & FLAGS_IGNORE))
  5657. assignment++;
  5658. break; /* FORMAT_STRING */
  5659. case FORMAT_DOUBLE:
  5660. {
  5661. trio_pointer_t pointer;
  5662. if (flags & FLAGS_IGNORE)
  5663. {
  5664. pointer = NULL;
  5665. }
  5666. else
  5667. {
  5668. pointer = (flags & FLAGS_LONGDOUBLE)
  5669. ? (trio_pointer_t)parameters[i].data.longdoublePointer
  5670. : (trio_pointer_t)parameters[i].data.doublePointer;
  5671. }
  5672. if (!TrioReadDouble(data, pointer, flags, width))
  5673. {
  5674. return assignment;
  5675. }
  5676. if (!(flags & FLAGS_IGNORE))
  5677. {
  5678. assignment++;
  5679. }
  5680. break; /* FORMAT_DOUBLE */
  5681. }
  5682. case FORMAT_GROUP:
  5683. {
  5684. int characterclass[MAX_CHARACTER_CLASS + 1];
  5685. int rc;
  5686. /* Skip over modifiers */
  5687. while (format[index] != SPECIFIER_GROUP)
  5688. {
  5689. index++;
  5690. }
  5691. /* Skip over group specifier */
  5692. index++;
  5693. memset(characterclass, 0, sizeof(characterclass));
  5694. rc = TrioGetCharacterClass(format,
  5695. &index,
  5696. &flags,
  5697. characterclass);
  5698. if (rc < 0)
  5699. return rc;
  5700. if (!TrioReadGroup(data,
  5701. (flags & FLAGS_IGNORE)
  5702. ? NULL
  5703. : parameters[i].data.string,
  5704. characterclass,
  5705. flags,
  5706. parameters[i].width))
  5707. return assignment;
  5708. if (!(flags & FLAGS_IGNORE))
  5709. assignment++;
  5710. }
  5711. break; /* FORMAT_GROUP */
  5712. case FORMAT_COUNT:
  5713. pointer = parameters[i].data.pointer;
  5714. if (NULL != pointer)
  5715. {
  5716. int count = data->committed;
  5717. if (ch != EOF)
  5718. count--; /* a character is read, but is not consumed yet */
  5719. #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
  5720. if (flags & FLAGS_SIZE_T)
  5721. *(size_t *)pointer = (size_t)count;
  5722. else
  5723. #endif
  5724. #if defined(QUALIFIER_PTRDIFF_T)
  5725. if (flags & FLAGS_PTRDIFF_T)
  5726. *(ptrdiff_t *)pointer = (ptrdiff_t)count;
  5727. else
  5728. #endif
  5729. #if defined(QUALIFIER_INTMAX_T)
  5730. if (flags & FLAGS_INTMAX_T)
  5731. *(trio_intmax_t *)pointer = (trio_intmax_t)count;
  5732. else
  5733. #endif
  5734. if (flags & FLAGS_QUAD)
  5735. {
  5736. *(trio_ulonglong_t *)pointer = (trio_ulonglong_t)count;
  5737. }
  5738. else if (flags & FLAGS_LONG)
  5739. {
  5740. *(long int *)pointer = (long int)count;
  5741. }
  5742. else if (flags & FLAGS_SHORT)
  5743. {
  5744. *(short int *)pointer = (short int)count;
  5745. }
  5746. else
  5747. {
  5748. *(int *)pointer = (int)count;
  5749. }
  5750. }
  5751. break; /* FORMAT_COUNT */
  5752. case FORMAT_CHAR:
  5753. #if TRIO_WIDECHAR
  5754. if (flags & FLAGS_WIDECHAR)
  5755. {
  5756. if (TrioReadWideChar(data,
  5757. (flags & FLAGS_IGNORE)
  5758. ? NULL
  5759. : parameters[i].data.wstring,
  5760. flags,
  5761. (width == NO_WIDTH) ? 1 : width) == 0)
  5762. return assignment;
  5763. }
  5764. else
  5765. #endif
  5766. {
  5767. if (TrioReadChar(data,
  5768. (flags & FLAGS_IGNORE)
  5769. ? NULL
  5770. : parameters[i].data.string,
  5771. flags,
  5772. (width == NO_WIDTH) ? 1 : width) == 0)
  5773. return assignment;
  5774. }
  5775. if (!(flags & FLAGS_IGNORE))
  5776. assignment++;
  5777. break; /* FORMAT_CHAR */
  5778. case FORMAT_POINTER:
  5779. if (!TrioReadPointer(data,
  5780. (flags & FLAGS_IGNORE)
  5781. ? NULL
  5782. : (trio_pointer_t *)parameters[i].data.pointer,
  5783. flags))
  5784. return assignment;
  5785. if (!(flags & FLAGS_IGNORE))
  5786. assignment++;
  5787. break; /* FORMAT_POINTER */
  5788. case FORMAT_PARAMETER:
  5789. break; /* FORMAT_PARAMETER */
  5790. default:
  5791. return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
  5792. }
  5793. ch = data->current;
  5794. index = parameters[i].indexAfterSpecifier;
  5795. i++;
  5796. }
  5797. else /* Not an % identifier */
  5798. {
  5799. if (isspace((int)format[index]))
  5800. {
  5801. /* Whitespaces may match any amount of whitespaces */
  5802. ch = TrioSkipWhitespaces(data);
  5803. }
  5804. else if (ch == format[index])
  5805. {
  5806. data->InStream(data, &ch);
  5807. }
  5808. else
  5809. return assignment;
  5810. index++;
  5811. }
  5812. }
  5813. return assignment;
  5814. }
  5815. /*************************************************************************
  5816. * TrioScan
  5817. */
  5818. TRIO_PRIVATE int
  5819. TrioScan
  5820. TRIO_ARGS6((source, sourceSize, InStream, format, arglist, argarray),
  5821. trio_pointer_t source,
  5822. size_t sourceSize,
  5823. void (*InStream) TRIO_PROTO((trio_class_t *, int *)),
  5824. TRIO_CONST char *format,
  5825. TRIO_VA_LIST_PTR arglist,
  5826. trio_pointer_t *argarray)
  5827. {
  5828. int status;
  5829. trio_parameter_t parameters[MAX_PARAMETERS];
  5830. trio_class_t data;
  5831. assert(VALID(InStream));
  5832. assert(VALID(format));
  5833. memset(&data, 0, sizeof(data));
  5834. data.InStream = InStream;
  5835. data.location = (trio_pointer_t)source;
  5836. data.max = sourceSize;
  5837. data.error = 0;
  5838. #if defined(USE_LOCALE)
  5839. if (NULL == internalLocaleValues)
  5840. {
  5841. TrioSetLocale();
  5842. }
  5843. #endif
  5844. status = TrioParse(TYPE_SCAN, format, parameters, arglist, argarray);
  5845. if (status < 0)
  5846. return status;
  5847. status = TrioScanProcess(&data, format, parameters);
  5848. if (data.error != 0)
  5849. {
  5850. status = data.error;
  5851. }
  5852. return status;
  5853. }
  5854. /*************************************************************************
  5855. * TrioInStreamFile
  5856. */
  5857. TRIO_PRIVATE void
  5858. TrioInStreamFile
  5859. TRIO_ARGS2((self, intPointer),
  5860. trio_class_t *self,
  5861. int *intPointer)
  5862. {
  5863. FILE *file;
  5864. assert(VALID(self));
  5865. assert(VALID(self->location));
  5866. assert(VALID(file));
  5867. file = (FILE *)self->location;
  5868. self->current = fgetc(file);
  5869. if (self->current == EOF)
  5870. {
  5871. self->error = (ferror(file))
  5872. ? TRIO_ERROR_RETURN(TRIO_ERRNO, 0)
  5873. : TRIO_ERROR_RETURN(TRIO_EOF, 0);
  5874. }
  5875. else
  5876. {
  5877. self->processed++;
  5878. self->committed++;
  5879. }
  5880. if (VALID(intPointer))
  5881. {
  5882. *intPointer = self->current;
  5883. }
  5884. }
  5885. /*************************************************************************
  5886. * TrioInStreamFileDescriptor
  5887. */
  5888. TRIO_PRIVATE void
  5889. TrioInStreamFileDescriptor
  5890. TRIO_ARGS2((self, intPointer),
  5891. trio_class_t *self,
  5892. int *intPointer)
  5893. {
  5894. int fd;
  5895. int size;
  5896. unsigned char input;
  5897. assert(VALID(self));
  5898. assert(VALID(self->location));
  5899. fd = *((int *)self->location);
  5900. size = read(fd, &input, sizeof(char));
  5901. if (size == -1)
  5902. {
  5903. self->error = TRIO_ERROR_RETURN(TRIO_ERRNO, 0);
  5904. self->current = EOF;
  5905. }
  5906. else
  5907. {
  5908. self->current = (size == 0) ? EOF : input;
  5909. }
  5910. if (self->current != EOF)
  5911. {
  5912. self->committed++;
  5913. self->processed++;
  5914. }
  5915. if (VALID(intPointer))
  5916. {
  5917. *intPointer = self->current;
  5918. }
  5919. }
  5920. /*************************************************************************
  5921. * TrioInStreamCustom
  5922. */
  5923. TRIO_PRIVATE void
  5924. TrioInStreamCustom
  5925. TRIO_ARGS2((self, intPointer),
  5926. trio_class_t *self,
  5927. int *intPointer)
  5928. {
  5929. trio_custom_t *data;
  5930. assert(VALID(self));
  5931. assert(VALID(self->location));
  5932. data = (trio_custom_t *)self->location;
  5933. self->current = (data->stream.in == NULL)
  5934. ? NIL
  5935. : (data->stream.in)(data->closure);
  5936. if (self->current == NIL)
  5937. {
  5938. self->current = EOF;
  5939. }
  5940. else
  5941. {
  5942. self->processed++;
  5943. self->committed++;
  5944. }
  5945. if (VALID(intPointer))
  5946. {
  5947. *intPointer = self->current;
  5948. }
  5949. }
  5950. /*************************************************************************
  5951. * TrioInStreamString
  5952. */
  5953. TRIO_PRIVATE void
  5954. TrioInStreamString
  5955. TRIO_ARGS2((self, intPointer),
  5956. trio_class_t *self,
  5957. int *intPointer)
  5958. {
  5959. unsigned char **buffer;
  5960. assert(VALID(self));
  5961. assert(VALID(self->location));
  5962. buffer = (unsigned char **)self->location;
  5963. self->current = (*buffer)[0];
  5964. if (self->current == NIL)
  5965. {
  5966. self->current = EOF;
  5967. }
  5968. else
  5969. {
  5970. (*buffer)++;
  5971. self->processed++;
  5972. self->committed++;
  5973. }
  5974. if (VALID(intPointer))
  5975. {
  5976. *intPointer = self->current;
  5977. }
  5978. }
  5979. /*************************************************************************
  5980. *
  5981. * Formatted scanning functions
  5982. *
  5983. ************************************************************************/
  5984. #if defined(TRIO_DOCUMENTATION)
  5985. # include "doc/doc_scanf.h"
  5986. #endif
  5987. /** @addtogroup Scanf
  5988. @{
  5989. */
  5990. /*************************************************************************
  5991. * scanf
  5992. */
  5993. /**
  5994. Scan characters from standard input stream.
  5995. @param format Formatting string.
  5996. @param ... Arguments.
  5997. @return Number of scanned characters.
  5998. */
  5999. TRIO_PUBLIC int
  6000. trio_scanf
  6001. TRIO_VARGS2((format, va_alist),
  6002. TRIO_CONST char *format,
  6003. TRIO_VA_DECL)
  6004. {
  6005. int status;
  6006. va_list args;
  6007. assert(VALID(format));
  6008. TRIO_VA_START(args, format);
  6009. status = TrioScan((trio_pointer_t)stdin, 0,
  6010. TrioInStreamFile,
  6011. format, TRIO_VA_LIST_ADDR(args), NULL);
  6012. TRIO_VA_END(args);
  6013. return status;
  6014. }
  6015. TRIO_PUBLIC int
  6016. trio_vscanf
  6017. TRIO_ARGS2((format, args),
  6018. TRIO_CONST char *format,
  6019. va_list args)
  6020. {
  6021. assert(VALID(format));
  6022. return TrioScan((trio_pointer_t)stdin, 0,
  6023. TrioInStreamFile,
  6024. format, TRIO_VA_LIST_ADDR(args), NULL);
  6025. }
  6026. TRIO_PUBLIC int
  6027. trio_scanfv
  6028. TRIO_ARGS2((format, args),
  6029. TRIO_CONST char *format,
  6030. trio_pointer_t *args)
  6031. {
  6032. assert(VALID(format));
  6033. return TrioScan((trio_pointer_t)stdin, 0,
  6034. TrioInStreamFile,
  6035. format, NULL, args);
  6036. }
  6037. /*************************************************************************
  6038. * fscanf
  6039. */
  6040. TRIO_PUBLIC int
  6041. trio_fscanf
  6042. TRIO_VARGS3((file, format, va_alist),
  6043. FILE *file,
  6044. TRIO_CONST char *format,
  6045. TRIO_VA_DECL)
  6046. {
  6047. int status;
  6048. va_list args;
  6049. assert(VALID(file));
  6050. assert(VALID(format));
  6051. TRIO_VA_START(args, format);
  6052. status = TrioScan((trio_pointer_t)file, 0,
  6053. TrioInStreamFile,
  6054. format, TRIO_VA_LIST_ADDR(args), NULL);
  6055. TRIO_VA_END(args);
  6056. return status;
  6057. }
  6058. TRIO_PUBLIC int
  6059. trio_vfscanf
  6060. TRIO_ARGS3((file, format, args),
  6061. FILE *file,
  6062. TRIO_CONST char *format,
  6063. va_list args)
  6064. {
  6065. assert(VALID(file));
  6066. assert(VALID(format));
  6067. return TrioScan((trio_pointer_t)file, 0,
  6068. TrioInStreamFile,
  6069. format, TRIO_VA_LIST_ADDR(args), NULL);
  6070. }
  6071. TRIO_PUBLIC int
  6072. trio_fscanfv
  6073. TRIO_ARGS3((file, format, args),
  6074. FILE *file,
  6075. TRIO_CONST char *format,
  6076. trio_pointer_t *args)
  6077. {
  6078. assert(VALID(file));
  6079. assert(VALID(format));
  6080. return TrioScan((trio_pointer_t)file, 0,
  6081. TrioInStreamFile,
  6082. format, NULL, args);
  6083. }
  6084. /*************************************************************************
  6085. * dscanf
  6086. */
  6087. TRIO_PUBLIC int
  6088. trio_dscanf
  6089. TRIO_VARGS3((fd, format, va_alist),
  6090. int fd,
  6091. TRIO_CONST char *format,
  6092. TRIO_VA_DECL)
  6093. {
  6094. int status;
  6095. va_list args;
  6096. assert(VALID(format));
  6097. TRIO_VA_START(args, format);
  6098. status = TrioScan((trio_pointer_t)&fd, 0,
  6099. TrioInStreamFileDescriptor,
  6100. format, TRIO_VA_LIST_ADDR(args), NULL);
  6101. TRIO_VA_END(args);
  6102. return status;
  6103. }
  6104. TRIO_PUBLIC int
  6105. trio_vdscanf
  6106. TRIO_ARGS3((fd, format, args),
  6107. int fd,
  6108. TRIO_CONST char *format,
  6109. va_list args)
  6110. {
  6111. assert(VALID(format));
  6112. return TrioScan((trio_pointer_t)&fd, 0,
  6113. TrioInStreamFileDescriptor,
  6114. format, TRIO_VA_LIST_ADDR(args), NULL);
  6115. }
  6116. TRIO_PUBLIC int
  6117. trio_dscanfv
  6118. TRIO_ARGS3((fd, format, args),
  6119. int fd,
  6120. TRIO_CONST char *format,
  6121. trio_pointer_t *args)
  6122. {
  6123. assert(VALID(format));
  6124. return TrioScan((trio_pointer_t)&fd, 0,
  6125. TrioInStreamFileDescriptor,
  6126. format, NULL, args);
  6127. }
  6128. /*************************************************************************
  6129. * cscanf
  6130. */
  6131. TRIO_PUBLIC int
  6132. trio_cscanf
  6133. TRIO_VARGS4((stream, closure, format, va_alist),
  6134. trio_instream_t stream,
  6135. trio_pointer_t closure,
  6136. TRIO_CONST char *format,
  6137. TRIO_VA_DECL)
  6138. {
  6139. int status;
  6140. va_list args;
  6141. trio_custom_t data;
  6142. assert(VALID(stream));
  6143. assert(VALID(format));
  6144. TRIO_VA_START(args, format);
  6145. data.stream.in = stream;
  6146. data.closure = closure;
  6147. status = TrioScan(&data, 0, TrioInStreamCustom, format, TRIO_VA_LIST_ADDR(args), NULL);
  6148. TRIO_VA_END(args);
  6149. return status;
  6150. }
  6151. TRIO_PUBLIC int
  6152. trio_vcscanf
  6153. TRIO_ARGS4((stream, closure, format, args),
  6154. trio_instream_t stream,
  6155. trio_pointer_t closure,
  6156. TRIO_CONST char *format,
  6157. va_list args)
  6158. {
  6159. trio_custom_t data;
  6160. assert(VALID(stream));
  6161. assert(VALID(format));
  6162. data.stream.in = stream;
  6163. data.closure = closure;
  6164. return TrioScan(&data, 0, TrioInStreamCustom, format, TRIO_VA_LIST_ADDR(args), NULL);
  6165. }
  6166. TRIO_PUBLIC int
  6167. trio_cscanfv
  6168. TRIO_ARGS4((stream, closure, format, args),
  6169. trio_instream_t stream,
  6170. trio_pointer_t closure,
  6171. TRIO_CONST char *format,
  6172. trio_pointer_t *args)
  6173. {
  6174. trio_custom_t data;
  6175. assert(VALID(stream));
  6176. assert(VALID(format));
  6177. data.stream.in = stream;
  6178. data.closure = closure;
  6179. return TrioScan(&data, 0, TrioInStreamCustom, format, NULL, args);
  6180. }
  6181. /*************************************************************************
  6182. * sscanf
  6183. */
  6184. TRIO_PUBLIC int
  6185. trio_sscanf
  6186. TRIO_VARGS3((buffer, format, va_alist),
  6187. TRIO_CONST char *buffer,
  6188. TRIO_CONST char *format,
  6189. TRIO_VA_DECL)
  6190. {
  6191. int status;
  6192. va_list args;
  6193. assert(VALID(buffer));
  6194. assert(VALID(format));
  6195. TRIO_VA_START(args, format);
  6196. status = TrioScan((trio_pointer_t)&buffer, 0,
  6197. TrioInStreamString,
  6198. format, TRIO_VA_LIST_ADDR(args), NULL);
  6199. TRIO_VA_END(args);
  6200. return status;
  6201. }
  6202. TRIO_PUBLIC int
  6203. trio_vsscanf
  6204. TRIO_ARGS3((buffer, format, args),
  6205. TRIO_CONST char *buffer,
  6206. TRIO_CONST char *format,
  6207. va_list args)
  6208. {
  6209. assert(VALID(buffer));
  6210. assert(VALID(format));
  6211. return TrioScan((trio_pointer_t)&buffer, 0,
  6212. TrioInStreamString,
  6213. format, TRIO_VA_LIST_ADDR(args), NULL);
  6214. }
  6215. TRIO_PUBLIC int
  6216. trio_sscanfv
  6217. TRIO_ARGS3((buffer, format, args),
  6218. TRIO_CONST char *buffer,
  6219. TRIO_CONST char *format,
  6220. trio_pointer_t *args)
  6221. {
  6222. assert(VALID(buffer));
  6223. assert(VALID(format));
  6224. return TrioScan((trio_pointer_t)&buffer, 0,
  6225. TrioInStreamString,
  6226. format, NULL, args);
  6227. }
  6228. /** @} End of Scanf documentation module */
  6229. /*************************************************************************
  6230. * trio_strerror
  6231. */
  6232. TRIO_PUBLIC TRIO_CONST char *
  6233. trio_strerror
  6234. TRIO_ARGS1((errorcode),
  6235. int errorcode)
  6236. {
  6237. /* Textual versions of the error codes */
  6238. switch (TRIO_ERROR_CODE(errorcode))
  6239. {
  6240. case TRIO_EOF:
  6241. return "End of file";
  6242. case TRIO_EINVAL:
  6243. return "Invalid argument";
  6244. case TRIO_ETOOMANY:
  6245. return "Too many arguments";
  6246. case TRIO_EDBLREF:
  6247. return "Double reference";
  6248. case TRIO_EGAP:
  6249. return "Reference gap";
  6250. case TRIO_ENOMEM:
  6251. return "Out of memory";
  6252. case TRIO_ERANGE:
  6253. return "Invalid range";
  6254. case TRIO_ECUSTOM:
  6255. return "Custom error";
  6256. default:
  6257. return "Unknown";
  6258. }
  6259. }