trio.c 156 KB

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