io.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* io.c -- io functions
  2. This file is part of the UCL data compression library.
  3. Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
  4. All Rights Reserved.
  5. The UCL library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of
  8. the License, or (at your option) any later version.
  9. The UCL library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with the UCL library; see the file COPYING.
  15. If not, write to the Free Software Foundation, Inc.,
  16. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. Markus F.X.J. Oberhumer
  18. <markus@oberhumer.com>
  19. http://www.oberhumer.com/opensource/ucl/
  20. */
  21. #include "ucl_conf.h"
  22. #if !defined(NO_STDIO_H)
  23. #include <stdio.h>
  24. #include "uclutil.h"
  25. #undef ucl_fread
  26. #undef ucl_fwrite
  27. /***********************************************************************
  28. //
  29. ************************************************************************/
  30. UCL_PUBLIC(ucl_uint)
  31. ucl_fread(FILE *f, ucl_voidp s, ucl_uint len)
  32. {
  33. #if 1 && (UCL_UINT_MAX <= SIZE_T_MAX)
  34. return fread(s,1,len,f);
  35. #else
  36. ucl_byte *p = (ucl_byte *) s;
  37. ucl_uint l = 0;
  38. size_t k;
  39. unsigned char *b;
  40. unsigned char buf[512];
  41. while (l < len)
  42. {
  43. k = len - l > sizeof(buf) ? sizeof(buf) : (size_t) (len - l);
  44. k = fread(buf,1,k,f);
  45. if (k <= 0)
  46. break;
  47. l += k;
  48. b = buf; do *p++ = *b++; while (--k > 0);
  49. }
  50. return l;
  51. #endif
  52. }
  53. /***********************************************************************
  54. //
  55. ************************************************************************/
  56. UCL_PUBLIC(ucl_uint)
  57. ucl_fwrite(FILE *f, const ucl_voidp s, ucl_uint len)
  58. {
  59. #if 1 && (UCL_UINT_MAX <= SIZE_T_MAX)
  60. return fwrite(s,1,len,f);
  61. #else
  62. const ucl_byte *p = (const ucl_byte *) s;
  63. ucl_uint l = 0;
  64. size_t k, n;
  65. unsigned char *b;
  66. unsigned char buf[512];
  67. while (l < len)
  68. {
  69. k = len - l > sizeof(buf) ? sizeof(buf) : (size_t) (len - l);
  70. b = buf; n = k; do *b++ = *p++; while (--n > 0);
  71. k = fwrite(buf,1,k,f);
  72. if (k <= 0)
  73. break;
  74. l += k;
  75. }
  76. return l;
  77. #endif
  78. }
  79. #endif /* !defined(NO_STDIO_H) */
  80. /*
  81. vi:ts=4:et
  82. */