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