00001 #include "dt_stream_util.h" 00002 00003 #include "dt_stream_text.h" 00004 #include "dt_stream_tree.h" 00005 #include "dt_stream_validator.h" 00006 #include "dt_node.h" 00007 00008 #include <stdio.h> 00009 00010 00018 dt_bool dt_stream_transfer( dt_reader* src, dt_writer* dst ) 00019 { 00020 dt_stream_step step; 00021 while( dt_read ( src, &step ) && dt_write( dst, &step ) ) 00022 if( step.kind == dt_end ) 00023 return 1; 00024 return 0; 00025 } 00026 00027 00040 dt_bool dt_stream_validated_transfer( dt_reader* src, dt_writer* dst ) 00041 { 00042 dt_writer* validator = dt_create_validator_writer( dst ); 00043 dt_bool ans = dt_stream_transfer( src, validator ) 00044 && dt_validator_writer_completed(validator); 00045 dt_free_writer( validator ); 00046 return ans; 00047 } 00048 00049 00053 dt_node* dt_read_tree_from_file( const char* srcPath ) 00054 { 00055 dt_node* ans = 00; 00056 FILE* srcF = fopen(srcPath,"rb"); 00057 if( srcF ) 00058 { 00059 dt_reader* src = dt_create_text_reader( srcF, 0 ); 00060 if( src ) 00061 { 00062 dt_writer* treeMaker = dt_create_tree_writer( & ans ); 00063 if( treeMaker ) 00064 { 00065 if( ! dt_stream_validated_transfer( src, treeMaker ) ) 00066 { 00067 dt_node_release( ans ); 00068 ans = 0; 00069 } 00070 dt_free_writer(treeMaker); 00071 } 00072 dt_free_reader(src); 00073 } 00074 fclose(srcF); 00075 } 00076 return ans; 00077 } 00078 00079 00083 dt_bool dt_write_tree_to_file( dt_node const* root, const char* dstPath ) 00084 { 00085 #ifdef _WIN32 00086 enum { defaultFormat = dt_text_crlf }; 00087 #else 00088 enum { defaultFormat = 0 }; 00089 #endif 00090 dt_bool ans = 0; 00091 FILE* dstF = fopen(dstPath,"wb"); 00092 if( dstF ) 00093 { 00094 dt_writer* dst = dt_create_text_writer( dstF, defaultFormat ); 00095 if( dst ) 00096 { 00097 ans = dt_stream_tree( root, dst ); 00098 dt_free_writer(dst); 00099 } 00100 ans = ( 0==fclose(dstF) ) && ans; 00101 } 00102 return ans; 00103 } 00104 00105 00106 00107 00108 00109