home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / program / funnel / writfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-23  |  10.2 KB  |  300 lines

  1. /*##############################################################################
  2.  
  3. FUNNNELWEB COPYRIGHT
  4. ====================
  5. FunnelWeb is a literate-programming macro preprocessor.
  6.  
  7. Copyright (C) 1992 Ross N. Williams.
  8.  
  9.    Ross N. Williams
  10.    ross@spam.adelaide.edu.au
  11.    16 Lerwick Avenue, Hazelwood Park 5066, Australia.
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of Version 2 of the GNU General Public License as
  15. published by the Free Software Foundation.
  16.  
  17. This program is distributed WITHOUT ANY WARRANTY; without even the implied
  18. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  19. See Version 2 of the GNU General Public License for more details.
  20.  
  21. You should have received a copy of Version 2 of the GNU General Public
  22. License along with this program. If not, you can FTP the license from
  23. prep.ai.mit.edu/pub/gnu/COPYING-2 or write to the Free Software
  24. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26. Section 2a of the license requires that all changes to this file be
  27. recorded prominently in this file. Please record all changes here.
  28.  
  29. Programmers:
  30.    RNW  Ross N. Williams  ross@spam.adelaide.edu.au
  31.  
  32. Changes:
  33.    07-May-1992  RNW  Program prepared for release under GNU GPL V2.
  34.  
  35. ##############################################################################*/
  36.  
  37.  
  38. /******************************************************************************/
  39. /*                                   WRITFILE.C                               */
  40. /******************************************************************************/
  41. /*                                                                            */
  42. /* Implementation Notes                                                       */
  43. /* --------------------                                                       */
  44. /* The .H file gives most of the details of what this package does. Here we   */
  45. /* just give some notes on how it is implemented.                             */
  46. /*                                                                            */
  47. /* MAGIC NUMBERS: These are placed at the head and tail of wf_t records so as */
  48. /* to assist in the detection of uninitialized or corrupted variables.        */
  49. /*                                                                            */
  50. /* TEXT VS BINARY: A big problem arises in choosing whether to write binary   */
  51. /* or text files. Binary files are far faster because they allow us to write  */
  52. /* out large slabs of text using fwrite. The only catch is that they do not   */
  53. /* translate EOL characters in the end of line representation of the host     */
  54. /* environment. Text files, on the other hand are slower (because we have     */
  55. /* only fputc and fputs) but do provide the translation. The solution we      */
  56. /* adopt is to use binary if the host environment's representation for end of */
  57. /* file is EOL (='\n'), but text otherwise. We can determine this by testing  */
  58. /* the symbol UNIX_EOL defined in machin.h. If binary output is chosen,       */
  59. /* only fwrite is called as we don't know if fwrite and fputc calls can be    */
  60. /* mixed (it would seem not).                                                 */
  61. /* Note: We could test the preprocessor symbol in each function. However,     */
  62. /* the wf_att function introduces a problem as we may be attaching to a text  */
  63. /* file in a UNIX_EOL environment (e.g. stdout). So we make the text/binary   */
  64. /* attribute and attribute of each file, rather than the environment which we */
  65. /* use only to direct us in how to create and tag (as binary or text) a file. */
  66. /*                                                                            */
  67. /******************************************************************************/
  68.  
  69. #include "style.h"
  70.  
  71. #include "as.h"
  72. #include "machin.h"
  73. #include "writfile.h"
  74.  
  75. /******************************************************************************/
  76.  
  77. #define MGC_HEAD (4837295L)
  78. #define MGC_TAIL (1213839L)
  79.  
  80. /* Set the following to TRUE to bomb the package upon the first error.        */
  81. /* This is a good idea during debugging if no output appears.                 */
  82. #define BOMB_ON_ERROR FALSE
  83.  
  84. /******************************************************************************/
  85.  
  86. LOCAL void wf_check P_((p_wf_t));
  87. LOCAL void wf_check(p_wf)
  88. p_wf_t p_wf;
  89. {
  90.  as_cold(p_wf!=NULL,"wf_check: p_wf==NULL.");
  91.  
  92.  as_cold(p_wf->wf_mhead==MGC_HEAD,
  93.          "wf_check: Magic header field has non-magic value.");
  94.  
  95.  as_cold(p_wf->wf_mtail==MGC_TAIL,
  96.          "wf_check: Magic trailer field has non-magic value.");
  97.  
  98.  as_cold(p_wf->wf_isope || p_wf->wf_pfile==NULL,
  99.          "wf_check: WF is closed but wf_pfile!=NULL.");
  100. }
  101.  
  102. /******************************************************************************/
  103.  
  104. LOCAL void wf_errlg P_((p_wf_t, char *));
  105. LOCAL void wf_errlg(p_wf,mess)
  106. /* This function is called whenever an error occurs on a stream. The main     */
  107. /* responsibility of this function is to set the error flag in the stream.    */
  108. /* However, it can do other stuff too such as log the error to the screen.    */
  109. p_wf_t p_wf;
  110. char   *mess;
  111. {
  112.  p_wf->wf_iserr=TRUE;
  113. #if BOMB_ON_ERROR
  114.    if (p_wf->wf_pfile == stdout)
  115.       fprintf(stderr,"The output file error occurred on STANDARD OUTPUT.\n");
  116.    else
  117.       fprintf(stderr,"The output file error occurred on an ordinary file.\n");
  118.    as_bomb(mess);
  119. #endif
  120. }
  121.  
  122. /******************************************************************************/
  123.  
  124. EXPORT void wf_ini(p_wf,normal)
  125. p_wf_t p_wf;
  126. bool   normal;
  127. {
  128.  p_wf->wf_mhead = MGC_HEAD;
  129.  p_wf->wf_iserr = !normal;
  130.  p_wf->wf_isope = FALSE;
  131.  p_wf->wf_istxt = FALSE;
  132.  p_wf->wf_pfile = NULL;
  133.  p_wf->wf_mtail = MGC_TAIL;
  134. }
  135.  
  136. /******************************************************************************/
  137.  
  138. EXPORT void wf_att(p_wf,wf_pfile)
  139. p_wf_t  p_wf;
  140. FILE   *wf_pfile;
  141. {
  142.  wf_check(p_wf);
  143.  if (p_wf->wf_iserr) return;
  144.  as_cold(!p_wf->wf_isope,"wf_att: WF is already open.");
  145.  
  146.  p_wf->wf_pfile = wf_pfile;
  147.  p_wf->wf_isope = TRUE;
  148.  p_wf->wf_istxt = TRUE; /* Play it safe with files we didn't open. */
  149.  wf_check(p_wf);
  150. }
  151.  
  152. /******************************************************************************/
  153.  
  154. EXPORT void wf_ope(p_wf,p_name)
  155. p_wf_t  p_wf;
  156. char   *p_name;
  157. {
  158.  FILE *result;
  159.  
  160.  wf_check(p_wf);
  161.  if (p_wf->wf_iserr) return;
  162.  as_cold(!p_wf->wf_isope,"wf_ope: WF is already open.");
  163.  
  164. /* Whether we open the file as text or binary depends on whether EOL maps     */
  165. /* to an end of line in the current environment.                              */
  166. #if UNIX_EOL
  167.  result=fopen(p_name,"wb"); p_wf->wf_istxt=FALSE;
  168. #else
  169.  result=fopen(p_name,"w"); p_wf->wf_istxt=TRUE;
  170. #endif
  171.  
  172.  if (result == FOPEN_F)
  173.    {
  174.     /* TRACE printf("Output file in error is \"%s\".\n",p_name); */
  175.     wf_errlg(p_wf,"wf_ope: Error opening output file.");
  176.    }
  177.  else
  178.    {p_wf->wf_isope=TRUE; p_wf->wf_pfile=result;}
  179.  wf_check(p_wf);
  180. }
  181.  
  182. /******************************************************************************/
  183.  
  184. EXPORT void wf_chr(p_wf,ch)
  185. p_wf_t p_wf;
  186. intchar   ch;
  187. {
  188.  wf_check(p_wf);
  189.  if ( p_wf->wf_iserr) return;
  190.  as_cold(p_wf->wf_isope,"wf_chr: WF is closed.");
  191.  
  192.  if (p_wf->wf_istxt)
  193.     {
  194.      if (fputc((int) ch,p_wf->wf_pfile) == FPUTC_F)
  195.         wf_errlg(p_wf,"wf_chr: Error fputc()ing to output file.");
  196.     }
  197.  else
  198.    {
  199.     if (fwrite(&ch,(size_t) 1,(size_t) 1,p_wf->wf_pfile) != 1)
  200.         wf_errlg(p_wf,"wf_chr: Error fwrite()ing to output file.");
  201.    }
  202.  wf_check(p_wf);
  203. }
  204.  
  205. /******************************************************************************/
  206.  
  207. EXPORT void wf_wr(p_wf,p_str)
  208. p_wf_t p_wf;
  209. char   *p_str;
  210. {
  211.  wf_check(p_wf);
  212.  if ( p_wf->wf_iserr) return;
  213.  as_cold(p_wf->wf_isope,"wf_wr: WF is closed.");
  214.  
  215.  if (p_wf->wf_istxt)
  216.    {
  217.     if (fputs(p_str,p_wf->wf_pfile) == FPUTS_F)
  218.        wf_errlg(p_wf,"wf_wr: Error fputs()ing to output file.");
  219.    }
  220.  else
  221.    {
  222.     size_t len = strlen(p_str);
  223.     if (fwrite(p_str,(size_t) 1,(size_t) len,p_wf->wf_pfile) != len)
  224.        wf_errlg(p_wf,"wf_wr: Error fwrite()ing to output file.");
  225.    }
  226.  wf_check(p_wf);
  227. }
  228.  
  229. /******************************************************************************/
  230.  
  231. EXPORT void wf_blk(p_wf,p_blk,blk_len)
  232. p_wf_t  p_wf;
  233. char   *p_blk;
  234. size_t  blk_len;
  235. {
  236.  wf_check(p_wf);
  237.  if ( p_wf->wf_iserr) return;
  238.  as_cold(p_wf->wf_isope,"wf_blk: WF is closed.");
  239.  
  240.  if (p_wf->wf_istxt)
  241.    {
  242.     /* Amazingly, fputc seems to be the only way to write out a block of */
  243.     /* bytes with end of line translation. Shocking, but true.           */
  244.     /* See Section B1.4 (p.246-247) of Kernighan and Ritchie.            */
  245.     /* Note: We can't use fputs because that requires a terminating nul. */
  246.     char *p;
  247.     char *p_post = p_blk+blk_len;
  248.     for (p=p_blk; p<p_post; p++)
  249.        if (fputc(*p,p_wf->wf_pfile) == FPUTC_F)
  250.          {
  251.           wf_errlg(p_wf,"wf_blk: Error fputc()ing to output file.");
  252.           break;
  253.          }
  254.    }
  255.  else
  256.    if (fwrite(p_blk,(size_t) 1,(size_t) blk_len,p_wf->wf_pfile) != blk_len)
  257.       wf_errlg(p_wf,"wf_blk: Error fwrite()ing to output file.");
  258.  wf_check(p_wf);
  259. }
  260.  
  261. /******************************************************************************/
  262.  
  263. EXPORT void wf_wl(p_wf,p_str)
  264. p_wf_t p_wf;
  265. char   *p_str;
  266. {
  267.  wf_wr(p_wf,p_str);
  268.  wf_chr(p_wf,'\n');
  269. }
  270.  
  271. /******************************************************************************/
  272.  
  273. EXPORT void wf_clo(p_wf)
  274. p_wf_t p_wf;
  275. {
  276.  wf_check(p_wf);
  277.  if ( p_wf->wf_iserr) return;
  278.  as_cold(p_wf->wf_isope,"wf_clo: WF is not open.");
  279.  if (fflush(p_wf->wf_pfile) != FFLUSH_S)
  280.     wf_errlg(p_wf,"wf_clo: Error fflush()ing output file.");
  281.  if (fclose(p_wf->wf_pfile) == FCLOSE_F)
  282.     wf_errlg(p_wf,"wf_clo: Error fclose()ing output file.");
  283.  p_wf->wf_isope=FALSE;
  284.  p_wf->wf_pfile=NULL;
  285.  wf_check(p_wf);
  286. }
  287.  
  288. /******************************************************************************/
  289.  
  290. EXPORT bool wf_err(p_wf)
  291. p_wf_t p_wf;
  292. {
  293.  wf_check(p_wf);
  294.  return p_wf->wf_iserr;
  295. }
  296.  
  297. /******************************************************************************/
  298. /*                              End of WRITFILE.C                             */
  299. /******************************************************************************/
  300.