|
| 1 | +#pragma once |
| 2 | +#include "cpp2oct_core.h" |
| 3 | + |
| 4 | +using namespace std; |
| 5 | + |
| 6 | +namespace cpp2octave { |
| 7 | + |
| 8 | + namespace c2o_datafile_gen_policies{ |
| 9 | + class c2o_datafile_WM_ASCII { |
| 10 | + public: |
| 11 | + static const string WM_FILE_PREFIX; // = "ASCII" |
| 12 | + static const string OCT_READ_MODE; //="r" |
| 13 | + |
| 14 | + static const ios_base::openmode WM_IOS_MODE;// = ios::out; |
| 15 | + |
| 16 | + template< class T > class reading_tags { }; |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + template< class T > |
| 21 | + static c2o_obj& serial_write( |
| 22 | + bool octFileIsOpen, |
| 23 | + c2o_obj& dest , |
| 24 | + const T* vect , |
| 25 | + size_t N , |
| 26 | + ofstream& out_file , |
| 27 | + c2o_obj & out_file_link , |
| 28 | + cpp2oct_core * owner |
| 29 | + ){ |
| 30 | + if( octFileIsOpen ){ |
| 31 | + stringstream o; |
| 32 | + for( int i = 0 ; i < N ; i++ ){ |
| 33 | + o << vect[i] << " " ; |
| 34 | + } |
| 35 | + o << "\n"; |
| 36 | + out_file << o.str(); |
| 37 | + |
| 38 | + owner->oct_f_call( |
| 39 | + dest , |
| 40 | + "fscanf" , |
| 41 | + out_file_link , |
| 42 | + reading_tags< T >::READING_TAG , |
| 43 | + static_cast<int> ( N ) |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + return dest; |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + template< > |
| 52 | + class c2o_datafile_WM_ASCII::reading_tags< double > { public: static const string READING_TAG; }; |
| 53 | + |
| 54 | + template< > |
| 55 | + class c2o_datafile_WM_ASCII::reading_tags< int > { public: static const string READING_TAG; }; |
| 56 | + |
| 57 | + class c2o_datafile_WM_BIN { |
| 58 | + public: |
| 59 | + static const string WM_FILE_PREFIX; //= "BIN" |
| 60 | + static const string OCT_READ_MODE; //="rb" |
| 61 | + |
| 62 | + static const ios_base::openmode WM_IOS_MODE;// = ios::out || ios::binary; |
| 63 | + |
| 64 | + template< class T > class reading_tags { }; |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + template< class T > |
| 69 | + static c2o_obj& serial_write( |
| 70 | + bool octFileIsOpen, |
| 71 | + c2o_obj& dest , |
| 72 | + const T* vect , |
| 73 | + size_t N , |
| 74 | + ofstream& out_file , |
| 75 | + c2o_obj & out_file_link , |
| 76 | + cpp2oct_core * owner |
| 77 | + ){ |
| 78 | + |
| 79 | + if( octFileIsOpen ){ |
| 80 | + out_file.write ((char*)vect, N * sizeof (T)); |
| 81 | + |
| 82 | + owner->oct_f_call( |
| 83 | + dest , |
| 84 | + "fread" , |
| 85 | + out_file_link , |
| 86 | + static_cast<int> ( N ), |
| 87 | + reading_tags< T >::READING_TAG |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + return dest; |
| 92 | + |
| 93 | + } |
| 94 | + }; |
| 95 | + template< > |
| 96 | + class c2o_datafile_WM_BIN::reading_tags< double > { public: static const string READING_TAG; }; |
| 97 | + |
| 98 | + template< > |
| 99 | + class c2o_datafile_WM_BIN::reading_tags< int > { public: static const string READING_TAG; }; |
| 100 | + }; |
| 101 | + |
| 102 | + |
| 103 | +using namespace c2o_datafile_gen_policies; |
| 104 | + |
| 105 | + |
| 106 | +typedef c2o_datafile_gen_policies::c2o_datafile_WM_ASCII c2o_datafile_P_ASCII; |
| 107 | +typedef c2o_datafile_gen_policies::c2o_datafile_WM_BIN c2o_datafile_P_BIN; |
| 108 | + |
| 109 | + |
| 110 | +template< class P__ > |
| 111 | +class c2o_datafile_gen : |
| 112 | + public c2o_datafile_handler |
| 113 | +{ |
| 114 | +private: |
| 115 | + ofstream out_file; |
| 116 | + c2o_obj out_file_link; |
| 117 | + bool octFileIsOpen; |
| 118 | + |
| 119 | + void out_file_init( ){ |
| 120 | + stringstream s; |
| 121 | + s << path << DIR_SEP_CH << f_name; |
| 122 | + string fname_str = s.str(); |
| 123 | + out_file.open( fname_str.c_str() , /*ios::out | ios::binary*/ P__::WM_IOS_MODE ); |
| 124 | + |
| 125 | + owner->f_fopen( out_file_link , fname_str , P__::OCT_READ_MODE ); |
| 126 | + octFileIsOpen = true; |
| 127 | + }; |
| 128 | + |
| 129 | +public: |
| 130 | + c2o_datafile_gen( cpp2oct_core * _owner , string _f_name ) : |
| 131 | + c2o_datafile_handler( _owner , _f_name ), |
| 132 | + out_file_link(_owner->oct_new_obj_base( string(P__::WM_FILE_PREFIX).append("_f_").append(_f_name)) ) |
| 133 | + |
| 134 | + { |
| 135 | + out_file_init( ); |
| 136 | + }; |
| 137 | + |
| 138 | + c2o_obj& serial_write( c2o_obj& dest , const int* vect , size_t N ){ |
| 139 | + return P__::serial_write( octFileIsOpen , dest , vect , N , out_file , out_file_link , owner ); |
| 140 | + } |
| 141 | + |
| 142 | + c2o_obj& serial_write( c2o_obj& dest , const double* vect , size_t N ){ |
| 143 | + return P__::serial_write( octFileIsOpen , dest , vect , N , out_file , out_file_link , owner); |
| 144 | + } |
| 145 | + |
| 146 | + void datafile_close( void ){ |
| 147 | + if( octFileIsOpen ){ |
| 148 | + out_file.close() ; |
| 149 | + owner->f_fclose( out_file_link ); |
| 150 | + octFileIsOpen = false; |
| 151 | + } |
| 152 | + }; |
| 153 | + |
| 154 | + ~c2o_datafile_gen(void){ |
| 155 | + datafile_close( ); |
| 156 | + }; |
| 157 | +}; |
| 158 | + |
| 159 | +} |
0 commit comments