write.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030
  1. /* Copyright (C) 2002-2015 Free Software Foundation, Inc.
  2. Contributed by Andy Vaught
  3. Namelist output contributed by Paul Thomas
  4. F2003 I/O support contributed by Jerry DeLisle
  5. This file is part of the GNU Fortran runtime library (libgfortran).
  6. Libgfortran is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. Libgfortran is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. #include "io.h"
  22. #include "fbuf.h"
  23. #include "format.h"
  24. #include "unix.h"
  25. #include <assert.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <stdlib.h>
  29. #include <errno.h>
  30. #define star_fill(p, n) memset(p, '*', n)
  31. typedef unsigned char uchar;
  32. /* Helper functions for character(kind=4) internal units. These are needed
  33. by write_float.def. */
  34. static void
  35. memcpy4 (gfc_char4_t *dest, const char *source, int k)
  36. {
  37. int j;
  38. const char *p = source;
  39. for (j = 0; j < k; j++)
  40. *dest++ = (gfc_char4_t) *p++;
  41. }
  42. /* This include contains the heart and soul of formatted floating point. */
  43. #include "write_float.def"
  44. /* Write out default char4. */
  45. static void
  46. write_default_char4 (st_parameter_dt *dtp, const gfc_char4_t *source,
  47. int src_len, int w_len)
  48. {
  49. char *p;
  50. int j, k = 0;
  51. gfc_char4_t c;
  52. uchar d;
  53. /* Take care of preceding blanks. */
  54. if (w_len > src_len)
  55. {
  56. k = w_len - src_len;
  57. p = write_block (dtp, k);
  58. if (p == NULL)
  59. return;
  60. if (is_char4_unit (dtp))
  61. {
  62. gfc_char4_t *p4 = (gfc_char4_t *) p;
  63. memset4 (p4, ' ', k);
  64. }
  65. else
  66. memset (p, ' ', k);
  67. }
  68. /* Get ready to handle delimiters if needed. */
  69. switch (dtp->u.p.current_unit->delim_status)
  70. {
  71. case DELIM_APOSTROPHE:
  72. d = '\'';
  73. break;
  74. case DELIM_QUOTE:
  75. d = '"';
  76. break;
  77. default:
  78. d = ' ';
  79. break;
  80. }
  81. /* Now process the remaining characters, one at a time. */
  82. for (j = 0; j < src_len; j++)
  83. {
  84. c = source[j];
  85. if (is_char4_unit (dtp))
  86. {
  87. gfc_char4_t *q;
  88. /* Handle delimiters if any. */
  89. if (c == d && d != ' ')
  90. {
  91. p = write_block (dtp, 2);
  92. if (p == NULL)
  93. return;
  94. q = (gfc_char4_t *) p;
  95. *q++ = c;
  96. }
  97. else
  98. {
  99. p = write_block (dtp, 1);
  100. if (p == NULL)
  101. return;
  102. q = (gfc_char4_t *) p;
  103. }
  104. *q = c;
  105. }
  106. else
  107. {
  108. /* Handle delimiters if any. */
  109. if (c == d && d != ' ')
  110. {
  111. p = write_block (dtp, 2);
  112. if (p == NULL)
  113. return;
  114. *p++ = (uchar) c;
  115. }
  116. else
  117. {
  118. p = write_block (dtp, 1);
  119. if (p == NULL)
  120. return;
  121. }
  122. *p = c > 255 ? '?' : (uchar) c;
  123. }
  124. }
  125. }
  126. /* Write out UTF-8 converted from char4. */
  127. static void
  128. write_utf8_char4 (st_parameter_dt *dtp, gfc_char4_t *source,
  129. int src_len, int w_len)
  130. {
  131. char *p;
  132. int j, k = 0;
  133. gfc_char4_t c;
  134. static const uchar masks[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
  135. static const uchar limits[6] = { 0x80, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE };
  136. int nbytes;
  137. uchar buf[6], d, *q;
  138. /* Take care of preceding blanks. */
  139. if (w_len > src_len)
  140. {
  141. k = w_len - src_len;
  142. p = write_block (dtp, k);
  143. if (p == NULL)
  144. return;
  145. memset (p, ' ', k);
  146. }
  147. /* Get ready to handle delimiters if needed. */
  148. switch (dtp->u.p.current_unit->delim_status)
  149. {
  150. case DELIM_APOSTROPHE:
  151. d = '\'';
  152. break;
  153. case DELIM_QUOTE:
  154. d = '"';
  155. break;
  156. default:
  157. d = ' ';
  158. break;
  159. }
  160. /* Now process the remaining characters, one at a time. */
  161. for (j = k; j < src_len; j++)
  162. {
  163. c = source[j];
  164. if (c < 0x80)
  165. {
  166. /* Handle the delimiters if any. */
  167. if (c == d && d != ' ')
  168. {
  169. p = write_block (dtp, 2);
  170. if (p == NULL)
  171. return;
  172. *p++ = (uchar) c;
  173. }
  174. else
  175. {
  176. p = write_block (dtp, 1);
  177. if (p == NULL)
  178. return;
  179. }
  180. *p = (uchar) c;
  181. }
  182. else
  183. {
  184. /* Convert to UTF-8 sequence. */
  185. nbytes = 1;
  186. q = &buf[6];
  187. do
  188. {
  189. *--q = ((c & 0x3F) | 0x80);
  190. c >>= 6;
  191. nbytes++;
  192. }
  193. while (c >= 0x3F || (c & limits[nbytes-1]));
  194. *--q = (c | masks[nbytes-1]);
  195. p = write_block (dtp, nbytes);
  196. if (p == NULL)
  197. return;
  198. while (q < &buf[6])
  199. *p++ = *q++;
  200. }
  201. }
  202. }
  203. void
  204. write_a (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
  205. {
  206. int wlen;
  207. char *p;
  208. wlen = f->u.string.length < 0
  209. || (f->format == FMT_G && f->u.string.length == 0)
  210. ? len : f->u.string.length;
  211. #ifdef HAVE_CRLF
  212. /* If this is formatted STREAM IO convert any embedded line feed characters
  213. to CR_LF on systems that use that sequence for newlines. See F2003
  214. Standard sections 10.6.3 and 9.9 for further information. */
  215. if (is_stream_io (dtp))
  216. {
  217. const char crlf[] = "\r\n";
  218. int i, q, bytes;
  219. q = bytes = 0;
  220. /* Write out any padding if needed. */
  221. if (len < wlen)
  222. {
  223. p = write_block (dtp, wlen - len);
  224. if (p == NULL)
  225. return;
  226. memset (p, ' ', wlen - len);
  227. }
  228. /* Scan the source string looking for '\n' and convert it if found. */
  229. for (i = 0; i < wlen; i++)
  230. {
  231. if (source[i] == '\n')
  232. {
  233. /* Write out the previously scanned characters in the string. */
  234. if (bytes > 0)
  235. {
  236. p = write_block (dtp, bytes);
  237. if (p == NULL)
  238. return;
  239. memcpy (p, &source[q], bytes);
  240. q += bytes;
  241. bytes = 0;
  242. }
  243. /* Write out the CR_LF sequence. */
  244. q++;
  245. p = write_block (dtp, 2);
  246. if (p == NULL)
  247. return;
  248. memcpy (p, crlf, 2);
  249. }
  250. else
  251. bytes++;
  252. }
  253. /* Write out any remaining bytes if no LF was found. */
  254. if (bytes > 0)
  255. {
  256. p = write_block (dtp, bytes);
  257. if (p == NULL)
  258. return;
  259. memcpy (p, &source[q], bytes);
  260. }
  261. }
  262. else
  263. {
  264. #endif
  265. p = write_block (dtp, wlen);
  266. if (p == NULL)
  267. return;
  268. if (unlikely (is_char4_unit (dtp)))
  269. {
  270. gfc_char4_t *p4 = (gfc_char4_t *) p;
  271. if (wlen < len)
  272. memcpy4 (p4, source, wlen);
  273. else
  274. {
  275. memset4 (p4, ' ', wlen - len);
  276. memcpy4 (p4 + wlen - len, source, len);
  277. }
  278. return;
  279. }
  280. if (wlen < len)
  281. memcpy (p, source, wlen);
  282. else
  283. {
  284. memset (p, ' ', wlen - len);
  285. memcpy (p + wlen - len, source, len);
  286. }
  287. #ifdef HAVE_CRLF
  288. }
  289. #endif
  290. }
  291. /* The primary difference between write_a_char4 and write_a is that we have to
  292. deal with writing from the first byte of the 4-byte character and pay
  293. attention to the most significant bytes. For ENCODING="default" write the
  294. lowest significant byte. If the 3 most significant bytes contain
  295. non-zero values, emit a '?'. For ENCODING="utf-8", convert the UCS-32 value
  296. to the UTF-8 encoded string before writing out. */
  297. void
  298. write_a_char4 (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
  299. {
  300. int wlen;
  301. gfc_char4_t *q;
  302. wlen = f->u.string.length < 0
  303. || (f->format == FMT_G && f->u.string.length == 0)
  304. ? len : f->u.string.length;
  305. q = (gfc_char4_t *) source;
  306. #ifdef HAVE_CRLF
  307. /* If this is formatted STREAM IO convert any embedded line feed characters
  308. to CR_LF on systems that use that sequence for newlines. See F2003
  309. Standard sections 10.6.3 and 9.9 for further information. */
  310. if (is_stream_io (dtp))
  311. {
  312. const gfc_char4_t crlf[] = {0x000d,0x000a};
  313. int i, bytes;
  314. gfc_char4_t *qq;
  315. bytes = 0;
  316. /* Write out any padding if needed. */
  317. if (len < wlen)
  318. {
  319. char *p;
  320. p = write_block (dtp, wlen - len);
  321. if (p == NULL)
  322. return;
  323. memset (p, ' ', wlen - len);
  324. }
  325. /* Scan the source string looking for '\n' and convert it if found. */
  326. qq = (gfc_char4_t *) source;
  327. for (i = 0; i < wlen; i++)
  328. {
  329. if (qq[i] == '\n')
  330. {
  331. /* Write out the previously scanned characters in the string. */
  332. if (bytes > 0)
  333. {
  334. if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
  335. write_utf8_char4 (dtp, q, bytes, 0);
  336. else
  337. write_default_char4 (dtp, q, bytes, 0);
  338. bytes = 0;
  339. }
  340. /* Write out the CR_LF sequence. */
  341. write_default_char4 (dtp, crlf, 2, 0);
  342. }
  343. else
  344. bytes++;
  345. }
  346. /* Write out any remaining bytes if no LF was found. */
  347. if (bytes > 0)
  348. {
  349. if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
  350. write_utf8_char4 (dtp, q, bytes, 0);
  351. else
  352. write_default_char4 (dtp, q, bytes, 0);
  353. }
  354. }
  355. else
  356. {
  357. #endif
  358. if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
  359. write_utf8_char4 (dtp, q, len, wlen);
  360. else
  361. write_default_char4 (dtp, q, len, wlen);
  362. #ifdef HAVE_CRLF
  363. }
  364. #endif
  365. }
  366. static GFC_INTEGER_LARGEST
  367. extract_int (const void *p, int len)
  368. {
  369. GFC_INTEGER_LARGEST i = 0;
  370. if (p == NULL)
  371. return i;
  372. switch (len)
  373. {
  374. case 1:
  375. {
  376. GFC_INTEGER_1 tmp;
  377. memcpy ((void *) &tmp, p, len);
  378. i = tmp;
  379. }
  380. break;
  381. case 2:
  382. {
  383. GFC_INTEGER_2 tmp;
  384. memcpy ((void *) &tmp, p, len);
  385. i = tmp;
  386. }
  387. break;
  388. case 4:
  389. {
  390. GFC_INTEGER_4 tmp;
  391. memcpy ((void *) &tmp, p, len);
  392. i = tmp;
  393. }
  394. break;
  395. case 8:
  396. {
  397. GFC_INTEGER_8 tmp;
  398. memcpy ((void *) &tmp, p, len);
  399. i = tmp;
  400. }
  401. break;
  402. #ifdef HAVE_GFC_INTEGER_16
  403. case 16:
  404. {
  405. GFC_INTEGER_16 tmp;
  406. memcpy ((void *) &tmp, p, len);
  407. i = tmp;
  408. }
  409. break;
  410. #endif
  411. default:
  412. internal_error (NULL, "bad integer kind");
  413. }
  414. return i;
  415. }
  416. static GFC_UINTEGER_LARGEST
  417. extract_uint (const void *p, int len)
  418. {
  419. GFC_UINTEGER_LARGEST i = 0;
  420. if (p == NULL)
  421. return i;
  422. switch (len)
  423. {
  424. case 1:
  425. {
  426. GFC_INTEGER_1 tmp;
  427. memcpy ((void *) &tmp, p, len);
  428. i = (GFC_UINTEGER_1) tmp;
  429. }
  430. break;
  431. case 2:
  432. {
  433. GFC_INTEGER_2 tmp;
  434. memcpy ((void *) &tmp, p, len);
  435. i = (GFC_UINTEGER_2) tmp;
  436. }
  437. break;
  438. case 4:
  439. {
  440. GFC_INTEGER_4 tmp;
  441. memcpy ((void *) &tmp, p, len);
  442. i = (GFC_UINTEGER_4) tmp;
  443. }
  444. break;
  445. case 8:
  446. {
  447. GFC_INTEGER_8 tmp;
  448. memcpy ((void *) &tmp, p, len);
  449. i = (GFC_UINTEGER_8) tmp;
  450. }
  451. break;
  452. #ifdef HAVE_GFC_INTEGER_16
  453. case 10:
  454. case 16:
  455. {
  456. GFC_INTEGER_16 tmp = 0;
  457. memcpy ((void *) &tmp, p, len);
  458. i = (GFC_UINTEGER_16) tmp;
  459. }
  460. break;
  461. #endif
  462. default:
  463. internal_error (NULL, "bad integer kind");
  464. }
  465. return i;
  466. }
  467. void
  468. write_l (st_parameter_dt *dtp, const fnode *f, char *source, int len)
  469. {
  470. char *p;
  471. int wlen;
  472. GFC_INTEGER_LARGEST n;
  473. wlen = (f->format == FMT_G && f->u.w == 0) ? 1 : f->u.w;
  474. p = write_block (dtp, wlen);
  475. if (p == NULL)
  476. return;
  477. n = extract_int (source, len);
  478. if (unlikely (is_char4_unit (dtp)))
  479. {
  480. gfc_char4_t *p4 = (gfc_char4_t *) p;
  481. memset4 (p4, ' ', wlen -1);
  482. p4[wlen - 1] = (n) ? 'T' : 'F';
  483. return;
  484. }
  485. memset (p, ' ', wlen -1);
  486. p[wlen - 1] = (n) ? 'T' : 'F';
  487. }
  488. static void
  489. write_boz (st_parameter_dt *dtp, const fnode *f, const char *q, int n)
  490. {
  491. int w, m, digits, nzero, nblank;
  492. char *p;
  493. w = f->u.integer.w;
  494. m = f->u.integer.m;
  495. /* Special case: */
  496. if (m == 0 && n == 0)
  497. {
  498. if (w == 0)
  499. w = 1;
  500. p = write_block (dtp, w);
  501. if (p == NULL)
  502. return;
  503. if (unlikely (is_char4_unit (dtp)))
  504. {
  505. gfc_char4_t *p4 = (gfc_char4_t *) p;
  506. memset4 (p4, ' ', w);
  507. }
  508. else
  509. memset (p, ' ', w);
  510. goto done;
  511. }
  512. digits = strlen (q);
  513. /* Select a width if none was specified. The idea here is to always
  514. print something. */
  515. if (w == 0)
  516. w = ((digits < m) ? m : digits);
  517. p = write_block (dtp, w);
  518. if (p == NULL)
  519. return;
  520. nzero = 0;
  521. if (digits < m)
  522. nzero = m - digits;
  523. /* See if things will work. */
  524. nblank = w - (nzero + digits);
  525. if (unlikely (is_char4_unit (dtp)))
  526. {
  527. gfc_char4_t *p4 = (gfc_char4_t *) p;
  528. if (nblank < 0)
  529. {
  530. memset4 (p4, '*', w);
  531. return;
  532. }
  533. if (!dtp->u.p.no_leading_blank)
  534. {
  535. memset4 (p4, ' ', nblank);
  536. q += nblank;
  537. memset4 (p4, '0', nzero);
  538. q += nzero;
  539. memcpy4 (p4, q, digits);
  540. }
  541. else
  542. {
  543. memset4 (p4, '0', nzero);
  544. q += nzero;
  545. memcpy4 (p4, q, digits);
  546. q += digits;
  547. memset4 (p4, ' ', nblank);
  548. dtp->u.p.no_leading_blank = 0;
  549. }
  550. return;
  551. }
  552. if (nblank < 0)
  553. {
  554. star_fill (p, w);
  555. goto done;
  556. }
  557. if (!dtp->u.p.no_leading_blank)
  558. {
  559. memset (p, ' ', nblank);
  560. p += nblank;
  561. memset (p, '0', nzero);
  562. p += nzero;
  563. memcpy (p, q, digits);
  564. }
  565. else
  566. {
  567. memset (p, '0', nzero);
  568. p += nzero;
  569. memcpy (p, q, digits);
  570. p += digits;
  571. memset (p, ' ', nblank);
  572. dtp->u.p.no_leading_blank = 0;
  573. }
  574. done:
  575. return;
  576. }
  577. static void
  578. write_decimal (st_parameter_dt *dtp, const fnode *f, const char *source,
  579. int len,
  580. const char *(*conv) (GFC_INTEGER_LARGEST, char *, size_t))
  581. {
  582. GFC_INTEGER_LARGEST n = 0;
  583. int w, m, digits, nsign, nzero, nblank;
  584. char *p;
  585. const char *q;
  586. sign_t sign;
  587. char itoa_buf[GFC_BTOA_BUF_SIZE];
  588. w = f->u.integer.w;
  589. m = f->format == FMT_G ? -1 : f->u.integer.m;
  590. n = extract_int (source, len);
  591. /* Special case: */
  592. if (m == 0 && n == 0)
  593. {
  594. if (w == 0)
  595. w = 1;
  596. p = write_block (dtp, w);
  597. if (p == NULL)
  598. return;
  599. if (unlikely (is_char4_unit (dtp)))
  600. {
  601. gfc_char4_t *p4 = (gfc_char4_t *) p;
  602. memset4 (p4, ' ', w);
  603. }
  604. else
  605. memset (p, ' ', w);
  606. goto done;
  607. }
  608. sign = calculate_sign (dtp, n < 0);
  609. if (n < 0)
  610. n = -n;
  611. nsign = sign == S_NONE ? 0 : 1;
  612. /* conv calls itoa which sets the negative sign needed
  613. by write_integer. The sign '+' or '-' is set below based on sign
  614. calculated above, so we just point past the sign in the string
  615. before proceeding to avoid double signs in corner cases.
  616. (see PR38504) */
  617. q = conv (n, itoa_buf, sizeof (itoa_buf));
  618. if (*q == '-')
  619. q++;
  620. digits = strlen (q);
  621. /* Select a width if none was specified. The idea here is to always
  622. print something. */
  623. if (w == 0)
  624. w = ((digits < m) ? m : digits) + nsign;
  625. p = write_block (dtp, w);
  626. if (p == NULL)
  627. return;
  628. nzero = 0;
  629. if (digits < m)
  630. nzero = m - digits;
  631. /* See if things will work. */
  632. nblank = w - (nsign + nzero + digits);
  633. if (unlikely (is_char4_unit (dtp)))
  634. {
  635. gfc_char4_t * p4 = (gfc_char4_t *) p;
  636. if (nblank < 0)
  637. {
  638. memset4 (p4, '*', w);
  639. goto done;
  640. }
  641. memset4 (p4, ' ', nblank);
  642. p4 += nblank;
  643. switch (sign)
  644. {
  645. case S_PLUS:
  646. *p4++ = '+';
  647. break;
  648. case S_MINUS:
  649. *p4++ = '-';
  650. break;
  651. case S_NONE:
  652. break;
  653. }
  654. memset4 (p4, '0', nzero);
  655. p4 += nzero;
  656. memcpy4 (p4, q, digits);
  657. return;
  658. }
  659. if (nblank < 0)
  660. {
  661. star_fill (p, w);
  662. goto done;
  663. }
  664. memset (p, ' ', nblank);
  665. p += nblank;
  666. switch (sign)
  667. {
  668. case S_PLUS:
  669. *p++ = '+';
  670. break;
  671. case S_MINUS:
  672. *p++ = '-';
  673. break;
  674. case S_NONE:
  675. break;
  676. }
  677. memset (p, '0', nzero);
  678. p += nzero;
  679. memcpy (p, q, digits);
  680. done:
  681. return;
  682. }
  683. /* Convert unsigned octal to ascii. */
  684. static const char *
  685. otoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
  686. {
  687. char *p;
  688. assert (len >= GFC_OTOA_BUF_SIZE);
  689. if (n == 0)
  690. return "0";
  691. p = buffer + GFC_OTOA_BUF_SIZE - 1;
  692. *p = '\0';
  693. while (n != 0)
  694. {
  695. *--p = '0' + (n & 7);
  696. n >>= 3;
  697. }
  698. return p;
  699. }
  700. /* Convert unsigned binary to ascii. */
  701. static const char *
  702. btoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
  703. {
  704. char *p;
  705. assert (len >= GFC_BTOA_BUF_SIZE);
  706. if (n == 0)
  707. return "0";
  708. p = buffer + GFC_BTOA_BUF_SIZE - 1;
  709. *p = '\0';
  710. while (n != 0)
  711. {
  712. *--p = '0' + (n & 1);
  713. n >>= 1;
  714. }
  715. return p;
  716. }
  717. /* The following three functions, btoa_big, otoa_big, and ztoa_big, are needed
  718. to convert large reals with kind sizes that exceed the largest integer type
  719. available on certain platforms. In these cases, byte by byte conversion is
  720. performed. Endianess is taken into account. */
  721. /* Conversion to binary. */
  722. static const char *
  723. btoa_big (const char *s, char *buffer, int len, GFC_UINTEGER_LARGEST *n)
  724. {
  725. char *q;
  726. int i, j;
  727. q = buffer;
  728. if (big_endian)
  729. {
  730. const char *p = s;
  731. for (i = 0; i < len; i++)
  732. {
  733. char c = *p;
  734. /* Test for zero. Needed by write_boz later. */
  735. if (*p != 0)
  736. *n = 1;
  737. for (j = 0; j < 8; j++)
  738. {
  739. *q++ = (c & 128) ? '1' : '0';
  740. c <<= 1;
  741. }
  742. p++;
  743. }
  744. }
  745. else
  746. {
  747. const char *p = s + len - 1;
  748. for (i = 0; i < len; i++)
  749. {
  750. char c = *p;
  751. /* Test for zero. Needed by write_boz later. */
  752. if (*p != 0)
  753. *n = 1;
  754. for (j = 0; j < 8; j++)
  755. {
  756. *q++ = (c & 128) ? '1' : '0';
  757. c <<= 1;
  758. }
  759. p--;
  760. }
  761. }
  762. *q = '\0';
  763. if (*n == 0)
  764. return "0";
  765. /* Move past any leading zeros. */
  766. while (*buffer == '0')
  767. buffer++;
  768. return buffer;
  769. }
  770. /* Conversion to octal. */
  771. static const char *
  772. otoa_big (const char *s, char *buffer, int len, GFC_UINTEGER_LARGEST *n)
  773. {
  774. char *q;
  775. int i, j, k;
  776. uint8_t octet;
  777. q = buffer + GFC_OTOA_BUF_SIZE - 1;
  778. *q = '\0';
  779. i = k = octet = 0;
  780. if (big_endian)
  781. {
  782. const char *p = s + len - 1;
  783. char c = *p;
  784. while (i < len)
  785. {
  786. /* Test for zero. Needed by write_boz later. */
  787. if (*p != 0)
  788. *n = 1;
  789. for (j = 0; j < 3 && i < len; j++)
  790. {
  791. octet |= (c & 1) << j;
  792. c >>= 1;
  793. if (++k > 7)
  794. {
  795. i++;
  796. k = 0;
  797. c = *--p;
  798. }
  799. }
  800. *--q = '0' + octet;
  801. octet = 0;
  802. }
  803. }
  804. else
  805. {
  806. const char *p = s;
  807. char c = *p;
  808. while (i < len)
  809. {
  810. /* Test for zero. Needed by write_boz later. */
  811. if (*p != 0)
  812. *n = 1;
  813. for (j = 0; j < 3 && i < len; j++)
  814. {
  815. octet |= (c & 1) << j;
  816. c >>= 1;
  817. if (++k > 7)
  818. {
  819. i++;
  820. k = 0;
  821. c = *++p;
  822. }
  823. }
  824. *--q = '0' + octet;
  825. octet = 0;
  826. }
  827. }
  828. if (*n == 0)
  829. return "0";
  830. /* Move past any leading zeros. */
  831. while (*q == '0')
  832. q++;
  833. return q;
  834. }
  835. /* Conversion to hexidecimal. */
  836. static const char *
  837. ztoa_big (const char *s, char *buffer, int len, GFC_UINTEGER_LARGEST *n)
  838. {
  839. static char a[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
  840. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  841. char *q;
  842. uint8_t h, l;
  843. int i;
  844. q = buffer;
  845. if (big_endian)
  846. {
  847. const char *p = s;
  848. for (i = 0; i < len; i++)
  849. {
  850. /* Test for zero. Needed by write_boz later. */
  851. if (*p != 0)
  852. *n = 1;
  853. h = (*p >> 4) & 0x0F;
  854. l = *p++ & 0x0F;
  855. *q++ = a[h];
  856. *q++ = a[l];
  857. }
  858. }
  859. else
  860. {
  861. const char *p = s + len - 1;
  862. for (i = 0; i < len; i++)
  863. {
  864. /* Test for zero. Needed by write_boz later. */
  865. if (*p != 0)
  866. *n = 1;
  867. h = (*p >> 4) & 0x0F;
  868. l = *p-- & 0x0F;
  869. *q++ = a[h];
  870. *q++ = a[l];
  871. }
  872. }
  873. *q = '\0';
  874. if (*n == 0)
  875. return "0";
  876. /* Move past any leading zeros. */
  877. while (*buffer == '0')
  878. buffer++;
  879. return buffer;
  880. }
  881. /* gfc_itoa()-- Integer to decimal conversion.
  882. The itoa function is a widespread non-standard extension to standard
  883. C, often declared in <stdlib.h>. Even though the itoa defined here
  884. is a static function we take care not to conflict with any prior
  885. non-static declaration. Hence the 'gfc_' prefix, which is normally
  886. reserved for functions with external linkage. */
  887. static const char *
  888. gfc_itoa (GFC_INTEGER_LARGEST n, char *buffer, size_t len)
  889. {
  890. int negative;
  891. char *p;
  892. GFC_UINTEGER_LARGEST t;
  893. assert (len >= GFC_ITOA_BUF_SIZE);
  894. if (n == 0)
  895. return "0";
  896. negative = 0;
  897. t = n;
  898. if (n < 0)
  899. {
  900. negative = 1;
  901. t = -n; /*must use unsigned to protect from overflow*/
  902. }
  903. p = buffer + GFC_ITOA_BUF_SIZE - 1;
  904. *p = '\0';
  905. while (t != 0)
  906. {
  907. *--p = '0' + (t % 10);
  908. t /= 10;
  909. }
  910. if (negative)
  911. *--p = '-';
  912. return p;
  913. }
  914. void
  915. write_i (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
  916. {
  917. write_decimal (dtp, f, p, len, (void *) gfc_itoa);
  918. }
  919. void
  920. write_b (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
  921. {
  922. const char *p;
  923. char itoa_buf[GFC_BTOA_BUF_SIZE];
  924. GFC_UINTEGER_LARGEST n = 0;
  925. if (len > (int) sizeof (GFC_UINTEGER_LARGEST))
  926. {
  927. p = btoa_big (source, itoa_buf, len, &n);
  928. write_boz (dtp, f, p, n);
  929. }
  930. else
  931. {
  932. n = extract_uint (source, len);
  933. p = btoa (n, itoa_buf, sizeof (itoa_buf));
  934. write_boz (dtp, f, p, n);
  935. }
  936. }
  937. void
  938. write_o (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
  939. {
  940. const char *p;
  941. char itoa_buf[GFC_OTOA_BUF_SIZE];
  942. GFC_UINTEGER_LARGEST n = 0;
  943. if (len > (int) sizeof (GFC_UINTEGER_LARGEST))
  944. {
  945. p = otoa_big (source, itoa_buf, len, &n);
  946. write_boz (dtp, f, p, n);
  947. }
  948. else
  949. {
  950. n = extract_uint (source, len);
  951. p = otoa (n, itoa_buf, sizeof (itoa_buf));
  952. write_boz (dtp, f, p, n);
  953. }
  954. }
  955. void
  956. write_z (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
  957. {
  958. const char *p;
  959. char itoa_buf[GFC_XTOA_BUF_SIZE];
  960. GFC_UINTEGER_LARGEST n = 0;
  961. if (len > (int) sizeof (GFC_UINTEGER_LARGEST))
  962. {
  963. p = ztoa_big (source, itoa_buf, len, &n);
  964. write_boz (dtp, f, p, n);
  965. }
  966. else
  967. {
  968. n = extract_uint (source, len);
  969. p = gfc_xtoa (n, itoa_buf, sizeof (itoa_buf));
  970. write_boz (dtp, f, p, n);
  971. }
  972. }
  973. void
  974. write_d (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
  975. {
  976. write_float (dtp, f, p, len, 0);
  977. }
  978. void
  979. write_e (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
  980. {
  981. write_float (dtp, f, p, len, 0);
  982. }
  983. void
  984. write_f (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
  985. {
  986. write_float (dtp, f, p, len, 0);
  987. }
  988. void
  989. write_en (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
  990. {
  991. write_float (dtp, f, p, len, 0);
  992. }
  993. void
  994. write_es (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
  995. {
  996. write_float (dtp, f, p, len, 0);
  997. }
  998. /* Take care of the X/TR descriptor. */
  999. void
  1000. write_x (st_parameter_dt *dtp, int len, int nspaces)
  1001. {
  1002. char *p;
  1003. p = write_block (dtp, len);
  1004. if (p == NULL)
  1005. return;
  1006. if (nspaces > 0 && len - nspaces >= 0)
  1007. {
  1008. if (unlikely (is_char4_unit (dtp)))
  1009. {
  1010. gfc_char4_t *p4 = (gfc_char4_t *) p;
  1011. memset4 (&p4[len - nspaces], ' ', nspaces);
  1012. }
  1013. else
  1014. memset (&p[len - nspaces], ' ', nspaces);
  1015. }
  1016. }
  1017. /* List-directed writing. */
  1018. /* Write a single character to the output. Returns nonzero if
  1019. something goes wrong. */
  1020. static int
  1021. write_char (st_parameter_dt *dtp, int c)
  1022. {
  1023. char *p;
  1024. p = write_block (dtp, 1);
  1025. if (p == NULL)
  1026. return 1;
  1027. if (unlikely (is_char4_unit (dtp)))
  1028. {
  1029. gfc_char4_t *p4 = (gfc_char4_t *) p;
  1030. *p4 = c;
  1031. return 0;
  1032. }
  1033. *p = (uchar) c;
  1034. return 0;
  1035. }
  1036. /* Write a list-directed logical value. */
  1037. static void
  1038. write_logical (st_parameter_dt *dtp, const char *source, int length)
  1039. {
  1040. write_char (dtp, extract_int (source, length) ? 'T' : 'F');
  1041. }
  1042. /* Write a list-directed integer value. */
  1043. static void
  1044. write_integer (st_parameter_dt *dtp, const char *source, int length)
  1045. {
  1046. char *p;
  1047. const char *q;
  1048. int digits;
  1049. int width;
  1050. char itoa_buf[GFC_ITOA_BUF_SIZE];
  1051. q = gfc_itoa (extract_int (source, length), itoa_buf, sizeof (itoa_buf));
  1052. switch (length)
  1053. {
  1054. case 1:
  1055. width = 4;
  1056. break;
  1057. case 2:
  1058. width = 6;
  1059. break;
  1060. case 4:
  1061. width = 11;
  1062. break;
  1063. case 8:
  1064. width = 20;
  1065. break;
  1066. default:
  1067. width = 0;
  1068. break;
  1069. }
  1070. digits = strlen (q);
  1071. if (width < digits)
  1072. width = digits;
  1073. p = write_block (dtp, width);
  1074. if (p == NULL)
  1075. return;
  1076. if (unlikely (is_char4_unit (dtp)))
  1077. {
  1078. gfc_char4_t *p4 = (gfc_char4_t *) p;
  1079. if (dtp->u.p.no_leading_blank)
  1080. {
  1081. memcpy4 (p4, q, digits);
  1082. memset4 (p4 + digits, ' ', width - digits);
  1083. }
  1084. else
  1085. {
  1086. memset4 (p4, ' ', width - digits);
  1087. memcpy4 (p4 + width - digits, q, digits);
  1088. }
  1089. return;
  1090. }
  1091. if (dtp->u.p.no_leading_blank)
  1092. {
  1093. memcpy (p, q, digits);
  1094. memset (p + digits, ' ', width - digits);
  1095. }
  1096. else
  1097. {
  1098. memset (p, ' ', width - digits);
  1099. memcpy (p + width - digits, q, digits);
  1100. }
  1101. }
  1102. /* Write a list-directed string. We have to worry about delimiting
  1103. the strings if the file has been opened in that mode. */
  1104. #define DELIM 1
  1105. #define NODELIM 0
  1106. static void
  1107. write_character (st_parameter_dt *dtp, const char *source, int kind, int length, int mode)
  1108. {
  1109. int i, extra;
  1110. char *p, d;
  1111. if (mode == DELIM)
  1112. {
  1113. switch (dtp->u.p.current_unit->delim_status)
  1114. {
  1115. case DELIM_APOSTROPHE:
  1116. d = '\'';
  1117. break;
  1118. case DELIM_QUOTE:
  1119. d = '"';
  1120. break;
  1121. default:
  1122. d = ' ';
  1123. break;
  1124. }
  1125. }
  1126. else
  1127. d = ' ';
  1128. if (kind == 1)
  1129. {
  1130. if (d == ' ')
  1131. extra = 0;
  1132. else
  1133. {
  1134. extra = 2;
  1135. for (i = 0; i < length; i++)
  1136. if (source[i] == d)
  1137. extra++;
  1138. }
  1139. p = write_block (dtp, length + extra);
  1140. if (p == NULL)
  1141. return;
  1142. if (unlikely (is_char4_unit (dtp)))
  1143. {
  1144. gfc_char4_t d4 = (gfc_char4_t) d;
  1145. gfc_char4_t *p4 = (gfc_char4_t *) p;
  1146. if (d4 == ' ')
  1147. memcpy4 (p4, source, length);
  1148. else
  1149. {
  1150. *p4++ = d4;
  1151. for (i = 0; i < length; i++)
  1152. {
  1153. *p4++ = (gfc_char4_t) source[i];
  1154. if (source[i] == d)
  1155. *p4++ = d4;
  1156. }
  1157. *p4 = d4;
  1158. }
  1159. return;
  1160. }
  1161. if (d == ' ')
  1162. memcpy (p, source, length);
  1163. else
  1164. {
  1165. *p++ = d;
  1166. for (i = 0; i < length; i++)
  1167. {
  1168. *p++ = source[i];
  1169. if (source[i] == d)
  1170. *p++ = d;
  1171. }
  1172. *p = d;
  1173. }
  1174. }
  1175. else
  1176. {
  1177. if (d == ' ')
  1178. {
  1179. if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
  1180. write_utf8_char4 (dtp, (gfc_char4_t *) source, length, 0);
  1181. else
  1182. write_default_char4 (dtp, (gfc_char4_t *) source, length, 0);
  1183. }
  1184. else
  1185. {
  1186. p = write_block (dtp, 1);
  1187. *p = d;
  1188. if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
  1189. write_utf8_char4 (dtp, (gfc_char4_t *) source, length, 0);
  1190. else
  1191. write_default_char4 (dtp, (gfc_char4_t *) source, length, 0);
  1192. p = write_block (dtp, 1);
  1193. *p = d;
  1194. }
  1195. }
  1196. }
  1197. /* Set an fnode to default format. */
  1198. static void
  1199. set_fnode_default (st_parameter_dt *dtp, fnode *f, int length)
  1200. {
  1201. f->format = FMT_G;
  1202. switch (length)
  1203. {
  1204. case 4:
  1205. f->u.real.w = 16;
  1206. f->u.real.d = 9;
  1207. f->u.real.e = 2;
  1208. break;
  1209. case 8:
  1210. f->u.real.w = 25;
  1211. f->u.real.d = 17;
  1212. f->u.real.e = 3;
  1213. break;
  1214. case 10:
  1215. f->u.real.w = 30;
  1216. f->u.real.d = 21;
  1217. f->u.real.e = 4;
  1218. break;
  1219. case 16:
  1220. f->u.real.w = 45;
  1221. f->u.real.d = 36;
  1222. f->u.real.e = 4;
  1223. break;
  1224. default:
  1225. internal_error (&dtp->common, "bad real kind");
  1226. break;
  1227. }
  1228. }
  1229. /* Output a real number with default format. To guarantee that a
  1230. binary -> decimal -> binary roundtrip conversion recovers the
  1231. original value, IEEE 754-2008 requires 9, 17, 21 and 36 significant
  1232. digits for REAL kinds 4, 8, 10, and 16, respectively. Thus, we use
  1233. 1PG16.9E2 for REAL(4), 1PG25.17E3 for REAL(8), 1PG30.21E4 for
  1234. REAL(10) and 1PG45.36E4 for REAL(16). The exception is that the
  1235. Fortran standard requires outputting an extra digit when the scale
  1236. factor is 1 and when the magnitude of the value is such that E
  1237. editing is used. However, gfortran compensates for this, and thus
  1238. for list formatted the same number of significant digits is
  1239. generated both when using F and E editing. */
  1240. void
  1241. write_real (st_parameter_dt *dtp, const char *source, int length)
  1242. {
  1243. fnode f ;
  1244. int org_scale = dtp->u.p.scale_factor;
  1245. dtp->u.p.scale_factor = 1;
  1246. set_fnode_default (dtp, &f, length);
  1247. write_float (dtp, &f, source , length, 1);
  1248. dtp->u.p.scale_factor = org_scale;
  1249. }
  1250. /* Similar to list formatted REAL output, for kPG0 where k > 0 we
  1251. compensate for the extra digit. */
  1252. void
  1253. write_real_g0 (st_parameter_dt *dtp, const char *source, int length, int d)
  1254. {
  1255. fnode f;
  1256. int comp_d;
  1257. set_fnode_default (dtp, &f, length);
  1258. if (d > 0)
  1259. f.u.real.d = d;
  1260. /* Compensate for extra digits when using scale factor, d is not
  1261. specified, and the magnitude is such that E editing is used. */
  1262. if (dtp->u.p.scale_factor > 0 && d == 0)
  1263. comp_d = 1;
  1264. else
  1265. comp_d = 0;
  1266. dtp->u.p.g0_no_blanks = 1;
  1267. write_float (dtp, &f, source , length, comp_d);
  1268. dtp->u.p.g0_no_blanks = 0;
  1269. }
  1270. static void
  1271. write_complex (st_parameter_dt *dtp, const char *source, int kind, size_t size)
  1272. {
  1273. char semi_comma =
  1274. dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';';
  1275. if (write_char (dtp, '('))
  1276. return;
  1277. write_real (dtp, source, kind);
  1278. if (write_char (dtp, semi_comma))
  1279. return;
  1280. write_real (dtp, source + size / 2, kind);
  1281. write_char (dtp, ')');
  1282. }
  1283. /* Write the separator between items. */
  1284. static void
  1285. write_separator (st_parameter_dt *dtp)
  1286. {
  1287. char *p;
  1288. p = write_block (dtp, options.separator_len);
  1289. if (p == NULL)
  1290. return;
  1291. if (unlikely (is_char4_unit (dtp)))
  1292. {
  1293. gfc_char4_t *p4 = (gfc_char4_t *) p;
  1294. memcpy4 (p4, options.separator, options.separator_len);
  1295. }
  1296. else
  1297. memcpy (p, options.separator, options.separator_len);
  1298. }
  1299. /* Write an item with list formatting.
  1300. TODO: handle skipping to the next record correctly, particularly
  1301. with strings. */
  1302. static void
  1303. list_formatted_write_scalar (st_parameter_dt *dtp, bt type, void *p, int kind,
  1304. size_t size)
  1305. {
  1306. if (dtp->u.p.current_unit == NULL)
  1307. return;
  1308. if (dtp->u.p.first_item)
  1309. {
  1310. dtp->u.p.first_item = 0;
  1311. write_char (dtp, ' ');
  1312. }
  1313. else
  1314. {
  1315. if (type != BT_CHARACTER || !dtp->u.p.char_flag ||
  1316. (dtp->u.p.current_unit->delim_status != DELIM_NONE
  1317. && dtp->u.p.current_unit->delim_status != DELIM_UNSPECIFIED))
  1318. write_separator (dtp);
  1319. }
  1320. switch (type)
  1321. {
  1322. case BT_INTEGER:
  1323. write_integer (dtp, p, kind);
  1324. break;
  1325. case BT_LOGICAL:
  1326. write_logical (dtp, p, kind);
  1327. break;
  1328. case BT_CHARACTER:
  1329. write_character (dtp, p, kind, size, DELIM);
  1330. break;
  1331. case BT_REAL:
  1332. write_real (dtp, p, kind);
  1333. break;
  1334. case BT_COMPLEX:
  1335. write_complex (dtp, p, kind, size);
  1336. break;
  1337. default:
  1338. internal_error (&dtp->common, "list_formatted_write(): Bad type");
  1339. }
  1340. fbuf_flush_list (dtp->u.p.current_unit, LIST_WRITING);
  1341. dtp->u.p.char_flag = (type == BT_CHARACTER);
  1342. }
  1343. void
  1344. list_formatted_write (st_parameter_dt *dtp, bt type, void *p, int kind,
  1345. size_t size, size_t nelems)
  1346. {
  1347. size_t elem;
  1348. char *tmp;
  1349. size_t stride = type == BT_CHARACTER ?
  1350. size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
  1351. tmp = (char *) p;
  1352. /* Big loop over all the elements. */
  1353. for (elem = 0; elem < nelems; elem++)
  1354. {
  1355. dtp->u.p.item_count++;
  1356. list_formatted_write_scalar (dtp, type, tmp + elem * stride, kind, size);
  1357. }
  1358. }
  1359. /* NAMELIST OUTPUT
  1360. nml_write_obj writes a namelist object to the output stream. It is called
  1361. recursively for derived type components:
  1362. obj = is the namelist_info for the current object.
  1363. offset = the offset relative to the address held by the object for
  1364. derived type arrays.
  1365. base = is the namelist_info of the derived type, when obj is a
  1366. component.
  1367. base_name = the full name for a derived type, including qualifiers
  1368. if any.
  1369. The returned value is a pointer to the object beyond the last one
  1370. accessed, including nested derived types. Notice that the namelist is
  1371. a linear linked list of objects, including derived types and their
  1372. components. A tree, of sorts, is implied by the compound names of
  1373. the derived type components and this is how this function recurses through
  1374. the list. */
  1375. /* A generous estimate of the number of characters needed to print
  1376. repeat counts and indices, including commas, asterices and brackets. */
  1377. #define NML_DIGITS 20
  1378. static void
  1379. namelist_write_newline (st_parameter_dt *dtp)
  1380. {
  1381. if (!is_internal_unit (dtp))
  1382. {
  1383. #ifdef HAVE_CRLF
  1384. write_character (dtp, "\r\n", 1, 2, NODELIM);
  1385. #else
  1386. write_character (dtp, "\n", 1, 1, NODELIM);
  1387. #endif
  1388. return;
  1389. }
  1390. if (is_array_io (dtp))
  1391. {
  1392. gfc_offset record;
  1393. int finished;
  1394. char *p;
  1395. int length = dtp->u.p.current_unit->bytes_left;
  1396. p = write_block (dtp, length);
  1397. if (p == NULL)
  1398. return;
  1399. if (unlikely (is_char4_unit (dtp)))
  1400. {
  1401. gfc_char4_t *p4 = (gfc_char4_t *) p;
  1402. memset4 (p4, ' ', length);
  1403. }
  1404. else
  1405. memset (p, ' ', length);
  1406. /* Now that the current record has been padded out,
  1407. determine where the next record in the array is. */
  1408. record = next_array_record (dtp, dtp->u.p.current_unit->ls,
  1409. &finished);
  1410. if (finished)
  1411. dtp->u.p.current_unit->endfile = AT_ENDFILE;
  1412. else
  1413. {
  1414. /* Now seek to this record */
  1415. record = record * dtp->u.p.current_unit->recl;
  1416. if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
  1417. {
  1418. generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
  1419. return;
  1420. }
  1421. dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
  1422. }
  1423. }
  1424. else
  1425. write_character (dtp, " ", 1, 1, NODELIM);
  1426. }
  1427. static namelist_info *
  1428. nml_write_obj (st_parameter_dt *dtp, namelist_info * obj, index_type offset,
  1429. namelist_info * base, char * base_name)
  1430. {
  1431. int rep_ctr;
  1432. int num;
  1433. int nml_carry;
  1434. int len;
  1435. index_type obj_size;
  1436. index_type nelem;
  1437. size_t dim_i;
  1438. size_t clen;
  1439. index_type elem_ctr;
  1440. size_t obj_name_len;
  1441. void * p;
  1442. char cup;
  1443. char * obj_name;
  1444. char * ext_name;
  1445. char * q;
  1446. size_t ext_name_len;
  1447. char rep_buff[NML_DIGITS];
  1448. namelist_info * cmp;
  1449. namelist_info * retval = obj->next;
  1450. size_t base_name_len;
  1451. size_t base_var_name_len;
  1452. size_t tot_len;
  1453. /* Set the character to be used to separate values
  1454. to a comma or semi-colon. */
  1455. char semi_comma =
  1456. dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';';
  1457. /* Write namelist variable names in upper case. If a derived type,
  1458. nothing is output. If a component, base and base_name are set. */
  1459. if (obj->type != BT_DERIVED)
  1460. {
  1461. namelist_write_newline (dtp);
  1462. write_character (dtp, " ", 1, 1, NODELIM);
  1463. len = 0;
  1464. if (base)
  1465. {
  1466. len = strlen (base->var_name);
  1467. base_name_len = strlen (base_name);
  1468. for (dim_i = 0; dim_i < base_name_len; dim_i++)
  1469. {
  1470. cup = toupper ((int) base_name[dim_i]);
  1471. write_character (dtp, &cup, 1, 1, NODELIM);
  1472. }
  1473. }
  1474. clen = strlen (obj->var_name);
  1475. for (dim_i = len; dim_i < clen; dim_i++)
  1476. {
  1477. cup = toupper ((int) obj->var_name[dim_i]);
  1478. if (cup == '+')
  1479. cup = '%';
  1480. write_character (dtp, &cup, 1, 1, NODELIM);
  1481. }
  1482. write_character (dtp, "=", 1, 1, NODELIM);
  1483. }
  1484. /* Counts the number of data output on a line, including names. */
  1485. num = 1;
  1486. len = obj->len;
  1487. switch (obj->type)
  1488. {
  1489. case BT_REAL:
  1490. obj_size = size_from_real_kind (len);
  1491. break;
  1492. case BT_COMPLEX:
  1493. obj_size = size_from_complex_kind (len);
  1494. break;
  1495. case BT_CHARACTER:
  1496. obj_size = obj->string_length;
  1497. break;
  1498. default:
  1499. obj_size = len;
  1500. }
  1501. if (obj->var_rank)
  1502. obj_size = obj->size;
  1503. /* Set the index vector and count the number of elements. */
  1504. nelem = 1;
  1505. for (dim_i = 0; dim_i < (size_t) obj->var_rank; dim_i++)
  1506. {
  1507. obj->ls[dim_i].idx = GFC_DESCRIPTOR_LBOUND(obj, dim_i);
  1508. nelem = nelem * GFC_DESCRIPTOR_EXTENT (obj, dim_i);
  1509. }
  1510. /* Main loop to output the data held in the object. */
  1511. rep_ctr = 1;
  1512. for (elem_ctr = 0; elem_ctr < nelem; elem_ctr++)
  1513. {
  1514. /* Build the pointer to the data value. The offset is passed by
  1515. recursive calls to this function for arrays of derived types.
  1516. Is NULL otherwise. */
  1517. p = (void *)(obj->mem_pos + elem_ctr * obj_size);
  1518. p += offset;
  1519. /* Check for repeat counts of intrinsic types. */
  1520. if ((elem_ctr < (nelem - 1)) &&
  1521. (obj->type != BT_DERIVED) &&
  1522. !memcmp (p, (void*)(p + obj_size ), obj_size ))
  1523. {
  1524. rep_ctr++;
  1525. }
  1526. /* Execute a repeated output. Note the flag no_leading_blank that
  1527. is used in the functions used to output the intrinsic types. */
  1528. else
  1529. {
  1530. if (rep_ctr > 1)
  1531. {
  1532. snprintf(rep_buff, NML_DIGITS, " %d*", rep_ctr);
  1533. write_character (dtp, rep_buff, 1, strlen (rep_buff), NODELIM);
  1534. dtp->u.p.no_leading_blank = 1;
  1535. }
  1536. num++;
  1537. /* Output the data, if an intrinsic type, or recurse into this
  1538. routine to treat derived types. */
  1539. switch (obj->type)
  1540. {
  1541. case BT_INTEGER:
  1542. write_integer (dtp, p, len);
  1543. break;
  1544. case BT_LOGICAL:
  1545. write_logical (dtp, p, len);
  1546. break;
  1547. case BT_CHARACTER:
  1548. if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
  1549. write_character (dtp, p, 4, obj->string_length, DELIM);
  1550. else
  1551. write_character (dtp, p, 1, obj->string_length, DELIM);
  1552. break;
  1553. case BT_REAL:
  1554. write_real (dtp, p, len);
  1555. break;
  1556. case BT_COMPLEX:
  1557. dtp->u.p.no_leading_blank = 0;
  1558. num++;
  1559. write_complex (dtp, p, len, obj_size);
  1560. break;
  1561. case BT_DERIVED:
  1562. /* To treat a derived type, we need to build two strings:
  1563. ext_name = the name, including qualifiers that prepends
  1564. component names in the output - passed to
  1565. nml_write_obj.
  1566. obj_name = the derived type name with no qualifiers but %
  1567. appended. This is used to identify the
  1568. components. */
  1569. /* First ext_name => get length of all possible components */
  1570. base_name_len = base_name ? strlen (base_name) : 0;
  1571. base_var_name_len = base ? strlen (base->var_name) : 0;
  1572. ext_name_len = base_name_len + base_var_name_len
  1573. + strlen (obj->var_name) + obj->var_rank * NML_DIGITS + 1;
  1574. ext_name = xmalloc (ext_name_len);
  1575. memcpy (ext_name, base_name, base_name_len);
  1576. clen = strlen (obj->var_name + base_var_name_len);
  1577. memcpy (ext_name + base_name_len,
  1578. obj->var_name + base_var_name_len, clen);
  1579. /* Append the qualifier. */
  1580. tot_len = base_name_len + clen;
  1581. for (dim_i = 0; dim_i < (size_t) obj->var_rank; dim_i++)
  1582. {
  1583. if (!dim_i)
  1584. {
  1585. ext_name[tot_len] = '(';
  1586. tot_len++;
  1587. }
  1588. snprintf (ext_name + tot_len, ext_name_len - tot_len, "%d",
  1589. (int) obj->ls[dim_i].idx);
  1590. tot_len += strlen (ext_name + tot_len);
  1591. ext_name[tot_len] = ((int) dim_i == obj->var_rank - 1) ? ')' : ',';
  1592. tot_len++;
  1593. }
  1594. ext_name[tot_len] = '\0';
  1595. for (q = ext_name; *q; q++)
  1596. if (*q == '+')
  1597. *q = '%';
  1598. /* Now obj_name. */
  1599. obj_name_len = strlen (obj->var_name) + 1;
  1600. obj_name = xmalloc (obj_name_len + 1);
  1601. memcpy (obj_name, obj->var_name, obj_name_len-1);
  1602. memcpy (obj_name + obj_name_len-1, "%", 2);
  1603. /* Now loop over the components. Update the component pointer
  1604. with the return value from nml_write_obj => this loop jumps
  1605. past nested derived types. */
  1606. for (cmp = obj->next;
  1607. cmp && !strncmp (cmp->var_name, obj_name, obj_name_len);
  1608. cmp = retval)
  1609. {
  1610. retval = nml_write_obj (dtp, cmp,
  1611. (index_type)(p - obj->mem_pos),
  1612. obj, ext_name);
  1613. }
  1614. free (obj_name);
  1615. free (ext_name);
  1616. goto obj_loop;
  1617. default:
  1618. internal_error (&dtp->common, "Bad type for namelist write");
  1619. }
  1620. /* Reset the leading blank suppression, write a comma (or semi-colon)
  1621. and, if 5 values have been output, write a newline and advance
  1622. to column 2. Reset the repeat counter. */
  1623. dtp->u.p.no_leading_blank = 0;
  1624. if (obj->type == BT_CHARACTER)
  1625. {
  1626. if (dtp->u.p.nml_delim != '\0')
  1627. write_character (dtp, &semi_comma, 1, 1, NODELIM);
  1628. }
  1629. else
  1630. write_character (dtp, &semi_comma, 1, 1, NODELIM);
  1631. if (num > 5)
  1632. {
  1633. num = 0;
  1634. if (dtp->u.p.nml_delim == '\0')
  1635. write_character (dtp, &semi_comma, 1, 1, NODELIM);
  1636. namelist_write_newline (dtp);
  1637. write_character (dtp, " ", 1, 1, NODELIM);
  1638. }
  1639. rep_ctr = 1;
  1640. }
  1641. /* Cycle through and increment the index vector. */
  1642. obj_loop:
  1643. nml_carry = 1;
  1644. for (dim_i = 0; nml_carry && (dim_i < (size_t) obj->var_rank); dim_i++)
  1645. {
  1646. obj->ls[dim_i].idx += nml_carry ;
  1647. nml_carry = 0;
  1648. if (obj->ls[dim_i].idx > GFC_DESCRIPTOR_UBOUND(obj,dim_i))
  1649. {
  1650. obj->ls[dim_i].idx = GFC_DESCRIPTOR_LBOUND(obj,dim_i);
  1651. nml_carry = 1;
  1652. }
  1653. }
  1654. }
  1655. /* Return a pointer beyond the furthest object accessed. */
  1656. return retval;
  1657. }
  1658. /* This is the entry function for namelist writes. It outputs the name
  1659. of the namelist and iterates through the namelist by calls to
  1660. nml_write_obj. The call below has dummys in the arguments used in
  1661. the treatment of derived types. */
  1662. void
  1663. namelist_write (st_parameter_dt *dtp)
  1664. {
  1665. namelist_info * t1, *t2, *dummy = NULL;
  1666. index_type i;
  1667. index_type dummy_offset = 0;
  1668. char c;
  1669. char * dummy_name = NULL;
  1670. /* Set the delimiter for namelist output. */
  1671. switch (dtp->u.p.current_unit->delim_status)
  1672. {
  1673. case DELIM_APOSTROPHE:
  1674. dtp->u.p.nml_delim = '\'';
  1675. break;
  1676. case DELIM_QUOTE:
  1677. case DELIM_UNSPECIFIED:
  1678. dtp->u.p.nml_delim = '"';
  1679. break;
  1680. default:
  1681. dtp->u.p.nml_delim = '\0';
  1682. }
  1683. write_character (dtp, "&", 1, 1, NODELIM);
  1684. /* Write namelist name in upper case - f95 std. */
  1685. for (i = 0 ;i < dtp->namelist_name_len ;i++ )
  1686. {
  1687. c = toupper ((int) dtp->namelist_name[i]);
  1688. write_character (dtp, &c, 1 ,1, NODELIM);
  1689. }
  1690. if (dtp->u.p.ionml != NULL)
  1691. {
  1692. t1 = dtp->u.p.ionml;
  1693. while (t1 != NULL)
  1694. {
  1695. t2 = t1;
  1696. t1 = nml_write_obj (dtp, t2, dummy_offset, dummy, dummy_name);
  1697. }
  1698. }
  1699. namelist_write_newline (dtp);
  1700. write_character (dtp, " /", 1, 2, NODELIM);
  1701. }
  1702. #undef NML_DIGITS