1
0

FileWriter.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "io/Writer.h"
  16. #include "io/FileWriter.h"
  17. #include "util/Identity.h"
  18. struct FileWriter_context {
  19. struct Writer generic;
  20. FILE* writeTo;
  21. Identity
  22. };
  23. /** @see Writer->write() */
  24. static int write(struct Writer* writer, const void* toWrite, unsigned long length)
  25. {
  26. struct FileWriter_context* context = Identity_check((struct FileWriter_context*) writer);
  27. size_t written = fwrite(toWrite, 1, length, context->writeTo);
  28. writer->bytesWritten += written;
  29. return written - length;
  30. }
  31. /** @see ArrayWriter.h */
  32. struct Writer* FileWriter_new(FILE* writeTo, struct Allocator* allocator)
  33. {
  34. struct FileWriter_context* context = Allocator_clone(allocator, (&(struct FileWriter_context) {
  35. .generic = {
  36. .write = write
  37. },
  38. .writeTo = writeTo
  39. }));
  40. Identity_set(context);
  41. return &context->generic;
  42. }