stdio_file.h.xml 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <chapter xml:id="stdio_file.h">
  2. <title><tt>__vic/stdio_file.h</tt></title>
  3. <p><tt>std::FILE</tt>-related C++ wrappers.</p>
  4. <chapter xml:id="stdio_file">
  5. <title><tt>stdio_file</tt></title>
  6. <code-block lang="C++"><![CDATA[
  7. class stdio_file : private non_copyable
  8. {
  9. public:
  10. explicit stdio_file(std::FILE *fp = nullptr);
  11. stdio_file(const char *name, const char *mode);
  12. ~stdio_file();
  13. // BEGIN C++11
  14. stdio_file(stdio_file &&o) noexcept;
  15. stdio_file &operator=(stdio_file &&o) noexcept;
  16. // END C++11
  17. bool open(const char *name, const char *mode);
  18. bool is_open() const;
  19. void close();
  20. bool close_nt() noexcept;
  21. void swap(stdio_file &o) noexcept;
  22. std::FILE *detach_handle() noexcept;
  23. std::FILE *attach_handle(std::FILE *fp) noexcept;
  24. std::FILE *handle() const;
  25. operator std::FILE*() const;
  26. };
  27. ]]></code-block>
  28. <p>Thin RAII-wrapper for <tt>std::FILE *</tt>. Controls file's life time.
  29. Automatic conversion to <tt>std::FILE *</tt> allows usage of the object
  30. whereever the file pointer is allowed/required.</p>
  31. <note>Although the class destructor closes the open file, it is more safe
  32. to use explicit <tt>close()</tt> call. Errors can happen when closing file,
  33. and <tt>close()</tt> can throw exception in such cases, while destructor -
  34. can't, so the error will be unnoticed by the application.</note>
  35. <section><title>Class members</title>
  36. <synopsis>
  37. <prototype>explicit stdio_file(std::FILE *fp = nullptr)</prototype>
  38. <p>Wraps already existing stream pointer.</p>
  39. <precondition><tt>fp</tt> is either a pointer to the open file or
  40. <tt>nullptr</tt>.</precondition>
  41. </synopsis>
  42. <synopsis>
  43. <prototype>stdio_file(const char *name, const char *mode)</prototype>
  44. <p>Calls <tt>open(name, mode)</tt>. The result should be checked using
  45. subsequent <tt>is_open()</tt> call!</p>
  46. </synopsis>
  47. <synopsis>
  48. <prototype>~stdio_file()</prototype>
  49. <p>Calls <tt>std::fclose()</tt> if <tt>is_open() == true</tt>.</p>
  50. </synopsis>
  51. <synopsis>
  52. <prototype>stdio_file(stdio_file &amp;&amp;o) noexcept <sign>C++11</sign></prototype>
  53. <prototype>stdio_file &amp;operator=(stdio_file &amp;&amp;o) noexcept <sign>C++11</sign></prototype>
  54. <p>Move operations for C++11 mode.</p>
  55. </synopsis>
  56. <synopsis>
  57. <prototype>bool open(const char *name, const char *mode)</prototype>
  58. <p>Calls <tt>std::fopen(name, mode)</tt>. Returns <tt>true</tt> in case of
  59. success.</p>
  60. <precondition><tt>is_open() == false</tt></precondition>
  61. </synopsis>
  62. <synopsis>
  63. <prototype>bool is_open() const</prototype>
  64. <p>Returns <tt>true</tt> if file is open.</p>
  65. </synopsis>
  66. <synopsis>
  67. <prototype>void close()</prototype>
  68. <p>Calls <tt>std::fclose()</tt>. No preliminary checks whether the file is open
  69. are performed! Throws if <tt>std::fclose()</tt> fails.</p>
  70. <precondition><tt>is_open() == true</tt></precondition>
  71. <postcondition><tt>is_open() == false</tt></postcondition>
  72. </synopsis>
  73. <synopsis>
  74. <prototype>bool close_nt() noexcept</prototype>
  75. <p>A counterpart of <tt>close()</tt> but never throws, returns <tt>false</tt>
  76. instead in case of error.</p>
  77. </synopsis>
  78. <synopsis>
  79. <prototype>void swap(stdio_file &amp;o) noexcept</prototype>
  80. <p>Swaps the value with <tt>o</tt>.</p>
  81. </synopsis>
  82. <synopsis>
  83. <prototype>std::FILE *detach_handle() noexcept</prototype>
  84. <p>Releases the file out of the object's control.</p>
  85. <postcondition><tt>is_open() == false</tt></postcondition>
  86. </synopsis>
  87. <synopsis>
  88. <prototype>std::FILE *attach_handle(std::FILE *fp) noexcept</prototype>
  89. <p>Takes <tt>fp</tt> under control and returns the previous handle value.</p>
  90. <precondition><tt>fp</tt> is either a poiner to the open file or
  91. <tt>nullptr</tt>.</precondition>
  92. <postcondition><tt>handle() == fp</tt></postcondition>
  93. </synopsis>
  94. <synopsis>
  95. <prototype>std::FILE *handle() const</prototype>
  96. <p>Returns the held handle value.</p>
  97. </synopsis>
  98. <synopsis>
  99. <prototype>operator std::FILE*() const</prototype>
  100. <p>Allows usage of the object as <tt>std::FILE *</tt>.</p>
  101. </synopsis>
  102. </section>
  103. <section><title>Example</title>
  104. <code-block lang="C++">
  105. __vic::stdio_file file("file.txt", "w");
  106. if(!file.is_open()) throw __vic::exception("Cannot open file");
  107. std::fprintf(file, "Message");
  108. file.close();
  109. // fclose() also will be called in case of exception by destructor
  110. </code-block>
  111. </section>
  112. </chapter>
  113. <chapter xml:id="read-FILE-char">
  114. <title><tt>read(std::FILE, char&amp;)</tt>,
  115. <tt>read(std::FILE, unsigned char&amp;)</tt></title>
  116. <code-block lang="C++"><![CDATA[
  117. bool read(std::FILE *fp, char &ch);
  118. bool read(std::FILE *fp, unsigned char &ch);
  119. ]]></code-block>
  120. <p>Attempts to read a byte from the C-stream. Returns <tt>true</tt> on succes,
  121. <tt>false</tt> on EOF or throws on error.</p>
  122. </chapter>
  123. <chapter xml:id="write-FILE-char">
  124. <title><tt>write(std::FILE, char)</tt></title>
  125. <code-block lang="C++"><![CDATA[
  126. void write(std::FILE *fp, char ch);
  127. ]]></code-block>
  128. <p>Writes a byte to the C-stream. Throws on error.</p>
  129. </chapter>
  130. <chapter xml:id="getline-FILE">
  131. <title><tt>getline(std::FILE)</tt></title>
  132. <code-block lang="C++"><![CDATA[
  133. bool getline(std::FILE *fp, std::string &str, char delim = '\n');
  134. ]]></code-block>
  135. <p>A counterpart of <tt>std::getline</tt> but for C-stream. Returns
  136. <tt>false</tt> if the end of the file was reached and nothing was read.</p>
  137. </chapter>
  138. </chapter>