home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.68.zip / src / asm / membufio.c < prev    next >
C/C++ Source or Header  |  2008-04-01  |  2KB  |  68 lines

  1. /*
  2.  * Copyright (c) 2005 Magnus Lind.
  3.  *
  4.  * This software is provided 'as-is', without any express or implied warranty.
  5.  * In no event will the authors be held liable for any damages arising from
  6.  * the use of this software.
  7.  *
  8.  * Permission is granted to anyone to use this software, alter it and re-
  9.  * distribute it freely for any non-commercial, non-profit purpose subject to
  10.  * the following restrictions:
  11.  *
  12.  *   1. The origin of this software must not be misrepresented; you must not
  13.  *   claim that you wrote the original software. If you use this software in a
  14.  *   product, an acknowledgment in the product documentation would be
  15.  *   appreciated but is not required.
  16.  *
  17.  *   2. Altered source versions must be plainly marked as such, and must not
  18.  *   be misrepresented as being the original software.
  19.  *
  20.  *   3. This notice may not be removed or altered from any distribution.
  21.  *
  22.  *   4. The names of this software and/or it's copyright holders may not be
  23.  *   used to endorse or promote products derived from this software without
  24.  *   specific prior written permission.
  25.  *
  26.  */
  27.  
  28. #include "membufio.h"
  29. #include "log.h"
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32.  
  33.  
  34. void read_file(const char *name, struct membuf *buf)
  35. {
  36.     char block[1024];
  37.     FILE *in;
  38.     int len;
  39.  
  40.     in = fopen(name, "rb");
  41.     if(in == NULL)
  42.     {
  43.         LOG(LOG_ERROR, ("Can't open file \"%s\" for input.\n", name));
  44.         exit(-1);
  45.     }
  46.     do
  47.     {
  48.         len = fread(block, 1, 1024, in);
  49.         membuf_append(buf, block, len);
  50.     }
  51.     while(len == 1024);
  52.     LOG(LOG_DEBUG, ("read %d bytes from file\n", len));
  53.     fclose(in);
  54. }
  55.  
  56. void write_file(const char *name, struct membuf *buf)
  57. {
  58.     FILE *out;
  59.     out = fopen(name, "wb");
  60.     if(out == NULL)
  61.     {
  62.         LOG(LOG_ERROR, ("Can't open file \"%s\" for output.\n", name));
  63.         exit(-1);
  64.     }
  65.     fwrite(membuf_get(buf), 1, membuf_memlen(buf), out);
  66.     fclose(out);
  67. }
  68.