binio.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /* Vis5D version 5.0 */
  2. /*
  3. Vis5D system for visualizing five dimensional gridded data sets
  4. Copyright (C) 1990 - 1997 Bill Hibbard, Johan Kellum, Brian Paul,
  5. Dave Santek, and Andre Battaiola.
  6. This program 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 1, or (at your option)
  9. any later version.
  10. This program 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. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /*
  19. * Functions to do binary I/O of floats, ints.
  20. *
  21. * >>>> These functions are built on top of Unix I/O functions, not stdio! <<<<
  22. *
  23. * The file format is assumed to be BIG-ENDIAN.
  24. * If this code is compiled with -DLITTLE and executes on a little endian
  25. * CPU then byte-swapping will be done.
  26. *
  27. * If an ANSI compiler is used prototypes and ANSI function declarations
  28. * are used. Otherwise use K&R conventions.
  29. *
  30. * If we're running on a CRAY (8-byte ints and floats), conversions will
  31. * be done as needed.
  32. */
  33. /*
  34. * Updates:
  35. *
  36. * April 13, 1995, brianp
  37. * added cray_to_ieee and iee_to_cray array conversion functions.
  38. * fixed potential cray bug in write_float4_array function.
  39. *
  40. */
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <unistd.h>
  44. #ifdef _CRAY
  45. # include <string.h>
  46. #include <grass/gis.h>
  47. #endif
  48. #include "binio.h"
  49. /**********************************************************************/
  50. /****** Byte Flipping *****/
  51. /**********************************************************************/
  52. #define FLIP4( n ) ( (n & 0xff000000) >> 24 \
  53. | (n & 0x00ff0000) >> 8 \
  54. | (n & 0x0000ff00) << 8 \
  55. | (n & 0x000000ff) << 24 )
  56. #define FLIP2( n ) (((unsigned short) (n & 0xff00)) >> 8 | (n & 0x00ff) << 8)
  57. /*
  58. * Flip the order of the 4 bytes in an array of 4-byte words.
  59. */
  60. void flip4(const unsigned int *src, unsigned int *dest, int n)
  61. {
  62. int i;
  63. for (i = 0; i < n; i++) {
  64. unsigned int tmp = src[i];
  65. dest[i] = FLIP4(tmp);
  66. }
  67. }
  68. /*
  69. * Flip the order of the 2 bytes in an array of 2-byte words.
  70. */
  71. void flip2(const unsigned short *src, unsigned short *dest, int n)
  72. {
  73. int i;
  74. for (i = 0; i < n; i++) {
  75. unsigned short tmp = src[i];
  76. dest[i] = FLIP2(tmp);
  77. }
  78. }
  79. #ifdef _CRAY
  80. /*****************************************************************************
  81. *
  82. * The following source code is in the public domain.
  83. * Specifically, we give to the public domain all rights for future licensing
  84. * of the source code, all resale rights, and all publishing rights.
  85. *
  86. * We ask, but do not require, that the following message be included in all
  87. * derived works:
  88. *
  89. * Portions developed at the National Center for Supercomputing Applications at
  90. * the University of Illinois at Urbana-Champaign.
  91. *
  92. * THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
  93. * SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
  94. * WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
  95. *
  96. ****************************************************************************/
  97. /** THESE ROUTINES MUST BE COMPILED ON THE CRAY ONLY SINCE THEY **/
  98. /** REQUIRE 8-BYTES PER C-TYPE LONG **/
  99. /* Cray to IEEE single precision */
  100. static void c_to_if(long *t, const long *f)
  101. {
  102. if (*f != 0) {
  103. *t = (((*f & 0x8000000000000000) | /* sign bit */
  104. ((((*f & 0x7fff000000000000) >> 48) - 16258) << 55)) + /* exp */
  105. (((*f & 0x00007fffff000000) + ((*f & 0x0000000000800000) << 1)) << 8)); /* mantissa */
  106. }
  107. else
  108. *t = *f;
  109. }
  110. #define C_TO_IF( T, F ) \
  111. if (F != 0) { \
  112. T = (((F & 0x8000000000000000) | \
  113. ((((F & 0x7fff000000000000) >> 48)-16258) << 55)) + \
  114. (((F & 0x00007fffff000000) + \
  115. ((F & 0x0000000000800000) << 1)) << 8)); \
  116. } \
  117. else { \
  118. T = F; \
  119. }
  120. /* IEEE single precison to Cray */
  121. static void if_to_c(long *t, const long *f)
  122. {
  123. if (*f != 0) {
  124. *t = (((*f & 0x8000000000000000) |
  125. ((*f & 0x7f80000000000000) >> 7) +
  126. (16258 << 48)) |
  127. (((*f & 0x007fffff00000000) >> 8) | (0x0000800000000000)));
  128. if ((*f << 1) == 0)
  129. *t = 0;
  130. }
  131. else
  132. *t = *f;
  133. }
  134. /* T and F must be longs! */
  135. #define IF_TO_C( T, F ) \
  136. if (F != 0) { \
  137. T = (((F & 0x8000000000000000) | \
  138. ((F & 0x7f80000000000000) >> 7) + \
  139. (16258 << 48)) | \
  140. (((F & 0x007fffff00000000) >> 8) | (0x0000800000000000))); \
  141. if ((F << 1) == 0) T = 0; \
  142. } \
  143. else { \
  144. T = F; \
  145. }
  146. /*
  147. * Convert an array of Cray 8-byte floats to an array of IEEE 4-byte floats.
  148. */
  149. void cray_to_ieee_array(long *dest, const float *source, int n)
  150. {
  151. long *dst;
  152. const long *src;
  153. long tmp1, tmp2;
  154. int i;
  155. dst = dest;
  156. src = (const long *)source;
  157. for (i = 0; i < n; i += 2) { /* add 1 in case n is odd */
  158. c_to_if(&tmp1, &src[i]);
  159. c_to_if(&tmp2, &src[i + 1]);
  160. *dst = (tmp1 & 0xffffffff00000000) | (tmp2 >> 32);
  161. dst++;
  162. }
  163. }
  164. /*
  165. * Convert an array of IEEE 4-byte floats to an array of 8-byte Cray floats.
  166. */
  167. void ieee_to_cray_array(float *dest, const long *source, int n)
  168. {
  169. long *dst;
  170. const long *src;
  171. int i;
  172. long ieee;
  173. src = source;
  174. dst = (long *)dest;
  175. for (i = 0; i < n; i++) {
  176. /* most significant 4-bytes of ieee contain bit pattern to convert */
  177. if ((i & 1) == 0) {
  178. /* get upper half */
  179. ieee = src[i / 2] & 0xffffffff00000000;
  180. }
  181. else {
  182. /* get lower half */
  183. ieee = src[i / 2] << 32;
  184. }
  185. if_to_c(dst, &ieee);
  186. dst++;
  187. }
  188. }
  189. #endif /*_CRAY*/
  190. /**********************************************************************/
  191. /***** Read Functions *****/
  192. /**********************************************************************/
  193. /*
  194. * Read a block of bytes.
  195. * Input: f - the file descriptor to read from.
  196. * b - address of buffer to read into.
  197. * n - number of bytes to read.
  198. * Return: number of bytes read, 0 if error.
  199. */
  200. int read_bytes(int f, void *b, int n)
  201. {
  202. return read(f, b, n);
  203. }
  204. /*
  205. * Read an array of 2-byte integers.
  206. * Input: f - file descriptor
  207. * iarray - address to put integers
  208. * n - number of integers to read.
  209. * Return: number of integers read.
  210. */
  211. int read_int2_array(int f, short *iarray, int n)
  212. {
  213. #ifdef _CRAY
  214. int i;
  215. signed char *buffer;
  216. int nread;
  217. buffer = (signed char *)G_malloc(n * 2);
  218. if (!buffer)
  219. return 0;
  220. nread = read(f, buffer, n * 2);
  221. if (nread <= 0)
  222. return 0;
  223. nread /= 2;
  224. for (i = 0; i < nread; i++) {
  225. /* don't forget about sign extension! */
  226. iarray[i] = (buffer[i * 2] * 256) | buffer[i * 2 + 1];
  227. }
  228. G_free(buffer);
  229. return nread;
  230. #else
  231. int nread = read(f, iarray, n * 2);
  232. if (nread <= 0)
  233. return 0;
  234. #ifdef LITTLE
  235. flip2((const unsigned short *)iarray, (unsigned short *)iarray,
  236. nread / 2);
  237. #endif
  238. return nread / 2;
  239. #endif
  240. }
  241. /*
  242. * Read an array of unsigned 2-byte integers.
  243. * Input: f - file descriptor
  244. * iarray - address to put integers
  245. * n - number of integers to read.
  246. * Return: number of integers read.
  247. */
  248. int read_uint2_array(int f, unsigned short *iarray, int n)
  249. {
  250. #ifdef _CRAY
  251. int i;
  252. unsigned char *buffer;
  253. int nread;
  254. buffer = (unsigned char *)G_malloc(n * 2);
  255. if (!buffer)
  256. return 0;
  257. nread = read(f, buffer, n * 2);
  258. if (nread <= 0)
  259. return 0;
  260. nread /= 2;
  261. for (i = 0; i < nread; i++) {
  262. iarray[i] = (buffer[i * 2] << 8) | buffer[i * 2 + 1];
  263. }
  264. G_free(buffer);
  265. return nread;
  266. #else
  267. int nread = read(f, iarray, n * 2);
  268. if (nread <= 0)
  269. return 0;
  270. #ifdef LITTLE
  271. flip2(iarray, iarray, nread / 2);
  272. #endif
  273. return nread / 2;
  274. #endif
  275. }
  276. /*
  277. * Read a 4-byte integer.
  278. * Input: f - the file descriptor to read from
  279. * i - pointer to integer to put result into.
  280. * Return: 1 = ok, 0 = error
  281. */
  282. int read_int4(int f, int *i)
  283. {
  284. #ifdef LITTLE
  285. /* read big endian and convert to little endian */
  286. unsigned int n;
  287. if (read(f, &n, 4) == 4) {
  288. *i = FLIP4(n);
  289. return 1;
  290. }
  291. else {
  292. return 0;
  293. }
  294. #else
  295. if (read(f, i, 4) == 4) {
  296. # ifdef _CRAY
  297. *i = *i >> 32;
  298. # endif
  299. return 1;
  300. }
  301. else {
  302. return 0;
  303. }
  304. #endif
  305. }
  306. /*
  307. * Read an array of 4-byte integers.
  308. * Input: f - file descriptor
  309. * iarray - address to put integers
  310. * n - number of integers to read.
  311. * Return: number of integers read.
  312. */
  313. int read_int4_array(int f, int *iarray, int n)
  314. {
  315. #ifdef _CRAY
  316. int j, nread;
  317. int *buffer;
  318. buffer = (int *)G_malloc((n + 1) * 4);
  319. if (!buffer)
  320. return 0;
  321. nread = read(f, buffer, 4 * n);
  322. if (nread <= 0) {
  323. return 0;
  324. }
  325. nread /= 4;
  326. for (j = 0; j < nread; j++) {
  327. if ((j & 1) == 0) {
  328. iarray[j] = buffer[j / 2] >> 32;
  329. }
  330. else {
  331. iarray[j] = buffer[j / 2] & 0xffffffff;
  332. }
  333. }
  334. G_free(buffer);
  335. return nread;
  336. #else
  337. int nread = read(f, iarray, 4 * n);
  338. if (nread <= 0)
  339. return 0;
  340. # ifdef LITTLE
  341. flip4((const unsigned int *)iarray, (unsigned int *)iarray, nread / 4);
  342. # endif
  343. return nread / 4;
  344. #endif
  345. }
  346. /*
  347. * Read a 4-byte IEEE float.
  348. * Input: f - the file descriptor to read from.
  349. * x - pointer to float to put result into.
  350. * Return: 1 = ok, 0 = error
  351. */
  352. int read_float4(int f, float *x)
  353. {
  354. #ifdef _CRAY
  355. long buffer = 0;
  356. if (read(f, &buffer, 4) == 4) {
  357. /* convert IEEE float (buffer) to Cray float (x) */
  358. if_to_c((long *)x, &buffer);
  359. return 1;
  360. }
  361. return 0;
  362. #else
  363. # ifdef LITTLE
  364. unsigned int n, *iptr;
  365. if (read(f, &n, 4) == 4) {
  366. iptr = (unsigned int *)x;
  367. *iptr = FLIP4(n);
  368. return 1;
  369. }
  370. else {
  371. return 0;
  372. }
  373. # else
  374. if (read(f, x, 4) == 4) {
  375. return 1;
  376. }
  377. else {
  378. return 0;
  379. }
  380. # endif
  381. #endif
  382. }
  383. /*
  384. * Read an array of 4-byte IEEE floats.
  385. * Input: f - file descriptor
  386. * x - address to put floats
  387. * n - number of floats to read.
  388. * Return: number of floats read.
  389. */
  390. int read_float4_array(int f, float *x, int n)
  391. {
  392. #ifdef _CRAY
  393. /* read IEEE floats into buffer, then convert to Cray format */
  394. long *buffer;
  395. int i, nread;
  396. buffer = (long *)G_malloc((n + 1) * 4);
  397. if (!buffer)
  398. return 0;
  399. nread = read(f, buffer, n * 4);
  400. if (nread <= 0)
  401. return 0;
  402. nread /= 4;
  403. ieee_to_cray_array(x, buffer, nread);
  404. G_free(buffer);
  405. return nread;
  406. #else
  407. int nread = read(f, x, 4 * n);
  408. if (nread <= 0)
  409. return 0;
  410. #ifdef LITTLE
  411. flip4((const unsigned int *)x, (unsigned int *)x, nread / 4);
  412. #endif
  413. return nread / 4;
  414. #endif
  415. }
  416. /*
  417. * Read a block of memory.
  418. * Input: f - file descriptor
  419. * data - address of first byte
  420. * elements - number of elements to read
  421. * elsize - size of each element to read (1, 2 or 4)
  422. * Return: number of elements written
  423. */
  424. int read_block(int f, void *data, int elements, int elsize)
  425. {
  426. if (elsize == 1) {
  427. return read(f, data, elements);
  428. }
  429. else if (elsize == 2) {
  430. #ifdef LITTLE
  431. int n;
  432. n = read(f, data, elements * 2) / 2;
  433. if (n == elements) {
  434. flip2((const unsigned short *)data, (unsigned short *)data,
  435. elements);
  436. }
  437. return n;
  438. #else
  439. return read(f, data, elements * 2) / 2;
  440. #endif
  441. }
  442. else if (elsize == 4) {
  443. #ifdef LITTLE
  444. int n;
  445. n = read(f, data, elements * 4) / 4;
  446. if (n == elements) {
  447. flip4((const unsigned int *)data, (unsigned int *)data, elements);
  448. }
  449. return n;
  450. #else
  451. return read(f, data, elements * 4) / 4;
  452. #endif
  453. }
  454. else {
  455. printf("Fatal error in read_block(): bad elsize (%d)\n", elsize);
  456. abort();
  457. }
  458. return 0;
  459. }
  460. /**********************************************************************/
  461. /***** Write Functions *****/
  462. /**********************************************************************/
  463. /*
  464. * Write a block of bytes.
  465. * Input: f - the file descriptor to write to.
  466. * b - address of buffer to write.
  467. * n - number of bytes to write.
  468. * Return: number of bytes written, 0 if error.
  469. */
  470. int write_bytes(int f, const void *b, int n)
  471. {
  472. return write(f, b, n);
  473. }
  474. /*
  475. * Write an array of 2-byte integers.
  476. * Input: f - file descriptor
  477. * iarray - address to put integers
  478. * n - number of integers to write.
  479. * Return: number of integers written
  480. */
  481. int write_int2_array(int f, const short *iarray, int n)
  482. {
  483. #ifdef _CRAY
  484. printf("write_int2_array not implemented!\n");
  485. exit(1);
  486. #else
  487. int nwritten;
  488. #ifdef LITTLE
  489. flip2((const unsigned short *)iarray, (unsigned short *)iarray, n);
  490. #endif
  491. nwritten = write(f, iarray, 2 * n);
  492. #ifdef LITTLE
  493. flip2((const unsigned short *)iarray, (unsigned short *)iarray, n);
  494. #endif
  495. if (nwritten <= 0)
  496. return 0;
  497. return nwritten / 2;
  498. #endif
  499. }
  500. /*
  501. * Write an array of 2-byte unsigned integers.
  502. * Input: f - file descriptor
  503. * iarray - address to put integers
  504. * n - number of integers to write.
  505. * Return: number of integers written
  506. */
  507. int write_uint2_array(int f, const unsigned short *iarray, int n)
  508. {
  509. #ifdef _CRAY
  510. int i, nwritten;
  511. unsigned char *buffer;
  512. buffer = (unsigned char *)G_malloc(2 * n);
  513. if (!buffer)
  514. return 0;
  515. for (i = 0; i < n; i++) {
  516. buffer[i * 2] = (iarray[i] >> 8) & 0xff;
  517. buffer[i * 2 + 1] = iarray[i] & 0xff;
  518. }
  519. nwritten = write(f, buffer, 2 * n);
  520. G_free(buffer);
  521. if (nwritten <= 0)
  522. return 0;
  523. else
  524. return nwritten / 2;
  525. #else
  526. int nwritten;
  527. #ifdef LITTLE
  528. flip2(iarray, (unsigned short *)iarray, n);
  529. #endif
  530. nwritten = write(f, iarray, 2 * n);
  531. #ifdef LITTLE
  532. flip2(iarray, (unsigned short *)iarray, n);
  533. #endif
  534. if (nwritten <= 0)
  535. return 0;
  536. else
  537. return nwritten / 2;
  538. #endif
  539. }
  540. /*
  541. * Write a 4-byte integer.
  542. *Input: f - the file descriptor
  543. * i - the integer
  544. * Return: 1 = ok, 0 = error
  545. */
  546. int write_int4(int f, int i)
  547. {
  548. #ifdef _CRAY
  549. i = i << 32;
  550. return write(f, &i, 4) > 0;
  551. #else
  552. # ifdef LITTLE
  553. i = FLIP4(i);
  554. # endif
  555. return write(f, &i, 4) > 0;
  556. #endif
  557. }
  558. /*
  559. * Write an array of 4-byte integers.
  560. * Input: f - the file descriptor
  561. * i - the array of ints
  562. * n - the number of ints in array
  563. * Return: number of integers written.
  564. */
  565. int write_int4_array(int f, const int *i, int n)
  566. {
  567. #ifdef _CRAY
  568. int j, nwritten;
  569. char *buf, *b, *ptr;
  570. b = buf = (char *)G_malloc(n * 4 + 8);
  571. if (!b)
  572. return 0;
  573. ptr = (char *)i;
  574. for (j = 0; j < n; j++) {
  575. ptr += 4; /* skip upper 4 bytes */
  576. *b++ = *ptr++;
  577. *b++ = *ptr++;
  578. *b++ = *ptr++;
  579. *b++ = *ptr++;
  580. }
  581. nwritten = write(f, buf, 4 * n);
  582. G_free(buf);
  583. if (nwritten <= 0)
  584. return 0;
  585. else
  586. return nwritten / 4;
  587. #else
  588. # ifdef LITTLE
  589. int nwritten;
  590. flip4((const unsigned int *)i, (unsigned int *)i, n);
  591. nwritten = write(f, i, 4 * n);
  592. flip4((const unsigned int *)i, (unsigned int *)i, n);
  593. if (nwritten <= 0)
  594. return 0;
  595. else
  596. return nwritten / 4;
  597. # else
  598. return write(f, i, 4 * n) / 4;
  599. # endif
  600. #endif
  601. }
  602. /*
  603. * Write a 4-byte IEEE float.
  604. * Input: f - the file descriptor
  605. * x - the float
  606. * Return: 1 = ok, 0 = error
  607. */
  608. int write_float4(int f, float x)
  609. {
  610. #ifdef _CRAY
  611. char buffer[8];
  612. c_to_if((long *)buffer, (const long *)&x);
  613. return write(f, buffer, 4) > 0;
  614. #else
  615. # ifdef LITTLE
  616. float y;
  617. unsigned int *iptr = (unsigned int *)&y, temp;
  618. y = (float)x;
  619. temp = FLIP4(*iptr);
  620. return write(f, &temp, 4) > 0;
  621. # else
  622. float y;
  623. y = (float)x;
  624. return write(f, &y, 4) > 0;
  625. # endif
  626. #endif
  627. }
  628. /*
  629. * Write an array of 4-byte IEEE floating point numbers.
  630. * Input: f - the file descriptor
  631. * x - the array of floats
  632. * n - number of floats in array
  633. * Return: number of float written.
  634. */
  635. int write_float4_array(int f, const float *x, int n)
  636. {
  637. #ifdef _CRAY
  638. /* convert cray floats to IEEE and put into buffer */
  639. int nwritten;
  640. long *buffer;
  641. buffer = (long *)G_malloc(n * 4 + 8);
  642. if (!buffer)
  643. return 0;
  644. cray_to_ieee_array(buffer, x, n);
  645. nwritten = write(f, buffer, 4 * n);
  646. G_free(buffer);
  647. if (nwritten <= 0)
  648. return 0;
  649. else
  650. return nwritten / 4;
  651. #else
  652. # ifdef LITTLE
  653. int nwritten;
  654. flip4((const unsigned int *)x, (unsigned int *)x, n);
  655. nwritten = write(f, x, 4 * n);
  656. flip4((const unsigned int *)x, (unsigned int *)x, n);
  657. if (nwritten <= 0)
  658. return 0;
  659. else
  660. return nwritten / 4;
  661. # else
  662. return write(f, x, 4 * n) / 4;
  663. # endif
  664. #endif
  665. }
  666. /*
  667. * Write a block of memory.
  668. * Input: f - file descriptor
  669. * data - address of first byte
  670. * elements - number of elements to write
  671. * elsize - size of each element to write (1, 2 or 4)
  672. * Return: number of elements written
  673. */
  674. int write_block(int f, const void *data, int elements, int elsize)
  675. {
  676. if (elsize == 1) {
  677. return write(f, data, elements);
  678. }
  679. else if (elsize == 2) {
  680. #ifdef LITTLE
  681. int n;
  682. flip2((const unsigned short *)data, (unsigned short *)data, elements);
  683. n = write(f, data, elements * 2) / 2;
  684. flip2((const unsigned short *)data, (unsigned short *)data, elements);
  685. return n;
  686. #else
  687. return write(f, data, elements * 2) / 2;
  688. #endif
  689. }
  690. else if (elsize == 4) {
  691. #ifdef LITTLE
  692. int n;
  693. flip4((const unsigned int *)data, (unsigned int *)data, elements);
  694. n = write(f, data, elements * 4) / 4;
  695. flip4((const unsigned int *)data, (unsigned int *)data, elements);
  696. return n;
  697. #else
  698. return write(f, data, elements * 4) / 4;
  699. #endif
  700. }
  701. else {
  702. printf("Fatal error in write_block(): bad elsize (%d)\n", elsize);
  703. abort();
  704. }
  705. return 0;
  706. }