bin_file.h.xml 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <chapter xml:id="bin_file.h">
  2. <title><tt>__vic/bin_file.h</tt></title>
  3. <chapter xml:id="bin_file">
  4. <title><tt>bin_file</tt></title>
  5. <code-block lang="C++"><![CDATA[
  6. class bin_file : private non_copyable
  7. {
  8. public:
  9. enum in_t { in };
  10. enum out_t { out };
  11. enum append_t { append };
  12. bin_file();
  13. bin_file(const char *fname, in_t);
  14. bin_file(const char *fname, out_t);
  15. bin_file(const char *fname, append_t);
  16. ~bin_file();
  17. // BEGIN C++11
  18. bin_file(bin_file &&o) noexcept;
  19. bin_file &operator=(bin_file &&o) noexcept;
  20. // END C++11
  21. bool open_in(const char *fname);
  22. bool open_out(const char *fname);
  23. bool open_append(const char *fname);
  24. size_t read_max(void *buf, size_t n);
  25. size_t read_some(void *buf, size_t n);
  26. void write_all(const void *buf, size_t n);
  27. bool is_open() const;
  28. void close();
  29. bool close_nt() noexcept;
  30. void swap(bin_file &o) noexcept;
  31. [[noreturn]] void throw_last_error(const char *msg);
  32. void throw_if_closed(const char *msg);
  33. };
  34. ]]></code-block>
  35. <p>Unbuffered binary file. OS-independent wrapper for low-level system API.</p>
  36. <p>Following open modes are available:</p>
  37. <list style="bulleted">
  38. <item><tt>in</tt> - open existing file for reading;</item>
  39. <item><tt>out</tt> - create new file for writing, overwrite if exists;</item>
  40. <item><tt>append</tt> - open existing file for appending (writing at end of
  41. file), create if does not exist.</item>
  42. </list>
  43. <section><title>Class members</title>
  44. <synopsis>
  45. <prototype>enum in_t { in }</prototype>
  46. <prototype>enum out_t { out }</prototype>
  47. <prototype>enum append_t { append }</prototype>
  48. <p>Constructor tags.</p>
  49. </synopsis>
  50. <synopsis>
  51. <prototype>bin_file()</prototype>
  52. <postcondition><tt>is_open() == false()</tt></postcondition>
  53. </synopsis>
  54. <synopsis>
  55. <prototype>bin_file(const char *fname, in_t)</prototype>
  56. <prototype>bin_file(const char *fname, out_t)</prototype>
  57. <prototype>bin_file(const char *fname, append_t)</prototype>
  58. <p>Call <tt>open_in(fname)</tt>, <tt>open_out(fname)</tt> or
  59. <tt>open_append(fname)</tt>, correspondingly. <tt>is_open()</tt> or
  60. <tt>throw_if_closed()</tt> has to be called then to check the result.</p>
  61. </synopsis>
  62. <synopsis>
  63. <prototype>~bin_file()</prototype>
  64. <p>Closes the file if <tt>is_open() == true</tt>.</p>
  65. </synopsis>
  66. <synopsis>
  67. <prototype>bin_file(bin_file &amp;&amp;o) noexcept <sign>C++11</sign></prototype>
  68. <prototype>bin_file &amp;operator=(bin_file &amp;&amp;o) noexcept <sign>C++11</sign></prototype>
  69. <p>Move operations for C++11 mode.</p>
  70. </synopsis>
  71. <synopsis>
  72. <prototype>bool open_in(const char *fname)</prototype>
  73. <prototype>bool open_out(const char *fname)</prototype>
  74. <prototype>bool open_append(const char *fname)</prototype>
  75. <p>Open file for reading, writing or appending, correspondingly. Return
  76. <tt>is_open()</tt>.</p>
  77. <precondition><tt>is_open() == false</tt></precondition>
  78. </synopsis>
  79. <synopsis>
  80. <prototype>bool is_open() const</prototype>
  81. <p>Returns <tt>true</tt> if file is open.</p>
  82. </synopsis>
  83. <synopsis>
  84. <prototype>size_t read_max(void *buf, size_t n)</prototype>
  85. <p>Tries to read <tt>n</tt> bytes to the specified buffer. Returns number of
  86. bytes read. Returned value can be less than requested only when end of file
  87. was reached. Throws on error.</p>
  88. <precondition><tt>is_open() == true</tt></precondition>
  89. </synopsis>
  90. <synopsis>
  91. <prototype>size_t read_some(void *buf, size_t n)</prototype>
  92. <p>Reads no more than <tt>n</tt> bytes to the specified buffer. Returns number
  93. of bytes read or <tt>0</tt> in case of end-of-file. The function returns after
  94. first chunk of any size was successfully received. Throws on error.</p>
  95. <precondition><tt>is_open() == true</tt></precondition>
  96. </synopsis>
  97. <synopsis>
  98. <prototype>void write_all(const void *buf, size_t n)</prototype>
  99. <p>Writes the whole buffer to the file. Throws on error.</p>
  100. <precondition><tt>is_open() == true</tt></precondition>
  101. </synopsis>
  102. <synopsis>
  103. <prototype>void close()</prototype>
  104. <p>Closes the file. Throws on error.</p>
  105. <precondition><tt>is_open() == true</tt></precondition>
  106. <postcondition><tt>is_open() == false</tt></postcondition>
  107. </synopsis>
  108. <synopsis>
  109. <prototype>bool close_nt() noexcept</prototype>
  110. <p>A counterpart of <tt>close()</tt> but never throws, returns <tt>false</tt>
  111. instead in case of error.</p>
  112. </synopsis>
  113. <synopsis>
  114. <prototype>void swap(bin_file &amp;o) noexcept</prototype>
  115. <p>Swaps the value with <tt>o</tt>.</p>
  116. </synopsis>
  117. <synopsis>
  118. <prototype>[[noreturn]] void throw_last_error(const char *msg)</prototype>
  119. <p>Throws exception with the last error description if available.
  120. <tt>what()</tt> will contain <tt>msg</tt> as a substring anyway.</p>
  121. </synopsis>
  122. <synopsis>
  123. <prototype>void throw_if_closed(const char *msg)</prototype>
  124. <p>Calls <tt>throw_last_error(msg)</tt> if <tt>!is_open()</tt>.</p>
  125. </synopsis>
  126. </section>
  127. <section><title>Example</title>
  128. <code-block lang="C++"><![CDATA[
  129. // Copy file
  130. __vic::bin_file in("file", __vic::bin_file::in);
  131. in.throw_if_closed("Cannot open file")
  132. __vic::bin_file out("file.copy", __vic::bin_file::out);
  133. out.throw_if_closed("Cannot create file")
  134. char buf[512];
  135. while(size_t n = in.read_some(buf, sizeof buf))
  136. out.write_all(buf, n);
  137. out.close();
  138. // in will be closed by destructor
  139. ]]></code-block>
  140. </section>
  141. </chapter>
  142. </chapter>