1 | #include <itpp/itbase.h> |
---|
2 | #include "loggers.h" |
---|
3 | #include "../osutils.h" |
---|
4 | |
---|
5 | #include <fcntl.h> |
---|
6 | #ifdef WIN32 |
---|
7 | #include <io.h> |
---|
8 | #endif |
---|
9 | |
---|
10 | void memlog::itsave ( const char* fname ) { |
---|
11 | it_file itf ( fname ); |
---|
12 | int i; |
---|
13 | for ( i=0; i<entries.length();i++ ) { |
---|
14 | if ( names ( i ).length() <1 ) { |
---|
15 | std::ostringstream o; |
---|
16 | o<<"Variable"<<i; |
---|
17 | itf << Name ( o.str() ) << vectors ( i ); |
---|
18 | } |
---|
19 | else { |
---|
20 | itf << Name ( names ( i ) ) << vectors ( i ); |
---|
21 | } |
---|
22 | } |
---|
23 | } |
---|
24 | |
---|
25 | void dirfilelog::init() { |
---|
26 | int i,j,k; |
---|
27 | int nsc=0; |
---|
28 | for ( i=0;i<entries.length();i++ ) {nsc+=entries ( i ).count();} |
---|
29 | ; //all entries!! |
---|
30 | |
---|
31 | char filename[200]; |
---|
32 | char num[3]; |
---|
33 | |
---|
34 | // Initialize directory |
---|
35 | makedir ( dirname ); // |
---|
36 | |
---|
37 | // directory OK, or it can be overwritten |
---|
38 | |
---|
39 | //Create filenames |
---|
40 | scalarnames.set_length ( nsc ); |
---|
41 | // For all entries |
---|
42 | int ii=0; |
---|
43 | for ( i=0;i<entries.length();i++ ) { //for entries |
---|
44 | for ( j=0;j<entries ( i ).length();j++ ) { //for RVs in entries |
---|
45 | int rvsize = entries ( i ).size ( j ); |
---|
46 | // for non-empty names |
---|
47 | if (names(i).length()>0) {scalarnames(ii)=names(i) + "_";} |
---|
48 | // add name |
---|
49 | scalarnames ( ii ) += entries ( i ).name ( j ); |
---|
50 | // add number when needed |
---|
51 | if ( rvsize >1 ) { |
---|
52 | // copy the same name for th whole RV |
---|
53 | for ( k=1;k<rvsize;k++ ) {scalarnames(ii+k)=scalarnames(ii);} |
---|
54 | // add numbers |
---|
55 | for ( k=0;k<rvsize;k++ ) { //for all scalars in given RV |
---|
56 | sprintf ( num,"%d",k ); |
---|
57 | scalarnames ( ii ) += (std::string)"_" + num; |
---|
58 | ii++; |
---|
59 | } |
---|
60 | } else { |
---|
61 | ii++;} |
---|
62 | } |
---|
63 | } |
---|
64 | //Create format |
---|
65 | string frm="format"; |
---|
66 | get_fname ( filename, dirname, frm ); |
---|
67 | |
---|
68 | FILE* fpf; |
---|
69 | fpf = fopen ( filename, "w" ); |
---|
70 | for ( i=0;i<ii;i++ ) { |
---|
71 | fprintf ( fpf, "%s RAW d 1\n", scalarnames ( i ).c_str() ); |
---|
72 | } |
---|
73 | fclose ( fpf ); |
---|
74 | |
---|
75 | memlog::init(); |
---|
76 | |
---|
77 | //Delete old data |
---|
78 | write_buffers ( 0 ); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | void dirfilelog::write_buffers ( int Len ) { |
---|
83 | int fp; |
---|
84 | int nen=entries.length(); //all entries!! |
---|
85 | double *Dt; |
---|
86 | int DtRows; |
---|
87 | int i,j,k; |
---|
88 | int ii; //index in scalarnames; |
---|
89 | int jj; //index in vectors() |
---|
90 | char filename[200]; |
---|
91 | |
---|
92 | it_assert_debug ( Len<=maxlen,"diffilelog" ); |
---|
93 | |
---|
94 | ii = 0; |
---|
95 | for ( i=0;i<nen;i++ ) { //for entries |
---|
96 | Dt = vectors ( i )._data(); |
---|
97 | DtRows = vectors ( i ).rows(); |
---|
98 | |
---|
99 | jj=0; |
---|
100 | for ( j=0;j<entries ( i ).length();j++ ) { //for RVs in entries |
---|
101 | int rvsize = entries ( i ).size ( j ); |
---|
102 | for ( k=0;k<rvsize;k++ ) { { //for all scalars in given RV |
---|
103 | get_fname ( filename,dirname,scalarnames ( ii ) ); |
---|
104 | if ( Len==0 ) //initialization |
---|
105 | fp = open ( filename, O_CREAT | O_WRONLY | O_TRUNC, 00644 ); |
---|
106 | else |
---|
107 | fp = open ( filename, O_CREAT | O_WRONLY | O_APPEND, 00644 ); |
---|
108 | |
---|
109 | write ( fp,&Dt[jj* ( DtRows ) ], ( Len ) *sizeof ( double ) ); |
---|
110 | close ( fp ); |
---|
111 | |
---|
112 | //next row in Dt |
---|
113 | jj++; |
---|
114 | //next scalarname |
---|
115 | ii++; |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | void dirfilelog::step ( ) { |
---|
123 | |
---|
124 | if ( ind== ( maxlen -1 ) ) { |
---|
125 | write_buffers ( ind+1 ); |
---|
126 | ind = 0; |
---|
127 | } |
---|
128 | else |
---|
129 | ind++; |
---|
130 | |
---|
131 | } |
---|
132 | |
---|
133 | void dirfilelog::finalize ( ) { |
---|
134 | if ( ind>0 ) |
---|
135 | write_buffers ( ind ); //assuming here that i+1 was not filled |
---|
136 | } |
---|