FILEUTIL.C 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
  11. COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include "pstypes.h"
  16. #include "fileutil.h"
  17. #include "cfile.h"
  18. #include "fix.h"
  19. #include "byteswap.h"
  20. int filelength(int fd)
  21. {
  22. int cur_pos, end_pos;
  23. cur_pos = lseek(fd, 0, SEEK_CUR);
  24. lseek(fd, 0, SEEK_END);
  25. end_pos = lseek(fd, 0, SEEK_CUR);
  26. lseek(fd, cur_pos, SEEK_SET);
  27. return end_pos;
  28. }
  29. byte read_byte(CFILE *fp)
  30. {
  31. byte b;
  32. cfread(&b, sizeof(byte), 1, fp);
  33. return b;
  34. }
  35. short read_short(CFILE *fp)
  36. {
  37. short s;
  38. cfread(&s, sizeof(short), 1, fp);
  39. return (s);
  40. }
  41. short read_short_swap(CFILE *fp)
  42. {
  43. short s;
  44. cfread(&s, sizeof(short), 1, fp);
  45. return swapshort(s);
  46. }
  47. int read_int(CFILE *fp)
  48. {
  49. uint i;
  50. cfread(&i, sizeof(uint), 1, fp);
  51. return i;
  52. }
  53. int read_int_swap(CFILE *fp)
  54. {
  55. uint i;
  56. cfread(&i, sizeof(uint), 1, fp);
  57. return swapint(i);
  58. }
  59. fix read_fix(CFILE *fp)
  60. {
  61. fix f;
  62. cfread(&f, sizeof(fix), 1, fp);
  63. return f;
  64. }
  65. fix read_fix_swap(CFILE *fp)
  66. {
  67. fix f;
  68. cfread(&f, sizeof(fix), 1, fp);
  69. return (fix)swapint((uint)f);
  70. }
  71. int write_byte(FILE *fp, byte b)
  72. {
  73. return (fwrite(&b, sizeof(byte), 1, fp));
  74. }
  75. int write_short(FILE *fp, short s)
  76. {
  77. return (fwrite(&s, sizeof(short), 1, fp));
  78. }
  79. int write_short_swap(FILE *fp, short s)
  80. {
  81. s = swapshort(s);
  82. return (fwrite(&s, sizeof(short), 1, fp));
  83. }
  84. int write_int(FILE *fp, int i)
  85. {
  86. return (fwrite(&i,sizeof(int), 1, fp));
  87. }
  88. int write_int_swap(FILE *fp, int i)
  89. {
  90. i = swapint(i);
  91. return (fwrite(&i,sizeof(int), 1, fp));
  92. }
  93. int write_fix(FILE *fp, fix f)
  94. {
  95. return (fwrite(&f, sizeof(fix), 1, fp));
  96. }
  97. int write_fix_swap(FILE *fp, fix f)
  98. {
  99. f = (fix)swapint((int)f);
  100. return (fwrite(&f, sizeof(fix), 1, fp));
  101. }