m_emit.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. /* This file contains the internal _pl_m_emit_integer, _pl_m_emit_float,
  16. _pl_m_emit_op_code, and _pl_m_emit_string routines, which are used by
  17. MetaPlotters. They take into account the desired format (binary
  18. metafile format or ascii [human-readable] metafile format.
  19. In the unnumbered versions of libplot prior to version 0.0 (which was
  20. released as part of plotutils-2.0, in 1/98), we assumed that in binary
  21. metafile format, two bytes sufficed to represent any integer. This was
  22. the convention used in traditional plot(5) format, and can be traced to
  23. the PDP-11. Unfortunately it confined us to the range -0x10000..0x7fff
  24. (assuming two's complement). Actually, the parsing in our `plot'
  25. utility always treated the arguments to pencolor(), fillcolor(), and
  26. filltype() specially. An argument of any of those functions was treated
  27. as an unsigned integer, so it could be in the range 0..0xffff.
  28. In version 0.0 of libplot, we switched in binary metafile format to
  29. representing integers as machine integers. The parsing of metafiles by
  30. `plot' now takes this into account. `plot' has command-line options for
  31. backward compatibility with plot(5) format.
  32. Our representation for floating-point numbers in binary metafiles is
  33. simply the machine representation for single-precision floating point.
  34. plot(5) format did not support floating point arguments, so there are no
  35. concerns over backward compatibility. */
  36. #include "sys-defines.h"
  37. #include "extern.h"
  38. /* emit one unsigned character, passed as an int */
  39. void
  40. _pl_m_emit_op_code (R___(Plotter *_plotter) int c)
  41. {
  42. if (_plotter->data->outfp)
  43. putc (c, _plotter->data->outfp);
  44. #ifdef LIBPLOTTER
  45. else if (_plotter->data->outstream)
  46. _plotter->data->outstream->put ((unsigned char)c);
  47. #endif
  48. }
  49. void
  50. _pl_m_emit_integer (R___(Plotter *_plotter) int x)
  51. {
  52. if (_plotter->data->outfp)
  53. {
  54. if (_plotter->meta_portable_output)
  55. fprintf (_plotter->data->outfp, " %d", x);
  56. else
  57. fwrite ((void *) &x, sizeof(int), 1, _plotter->data->outfp);
  58. }
  59. #ifdef LIBPLOTTER
  60. else if (_plotter->data->outstream)
  61. {
  62. if (_plotter->meta_portable_output)
  63. (*(_plotter->data->outstream)) << ' ' << x;
  64. else
  65. _plotter->data->outstream->write((char *)&x, sizeof(int));
  66. }
  67. #endif
  68. }
  69. void
  70. _pl_m_emit_float (R___(Plotter *_plotter) double x)
  71. {
  72. if (_plotter->data->outfp)
  73. {
  74. if (_plotter->meta_portable_output)
  75. {
  76. /* treat equality with zero specially, since some printf's print
  77. negative zero differently from positive zero, and that may
  78. prevent regression tests from working properly */
  79. fprintf (_plotter->data->outfp, x == 0.0 ? " 0" : " %g", x);
  80. }
  81. else
  82. {
  83. float f;
  84. f = FROUND(x);
  85. fwrite ((void *) &f, sizeof(float), 1, _plotter->data->outfp);
  86. }
  87. }
  88. #ifdef LIBPLOTTER
  89. else if (_plotter->data->outstream)
  90. {
  91. if (_plotter->meta_portable_output)
  92. (*(_plotter->data->outstream)) << ' ' << x;
  93. else
  94. {
  95. float f;
  96. f = FROUND(x);
  97. _plotter->data->outstream->write((char *)&f, sizeof(float));
  98. }
  99. }
  100. #endif
  101. }
  102. void
  103. _pl_m_emit_string (R___(Plotter *_plotter) const char *s)
  104. {
  105. bool has_newline;
  106. char *t = NULL; /* keep compiler happy */
  107. char *nl;
  108. const char *u;
  109. /* null pointer handled specially */
  110. if (s == NULL)
  111. s = "(null)";
  112. if (strchr (s, '\n'))
  113. /* don't grok arg strings containing newlines; truncate at first
  114. newline if any */
  115. {
  116. has_newline = true;
  117. t = (char *)_pl_xmalloc (strlen (s) + 1);
  118. strcpy (t, s);
  119. nl = strchr (t, '\n');
  120. *nl = '\0';
  121. u = t;
  122. }
  123. else
  124. {
  125. has_newline = false;
  126. u = s;
  127. }
  128. /* emit string, with appended newline if output format is binary (old
  129. plot(3) convention, which makes sense only if there can be at most one
  130. string among the command arguments, and it's positioned last) */
  131. if (_plotter->data->outfp)
  132. {
  133. fputs (u, _plotter->data->outfp);
  134. if (_plotter->meta_portable_output == false)
  135. putc ('\n', _plotter->data->outfp);
  136. }
  137. #ifdef LIBPLOTTER
  138. else if (_plotter->data->outstream)
  139. {
  140. (*(_plotter->data->outstream)) << u;
  141. if (_plotter->meta_portable_output == false)
  142. (*(_plotter->data->outstream)) << '\n';
  143. }
  144. #endif
  145. if (has_newline)
  146. free (t);
  147. }
  148. /* End a directive that was begun by invoking _pl_m_emit_op_code() (q.v.). In
  149. portable format, the terminator is a newline; in binary format, there is
  150. no terminator. */
  151. void
  152. _pl_m_emit_terminator (S___(Plotter *_plotter))
  153. {
  154. if (_plotter->meta_portable_output)
  155. {
  156. if (_plotter->data->outfp)
  157. putc ('\n', _plotter->data->outfp);
  158. #ifdef LIBPLOTTER
  159. else if (_plotter->data->outstream)
  160. (*(_plotter->data->outstream)) << '\n';
  161. #endif
  162. }
  163. }