[262] | 1 | |
---|
[92] | 2 | #include "loggers.h" |
---|
[93] | 3 | #include "../osutils.h" |
---|
[92] | 4 | |
---|
| 5 | #include <fcntl.h> |
---|
[95] | 6 | #ifdef WIN32 |
---|
| 7 | #include <io.h> |
---|
| 8 | #endif |
---|
[92] | 9 | |
---|
[254] | 10 | namespace bdm{ |
---|
[162] | 11 | void memlog::itsave ( const char* fname ) { |
---|
| 12 | it_file itf ( fname ); |
---|
[102] | 13 | int i; |
---|
[347] | 14 | string vec_name=""; |
---|
| 15 | int istart, iend; |
---|
[162] | 16 | for ( i=0; i<entries.length();i++ ) { |
---|
[347] | 17 | istart=0; |
---|
| 18 | for (int j=0; j<entries(i).length(); j++){ // same for as in add!!! |
---|
| 19 | vec_name = names(i) + entries(i).name(j); |
---|
| 20 | iend=istart+entries(i).size(j)-1; |
---|
| 21 | itf << Name ( vec_name ) << vectors ( i ).get_cols(istart,iend); |
---|
| 22 | // move start after current end... |
---|
| 23 | istart=iend+1; |
---|
[162] | 24 | } |
---|
[347] | 25 | |
---|
[102] | 26 | } |
---|
| 27 | } |
---|
| 28 | |
---|
[357] | 29 | void memlog::from_setting( const Setting &root ) |
---|
| 30 | { |
---|
| 31 | // TODO tady se natvrdo ocekava existence stringu, nejsou zadne defaulty.. je to tak OK? |
---|
[358] | 32 | string itfilename = (const char*)root["filename"]; |
---|
[357] | 33 | maxlen = root["maxlen"]; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | /*void memlog::to_setting( Setting &root ) const |
---|
| 37 | { |
---|
| 38 | Transport::to_setting( root ); |
---|
| 39 | |
---|
| 40 | Setting &kilometers_setting = root.add("kilometers", Setting::TypeInt ); |
---|
| 41 | kilometers_setting = kilometers; |
---|
| 42 | |
---|
| 43 | UI::save( passengers, root, "passengers" ); |
---|
| 44 | }*/ |
---|
| 45 | |
---|
[92] | 46 | void dirfilelog::init() { |
---|
| 47 | int i,j,k; |
---|
| 48 | int nsc=0; |
---|
[270] | 49 | for ( i=0;i<entries.length();i++ ) {nsc+=entries ( i )._dsize();} |
---|
[92] | 50 | ; //all entries!! |
---|
| 51 | |
---|
| 52 | char filename[200]; |
---|
| 53 | char num[3]; |
---|
| 54 | |
---|
| 55 | // Initialize directory |
---|
[162] | 56 | makedir ( dirname ); // |
---|
[92] | 57 | |
---|
| 58 | // directory OK, or it can be overwritten |
---|
| 59 | |
---|
| 60 | //Create filenames |
---|
| 61 | scalarnames.set_length ( nsc ); |
---|
| 62 | // For all entries |
---|
| 63 | int ii=0; |
---|
| 64 | for ( i=0;i<entries.length();i++ ) { //for entries |
---|
| 65 | for ( j=0;j<entries ( i ).length();j++ ) { //for RVs in entries |
---|
| 66 | int rvsize = entries ( i ).size ( j ); |
---|
[230] | 67 | // for non-empty names |
---|
| 68 | if (names(i).length()>0) {scalarnames(ii)=names(i) + "_";} |
---|
| 69 | // add name |
---|
| 70 | scalarnames ( ii ) += entries ( i ).name ( j ); |
---|
| 71 | // add number when needed |
---|
| 72 | if ( rvsize >1 ) { |
---|
| 73 | // copy the same name for th whole RV |
---|
| 74 | for ( k=1;k<rvsize;k++ ) {scalarnames(ii+k)=scalarnames(ii);} |
---|
| 75 | // add numbers |
---|
[92] | 76 | for ( k=0;k<rvsize;k++ ) { //for all scalars in given RV |
---|
| 77 | sprintf ( num,"%d",k ); |
---|
[230] | 78 | scalarnames ( ii ) += (std::string)"_" + num; |
---|
[92] | 79 | ii++; |
---|
| 80 | } |
---|
[357] | 81 | } |
---|
| 82 | else { |
---|
| 83 | ii++;} |
---|
[92] | 84 | } |
---|
| 85 | } |
---|
| 86 | //Create format |
---|
| 87 | string frm="format"; |
---|
| 88 | get_fname ( filename, dirname, frm ); |
---|
| 89 | |
---|
| 90 | FILE* fpf; |
---|
| 91 | fpf = fopen ( filename, "w" ); |
---|
| 92 | for ( i=0;i<ii;i++ ) { |
---|
| 93 | fprintf ( fpf, "%s RAW d 1\n", scalarnames ( i ).c_str() ); |
---|
| 94 | } |
---|
| 95 | fclose ( fpf ); |
---|
| 96 | |
---|
| 97 | memlog::init(); |
---|
| 98 | |
---|
[162] | 99 | //Delete old data |
---|
[92] | 100 | write_buffers ( 0 ); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | void dirfilelog::write_buffers ( int Len ) { |
---|
| 105 | int fp; |
---|
| 106 | int nen=entries.length(); //all entries!! |
---|
| 107 | double *Dt; |
---|
| 108 | int DtRows; |
---|
| 109 | int i,j,k; |
---|
| 110 | int ii; //index in scalarnames; |
---|
| 111 | int jj; //index in vectors() |
---|
| 112 | char filename[200]; |
---|
| 113 | |
---|
| 114 | it_assert_debug ( Len<=maxlen,"diffilelog" ); |
---|
| 115 | |
---|
| 116 | ii = 0; |
---|
| 117 | for ( i=0;i<nen;i++ ) { //for entries |
---|
| 118 | Dt = vectors ( i )._data(); |
---|
[162] | 119 | DtRows = vectors ( i ).rows(); |
---|
| 120 | |
---|
[92] | 121 | jj=0; |
---|
| 122 | for ( j=0;j<entries ( i ).length();j++ ) { //for RVs in entries |
---|
| 123 | int rvsize = entries ( i ).size ( j ); |
---|
| 124 | for ( k=0;k<rvsize;k++ ) { { //for all scalars in given RV |
---|
| 125 | get_fname ( filename,dirname,scalarnames ( ii ) ); |
---|
| 126 | if ( Len==0 ) //initialization |
---|
| 127 | fp = open ( filename, O_CREAT | O_WRONLY | O_TRUNC, 00644 ); |
---|
| 128 | else |
---|
| 129 | fp = open ( filename, O_CREAT | O_WRONLY | O_APPEND, 00644 ); |
---|
| 130 | |
---|
[162] | 131 | write ( fp,&Dt[jj* ( DtRows ) ], ( Len ) *sizeof ( double ) ); |
---|
[92] | 132 | close ( fp ); |
---|
| 133 | |
---|
| 134 | //next row in Dt |
---|
| 135 | jj++; |
---|
| 136 | //next scalarname |
---|
| 137 | ii++; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | |
---|
[162] | 144 | void dirfilelog::step ( ) { |
---|
[92] | 145 | |
---|
[162] | 146 | if ( ind== ( maxlen -1 ) ) { |
---|
| 147 | write_buffers ( ind+1 ); |
---|
| 148 | ind = 0; |
---|
| 149 | } |
---|
| 150 | else |
---|
| 151 | ind++; |
---|
[92] | 152 | |
---|
| 153 | } |
---|
[162] | 154 | |
---|
| 155 | void dirfilelog::finalize ( ) { |
---|
| 156 | if ( ind>0 ) |
---|
| 157 | write_buffers ( ind ); //assuming here that i+1 was not filled |
---|
| 158 | } |
---|
[254] | 159 | |
---|
[357] | 160 | |
---|
| 161 | void dirfilelog::from_setting( const Setting &root ) |
---|
| 162 | { |
---|
| 163 | // TODO tady se natvrdo ocekava existence stringu, nejsou zadne defaulty.. je to tak OK? |
---|
[358] | 164 | dirname = (const char*)root["dirname"]; |
---|
[357] | 165 | maxlen = root["maxlen"]; |
---|
| 166 | scalarnames.set_length(0); |
---|
[262] | 167 | } |
---|
[357] | 168 | |
---|
| 169 | /*void dirfilelog::to_setting( Setting &root ) const |
---|
| 170 | { |
---|
| 171 | Transport::to_setting( root ); |
---|
| 172 | |
---|
| 173 | Setting &kilometers_setting = root.add("kilometers", Setting::TypeInt ); |
---|
| 174 | kilometers_setting = kilometers; |
---|
| 175 | |
---|
| 176 | UI::save( passengers, root, "passengers" ); |
---|
| 177 | }*/ |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | } |
---|