1 | #include <itpp/itbase.h> |
---|
2 | #include "loggers.h" |
---|
3 | #include "../osutils.h" |
---|
4 | |
---|
5 | #include <fcntl.h> |
---|
6 | |
---|
7 | void dirfilelog::init() { |
---|
8 | int i,j,k; |
---|
9 | int nsc=0; |
---|
10 | for ( i=0;i<entries.length();i++ ) {nsc+=entries ( i ).count();} |
---|
11 | ; //all entries!! |
---|
12 | |
---|
13 | char filename[200]; |
---|
14 | char num[3]; |
---|
15 | |
---|
16 | // Initialize directory |
---|
17 | makedir(dirname); // |
---|
18 | |
---|
19 | // directory OK, or it can be overwritten |
---|
20 | |
---|
21 | //Create filenames |
---|
22 | scalarnames.set_length ( nsc ); |
---|
23 | // For all entries |
---|
24 | int ii=0; |
---|
25 | for ( i=0;i<entries.length();i++ ) { //for entries |
---|
26 | for ( j=0;j<entries ( i ).length();j++ ) { //for RVs in entries |
---|
27 | int rvsize = entries ( i ).size ( j ); |
---|
28 | if ( rvsize ==1 ) { |
---|
29 | scalarnames ( ii ) = names ( i ) + "_" + entries ( i ).name ( j ); |
---|
30 | ii++; |
---|
31 | } else { |
---|
32 | for ( k=0;k<rvsize;k++ ) { //for all scalars in given RV |
---|
33 | sprintf ( num,"%d",k ); |
---|
34 | scalarnames ( ii ) = names ( i ) + "_" + entries ( i ).name ( j ) + "_" + num; |
---|
35 | ii++; |
---|
36 | } |
---|
37 | } |
---|
38 | } |
---|
39 | } |
---|
40 | //Create format |
---|
41 | string frm="format"; |
---|
42 | get_fname ( filename, dirname, frm ); |
---|
43 | |
---|
44 | FILE* fpf; |
---|
45 | fpf = fopen ( filename, "w" ); |
---|
46 | for ( i=0;i<ii;i++ ) { |
---|
47 | fprintf ( fpf, "%s RAW d 1\n", scalarnames ( i ).c_str() ); |
---|
48 | } |
---|
49 | fclose ( fpf ); |
---|
50 | |
---|
51 | memlog::init(); |
---|
52 | |
---|
53 | write_buffers ( 0 ); |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | void dirfilelog::write_buffers ( int Len ) { |
---|
58 | int fp; |
---|
59 | int nen=entries.length(); //all entries!! |
---|
60 | double *Dt; |
---|
61 | int DtRows; |
---|
62 | int i,j,k; |
---|
63 | int ii; //index in scalarnames; |
---|
64 | int jj; //index in vectors() |
---|
65 | char filename[200]; |
---|
66 | |
---|
67 | it_assert_debug ( Len<=maxlen,"diffilelog" ); |
---|
68 | |
---|
69 | ii = 0; |
---|
70 | for ( i=0;i<nen;i++ ) { //for entries |
---|
71 | Dt = vectors ( i )._data(); |
---|
72 | DtRows = vectors(i).rows(); |
---|
73 | |
---|
74 | jj=0; |
---|
75 | for ( j=0;j<entries ( i ).length();j++ ) { //for RVs in entries |
---|
76 | int rvsize = entries ( i ).size ( j ); |
---|
77 | for ( k=0;k<rvsize;k++ ) { { //for all scalars in given RV |
---|
78 | get_fname ( filename,dirname,scalarnames ( ii ) ); |
---|
79 | if ( Len==0 ) //initialization |
---|
80 | fp = open ( filename, O_CREAT | O_WRONLY | O_TRUNC, 00644 ); |
---|
81 | else |
---|
82 | fp = open ( filename, O_CREAT | O_WRONLY | O_APPEND, 00644 ); |
---|
83 | |
---|
84 | write ( fp,&Dt[jj*(DtRows)],(Len)*sizeof ( double ) ); |
---|
85 | close ( fp ); |
---|
86 | |
---|
87 | //next row in Dt |
---|
88 | jj++; |
---|
89 | //next scalarname |
---|
90 | ii++; |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | void dirfilelog::step ( bool final ) { |
---|
98 | |
---|
99 | if ( final ) { |
---|
100 | if ( ind>0 ) |
---|
101 | write_buffers ( ind ); //assuming here that i+1 was not filled |
---|
102 | } else |
---|
103 | if ( ind== ( maxlen -1 ) ) { |
---|
104 | write_buffers ( ind+1 ); |
---|
105 | ind = 0; |
---|
106 | } else |
---|
107 | ind++; |
---|
108 | |
---|
109 | } |
---|