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