PageRenderTime 5259ms CodeModel.GetById 30ms RepoModel.GetById 12ms app.codeStats 0ms

/gprolog-1.4.0/src/BipsPl/stream_supp.h

#
C Header | 472 lines | 254 code | 164 blank | 54 comment | 11 complexity | 232c32d058e6269b6cf4ed6ba8d79f40 MD5 | raw file
Possible License(s): LGPL-3.0
  1. /*-------------------------------------------------------------------------*
  2. * GNU Prolog *
  3. * *
  4. * Part : Prolog buit-in predicates *
  5. * File : stream_supp.h *
  6. * Descr.: stream support - header file *
  7. * Author: Daniel Diaz *
  8. * *
  9. * Copyright (C) 1999-2011 Daniel Diaz *
  10. * *
  11. * This file is part of GNU Prolog *
  12. * *
  13. * GNU Prolog is free software: you can redistribute it and/or *
  14. * modify it under the terms of either: *
  15. * *
  16. * - the GNU Lesser General Public License as published by the Free *
  17. * Software Foundation; either version 3 of the License, or (at your *
  18. * option) any later version. *
  19. * *
  20. * or *
  21. * *
  22. * - the GNU General Public License as published by the Free *
  23. * Software Foundation; either version 2 of the License, or (at your *
  24. * option) any later version. *
  25. * *
  26. * or both in parallel, as here. *
  27. * *
  28. * GNU Prolog is distributed in the hope that it will be useful, *
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  31. * General Public License for more details. *
  32. * *
  33. * You should have received copies of the GNU General Public License and *
  34. * the GNU Lesser General Public License along with this program. If *
  35. * not, see http://www.gnu.org/licenses/. *
  36. *-------------------------------------------------------------------------*/
  37. /* $Id: stream_supp.h,v 1.22 2011/03/28 16:20:05 diaz Exp $ */
  38. #include <stdio.h>
  39. /*---------------------------------*
  40. * Constants *
  41. *---------------------------------*/
  42. #define STREAM_PB_SIZE 8 /* push back buffer size */
  43. #define STREAM_MODE_READ 0
  44. #define STREAM_MODE_WRITE 1
  45. #define STREAM_MODE_APPEND 2
  46. #define STREAM_EOF_ACTION_ERROR 0
  47. #define STREAM_EOF_ACTION_EOF_CODE 1
  48. #define STREAM_EOF_ACTION_RESET 2
  49. #define STREAM_BUFFERING_NONE 0
  50. #define STREAM_BUFFERING_LINE 1
  51. #define STREAM_BUFFERING_BLOCK 2
  52. #define STREAM_EOF_NOT 0
  53. #define STREAM_EOF_AT 1
  54. #define STREAM_EOF_PAST 2
  55. /* values for Get_Stream_Or_Alias */
  56. #define STREAM_CHECK_VALID 0 /* simply a valid stream */
  57. #define STREAM_CHECK_EXIST 1 /* valid and exist */
  58. #define STREAM_CHECK_INPUT 2 /* valid, exist and mode=input */
  59. #define STREAM_CHECK_OUTPUT 3 /* valid, exist and mode=output */
  60. #define STREAM_FCT_UNDEFINED ((StmFct) (-1)) /* for optional fct */
  61. /* Constant term streams (prop.other) */
  62. #define TERM_STREAM_ATOM 1 /* values also used in stream.pl */
  63. #define TERM_STREAM_CHARS 2
  64. #define TERM_STREAM_CODES 3
  65. /*---------------------------------*
  66. * Type Definitions *
  67. *---------------------------------*/
  68. typedef struct /* Stream properties */
  69. { /* ------------------------------ */
  70. unsigned mode:2; /* see STREAM_MODE_xxx defs */
  71. unsigned input:1; /* is it an input stream ? */
  72. unsigned output:1; /* is it an output stream ? */
  73. unsigned text:1; /* is it a text stream . (or bin) */
  74. unsigned reposition:1; /* can it be repositioned ? */
  75. unsigned eof_action:2; /* see STREAM_EOF_ACTION_xxx defs */
  76. unsigned buffering:2; /* see STREAM_BUFFERING_xxx defs */
  77. unsigned special_close:1; /* does it need a special close ? */
  78. unsigned other:8; /* other prop (1,2,3=term_streams */
  79. } /* 4=socket_stream) */
  80. StmProp;
  81. typedef struct /* Push Back stack */
  82. { /* ------------------------------ */
  83. int buff[STREAM_PB_SIZE]; /* the buffer */
  84. int *ptr; /* pointer into the buffer */
  85. int nb_elems; /* # of elements in the buffer */
  86. }
  87. PbStk;
  88. typedef int (*StmFct) (); /* generic type for file fctions */
  89. typedef struct stm_lst *PStmLst;
  90. typedef struct stm_lst /* Chained stream list */
  91. { /* ------------------------------ */
  92. int stm; /* the stream */
  93. PStmLst next; /* next entry */
  94. } StmLst;
  95. typedef struct stm_inf /* Stream information */
  96. { /* ------------------------------ */
  97. int atom_file_name; /* atom associated to filename */
  98. PlLong file; /* accessor (FILE *,TTYInf *) != 0*/
  99. StmProp prop; /* assoctiated properties */
  100. StmLst *mirror; /* mirror streams */
  101. StmLst *mirror_of; /* streams this stream as mirror */
  102. /* ----- Basic I/O functions ---- */
  103. StmFct fct_getc; /* get char function (mandatory) */
  104. StmFct fct_putc; /* put char function (mandatory) */
  105. StmFct fct_flush; /* flush function (optional) */
  106. StmFct fct_close; /* close function (optional) */
  107. StmFct fct_tell; /* tell function (optional) */
  108. StmFct fct_seek; /* seek function (optional) */
  109. StmFct fct_clearerr; /* clearerr function (optional) */
  110. /* ------ Read information ----- */
  111. Bool eof_reached; /* has eof char been read ? */
  112. PbStk pb_char; /* character push back stack */
  113. /* ---- Position information --- */
  114. int char_count; /* character read count */
  115. int line_count; /* line read count */
  116. int line_pos; /* line position */
  117. PbStk pb_line_pos; /* line position push back stack */
  118. }
  119. StmInf;
  120. typedef struct /* Alias information */
  121. { /* ------------------------------ */
  122. PlLong atom; /* atom of the alias (the key) */
  123. int stm; /* associated stream */
  124. }
  125. AliasInf;
  126. typedef struct /* String Stream information */
  127. { /* ------------------------------ */
  128. char *buff; /* the I/O buffer */
  129. char *ptr; /* current position into the buff */
  130. Bool buff_alloc_size; /* mallocated size (iff output) */
  131. }
  132. StrSInf;
  133. /*---------------------------------*
  134. * Global Variables *
  135. *---------------------------------*/
  136. #ifdef STREAM_SUPP_FILE
  137. StmInf **pl_stm_tbl;
  138. int pl_stm_tbl_size;
  139. int pl_stm_last_used;
  140. char *pl_alias_tbl;
  141. WamWord pl_last_input_sora;
  142. WamWord pl_last_output_sora;
  143. int pl_stm_stdin;
  144. int pl_stm_stdout;
  145. int pl_stm_stderr;
  146. int pl_stm_input;
  147. int pl_stm_output;
  148. int pl_stm_error;
  149. int pl_stm_top_level_input;
  150. int pl_stm_top_level_output;
  151. int pl_stm_debugger_input;
  152. int pl_stm_debugger_output;
  153. char *pl_le_prompt;
  154. int pl_use_le_prompt;
  155. int pl_atom_stream;
  156. int pl_atom_user_input;
  157. int pl_atom_user_output;
  158. int pl_atom_user_error;
  159. int pl_atom_top_level_input;
  160. int pl_atom_top_level_output;
  161. int pl_atom_debugger_input;
  162. int pl_atom_debugger_output;
  163. int pl_atom_read;
  164. int pl_atom_write;
  165. int pl_atom_append;
  166. int pl_atom_reposition;
  167. int pl_atom_stream_position;
  168. int pl_atom_text;
  169. int pl_atom_binary;
  170. int pl_atom_error;
  171. int pl_atom_eof_code;
  172. int pl_atom_reset;
  173. int pl_atom_none;
  174. int pl_atom_line;
  175. int pl_atom_block;
  176. int pl_atom_not;
  177. int pl_atom_at;
  178. int pl_atom_past;
  179. int pl_atom_bof;
  180. int pl_atom_current;
  181. int pl_atom_eof;
  182. #else
  183. extern StmInf **pl_stm_tbl;
  184. extern int pl_stm_tbl_size;
  185. extern int pl_stm_last_used;
  186. extern char *pl_alias_tbl;
  187. extern WamWord pl_last_input_sora;
  188. extern WamWord pl_last_output_sora;
  189. extern int pl_stm_stdin;
  190. extern int pl_stm_stdout;
  191. extern int pl_stm_stderr;
  192. extern int pl_stm_input;
  193. extern int pl_stm_output;
  194. extern int pl_stm_error;
  195. extern int pl_stm_top_level_input;
  196. extern int pl_stm_top_level_output;
  197. extern int pl_stm_debugger_input;
  198. extern int pl_stm_debugger_output;
  199. extern char *pl_le_prompt;
  200. extern int pl_use_le_prompt;
  201. extern int pl_atom_stream;
  202. extern int pl_atom_user_input;
  203. extern int pl_atom_user_output;
  204. extern int pl_atom_top_level_input;
  205. extern int pl_atom_top_level_output;
  206. extern int pl_atom_debugger_input;
  207. extern int pl_atom_debugger_output;
  208. extern int pl_atom_read;
  209. extern int pl_atom_write;
  210. extern int pl_atom_append;
  211. extern int pl_atom_reposition;
  212. extern int pl_atom_stream_position;
  213. extern int pl_atom_text;
  214. extern int pl_atom_binary;
  215. extern int pl_atom_error;
  216. extern int pl_atom_eof_code;
  217. extern int pl_atom_reset;
  218. extern int pl_atom_none;
  219. extern int pl_atom_line;
  220. extern int pl_atom_block;
  221. extern int pl_atom_not;
  222. extern int pl_atom_at;
  223. extern int pl_atom_past;
  224. extern int pl_atom_bof;
  225. extern int pl_atom_current;
  226. extern int pl_atom_eof;
  227. #endif
  228. /*---------------------------------*
  229. * Function Prototypes *
  230. *---------------------------------*/
  231. int Pl_Add_Stream(int atom_file_name, PlLong file, StmProp prop,
  232. StmFct fct_getc, StmFct fct_putc,
  233. StmFct fct_flush, StmFct fct_close,
  234. StmFct fct_tell, StmFct fct_seek, StmFct fct_clearerr);
  235. int Pl_Add_Stream_For_Stdio_Desc(FILE *f, int atom_path, int mode, int text);
  236. int Pl_Add_Stream_For_Stdio_File(char *path, int mode, Bool text);
  237. void Pl_Delete_Stream(int stm);
  238. int Pl_Find_Stream_By_Alias(int atom_alias);
  239. Bool Pl_Add_Alias_To_Stream(int atom_alias, int stm);
  240. void Pl_Reassign_Alias(int atom_alias, int stm);
  241. void Pl_Add_Mirror_To_Stream(int stm, int m_stm);
  242. Bool Pl_Del_Mirror_From_Stream(int stm, int m_stm);
  243. int Pl_Find_Stream_From_PStm(StmInf *pstm);
  244. void Pl_Flush_All_Streams(void);
  245. void Pl_Set_Stream_Buffering(int stm);
  246. int Pl_Get_Stream_Or_Alias(WamWord sora_word, int test_mask);
  247. void Pl_Check_Stream_Type(int stm, Bool check_text, Bool for_input);
  248. WamWord Pl_Make_Stream_Tagged_Word(int stm);
  249. Bool Pl_Stdio_Is_Repositionable(FILE *f);
  250. void Pl_Stdio_Set_Buffering(FILE *f, int buffering);
  251. FILE *Pl_Stdio_Desc_Of_Stream(int stm);
  252. int Pl_Io_Fileno_Of_Stream(int stm);
  253. int Pl_Stream_Getc(StmInf *pstm);
  254. int Pl_Stream_Get_Key(StmInf *pstm, Bool echo, Bool catch_ctrl_c);
  255. void Pl_Stream_Ungetc(int c, StmInf *pstm);
  256. int Pl_Stream_Peekc(StmInf *pstm);
  257. char *Pl_Stream_Gets(char *str, int size, StmInf *pstm);
  258. char *Pl_Stream_Gets_Prompt(char *prompt, StmInf *pstm_o,
  259. char *str, int size, StmInf *pstm_i);
  260. void Pl_Stream_Putc(int c, StmInf *pstm);
  261. int Pl_Stream_Puts(char *str, StmInf *pstm);
  262. int Pl_Stream_Printf(StmInf *pstm, char *format, ...);
  263. void Pl_Stream_Flush(StmInf *pstm);
  264. int Pl_Stream_Close(StmInf *pstm);
  265. int Pl_Stream_End_Of_Stream(StmInf *pstm);
  266. void Pl_Stream_Get_Position(StmInf *pstm, int *offset,
  267. int *char_count, int *line_count, int *line_pos);
  268. int Pl_Stream_Set_Position(StmInf *pstm, int whence, int offset,
  269. int char_count, int line_count, int line_pos);
  270. int Pl_Stream_Set_Position_LC(StmInf *pstm, int line_count, int line_pos);
  271. int Pl_Add_Str_Stream(char *buff, int prop_other);
  272. void Pl_Delete_Str_Stream(int stm);
  273. char *Pl_Term_Write_Str_Stream(int stm);
  274. void Pl_Close_Stm(int stm, Bool force); /* from close_c.c */
  275. #define PB_Init(pb) pb.ptr = pb.buff, pb.nb_elems = 0;
  276. #define PB_Is_Empty(pb) (pb.nb_elems == 0)
  277. #define PB_Push(pb, elem) \
  278. do \
  279. { \
  280. *(pb.ptr) = (elem); \
  281. if (pb.ptr != pb.buff + STREAM_PB_SIZE - 1) \
  282. pb.ptr++; \
  283. else \
  284. pb.ptr = pb.buff; \
  285. if (pb.nb_elems < STREAM_PB_SIZE) \
  286. pb.nb_elems++; \
  287. } \
  288. while (0)
  289. #define PB_Pop(pb, elem) \
  290. do \
  291. { \
  292. if (pb.ptr != pb.buff) \
  293. pb.ptr--; \
  294. else \
  295. pb.ptr = pb.buff + STREAM_PB_SIZE - 1; \
  296. (elem) = *pb.ptr; \
  297. pb.nb_elems--; \
  298. } \
  299. while (0)
  300. #define PB_Top(pb, elem) \
  301. do \
  302. { \
  303. if (pb.ptr != pb.buff) \
  304. (elem) = pb.ptr[-1]; \
  305. else \
  306. (elem) = pb.buff[STREAM_PB_SIZE - 1]; \
  307. } \
  308. while (0)