Formatted-Input.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <html lang="en">
  2. <head>
  3. <title>Formatted Input - avram - a virtual machine code interpreter</title>
  4. <meta http-equiv="Content-Type" content="text/html">
  5. <meta name="description" content="avram - a virtual machine code interpreter">
  6. <meta name="generator" content="makeinfo 4.13">
  7. <link title="Top" rel="start" href="index.html#Top">
  8. <link rel="up" href="File-Manipulation.html#File-Manipulation" title="File Manipulation">
  9. <link rel="prev" href="Raw-Files.html#Raw-Files" title="Raw Files">
  10. <link rel="next" href="Formatted-Output.html#Formatted-Output" title="Formatted Output">
  11. <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
  12. <meta http-equiv="Content-Style-Type" content="text/css">
  13. <style type="text/css"><!--
  14. pre.display { font-family:inherit }
  15. pre.format { font-family:inherit }
  16. pre.smalldisplay { font-family:inherit; font-size:smaller }
  17. pre.smallformat { font-family:inherit; font-size:smaller }
  18. pre.smallexample { font-size:smaller }
  19. pre.smalllisp { font-size:smaller }
  20. span.sc { font-variant:small-caps }
  21. span.roman { font-family:serif; font-weight:normal; }
  22. span.sansserif { font-family:sans-serif; font-weight:normal; }
  23. --></style>
  24. </head>
  25. <body>
  26. <div class="node">
  27. <a name="Formatted-Input"></a>
  28. <p>
  29. Next:&nbsp;<a rel="next" accesskey="n" href="Formatted-Output.html#Formatted-Output">Formatted Output</a>,
  30. Previous:&nbsp;<a rel="previous" accesskey="p" href="Raw-Files.html#Raw-Files">Raw Files</a>,
  31. Up:&nbsp;<a rel="up" accesskey="u" href="File-Manipulation.html#File-Manipulation">File Manipulation</a>
  32. <hr>
  33. </div>
  34. <h4 class="subsection">3.3.3 Formatted Input</h4>
  35. <p>Some functions relating to the input of text files or data files with
  36. preambles are declared in the header file <samp><span class="file">formin.h</span></samp>. The usage of
  37. these functions is as follows.
  38. <div class="defun">
  39. &mdash; Function: list <b>avm_preamble_and_contents</b> (<var>FILE *source, char *filename</var>)<var><a name="index-avm_005fpreamble_005fand_005fcontents-555"></a></var><br>
  40. <blockquote><p>This function loads a file of either text or data format into memory.
  41. <dl>
  42. <dt><var>source</var><dd>should be initialized by the caller as the address of a file
  43. already open for reading that will be read from its current position.
  44. <br><dt><var>filename</var><dd>should be set by the caller to the address of a null terminated
  45. character string giving the name of the file that will be used if an i/o
  46. error message needs to be written about it. If it is a <code>NULL</code>
  47. pointer, standard input is assumed.
  48. </dl>
  49. <p>The result returned by the function will be a list whose <code>head</code>
  50. <a name="index-preamble-556"></a>represents the preamble of the file and whose <code>tail</code> represents the
  51. contents. As a side effect, the input file will be closed, unless the
  52. <var>filename</var> parameter is <code>NULL</code>.
  53. <p>If the file conforms to the format described in <a href="File-Format.html#File-Format">File Format</a>, the
  54. preamble is a list of character strings. In the result returned by the
  55. function, the <code>head</code> field will be a list with one item for each
  56. line in the file, and each item will be a list of character
  57. representations as in <a href="Character-Table.html#Character-Table">Character Table</a>, but with the leading hashes
  58. stripped. The <code>tail</code> will be the list specified by remainder of the
  59. file according to <a href="Concrete-Syntax.html#Concrete-Syntax">Concrete Syntax</a>. If the file has an empty
  60. preamble but is nevertheless a data file, the <code>head</code> will be a list
  61. whose <code>head</code> and <code>tail</code> are both <code>NULL</code>.
  62. <p>If the file does not conform to the format in <a href="File-Format.html#File-Format">File Format</a>, then
  63. the <code>head</code> of the result will be <code>NULL</code>, and the <code>tail</code>
  64. will be a list of lists of character representations, with one for each
  65. line.
  66. <p>Whether or not the file conforms to the format is determined on the fly,
  67. so this function is useful for situations in which the format is not
  68. known in advance. The conventions regarding the preamble and contents
  69. maintained by this function are the same as those used by virtual code
  70. applications as described in <a href="Standard-Output-Representation.html#Standard-Output-Representation">Standard Output Representation</a> and
  71. <a href="Input-Data-Structure.html#Input-Data-Structure">Input Data Structure</a>.
  72. <p>The characters used for line breaks are not explicitly represented in
  73. <a name="index-line-breaks-557"></a>the result. Depending on the host system, line breaks in text files may
  74. be represented either by the character code 10, or by the sequence 13
  75. 10. However, in order for the library to deal with binary files in a
  76. portable way, a line break always corresponds to a 10 as far as this
  77. function is concerned regardless of the host, and a 13 is treated like
  78. any other character. Hence, if this function were used on binary files
  79. that happened to have some 10s in them, the exact contents of a
  80. file could be reconstructed easily by appending a 10 to all but the last
  81. line and flattening the list.
  82. <p>A considerable amount of memory may need to be allocated by this
  83. function in order to store the file as a list. If not enough memory is
  84. available, the function prints an error message to standard error and
  85. aborts rather than returning to the caller. However, i/o errors are not
  86. fatal, and will cause the function to print a warning but attempt to
  87. continue.
  88. </p></blockquote></div>
  89. <div class="defun">
  90. &mdash; Function: list <b>avm_load</b> (<var>FILE *source, char *filename, int raw</var>)<var><a name="index-avm_005fload-558"></a></var><br>
  91. <blockquote><p>Similarly to <code>avm_preamble_and_contents</code>, this function also loads
  92. a file into memory, but the format is specified in advance.
  93. <dl>
  94. <dt><var>source</var><dd>should be set by the caller to the address of an already open file for
  95. reading, which will be read from its current position.
  96. <br><dt><var>filename</var><dd>should be initialized by the caller as a pointer to a null terminated
  97. string containing the name of the file that will be reported to the user
  98. in the event of an error reading from it. If it is a <code>NULL</code>
  99. pointer, standard input is assumed.
  100. <br><dt><var>raw</var><dd>is set to a non-zero value by the caller to indicate that the file is
  101. expected to conform to the format in <a href="File-Format.html#File-Format">File Format</a>. If the file is
  102. an ordinary text file, then it should be set to zero.
  103. </dl>
  104. <p>In the case of a data file, which is when <var>raw</var> is non-zero,
  105. the result returned by this function will be a list representing the
  106. data section of the file and ignoring the preamble. In the case of a
  107. text file, the result will be a list of lists of character
  108. representations as per <a href="Character-Table.html#Character-Table">Character Table</a>, with one such list for
  109. each line in the file. Similar comments about line breaks to those
  110. mentioned under <code>avm_preamble_and_contents</code> are applicable.
  111. <p>As a side effect of this function, the <var>source</var> file will be
  112. closed, unless the <var>filename</var> is a <code>NULL</code> pointer.
  113. <p>This function is useful when the type of file is known in advance. If a
  114. data file is indicated by the <var>raw</var> parameter but the format
  115. is incorrect, an error message is reported and the process
  116. terminates. The error message will be of the form
  117. <pre class="display"> <var>program-name</var><code>: invalid raw file format in </code><var>filename</var>
  118. </pre>
  119. <p>Alternatively, if a text file is indicated by the
  120. <a name="index-g_t_0040code_007binvalid-raw-file-format_007d-559"></a><var>raw</var> parameter, then no attempt is made to test whether it
  121. could be interpreted as data, even if it could be. This behavior differs
  122. from that of <code>avm_preamble_and_contents</code>, where a bad data
  123. file format causes the file to be treated as text, and a valid data file
  124. format, even in a &ldquo;text&rdquo; file, causes it to be treated as data.
  125. <p>Memory requirements for this function are significant and will cause the
  126. process to abort with an error message in the event of insufficient free
  127. memory. Messages pertaining to i/o errors are also possible and are not
  128. fatal.
  129. </p></blockquote></div>
  130. <div class="defun">
  131. &mdash; Function: void <b>avm_initialize_formin</b> ()<var><a name="index-avm_005finitialize_005fformin-560"></a></var><br>
  132. <blockquote><p>This function should be called before either of the other functions in
  133. this section is called, as it initializes some necessary static data
  134. structures. Results of the other functions are undefined if this one is
  135. not called first.
  136. </p></blockquote></div>
  137. <div class="defun">
  138. &mdash; Function: void <b>avm_count_formin</b> ()<var><a name="index-avm_005fcount_005fformin-561"></a></var><br>
  139. <blockquote><p>This function should be called after the last call to any of the other
  140. functions in this section, as it is necessary for cleaning up and
  141. reclaiming some internal data. If any storage remains unreclaimed due to
  142. memory leaks in these functions or to misuse of them, a warning message
  143. is written to standard error. If the function <code>avm_count_lists</code> is
  144. also being used by the client program, it should be called after this
  145. one.
  146. </p></blockquote></div>
  147. </body></html>