/*-------------------------------------------------------------------------* * PL_SET_STREAM_LINE_COLUMN_3 * * * *-------------------------------------------------------------------------*/ Bool Pl_Set_Stream_Line_Column_3(WamWord sora_word, WamWord line_word, WamWord col_word) { int stm; StmInf *pstm; PlLong line_count, line_pos; stm = Pl_Get_Stream_Or_Alias(sora_word, STREAM_CHECK_EXIST); pstm = pl_stm_tbl[stm]; if (!pstm->prop.reposition) Pl_Err_Permission(pl_permission_operation_reposition, pl_permission_type_stream, sora_word); if (!pstm->prop.text) Pl_Err_Permission(pl_permission_operation_reposition, pl_permission_type_binary_stream, sora_word); line_count = Pl_Rd_Integer_Check(line_word) - 1; line_pos = Pl_Rd_Integer_Check(col_word) - 1; return line_count >= 0 && line_pos >= 0 && Pl_Stream_Set_Position_LC(pstm, line_count, line_pos) == 0; }
/*-------------------------------------------------------------------------* * PL_SET_STREAM_TYPE_2 * * * *-------------------------------------------------------------------------*/ void Pl_Set_Stream_Type_2(WamWord sora_word, WamWord is_text_word) { int stm; StmInf *pstm; int text; stm = Pl_Get_Stream_Or_Alias(sora_word, STREAM_CHECK_EXIST); pstm = pl_stm_tbl[stm]; text = Pl_Rd_Integer_Check(is_text_word); if ((unsigned) text == pstm->prop.text) return; if (pstm->char_count) Pl_Err_Permission(pl_permission_operation_modify, pl_permission_type_stream, sora_word); pstm->prop.text = text; #if defined(_WIN32) || defined(__CYGWIN__) { FILE *f; f = Pl_Stdio_Desc_Of_Stream(stm); if (f == NULL) return; setmode(fileno(f), (text) ? O_TEXT : O_BINARY); } #endif }
/*-------------------------------------------------------------------------* * PL_CLOSE_INPUT_TERM_STREAM_1 * * * *-------------------------------------------------------------------------*/ void Pl_Close_Input_Term_Stream_1(WamWord sora_word) { int stm; StmInf *pstm; StrSInf *str_stream; int type; stm = Pl_Get_Stream_Or_Alias(sora_word, STREAM_CHECK_EXIST); pstm = pl_stm_tbl[stm]; type = pstm->prop.other; if (type < 1 || type > 3) Pl_Err_Domain(pl_domain_term_stream_or_alias, sora_word); if (pstm->prop.output) Pl_Err_Permission(pl_permission_operation_close, pl_permission_type_stream, sora_word); if (type != TERM_STREAM_ATOM) { str_stream = (StrSInf *) (pstm->file); Free(str_stream->buff); } Pl_Delete_Str_Stream(stm); }
/*-------------------------------------------------------------------------* * PL_SEEK_4 * * * *-------------------------------------------------------------------------*/ Bool Pl_Seek_4(WamWord sora_word, WamWord whence_word, WamWord offset_word, WamWord new_loc_word) { int stm; StmInf *pstm; int whence; PlLong offset; int atom; PlLong p[4]; stm = Pl_Get_Stream_Or_Alias(sora_word, STREAM_CHECK_EXIST); pstm = pl_stm_tbl[stm]; if (!pstm->prop.reposition) Pl_Err_Permission(pl_permission_operation_reposition, pl_permission_type_stream, sora_word); if (pstm->prop.text) Pl_Err_Permission(pl_permission_operation_reposition, pl_permission_type_text_stream, sora_word); atom = Pl_Rd_Atom_Check(whence_word); if (atom == pl_atom_bof) whence = SEEK_SET; else if (atom == pl_atom_current) whence = SEEK_CUR; else if (atom == pl_atom_eof) whence = SEEK_END; else Pl_Err_Domain(pl_domain_stream_seek_method, whence_word); offset = Pl_Rd_Integer_Check(offset_word); Pl_Check_For_Un_Integer(new_loc_word); if (Pl_Stream_Set_Position(pstm, whence, offset, offset, 0, 0) != 0) return FALSE; Pl_Stream_Get_Position(pstm, &offset, p + 1, p + 2, p + 3); return Pl_Get_Integer(offset, new_loc_word); }
/*-------------------------------------------------------------------------* * PL_SET_STREAM_EOF_ACTION_2 * * * *-------------------------------------------------------------------------*/ void Pl_Set_Stream_Eof_Action_2(WamWord sora_word, WamWord action_word) { int stm; StmInf *pstm; stm = Pl_Get_Stream_Or_Alias(sora_word, STREAM_CHECK_EXIST); pstm = pl_stm_tbl[stm]; if (pstm->prop.output) Pl_Err_Permission(pl_permission_operation_modify, pl_permission_type_stream, sora_word); pstm->prop.eof_action = Pl_Rd_Integer_Check(action_word); }
/*-------------------------------------------------------------------------* * PL_LINE_POSITION_2 * * * *-------------------------------------------------------------------------*/ Bool Pl_Line_Position_2(WamWord sora_word, WamWord count_word) { int stm; StmInf *pstm; PlLong offset, char_count, line_count, line_pos; stm = Pl_Get_Stream_Or_Alias(sora_word, STREAM_CHECK_EXIST); pstm = pl_stm_tbl[stm]; if (!pstm->prop.text) Pl_Err_Permission(pl_permission_operation_access, pl_permission_type_binary_stream, sora_word); Pl_Stream_Get_Position(pstm, &offset, &char_count, &line_count, &line_pos); return Pl_Un_Integer_Check(line_pos, count_word); }
/*-------------------------------------------------------------------------* * PL_CLOSE_OUTPUT_TERM_STREAM_2 * * * *-------------------------------------------------------------------------*/ Bool Pl_Close_Output_Term_Stream_2(WamWord sora_word, WamWord sink_term_word) { int stm; StmInf *pstm; int type; char *str; stm = Pl_Get_Stream_Or_Alias(sora_word, STREAM_CHECK_EXIST); pstm = pl_stm_tbl[stm]; type = pstm->prop.other; if (type < 1 || type > 3) Pl_Err_Domain(pl_domain_term_stream_or_alias, sora_word); if (pstm->prop.input) Pl_Err_Permission(pl_permission_operation_close, pl_permission_type_stream, sora_word); str = Pl_Term_Write_Str_Stream(stm); switch (SYS_VAR_OPTION_MASK) { case TERM_STREAM_ATOM: if (!Pl_Un_String_Check(str, sink_term_word)) return FALSE; break; case TERM_STREAM_CHARS: if (!Pl_Un_Chars_Check(str, sink_term_word)) return FALSE; break; case TERM_STREAM_CODES: if (!Pl_Un_Codes_Check(str, sink_term_word)) return FALSE; break; } Pl_Delete_Str_Stream(stm); return TRUE; }
/*-------------------------------------------------------------------------* * PL_SET_STREAM_POSITION_2 * * * *-------------------------------------------------------------------------*/ Bool Pl_Set_Stream_Position_2(WamWord sora_word, WamWord position_word) { WamWord word, tag_mask; WamWord p_word[4]; int p[4]; int i; int stm; StmInf *pstm; stm = Pl_Get_Stream_Or_Alias(sora_word, STREAM_CHECK_EXIST); pstm = pl_stm_tbl[stm]; if (!pstm->prop.reposition) Pl_Err_Permission(pl_permission_operation_reposition, pl_permission_type_stream, sora_word); DEREF(position_word, word, tag_mask); if (tag_mask == TAG_REF_MASK) Pl_Err_Instantiation(); if (!Pl_Get_Structure(pl_atom_stream_position, 4, position_word)) dom_error: Pl_Err_Domain(pl_domain_stream_position, position_word); for (i = 0; i < 4; i++) { p_word[i] = Pl_Unify_Variable(); DEREF(p_word[i], word, tag_mask); if (tag_mask == TAG_REF_MASK) Pl_Err_Instantiation(); if (tag_mask != TAG_INT_MASK) goto dom_error; p[i] = UnTag_INT(word); } return Pl_Stream_Set_Position(pstm, SEEK_SET, p[0], p[1], p[2], p[3]) == 0; }
/*-------------------------------------------------------------------------* * PL_SR_OPEN_FILE_2 * * * *-------------------------------------------------------------------------*/ void Pl_SR_Open_File_2(WamWord file_name_word, WamWord from_stream_word) { SRInf *sr = cur_sr; int atom_file_name; int stm; SRFile *file; Bool from_stream = Pl_Rd_Boolean(from_stream_word); Bool master_file = (sr->file_top == NULL); StmInf *pstm, *pstm_tmp; if (sr->next_to_reread == NULL) { if (from_stream) { stm = Pl_Get_Stream_Or_Alias(file_name_word, STREAM_CHECK_INPUT); Pl_Check_Stream_Type(stm, TRUE, TRUE); atom_file_name = pl_stm_tbl[stm]->atom_file_name; } else { atom_file_name = Pl_Rd_Atom(file_name_word); if (strcmp(pl_atom_tbl[atom_file_name].name, "user") == 0) #if 0 stm = pl_stm_input; #else { stm = Pl_Add_Stream(0, (long) 0, pl_stm_tbl[pl_stm_input]->prop, NULL, NULL, NULL, NULL, NULL, NULL, NULL); *pl_stm_tbl[stm] = *pl_stm_tbl[pl_stm_input]; } #endif else { stm = Pl_Add_Stream_For_Stdio_File(pl_atom_tbl[atom_file_name].name, STREAM_MODE_READ, TRUE); if (stm < 0) { if (errno == ENOENT || errno == ENOTDIR) Pl_Err_Existence(pl_existence_source_sink, file_name_word); else Pl_Err_Permission(pl_permission_operation_open, pl_permission_type_source_sink, file_name_word); } } } pstm = pl_stm_tbl[stm]; file = (SRFile *) Malloc(sizeof(SRFile)); file->atom_file_name = atom_file_name; file->stm = stm; file->reposition = pstm->prop.reposition; if (!file->reposition) { file->tmp_path = Pl_M_Tempnam(NULL, NULL); file->tmp_stm = Pl_Add_Stream_For_Stdio_File(file->tmp_path, STREAM_MODE_WRITE, TRUE); if (file->tmp_stm < 0) Pl_Fatal_Error("cannot create tmp file %s in %s:%d", file->tmp_path, __FILE__, __LINE__); /* try to be similar to original file */ pstm_tmp = pl_stm_tbl[file->tmp_stm]; pstm_tmp->atom_file_name = atom_file_name; pstm_tmp->prop.eof_action = pstm->prop.eof_action; if (pstm_tmp->prop.buffering != pstm->prop.buffering) { pstm_tmp->prop.buffering = pstm->prop.buffering; Pl_Stdio_Set_Buffering((FILE *) pstm_tmp->file, pstm_tmp->prop.buffering); } Pl_Add_Mirror_To_Stream(stm, file->tmp_stm); } else { file->tmp_path = NULL; file->tmp_stm = -1; } file->next = NULL; if (sr->file_first == NULL) sr->file_first = file; else sr->file_last->next = file; sr->file_last = file; }
/*-------------------------------------------------------------------------* * PL_OPEN_3 * * * *-------------------------------------------------------------------------*/ void Pl_Open_3(WamWord source_sink_word, WamWord mode_word, WamWord stm_word) { WamWord word, tag_mask; int atom; int mode; Bool text; StmProp prop; char *path; int atom_file_name; int stm; FILE *f; int mask = SYS_VAR_OPTION_MASK; Bool reposition; DEREF(source_sink_word, word, tag_mask); if (tag_mask == TAG_REF_MASK) Pl_Err_Instantiation(); if (tag_mask != TAG_ATM_MASK) Pl_Err_Domain(pl_domain_source_sink, source_sink_word); atom_file_name = UnTag_ATM(word); path = pl_atom_tbl[atom_file_name].name; if ((path = Pl_M_Absolute_Path_Name(path)) == NULL) Pl_Err_Existence(pl_existence_source_sink, source_sink_word); text = mask & 1; mask >>= 1; atom = Pl_Rd_Atom_Check(mode_word); if (atom == pl_atom_read) mode = STREAM_MODE_READ; else if (atom == pl_atom_write) mode = STREAM_MODE_WRITE; else if (atom == pl_atom_append) mode = STREAM_MODE_APPEND; else Pl_Err_Domain(pl_domain_io_mode, mode_word); stm = Pl_Add_Stream_For_Stdio_File(path, mode, text); if (stm < 0) { if (errno == ENOENT || errno == ENOTDIR) Pl_Err_Existence(pl_existence_source_sink, source_sink_word); else Pl_Err_Permission(pl_permission_operation_open, pl_permission_type_source_sink, source_sink_word); } prop = pl_stm_tbl[stm]->prop; f = (FILE *) pl_stm_tbl[stm]->file; /* change properties wrt to specified ones */ if ((mask & 2) != 0) /* reposition specified */ { reposition = mask & 1; if (reposition && !prop.reposition) { fclose(f); word = Pl_Put_Structure(pl_atom_reposition, 1); Pl_Unify_Atom(pl_atom_true); Pl_Err_Permission(pl_permission_operation_open, pl_permission_type_source_sink, word); } prop.reposition = reposition; } mask >>= 2; if ((mask & 4) != 0) /* eof_action specified */ prop.eof_action = mask & 3; mask >>= 3; if ((mask & 4) != 0) /* buffering specified */ if (prop.buffering != (unsigned) (mask & 3)) /* cast for MSVC warning */ { prop.buffering = mask & 3; Pl_Stdio_Set_Buffering(f, prop.buffering); } mask >>= 3; pl_stm_tbl[stm]->atom_file_name = atom_file_name; pl_stm_tbl[stm]->prop = prop; Pl_Get_Integer(stm, stm_word); }