gsdsrc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Copyright (C) 1997, 2000 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: gsdsrc.h,v 1.3 2000/09/19 19:00:28 lpd Exp $ */
  16. /* DataSource definitions */
  17. #ifndef gsdsrc_INCLUDED
  18. # define gsdsrc_INCLUDED
  19. #include "gsstruct.h"
  20. /* ---------------- Types and structures ---------------- */
  21. /*
  22. * A gs_data_source_t represents the data source for various constructs. It
  23. * can be a string (either a gs_string or a byte-type object), a
  24. * positionable, non-procedure-based stream, or an array of floats. An
  25. * ordinary positionable file stream will do, as long as the client doesn't
  26. * attempt to read past the EOF.
  27. *
  28. * The handling of floats is anomalous, but we don't see a good alternative
  29. * at the moment.
  30. */
  31. #ifndef stream_DEFINED
  32. # define stream_DEFINED
  33. typedef struct stream_s stream;
  34. #endif
  35. /*
  36. * Prepare to access a block of data from a source. buf must be a client-
  37. * supplied buffer of at least length bytes. If ptr == 0, always copy the
  38. * data into buf. If ptr != 0, either copy the data into buf and set *ptr =
  39. * buf, or set *ptr to point to the data (which might be invalidated by the
  40. * next call). Note that this procedure may or may not do bounds checking.
  41. */
  42. #define data_source_proc_access(proc)\
  43. int proc(P5(const gs_data_source_t *psrc, ulong start, uint length,\
  44. byte *buf, const byte **ptr))
  45. typedef enum {
  46. data_source_type_string,
  47. data_source_type_bytes,
  48. data_source_type_floats,
  49. data_source_type_stream
  50. } gs_data_source_type_t;
  51. #ifndef gs_data_source_DEFINED
  52. # define gs_data_source_DEFINED
  53. typedef struct gs_data_source_s gs_data_source_t;
  54. #endif
  55. struct gs_data_source_s {
  56. data_source_proc_access((*access));
  57. gs_data_source_type_t type;
  58. union d_ {
  59. gs_const_string str; /* also used for byte objects */
  60. stream *strm;
  61. } data;
  62. };
  63. #define data_source_access_only(psrc, start, length, buf, ptr)\
  64. (*(psrc)->access)(psrc, (ulong)(start), length, buf, ptr)
  65. #define data_source_access(psrc, start, length, buf, ptr)\
  66. BEGIN\
  67. int code_ = data_source_access_only(psrc, start, length, buf, ptr);\
  68. if ( code_ < 0 ) return code_;\
  69. END
  70. #define data_source_copy_only(psrc, start, length, buf)\
  71. data_source_access_only(psrc, start, length, buf, (const byte **)0)
  72. #define data_source_copy(psrc, start, length, buf)\
  73. data_source_access(psrc, start, length, buf, (const byte **)0)
  74. /*
  75. * Data sources are always embedded in other structures, but they do have
  76. * pointers that need to be traced and relocated, so they do have a GC
  77. * structure type.
  78. */
  79. extern_st(st_data_source);
  80. #define public_st_data_source() /* in gsdsrc.c */\
  81. gs_public_st_composite(st_data_source, gs_data_source_t, "gs_data_source_t",\
  82. data_source_enum_ptrs, data_source_reloc_ptrs)
  83. #define st_data_source_max_ptrs 1
  84. /* ---------------- Procedures ---------------- */
  85. /* Initialize a data source of the various known types. */
  86. data_source_proc_access(data_source_access_string);
  87. #define data_source_init_string(psrc, strg)\
  88. ((psrc)->type = data_source_type_string,\
  89. (psrc)->data.str = strg, (psrc)->access = data_source_access_string)
  90. #define data_source_init_string2(psrc, bytes, len)\
  91. ((psrc)->type = data_source_type_string,\
  92. (psrc)->data.str.data = bytes, (psrc)->data.str.size = len,\
  93. (psrc)->access = data_source_access_string)
  94. data_source_proc_access(data_source_access_bytes);
  95. #define data_source_init_bytes(psrc, bytes, len)\
  96. ((psrc)->type = data_source_type_bytes,\
  97. (psrc)->data.str.data = bytes, (psrc)->data.str.size = len,\
  98. (psrc)->access = data_source_access_bytes)
  99. #define data_source_init_floats(psrc, floats, count)\
  100. ((psrc)->type = data_source_type_floats,\
  101. (psrc)->data.str.data = (byte *)floats,\
  102. (psrc)->data.str.size = (count) * sizeof(float),\
  103. (psrc)->access = data_source_access_bytes)
  104. data_source_proc_access(data_source_access_stream);
  105. #define data_source_init_stream(psrc, s)\
  106. ((psrc)->type = data_source_type_stream,\
  107. (psrc)->data.strm = s, (psrc)->access = data_source_access_stream)
  108. #define data_source_is_stream(dsource)\
  109. ((dsource).type == data_source_type_stream)
  110. #define data_source_is_array(dsource)\
  111. ((dsource).type == data_source_type_floats)
  112. #endif /* gsdsrc_INCLUDED */