- Improve the file handle closing code

This commit is contained in:
Zeev Suraski 1999-11-26 23:45:22 +00:00
parent cd53944435
commit 58153dfed0
3 changed files with 45 additions and 18 deletions

View File

@ -183,23 +183,47 @@ inline void restore_lexical_state(zend_lex_state *lex_state CLS_DC)
BEGIN_EXTERN_C()
ZEND_API void zend_open_file_dtor(void *f)
ZEND_API void zend_open_file_dtor(zend_file_handle *fh)
{
switch (fh->type) {
case ZEND_HANDLE_FP:
fclose(fh->handle.fp);
break;
#ifdef ZTS
delete *((ifstream **) f);
#else
fclose(*((FILE **) f));
case ZEND_HANDLE_FSTREAM:
delete ((ifstream *) fh->handle.is);
break;
case ZEND_HANDLE_STDIOSTREAM:
delete ((istdiostream *) fh->handle.is);
break;
#endif
}
}
int zend_compare_file_handles(zend_file_handle *fh1, zend_file_handle *fh2)
{
if (fh1->type != fh2->type) {
return 0;
}
switch (fh1->type) {
case ZEND_HANDLE_FP:
return fh1->handle.fp==fh2->handle.fp;
break;
#ifdef ZTS
case ZEND_HANDLE_FSTREAM:
case ZEND_HANDLE_STDIOSTREAM:
return fh1->handle.is==fh2->handle.is;
break;
}
#endif
return 0;
}
ZEND_API void zend_close_file_handle(zend_file_handle *file_handle CLS_DC)
{
#ifdef ZTS
zend_llist_del_element(&CG(open_files), &file_handle->handle.is);
#else
zend_llist_del_element(&CG(open_files), &file_handle->handle.fp);
#endif
zend_llist_del_element(&CG(open_files), file_handle, (int (*)(void *, void *)) zend_compare_file_handles);
}
@ -221,7 +245,7 @@ ZEND_API int open_file_for_scanning(zend_file_handle *file_handle CLS_DC)
return FAILURE;
}
if (file_handle->handle.fp != stdin) {
zend_llist_add_element(&CG(open_files), &file_handle->handle.fp);
zend_llist_add_element(&CG(open_files), file_handle);
}
file_handle->type = ZEND_HANDLE_FP;
/* Reset the scanner for scanning the new file */
@ -232,6 +256,7 @@ ZEND_API int open_file_for_scanning(zend_file_handle *file_handle CLS_DC)
switch (file_handle->type) {
case ZEND_HANDLE_FD:
file_handle->handle.is = new ifstream(file_handle->handle.fd);
file_handle->type = ZEND_HANDLE_FSTREAM;
break;
case ZEND_HANDLE_FILENAME:
{
@ -240,6 +265,7 @@ ZEND_API int open_file_for_scanning(zend_file_handle *file_handle CLS_DC)
return FAILURE;
}
file_handle->handle.is = new istdiostream(file_handle->handle.fp);
file_handle->type = ZEND_HANDLE_STDIOSTREAM;
break;
}
case ZEND_HANDLE_FP:
@ -250,6 +276,7 @@ ZEND_API int open_file_for_scanning(zend_file_handle *file_handle CLS_DC)
return FAILURE;
}
file_handle->handle.is = new istdiostream(file_handle->handle.fp);
file_handle->type = ZEND_HANDLE_STDIOSTREAM;
}
break;
}
@ -258,9 +285,8 @@ ZEND_API int open_file_for_scanning(zend_file_handle *file_handle CLS_DC)
return FAILURE;
}
if (file_handle->handle.is != &cin) {
zend_llist_add_element(&CG(open_files), &file_handle->handle.is);
zend_llist_add_element(&CG(open_files), file_handle);
}
file_handle->type = ZEND_HANDLE_ISTREAM;
CG(ZFL) = new ZendFlexLexer;
CG(ZFL)->switch_streams(file_handle->handle.is, &cout);

View File

@ -94,7 +94,7 @@ void init_compiler(CLS_D ELS_DC)
zend_hash_apply(&module_registry, (int (*)(void *)) module_registry_request_startup);
init_resource_list(ELS_C);
CG(unclean_shutdown) = 0;
zend_llist_init(&CG(open_files), sizeof(void *), zend_open_file_dtor, 0);
zend_llist_init(&CG(open_files), sizeof(zend_file_handle), zend_open_file_dtor, 0);
}

View File

@ -354,7 +354,7 @@ ZEND_API int open_file_for_scanning(zend_file_handle *file_handle CLS_DC);
ZEND_API void init_op_array(zend_op_array *op_array, int initial_ops_size);
ZEND_API void destroy_op_array(zend_op_array *op_array);
ZEND_API void zend_close_file_handle(zend_file_handle *file_handle CLS_DC);
ZEND_API void zend_open_file_dtor(void *f);
ZEND_API void zend_open_file_dtor(zend_file_handle *fh);
END_EXTERN_C()
ZEND_API int destroy_zend_function(zend_function *function);
@ -543,10 +543,11 @@ int zendlex(znode *zendlval CLS_DC);
#define ZEND_RT (1<<1)
#define ZEND_HANDLE_FILENAME 0
#define ZEND_HANDLE_FD 1
#define ZEND_HANDLE_FP 2
#define ZEND_HANDLE_ISTREAM 3
#define ZEND_HANDLE_FILENAME 0
#define ZEND_HANDLE_FD 1
#define ZEND_HANDLE_FP 2
#define ZEND_HANDLE_STDIOSTREAM 3
#define ZEND_HANDLE_FSTREAM 4
#define ZEND_DECLARE_CLASS 1
#define ZEND_DECLARE_FUNCTION 2