mfile.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // This file is part of BOINC.
  2. // http://boinc.berkeley.edu
  3. // Copyright (C) 2008 University of California
  4. //
  5. // BOINC is free software; you can redistribute it and/or modify it
  6. // under the terms of the GNU Lesser General Public License
  7. // as published by the Free Software Foundation,
  8. // either version 3 of the License, or (at your option) any later version.
  9. //
  10. // BOINC 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.
  13. // See the GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with BOINC. If not, see <http://www.gnu.org/licenses/>.
  17. #ifndef BOINC_MFILE_H
  18. #define BOINC_MFILE_H
  19. #include <cstdio>
  20. #include <cstdarg>
  21. // MFILE provides memory-buffered output with a FILE-type interface.
  22. // BOINC uses this in a couple of places:
  23. // 1) in the GUI RPC system (since in Windows you can't fdopen a socket)
  24. // 2) in the API, to support a primitive form of checkpointing:
  25. // Write all your output (and restart file) to MFILEs.
  26. // The output is buffered in memory.
  27. // Then close or flush all the MFILEs;
  28. // all the buffers will be flushed to disk, almost atomically.
  29. class MFILE {
  30. char* buf; // NULL-terminated
  31. int len;
  32. FILE* f;
  33. public:
  34. MFILE();
  35. ~MFILE();
  36. int open(const char* path, const char* mode);
  37. int _putchar(char);
  38. int puts(const char*);
  39. int vprintf(const char* format, va_list);
  40. int printf(const char* format, ...);
  41. size_t write(const void *, size_t size, size_t nitems);
  42. int close();
  43. int flush();
  44. long tell() const;
  45. void get_buf(char*&, int&);
  46. // get the MFILE's internal buffer and its length.
  47. // The caller assumes ownership of the buffer and must free() it.
  48. // The MFILE's buffer is set to empty
  49. };
  50. #endif